| 1 |
<?php |
| 2 |
|
| 3 |
namespace Yoast\WP\SEO\Actions\Indexing; |
| 4 |
|
| 5 |
use wpdb; |
| 6 |
use Yoast\WP\SEO\Builders\Indexable_Link_Builder; |
| 7 |
use Yoast\WP\SEO\Helpers\Indexable_Helper; |
| 8 |
use Yoast\WP\SEO\Models\SEO_Links; |
| 9 |
use Yoast\WP\SEO\Repositories\Indexable_Repository; |
| 10 |
|
| 11 |
/** |
| 12 |
* Reindexing action for link indexables. |
| 13 |
*/ |
| 14 |
abstract class Abstract_Link_Indexing_Action extends Abstract_Indexing_Action { |
| 15 |
|
| 16 |
/** |
| 17 |
* The link builder. |
| 18 |
* |
| 19 |
* @var Indexable_Link_Builder |
| 20 |
*/ |
| 21 |
protected $link_builder; |
| 22 |
|
| 23 |
/** |
| 24 |
* The indexable helper. |
| 25 |
* |
| 26 |
* @var Indexable_Helper |
| 27 |
*/ |
| 28 |
protected $indexable_helper; |
| 29 |
|
| 30 |
/** |
| 31 |
* The indexable repository. |
| 32 |
* |
| 33 |
* @var Indexable_Repository |
| 34 |
*/ |
| 35 |
protected $repository; |
| 36 |
|
| 37 |
/** |
| 38 |
* The WordPress database instance. |
| 39 |
* |
| 40 |
* @var wpdb |
| 41 |
*/ |
| 42 |
protected $wpdb; |
| 43 |
|
| 44 |
/** |
| 45 |
* Indexable_Post_Indexing_Action constructor |
| 46 |
* |
| 47 |
* @param Indexable_Link_Builder $link_builder The indexable link builder. |
| 48 |
* @param Indexable_Helper $indexable_helper The indexable repository. |
| 49 |
* @param Indexable_Repository $repository The indexable repository. |
| 50 |
* @param wpdb $wpdb The WordPress database instance. |
| 51 |
*/ |
| 52 |
public function __construct( |
| 53 |
Indexable_Link_Builder $link_builder, |
| 54 |
Indexable_Helper $indexable_helper, |
| 55 |
Indexable_Repository $repository, |
| 56 |
wpdb $wpdb |
| 57 |
) { |
| 58 |
$this->link_builder = $link_builder; |
| 59 |
$this->indexable_helper = $indexable_helper; |
| 60 |
$this->repository = $repository; |
| 61 |
$this->wpdb = $wpdb; |
| 62 |
} |
| 63 |
|
| 64 |
/** |
| 65 |
* Builds links for indexables which haven't had their links indexed yet. |
| 66 |
* |
| 67 |
* @return SEO_Links[] The created SEO links. |
| 68 |
*/ |
| 69 |
public function index() { |
| 70 |
$objects = $this->get_objects(); |
| 71 |
|
| 72 |
$objects_by_type = []; |
| 73 |
foreach ( $objects as $object ) { |
| 74 |
$objects_by_type[ $object->type ][] = (int) $object->id; |
| 75 |
} |
| 76 |
|
| 77 |
$indexable_map = []; |
| 78 |
foreach ( $objects_by_type as $type => $ids ) { |
| 79 |
foreach ( $this->repository->find_by_multiple_ids_and_type( $ids, $type ) as $indexable ) { |
| 80 |
if ( $indexable ) { |
| 81 |
$indexable_map[ $type ][ (int) $indexable->object_id ] = $indexable; |
| 82 |
} |
| 83 |
} |
| 84 |
} |
| 85 |
|
| 86 |
$indexables = []; |
| 87 |
foreach ( $objects as $object ) { |
| 88 |
$indexable = ( $indexable_map[ $object->type ][ (int) $object->id ] ?? null ); |
| 89 |
if ( $indexable ) { |
| 90 |
$this->link_builder->build( $indexable, $object->content ); |
| 91 |
$this->indexable_helper->save_indexable( $indexable ); |
| 92 |
|
| 93 |
$indexables[] = $indexable; |
| 94 |
} |
| 95 |
} |
| 96 |
|
| 97 |
if ( \count( $indexables ) > 0 ) { |
| 98 |
\delete_transient( static::UNINDEXED_COUNT_TRANSIENT ); |
| 99 |
\delete_transient( static::UNINDEXED_LIMITED_COUNT_TRANSIENT ); |
| 100 |
} |
| 101 |
|
| 102 |
return $indexables; |
| 103 |
} |
| 104 |
|
| 105 |
/** |
| 106 |
* In the case of term-links and post-links we want to use the total unindexed count, because using |
| 107 |
* the limited unindexed count actually leads to worse performance. |
| 108 |
* |
| 109 |
* @param int|bool $limit Unused. |
| 110 |
* |
| 111 |
* @return int The total number of unindexed links. |
| 112 |
*/ |
| 113 |
public function get_limited_unindexed_count( $limit = false ) { |
| 114 |
return $this->get_total_unindexed(); |
| 115 |
} |
| 116 |
|
| 117 |
/** |
| 118 |
* Returns the number of texts that will be indexed in a single link indexing pass. |
| 119 |
* |
| 120 |
* @return int The limit. |
| 121 |
*/ |
| 122 |
public function get_limit() { |
| 123 |
/** |
| 124 |
* Filter 'wpseo_link_indexing_limit' - Allow filtering the number of texts indexed during each link indexing pass. |
| 125 |
* |
| 126 |
* @param int $limit The maximum number of texts indexed. |
| 127 |
*/ |
| 128 |
return \apply_filters( 'wpseo_link_indexing_limit', 5 ); |
| 129 |
} |
| 130 |
|
| 131 |
/** |
| 132 |
* Returns objects to be indexed. |
| 133 |
* |
| 134 |
* @return array Objects to be indexed, should be an array of objects with object_id, object_type and content. |
| 135 |
*/ |
| 136 |
abstract protected function get_objects(); |
| 137 |
} |
| 138 |
|