context = $context; $this->nonce_actions = array( self::NONCE_UPDATES, ); } /** * Registers functionality through WordPress hooks. * * @since 1.93.0 */ public function register() { add_filter( 'googlesitekit_rest_routes', function ( $routes ) { return array_merge( $routes, $this->get_rest_routes() ); } ); add_filter( 'googlesitekit_apifetch_preload_paths', function ( $paths ) { return array_merge( $paths, array( '/' . REST_Routes::REST_ROOT . '/core/user/data/nonces', ) ); } ); } /** * Generate nonces for the current user. * * @since 1.93.0 * * @return array List of nonces. */ public function get_nonces() { $nonces = array(); foreach ( $this->nonce_actions as $nonce_action ) { $nonces[ $nonce_action ] = wp_create_nonce( $nonce_action ); } return $nonces; } /** * Gets related REST routes. * * @since 1.93.0 * * @return array List of REST_Route objects. */ private function get_rest_routes() { $can_access_nonces = function () { return is_user_logged_in(); }; return array( new REST_Route( 'core/user/data/nonces', array( array( 'methods' => WP_REST_Server::READABLE, 'callback' => function () { return new WP_REST_Response( $this->get_nonces() ); }, 'permission_callback' => $can_access_nonces, ), ) ), ); } }