post_indexation_action = $post_indexation_action; $this->term_indexation_action = $term_indexation_action; $this->post_type_archive_indexation_action = $post_type_archive_indexation_action; $this->general_indexation_action = $general_indexation_action; $this->indexable_indexing_complete_action = $indexable_indexing_complete_action; $this->indexing_complete_action = $indexing_complete_action; $this->prepare_indexing_action = $prepare_indexing_action; $this->options_helper = $options_helper; $this->post_link_indexing_action = $post_link_indexing_action; $this->term_link_indexing_action = $term_link_indexing_action; $this->indexing_helper = $indexing_helper; } /** * Registers the routes used to index indexables. * * @return void */ public function register_routes() { $route_args = [ 'methods' => 'POST', 'callback' => [ $this, 'index_posts' ], 'permission_callback' => [ $this, 'can_index' ], ]; \register_rest_route( Main::API_V1_NAMESPACE, self::POSTS_ROUTE, $route_args ); $route_args['callback'] = [ $this, 'index_terms' ]; \register_rest_route( Main::API_V1_NAMESPACE, self::TERMS_ROUTE, $route_args ); $route_args['callback'] = [ $this, 'index_post_type_archives' ]; \register_rest_route( Main::API_V1_NAMESPACE, self::POST_TYPE_ARCHIVES_ROUTE, $route_args ); $route_args['callback'] = [ $this, 'index_general' ]; \register_rest_route( Main::API_V1_NAMESPACE, self::GENERAL_ROUTE, $route_args ); $route_args['callback'] = [ $this, 'prepare' ]; \register_rest_route( Main::API_V1_NAMESPACE, self::PREPARE_ROUTE, $route_args ); $route_args['callback'] = [ $this, 'indexables_complete' ]; \register_rest_route( Main::API_V1_NAMESPACE, self::INDEXABLES_COMPLETE_ROUTE, $route_args ); $route_args['callback'] = [ $this, 'complete' ]; \register_rest_route( Main::API_V1_NAMESPACE, self::COMPLETE_ROUTE, $route_args ); $route_args['callback'] = [ $this, 'index_post_links' ]; \register_rest_route( Main::API_V1_NAMESPACE, self::POST_LINKS_INDEXING_ROUTE, $route_args ); $route_args['callback'] = [ $this, 'index_term_links' ]; \register_rest_route( Main::API_V1_NAMESPACE, self::TERM_LINKS_INDEXING_ROUTE, $route_args ); } /** * Indexes a number of unindexed posts. * * @return WP_REST_Response The response. */ public function index_posts() { return $this->run_indexation_action( $this->post_indexation_action, self::FULL_POSTS_ROUTE ); } /** * Indexes a number of unindexed terms. * * @return WP_REST_Response The response. */ public function index_terms() { return $this->run_indexation_action( $this->term_indexation_action, self::FULL_TERMS_ROUTE ); } /** * Indexes a number of unindexed post type archive pages. * * @return WP_REST_Response The response. */ public function index_post_type_archives() { return $this->run_indexation_action( $this->post_type_archive_indexation_action, self::FULL_POST_TYPE_ARCHIVES_ROUTE ); } /** * Indexes a number of unindexed general items. * * @return WP_REST_Response The response. */ public function index_general() { return $this->run_indexation_action( $this->general_indexation_action, self::FULL_GENERAL_ROUTE ); } /** * Indexes a number of posts for post links. * * @return WP_REST_Response The response. */ public function index_post_links() { return $this->run_indexation_action( $this->post_link_indexing_action, self::FULL_POST_LINKS_INDEXING_ROUTE ); } /** * Indexes a number of terms for term links. * * @return WP_REST_Response The response. */ public function index_term_links() { return $this->run_indexation_action( $this->term_link_indexing_action, self::FULL_TERM_LINKS_INDEXING_ROUTE ); } /** * Prepares the indexation. * * @return WP_REST_Response The response. */ public function prepare() { $this->prepare_indexing_action->prepare(); return $this->respond_with( [], false ); } /** * Completes the indexable indexation. * * @return WP_REST_Response The response. */ public function indexables_complete() { $this->indexable_indexing_complete_action->complete(); return $this->respond_with( [], false ); } /** * Completes the indexation. * * @return WP_REST_Response The response. */ public function complete() { $this->indexing_complete_action->complete(); return $this->respond_with( [], false ); } /** * Whether or not the current user is allowed to index. * * @return bool Whether or not the current user is allowed to index. */ public function can_index() { return \current_user_can( 'edit_posts' ); } /** * Runs an indexing action and returns the response. * * @param Indexation_Action_Interface $indexation_action The indexing action. * @param string $url The url of the indexing route. * * @return WP_REST_Response|WP_Error The response, or an error when running the indexing action failed. */ protected function run_indexation_action( Indexation_Action_Interface $indexation_action, $url ) { try { return parent::run_indexation_action( $indexation_action, $url ); } catch ( Exception $exception ) { $this->indexing_helper->indexing_failed(); return new WP_Error( 'wpseo_error_indexing', $exception->getMessage(), [ 'stackTrace' => $exception->getTraceAsString() ] ); } } }