diff --git a/src/indexables/application/actions/verify-indexable-action-factory.php b/src/indexables/application/actions/verify-indexable-action-factory.php index fb87d74dff3..0bb86deebed 100644 --- a/src/indexables/application/actions/verify-indexable-action-factory.php +++ b/src/indexables/application/actions/verify-indexable-action-factory.php @@ -8,8 +8,10 @@ use Yoast\WP\SEO\Indexables\Domain\Exceptions\No_Verification_Action_Left_Exception; use Yoast\WP\SEO\Indexables\Domain\Exceptions\Verify_Action_Not_Found_Exception; use Yoast\WP\SEO\Indexables\Infrastructure\Actions\Verify_General_Indexables_Action; +use Yoast\WP\SEO\Indexables\Infrastructure\Actions\Verify_Post_Links_Indexables_Action; use Yoast\WP\SEO\Indexables\Infrastructure\Actions\Verify_Post_Type_Archives_Indexables_Action; use Yoast\WP\SEO\Indexables\Infrastructure\Actions\Verify_Term_Indexables_Action; +use Yoast\WP\SEO\Indexables\Infrastructure\Actions\Verify_Term_Links_Indexables_Action; class Verify_Indexable_Action_Factory implements Verify_Indexable_Action_Factory_Interface { @@ -27,22 +29,28 @@ class Verify_Indexable_Action_Factory implements Verify_Indexable_Action_Factory ]; /** - * @var \Yoast\WP\SEO\Indexables\Infrastructure\Actions\Verify_General_Indexables_Action + * @var Verify_General_Indexables_Action */ protected $verify_general_indexables_action; /** - * @var \Yoast\WP\SEO\Indexables\Infrastructure\Actions\Verify_Post_Type_Archives_Indexables_Action + * @var Verify_Post_Type_Archives_Indexables_Action */ protected $verify_post_type_archives_indexables_action; + /** + * @var Verify_Term_Links_Indexables_Action + */ + protected $verify_term_links_indexables_action; public function __construct( Verify_Term_Indexables_Action $verify_term_indexables_action, Verify_General_Indexables_Action $verify_general_indexables_action, - Verify_Post_Type_Archives_Indexables_Action $verify_post_type_archives_indexables_action + Verify_Post_Type_Archives_Indexables_Action $verify_post_type_archives_indexables_action, + Verify_Term_Links_Indexables_Action $verify_term_links_indexables_action ) { $this->verify_term_indexables_action = $verify_term_indexables_action; $this->verify_general_indexables_action = $verify_general_indexables_action; $this->verify_post_type_archives_indexables_action = $verify_post_type_archives_indexables_action; + $this->verify_term_links_indexables_action = $verify_term_links_indexables_action; } /** @@ -56,6 +64,8 @@ public function get( Current_Verification_Action $verification_action ): Verify_ return $this->verify_general_indexables_action; case'post-type-archives': return $this->verify_post_type_archives_indexables_action; + case 'term-links': + return $this->verify_term_links_indexables_action; default: throw new Verify_Action_Not_Found_Exception(); } diff --git a/src/indexables/infrastructure/actions/verify-term-links-indexables-action.php b/src/indexables/infrastructure/actions/verify-term-links-indexables-action.php new file mode 100644 index 00000000000..192f92e7d1b --- /dev/null +++ b/src/indexables/infrastructure/actions/verify-term-links-indexables-action.php @@ -0,0 +1,109 @@ +taxonomy = $taxonomy; + $this->repository = $repository; + $this->link_builder = $link_builder; + } + + /** + * @var \wpdb $wpdb The wp query. + */ + private $wpdb; + + public function re_build_indexables( Last_Batch_Count $last_batch_count, Batch_Size $batch_size ): bool { + $query = $this->get_query( $last_batch_count->get_last_batch(), $batch_size->get_batch_size() ); + + // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared -- Function get_select_query returns a prepared query. + $terms = $this->wpdb->get_results( $query ); + + $term_list = \array_map( + static function ( $term ) { + return (object) [ + 'id' => (int) $term->term_id, + 'type' => 'term', + 'content' => $term->description, + ]; + }, + $terms + ); + + $indexables = []; + foreach ( $term_list as $term ) { + $indexable = $this->repository->find_by_id_and_type( $term->id, $term->type ); + if ( $indexable ) { + $this->link_builder->build( $indexable, $term->content ); + + $indexables[] = $indexable; + } + } + + return $batch_size->should_keep_going( \count( $indexables ) ); + } + + /** + * @param \wpdb $wpdb + * + * @return void + * @required + */ + public function set_wpdb( \wpdb $wpdb ) { + $this->wpdb = $wpdb; + } + + private function get_query( $limit, $batch_size ) { + $taxonomy_table = $this->wpdb->term_taxonomy; + $public_taxonomies = $this->taxonomy->get_indexable_taxonomies(); + $placeholders = \implode( ', ', \array_fill( 0, \count( $public_taxonomies ), '%s' ) ); + + $replacements = []; + \array_push( $replacements, ...$public_taxonomies ); + + + $limit_query = 'LIMIT %d'; + $replacements[] = $batch_size; + $offset_query = ''; + if ( $limit !== 0 ) { + $offset_query = 'OFFSET %d'; + $replacements[] = ( $limit + $batch_size ); + } + + return $this->wpdb->prepare( + " + SELECT term_id, description + FROM {$taxonomy_table} AS T + WHERE taxonomy IN ($placeholders) + $limit_query $offset_query", + $replacements + ); + } +}