Skip to content

Commit

Permalink
Implement link builder.
Browse files Browse the repository at this point in the history
  • Loading branch information
thijsoo committed Jun 19, 2023
1 parent bca7ecd commit bbf4fcb
Show file tree
Hide file tree
Showing 2 changed files with 122 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand All @@ -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;
}

/**
Expand All @@ -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();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
<?php

namespace Yoast\WP\SEO\Indexables\Infrastructure\Actions;

use Yoast\WP\SEO\Builders\Indexable_Link_Builder;
use Yoast\WP\SEO\Helpers\Taxonomy_Helper;
use Yoast\WP\SEO\Indexables\Domain\Actions\Verify_Indexables_Action_Interface;
use Yoast\WP\SEO\Indexables\Domain\Batch_Size;
use Yoast\WP\SEO\Indexables\Domain\Last_Batch_Count;
use Yoast\WP\SEO\Repositories\Indexable_Repository;

class Verify_Term_Links_Indexables_Action implements Verify_Indexables_Action_Interface {

/**
* @var \Yoast\WP\SEO\Helpers\Taxonomy_Helper
*/
protected $taxonomy;

/**
* @var \Yoast\WP\SEO\Repositories\Indexable_Repository
*/
protected $repository;
/**
* @var \Yoast\WP\SEO\Builders\Indexable_Link_Builder
*/
protected $link_builder;

public function __construct(
Taxonomy_Helper $taxonomy,
Indexable_Repository $repository,
Indexable_Link_Builder $link_builder
) {
$this->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
);
}
}

0 comments on commit bbf4fcb

Please sign in to comment.