s->post_builder = $post_builder; $this->term_builder = $term_builder; $this->home_page_builder = $home_page_builder; $this->post_type_archive_builder = $post_type_archive_builder; $this->date_archive_builder = $date_archive_builder; $this->system_page_builder = $system_page_builder; $this->hierarchy_builder = $hierarchy_builder; $this->primary_term_builder = $primary_term_builder; $this->indexable_helper = $indexable_helper; $this->version_manager = $version_manager; $this->link_builder = $link_builder; } /** * Sets the indexable repository. Done to avoid circular dependencies. * * @required * * @param Indexable_Repository $indexable_repository The indexable repository. * * @return void */ public function set_indexable_repository( Indexable_Repository $indexable_repository ) { $this->indexable_repository = $indexable_repository; } /** * Creates a clean copy of an Indexable to allow for later database operations. * * @param Indexable $indexable The Indexable to copy. * * @return bool|Indexable */ protected function deep_copy_indexable( $indexable ) { return $this->indexable_repository ->query() ->create( $indexable->as_array() ); } /** * Creates an indexable by its ID and type. * * @param int $object_id The indexable object ID. * @param string $object_type The indexable object type. * @param Indexable|bool $indexable Optional. An existing indexable to overwrite. * * @return bool|Indexable Instance of indexable. False when unable to build. */ public function build_for_id_and_type( $object_id, $object_type, $indexable = false ) { $defaults = [ 'object_type' => $object_type, 'object_id' => $object_id, ]; $indexable = $this->build( $indexable, $defaults ); return $indexable; } /** * Creates an indexable for the homepage. * * @param Indexable|bool $indexable Optional. An existing indexable to overwrite. * * @return Indexable The home page indexable. */ public function build_for_home_page( $indexable = false ) { return $this->build( $indexable, [ 'object_type' => 'home-page' ] ); } /** * Creates an indexable for the date archive. * * @param Indexable|bool $indexable Optional. An existing indexable to overwrite. * * @return Indexable The date archive indexable. */ public function build_for_date_archive( $indexable = false ) { return $this->build( $indexable, [ 'object_type' => 'date-archive' ] ); } /** * Creates an indexable for a post type archive. * * @param string $post_type The post type. * @param Indexable|bool $indexable Optional. An existing indexable to overwrite. * * @return Indexable The post type archive indexable. */ public function build_for_post_type_archive( $post_type, $indexable = false ) { $defaults = [ 'object_type' => 'post-type-archive', 'object_sub_type' => $post_type, ]; return $this->build( $indexable, $defaults ); } /** * Creates an indexable for a system page. * * @param string $page_type The type of system page. * @param Indexable|bool $indexable Optional. An existing indexable to overwrite. * * @return Indexable The search result indexable. */ public function build_for_system_page( $page_type, $indexable = false ) { $defaults = [ 'object_type' => 'system-page', 'object_sub_type' => $page_type, ]; return $this->build( $indexable, $defaults ); } /** * Ensures we have a valid indexable. Creates one if false is passed. * * @param Indexable|false $indexable The indexable. * @param array $defaults The initial properties of the Indexable. * * @return Indexable The indexable. */ protected function ensure_indexable( $indexable, $defaults = [] ) { if ( ! $indexable ) { return $this->indexable_repository->query()->create( $defaults ); } return $indexable; } /** * Build and author indexable from an author id if it does not exist yet, or if the author indexable needs to be upgraded. * * @param int $author_id The author id. * * @return Indexable|false The author indexable if it has been built, `false` if it could not be built. */ protected function maybe_build_author_indexable( $author_id ) { $author_indexable = $this->indexable_repository->find_by_id_and_type( $author_id, 'user', false ); if ( ! $author_indexable || $this->version_manager->indexable_needs_upgrade( $author_indexable ) ) { // Try to build the author. $author_defaults = [ 'object_type' => 'user', 'object_id' => $author_id, ]; $author_indexable = $this->build( $author_indexable, $author_defaults ); } return $author_indexable; } /** * Checks if the indexable type is one that is not supposed to have object ID for. * * @param string $type The type of the indexable. * * @return bool Whether the indexable type is one that is not supposed to have object ID for. */ protected function is_type_with_no_id( $type ) { return \in_array( $type, [ 'home-page', 'date-archive', 'post-type-archive', 'system-page' ], true ); } // phpcs:disable Squiz.Commenting.FunctionCommentThrowTag.Missing -- Exceptions are handled by the catch statement in the method. /** * Rebuilds an Indexable from scratch. * * @param Indexable $indexable The Indexable to (re)build. * @param array|null $defaults The object type of the Indexable. * * @return Indexable|false The resulting Indexable. */ public function build( $indexable, $defaults = null ) { // Backup the previous Indexable, if there was one. $indexable_before = ( $indexable ) ? $this->deep_copy_indexable( $indexable ) : null; // Make sure we have an Indexable to work with. $indexable = $this->ensure_indexable( $indexable, $defaults ); try { if ( $indexable->object_id === 0 ) { throw Not_Built_Exception::invalid_object_id( $indexable->object_id ); } switch ( $indexable->object_type ) { case 'post': $indexable = $this->post_builder->build( $indexable->object_id, $indexable ); // Save indexable, to make sure it can be queried when building related objects like the author indexable and hierarchy. $indexable = $this->indexable_helper->save_indexable( $indexable, $indexable_before ); // For attachments, we have to make sure to patch any potentially previously cleaned up SEO links. if ( \is_a( $indexable, Indexable::class ) && $indexable->object_sub_type === 'attachment' ) { $this->link_builder->patch_seo_links( $indexable ); } // Always rebuild the primary term. $this->primary_term_builder->build( $indexable->object_id ); // Always rebuild the hierarchy; this needs the primary term to run correctly. $this->hierarchy_builder->build( $indexable ); $this->maybe_build_author_indexable( $indexable->author_id ); // The indexable is already saved, so return early. return $indexable; case 'user': $indexable = $this->author_builder->build( $indexable->object_id, $indexable ); break; case 'term': $indexable = $this->term_builder->build( $indexable->object_id, $indexable ); // Save indexable, to make sure it can be queried when building hierarchy. $indexable = $this->indexable_helper->save_indexable( $indexable, $indexable_before ); $this->hierarchy_builder->build( $indexable ); // The indexable is already saved, so return early. return $indexable; case 'home-page': $indexable = $this->home_page_builder->build( $indexable ); break; case 'date-archive': $indexable = $this->date_archive_builder->build( $indexable ); break; case 'post-type-archive': $indexable = $this->post_type_archive_builder->build( $indexable->object_sub_type, $indexable ); break; case 'system-page': $indexable = $this->system_page_builder->build( $indexable->object_sub_type, $indexable ); break; } return $this->indexable_helper->save_indexable( $indexable, $indexable_before ); } catch ( Source_Exception $exception ) { if ( ! $this->is_type_with_no_id( $indexable->object_type ) && ! isset( $indexable->object_id ) ) { return false; } /** * The current indexable could not be indexed. Create a placeholder indexable, so we can * skip this indexable in future indexing runs. * * @var Indexable $indexable */ $indexable = $this->ensure_indexable( $indexable, [ 'object_id' => $indexable->object_id, 'object_type' => $indexable->object_type, 'post_status' => 'unindexed', 'version' => 0, ] ); // If we already had an existing indexable, mark it as unindexed. We cannot rely on its validity anymore. $indexable->post_status = 'unindexed'; // Make sure that the indexing process doesn't get stuck in a loop on this broken indexable. $indexable = $this->version_manager->set_latest( $indexable ); return $this->indexable_helper->save_indexable( $indexable, $indexable_before ); } catch ( Not_Built_Exception $exception ) { return false; } } // phpcs:enable } s->post_builder = $post_builder; $this->term_builder = $term_builder; $this->home_page_builder = $home_page_builder; $this->post_type_archive_builder = $post_type_archive_builder; $this->date_archive_builder = $date_archive_builder; $this->system_page_builder = $system_page_builder; $this->hierarchy_builder = $hierarchy_builder; $this->primary_term_builder = $primary_term_builder; $this->indexable_helper = $indexable_helper; $this->version_manager = $version_manager; $this->link_builder = $link_builder; } /** * Sets the indexable repository. Done to avoid circular dependencies. * * @required * * @param Indexable_Repository $indexable_repository The indexable repository. * * @return void */ public function set_indexable_repository( Indexable_Repository $indexable_repository ) { $this->indexable_repository = $indexable_repository; } /** * Creates a clean copy of an Indexable to allow for later database operations. * * @param Indexable $indexable The Indexable to copy. * * @return bool|Indexable */ protected function deep_copy_indexable( $indexable ) { return $this->indexable_repository ->query() ->create( $indexable->as_array() ); } /** * Creates an indexable by its ID and type. * * @param int $object_id The indexable object ID. * @param string $object_type The indexable object type. * @param Indexable|bool $indexable Optional. An existing indexable to overwrite. * * @return bool|Indexable Instance of indexable. False when unable to build. */ public function build_for_id_and_type( $object_id, $object_type, $indexable = false ) { $defaults = [ 'object_type' => $object_type, 'object_id' => $object_id, ]; $indexable = $this->build( $indexable, $defaults ); return $indexable; } /** * Creates an indexable for the homepage. * * @param Indexable|bool $indexable Optional. An existing indexable to overwrite. * * @return Indexable The home page indexable. */ public function build_for_home_page( $indexable = false ) { return $this->build( $indexable, [ 'object_type' => 'home-page' ] ); } /** * Creates an indexable for the date archive. * * @param Indexable|bool $indexable Optional. An existing indexable to overwrite. * * @return Indexable The date archive indexable. */ public function build_for_date_archive( $indexable = false ) { return $this->build( $indexable, [ 'object_type' => 'date-archive' ] ); } /** * Creates an indexable for a post type archive. * * @param string $post_type The post type. * @param Indexable|bool $indexable Optional. An existing indexable to overwrite. * * @return Indexable The post type archive indexable. */ public function build_for_post_type_archive( $post_type, $indexable = false ) { $defaults = [ 'object_type' => 'post-type-archive', 'object_sub_type' => $post_type, ]; return $this->build( $indexable, $defaults ); } /** * Creates an indexable for a system page. * * @param string $page_type The type of system page. * @param Indexable|bool $indexable Optional. An existing indexable to overwrite. * * @return Indexable The search result indexable. */ public function build_for_system_page( $page_type, $indexable = false ) { $defaults = [ 'object_type' => 'system-page', 'object_sub_type' => $page_type, ]; return $this->build( $indexable, $defaults ); } /** * Ensures we have a valid indexable. Creates one if false is passed. * * @param Indexable|false $indexable The indexable. * @param array $defaults The initial properties of the Indexable. * * @return Indexable The indexable. */ protected function ensure_indexable( $indexable, $defaults = [] ) { if ( ! $indexable ) { return $this->indexable_repository->query()->create( $defaults ); } return $indexable; } /** * Build and author indexable from an author id if it does not exist yet, or if the author indexable needs to be upgraded. * * @param int $author_id The author id. * * @return Indexable|false The author indexable if it has been built, `false` if it could not be built. */ protected function maybe_build_author_indexable( $author_id ) { $author_indexable = $this->indexable_repository->find_by_id_and_type( $author_id, 'user', false ); if ( ! $author_indexable || $this->version_manager->indexable_needs_upgrade( $author_indexable ) ) { // Try to build the author. $author_defaults = [ 'object_type' => 'user', 'object_id' => $author_id, ]; $author_indexable = $this->build( $author_indexable, $author_defaults ); } return $author_indexable; } /** * Checks if the indexable type is one that is not supposed to have object ID for. * * @param string $type The type of the indexable. * * @return bool Whether the indexable type is one that is not supposed to have object ID for. */ protected function is_type_with_no_id( $type ) { return \in_array( $type, [ 'home-page', 'date-archive', 'post-type-archive', 'system-page' ], true ); } // phpcs:disable Squiz.Commenting.FunctionCommentThrowTag.Missing -- Exceptions are handled by the catch statement in the method. /** * Rebuilds an Indexable from scratch. * * @param Indexable $indexable The Indexable to (re)build. * @param array|null $defaults The object type of the Indexable. * * @return Indexable|false The resulting Indexable. */ public function build( $indexable, $defaults = null ) { // Backup the previous Indexable, if there was one. $indexable_before = ( $indexable ) ? $this->deep_copy_indexable( $indexable ) : null; // Make sure we have an Indexable to work with. $indexable = $this->ensure_indexable( $indexable, $defaults ); try { if ( $indexable->object_id === 0 ) { throw Not_Built_Exception::invalid_object_id( $indexable->object_id ); } switch ( $indexable->object_type ) { case 'post': $indexable = $this->post_builder->build( $indexable->object_id, $indexable ); // Save indexable, to make sure it can be queried when building related objects like the author indexable and hierarchy. $indexable = $this->indexable_helper->save_indexable( $indexable, $indexable_before ); // For attachments, we have to make sure to patch any potentially previously cleaned up SEO links. if ( \is_a( $indexable, Indexable::class ) && $indexable->object_sub_type === 'attachment' ) { $this->link_builder->patch_seo_links( $indexable ); } // Always rebuild the primary term. $this->primary_term_builder->build( $indexable->object_id ); // Always rebuild the hierarchy; this needs the primary term to run correctly. $this->hierarchy_builder->build( $indexable ); $this->maybe_build_author_indexable( $indexable->author_id ); // The indexable is already saved, so return early. return $indexable; case 'user': $indexable = $this->author_builder->build( $indexable->object_id, $indexable ); break; case 'term': $indexable = $this->term_builder->build( $indexable->object_id, $indexable ); // Save indexable, to make sure it can be queried when building hierarchy. $indexable = $this->indexable_helper->save_indexable( $indexable, $indexable_before ); $this->hierarchy_builder->build( $indexable ); // The indexable is already saved, so return early. return $indexable; case 'home-page': $indexable = $this->home_page_builder->build( $indexable ); break; case 'date-archive': $indexable = $this->date_archive_builder->build( $indexable ); break; case 'post-type-archive': $indexable = $this->post_type_archive_builder->build( $indexable->object_sub_type, $indexable ); break; case 'system-page': $indexable = $this->system_page_builder->build( $indexable->object_sub_type, $indexable ); break; } return $this->indexable_helper->save_indexable( $indexable, $indexable_before ); } catch ( Source_Exception $exception ) { if ( ! $this->is_type_with_no_id( $indexable->object_type ) && ! isset( $indexable->object_id ) ) { return false; } /** * The current indexable could not be indexed. Create a placeholder indexable, so we can * skip this indexable in future indexing runs. * * @var Indexable $indexable */ $indexable = $this->ensure_indexable( $indexable, [ 'object_id' => $indexable->object_id, 'object_type' => $indexable->object_type, 'post_status' => 'unindexed', 'version' => 0, ] ); // If we already had an existing indexable, mark it as unindexed. We cannot rely on its validity anymore. $indexable->post_status = 'unindexed'; // Make sure that the indexing process doesn't get stuck in a loop on this broken indexable. $indexable = $this->version_manager->set_latest( $indexable ); return $this->indexable_helper->save_indexable( $indexable, $indexable_before ); } catch ( Not_Built_Exception $exception ) { return false; } } // phpcs:enable } s->post_builder = $post_builder; $this->term_builder = $term_builder; $this->home_page_builder = $home_page_builder; $this->post_type_archive_builder = $post_type_archive_builder; $this->date_archive_builder = $date_archive_builder; $this->system_page_builder = $system_page_builder; $this->hierarchy_builder = $hierarchy_builder; $this->primary_term_builder = $primary_term_builder; $this->indexable_helper = $indexable_helper; $this->version_manager = $version_manager; $this->link_builder = $link_builder; } /** * Sets the indexable repository. Done to avoid circular dependencies. * * @required * * @param Indexable_Repository $indexable_repository The indexable repository. * * @return void */ public function set_indexable_repository( Indexable_Repository $indexable_repository ) { $this->indexable_repository = $indexable_repository; } /** * Creates a clean copy of an Indexable to allow for later database operations. * * @param Indexable $indexable The Indexable to copy. * * @return bool|Indexable */ protected function deep_copy_indexable( $indexable ) { return $this->indexable_repository ->query() ->create( $indexable->as_array() ); } /** * Creates an indexable by its ID and type. * * @param int $object_id The indexable object ID. * @param string $object_type The indexable object type. * @param Indexable|bool $indexable Optional. An existing indexable to overwrite. * * @return bool|Indexable Instance of indexable. False when unable to build. */ public function build_for_id_and_type( $object_id, $object_type, $indexable = false ) { $defaults = [ 'object_type' => $object_type, 'object_id' => $object_id, ]; $indexable = $this->build( $indexable, $defaults ); return $indexable; } /** * Creates an indexable for the homepage. * * @param Indexable|bool $indexable Optional. An existing indexable to overwrite. * * @return Indexable The home page indexable. */ public function build_for_home_page( $indexable = false ) { return $this->build( $indexable, [ 'object_type' => 'home-page' ] ); } /** * Creates an indexable for the date archive. * * @param Indexable|bool $indexable Optional. An existing indexable to overwrite. * * @return Indexable The date archive indexable. */ public function build_for_date_archive( $indexable = false ) { return $this->build( $indexable, [ 'object_type' => 'date-archive' ] ); } /** * Creates an indexable for a post type archive. * * @param string $post_type The post type. * @param Indexable|bool $indexable Optional. An existing indexable to overwrite. * * @return Indexable The post type archive indexable. */ public function build_for_post_type_archive( $post_type, $indexable = false ) { $defaults = [ 'object_type' => 'post-type-archive', 'object_sub_type' => $post_type, ]; return $this->build( $indexable, $defaults ); } /** * Creates an indexable for a system page. * * @param string $page_type The type of system page. * @param Indexable|bool $indexable Optional. An existing indexable to overwrite. * * @return Indexable The search result indexable. */ public function build_for_system_page( $page_type, $indexable = false ) { $defaults = [ 'object_type' => 'system-page', 'object_sub_type' => $page_type, ]; return $this->build( $indexable, $defaults ); } /** * Ensures we have a valid indexable. Creates one if false is passed. * * @param Indexable|false $indexable The indexable. * @param array $defaults The initial properties of the Indexable. * * @return Indexable The indexable. */ protected function ensure_indexable( $indexable, $defaults = [] ) { if ( ! $indexable ) { return $this->indexable_repository->query()->create( $defaults ); } return $indexable; } /** * Build and author indexable from an author id if it does not exist yet, or if the author indexable needs to be upgraded. * * @param int $author_id The author id. * * @return Indexable|false The author indexable if it has been built, `false` if it could not be built. */ protected function maybe_build_author_indexable( $author_id ) { $author_indexable = $this->indexable_repository->find_by_id_and_type( $author_id, 'user', false ); if ( ! $author_indexable || $this->version_manager->indexable_needs_upgrade( $author_indexable ) ) { // Try to build the author. $author_defaults = [ 'object_type' => 'user', 'object_id' => $author_id, ]; $author_indexable = $this->build( $author_indexable, $author_defaults ); } return $author_indexable; } /** * Checks if the indexable type is one that is not supposed to have object ID for. * * @param string $type The type of the indexable. * * @return bool Whether the indexable type is one that is not supposed to have object ID for. */ protected function is_type_with_no_id( $type ) { return \in_array( $type, [ 'home-page', 'date-archive', 'post-type-archive', 'system-page' ], true ); } // phpcs:disable Squiz.Commenting.FunctionCommentThrowTag.Missing -- Exceptions are handled by the catch statement in the method. /** * Rebuilds an Indexable from scratch. * * @param Indexable $indexable The Indexable to (re)build. * @param array|null $defaults The object type of the Indexable. * * @return Indexable|false The resulting Indexable. */ public function build( $indexable, $defaults = null ) { // Backup the previous Indexable, if there was one. $indexable_before = ( $indexable ) ? $this->deep_copy_indexable( $indexable ) : null; // Make sure we have an Indexable to work with. $indexable = $this->ensure_indexable( $indexable, $defaults ); try { if ( $indexable->object_id === 0 ) { throw Not_Built_Exception::invalid_object_id( $indexable->object_id ); } switch ( $indexable->object_type ) { case 'post': $indexable = $this->post_builder->build( $indexable->object_id, $indexable ); // Save indexable, to make sure it can be queried when building related objects like the author indexable and hierarchy. $indexable = $this->indexable_helper->save_indexable( $indexable, $indexable_before ); // For attachments, we have to make sure to patch any potentially previously cleaned up SEO links. if ( \is_a( $indexable, Indexable::class ) && $indexable->object_sub_type === 'attachment' ) { $this->link_builder->patch_seo_links( $indexable ); } // Always rebuild the primary term. $this->primary_term_builder->build( $indexable->object_id ); // Always rebuild the hierarchy; this needs the primary term to run correctly. $this->hierarchy_builder->build( $indexable ); $this->maybe_build_author_indexable( $indexable->author_id ); // The indexable is already saved, so return early. return $indexable; case 'user': $indexable = $this->author_builder->build( $indexable->object_id, $indexable ); break; case 'term': $indexable = $this->term_builder->build( $indexable->object_id, $indexable ); // Save indexable, to make sure it can be queried when building hierarchy. $indexable = $this->indexable_helper->save_indexable( $indexable, $indexable_before ); $this->hierarchy_builder->build( $indexable ); // The indexable is already saved, so return early. return $indexable; case 'home-page': $indexable = $this->home_page_builder->build( $indexable ); break; case 'date-archive': $indexable = $this->date_archive_builder->build( $indexable ); break; case 'post-type-archive': $indexable = $this->post_type_archive_builder->build( $indexable->object_sub_type, $indexable ); break; case 'system-page': $indexable = $this->system_page_builder->build( $indexable->object_sub_type, $indexable ); break; } return $this->indexable_helper->save_indexable( $indexable, $indexable_before ); } catch ( Source_Exception $exception ) { if ( ! $this->is_type_with_no_id( $indexable->object_type ) && ! isset( $indexable->object_id ) ) { return false; } /** * The current indexable could not be indexed. Create a placeholder indexable, so we can * skip this indexable in future indexing runs. * * @var Indexable $indexable */ $indexable = $this->ensure_indexable( $indexable, [ 'object_id' => $indexable->object_id, 'object_type' => $indexable->object_type, 'post_status' => 'unindexed', 'version' => 0, ] ); // If we already had an existing indexable, mark it as unindexed. We cannot rely on its validity anymore. $indexable->post_status = 'unindexed'; // Make sure that the indexing process doesn't get stuck in a loop on this broken indexable. $indexable = $this->version_manager->set_latest( $indexable ); return $this->indexable_helper->save_indexable( $indexable, $indexable_before ); } catch ( Not_Built_Exception $exception ) { return false; } } // phpcs:enable } if ( ! isset( $_GET['page'] ) || ( $_GET['page'] !== 'wpseo_dashboard' && $_GET['page'] !== General_Page_Integration::PAGE ) || \is_network_admin() ) { return; } $this->admin_asset_manager->enqueue_script( 'indexation' ); $this->admin_asset_manager->enqueue_style( 'first-time-configuration' ); $this->admin_asset_manager->enqueue_style( 'admin-css' ); $this->admin_asset_manager->enqueue_style( 'monorepo' ); $data = [ 'disabled' => ! \YoastSEO()->helpers->indexable->should_index_indexables(), 'amount' => \YoastSEO()->helpers->indexing->get_filtered_unindexed_count(), 'firstTime' => ( \YoastSEO()->helpers->indexing->is_initial_indexing() === true ), 'errorMessage' => '', 'restApi' => [ 'root' => \esc_url_raw( \rest_url() ), 'indexing_endpoints' => $this->get_endpoints(), 'nonce' => \wp_create_nonce( 'wp_rest' ), ], ]; /** * Filter: 'wpseo_indexing_data' Filter to adapt the data used in the indexing process. * * @param array $data The indexing data to adapt. */ $data = \apply_filters( 'wpseo_indexing_data', $data ); $this->admin_asset_manager->localize_script( 'indexation', 'yoastIndexingData', $data ); $person_id = $this->get_person_id(); $social_profiles = $this->get_social_profiles(); // This filter is documented in admin/views/tabs/metas/paper-content/general/knowledge-graph.php. $knowledge_graph_message = \apply_filters( 'wpseo_knowledge_graph_setting_msg', '' ); $finished_steps = $this->get_finished_steps(); $options = $this->get_company_or_person_options(); $selected_option_label = ''; $filtered_options = \array_filter( $options, function ( $item ) { return $item['value'] === $this->is_company_or_person(); } ); $selected_option = \reset( $filtered_options ); if ( \is_array( $selected_option ) ) { $selected_option_label = $selected_option['label']; } $data_ftc = [ 'canEditUser' => $this->can_edit_profile( $person_id ), 'companyOrPerson' => $this->is_company_or_person(), 'companyOrPersonLabel' => $selected_option_label, 'companyName' => $this->get_company_name(), 'fallbackCompanyName' => $this->get_fallback_company_name( $this->get_company_name() ), 'websiteName' => $this->get_website_name(), 'fallbackWebsiteName' => $this->get_fallback_website_name( $this->get_website_name() ), 'companyLogo' => $this->get_company_logo(), 'companyLogoFallback' => $this->get_company_fallback_logo( $this->get_company_logo() ), 'companyLogoId' => $this->get_person_logo_id(), 'finishedSteps' => $finished_steps, 'personId' => (int) $person_id, 'personName' => $this->get_person_name(), 'personLogo' => $this->get_person_logo(), 'personLogoFallback' => $this->get_person_fallback_logo( $this->get_person_logo() ), 'personLogoId' => $this->get_person_logo_id(), 'siteTagline' => $this->get_site_tagline(), 'socialProfiles' => [ 'facebookUrl' => $social_profiles['facebook_site'], 'twitterUsername' => $social_profiles['twitter_site'], 'otherSocialUrls' => $social_profiles['other_social_urls'], ], 'isPremium' => $this->product_helper->is_premium(), 'isWooCommerceActive' => $this->woocommerce_helper->is_active(), 'isWooCommerceSeoActive' => $this->is_wooseo_active(), 'tracking' => $this->has_tracking_enabled(), 'isTrackingAllowedMultisite' => $this->is_tracking_enabled_multisite(), 'isMainSite' => $this->is_main_site(), 'companyOrPersonOptions' => $options, 'shouldForceCompany' => $this->should_force_company(), 'knowledgeGraphMessage' => $knowledge_graph_message, 'shortlinks' => [ 'gdpr' => $this->shortlinker->build_shortlink( 'https://yoa.st/gdpr-config-workout' ), 'configIndexables' => $this->shortlinker->build_shortlink( 'https://yoa.st/config-indexables' ), 'configIndexablesBenefits' => $this->shortlinker->build_shortlink( 'https://yoa.st/config-indexables-benefits' ), 'indexationLearnMore' => $this->shortlinker->build_shortlink( 'https://yoa.st/ftc-indexation-premium-learn-more' ), 'reprWoocommerceLearnMore' => $this->shortlinker->build_shortlink( 'https://yoa.st/ftc-representation-wooseo-learn-more' ), 'reprLocalLearnMore' => $this->shortlinker->build_shortlink( 'https://yoa.st/ftc-representation-local-learn-more' ), 'finishLearnMore' => $this->shortlinker->build_shortlink( 'https://yoa.st/ftc-finish-premium-learn-more' ), ], ]; $this->admin_asset_manager->localize_script( 'general-page', 'wpseoFirstTimeConfigurationData', $data_ftc ); } /** * Retrieves a list of the endpoints to use. * * @return array The endpoints. */ protected function get_endpoints() { $endpoints = [ 'prepare' => Indexing_Route::FULL_PREPARE_ROUTE, 'terms' => Indexing_Route::FULL_TERMS_ROUTE, 'posts' => Indexing_Route::FULL_POSTS_ROUTE, 'archives' => Indexing_Route::FULL_POST_TYPE_ARCHIVES_ROUTE, 'general' => Indexing_Route::FULL_GENERAL_ROUTE, 'indexablesComplete' => Indexing_Route::FULL_INDEXABLES_COMPLETE_ROUTE, 'post_link' => Indexing_Route::FULL_POST_LINKS_INDEXING_ROUTE, 'term_link' => Indexing_Route::FULL_TERM_LINKS_INDEXING_ROUTE, ]; $endpoints = \apply_filters( 'wpseo_indexing_endpoints', $endpoints ); $endpoints['complete'] = Indexing_Route::FULL_COMPLETE_ROUTE; return $endpoints; } // ** Private functions ** // /** * Returns the finished steps array. * * @return array An array with the finished steps. */ private function get_finished_steps() { return $this->options_helper->get( 'configuration_finished_steps', [] ); } /** * Returns the entity represented by the site. * * @return string The entity represented by the site. */ private function is_company_or_person() { return $this->options_helper->get( 'company_or_person', '' ); } /** * Gets the company name from the option in the database. * * @return string The company name. */ private function get_company_name() { return $this->options_helper->get( 'company_name', '' ); } /** * Gets the fallback company name from the option in the database if there is no company name. * * @param string $company_name The given company name by the user, default empty string. * * @return string|false The company name. */ private function get_fallback_company_name( $company_name ) { if ( $company_name ) { return false; } return \get_bloginfo( 'name' ); } /** * Gets the website name from the option in the database. * * @return string The website name. */ private function get_website_name() { return $this->options_helper->get( 'website_name', '' ); } /** * Gets the fallback website name from the option in the database if there is no website name. * * @param string $website_name The given website name by the user, default empty string. * * @return string|false The website name. */ private function get_fallback_website_name( $website_name ) { if ( $website_name ) { return false; } return \get_bloginfo( 'name' ); } /** * Gets the company logo from the option in the database. * * @return string The company logo. */ private function get_company_logo() { return $this->options_helper->get( 'company_logo', '' ); } /** * Gets the company logo id from the option in the database. * * @return string The company logo id. */ private function get_company_logo_id() { return $this->options_helper->get( 'company_logo_id', '' ); } /** * Gets the company logo url from the option in the database. * * @param string $company_logo The given company logo by the user, default empty. * * @return string|false The company logo URL. */ private function get_company_fallback_logo( $company_logo ) { if ( $company_logo ) { return false; } $logo_id = $this->meta_tags_context->fallback_to_site_logo(); return \esc_url( \wp_get_attachment_url( $logo_id ) ); } /** * Gets the person id from the option in the database. * * @return int|null The person id, null if empty. */ private function get_person_id() { return $this->options_helper->get( 'company_or_person_user_id' ); } /** * Gets the person id from the option in the database. * * @return int|null The person id, null if empty. */ private function get_person_name() { $user = \get_userdata( $this->get_person_id() ); if ( $user instanceof WP_User ) { return $user->get( 'display_name' ); } return ''; } /** * Gets the person avatar from the option in the database. * * @return string The person logo. */ private function get_person_logo() { return $this->options_helper->get( 'person_logo', '' ); } /** * Gets the person logo url from the option in the database. * * @param string $person_logo The given person logo by the user, default empty. * * @return string|false The person logo URL. */ private function get_person_fallback_logo( $person_logo ) { if ( $person_logo ) { return false; } $logo_id = $this->meta_tags_context->fallback_to_site_logo(); return \esc_url( \wp_get_attachment_url( $logo_id ) ); } /** * Gets the person logo id from the option in the database. * * @return string The person logo id. */ private function get_person_logo_id() { return $this->options_helper->get( 'person_logo_id', '' ); } /** * Gets the site tagline. * * @return string The site tagline. */ private function get_site_tagline() { return \get_bloginfo( 'description' ); } /** * Gets the social profiles stored in the database. * * @return string[] The social profiles. */ private function get_social_profiles() { return $this->social_profiles_helper->get_organization_social_profiles(); } /** * Checks whether tracking is enabled. * * @return bool True if tracking is enabled, false otherwise, null if in Free and conf. workout step not finished. */ private function has_tracking_enabled() { $default = false; if ( $this->product_helper->is_premium() ) { $default = true; } return $this->options_helper->get( 'tracking', $default ); } /** * Checks whether tracking option is allowed at network level. * * @return bool True if option change is allowed, false otherwise. */ private function is_tracking_enabled_multisite() { $default = true; if ( ! \is_multisite() ) { return $default; } return $this->options_helper->get( 'allow_tracking', $default ); } /** * Checks whether we are in a main site. * * @return bool True if it's the main site or a single site, false if it's a subsite. */ private function is_main_site() { return \is_main_site(); } /** * Gets the options for the Company or Person select. * Returns only the company option if it is forced (by Local SEO), otherwise returns company and person option. * * @return array The options for the company-or-person select. */ private function get_company_or_person_options() { $options = [ [ 'label' => \__( 'Organization', 'wordpress-seo' ), 'value' => 'company', 'id' => 'company', ], [ 'label' => \__( 'Person', 'wordpress-seo' ), 'value' => 'person', 'id' => 'person', ], ]; if ( $this->should_force_company() ) { $options = [ [ 'label' => \__( 'Organization', 'wordpress-seo' ), 'value' => 'company', 'id' => 'company', ], ]; } return $options; } /** * Checks whether we should force "Organization". * * @return bool */ private function should_force_company() { return $this->addon_manager->is_installed( WPSEO_Addon_Manager::LOCAL_SLUG ); } /** * Checks if the current user has the capability to edit a specific user. * * @param int $person_id The id of the person to edit. * * @return bool */ private function can_edit_profile( $person_id ) { return \current_user_can( 'edit_user', $person_id ); } /** * Checks if Yoast WooCommerce SEO is active. * * @return bool */ private function is_wooseo_active() { $addon_manager = new WPSEO_Addon_Manager(); return $addon_manager->is_installed( WPSEO_Addon_Manager::WOOCOMMERCE_SLUG ); } } s->post_builder = $post_builder; $this->term_builder = $term_builder; $this->home_page_builder = $home_page_builder; $this->post_type_archive_builder = $post_type_archive_builder; $this->date_archive_builder = $date_archive_builder; $this->system_page_builder = $system_page_builder; $this->hierarchy_builder = $hierarchy_builder; $this->primary_term_builder = $primary_term_builder; $this->indexable_helper = $indexable_helper; $this->version_manager = $version_manager; $this->link_builder = $link_builder; } /** * Sets the indexable repository. Done to avoid circular dependencies. * * @required * * @param Indexable_Repository $indexable_repository The indexable repository. * * @return void */ public function set_indexable_repository( Indexable_Repository $indexable_repository ) { $this->indexable_repository = $indexable_repository; } /** * Creates a clean copy of an Indexable to allow for later database operations. * * @param Indexable $indexable The Indexable to copy. * * @return bool|Indexable */ protected function deep_copy_indexable( $indexable ) { return $this->indexable_repository ->query() ->create( $indexable->as_array() ); } /** * Creates an indexable by its ID and type. * * @param int $object_id The indexable object ID. * @param string $object_type The indexable object type. * @param Indexable|bool $indexable Optional. An existing indexable to overwrite. * * @return bool|Indexable Instance of indexable. False when unable to build. */ public function build_for_id_and_type( $object_id, $object_type, $indexable = false ) { $defaults = [ 'object_type' => $object_type, 'object_id' => $object_id, ]; $indexable = $this->build( $indexable, $defaults ); return $indexable; } /** * Creates an indexable for the homepage. * * @param Indexable|bool $indexable Optional. An existing indexable to overwrite. * * @return Indexable The home page indexable. */ public function build_for_home_page( $indexable = false ) { return $this->build( $indexable, [ 'object_type' => 'home-page' ] ); } /** * Creates an indexable for the date archive. * * @param Indexable|bool $indexable Optional. An existing indexable to overwrite. * * @return Indexable The date archive indexable. */ public function build_for_date_archive( $indexable = false ) { return $this->build( $indexable, [ 'object_type' => 'date-archive' ] ); } /** * Creates an indexable for a post type archive. * * @param string $post_type The post type. * @param Indexable|bool $indexable Optional. An existing indexable to overwrite. * * @return Indexable The post type archive indexable. */ public function build_for_post_type_archive( $post_type, $indexable = false ) { $defaults = [ 'object_type' => 'post-type-archive', 'object_sub_type' => $post_type, ]; return $this->build( $indexable, $defaults ); } /** * Creates an indexable for a system page. * * @param string $page_type The type of system page. * @param Indexable|bool $indexable Optional. An existing indexable to overwrite. * * @return Indexable The search result indexable. */ public function build_for_system_page( $page_type, $indexable = false ) { $defaults = [ 'object_type' => 'system-page', 'object_sub_type' => $page_type, ]; return $this->build( $indexable, $defaults ); } /** * Ensures we have a valid indexable. Creates one if false is passed. * * @param Indexable|false $indexable The indexable. * @param array $defaults The initial properties of the Indexable. * * @return Indexable The indexable. */ protected function ensure_indexable( $indexable, $defaults = [] ) { if ( ! $indexable ) { return $this->indexable_repository->query()->create( $defaults ); } return $indexable; } /** * Build and author indexable from an author id if it does not exist yet, or if the author indexable needs to be upgraded. * * @param int $author_id The author id. * * @return Indexable|false The author indexable if it has been built, `false` if it could not be built. */ protected function maybe_build_author_indexable( $author_id ) { $author_indexable = $this->indexable_repository->find_by_id_and_type( $author_id, 'user', false ); if ( ! $author_indexable || $this->version_manager->indexable_needs_upgrade( $author_indexable ) ) { // Try to build the author. $author_defaults = [ 'object_type' => 'user', 'object_id' => $author_id, ]; $author_indexable = $this->build( $author_indexable, $author_defaults ); } return $author_indexable; } /** * Checks if the indexable type is one that is not supposed to have object ID for. * * @param string $type The type of the indexable. * * @return bool Whether the indexable type is one that is not supposed to have object ID for. */ protected function is_type_with_no_id( $type ) { return \in_array( $type, [ 'home-page', 'date-archive', 'post-type-archive', 'system-page' ], true ); } // phpcs:disable Squiz.Commenting.FunctionCommentThrowTag.Missing -- Exceptions are handled by the catch statement in the method. /** * Rebuilds an Indexable from scratch. * * @param Indexable $indexable The Indexable to (re)build. * @param array|null $defaults The object type of the Indexable. * * @return Indexable|false The resulting Indexable. */ public function build( $indexable, $defaults = null ) { // Backup the previous Indexable, if there was one. $indexable_before = ( $indexable ) ? $this->deep_copy_indexable( $indexable ) : null; // Make sure we have an Indexable to work with. $indexable = $this->ensure_indexable( $indexable, $defaults ); try { if ( $indexable->object_id === 0 ) { throw Not_Built_Exception::invalid_object_id( $indexable->object_id ); } switch ( $indexable->object_type ) { case 'post': $indexable = $this->post_builder->build( $indexable->object_id, $indexable ); // Save indexable, to make sure it can be queried when building related objects like the author indexable and hierarchy. $indexable = $this->indexable_helper->save_indexable( $indexable, $indexable_before ); // For attachments, we have to make sure to patch any potentially previously cleaned up SEO links. if ( \is_a( $indexable, Indexable::class ) && $indexable->object_sub_type === 'attachment' ) { $this->link_builder->patch_seo_links( $indexable ); } // Always rebuild the primary term. $this->primary_term_builder->build( $indexable->object_id ); // Always rebuild the hierarchy; this needs the primary term to run correctly. $this->hierarchy_builder->build( $indexable ); $this->maybe_build_author_indexable( $indexable->author_id ); // The indexable is already saved, so return early. return $indexable; case 'user': $indexable = $this->author_builder->build( $indexable->object_id, $indexable ); break; case 'term': $indexable = $this->term_builder->build( $indexable->object_id, $indexable ); // Save indexable, to make sure it can be queried when building hierarchy. $indexable = $this->indexable_helper->save_indexable( $indexable, $indexable_before ); $this->hierarchy_builder->build( $indexable ); // The indexable is already saved, so return early. return $indexable; case 'home-page': $indexable = $this->home_page_builder->build( $indexable ); break; case 'date-archive': $indexable = $this->date_archive_builder->build( $indexable ); break; case 'post-type-archive': $indexable = $this->post_type_archive_builder->build( $indexable->object_sub_type, $indexable ); break; case 'system-page': $indexable = $this->system_page_builder->build( $indexable->object_sub_type, $indexable ); break; } return $this->indexable_helper->save_indexable( $indexable, $indexable_before ); } catch ( Source_Exception $exception ) { if ( ! $this->is_type_with_no_id( $indexable->object_type ) && ! isset( $indexable->object_id ) ) { return false; } /** * The current indexable could not be indexed. Create a placeholder indexable, so we can * skip this indexable in future indexing runs. * * @var Indexable $indexable */ $indexable = $this->ensure_indexable( $indexable, [ 'object_id' => $indexable->object_id, 'object_type' => $indexable->object_type, 'post_status' => 'unindexed', 'version' => 0, ] ); // If we already had an existing indexable, mark it as unindexed. We cannot rely on its validity anymore. $indexable->post_status = 'unindexed'; // Make sure that the indexing process doesn't get stuck in a loop on this broken indexable. $indexable = $this->version_manager->set_latest( $indexable ); return $this->indexable_helper->save_indexable( $indexable, $indexable_before ); } catch ( Not_Built_Exception $exception ) { return false; } } // phpcs:enable } s->post_builder = $post_builder; $this->term_builder = $term_builder; $this->home_page_builder = $home_page_builder; $this->post_type_archive_builder = $post_type_archive_builder; $this->date_archive_builder = $date_archive_builder; $this->system_page_builder = $system_page_builder; $this->hierarchy_builder = $hierarchy_builder; $this->primary_term_builder = $primary_term_builder; $this->indexable_helper = $indexable_helper; $this->version_manager = $version_manager; $this->link_builder = $link_builder; } /** * Sets the indexable repository. Done to avoid circular dependencies. * * @required * * @param Indexable_Repository $indexable_repository The indexable repository. * * @return void */ public function set_indexable_repository( Indexable_Repository $indexable_repository ) { $this->indexable_repository = $indexable_repository; } /** * Creates a clean copy of an Indexable to allow for later database operations. * * @param Indexable $indexable The Indexable to copy. * * @return bool|Indexable */ protected function deep_copy_indexable( $indexable ) { return $this->indexable_repository ->query() ->create( $indexable->as_array() ); } /** * Creates an indexable by its ID and type. * * @param int $object_id The indexable object ID. * @param string $object_type The indexable object type. * @param Indexable|bool $indexable Optional. An existing indexable to overwrite. * * @return bool|Indexable Instance of indexable. False when unable to build. */ public function build_for_id_and_type( $object_id, $object_type, $indexable = false ) { $defaults = [ 'object_type' => $object_type, 'object_id' => $object_id, ]; $indexable = $this->build( $indexable, $defaults ); return $indexable; } /** * Creates an indexable for the homepage. * * @param Indexable|bool $indexable Optional. An existing indexable to overwrite. * * @return Indexable The home page indexable. */ public function build_for_home_page( $indexable = false ) { return $this->build( $indexable, [ 'object_type' => 'home-page' ] ); } /** * Creates an indexable for the date archive. * * @param Indexable|bool $indexable Optional. An existing indexable to overwrite. * * @return Indexable The date archive indexable. */ public function build_for_date_archive( $indexable = false ) { return $this->build( $indexable, [ 'object_type' => 'date-archive' ] ); } /** * Creates an indexable for a post type archive. * * @param string $post_type The post type. * @param Indexable|bool $indexable Optional. An existing indexable to overwrite. * * @return Indexable The post type archive indexable. */ public function build_for_post_type_archive( $post_type, $indexable = false ) { $defaults = [ 'object_type' => 'post-type-archive', 'object_sub_type' => $post_type, ]; return $this->build( $indexable, $defaults ); } /** * Creates an indexable for a system page. * * @param string $page_type The type of system page. * @param Indexable|bool $indexable Optional. An existing indexable to overwrite. * * @return Indexable The search result indexable. */ public function build_for_system_page( $page_type, $indexable = false ) { $defaults = [ 'object_type' => 'system-page', 'object_sub_type' => $page_type, ]; return $this->build( $indexable, $defaults ); } /** * Ensures we have a valid indexable. Creates one if false is passed. * * @param Indexable|false $indexable The indexable. * @param array $defaults The initial properties of the Indexable. * * @return Indexable The indexable. */ protected function ensure_indexable( $indexable, $defaults = [] ) { if ( ! $indexable ) { return $this->indexable_repository->query()->create( $defaults ); } return $indexable; } /** * Build and author indexable from an author id if it does not exist yet, or if the author indexable needs to be upgraded. * * @param int $author_id The author id. * * @return Indexable|false The author indexable if it has been built, `false` if it could not be built. */ protected function maybe_build_author_indexable( $author_id ) { $author_indexable = $this->indexable_repository->find_by_id_and_type( $author_id, 'user', false ); if ( ! $author_indexable || $this->version_manager->indexable_needs_upgrade( $author_indexable ) ) { // Try to build the author. $author_defaults = [ 'object_type' => 'user', 'object_id' => $author_id, ]; $author_indexable = $this->build( $author_indexable, $author_defaults ); } return $author_indexable; } /** * Checks if the indexable type is one that is not supposed to have object ID for. * * @param string $type The type of the indexable. * * @return bool Whether the indexable type is one that is not supposed to have object ID for. */ protected function is_type_with_no_id( $type ) { return \in_array( $type, [ 'home-page', 'date-archive', 'post-type-archive', 'system-page' ], true ); } // phpcs:disable Squiz.Commenting.FunctionCommentThrowTag.Missing -- Exceptions are handled by the catch statement in the method. /** * Rebuilds an Indexable from scratch. * * @param Indexable $indexable The Indexable to (re)build. * @param array|null $defaults The object type of the Indexable. * * @return Indexable|false The resulting Indexable. */ public function build( $indexable, $defaults = null ) { // Backup the previous Indexable, if there was one. $indexable_before = ( $indexable ) ? $this->deep_copy_indexable( $indexable ) : null; // Make sure we have an Indexable to work with. $indexable = $this->ensure_indexable( $indexable, $defaults ); try { if ( $indexable->object_id === 0 ) { throw Not_Built_Exception::invalid_object_id( $indexable->object_id ); } switch ( $indexable->object_type ) { case 'post': $indexable = $this->post_builder->build( $indexable->object_id, $indexable ); // Save indexable, to make sure it can be queried when building related objects like the author indexable and hierarchy. $indexable = $this->indexable_helper->save_indexable( $indexable, $indexable_before ); // For attachments, we have to make sure to patch any potentially previously cleaned up SEO links. if ( \is_a( $indexable, Indexable::class ) && $indexable->object_sub_type === 'attachment' ) { $this->link_builder->patch_seo_links( $indexable ); } // Always rebuild the primary term. $this->primary_term_builder->build( $indexable->object_id ); // Always rebuild the hierarchy; this needs the primary term to run correctly. $this->hierarchy_builder->build( $indexable ); $this->maybe_build_author_indexable( $indexable->author_id ); // The indexable is already saved, so return early. return $indexable; case 'user': $indexable = $this->author_builder->build( $indexable->object_id, $indexable ); break; case 'term': $indexable = $this->term_builder->build( $indexable->object_id, $indexable ); // Save indexable, to make sure it can be queried when building hierarchy. $indexable = $this->indexable_helper->save_indexable( $indexable, $indexable_before ); $this->hierarchy_builder->build( $indexable ); // The indexable is already saved, so return early. return $indexable; case 'home-page': $indexable = $this->home_page_builder->build( $indexable ); break; case 'date-archive': $indexable = $this->date_archive_builder->build( $indexable ); break; case 'post-type-archive': $indexable = $this->post_type_archive_builder->build( $indexable->object_sub_type, $indexable ); break; case 'system-page': $indexable = $this->system_page_builder->build( $indexable->object_sub_type, $indexable ); break; } return $this->indexable_helper->save_indexable( $indexable, $indexable_before ); } catch ( Source_Exception $exception ) { if ( ! $this->is_type_with_no_id( $indexable->object_type ) && ! isset( $indexable->object_id ) ) { return false; } /** * The current indexable could not be indexed. Create a placeholder indexable, so we can * skip this indexable in future indexing runs. * * @var Indexable $indexable */ $indexable = $this->ensure_indexable( $indexable, [ 'object_id' => $indexable->object_id, 'object_type' => $indexable->object_type, 'post_status' => 'unindexed', 'version' => 0, ] ); // If we already had an existing indexable, mark it as unindexed. We cannot rely on its validity anymore. $indexable->post_status = 'unindexed'; // Make sure that the indexing process doesn't get stuck in a loop on this broken indexable. $indexable = $this->version_manager->set_latest( $indexable ); return $this->indexable_helper->save_indexable( $indexable, $indexable_before ); } catch ( Not_Built_Exception $exception ) { return false; } } // phpcs:enable } s->post_builder = $post_builder; $this->term_builder = $term_builder; $this->home_page_builder = $home_page_builder; $this->post_type_archive_builder = $post_type_archive_builder; $this->date_archive_builder = $date_archive_builder; $this->system_page_builder = $system_page_builder; $this->hierarchy_builder = $hierarchy_builder; $this->primary_term_builder = $primary_term_builder; $this->indexable_helper = $indexable_helper; $this->version_manager = $version_manager; $this->link_builder = $link_builder; } /** * Sets the indexable repository. Done to avoid circular dependencies. * * @required * * @param Indexable_Repository $indexable_repository The indexable repository. * * @return void */ public function set_indexable_repository( Indexable_Repository $indexable_repository ) { $this->indexable_repository = $indexable_repository; } /** * Creates a clean copy of an Indexable to allow for later database operations. * * @param Indexable $indexable The Indexable to copy. * * @return bool|Indexable */ protected function deep_copy_indexable( $indexable ) { return $this->indexable_repository ->query() ->create( $indexable->as_array() ); } /** * Creates an indexable by its ID and type. * * @param int $object_id The indexable object ID. * @param string $object_type The indexable object type. * @param Indexable|bool $indexable Optional. An existing indexable to overwrite. * * @return bool|Indexable Instance of indexable. False when unable to build. */ public function build_for_id_and_type( $object_id, $object_type, $indexable = false ) { $defaults = [ 'object_type' => $object_type, 'object_id' => $object_id, ]; $indexable = $this->build( $indexable, $defaults ); return $indexable; } /** * Creates an indexable for the homepage. * * @param Indexable|bool $indexable Optional. An existing indexable to overwrite. * * @return Indexable The home page indexable. */ public function build_for_home_page( $indexable = false ) { return $this->build( $indexable, [ 'object_type' => 'home-page' ] ); } /** * Creates an indexable for the date archive. * * @param Indexable|bool $indexable Optional. An existing indexable to overwrite. * * @return Indexable The date archive indexable. */ public function build_for_date_archive( $indexable = false ) { return $this->build( $indexable, [ 'object_type' => 'date-archive' ] ); } /** * Creates an indexable for a post type archive. * * @param string $post_type The post type. * @param Indexable|bool $indexable Optional. An existing indexable to overwrite. * * @return Indexable The post type archive indexable. */ public function build_for_post_type_archive( $post_type, $indexable = false ) { $defaults = [ 'object_type' => 'post-type-archive', 'object_sub_type' => $post_type, ]; return $this->build( $indexable, $defaults ); } /** * Creates an indexable for a system page. * * @param string $page_type The type of system page. * @param Indexable|bool $indexable Optional. An existing indexable to overwrite. * * @return Indexable The search result indexable. */ public function build_for_system_page( $page_type, $indexable = false ) { $defaults = [ 'object_type' => 'system-page', 'object_sub_type' => $page_type, ]; return $this->build( $indexable, $defaults ); } /** * Ensures we have a valid indexable. Creates one if false is passed. * * @param Indexable|false $indexable The indexable. * @param array $defaults The initial properties of the Indexable. * * @return Indexable The indexable. */ protected function ensure_indexable( $indexable, $defaults = [] ) { if ( ! $indexable ) { return $this->indexable_repository->query()->create( $defaults ); } return $indexable; } /** * Build and author indexable from an author id if it does not exist yet, or if the author indexable needs to be upgraded. * * @param int $author_id The author id. * * @return Indexable|false The author indexable if it has been built, `false` if it could not be built. */ protected function maybe_build_author_indexable( $author_id ) { $author_indexable = $this->indexable_repository->find_by_id_and_type( $author_id, 'user', false ); if ( ! $author_indexable || $this->version_manager->indexable_needs_upgrade( $author_indexable ) ) { // Try to build the author. $author_defaults = [ 'object_type' => 'user', 'object_id' => $author_id, ]; $author_indexable = $this->build( $author_indexable, $author_defaults ); } return $author_indexable; } /** * Checks if the indexable type is one that is not supposed to have object ID for. * * @param string $type The type of the indexable. * * @return bool Whether the indexable type is one that is not supposed to have object ID for. */ protected function is_type_with_no_id( $type ) { return \in_array( $type, [ 'home-page', 'date-archive', 'post-type-archive', 'system-page' ], true ); } // phpcs:disable Squiz.Commenting.FunctionCommentThrowTag.Missing -- Exceptions are handled by the catch statement in the method. /** * Rebuilds an Indexable from scratch. * * @param Indexable $indexable The Indexable to (re)build. * @param array|null $defaults The object type of the Indexable. * * @return Indexable|false The resulting Indexable. */ public function build( $indexable, $defaults = null ) { // Backup the previous Indexable, if there was one. $indexable_before = ( $indexable ) ? $this->deep_copy_indexable( $indexable ) : null; // Make sure we have an Indexable to work with. $indexable = $this->ensure_indexable( $indexable, $defaults ); try { if ( $indexable->object_id === 0 ) { throw Not_Built_Exception::invalid_object_id( $indexable->object_id ); } switch ( $indexable->object_type ) { case 'post': $indexable = $this->post_builder->build( $indexable->object_id, $indexable ); // Save indexable, to make sure it can be queried when building related objects like the author indexable and hierarchy. $indexable = $this->indexable_helper->save_indexable( $indexable, $indexable_before ); // For attachments, we have to make sure to patch any potentially previously cleaned up SEO links. if ( \is_a( $indexable, Indexable::class ) && $indexable->object_sub_type === 'attachment' ) { $this->link_builder->patch_seo_links( $indexable ); } // Always rebuild the primary term. $this->primary_term_builder->build( $indexable->object_id ); // Always rebuild the hierarchy; this needs the primary term to run correctly. $this->hierarchy_builder->build( $indexable ); $this->maybe_build_author_indexable( $indexable->author_id ); // The indexable is already saved, so return early. return $indexable; case 'user': $indexable = $this->author_builder->build( $indexable->object_id, $indexable ); break; case 'term': $indexable = $this->term_builder->build( $indexable->object_id, $indexable ); // Save indexable, to make sure it can be queried when building hierarchy. $indexable = $this->indexable_helper->save_indexable( $indexable, $indexable_before ); $this->hierarchy_builder->build( $indexable ); // The indexable is already saved, so return early. return $indexable; case 'home-page': $indexable = $this->home_page_builder->build( $indexable ); break; case 'date-archive': $indexable = $this->date_archive_builder->build( $indexable ); break; case 'post-type-archive': $indexable = $this->post_type_archive_builder->build( $indexable->object_sub_type, $indexable ); break; case 'system-page': $indexable = $this->system_page_builder->build( $indexable->object_sub_type, $indexable ); break; } return $this->indexable_helper->save_indexable( $indexable, $indexable_before ); } catch ( Source_Exception $exception ) { if ( ! $this->is_type_with_no_id( $indexable->object_type ) && ! isset( $indexable->object_id ) ) { return false; } /** * The current indexable could not be indexed. Create a placeholder indexable, so we can * skip this indexable in future indexing runs. * * @var Indexable $indexable */ $indexable = $this->ensure_indexable( $indexable, [ 'object_id' => $indexable->object_id, 'object_type' => $indexable->object_type, 'post_status' => 'unindexed', 'version' => 0, ] ); // If we already had an existing indexable, mark it as unindexed. We cannot rely on its validity anymore. $indexable->post_status = 'unindexed'; // Make sure that the indexing process doesn't get stuck in a loop on this broken indexable. $indexable = $this->version_manager->set_latest( $indexable ); return $this->indexable_helper->save_indexable( $indexable, $indexable_before ); } catch ( Not_Built_Exception $exception ) { return false; } } // phpcs:enable } s->post_builder = $post_builder; $this->term_builder = $term_builder; $this->home_page_builder = $home_page_builder; $this->post_type_archive_builder = $post_type_archive_builder; $this->date_archive_builder = $date_archive_builder; $this->system_page_builder = $system_page_builder; $this->hierarchy_builder = $hierarchy_builder; $this->primary_term_builder = $primary_term_builder; $this->indexable_helper = $indexable_helper; $this->version_manager = $version_manager; $this->link_builder = $link_builder; } /** * Sets the indexable repository. Done to avoid circular dependencies. * * @required * * @param Indexable_Repository $indexable_repository The indexable repository. * * @return void */ public function set_indexable_repository( Indexable_Repository $indexable_repository ) { $this->indexable_repository = $indexable_repository; } /** * Creates a clean copy of an Indexable to allow for later database operations. * * @param Indexable $indexable The Indexable to copy. * * @return bool|Indexable */ protected function deep_copy_indexable( $indexable ) { return $this->indexable_repository ->query() ->create( $indexable->as_array() ); } /** * Creates an indexable by its ID and type. * * @param int $object_id The indexable object ID. * @param string $object_type The indexable object type. * @param Indexable|bool $indexable Optional. An existing indexable to overwrite. * * @return bool|Indexable Instance of indexable. False when unable to build. */ public function build_for_id_and_type( $object_id, $object_type, $indexable = false ) { $defaults = [ 'object_type' => $object_type, 'object_id' => $object_id, ]; $indexable = $this->build( $indexable, $defaults ); return $indexable; } /** * Creates an indexable for the homepage. * * @param Indexable|bool $indexable Optional. An existing indexable to overwrite. * * @return Indexable The home page indexable. */ public function build_for_home_page( $indexable = false ) { return $this->build( $indexable, [ 'object_type' => 'home-page' ] ); } /** * Creates an indexable for the date archive. * * @param Indexable|bool $indexable Optional. An existing indexable to overwrite. * * @return Indexable The date archive indexable. */ public function build_for_date_archive( $indexable = false ) { return $this->build( $indexable, [ 'object_type' => 'date-archive' ] ); } /** * Creates an indexable for a post type archive. * * @param string $post_type The post type. * @param Indexable|bool $indexable Optional. An existing indexable to overwrite. * * @return Indexable The post type archive indexable. */ public function build_for_post_type_archive( $post_type, $indexable = false ) { $defaults = [ 'object_type' => 'post-type-archive', 'object_sub_type' => $post_type, ]; return $this->build( $indexable, $defaults ); } /** * Creates an indexable for a system page. * * @param string $page_type The type of system page. * @param Indexable|bool $indexable Optional. An existing indexable to overwrite. * * @return Indexable The search result indexable. */ public function build_for_system_page( $page_type, $indexable = false ) { $defaults = [ 'object_type' => 'system-page', 'object_sub_type' => $page_type, ]; return $this->build( $indexable, $defaults ); } /** * Ensures we have a valid indexable. Creates one if false is passed. * * @param Indexable|false $indexable The indexable. * @param array $defaults The initial properties of the Indexable. * * @return Indexable The indexable. */ protected function ensure_indexable( $indexable, $defaults = [] ) { if ( ! $indexable ) { return $this->indexable_repository->query()->create( $defaults ); } return $indexable; } /** * Build and author indexable from an author id if it does not exist yet, or if the author indexable needs to be upgraded. * * @param int $author_id The author id. * * @return Indexable|false The author indexable if it has been built, `false` if it could not be built. */ protected function maybe_build_author_indexable( $author_id ) { $author_indexable = $this->indexable_repository->find_by_id_and_type( $author_id, 'user', false ); if ( ! $author_indexable || $this->version_manager->indexable_needs_upgrade( $author_indexable ) ) { // Try to build the author. $author_defaults = [ 'object_type' => 'user', 'object_id' => $author_id, ]; $author_indexable = $this->build( $author_indexable, $author_defaults ); } return $author_indexable; } /** * Checks if the indexable type is one that is not supposed to have object ID for. * * @param string $type The type of the indexable. * * @return bool Whether the indexable type is one that is not supposed to have object ID for. */ protected function is_type_with_no_id( $type ) { return \in_array( $type, [ 'home-page', 'date-archive', 'post-type-archive', 'system-page' ], true ); } // phpcs:disable Squiz.Commenting.FunctionCommentThrowTag.Missing -- Exceptions are handled by the catch statement in the method. /** * Rebuilds an Indexable from scratch. * * @param Indexable $indexable The Indexable to (re)build. * @param array|null $defaults The object type of the Indexable. * * @return Indexable|false The resulting Indexable. */ public function build( $indexable, $defaults = null ) { // Backup the previous Indexable, if there was one. $indexable_before = ( $indexable ) ? $this->deep_copy_indexable( $indexable ) : null; // Make sure we have an Indexable to work with. $indexable = $this->ensure_indexable( $indexable, $defaults ); try { if ( $indexable->object_id === 0 ) { throw Not_Built_Exception::invalid_object_id( $indexable->object_id ); } switch ( $indexable->object_type ) { case 'post': $indexable = $this->post_builder->build( $indexable->object_id, $indexable ); // Save indexable, to make sure it can be queried when building related objects like the author indexable and hierarchy. $indexable = $this->indexable_helper->save_indexable( $indexable, $indexable_before ); // For attachments, we have to make sure to patch any potentially previously cleaned up SEO links. if ( \is_a( $indexable, Indexable::class ) && $indexable->object_sub_type === 'attachment' ) { $this->link_builder->patch_seo_links( $indexable ); } // Always rebuild the primary term. $this->primary_term_builder->build( $indexable->object_id ); // Always rebuild the hierarchy; this needs the primary term to run correctly. $this->hierarchy_builder->build( $indexable ); $this->maybe_build_author_indexable( $indexable->author_id ); // The indexable is already saved, so return early. return $indexable; case 'user': $indexable = $this->author_builder->build( $indexable->object_id, $indexable ); break; case 'term': $indexable = $this->term_builder->build( $indexable->object_id, $indexable ); // Save indexable, to make sure it can be queried when building hierarchy. $indexable = $this->indexable_helper->save_indexable( $indexable, $indexable_before ); $this->hierarchy_builder->build( $indexable ); // The indexable is already saved, so return early. return $indexable; case 'home-page': $indexable = $this->home_page_builder->build( $indexable ); break; case 'date-archive': $indexable = $this->date_archive_builder->build( $indexable ); break; case 'post-type-archive': $indexable = $this->post_type_archive_builder->build( $indexable->object_sub_type, $indexable ); break; case 'system-page': $indexable = $this->system_page_builder->build( $indexable->object_sub_type, $indexable ); break; } return $this->indexable_helper->save_indexable( $indexable, $indexable_before ); } catch ( Source_Exception $exception ) { if ( ! $this->is_type_with_no_id( $indexable->object_type ) && ! isset( $indexable->object_id ) ) { return false; } /** * The current indexable could not be indexed. Create a placeholder indexable, so we can * skip this indexable in future indexing runs. * * @var Indexable $indexable */ $indexable = $this->ensure_indexable( $indexable, [ 'object_id' => $indexable->object_id, 'object_type' => $indexable->object_type, 'post_status' => 'unindexed', 'version' => 0, ] ); // If we already had an existing indexable, mark it as unindexed. We cannot rely on its validity anymore. $indexable->post_status = 'unindexed'; // Make sure that the indexing process doesn't get stuck in a loop on this broken indexable. $indexable = $this->version_manager->set_latest( $indexable ); return $this->indexable_helper->save_indexable( $indexable, $indexable_before ); } catch ( Not_Built_Exception $exception ) { return false; } } // phpcs:enable } s->post_builder = $post_builder; $this->term_builder = $term_builder; $this->home_page_builder = $home_page_builder; $this->post_type_archive_builder = $post_type_archive_builder; $this->date_archive_builder = $date_archive_builder; $this->system_page_builder = $system_page_builder; $this->hierarchy_builder = $hierarchy_builder; $this->primary_term_builder = $primary_term_builder; $this->indexable_helper = $indexable_helper; $this->version_manager = $version_manager; $this->link_builder = $link_builder; } /** * Sets the indexable repository. Done to avoid circular dependencies. * * @required * * @param Indexable_Repository $indexable_repository The indexable repository. * * @return void */ public function set_indexable_repository( Indexable_Repository $indexable_repository ) { $this->indexable_repository = $indexable_repository; } /** * Creates a clean copy of an Indexable to allow for later database operations. * * @param Indexable $indexable The Indexable to copy. * * @return bool|Indexable */ protected function deep_copy_indexable( $indexable ) { return $this->indexable_repository ->query() ->create( $indexable->as_array() ); } /** * Creates an indexable by its ID and type. * * @param int $object_id The indexable object ID. * @param string $object_type The indexable object type. * @param Indexable|bool $indexable Optional. An existing indexable to overwrite. * * @return bool|Indexable Instance of indexable. False when unable to build. */ public function build_for_id_and_type( $object_id, $object_type, $indexable = false ) { $defaults = [ 'object_type' => $object_type, 'object_id' => $object_id, ]; $indexable = $this->build( $indexable, $defaults ); return $indexable; } /** * Creates an indexable for the homepage. * * @param Indexable|bool $indexable Optional. An existing indexable to overwrite. * * @return Indexable The home page indexable. */ public function build_for_home_page( $indexable = false ) { return $this->build( $indexable, [ 'object_type' => 'home-page' ] ); } /** * Creates an indexable for the date archive. * * @param Indexable|bool $indexable Optional. An existing indexable to overwrite. * * @return Indexable The date archive indexable. */ public function build_for_date_archive( $indexable = false ) { return $this->build( $indexable, [ 'object_type' => 'date-archive' ] ); } /** * Creates an indexable for a post type archive. * * @param string $post_type The post type. * @param Indexable|bool $indexable Optional. An existing indexable to overwrite. * * @return Indexable The post type archive indexable. */ public function build_for_post_type_archive( $post_type, $indexable = false ) { $defaults = [ 'object_type' => 'post-type-archive', 'object_sub_type' => $post_type, ]; return $this->build( $indexable, $defaults ); } /** * Creates an indexable for a system page. * * @param string $page_type The type of system page. * @param Indexable|bool $indexable Optional. An existing indexable to overwrite. * * @return Indexable The search result indexable. */ public function build_for_system_page( $page_type, $indexable = false ) { $defaults = [ 'object_type' => 'system-page', 'object_sub_type' => $page_type, ]; return $this->build( $indexable, $defaults ); } /** * Ensures we have a valid indexable. Creates one if false is passed. * * @param Indexable|false $indexable The indexable. * @param array $defaults The initial properties of the Indexable. * * @return Indexable The indexable. */ protected function ensure_indexable( $indexable, $defaults = [] ) { if ( ! $indexable ) { return $this->indexable_repository->query()->create( $defaults ); } return $indexable; } /** * Build and author indexable from an author id if it does not exist yet, or if the author indexable needs to be upgraded. * * @param int $author_id The author id. * * @return Indexable|false The author indexable if it has been built, `false` if it could not be built. */ protected function maybe_build_author_indexable( $author_id ) { $author_indexable = $this->indexable_repository->find_by_id_and_type( $author_id, 'user', false ); if ( ! $author_indexable || $this->version_manager->indexable_needs_upgrade( $author_indexable ) ) { // Try to build the author. $author_defaults = [ 'object_type' => 'user', 'object_id' => $author_id, ]; $author_indexable = $this->build( $author_indexable, $author_defaults ); } return $author_indexable; } /** * Checks if the indexable type is one that is not supposed to have object ID for. * * @param string $type The type of the indexable. * * @return bool Whether the indexable type is one that is not supposed to have object ID for. */ protected function is_type_with_no_id( $type ) { return \in_array( $type, [ 'home-page', 'date-archive', 'post-type-archive', 'system-page' ], true ); } // phpcs:disable Squiz.Commenting.FunctionCommentThrowTag.Missing -- Exceptions are handled by the catch statement in the method. /** * Rebuilds an Indexable from scratch. * * @param Indexable $indexable The Indexable to (re)build. * @param array|null $defaults The object type of the Indexable. * * @return Indexable|false The resulting Indexable. */ public function build( $indexable, $defaults = null ) { // Backup the previous Indexable, if there was one. $indexable_before = ( $indexable ) ? $this->deep_copy_indexable( $indexable ) : null; // Make sure we have an Indexable to work with. $indexable = $this->ensure_indexable( $indexable, $defaults ); try { if ( $indexable->object_id === 0 ) { throw Not_Built_Exception::invalid_object_id( $indexable->object_id ); } switch ( $indexable->object_type ) { case 'post': $indexable = $this->post_builder->build( $indexable->object_id, $indexable ); // Save indexable, to make sure it can be queried when building related objects like the author indexable and hierarchy. $indexable = $this->indexable_helper->save_indexable( $indexable, $indexable_before ); // For attachments, we have to make sure to patch any potentially previously cleaned up SEO links. if ( \is_a( $indexable, Indexable::class ) && $indexable->object_sub_type === 'attachment' ) { $this->link_builder->patch_seo_links( $indexable ); } // Always rebuild the primary term. $this->primary_term_builder->build( $indexable->object_id ); // Always rebuild the hierarchy; this needs the primary term to run correctly. $this->hierarchy_builder->build( $indexable ); $this->maybe_build_author_indexable( $indexable->author_id ); // The indexable is already saved, so return early. return $indexable; case 'user': $indexable = $this->author_builder->build( $indexable->object_id, $indexable ); break; case 'term': $indexable = $this->term_builder->build( $indexable->object_id, $indexable ); // Save indexable, to make sure it can be queried when building hierarchy. $indexable = $this->indexable_helper->save_indexable( $indexable, $indexable_before ); $this->hierarchy_builder->build( $indexable ); // The indexable is already saved, so return early. return $indexable; case 'home-page': $indexable = $this->home_page_builder->build( $indexable ); break; case 'date-archive': $indexable = $this->date_archive_builder->build( $indexable ); break; case 'post-type-archive': $indexable = $this->post_type_archive_builder->build( $indexable->object_sub_type, $indexable ); break; case 'system-page': $indexable = $this->system_page_builder->build( $indexable->object_sub_type, $indexable ); break; } return $this->indexable_helper->save_indexable( $indexable, $indexable_before ); } catch ( Source_Exception $exception ) { if ( ! $this->is_type_with_no_id( $indexable->object_type ) && ! isset( $indexable->object_id ) ) { return false; } /** * The current indexable could not be indexed. Create a placeholder indexable, so we can * skip this indexable in future indexing runs. * * @var Indexable $indexable */ $indexable = $this->ensure_indexable( $indexable, [ 'object_id' => $indexable->object_id, 'object_type' => $indexable->object_type, 'post_status' => 'unindexed', 'version' => 0, ] ); // If we already had an existing indexable, mark it as unindexed. We cannot rely on its validity anymore. $indexable->post_status = 'unindexed'; // Make sure that the indexing process doesn't get stuck in a loop on this broken indexable. $indexable = $this->version_manager->set_latest( $indexable ); return $this->indexable_helper->save_indexable( $indexable, $indexable_before ); } catch ( Not_Built_Exception $exception ) { return false; } } // phpcs:enable } s->post_builder = $post_builder; $this->term_builder = $term_builder; $this->home_page_builder = $home_page_builder; $this->post_type_archive_builder = $post_type_archive_builder; $this->date_archive_builder = $date_archive_builder; $this->system_page_builder = $system_page_builder; $this->hierarchy_builder = $hierarchy_builder; $this->primary_term_builder = $primary_term_builder; $this->indexable_helper = $indexable_helper; $this->version_manager = $version_manager; $this->link_builder = $link_builder; } /** * Sets the indexable repository. Done to avoid circular dependencies. * * @required * * @param Indexable_Repository $indexable_repository The indexable repository. * * @return void */ public function set_indexable_repository( Indexable_Repository $indexable_repository ) { $this->indexable_repository = $indexable_repository; } /** * Creates a clean copy of an Indexable to allow for later database operations. * * @param Indexable $indexable The Indexable to copy. * * @return bool|Indexable */ protected function deep_copy_indexable( $indexable ) { return $this->indexable_repository ->query() ->create( $indexable->as_array() ); } /** * Creates an indexable by its ID and type. * * @param int $object_id The indexable object ID. * @param string $object_type The indexable object type. * @param Indexable|bool $indexable Optional. An existing indexable to overwrite. * * @return bool|Indexable Instance of indexable. False when unable to build. */ public function build_for_id_and_type( $object_id, $object_type, $indexable = false ) { $defaults = [ 'object_type' => $object_type, 'object_id' => $object_id, ]; $indexable = $this->build( $indexable, $defaults ); return $indexable; } /** * Creates an indexable for the homepage. * * @param Indexable|bool $indexable Optional. An existing indexable to overwrite. * * @return Indexable The home page indexable. */ public function build_for_home_page( $indexable = false ) { return $this->build( $indexable, [ 'object_type' => 'home-page' ] ); } /** * Creates an indexable for the date archive. * * @param Indexable|bool $indexable Optional. An existing indexable to overwrite. * * @return Indexable The date archive indexable. */ public function build_for_date_archive( $indexable = false ) { return $this->build( $indexable, [ 'object_type' => 'date-archive' ] ); } /** * Creates an indexable for a post type archive. * * @param string $post_type The post type. * @param Indexable|bool $indexable Optional. An existing indexable to overwrite. * * @return Indexable The post type archive indexable. */ public function build_for_post_type_archive( $post_type, $indexable = false ) { $defaults = [ 'object_type' => 'post-type-archive', 'object_sub_type' => $post_type, ]; return $this->build( $indexable, $defaults ); } /** * Creates an indexable for a system page. * * @param string $page_type The type of system page. * @param Indexable|bool $indexable Optional. An existing indexable to overwrite. * * @return Indexable The search result indexable. */ public function build_for_system_page( $page_type, $indexable = false ) { $defaults = [ 'object_type' => 'system-page', 'object_sub_type' => $page_type, ]; return $this->build( $indexable, $defaults ); } /** * Ensures we have a valid indexable. Creates one if false is passed. * * @param Indexable|false $indexable The indexable. * @param array $defaults The initial properties of the Indexable. * * @return Indexable The indexable. */ protected function ensure_indexable( $indexable, $defaults = [] ) { if ( ! $indexable ) { return $this->indexable_repository->query()->create( $defaults ); } return $indexable; } /** * Build and author indexable from an author id if it does not exist yet, or if the author indexable needs to be upgraded. * * @param int $author_id The author id. * * @return Indexable|false The author indexable if it has been built, `false` if it could not be built. */ protected function maybe_build_author_indexable( $author_id ) { $author_indexable = $this->indexable_repository->find_by_id_and_type( $author_id, 'user', false ); if ( ! $author_indexable || $this->version_manager->indexable_needs_upgrade( $author_indexable ) ) { // Try to build the author. $author_defaults = [ 'object_type' => 'user', 'object_id' => $author_id, ]; $author_indexable = $this->build( $author_indexable, $author_defaults ); } return $author_indexable; } /** * Checks if the indexable type is one that is not supposed to have object ID for. * * @param string $type The type of the indexable. * * @return bool Whether the indexable type is one that is not supposed to have object ID for. */ protected function is_type_with_no_id( $type ) { return \in_array( $type, [ 'home-page', 'date-archive', 'post-type-archive', 'system-page' ], true ); } // phpcs:disable Squiz.Commenting.FunctionCommentThrowTag.Missing -- Exceptions are handled by the catch statement in the method. /** * Rebuilds an Indexable from scratch. * * @param Indexable $indexable The Indexable to (re)build. * @param array|null $defaults The object type of the Indexable. * * @return Indexable|false The resulting Indexable. */ public function build( $indexable, $defaults = null ) { // Backup the previous Indexable, if there was one. $indexable_before = ( $indexable ) ? $this->deep_copy_indexable( $indexable ) : null; // Make sure we have an Indexable to work with. $indexable = $this->ensure_indexable( $indexable, $defaults ); try { if ( $indexable->object_id === 0 ) { throw Not_Built_Exception::invalid_object_id( $indexable->object_id ); } switch ( $indexable->object_type ) { case 'post': $indexable = $this->post_builder->build( $indexable->object_id, $indexable ); // Save indexable, to make sure it can be queried when building related objects like the author indexable and hierarchy. $indexable = $this->indexable_helper->save_indexable( $indexable, $indexable_before ); // For attachments, we have to make sure to patch any potentially previously cleaned up SEO links. if ( \is_a( $indexable, Indexable::class ) && $indexable->object_sub_type === 'attachment' ) { $this->link_builder->patch_seo_links( $indexable ); } // Always rebuild the primary term. $this->primary_term_builder->build( $indexable->object_id ); // Always rebuild the hierarchy; this needs the primary term to run correctly. $this->hierarchy_builder->build( $indexable ); $this->maybe_build_author_indexable( $indexable->author_id ); // The indexable is already saved, so return early. return $indexable; case 'user': $indexable = $this->author_builder->build( $indexable->object_id, $indexable ); break; case 'term': $indexable = $this->term_builder->build( $indexable->object_id, $indexable ); // Save indexable, to make sure it can be queried when building hierarchy. $indexable = $this->indexable_helper->save_indexable( $indexable, $indexable_before ); $this->hierarchy_builder->build( $indexable ); // The indexable is already saved, so return early. return $indexable; case 'home-page': $indexable = $this->home_page_builder->build( $indexable ); break; case 'date-archive': $indexable = $this->date_archive_builder->build( $indexable ); break; case 'post-type-archive': $indexable = $this->post_type_archive_builder->build( $indexable->object_sub_type, $indexable ); break; case 'system-page': $indexable = $this->system_page_builder->build( $indexable->object_sub_type, $indexable ); break; } return $this->indexable_helper->save_indexable( $indexable, $indexable_before ); } catch ( Source_Exception $exception ) { if ( ! $this->is_type_with_no_id( $indexable->object_type ) && ! isset( $indexable->object_id ) ) { return false; } /** * The current indexable could not be indexed. Create a placeholder indexable, so we can * skip this indexable in future indexing runs. * * @var Indexable $indexable */ $indexable = $this->ensure_indexable( $indexable, [ 'object_id' => $indexable->object_id, 'object_type' => $indexable->object_type, 'post_status' => 'unindexed', 'version' => 0, ] ); // If we already had an existing indexable, mark it as unindexed. We cannot rely on its validity anymore. $indexable->post_status = 'unindexed'; // Make sure that the indexing process doesn't get stuck in a loop on this broken indexable. $indexable = $this->version_manager->set_latest( $indexable ); return $this->indexable_helper->save_indexable( $indexable, $indexable_before ); } catch ( Not_Built_Exception $exception ) { return false; } } // phpcs:enable } s->post_builder = $post_builder; $this->term_builder = $term_builder; $this->home_page_builder = $home_page_builder; $this->post_type_archive_builder = $post_type_archive_builder; $this->date_archive_builder = $date_archive_builder; $this->system_page_builder = $system_page_builder; $this->hierarchy_builder = $hierarchy_builder; $this->primary_term_builder = $primary_term_builder; $this->indexable_helper = $indexable_helper; $this->version_manager = $version_manager; $this->link_builder = $link_builder; } /** * Sets the indexable repository. Done to avoid circular dependencies. * * @required * * @param Indexable_Repository $indexable_repository The indexable repository. * * @return void */ public function set_indexable_repository( Indexable_Repository $indexable_repository ) { $this->indexable_repository = $indexable_repository; } /** * Creates a clean copy of an Indexable to allow for later database operations. * * @param Indexable $indexable The Indexable to copy. * * @return bool|Indexable */ protected function deep_copy_indexable( $indexable ) { return $this->indexable_repository ->query() ->create( $indexable->as_array() ); } /** * Creates an indexable by its ID and type. * * @param int $object_id The indexable object ID. * @param string $object_type The indexable object type. * @param Indexable|bool $indexable Optional. An existing indexable to overwrite. * * @return bool|Indexable Instance of indexable. False when unable to build. */ public function build_for_id_and_type( $object_id, $object_type, $indexable = false ) { $defaults = [ 'object_type' => $object_type, 'object_id' => $object_id, ]; $indexable = $this->build( $indexable, $defaults ); return $indexable; } /** * Creates an indexable for the homepage. * * @param Indexable|bool $indexable Optional. An existing indexable to overwrite. * * @return Indexable The home page indexable. */ public function build_for_home_page( $indexable = false ) { return $this->build( $indexable, [ 'object_type' => 'home-page' ] ); } /** * Creates an indexable for the date archive. * * @param Indexable|bool $indexable Optional. An existing indexable to overwrite. * * @return Indexable The date archive indexable. */ public function build_for_date_archive( $indexable = false ) { return $this->build( $indexable, [ 'object_type' => 'date-archive' ] ); } /** * Creates an indexable for a post type archive. * * @param string $post_type The post type. * @param Indexable|bool $indexable Optional. An existing indexable to overwrite. * * @return Indexable The post type archive indexable. */ public function build_for_post_type_archive( $post_type, $indexable = false ) { $defaults = [ 'object_type' => 'post-type-archive', 'object_sub_type' => $post_type, ]; return $this->build( $indexable, $defaults ); } /** * Creates an indexable for a system page. * * @param string $page_type The type of system page. * @param Indexable|bool $indexable Optional. An existing indexable to overwrite. * * @return Indexable The search result indexable. */ public function build_for_system_page( $page_type, $indexable = false ) { $defaults = [ 'object_type' => 'system-page', 'object_sub_type' => $page_type, ]; return $this->build( $indexable, $defaults ); } /** * Ensures we have a valid indexable. Creates one if false is passed. * * @param Indexable|false $indexable The indexable. * @param array $defaults The initial properties of the Indexable. * * @return Indexable The indexable. */ protected function ensure_indexable( $indexable, $defaults = [] ) { if ( ! $indexable ) { return $this->indexable_repository->query()->create( $defaults ); } return $indexable; } /** * Build and author indexable from an author id if it does not exist yet, or if the author indexable needs to be upgraded. * * @param int $author_id The author id. * * @return Indexable|false The author indexable if it has been built, `false` if it could not be built. */ protected function maybe_build_author_indexable( $author_id ) { $author_indexable = $this->indexable_repository->find_by_id_and_type( $author_id, 'user', false ); if ( ! $author_indexable || $this->version_manager->indexable_needs_upgrade( $author_indexable ) ) { // Try to build the author. $author_defaults = [ 'object_type' => 'user', 'object_id' => $author_id, ]; $author_indexable = $this->build( $author_indexable, $author_defaults ); } return $author_indexable; } /** * Checks if the indexable type is one that is not supposed to have object ID for. * * @param string $type The type of the indexable. * * @return bool Whether the indexable type is one that is not supposed to have object ID for. */ protected function is_type_with_no_id( $type ) { return \in_array( $type, [ 'home-page', 'date-archive', 'post-type-archive', 'system-page' ], true ); } // phpcs:disable Squiz.Commenting.FunctionCommentThrowTag.Missing -- Exceptions are handled by the catch statement in the method. /** * Rebuilds an Indexable from scratch. * * @param Indexable $indexable The Indexable to (re)build. * @param array|null $defaults The object type of the Indexable. * * @return Indexable|false The resulting Indexable. */ public function build( $indexable, $defaults = null ) { // Backup the previous Indexable, if there was one. $indexable_before = ( $indexable ) ? $this->deep_copy_indexable( $indexable ) : null; // Make sure we have an Indexable to work with. $indexable = $this->ensure_indexable( $indexable, $defaults ); try { if ( $indexable->object_id === 0 ) { throw Not_Built_Exception::invalid_object_id( $indexable->object_id ); } switch ( $indexable->object_type ) { case 'post': $indexable = $this->post_builder->build( $indexable->object_id, $indexable ); // Save indexable, to make sure it can be queried when building related objects like the author indexable and hierarchy. $indexable = $this->indexable_helper->save_indexable( $indexable, $indexable_before ); // For attachments, we have to make sure to patch any potentially previously cleaned up SEO links. if ( \is_a( $indexable, Indexable::class ) && $indexable->object_sub_type === 'attachment' ) { $this->link_builder->patch_seo_links( $indexable ); } // Always rebuild the primary term. $this->primary_term_builder->build( $indexable->object_id ); // Always rebuild the hierarchy; this needs the primary term to run correctly. $this->hierarchy_builder->build( $indexable ); $this->maybe_build_author_indexable( $indexable->author_id ); // The indexable is already saved, so return early. return $indexable; case 'user': $indexable = $this->author_builder->build( $indexable->object_id, $indexable ); break; case 'term': $indexable = $this->term_builder->build( $indexable->object_id, $indexable ); // Save indexable, to make sure it can be queried when building hierarchy. $indexable = $this->indexable_helper->save_indexable( $indexable, $indexable_before ); $this->hierarchy_builder->build( $indexable ); // The indexable is already saved, so return early. return $indexable; case 'home-page': $indexable = $this->home_page_builder->build( $indexable ); break; case 'date-archive': $indexable = $this->date_archive_builder->build( $indexable ); break; case 'post-type-archive': $indexable = $this->post_type_archive_builder->build( $indexable->object_sub_type, $indexable ); break; case 'system-page': $indexable = $this->system_page_builder->build( $indexable->object_sub_type, $indexable ); break; } return $this->indexable_helper->save_indexable( $indexable, $indexable_before ); } catch ( Source_Exception $exception ) { if ( ! $this->is_type_with_no_id( $indexable->object_type ) && ! isset( $indexable->object_id ) ) { return false; } /** * The current indexable could not be indexed. Create a placeholder indexable, so we can * skip this indexable in future indexing runs. * * @var Indexable $indexable */ $indexable = $this->ensure_indexable( $indexable, [ 'object_id' => $indexable->object_id, 'object_type' => $indexable->object_type, 'post_status' => 'unindexed', 'version' => 0, ] ); // If we already had an existing indexable, mark it as unindexed. We cannot rely on its validity anymore. $indexable->post_status = 'unindexed'; // Make sure that the indexing process doesn't get stuck in a loop on this broken indexable. $indexable = $this->version_manager->set_latest( $indexable ); return $this->indexable_helper->save_indexable( $indexable, $indexable_before ); } catch ( Not_Built_Exception $exception ) { return false; } } // phpcs:enable } s->post_builder = $post_builder; $this->term_builder = $term_builder; $this->home_page_builder = $home_page_builder; $this->post_type_archive_builder = $post_type_archive_builder; $this->date_archive_builder = $date_archive_builder; $this->system_page_builder = $system_page_builder; $this->hierarchy_builder = $hierarchy_builder; $this->primary_term_builder = $primary_term_builder; $this->indexable_helper = $indexable_helper; $this->version_manager = $version_manager; $this->link_builder = $link_builder; } /** * Sets the indexable repository. Done to avoid circular dependencies. * * @required * * @param Indexable_Repository $indexable_repository The indexable repository. * * @return void */ public function set_indexable_repository( Indexable_Repository $indexable_repository ) { $this->indexable_repository = $indexable_repository; } /** * Creates a clean copy of an Indexable to allow for later database operations. * * @param Indexable $indexable The Indexable to copy. * * @return bool|Indexable */ protected function deep_copy_indexable( $indexable ) { return $this->indexable_repository ->query() ->create( $indexable->as_array() ); } /** * Creates an indexable by its ID and type. * * @param int $object_id The indexable object ID. * @param string $object_type The indexable object type. * @param Indexable|bool $indexable Optional. An existing indexable to overwrite. * * @return bool|Indexable Instance of indexable. False when unable to build. */ public function build_for_id_and_type( $object_id, $object_type, $indexable = false ) { $defaults = [ 'object_type' => $object_type, 'object_id' => $object_id, ]; $indexable = $this->build( $indexable, $defaults ); return $indexable; } /** * Creates an indexable for the homepage. * * @param Indexable|bool $indexable Optional. An existing indexable to overwrite. * * @return Indexable The home page indexable. */ public function build_for_home_page( $indexable = false ) { return $this->build( $indexable, [ 'object_type' => 'home-page' ] ); } /** * Creates an indexable for the date archive. * * @param Indexable|bool $indexable Optional. An existing indexable to overwrite. * * @return Indexable The date archive indexable. */ public function build_for_date_archive( $indexable = false ) { return $this->build( $indexable, [ 'object_type' => 'date-archive' ] ); } /** * Creates an indexable for a post type archive. * * @param string $post_type The post type. * @param Indexable|bool $indexable Optional. An existing indexable to overwrite. * * @return Indexable The post type archive indexable. */ public function build_for_post_type_archive( $post_type, $indexable = false ) { $defaults = [ 'object_type' => 'post-type-archive', 'object_sub_type' => $post_type, ]; return $this->build( $indexable, $defaults ); } /** * Creates an indexable for a system page. * * @param string $page_type The type of system page. * @param Indexable|bool $indexable Optional. An existing indexable to overwrite. * * @return Indexable The search result indexable. */ public function build_for_system_page( $page_type, $indexable = false ) { $defaults = [ 'object_type' => 'system-page', 'object_sub_type' => $page_type, ]; return $this->build( $indexable, $defaults ); } /** * Ensures we have a valid indexable. Creates one if false is passed. * * @param Indexable|false $indexable The indexable. * @param array $defaults The initial properties of the Indexable. * * @return Indexable The indexable. */ protected function ensure_indexable( $indexable, $defaults = [] ) { if ( ! $indexable ) { return $this->indexable_repository->query()->create( $defaults ); } return $indexable; } /** * Build and author indexable from an author id if it does not exist yet, or if the author indexable needs to be upgraded. * * @param int $author_id The author id. * * @return Indexable|false The author indexable if it has been built, `false` if it could not be built. */ protected function maybe_build_author_indexable( $author_id ) { $author_indexable = $this->indexable_repository->find_by_id_and_type( $author_id, 'user', false ); if ( ! $author_indexable || $this->version_manager->indexable_needs_upgrade( $author_indexable ) ) { // Try to build the author. $author_defaults = [ 'object_type' => 'user', 'object_id' => $author_id, ]; $author_indexable = $this->build( $author_indexable, $author_defaults ); } return $author_indexable; } /** * Checks if the indexable type is one that is not supposed to have object ID for. * * @param string $type The type of the indexable. * * @return bool Whether the indexable type is one that is not supposed to have object ID for. */ protected function is_type_with_no_id( $type ) { return \in_array( $type, [ 'home-page', 'date-archive', 'post-type-archive', 'system-page' ], true ); } // phpcs:disable Squiz.Commenting.FunctionCommentThrowTag.Missing -- Exceptions are handled by the catch statement in the method. /** * Rebuilds an Indexable from scratch. * * @param Indexable $indexable The Indexable to (re)build. * @param array|null $defaults The object type of the Indexable. * * @return Indexable|false The resulting Indexable. */ public function build( $indexable, $defaults = null ) { // Backup the previous Indexable, if there was one. $indexable_before = ( $indexable ) ? $this->deep_copy_indexable( $indexable ) : null; // Make sure we have an Indexable to work with. $indexable = $this->ensure_indexable( $indexable, $defaults ); try { if ( $indexable->object_id === 0 ) { throw Not_Built_Exception::invalid_object_id( $indexable->object_id ); } switch ( $indexable->object_type ) { case 'post': $indexable = $this->post_builder->build( $indexable->object_id, $indexable ); // Save indexable, to make sure it can be queried when building related objects like the author indexable and hierarchy. $indexable = $this->indexable_helper->save_indexable( $indexable, $indexable_before ); // For attachments, we have to make sure to patch any potentially previously cleaned up SEO links. if ( \is_a( $indexable, Indexable::class ) && $indexable->object_sub_type === 'attachment' ) { $this->link_builder->patch_seo_links( $indexable ); } // Always rebuild the primary term. $this->primary_term_builder->build( $indexable->object_id ); // Always rebuild the hierarchy; this needs the primary term to run correctly. $this->hierarchy_builder->build( $indexable ); $this->maybe_build_author_indexable( $indexable->author_id ); // The indexable is already saved, so return early. return $indexable; case 'user': $indexable = $this->author_builder->build( $indexable->object_id, $indexable ); break; case 'term': $indexable = $this->term_builder->build( $indexable->object_id, $indexable ); // Save indexable, to make sure it can be queried when building hierarchy. $indexable = $this->indexable_helper->save_indexable( $indexable, $indexable_before ); $this->hierarchy_builder->build( $indexable ); // The indexable is already saved, so return early. return $indexable; case 'home-page': $indexable = $this->home_page_builder->build( $indexable ); break; case 'date-archive': $indexable = $this->date_archive_builder->build( $indexable ); break; case 'post-type-archive': $indexable = $this->post_type_archive_builder->build( $indexable->object_sub_type, $indexable ); break; case 'system-page': $indexable = $this->system_page_builder->build( $indexable->object_sub_type, $indexable ); break; } return $this->indexable_helper->save_indexable( $indexable, $indexable_before ); } catch ( Source_Exception $exception ) { if ( ! $this->is_type_with_no_id( $indexable->object_type ) && ! isset( $indexable->object_id ) ) { return false; } /** * The current indexable could not be indexed. Create a placeholder indexable, so we can * skip this indexable in future indexing runs. * * @var Indexable $indexable */ $indexable = $this->ensure_indexable( $indexable, [ 'object_id' => $indexable->object_id, 'object_type' => $indexable->object_type, 'post_status' => 'unindexed', 'version' => 0, ] ); // If we already had an existing indexable, mark it as unindexed. We cannot rely on its validity anymore. $indexable->post_status = 'unindexed'; // Make sure that the indexing process doesn't get stuck in a loop on this broken indexable. $indexable = $this->version_manager->set_latest( $indexable ); return $this->indexable_helper->save_indexable( $indexable, $indexable_before ); } catch ( Not_Built_Exception $exception ) { return false; } } // phpcs:enable } s->post_builder = $post_builder; $this->term_builder = $term_builder; $this->home_page_builder = $home_page_builder; $this->post_type_archive_builder = $post_type_archive_builder; $this->date_archive_builder = $date_archive_builder; $this->system_page_builder = $system_page_builder; $this->hierarchy_builder = $hierarchy_builder; $this->primary_term_builder = $primary_term_builder; $this->indexable_helper = $indexable_helper; $this->version_manager = $version_manager; $this->link_builder = $link_builder; } /** * Sets the indexable repository. Done to avoid circular dependencies. * * @required * * @param Indexable_Repository $indexable_repository The indexable repository. * * @return void */ public function set_indexable_repository( Indexable_Repository $indexable_repository ) { $this->indexable_repository = $indexable_repository; } /** * Creates a clean copy of an Indexable to allow for later database operations. * * @param Indexable $indexable The Indexable to copy. * * @return bool|Indexable */ protected function deep_copy_indexable( $indexable ) { return $this->indexable_repository ->query() ->create( $indexable->as_array() ); } /** * Creates an indexable by its ID and type. * * @param int $object_id The indexable object ID. * @param string $object_type The indexable object type. * @param Indexable|bool $indexable Optional. An existing indexable to overwrite. * * @return bool|Indexable Instance of indexable. False when unable to build. */ public function build_for_id_and_type( $object_id, $object_type, $indexable = false ) { $defaults = [ 'object_type' => $object_type, 'object_id' => $object_id, ]; $indexable = $this->build( $indexable, $defaults ); return $indexable; } /** * Creates an indexable for the homepage. * * @param Indexable|bool $indexable Optional. An existing indexable to overwrite. * * @return Indexable The home page indexable. */ public function build_for_home_page( $indexable = false ) { return $this->build( $indexable, [ 'object_type' => 'home-page' ] ); } /** * Creates an indexable for the date archive. * * @param Indexable|bool $indexable Optional. An existing indexable to overwrite. * * @return Indexable The date archive indexable. */ public function build_for_date_archive( $indexable = false ) { return $this->build( $indexable, [ 'object_type' => 'date-archive' ] ); } /** * Creates an indexable for a post type archive. * * @param string $post_type The post type. * @param Indexable|bool $indexable Optional. An existing indexable to overwrite. * * @return Indexable The post type archive indexable. */ public function build_for_post_type_archive( $post_type, $indexable = false ) { $defaults = [ 'object_type' => 'post-type-archive', 'object_sub_type' => $post_type, ]; return $this->build( $indexable, $defaults ); } /** * Creates an indexable for a system page. * * @param string $page_type The type of system page. * @param Indexable|bool $indexable Optional. An existing indexable to overwrite. * * @return Indexable The search result indexable. */ public function build_for_system_page( $page_type, $indexable = false ) { $defaults = [ 'object_type' => 'system-page', 'object_sub_type' => $page_type, ]; return $this->build( $indexable, $defaults ); } /** * Ensures we have a valid indexable. Creates one if false is passed. * * @param Indexable|false $indexable The indexable. * @param array $defaults The initial properties of the Indexable. * * @return Indexable The indexable. */ protected function ensure_indexable( $indexable, $defaults = [] ) { if ( ! $indexable ) { return $this->indexable_repository->query()->create( $defaults ); } return $indexable; } /** * Build and author indexable from an author id if it does not exist yet, or if the author indexable needs to be upgraded. * * @param int $author_id The author id. * * @return Indexable|false The author indexable if it has been built, `false` if it could not be built. */ protected function maybe_build_author_indexable( $author_id ) { $author_indexable = $this->indexable_repository->find_by_id_and_type( $author_id, 'user', false ); if ( ! $author_indexable || $this->version_manager->indexable_needs_upgrade( $author_indexable ) ) { // Try to build the author. $author_defaults = [ 'object_type' => 'user', 'object_id' => $author_id, ]; $author_indexable = $this->build( $author_indexable, $author_defaults ); } return $author_indexable; } /** * Checks if the indexable type is one that is not supposed to have object ID for. * * @param string $type The type of the indexable. * * @return bool Whether the indexable type is one that is not supposed to have object ID for. */ protected function is_type_with_no_id( $type ) { return \in_array( $type, [ 'home-page', 'date-archive', 'post-type-archive', 'system-page' ], true ); } // phpcs:disable Squiz.Commenting.FunctionCommentThrowTag.Missing -- Exceptions are handled by the catch statement in the method. /** * Rebuilds an Indexable from scratch. * * @param Indexable $indexable The Indexable to (re)build. * @param array|null $defaults The object type of the Indexable. * * @return Indexable|false The resulting Indexable. */ public function build( $indexable, $defaults = null ) { // Backup the previous Indexable, if there was one. $indexable_before = ( $indexable ) ? $this->deep_copy_indexable( $indexable ) : null; // Make sure we have an Indexable to work with. $indexable = $this->ensure_indexable( $indexable, $defaults ); try { if ( $indexable->object_id === 0 ) { throw Not_Built_Exception::invalid_object_id( $indexable->object_id ); } switch ( $indexable->object_type ) { case 'post': $indexable = $this->post_builder->build( $indexable->object_id, $indexable ); // Save indexable, to make sure it can be queried when building related objects like the author indexable and hierarchy. $indexable = $this->indexable_helper->save_indexable( $indexable, $indexable_before ); // For attachments, we have to make sure to patch any potentially previously cleaned up SEO links. if ( \is_a( $indexable, Indexable::class ) && $indexable->object_sub_type === 'attachment' ) { $this->link_builder->patch_seo_links( $indexable ); } // Always rebuild the primary term. $this->primary_term_builder->build( $indexable->object_id ); // Always rebuild the hierarchy; this needs the primary term to run correctly. $this->hierarchy_builder->build( $indexable ); $this->maybe_build_author_indexable( $indexable->author_id ); // The indexable is already saved, so return early. return $indexable; case 'user': $indexable = $this->author_builder->build( $indexable->object_id, $indexable ); break; case 'term': $indexable = $this->term_builder->build( $indexable->object_id, $indexable ); // Save indexable, to make sure it can be queried when building hierarchy. $indexable = $this->indexable_helper->save_indexable( $indexable, $indexable_before ); $this->hierarchy_builder->build( $indexable ); // The indexable is already saved, so return early. return $indexable; case 'home-page': $indexable = $this->home_page_builder->build( $indexable ); break; case 'date-archive': $indexable = $this->date_archive_builder->build( $indexable ); break; case 'post-type-archive': $indexable = $this->post_type_archive_builder->build( $indexable->object_sub_type, $indexable ); break; case 'system-page': $indexable = $this->system_page_builder->build( $indexable->object_sub_type, $indexable ); break; } return $this->indexable_helper->save_indexable( $indexable, $indexable_before ); } catch ( Source_Exception $exception ) { if ( ! $this->is_type_with_no_id( $indexable->object_type ) && ! isset( $indexable->object_id ) ) { return false; } /** * The current indexable could not be indexed. Create a placeholder indexable, so we can * skip this indexable in future indexing runs. * * @var Indexable $indexable */ $indexable = $this->ensure_indexable( $indexable, [ 'object_id' => $indexable->object_id, 'object_type' => $indexable->object_type, 'post_status' => 'unindexed', 'version' => 0, ] ); // If we already had an existing indexable, mark it as unindexed. We cannot rely on its validity anymore. $indexable->post_status = 'unindexed'; // Make sure that the indexing process doesn't get stuck in a loop on this broken indexable. $indexable = $this->version_manager->set_latest( $indexable ); return $this->indexable_helper->save_indexable( $indexable, $indexable_before ); } catch ( Not_Built_Exception $exception ) { return false; } } // phpcs:enable } s->post_builder = $post_builder; $this->term_builder = $term_builder; $this->home_page_builder = $home_page_builder; $this->post_type_archive_builder = $post_type_archive_builder; $this->date_archive_builder = $date_archive_builder; $this->system_page_builder = $system_page_builder; $this->hierarchy_builder = $hierarchy_builder; $this->primary_term_builder = $primary_term_builder; $this->indexable_helper = $indexable_helper; $this->version_manager = $version_manager; $this->link_builder = $link_builder; } /** * Sets the indexable repository. Done to avoid circular dependencies. * * @required * * @param Indexable_Repository $indexable_repository The indexable repository. * * @return void */ public function set_indexable_repository( Indexable_Repository $indexable_repository ) { $this->indexable_repository = $indexable_repository; } /** * Creates a clean copy of an Indexable to allow for later database operations. * * @param Indexable $indexable The Indexable to copy. * * @return bool|Indexable */ protected function deep_copy_indexable( $indexable ) { return $this->indexable_repository ->query() ->create( $indexable->as_array() ); } /** * Creates an indexable by its ID and type. * * @param int $object_id The indexable object ID. * @param string $object_type The indexable object type. * @param Indexable|bool $indexable Optional. An existing indexable to overwrite. * * @return bool|Indexable Instance of indexable. False when unable to build. */ public function build_for_id_and_type( $object_id, $object_type, $indexable = false ) { $defaults = [ 'object_type' => $object_type, 'object_id' => $object_id, ]; $indexable = $this->build( $indexable, $defaults ); return $indexable; } /** * Creates an indexable for the homepage. * * @param Indexable|bool $indexable Optional. An existing indexable to overwrite. * * @return Indexable The home page indexable. */ public function build_for_home_page( $indexable = false ) { return $this->build( $indexable, [ 'object_type' => 'home-page' ] ); } /** * Creates an indexable for the date archive. * * @param Indexable|bool $indexable Optional. An existing indexable to overwrite. * * @return Indexable The date archive indexable. */ public function build_for_date_archive( $indexable = false ) { return $this->build( $indexable, [ 'object_type' => 'date-archive' ] ); } /** * Creates an indexable for a post type archive. * * @param string $post_type The post type. * @param Indexable|bool $indexable Optional. An existing indexable to overwrite. * * @return Indexable The post type archive indexable. */ public function build_for_post_type_archive( $post_type, $indexable = false ) { $defaults = [ 'object_type' => 'post-type-archive', 'object_sub_type' => $post_type, ]; return $this->build( $indexable, $defaults ); } /** * Creates an indexable for a system page. * * @param string $page_type The type of system page. * @param Indexable|bool $indexable Optional. An existing indexable to overwrite. * * @return Indexable The search result indexable. */ public function build_for_system_page( $page_type, $indexable = false ) { $defaults = [ 'object_type' => 'system-page', 'object_sub_type' => $page_type, ]; return $this->build( $indexable, $defaults ); } /** * Ensures we have a valid indexable. Creates one if false is passed. * * @param Indexable|false $indexable The indexable. * @param array $defaults The initial properties of the Indexable. * * @return Indexable The indexable. */ protected function ensure_indexable( $indexable, $defaults = [] ) { if ( ! $indexable ) { return $this->indexable_repository->query()->create( $defaults ); } return $indexable; } /** * Build and author indexable from an author id if it does not exist yet, or if the author indexable needs to be upgraded. * * @param int $author_id The author id. * * @return Indexable|false The author indexable if it has been built, `false` if it could not be built. */ protected function maybe_build_author_indexable( $author_id ) { $author_indexable = $this->indexable_repository->find_by_id_and_type( $author_id, 'user', false ); if ( ! $author_indexable || $this->version_manager->indexable_needs_upgrade( $author_indexable ) ) { // Try to build the author. $author_defaults = [ 'object_type' => 'user', 'object_id' => $author_id, ]; $author_indexable = $this->build( $author_indexable, $author_defaults ); } return $author_indexable; } /** * Checks if the indexable type is one that is not supposed to have object ID for. * * @param string $type The type of the indexable. * * @return bool Whether the indexable type is one that is not supposed to have object ID for. */ protected function is_type_with_no_id( $type ) { return \in_array( $type, [ 'home-page', 'date-archive', 'post-type-archive', 'system-page' ], true ); } // phpcs:disable Squiz.Commenting.FunctionCommentThrowTag.Missing -- Exceptions are handled by the catch statement in the method. /** * Rebuilds an Indexable from scratch. * * @param Indexable $indexable The Indexable to (re)build. * @param array|null $defaults The object type of the Indexable. * * @return Indexable|false The resulting Indexable. */ public function build( $indexable, $defaults = null ) { // Backup the previous Indexable, if there was one. $indexable_before = ( $indexable ) ? $this->deep_copy_indexable( $indexable ) : null; // Make sure we have an Indexable to work with. $indexable = $this->ensure_indexable( $indexable, $defaults ); try { if ( $indexable->object_id === 0 ) { throw Not_Built_Exception::invalid_object_id( $indexable->object_id ); } switch ( $indexable->object_type ) { case 'post': $indexable = $this->post_builder->build( $indexable->object_id, $indexable ); // Save indexable, to make sure it can be queried when building related objects like the author indexable and hierarchy. $indexable = $this->indexable_helper->save_indexable( $indexable, $indexable_before ); // For attachments, we have to make sure to patch any potentially previously cleaned up SEO links. if ( \is_a( $indexable, Indexable::class ) && $indexable->object_sub_type === 'attachment' ) { $this->link_builder->patch_seo_links( $indexable ); } // Always rebuild the primary term. $this->primary_term_builder->build( $indexable->object_id ); // Always rebuild the hierarchy; this needs the primary term to run correctly. $this->hierarchy_builder->build( $indexable ); $this->maybe_build_author_indexable( $indexable->author_id ); // The indexable is already saved, so return early. return $indexable; case 'user': $indexable = $this->author_builder->build( $indexable->object_id, $indexable ); break; case 'term': $indexable = $this->term_builder->build( $indexable->object_id, $indexable ); // Save indexable, to make sure it can be queried when building hierarchy. $indexable = $this->indexable_helper->save_indexable( $indexable, $indexable_before ); $this->hierarchy_builder->build( $indexable ); // The indexable is already saved, so return early. return $indexable; case 'home-page': $indexable = $this->home_page_builder->build( $indexable ); break; case 'date-archive': $indexable = $this->date_archive_builder->build( $indexable ); break; case 'post-type-archive': $indexable = $this->post_type_archive_builder->build( $indexable->object_sub_type, $indexable ); break; case 'system-page': $indexable = $this->system_page_builder->build( $indexable->object_sub_type, $indexable ); break; } return $this->indexable_helper->save_indexable( $indexable, $indexable_before ); } catch ( Source_Exception $exception ) { if ( ! $this->is_type_with_no_id( $indexable->object_type ) && ! isset( $indexable->object_id ) ) { return false; } /** * The current indexable could not be indexed. Create a placeholder indexable, so we can * skip this indexable in future indexing runs. * * @var Indexable $indexable */ $indexable = $this->ensure_indexable( $indexable, [ 'object_id' => $indexable->object_id, 'object_type' => $indexable->object_type, 'post_status' => 'unindexed', 'version' => 0, ] ); // If we already had an existing indexable, mark it as unindexed. We cannot rely on its validity anymore. $indexable->post_status = 'unindexed'; // Make sure that the indexing process doesn't get stuck in a loop on this broken indexable. $indexable = $this->version_manager->set_latest( $indexable ); return $this->indexable_helper->save_indexable( $indexable, $indexable_before ); } catch ( Not_Built_Exception $exception ) { return false; } } // phpcs:enable } s->post_builder = $post_builder; $this->term_builder = $term_builder; $this->home_page_builder = $home_page_builder; $this->post_type_archive_builder = $post_type_archive_builder; $this->date_archive_builder = $date_archive_builder; $this->system_page_builder = $system_page_builder; $this->hierarchy_builder = $hierarchy_builder; $this->primary_term_builder = $primary_term_builder; $this->indexable_helper = $indexable_helper; $this->version_manager = $version_manager; $this->link_builder = $link_builder; } /** * Sets the indexable repository. Done to avoid circular dependencies. * * @required * * @param Indexable_Repository $indexable_repository The indexable repository. * * @return void */ public function set_indexable_repository( Indexable_Repository $indexable_repository ) { $this->indexable_repository = $indexable_repository; } /** * Creates a clean copy of an Indexable to allow for later database operations. * * @param Indexable $indexable The Indexable to copy. * * @return bool|Indexable */ protected function deep_copy_indexable( $indexable ) { return $this->indexable_repository ->query() ->create( $indexable->as_array() ); } /** * Creates an indexable by its ID and type. * * @param int $object_id The indexable object ID. * @param string $object_type The indexable object type. * @param Indexable|bool $indexable Optional. An existing indexable to overwrite. * * @return bool|Indexable Instance of indexable. False when unable to build. */ public function build_for_id_and_type( $object_id, $object_type, $indexable = false ) { $defaults = [ 'object_type' => $object_type, 'object_id' => $object_id, ]; $indexable = $this->build( $indexable, $defaults ); return $indexable; } /** * Creates an indexable for the homepage. * * @param Indexable|bool $indexable Optional. An existing indexable to overwrite. * * @return Indexable The home page indexable. */ public function build_for_home_page( $indexable = false ) { return $this->build( $indexable, [ 'object_type' => 'home-page' ] ); } /** * Creates an indexable for the date archive. * * @param Indexable|bool $indexable Optional. An existing indexable to overwrite. * * @return Indexable The date archive indexable. */ public function build_for_date_archive( $indexable = false ) { return $this->build( $indexable, [ 'object_type' => 'date-archive' ] ); } /** * Creates an indexable for a post type archive. * * @param string $post_type The post type. * @param Indexable|bool $indexable Optional. An existing indexable to overwrite. * * @return Indexable The post type archive indexable. */ public function build_for_post_type_archive( $post_type, $indexable = false ) { $defaults = [ 'object_type' => 'post-type-archive', 'object_sub_type' => $post_type, ]; return $this->build( $indexable, $defaults ); } /** * Creates an indexable for a system page. * * @param string $page_type The type of system page. * @param Indexable|bool $indexable Optional. An existing indexable to overwrite. * * @return Indexable The search result indexable. */ public function build_for_system_page( $page_type, $indexable = false ) { $defaults = [ 'object_type' => 'system-page', 'object_sub_type' => $page_type, ]; return $this->build( $indexable, $defaults ); } /** * Ensures we have a valid indexable. Creates one if false is passed. * * @param Indexable|false $indexable The indexable. * @param array $defaults The initial properties of the Indexable. * * @return Indexable The indexable. */ protected function ensure_indexable( $indexable, $defaults = [] ) { if ( ! $indexable ) { return $this->indexable_repository->query()->create( $defaults ); } return $indexable; } /** * Build and author indexable from an author id if it does not exist yet, or if the author indexable needs to be upgraded. * * @param int $author_id The author id. * * @return Indexable|false The author indexable if it has been built, `false` if it could not be built. */ protected function maybe_build_author_indexable( $author_id ) { $author_indexable = $this->indexable_repository->find_by_id_and_type( $author_id, 'user', false ); if ( ! $author_indexable || $this->version_manager->indexable_needs_upgrade( $author_indexable ) ) { // Try to build the author. $author_defaults = [ 'object_type' => 'user', 'object_id' => $author_id, ]; $author_indexable = $this->build( $author_indexable, $author_defaults ); } return $author_indexable; } /** * Checks if the indexable type is one that is not supposed to have object ID for. * * @param string $type The type of the indexable. * * @return bool Whether the indexable type is one that is not supposed to have object ID for. */ protected function is_type_with_no_id( $type ) { return \in_array( $type, [ 'home-page', 'date-archive', 'post-type-archive', 'system-page' ], true ); } // phpcs:disable Squiz.Commenting.FunctionCommentThrowTag.Missing -- Exceptions are handled by the catch statement in the method. /** * Rebuilds an Indexable from scratch. * * @param Indexable $indexable The Indexable to (re)build. * @param array|null $defaults The object type of the Indexable. * * @return Indexable|false The resulting Indexable. */ public function build( $indexable, $defaults = null ) { // Backup the previous Indexable, if there was one. $indexable_before = ( $indexable ) ? $this->deep_copy_indexable( $indexable ) : null; // Make sure we have an Indexable to work with. $indexable = $this->ensure_indexable( $indexable, $defaults ); try { if ( $indexable->object_id === 0 ) { throw Not_Built_Exception::invalid_object_id( $indexable->object_id ); } switch ( $indexable->object_type ) { case 'post': $indexable = $this->post_builder->build( $indexable->object_id, $indexable ); // Save indexable, to make sure it can be queried when building related objects like the author indexable and hierarchy. $indexable = $this->indexable_helper->save_indexable( $indexable, $indexable_before ); // For attachments, we have to make sure to patch any potentially previously cleaned up SEO links. if ( \is_a( $indexable, Indexable::class ) && $indexable->object_sub_type === 'attachment' ) { $this->link_builder->patch_seo_links( $indexable ); } // Always rebuild the primary term. $this->primary_term_builder->build( $indexable->object_id ); // Always rebuild the hierarchy; this needs the primary term to run correctly. $this->hierarchy_builder->build( $indexable ); $this->maybe_build_author_indexable( $indexable->author_id ); // The indexable is already saved, so return early. return $indexable; case 'user': $indexable = $this->author_builder->build( $indexable->object_id, $indexable ); break; case 'term': $indexable = $this->term_builder->build( $indexable->object_id, $indexable ); // Save indexable, to make sure it can be queried when building hierarchy. $indexable = $this->indexable_helper->save_indexable( $indexable, $indexable_before ); $this->hierarchy_builder->build( $indexable ); // The indexable is already saved, so return early. return $indexable; case 'home-page': $indexable = $this->home_page_builder->build( $indexable ); break; case 'date-archive': $indexable = $this->date_archive_builder->build( $indexable ); break; case 'post-type-archive': $indexable = $this->post_type_archive_builder->build( $indexable->object_sub_type, $indexable ); break; case 'system-page': $indexable = $this->system_page_builder->build( $indexable->object_sub_type, $indexable ); break; } return $this->indexable_helper->save_indexable( $indexable, $indexable_before ); } catch ( Source_Exception $exception ) { if ( ! $this->is_type_with_no_id( $indexable->object_type ) && ! isset( $indexable->object_id ) ) { return false; } /** * The current indexable could not be indexed. Create a placeholder indexable, so we can * skip this indexable in future indexing runs. * * @var Indexable $indexable */ $indexable = $this->ensure_indexable( $indexable, [ 'object_id' => $indexable->object_id, 'object_type' => $indexable->object_type, 'post_status' => 'unindexed', 'version' => 0, ] ); // If we already had an existing indexable, mark it as unindexed. We cannot rely on its validity anymore. $indexable->post_status = 'unindexed'; // Make sure that the indexing process doesn't get stuck in a loop on this broken indexable. $indexable = $this->version_manager->set_latest( $indexable ); return $this->indexable_helper->save_indexable( $indexable, $indexable_before ); } catch ( Not_Built_Exception $exception ) { return false; } } // phpcs:enable } s->post_builder = $post_builder; $this->term_builder = $term_builder; $this->home_page_builder = $home_page_builder; $this->post_type_archive_builder = $post_type_archive_builder; $this->date_archive_builder = $date_archive_builder; $this->system_page_builder = $system_page_builder; $this->hierarchy_builder = $hierarchy_builder; $this->primary_term_builder = $primary_term_builder; $this->indexable_helper = $indexable_helper; $this->version_manager = $version_manager; $this->link_builder = $link_builder; } /** * Sets the indexable repository. Done to avoid circular dependencies. * * @required * * @param Indexable_Repository $indexable_repository The indexable repository. * * @return void */ public function set_indexable_repository( Indexable_Repository $indexable_repository ) { $this->indexable_repository = $indexable_repository; } /** * Creates a clean copy of an Indexable to allow for later database operations. * * @param Indexable $indexable The Indexable to copy. * * @return bool|Indexable */ protected function deep_copy_indexable( $indexable ) { return $this->indexable_repository ->query() ->create( $indexable->as_array() ); } /** * Creates an indexable by its ID and type. * * @param int $object_id The indexable object ID. * @param string $object_type The indexable object type. * @param Indexable|bool $indexable Optional. An existing indexable to overwrite. * * @return bool|Indexable Instance of indexable. False when unable to build. */ public function build_for_id_and_type( $object_id, $object_type, $indexable = false ) { $defaults = [ 'object_type' => $object_type, 'object_id' => $object_id, ]; $indexable = $this->build( $indexable, $defaults ); return $indexable; } /** * Creates an indexable for the homepage. * * @param Indexable|bool $indexable Optional. An existing indexable to overwrite. * * @return Indexable The home page indexable. */ public function build_for_home_page( $indexable = false ) { return $this->build( $indexable, [ 'object_type' => 'home-page' ] ); } /** * Creates an indexable for the date archive. * * @param Indexable|bool $indexable Optional. An existing indexable to overwrite. * * @return Indexable The date archive indexable. */ public function build_for_date_archive( $indexable = false ) { return $this->build( $indexable, [ 'object_type' => 'date-archive' ] ); } /** * Creates an indexable for a post type archive. * * @param string $post_type The post type. * @param Indexable|bool $indexable Optional. An existing indexable to overwrite. * * @return Indexable The post type archive indexable. */ public function build_for_post_type_archive( $post_type, $indexable = false ) { $defaults = [ 'object_type' => 'post-type-archive', 'object_sub_type' => $post_type, ]; return $this->build( $indexable, $defaults ); } /** * Creates an indexable for a system page. * * @param string $page_type The type of system page. * @param Indexable|bool $indexable Optional. An existing indexable to overwrite. * * @return Indexable The search result indexable. */ public function build_for_system_page( $page_type, $indexable = false ) { $defaults = [ 'object_type' => 'system-page', 'object_sub_type' => $page_type, ]; return $this->build( $indexable, $defaults ); } /** * Ensures we have a valid indexable. Creates one if false is passed. * * @param Indexable|false $indexable The indexable. * @param array $defaults The initial properties of the Indexable. * * @return Indexable The indexable. */ protected function ensure_indexable( $indexable, $defaults = [] ) { if ( ! $indexable ) { return $this->indexable_repository->query()->create( $defaults ); } return $indexable; } /** * Build and author indexable from an author id if it does not exist yet, or if the author indexable needs to be upgraded. * * @param int $author_id The author id. * * @return Indexable|false The author indexable if it has been built, `false` if it could not be built. */ protected function maybe_build_author_indexable( $author_id ) { $author_indexable = $this->indexable_repository->find_by_id_and_type( $author_id, 'user', false ); if ( ! $author_indexable || $this->version_manager->indexable_needs_upgrade( $author_indexable ) ) { // Try to build the author. $author_defaults = [ 'object_type' => 'user', 'object_id' => $author_id, ]; $author_indexable = $this->build( $author_indexable, $author_defaults ); } return $author_indexable; } /** * Checks if the indexable type is one that is not supposed to have object ID for. * * @param string $type The type of the indexable. * * @return bool Whether the indexable type is one that is not supposed to have object ID for. */ protected function is_type_with_no_id( $type ) { return \in_array( $type, [ 'home-page', 'date-archive', 'post-type-archive', 'system-page' ], true ); } // phpcs:disable Squiz.Commenting.FunctionCommentThrowTag.Missing -- Exceptions are handled by the catch statement in the method. /** * Rebuilds an Indexable from scratch. * * @param Indexable $indexable The Indexable to (re)build. * @param array|null $defaults The object type of the Indexable. * * @return Indexable|false The resulting Indexable. */ public function build( $indexable, $defaults = null ) { // Backup the previous Indexable, if there was one. $indexable_before = ( $indexable ) ? $this->deep_copy_indexable( $indexable ) : null; // Make sure we have an Indexable to work with. $indexable = $this->ensure_indexable( $indexable, $defaults ); try { if ( $indexable->object_id === 0 ) { throw Not_Built_Exception::invalid_object_id( $indexable->object_id ); } switch ( $indexable->object_type ) { case 'post': $indexable = $this->post_builder->build( $indexable->object_id, $indexable ); // Save indexable, to make sure it can be queried when building related objects like the author indexable and hierarchy. $indexable = $this->indexable_helper->save_indexable( $indexable, $indexable_before ); // For attachments, we have to make sure to patch any potentially previously cleaned up SEO links. if ( \is_a( $indexable, Indexable::class ) && $indexable->object_sub_type === 'attachment' ) { $this->link_builder->patch_seo_links( $indexable ); } // Always rebuild the primary term. $this->primary_term_builder->build( $indexable->object_id ); // Always rebuild the hierarchy; this needs the primary term to run correctly. $this->hierarchy_builder->build( $indexable ); $this->maybe_build_author_indexable( $indexable->author_id ); // The indexable is already saved, so return early. return $indexable; case 'user': $indexable = $this->author_builder->build( $indexable->object_id, $indexable ); break; case 'term': $indexable = $this->term_builder->build( $indexable->object_id, $indexable ); // Save indexable, to make sure it can be queried when building hierarchy. $indexable = $this->indexable_helper->save_indexable( $indexable, $indexable_before ); $this->hierarchy_builder->build( $indexable ); // The indexable is already saved, so return early. return $indexable; case 'home-page': $indexable = $this->home_page_builder->build( $indexable ); break; case 'date-archive': $indexable = $this->date_archive_builder->build( $indexable ); break; case 'post-type-archive': $indexable = $this->post_type_archive_builder->build( $indexable->object_sub_type, $indexable ); break; case 'system-page': $indexable = $this->system_page_builder->build( $indexable->object_sub_type, $indexable ); break; } return $this->indexable_helper->save_indexable( $indexable, $indexable_before ); } catch ( Source_Exception $exception ) { if ( ! $this->is_type_with_no_id( $indexable->object_type ) && ! isset( $indexable->object_id ) ) { return false; } /** * The current indexable could not be indexed. Create a placeholder indexable, so we can * skip this indexable in future indexing runs. * * @var Indexable $indexable */ $indexable = $this->ensure_indexable( $indexable, [ 'object_id' => $indexable->object_id, 'object_type' => $indexable->object_type, 'post_status' => 'unindexed', 'version' => 0, ] ); // If we already had an existing indexable, mark it as unindexed. We cannot rely on its validity anymore. $indexable->post_status = 'unindexed'; // Make sure that the indexing process doesn't get stuck in a loop on this broken indexable. $indexable = $this->version_manager->set_latest( $indexable ); return $this->indexable_helper->save_indexable( $indexable, $indexable_before ); } catch ( Not_Built_Exception $exception ) { return false; } } // phpcs:enable } s->post_builder = $post_builder; $this->term_builder = $term_builder; $this->home_page_builder = $home_page_builder; $this->post_type_archive_builder = $post_type_archive_builder; $this->date_archive_builder = $date_archive_builder; $this->system_page_builder = $system_page_builder; $this->hierarchy_builder = $hierarchy_builder; $this->primary_term_builder = $primary_term_builder; $this->indexable_helper = $indexable_helper; $this->version_manager = $version_manager; $this->link_builder = $link_builder; } /** * Sets the indexable repository. Done to avoid circular dependencies. * * @required * * @param Indexable_Repository $indexable_repository The indexable repository. * * @return void */ public function set_indexable_repository( Indexable_Repository $indexable_repository ) { $this->indexable_repository = $indexable_repository; } /** * Creates a clean copy of an Indexable to allow for later database operations. * * @param Indexable $indexable The Indexable to copy. * * @return bool|Indexable */ protected function deep_copy_indexable( $indexable ) { return $this->indexable_repository ->query() ->create( $indexable->as_array() ); } /** * Creates an indexable by its ID and type. * * @param int $object_id The indexable object ID. * @param string $object_type The indexable object type. * @param Indexable|bool $indexable Optional. An existing indexable to overwrite. * * @return bool|Indexable Instance of indexable. False when unable to build. */ public function build_for_id_and_type( $object_id, $object_type, $indexable = false ) { $defaults = [ 'object_type' => $object_type, 'object_id' => $object_id, ]; $indexable = $this->build( $indexable, $defaults ); return $indexable; } /** * Creates an indexable for the homepage. * * @param Indexable|bool $indexable Optional. An existing indexable to overwrite. * * @return Indexable The home page indexable. */ public function build_for_home_page( $indexable = false ) { return $this->build( $indexable, [ 'object_type' => 'home-page' ] ); } /** * Creates an indexable for the date archive. * * @param Indexable|bool $indexable Optional. An existing indexable to overwrite. * * @return Indexable The date archive indexable. */ public function build_for_date_archive( $indexable = false ) { return $this->build( $indexable, [ 'object_type' => 'date-archive' ] ); } /** * Creates an indexable for a post type archive. * * @param string $post_type The post type. * @param Indexable|bool $indexable Optional. An existing indexable to overwrite. * * @return Indexable The post type archive indexable. */ public function build_for_post_type_archive( $post_type, $indexable = false ) { $defaults = [ 'object_type' => 'post-type-archive', 'object_sub_type' => $post_type, ]; return $this->build( $indexable, $defaults ); } /** * Creates an indexable for a system page. * * @param string $page_type The type of system page. * @param Indexable|bool $indexable Optional. An existing indexable to overwrite. * * @return Indexable The search result indexable. */ public function build_for_system_page( $page_type, $indexable = false ) { $defaults = [ 'object_type' => 'system-page', 'object_sub_type' => $page_type, ]; return $this->build( $indexable, $defaults ); } /** * Ensures we have a valid indexable. Creates one if false is passed. * * @param Indexable|false $indexable The indexable. * @param array $defaults The initial properties of the Indexable. * * @return Indexable The indexable. */ protected function ensure_indexable( $indexable, $defaults = [] ) { if ( ! $indexable ) { return $this->indexable_repository->query()->create( $defaults ); } return $indexable; } /** * Build and author indexable from an author id if it does not exist yet, or if the author indexable needs to be upgraded. * * @param int $author_id The author id. * * @return Indexable|false The author indexable if it has been built, `false` if it could not be built. */ protected function maybe_build_author_indexable( $author_id ) { $author_indexable = $this->indexable_repository->find_by_id_and_type( $author_id, 'user', false ); if ( ! $author_indexable || $this->version_manager->indexable_needs_upgrade( $author_indexable ) ) { // Try to build the author. $author_defaults = [ 'object_type' => 'user', 'object_id' => $author_id, ]; $author_indexable = $this->build( $author_indexable, $author_defaults ); } return $author_indexable; } /** * Checks if the indexable type is one that is not supposed to have object ID for. * * @param string $type The type of the indexable. * * @return bool Whether the indexable type is one that is not supposed to have object ID for. */ protected function is_type_with_no_id( $type ) { return \in_array( $type, [ 'home-page', 'date-archive', 'post-type-archive', 'system-page' ], true ); } // phpcs:disable Squiz.Commenting.FunctionCommentThrowTag.Missing -- Exceptions are handled by the catch statement in the method. /** * Rebuilds an Indexable from scratch. * * @param Indexable $indexable The Indexable to (re)build. * @param array|null $defaults The object type of the Indexable. * * @return Indexable|false The resulting Indexable. */ public function build( $indexable, $defaults = null ) { // Backup the previous Indexable, if there was one. $indexable_before = ( $indexable ) ? $this->deep_copy_indexable( $indexable ) : null; // Make sure we have an Indexable to work with. $indexable = $this->ensure_indexable( $indexable, $defaults ); try { if ( $indexable->object_id === 0 ) { throw Not_Built_Exception::invalid_object_id( $indexable->object_id ); } switch ( $indexable->object_type ) { case 'post': $indexable = $this->post_builder->build( $indexable->object_id, $indexable ); // Save indexable, to make sure it can be queried when building related objects like the author indexable and hierarchy. $indexable = $this->indexable_helper->save_indexable( $indexable, $indexable_before ); // For attachments, we have to make sure to patch any potentially previously cleaned up SEO links. if ( \is_a( $indexable, Indexable::class ) && $indexable->object_sub_type === 'attachment' ) { $this->link_builder->patch_seo_links( $indexable ); } // Always rebuild the primary term. $this->primary_term_builder->build( $indexable->object_id ); // Always rebuild the hierarchy; this needs the primary term to run correctly. $this->hierarchy_builder->build( $indexable ); $this->maybe_build_author_indexable( $indexable->author_id ); // The indexable is already saved, so return early. return $indexable; case 'user': $indexable = $this->author_builder->build( $indexable->object_id, $indexable ); break; case 'term': $indexable = $this->term_builder->build( $indexable->object_id, $indexable ); // Save indexable, to make sure it can be queried when building hierarchy. $indexable = $this->indexable_helper->save_indexable( $indexable, $indexable_before ); $this->hierarchy_builder->build( $indexable ); // The indexable is already saved, so return early. return $indexable; case 'home-page': $indexable = $this->home_page_builder->build( $indexable ); break; case 'date-archive': $indexable = $this->date_archive_builder->build( $indexable ); break; case 'post-type-archive': $indexable = $this->post_type_archive_builder->build( $indexable->object_sub_type, $indexable ); break; case 'system-page': $indexable = $this->system_page_builder->build( $indexable->object_sub_type, $indexable ); break; } return $this->indexable_helper->save_indexable( $indexable, $indexable_before ); } catch ( Source_Exception $exception ) { if ( ! $this->is_type_with_no_id( $indexable->object_type ) && ! isset( $indexable->object_id ) ) { return false; } /** * The current indexable could not be indexed. Create a placeholder indexable, so we can * skip this indexable in future indexing runs. * * @var Indexable $indexable */ $indexable = $this->ensure_indexable( $indexable, [ 'object_id' => $indexable->object_id, 'object_type' => $indexable->object_type, 'post_status' => 'unindexed', 'version' => 0, ] ); // If we already had an existing indexable, mark it as unindexed. We cannot rely on its validity anymore. $indexable->post_status = 'unindexed'; // Make sure that the indexing process doesn't get stuck in a loop on this broken indexable. $indexable = $this->version_manager->set_latest( $indexable ); return $this->indexable_helper->save_indexable( $indexable, $indexable_before ); } catch ( Not_Built_Exception $exception ) { return false; } } // phpcs:enable } s->post_builder = $post_builder; $this->term_builder = $term_builder; $this->home_page_builder = $home_page_builder; $this->post_type_archive_builder = $post_type_archive_builder; $this->date_archive_builder = $date_archive_builder; $this->system_page_builder = $system_page_builder; $this->hierarchy_builder = $hierarchy_builder; $this->primary_term_builder = $primary_term_builder; $this->indexable_helper = $indexable_helper; $this->version_manager = $version_manager; $this->link_builder = $link_builder; } /** * Sets the indexable repository. Done to avoid circular dependencies. * * @required * * @param Indexable_Repository $indexable_repository The indexable repository. * * @return void */ public function set_indexable_repository( Indexable_Repository $indexable_repository ) { $this->indexable_repository = $indexable_repository; } /** * Creates a clean copy of an Indexable to allow for later database operations. * * @param Indexable $indexable The Indexable to copy. * * @return bool|Indexable */ protected function deep_copy_indexable( $indexable ) { return $this->indexable_repository ->query() ->create( $indexable->as_array() ); } /** * Creates an indexable by its ID and type. * * @param int $object_id The indexable object ID. * @param string $object_type The indexable object type. * @param Indexable|bool $indexable Optional. An existing indexable to overwrite. * * @return bool|Indexable Instance of indexable. False when unable to build. */ public function build_for_id_and_type( $object_id, $object_type, $indexable = false ) { $defaults = [ 'object_type' => $object_type, 'object_id' => $object_id, ]; $indexable = $this->build( $indexable, $defaults ); return $indexable; } /** * Creates an indexable for the homepage. * * @param Indexable|bool $indexable Optional. An existing indexable to overwrite. * * @return Indexable The home page indexable. */ public function build_for_home_page( $indexable = false ) { return $this->build( $indexable, [ 'object_type' => 'home-page' ] ); } /** * Creates an indexable for the date archive. * * @param Indexable|bool $indexable Optional. An existing indexable to overwrite. * * @return Indexable The date archive indexable. */ public function build_for_date_archive( $indexable = false ) { return $this->build( $indexable, [ 'object_type' => 'date-archive' ] ); } /** * Creates an indexable for a post type archive. * * @param string $post_type The post type. * @param Indexable|bool $indexable Optional. An existing indexable to overwrite. * * @return Indexable The post type archive indexable. */ public function build_for_post_type_archive( $post_type, $indexable = false ) { $defaults = [ 'object_type' => 'post-type-archive', 'object_sub_type' => $post_type, ]; return $this->build( $indexable, $defaults ); } /** * Creates an indexable for a system page. * * @param string $page_type The type of system page. * @param Indexable|bool $indexable Optional. An existing indexable to overwrite. * * @return Indexable The search result indexable. */ public function build_for_system_page( $page_type, $indexable = false ) { $defaults = [ 'object_type' => 'system-page', 'object_sub_type' => $page_type, ]; return $this->build( $indexable, $defaults ); } /** * Ensures we have a valid indexable. Creates one if false is passed. * * @param Indexable|false $indexable The indexable. * @param array $defaults The initial properties of the Indexable. * * @return Indexable The indexable. */ protected function ensure_indexable( $indexable, $defaults = [] ) { if ( ! $indexable ) { return $this->indexable_repository->query()->create( $defaults ); } return $indexable; } /** * Build and author indexable from an author id if it does not exist yet, or if the author indexable needs to be upgraded. * * @param int $author_id The author id. * * @return Indexable|false The author indexable if it has been built, `false` if it could not be built. */ protected function maybe_build_author_indexable( $author_id ) { $author_indexable = $this->indexable_repository->find_by_id_and_type( $author_id, 'user', false ); if ( ! $author_indexable || $this->version_manager->indexable_needs_upgrade( $author_indexable ) ) { // Try to build the author. $author_defaults = [ 'object_type' => 'user', 'object_id' => $author_id, ]; $author_indexable = $this->build( $author_indexable, $author_defaults ); } return $author_indexable; } /** * Checks if the indexable type is one that is not supposed to have object ID for. * * @param string $type The type of the indexable. * * @return bool Whether the indexable type is one that is not supposed to have object ID for. */ protected function is_type_with_no_id( $type ) { return \in_array( $type, [ 'home-page', 'date-archive', 'post-type-archive', 'system-page' ], true ); } // phpcs:disable Squiz.Commenting.FunctionCommentThrowTag.Missing -- Exceptions are handled by the catch statement in the method. /** * Rebuilds an Indexable from scratch. * * @param Indexable $indexable The Indexable to (re)build. * @param array|null $defaults The object type of the Indexable. * * @return Indexable|false The resulting Indexable. */ public function build( $indexable, $defaults = null ) { // Backup the previous Indexable, if there was one. $indexable_before = ( $indexable ) ? $this->deep_copy_indexable( $indexable ) : null; // Make sure we have an Indexable to work with. $indexable = $this->ensure_indexable( $indexable, $defaults ); try { if ( $indexable->object_id === 0 ) { throw Not_Built_Exception::invalid_object_id( $indexable->object_id ); } switch ( $indexable->object_type ) { case 'post': $indexable = $this->post_builder->build( $indexable->object_id, $indexable ); // Save indexable, to make sure it can be queried when building related objects like the author indexable and hierarchy. $indexable = $this->indexable_helper->save_indexable( $indexable, $indexable_before ); // For attachments, we have to make sure to patch any potentially previously cleaned up SEO links. if ( \is_a( $indexable, Indexable::class ) && $indexable->object_sub_type === 'attachment' ) { $this->link_builder->patch_seo_links( $indexable ); } // Always rebuild the primary term. $this->primary_term_builder->build( $indexable->object_id ); // Always rebuild the hierarchy; this needs the primary term to run correctly. $this->hierarchy_builder->build( $indexable ); $this->maybe_build_author_indexable( $indexable->author_id ); // The indexable is already saved, so return early. return $indexable; case 'user': $indexable = $this->author_builder->build( $indexable->object_id, $indexable ); break; case 'term': $indexable = $this->term_builder->build( $indexable->object_id, $indexable ); // Save indexable, to make sure it can be queried when building hierarchy. $indexable = $this->indexable_helper->save_indexable( $indexable, $indexable_before ); $this->hierarchy_builder->build( $indexable ); // The indexable is already saved, so return early. return $indexable; case 'home-page': $indexable = $this->home_page_builder->build( $indexable ); break; case 'date-archive': $indexable = $this->date_archive_builder->build( $indexable ); break; case 'post-type-archive': $indexable = $this->post_type_archive_builder->build( $indexable->object_sub_type, $indexable ); break; case 'system-page': $indexable = $this->system_page_builder->build( $indexable->object_sub_type, $indexable ); break; } return $this->indexable_helper->save_indexable( $indexable, $indexable_before ); } catch ( Source_Exception $exception ) { if ( ! $this->is_type_with_no_id( $indexable->object_type ) && ! isset( $indexable->object_id ) ) { return false; } /** * The current indexable could not be indexed. Create a placeholder indexable, so we can * skip this indexable in future indexing runs. * * @var Indexable $indexable */ $indexable = $this->ensure_indexable( $indexable, [ 'object_id' => $indexable->object_id, 'object_type' => $indexable->object_type, 'post_status' => 'unindexed', 'version' => 0, ] ); // If we already had an existing indexable, mark it as unindexed. We cannot rely on its validity anymore. $indexable->post_status = 'unindexed'; // Make sure that the indexing process doesn't get stuck in a loop on this broken indexable. $indexable = $this->version_manager->set_latest( $indexable ); return $this->indexable_helper->save_indexable( $indexable, $indexable_before ); } catch ( Not_Built_Exception $exception ) { return false; } } // phpcs:enable } s->post_builder = $post_builder; $this->term_builder = $term_builder; $this->home_page_builder = $home_page_builder; $this->post_type_archive_builder = $post_type_archive_builder; $this->date_archive_builder = $date_archive_builder; $this->system_page_builder = $system_page_builder; $this->hierarchy_builder = $hierarchy_builder; $this->primary_term_builder = $primary_term_builder; $this->indexable_helper = $indexable_helper; $this->version_manager = $version_manager; $this->link_builder = $link_builder; } /** * Sets the indexable repository. Done to avoid circular dependencies. * * @required * * @param Indexable_Repository $indexable_repository The indexable repository. * * @return void */ public function set_indexable_repository( Indexable_Repository $indexable_repository ) { $this->indexable_repository = $indexable_repository; } /** * Creates a clean copy of an Indexable to allow for later database operations. * * @param Indexable $indexable The Indexable to copy. * * @return bool|Indexable */ protected function deep_copy_indexable( $indexable ) { return $this->indexable_repository ->query() ->create( $indexable->as_array() ); } /** * Creates an indexable by its ID and type. * * @param int $object_id The indexable object ID. * @param string $object_type The indexable object type. * @param Indexable|bool $indexable Optional. An existing indexable to overwrite. * * @return bool|Indexable Instance of indexable. False when unable to build. */ public function build_for_id_and_type( $object_id, $object_type, $indexable = false ) { $defaults = [ 'object_type' => $object_type, 'object_id' => $object_id, ]; $indexable = $this->build( $indexable, $defaults ); return $indexable; } /** * Creates an indexable for the homepage. * * @param Indexable|bool $indexable Optional. An existing indexable to overwrite. * * @return Indexable The home page indexable. */ public function build_for_home_page( $indexable = false ) { return $this->build( $indexable, [ 'object_type' => 'home-page' ] ); } /** * Creates an indexable for the date archive. * * @param Indexable|bool $indexable Optional. An existing indexable to overwrite. * * @return Indexable The date archive indexable. */ public function build_for_date_archive( $indexable = false ) { return $this->build( $indexable, [ 'object_type' => 'date-archive' ] ); } /** * Creates an indexable for a post type archive. * * @param string $post_type The post type. * @param Indexable|bool $indexable Optional. An existing indexable to overwrite. * * @return Indexable The post type archive indexable. */ public function build_for_post_type_archive( $post_type, $indexable = false ) { $defaults = [ 'object_type' => 'post-type-archive', 'object_sub_type' => $post_type, ]; return $this->build( $indexable, $defaults ); } /** * Creates an indexable for a system page. * * @param string $page_type The type of system page. * @param Indexable|bool $indexable Optional. An existing indexable to overwrite. * * @return Indexable The search result indexable. */ public function build_for_system_page( $page_type, $indexable = false ) { $defaults = [ 'object_type' => 'system-page', 'object_sub_type' => $page_type, ]; return $this->build( $indexable, $defaults ); } /** * Ensures we have a valid indexable. Creates one if false is passed. * * @param Indexable|false $indexable The indexable. * @param array $defaults The initial properties of the Indexable. * * @return Indexable The indexable. */ protected function ensure_indexable( $indexable, $defaults = [] ) { if ( ! $indexable ) { return $this->indexable_repository->query()->create( $defaults ); } return $indexable; } /** * Build and author indexable from an author id if it does not exist yet, or if the author indexable needs to be upgraded. * * @param int $author_id The author id. * * @return Indexable|false The author indexable if it has been built, `false` if it could not be built. */ protected function maybe_build_author_indexable( $author_id ) { $author_indexable = $this->indexable_repository->find_by_id_and_type( $author_id, 'user', false ); if ( ! $author_indexable || $this->version_manager->indexable_needs_upgrade( $author_indexable ) ) { // Try to build the author. $author_defaults = [ 'object_type' => 'user', 'object_id' => $author_id, ]; $author_indexable = $this->build( $author_indexable, $author_defaults ); } return $author_indexable; } /** * Checks if the indexable type is one that is not supposed to have object ID for. * * @param string $type The type of the indexable. * * @return bool Whether the indexable type is one that is not supposed to have object ID for. */ protected function is_type_with_no_id( $type ) { return \in_array( $type, [ 'home-page', 'date-archive', 'post-type-archive', 'system-page' ], true ); } // phpcs:disable Squiz.Commenting.FunctionCommentThrowTag.Missing -- Exceptions are handled by the catch statement in the method. /** * Rebuilds an Indexable from scratch. * * @param Indexable $indexable The Indexable to (re)build. * @param array|null $defaults The object type of the Indexable. * * @return Indexable|false The resulting Indexable. */ public function build( $indexable, $defaults = null ) { // Backup the previous Indexable, if there was one. $indexable_before = ( $indexable ) ? $this->deep_copy_indexable( $indexable ) : null; // Make sure we have an Indexable to work with. $indexable = $this->ensure_indexable( $indexable, $defaults ); try { if ( $indexable->object_id === 0 ) { throw Not_Built_Exception::invalid_object_id( $indexable->object_id ); } switch ( $indexable->object_type ) { case 'post': $indexable = $this->post_builder->build( $indexable->object_id, $indexable ); // Save indexable, to make sure it can be queried when building related objects like the author indexable and hierarchy. $indexable = $this->indexable_helper->save_indexable( $indexable, $indexable_before ); // For attachments, we have to make sure to patch any potentially previously cleaned up SEO links. if ( \is_a( $indexable, Indexable::class ) && $indexable->object_sub_type === 'attachment' ) { $this->link_builder->patch_seo_links( $indexable ); } // Always rebuild the primary term. $this->primary_term_builder->build( $indexable->object_id ); // Always rebuild the hierarchy; this needs the primary term to run correctly. $this->hierarchy_builder->build( $indexable ); $this->maybe_build_author_indexable( $indexable->author_id ); // The indexable is already saved, so return early. return $indexable; case 'user': $indexable = $this->author_builder->build( $indexable->object_id, $indexable ); break; case 'term': $indexable = $this->term_builder->build( $indexable->object_id, $indexable ); // Save indexable, to make sure it can be queried when building hierarchy. $indexable = $this->indexable_helper->save_indexable( $indexable, $indexable_before ); $this->hierarchy_builder->build( $indexable ); // The indexable is already saved, so return early. return $indexable; case 'home-page': $indexable = $this->home_page_builder->build( $indexable ); break; case 'date-archive': $indexable = $this->date_archive_builder->build( $indexable ); break; case 'post-type-archive': $indexable = $this->post_type_archive_builder->build( $indexable->object_sub_type, $indexable ); break; case 'system-page': $indexable = $this->system_page_builder->build( $indexable->object_sub_type, $indexable ); break; } return $this->indexable_helper->save_indexable( $indexable, $indexable_before ); } catch ( Source_Exception $exception ) { if ( ! $this->is_type_with_no_id( $indexable->object_type ) && ! isset( $indexable->object_id ) ) { return false; } /** * The current indexable could not be indexed. Create a placeholder indexable, so we can * skip this indexable in future indexing runs. * * @var Indexable $indexable */ $indexable = $this->ensure_indexable( $indexable, [ 'object_id' => $indexable->object_id, 'object_type' => $indexable->object_type, 'post_status' => 'unindexed', 'version' => 0, ] ); // If we already had an existing indexable, mark it as unindexed. We cannot rely on its validity anymore. $indexable->post_status = 'unindexed'; // Make sure that the indexing process doesn't get stuck in a loop on this broken indexable. $indexable = $this->version_manager->set_latest( $indexable ); return $this->indexable_helper->save_indexable( $indexable, $indexable_before ); } catch ( Not_Built_Exception $exception ) { return false; } } // phpcs:enable } s->post_builder = $post_builder; $this->term_builder = $term_builder; $this->home_page_builder = $home_page_builder; $this->post_type_archive_builder = $post_type_archive_builder; $this->date_archive_builder = $date_archive_builder; $this->system_page_builder = $system_page_builder; $this->hierarchy_builder = $hierarchy_builder; $this->primary_term_builder = $primary_term_builder; $this->indexable_helper = $indexable_helper; $this->version_manager = $version_manager; $this->link_builder = $link_builder; } /** * Sets the indexable repository. Done to avoid circular dependencies. * * @required * * @param Indexable_Repository $indexable_repository The indexable repository. * * @return void */ public function set_indexable_repository( Indexable_Repository $indexable_repository ) { $this->indexable_repository = $indexable_repository; } /** * Creates a clean copy of an Indexable to allow for later database operations. * * @param Indexable $indexable The Indexable to copy. * * @return bool|Indexable */ protected function deep_copy_indexable( $indexable ) { return $this->indexable_repository ->query() ->create( $indexable->as_array() ); } /** * Creates an indexable by its ID and type. * * @param int $object_id The indexable object ID. * @param string $object_type The indexable object type. * @param Indexable|bool $indexable Optional. An existing indexable to overwrite. * * @return bool|Indexable Instance of indexable. False when unable to build. */ public function build_for_id_and_type( $object_id, $object_type, $indexable = false ) { $defaults = [ 'object_type' => $object_type, 'object_id' => $object_id, ]; $indexable = $this->build( $indexable, $defaults ); return $indexable; } /** * Creates an indexable for the homepage. * * @param Indexable|bool $indexable Optional. An existing indexable to overwrite. * * @return Indexable The home page indexable. */ public function build_for_home_page( $indexable = false ) { return $this->build( $indexable, [ 'object_type' => 'home-page' ] ); } /** * Creates an indexable for the date archive. * * @param Indexable|bool $indexable Optional. An existing indexable to overwrite. * * @return Indexable The date archive indexable. */ public function build_for_date_archive( $indexable = false ) { return $this->build( $indexable, [ 'object_type' => 'date-archive' ] ); } /** * Creates an indexable for a post type archive. * * @param string $post_type The post type. * @param Indexable|bool $indexable Optional. An existing indexable to overwrite. * * @return Indexable The post type archive indexable. */ public function build_for_post_type_archive( $post_type, $indexable = false ) { $defaults = [ 'object_type' => 'post-type-archive', 'object_sub_type' => $post_type, ]; return $this->build( $indexable, $defaults ); } /** * Creates an indexable for a system page. * * @param string $page_type The type of system page. * @param Indexable|bool $indexable Optional. An existing indexable to overwrite. * * @return Indexable The search result indexable. */ public function build_for_system_page( $page_type, $indexable = false ) { $defaults = [ 'object_type' => 'system-page', 'object_sub_type' => $page_type, ]; return $this->build( $indexable, $defaults ); } /** * Ensures we have a valid indexable. Creates one if false is passed. * * @param Indexable|false $indexable The indexable. * @param array $defaults The initial properties of the Indexable. * * @return Indexable The indexable. */ protected function ensure_indexable( $indexable, $defaults = [] ) { if ( ! $indexable ) { return $this->indexable_repository->query()->create( $defaults ); } return $indexable; } /** * Build and author indexable from an author id if it does not exist yet, or if the author indexable needs to be upgraded. * * @param int $author_id The author id. * * @return Indexable|false The author indexable if it has been built, `false` if it could not be built. */ protected function maybe_build_author_indexable( $author_id ) { $author_indexable = $this->indexable_repository->find_by_id_and_type( $author_id, 'user', false ); if ( ! $author_indexable || $this->version_manager->indexable_needs_upgrade( $author_indexable ) ) { // Try to build the author. $author_defaults = [ 'object_type' => 'user', 'object_id' => $author_id, ]; $author_indexable = $this->build( $author_indexable, $author_defaults ); } return $author_indexable; } /** * Checks if the indexable type is one that is not supposed to have object ID for. * * @param string $type The type of the indexable. * * @return bool Whether the indexable type is one that is not supposed to have object ID for. */ protected function is_type_with_no_id( $type ) { return \in_array( $type, [ 'home-page', 'date-archive', 'post-type-archive', 'system-page' ], true ); } // phpcs:disable Squiz.Commenting.FunctionCommentThrowTag.Missing -- Exceptions are handled by the catch statement in the method. /** * Rebuilds an Indexable from scratch. * * @param Indexable $indexable The Indexable to (re)build. * @param array|null $defaults The object type of the Indexable. * * @return Indexable|false The resulting Indexable. */ public function build( $indexable, $defaults = null ) { // Backup the previous Indexable, if there was one. $indexable_before = ( $indexable ) ? $this->deep_copy_indexable( $indexable ) : null; // Make sure we have an Indexable to work with. $indexable = $this->ensure_indexable( $indexable, $defaults ); try { if ( $indexable->object_id === 0 ) { throw Not_Built_Exception::invalid_object_id( $indexable->object_id ); } switch ( $indexable->object_type ) { case 'post': $indexable = $this->post_builder->build( $indexable->object_id, $indexable ); // Save indexable, to make sure it can be queried when building related objects like the author indexable and hierarchy. $indexable = $this->indexable_helper->save_indexable( $indexable, $indexable_before ); // For attachments, we have to make sure to patch any potentially previously cleaned up SEO links. if ( \is_a( $indexable, Indexable::class ) && $indexable->object_sub_type === 'attachment' ) { $this->link_builder->patch_seo_links( $indexable ); } // Always rebuild the primary term. $this->primary_term_builder->build( $indexable->object_id ); // Always rebuild the hierarchy; this needs the primary term to run correctly. $this->hierarchy_builder->build( $indexable ); $this->maybe_build_author_indexable( $indexable->author_id ); // The indexable is already saved, so return early. return $indexable; case 'user': $indexable = $this->author_builder->build( $indexable->object_id, $indexable ); break; case 'term': $indexable = $this->term_builder->build( $indexable->object_id, $indexable ); // Save indexable, to make sure it can be queried when building hierarchy. $indexable = $this->indexable_helper->save_indexable( $indexable, $indexable_before ); $this->hierarchy_builder->build( $indexable ); // The indexable is already saved, so return early. return $indexable; case 'home-page': $indexable = $this->home_page_builder->build( $indexable ); break; case 'date-archive': $indexable = $this->date_archive_builder->build( $indexable ); break; case 'post-type-archive': $indexable = $this->post_type_archive_builder->build( $indexable->object_sub_type, $indexable ); break; case 'system-page': $indexable = $this->system_page_builder->build( $indexable->object_sub_type, $indexable ); break; } return $this->indexable_helper->save_indexable( $indexable, $indexable_before ); } catch ( Source_Exception $exception ) { if ( ! $this->is_type_with_no_id( $indexable->object_type ) && ! isset( $indexable->object_id ) ) { return false; } /** * The current indexable could not be indexed. Create a placeholder indexable, so we can * skip this indexable in future indexing runs. * * @var Indexable $indexable */ $indexable = $this->ensure_indexable( $indexable, [ 'object_id' => $indexable->object_id, 'object_type' => $indexable->object_type, 'post_status' => 'unindexed', 'version' => 0, ] ); // If we already had an existing indexable, mark it as unindexed. We cannot rely on its validity anymore. $indexable->post_status = 'unindexed'; // Make sure that the indexing process doesn't get stuck in a loop on this broken indexable. $indexable = $this->version_manager->set_latest( $indexable ); return $this->indexable_helper->save_indexable( $indexable, $indexable_before ); } catch ( Not_Built_Exception $exception ) { return false; } } // phpcs:enable } s->post_builder = $post_builder; $this->term_builder = $term_builder; $this->home_page_builder = $home_page_builder; $this->post_type_archive_builder = $post_type_archive_builder; $this->date_archive_builder = $date_archive_builder; $this->system_page_builder = $system_page_builder; $this->hierarchy_builder = $hierarchy_builder; $this->primary_term_builder = $primary_term_builder; $this->indexable_helper = $indexable_helper; $this->version_manager = $version_manager; $this->link_builder = $link_builder; } /** * Sets the indexable repository. Done to avoid circular dependencies. * * @required * * @param Indexable_Repository $indexable_repository The indexable repository. * * @return void */ public function set_indexable_repository( Indexable_Repository $indexable_repository ) { $this->indexable_repository = $indexable_repository; } /** * Creates a clean copy of an Indexable to allow for later database operations. * * @param Indexable $indexable The Indexable to copy. * * @return bool|Indexable */ protected function deep_copy_indexable( $indexable ) { return $this->indexable_repository ->query() ->create( $indexable->as_array() ); } /** * Creates an indexable by its ID and type. * * @param int $object_id The indexable object ID. * @param string $object_type The indexable object type. * @param Indexable|bool $indexable Optional. An existing indexable to overwrite. * * @return bool|Indexable Instance of indexable. False when unable to build. */ public function build_for_id_and_type( $object_id, $object_type, $indexable = false ) { $defaults = [ 'object_type' => $object_type, 'object_id' => $object_id, ]; $indexable = $this->build( $indexable, $defaults ); return $indexable; } /** * Creates an indexable for the homepage. * * @param Indexable|bool $indexable Optional. An existing indexable to overwrite. * * @return Indexable The home page indexable. */ public function build_for_home_page( $indexable = false ) { return $this->build( $indexable, [ 'object_type' => 'home-page' ] ); } /** * Creates an indexable for the date archive. * * @param Indexable|bool $indexable Optional. An existing indexable to overwrite. * * @return Indexable The date archive indexable. */ public function build_for_date_archive( $indexable = false ) { return $this->build( $indexable, [ 'object_type' => 'date-archive' ] ); } /** * Creates an indexable for a post type archive. * * @param string $post_type The post type. * @param Indexable|bool $indexable Optional. An existing indexable to overwrite. * * @return Indexable The post type archive indexable. */ public function build_for_post_type_archive( $post_type, $indexable = false ) { $defaults = [ 'object_type' => 'post-type-archive', 'object_sub_type' => $post_type, ]; return $this->build( $indexable, $defaults ); } /** * Creates an indexable for a system page. * * @param string $page_type The type of system page. * @param Indexable|bool $indexable Optional. An existing indexable to overwrite. * * @return Indexable The search result indexable. */ public function build_for_system_page( $page_type, $indexable = false ) { $defaults = [ 'object_type' => 'system-page', 'object_sub_type' => $page_type, ]; return $this->build( $indexable, $defaults ); } /** * Ensures we have a valid indexable. Creates one if false is passed. * * @param Indexable|false $indexable The indexable. * @param array $defaults The initial properties of the Indexable. * * @return Indexable The indexable. */ protected function ensure_indexable( $indexable, $defaults = [] ) { if ( ! $indexable ) { return $this->indexable_repository->query()->create( $defaults ); } return $indexable; } /** * Build and author indexable from an author id if it does not exist yet, or if the author indexable needs to be upgraded. * * @param int $author_id The author id. * * @return Indexable|false The author indexable if it has been built, `false` if it could not be built. */ protected function maybe_build_author_indexable( $author_id ) { $author_indexable = $this->indexable_repository->find_by_id_and_type( $author_id, 'user', false ); if ( ! $author_indexable || $this->version_manager->indexable_needs_upgrade( $author_indexable ) ) { // Try to build the author. $author_defaults = [ 'object_type' => 'user', 'object_id' => $author_id, ]; $author_indexable = $this->build( $author_indexable, $author_defaults ); } return $author_indexable; } /** * Checks if the indexable type is one that is not supposed to have object ID for. * * @param string $type The type of the indexable. * * @return bool Whether the indexable type is one that is not supposed to have object ID for. */ protected function is_type_with_no_id( $type ) { return \in_array( $type, [ 'home-page', 'date-archive', 'post-type-archive', 'system-page' ], true ); } // phpcs:disable Squiz.Commenting.FunctionCommentThrowTag.Missing -- Exceptions are handled by the catch statement in the method. /** * Rebuilds an Indexable from scratch. * * @param Indexable $indexable The Indexable to (re)build. * @param array|null $defaults The object type of the Indexable. * * @return Indexable|false The resulting Indexable. */ public function build( $indexable, $defaults = null ) { // Backup the previous Indexable, if there was one. $indexable_before = ( $indexable ) ? $this->deep_copy_indexable( $indexable ) : null; // Make sure we have an Indexable to work with. $indexable = $this->ensure_indexable( $indexable, $defaults ); try { if ( $indexable->object_id === 0 ) { throw Not_Built_Exception::invalid_object_id( $indexable->object_id ); } switch ( $indexable->object_type ) { case 'post': $indexable = $this->post_builder->build( $indexable->object_id, $indexable ); // Save indexable, to make sure it can be queried when building related objects like the author indexable and hierarchy. $indexable = $this->indexable_helper->save_indexable( $indexable, $indexable_before ); // For attachments, we have to make sure to patch any potentially previously cleaned up SEO links. if ( \is_a( $indexable, Indexable::class ) && $indexable->object_sub_type === 'attachment' ) { $this->link_builder->patch_seo_links( $indexable ); } // Always rebuild the primary term. $this->primary_term_builder->build( $indexable->object_id ); // Always rebuild the hierarchy; this needs the primary term to run correctly. $this->hierarchy_builder->build( $indexable ); $this->maybe_build_author_indexable( $indexable->author_id ); // The indexable is already saved, so return early. return $indexable; case 'user': $indexable = $this->author_builder->build( $indexable->object_id, $indexable ); break; case 'term': $indexable = $this->term_builder->build( $indexable->object_id, $indexable ); // Save indexable, to make sure it can be queried when building hierarchy. $indexable = $this->indexable_helper->save_indexable( $indexable, $indexable_before ); $this->hierarchy_builder->build( $indexable ); // The indexable is already saved, so return early. return $indexable; case 'home-page': $indexable = $this->home_page_builder->build( $indexable ); break; case 'date-archive': $indexable = $this->date_archive_builder->build( $indexable ); break; case 'post-type-archive': $indexable = $this->post_type_archive_builder->build( $indexable->object_sub_type, $indexable ); break; case 'system-page': $indexable = $this->system_page_builder->build( $indexable->object_sub_type, $indexable ); break; } return $this->indexable_helper->save_indexable( $indexable, $indexable_before ); } catch ( Source_Exception $exception ) { if ( ! $this->is_type_with_no_id( $indexable->object_type ) && ! isset( $indexable->object_id ) ) { return false; } /** * The current indexable could not be indexed. Create a placeholder indexable, so we can * skip this indexable in future indexing runs. * * @var Indexable $indexable */ $indexable = $this->ensure_indexable( $indexable, [ 'object_id' => $indexable->object_id, 'object_type' => $indexable->object_type, 'post_status' => 'unindexed', 'version' => 0, ] ); // If we already had an existing indexable, mark it as unindexed. We cannot rely on its validity anymore. $indexable->post_status = 'unindexed'; // Make sure that the indexing process doesn't get stuck in a loop on this broken indexable. $indexable = $this->version_manager->set_latest( $indexable ); return $this->indexable_helper->save_indexable( $indexable, $indexable_before ); } catch ( Not_Built_Exception $exception ) { return false; } } // phpcs:enable } s->post_builder = $post_builder; $this->term_builder = $term_builder; $this->home_page_builder = $home_page_builder; $this->post_type_archive_builder = $post_type_archive_builder; $this->date_archive_builder = $date_archive_builder; $this->system_page_builder = $system_page_builder; $this->hierarchy_builder = $hierarchy_builder; $this->primary_term_builder = $primary_term_builder; $this->indexable_helper = $indexable_helper; $this->version_manager = $version_manager; $this->link_builder = $link_builder; } /** * Sets the indexable repository. Done to avoid circular dependencies. * * @required * * @param Indexable_Repository $indexable_repository The indexable repository. * * @return void */ public function set_indexable_repository( Indexable_Repository $indexable_repository ) { $this->indexable_repository = $indexable_repository; } /** * Creates a clean copy of an Indexable to allow for later database operations. * * @param Indexable $indexable The Indexable to copy. * * @return bool|Indexable */ protected function deep_copy_indexable( $indexable ) { return $this->indexable_repository ->query() ->create( $indexable->as_array() ); } /** * Creates an indexable by its ID and type. * * @param int $object_id The indexable object ID. * @param string $object_type The indexable object type. * @param Indexable|bool $indexable Optional. An existing indexable to overwrite. * * @return bool|Indexable Instance of indexable. False when unable to build. */ public function build_for_id_and_type( $object_id, $object_type, $indexable = false ) { $defaults = [ 'object_type' => $object_type, 'object_id' => $object_id, ]; $indexable = $this->build( $indexable, $defaults ); return $indexable; } /** * Creates an indexable for the homepage. * * @param Indexable|bool $indexable Optional. An existing indexable to overwrite. * * @return Indexable The home page indexable. */ public function build_for_home_page( $indexable = false ) { return $this->build( $indexable, [ 'object_type' => 'home-page' ] ); } /** * Creates an indexable for the date archive. * * @param Indexable|bool $indexable Optional. An existing indexable to overwrite. * * @return Indexable The date archive indexable. */ public function build_for_date_archive( $indexable = false ) { return $this->build( $indexable, [ 'object_type' => 'date-archive' ] ); } /** * Creates an indexable for a post type archive. * * @param string $post_type The post type. * @param Indexable|bool $indexable Optional. An existing indexable to overwrite. * * @return Indexable The post type archive indexable. */ public function build_for_post_type_archive( $post_type, $indexable = false ) { $defaults = [ 'object_type' => 'post-type-archive', 'object_sub_type' => $post_type, ]; return $this->build( $indexable, $defaults ); } /** * Creates an indexable for a system page. * * @param string $page_type The type of system page. * @param Indexable|bool $indexable Optional. An existing indexable to overwrite. * * @return Indexable The search result indexable. */ public function build_for_system_page( $page_type, $indexable = false ) { $defaults = [ 'object_type' => 'system-page', 'object_sub_type' => $page_type, ]; return $this->build( $indexable, $defaults ); } /** * Ensures we have a valid indexable. Creates one if false is passed. * * @param Indexable|false $indexable The indexable. * @param array $defaults The initial properties of the Indexable. * * @return Indexable The indexable. */ protected function ensure_indexable( $indexable, $defaults = [] ) { if ( ! $indexable ) { return $this->indexable_repository->query()->create( $defaults ); } return $indexable; } /** * Build and author indexable from an author id if it does not exist yet, or if the author indexable needs to be upgraded. * * @param int $author_id The author id. * * @return Indexable|false The author indexable if it has been built, `false` if it could not be built. */ protected function maybe_build_author_indexable( $author_id ) { $author_indexable = $this->indexable_repository->find_by_id_and_type( $author_id, 'user', false ); if ( ! $author_indexable || $this->version_manager->indexable_needs_upgrade( $author_indexable ) ) { // Try to build the author. $author_defaults = [ 'object_type' => 'user', 'object_id' => $author_id, ]; $author_indexable = $this->build( $author_indexable, $author_defaults ); } return $author_indexable; } /** * Checks if the indexable type is one that is not supposed to have object ID for. * * @param string $type The type of the indexable. * * @return bool Whether the indexable type is one that is not supposed to have object ID for. */ protected function is_type_with_no_id( $type ) { return \in_array( $type, [ 'home-page', 'date-archive', 'post-type-archive', 'system-page' ], true ); } // phpcs:disable Squiz.Commenting.FunctionCommentThrowTag.Missing -- Exceptions are handled by the catch statement in the method. /** * Rebuilds an Indexable from scratch. * * @param Indexable $indexable The Indexable to (re)build. * @param array|null $defaults The object type of the Indexable. * * @return Indexable|false The resulting Indexable. */ public function build( $indexable, $defaults = null ) { // Backup the previous Indexable, if there was one. $indexable_before = ( $indexable ) ? $this->deep_copy_indexable( $indexable ) : null; // Make sure we have an Indexable to work with. $indexable = $this->ensure_indexable( $indexable, $defaults ); try { if ( $indexable->object_id === 0 ) { throw Not_Built_Exception::invalid_object_id( $indexable->object_id ); } switch ( $indexable->object_type ) { case 'post': $indexable = $this->post_builder->build( $indexable->object_id, $indexable ); // Save indexable, to make sure it can be queried when building related objects like the author indexable and hierarchy. $indexable = $this->indexable_helper->save_indexable( $indexable, $indexable_before ); // For attachments, we have to make sure to patch any potentially previously cleaned up SEO links. if ( \is_a( $indexable, Indexable::class ) && $indexable->object_sub_type === 'attachment' ) { $this->link_builder->patch_seo_links( $indexable ); } // Always rebuild the primary term. $this->primary_term_builder->build( $indexable->object_id ); // Always rebuild the hierarchy; this needs the primary term to run correctly. $this->hierarchy_builder->build( $indexable ); $this->maybe_build_author_indexable( $indexable->author_id ); // The indexable is already saved, so return early. return $indexable; case 'user': $indexable = $this->author_builder->build( $indexable->object_id, $indexable ); break; case 'term': $indexable = $this->term_builder->build( $indexable->object_id, $indexable ); // Save indexable, to make sure it can be queried when building hierarchy. $indexable = $this->indexable_helper->save_indexable( $indexable, $indexable_before ); $this->hierarchy_builder->build( $indexable ); // The indexable is already saved, so return early. return $indexable; case 'home-page': $indexable = $this->home_page_builder->build( $indexable ); break; case 'date-archive': $indexable = $this->date_archive_builder->build( $indexable ); break; case 'post-type-archive': $indexable = $this->post_type_archive_builder->build( $indexable->object_sub_type, $indexable ); break; case 'system-page': $indexable = $this->system_page_builder->build( $indexable->object_sub_type, $indexable ); break; } return $this->indexable_helper->save_indexable( $indexable, $indexable_before ); } catch ( Source_Exception $exception ) { if ( ! $this->is_type_with_no_id( $indexable->object_type ) && ! isset( $indexable->object_id ) ) { return false; } /** * The current indexable could not be indexed. Create a placeholder indexable, so we can * skip this indexable in future indexing runs. * * @var Indexable $indexable */ $indexable = $this->ensure_indexable( $indexable, [ 'object_id' => $indexable->object_id, 'object_type' => $indexable->object_type, 'post_status' => 'unindexed', 'version' => 0, ] ); // If we already had an existing indexable, mark it as unindexed. We cannot rely on its validity anymore. $indexable->post_status = 'unindexed'; // Make sure that the indexing process doesn't get stuck in a loop on this broken indexable. $indexable = $this->version_manager->set_latest( $indexable ); return $this->indexable_helper->save_indexable( $indexable, $indexable_before ); } catch ( Not_Built_Exception $exception ) { return false; } } // phpcs:enable } s->post_builder = $post_builder; $this->term_builder = $term_builder; $this->home_page_builder = $home_page_builder; $this->post_type_archive_builder = $post_type_archive_builder; $this->date_archive_builder = $date_archive_builder; $this->system_page_builder = $system_page_builder; $this->hierarchy_builder = $hierarchy_builder; $this->primary_term_builder = $primary_term_builder; $this->indexable_helper = $indexable_helper; $this->version_manager = $version_manager; $this->link_builder = $link_builder; } /** * Sets the indexable repository. Done to avoid circular dependencies. * * @required * * @param Indexable_Repository $indexable_repository The indexable repository. * * @return void */ public function set_indexable_repository( Indexable_Repository $indexable_repository ) { $this->indexable_repository = $indexable_repository; } /** * Creates a clean copy of an Indexable to allow for later database operations. * * @param Indexable $indexable The Indexable to copy. * * @return bool|Indexable */ protected function deep_copy_indexable( $indexable ) { return $this->indexable_repository ->query() ->create( $indexable->as_array() ); } /** * Creates an indexable by its ID and type. * * @param int $object_id The indexable object ID. * @param string $object_type The indexable object type. * @param Indexable|bool $indexable Optional. An existing indexable to overwrite. * * @return bool|Indexable Instance of indexable. False when unable to build. */ public function build_for_id_and_type( $object_id, $object_type, $indexable = false ) { $defaults = [ 'object_type' => $object_type, 'object_id' => $object_id, ]; $indexable = $this->build( $indexable, $defaults ); return $indexable; } /** * Creates an indexable for the homepage. * * @param Indexable|bool $indexable Optional. An existing indexable to overwrite. * * @return Indexable The home page indexable. */ public function build_for_home_page( $indexable = false ) { return $this->build( $indexable, [ 'object_type' => 'home-page' ] ); } /** * Creates an indexable for the date archive. * * @param Indexable|bool $indexable Optional. An existing indexable to overwrite. * * @return Indexable The date archive indexable. */ public function build_for_date_archive( $indexable = false ) { return $this->build( $indexable, [ 'object_type' => 'date-archive' ] ); } /** * Creates an indexable for a post type archive. * * @param string $post_type The post type. * @param Indexable|bool $indexable Optional. An existing indexable to overwrite. * * @return Indexable The post type archive indexable. */ public function build_for_post_type_archive( $post_type, $indexable = false ) { $defaults = [ 'object_type' => 'post-type-archive', 'object_sub_type' => $post_type, ]; return $this->build( $indexable, $defaults ); } /** * Creates an indexable for a system page. * * @param string $page_type The type of system page. * @param Indexable|bool $indexable Optional. An existing indexable to overwrite. * * @return Indexable The search result indexable. */ public function build_for_system_page( $page_type, $indexable = false ) { $defaults = [ 'object_type' => 'system-page', 'object_sub_type' => $page_type, ]; return $this->build( $indexable, $defaults ); } /** * Ensures we have a valid indexable. Creates one if false is passed. * * @param Indexable|false $indexable The indexable. * @param array $defaults The initial properties of the Indexable. * * @return Indexable The indexable. */ protected function ensure_indexable( $indexable, $defaults = [] ) { if ( ! $indexable ) { return $this->indexable_repository->query()->create( $defaults ); } return $indexable; } /** * Build and author indexable from an author id if it does not exist yet, or if the author indexable needs to be upgraded. * * @param int $author_id The author id. * * @return Indexable|false The author indexable if it has been built, `false` if it could not be built. */ protected function maybe_build_author_indexable( $author_id ) { $author_indexable = $this->indexable_repository->find_by_id_and_type( $author_id, 'user', false ); if ( ! $author_indexable || $this->version_manager->indexable_needs_upgrade( $author_indexable ) ) { // Try to build the author. $author_defaults = [ 'object_type' => 'user', 'object_id' => $author_id, ]; $author_indexable = $this->build( $author_indexable, $author_defaults ); } return $author_indexable; } /** * Checks if the indexable type is one that is not supposed to have object ID for. * * @param string $type The type of the indexable. * * @return bool Whether the indexable type is one that is not supposed to have object ID for. */ protected function is_type_with_no_id( $type ) { return \in_array( $type, [ 'home-page', 'date-archive', 'post-type-archive', 'system-page' ], true ); } // phpcs:disable Squiz.Commenting.FunctionCommentThrowTag.Missing -- Exceptions are handled by the catch statement in the method. /** * Rebuilds an Indexable from scratch. * * @param Indexable $indexable The Indexable to (re)build. * @param array|null $defaults The object type of the Indexable. * * @return Indexable|false The resulting Indexable. */ public function build( $indexable, $defaults = null ) { // Backup the previous Indexable, if there was one. $indexable_before = ( $indexable ) ? $this->deep_copy_indexable( $indexable ) : null; // Make sure we have an Indexable to work with. $indexable = $this->ensure_indexable( $indexable, $defaults ); try { if ( $indexable->object_id === 0 ) { throw Not_Built_Exception::invalid_object_id( $indexable->object_id ); } switch ( $indexable->object_type ) { case 'post': $indexable = $this->post_builder->build( $indexable->object_id, $indexable ); // Save indexable, to make sure it can be queried when building related objects like the author indexable and hierarchy. $indexable = $this->indexable_helper->save_indexable( $indexable, $indexable_before ); // For attachments, we have to make sure to patch any potentially previously cleaned up SEO links. if ( \is_a( $indexable, Indexable::class ) && $indexable->object_sub_type === 'attachment' ) { $this->link_builder->patch_seo_links( $indexable ); } // Always rebuild the primary term. $this->primary_term_builder->build( $indexable->object_id ); // Always rebuild the hierarchy; this needs the primary term to run correctly. $this->hierarchy_builder->build( $indexable ); $this->maybe_build_author_indexable( $indexable->author_id ); // The indexable is already saved, so return early. return $indexable; case 'user': $indexable = $this->author_builder->build( $indexable->object_id, $indexable ); break; case 'term': $indexable = $this->term_builder->build( $indexable->object_id, $indexable ); // Save indexable, to make sure it can be queried when building hierarchy. $indexable = $this->indexable_helper->save_indexable( $indexable, $indexable_before ); $this->hierarchy_builder->build( $indexable ); // The indexable is already saved, so return early. return $indexable; case 'home-page': $indexable = $this->home_page_builder->build( $indexable ); break; case 'date-archive': $indexable = $this->date_archive_builder->build( $indexable ); break; case 'post-type-archive': $indexable = $this->post_type_archive_builder->build( $indexable->object_sub_type, $indexable ); break; case 'system-page': $indexable = $this->system_page_builder->build( $indexable->object_sub_type, $indexable ); break; } return $this->indexable_helper->save_indexable( $indexable, $indexable_before ); } catch ( Source_Exception $exception ) { if ( ! $this->is_type_with_no_id( $indexable->object_type ) && ! isset( $indexable->object_id ) ) { return false; } /** * The current indexable could not be indexed. Create a placeholder indexable, so we can * skip this indexable in future indexing runs. * * @var Indexable $indexable */ $indexable = $this->ensure_indexable( $indexable, [ 'object_id' => $indexable->object_id, 'object_type' => $indexable->object_type, 'post_status' => 'unindexed', 'version' => 0, ] ); // If we already had an existing indexable, mark it as unindexed. We cannot rely on its validity anymore. $indexable->post_status = 'unindexed'; // Make sure that the indexing process doesn't get stuck in a loop on this broken indexable. $indexable = $this->version_manager->set_latest( $indexable ); return $this->indexable_helper->save_indexable( $indexable, $indexable_before ); } catch ( Not_Built_Exception $exception ) { return false; } } // phpcs:enable }
Fatal error: Uncaught Error: Class "Yoast\WP\SEO\Builders\Indexable_Builder" not found in /htdocs/wp-content/plugins/wordpress-seo/src/generated/container.php:2095 Stack trace: #0 /htdocs/wp-content/plugins/wordpress-seo/src/generated/container.php(5963): Yoast\WP\SEO\Generated\Cached_Container->getIndexableBuilderService() #1 /htdocs/wp-content/plugins/wordpress-seo/vendor_prefixed/symfony/dependency-injection/Container.php(271): Yoast\WP\SEO\Generated\Cached_Container->getIndexableRepositoryService() #2 /htdocs/wp-content/plugins/wordpress-seo/src/surfaces/classes-surface.php(38): YoastSEO_Vendor\Symfony\Component\DependencyInjection\Container->get('Yoast\\WP\\SEO\\Re...') #3 /htdocs/wp-content/plugins/wordpress-seo/inc/class-wpseo-admin-bar-menu.php(133): Yoast\WP\SEO\Surfaces\Classes_Surface->get('Yoast\\WP\\SEO\\Re...') #4 /htdocs/wp-content/plugins/wordpress-seo/inc/wpseo-non-ajax-functions.php(20): WPSEO_Admin_Bar_Menu->__construct() #5 /htdocs/wp-includes/class-wp-hook.php(324): wpseo_initialize_admin_bar('') #6 /htdocs/wp-includes/class-wp-hook.php(348): WP_Hook->apply_filters(NULL, Array) #7 /htdocs/wp-includes/plugin.php(517): WP_Hook->do_action(Array) #8 /htdocs/wp-settings.php(749): do_action('wp_loaded') #9 /htdocs/wp-config.php(101): require_once('/htdocs/wp-sett...') #10 /htdocs/wp-load.php(50): require_once('/htdocs/wp-conf...') #11 /htdocs/wp-blog-header.php(13): require_once('/htdocs/wp-load...') #12 /htdocs/index.php(17): require('/htdocs/wp-blog...') #13 {main} thrown in /htdocs/wp-content/plugins/wordpress-seo/src/generated/container.php on line 2095