← All changes
|
src/actions/indexing/abstract-link-indexing-action.php
+38
-13
18.1
→
trunk
View file →
| @@ -3,15 +3,14 @@ | ||
| 3 | 3 | namespace Yoast\WP\SEO\Actions\Indexing; |
| 4 | 4 | |
| 5 | 5 | use wpdb; |
| 6 | 6 | use Yoast\WP\SEO\Builders\Indexable_Link_Builder; |
| 7 | +use Yoast\WP\SEO\Helpers\Indexable_Helper; | |
| 7 | 8 | use Yoast\WP\SEO\Models\SEO_Links; |
| 8 | 9 | use Yoast\WP\SEO\Repositories\Indexable_Repository; |
| 9 | 10 | |
| 10 | 11 | /** |
| 11 | 12 | * Reindexing action for link indexables. |
| 12 | - * | |
| 13 | - * @phpcs:disable Yoast.NamingConventions.ObjectNameDepth.MaxExceeded | |
| 14 | 13 | */ |
| 15 | 14 | abstract class Abstract_Link_Indexing_Action extends Abstract_Indexing_Action { |
| 16 | 15 | |
| 17 | 16 | /** |
| @@ -21,8 +20,15 @@ | ||
| 21 | 20 | */ |
| 22 | 21 | protected $link_builder; |
| 23 | 22 | |
| 24 | 23 | /** |
| 24 | + * The indexable helper. | |
| 25 | + * | |
| 26 | + * @var Indexable_Helper | |
| 27 | + */ | |
| 28 | + protected $indexable_helper; | |
| 29 | + | |
| 30 | + /** | |
| 25 | 31 | * The indexable repository. |
| 26 | 32 | * |
| 27 | 33 | * @var Indexable_Repository |
| 28 | 34 | */ |
| @@ -37,20 +43,23 @@ | ||
| 37 | 43 | |
| 38 | 44 | /** |
| 39 | 45 | * Indexable_Post_Indexing_Action constructor |
| 40 | 46 | * |
| 41 | - * @param Indexable_Link_Builder $link_builder The indexable link builder. | |
| 42 | - * @param Indexable_Repository $repository The indexable repository. | |
| 43 | - * @param wpdb $wpdb The WordPress database instance. | |
| 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. | |
| 44 | 51 | */ |
| 45 | 52 | public function __construct( |
| 46 | 53 | Indexable_Link_Builder $link_builder, |
| 54 | + Indexable_Helper $indexable_helper, | |
| 47 | 55 | Indexable_Repository $repository, |
| 48 | 56 | wpdb $wpdb |
| 49 | 57 | ) { |
| 50 | - $this->link_builder = $link_builder; | |
| 51 | - $this->repository = $repository; | |
| 52 | - $this->wpdb = $wpdb; | |
| 58 | + $this->link_builder = $link_builder; | |
| 59 | + $this->indexable_helper = $indexable_helper; | |
| 60 | + $this->repository = $repository; | |
| 61 | + $this->wpdb = $wpdb; | |
| 53 | 62 | } |
| 54 | 63 | |
| 55 | 64 | /** |
| 56 | 65 | * Builds links for indexables which haven't had their links indexed yet. |
| @@ -59,15 +68,31 @@ | ||
| 59 | 68 | */ |
| 60 | 69 | public function index() { |
| 61 | 70 | $objects = $this->get_objects(); |
| 62 | 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 | + | |
| 63 | 86 | $indexables = []; |
| 64 | 87 | foreach ( $objects as $object ) { |
| 65 | - $indexable = $this->repository->find_by_id_and_type( $object->id, $object->type ); | |
| 66 | - $this->link_builder->build( $indexable, $object->content ); | |
| 67 | - $indexable->save(); | |
| 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 ); | |
| 68 | 92 | |
| 69 | - $indexables[] = $indexable; | |
| 93 | + $indexables[] = $indexable; | |
| 94 | + } | |
| 70 | 95 | } |
| 71 | 96 | |
| 72 | 97 | if ( \count( $indexables ) > 0 ) { |
| 73 | 98 | \delete_transient( static::UNINDEXED_COUNT_TRANSIENT ); |
| @@ -97,9 +122,9 @@ | ||
| 97 | 122 | public function get_limit() { |
| 98 | 123 | /** |
| 99 | 124 | * Filter 'wpseo_link_indexing_limit' - Allow filtering the number of texts indexed during each link indexing pass. |
| 100 | 125 | * |
| 101 | - * @api int The maximum number of texts indexed. | |
| 126 | + * @param int $limit The maximum number of texts indexed. | |
| 102 | 127 | */ |
| 103 | 128 | return \apply_filters( 'wpseo_link_indexing_limit', 5 ); |
| 104 | 129 | } |
| 105 | 130 | |