PluginProbe
Yoast SEO – Advanced SEO with real-time guidance and built-in AI / trunk
Yoast SEO – Advanced SEO with real-time guidance and built-in AI vtrunk
28.5 28.4 28.3 28.2 28.1 28.0 27.9 27.8 27.7 27.6 27.5 trunk 18.0 18.1 18.2 18.3 18.4 18.4.1 18.5 18.5.1 18.6 18.7 18.8 18.9 19.0 All 129 releases
wordpress-seo / src / repositories / indexable-repository.php

indexable-repository.php in Yoast SEO – Advanced SEO with real-time guidance and built-in AI trunk, at src/repositories/indexable-repository.php

828 lines 25.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace Yoast\WP\SEO\Repositories;
4
5 use Psr\Log\LoggerInterface;
6 use wpdb;
7 use Yoast\WP\Lib\Model;
8 use Yoast\WP\Lib\ORM;
9 use Yoast\WP\SEO\Builders\Indexable_Builder;
10 use Yoast\WP\SEO\Helpers\Current_Page_Helper;
11 use Yoast\WP\SEO\Helpers\Indexable_Helper;
12 use Yoast\WP\SEO\Loggers\Logger;
13 use Yoast\WP\SEO\Models\Indexable;
14 use Yoast\WP\SEO\Services\Indexables\Indexable_Version_Manager;
15
16 /**
17 * Class Indexable_Repository.
18 */
19 class Indexable_Repository {
20
21 /**
22 * The maximum number of comma-separated phrases honoured by a title-keyword search.
23 * Any phrases beyond this are ignored so an oversized list cannot blow up the query.
24 *
25 * @var int
26 */
27 public const MAX_TITLE_KEYWORD_PHRASES = 10;
28
29 /**
30 * The maximum page size honoured by a title-keyword search.
31 *
32 * @var int
33 */
34 public const MAX_TITLE_KEYWORD_PAGE_SIZE = 100;
35
36 /**
37 * The indexable builder.
38 *
39 * @var Indexable_Builder
40 */
41 private $builder;
42
43 /**
44 * Represents the hierarchy repository.
45 *
46 * @var Indexable_Hierarchy_Repository
47 */
48 protected $hierarchy_repository;
49
50 /**
51 * The current page helper.
52 *
53 * @var Current_Page_Helper
54 */
55 protected $current_page;
56
57 /**
58 * The logger object.
59 *
60 * @var LoggerInterface
61 */
62 protected $logger;
63
64 /**
65 * The WordPress database.
66 *
67 * @var wpdb
68 */
69 protected $wpdb;
70
71 /**
72 * Represents the indexable helper.
73 *
74 * @var Indexable_Helper
75 */
76 protected $indexable_helper;
77
78 /**
79 * Checks if Indexables are up to date.
80 *
81 * @var Indexable_Version_Manager
82 */
83 protected $version_manager;
84
85 /**
86 * Returns the instance of this class constructed through the ORM Wrapper.
87 *
88 * @param Indexable_Builder $builder The indexable builder.
89 * @param Current_Page_Helper $current_page The current post helper.
90 * @param Logger $logger The logger.
91 * @param Indexable_Hierarchy_Repository $hierarchy_repository The hierarchy repository.
92 * @param wpdb $wpdb The WordPress database instance.
93 * @param Indexable_Version_Manager $version_manager The indexable version manager.
94 */
95 public function __construct(
96 Indexable_Builder $builder,
97 Current_Page_Helper $current_page,
98 Logger $logger,
99 Indexable_Hierarchy_Repository $hierarchy_repository,
100 wpdb $wpdb,
101 Indexable_Version_Manager $version_manager
102 ) {
103 $this->builder = $builder;
104 $this->current_page = $current_page;
105 $this->logger = $logger;
106 $this->hierarchy_repository = $hierarchy_repository;
107 $this->wpdb = $wpdb;
108 $this->version_manager = $version_manager;
109 }
110
111 /**
112 * Starts a query for this repository.
113 *
114 * @return ORM
115 */
116 public function query() {
117 return Model::of_type( 'Indexable' );
118 }
119
120 /**
121 * Attempts to find the indexable for the current WordPress page. Returns false if no indexable could be found.
122 * This may be the result of the indexable not existing or of being unable to determine what type of page the
123 * current page is.
124 *
125 * @return bool|Indexable The indexable. If no indexable is found returns an empty indexable. Returns false if
126 * there is a database error.
127 */
128 public function for_current_page() {
129 $indexable = false;
130
131 switch ( true ) {
132 case $this->current_page->is_simple_page():
133 $indexable = $this->find_by_id_and_type( $this->current_page->get_simple_page_id(), 'post' );
134 break;
135 case $this->current_page->is_home_static_page():
136 $indexable = $this->find_by_id_and_type( $this->current_page->get_front_page_id(), 'post' );
137 break;
138 case $this->current_page->is_home_posts_page():
139 $indexable = $this->find_for_home_page();
140 break;
141 case $this->current_page->is_term_archive():
142 $indexable = $this->find_by_id_and_type( $this->current_page->get_term_id(), 'term' );
143 break;
144 case $this->current_page->is_date_archive():
145 $indexable = $this->find_for_date_archive();
146 break;
147 case $this->current_page->is_search_result():
148 $indexable = $this->find_for_system_page( 'search-result' );
149 break;
150 case $this->current_page->is_post_type_archive():
151 $indexable = $this->find_for_post_type_archive( $this->current_page->get_queried_post_type() );
152 break;
153 case $this->current_page->is_author_archive():
154 $indexable = $this->find_by_id_and_type( $this->current_page->get_author_id(), 'user' );
155 break;
156 case $this->current_page->is_404():
157 $indexable = $this->find_for_system_page( '404' );
158 break;
159 }
160
161 if ( $indexable === false ) {
162 return $this->query()->create(
163 [
164 'object_type' => 'unknown',
165 'post_status' => 'unindexed',
166 'version' => 1,
167 ],
168 );
169 }
170
171 return $indexable;
172 }
173
174 /**
175 * Retrieves an indexable by its permalink.
176 *
177 * @param string $permalink The indexable permalink.
178 *
179 * @return bool|Indexable The indexable, false if none could be found.
180 */
181 public function find_by_permalink( $permalink ) {
182 $permalink_hash = \strlen( $permalink ) . ':' . \md5( $permalink );
183
184 // Find by both permalink_hash and permalink, permalink_hash is indexed so will be used first by the DB to optimize the query.
185 return $this->query()
186 ->where( 'permalink_hash', $permalink_hash )
187 ->where( 'permalink', $permalink )
188 ->find_one();
189 }
190
191 /**
192 * Retrieves all the indexable instances of a certain object type.
193 *
194 * @param string $object_type The object type.
195 *
196 * @return Indexable[] The array with all the indexable instances of a certain object type.
197 */
198 public function find_all_with_type( $object_type ) {
199 /**
200 * The array with all the indexable instances of a certain object type.
201 *
202 * @var Indexable[] $indexables
203 */
204 $indexables = $this
205 ->query()
206 ->where( 'object_type', $object_type )
207 ->find_many();
208
209 return \array_map( [ $this, 'upgrade_indexable' ], $indexables );
210 }
211
212 /**
213 * Retrieves all the indexable instances of a certain object subtype.
214 *
215 * @param string $object_type The object type.
216 * @param string $object_sub_type The object subtype.
217 *
218 * @return Indexable[] The array with all the indexable instances of a certain object subtype.
219 */
220 public function find_all_with_type_and_sub_type( $object_type, $object_sub_type ) {
221 /**
222 * The array with all the indexable instances of a certain object type and subtype.
223 *
224 * @var Indexable[] $indexables
225 */
226 $indexables = $this
227 ->query()
228 ->where( 'object_type', $object_type )
229 ->where( 'object_sub_type', $object_sub_type )
230 ->find_many();
231
232 return \array_map( [ $this, 'upgrade_indexable' ], $indexables );
233 }
234
235 /**
236 * Retrieves a paginated set of indexable instances of public indexables.
237 *
238 * @param int $page The page number (1-based).
239 * @param int $page_size The number of items per page.
240 * @param string $post_type The post type indexables to find.
241 *
242 * @return Indexable[] The array with the paginated indexable instances which are public.
243 */
244 public function find_all_public_paginated( int $page, int $page_size, string $post_type ): array {
245 $offset = ( ( $page - 1 ) * $page_size );
246
247 $query = $this->query()->where_raw( '( is_public IS NULL OR is_public = 1 ) AND ( is_robots_noindex IS NULL OR is_robots_noindex = 0 )' );
248 $query->where( 'object_sub_type', $post_type );
249 $query->where( 'post_status', 'publish' );
250
251 $indexables = $query->order_by_asc( 'id' )->limit( $page_size )->offset( $offset )->find_many();
252
253 return \array_map( [ $this, 'upgrade_indexable' ], $indexables );
254 }
255
256 /**
257 * Retrieves the homepage indexable.
258 *
259 * @param bool $auto_create Optional. Create the indexable if it does not exist.
260 *
261 * @return bool|Indexable Instance of indexable.
262 */
263 public function find_for_home_page( $auto_create = true ) {
264 $indexable = \wp_cache_get( 'home-page', 'yoast-seo-indexables' );
265 if ( ! $indexable ) {
266 /**
267 * Indexable instance.
268 *
269 * @var Indexable $indexable
270 */
271 $indexable = $this->query()->where( 'object_type', 'home-page' )->find_one();
272
273 if ( $auto_create && ! $indexable ) {
274 $indexable = $this->builder->build_for_home_page();
275 }
276
277 $indexable = $this->upgrade_indexable( $indexable );
278
279 \wp_cache_set( 'home-page', $indexable, 'yoast-seo-indexables', ( 5 * \MINUTE_IN_SECONDS ) );
280 }
281
282 return $indexable;
283 }
284
285 /**
286 * Retrieves the date archive indexable.
287 *
288 * @param bool $auto_create Optional. Create the indexable if it does not exist.
289 *
290 * @return bool|Indexable Instance of indexable.
291 */
292 public function find_for_date_archive( $auto_create = true ) {
293 /**
294 * Indexable instance.
295 *
296 * @var Indexable $indexable
297 */
298 $indexable = $this->query()->where( 'object_type', 'date-archive' )->find_one();
299
300 if ( $auto_create && ! $indexable ) {
301 $indexable = $this->builder->build_for_date_archive();
302 }
303
304 return $this->upgrade_indexable( $indexable );
305 }
306
307 /**
308 * Retrieves an indexable for a post type archive.
309 *
310 * @param string $post_type The post type.
311 * @param bool $auto_create Optional. Create the indexable if it does not exist.
312 *
313 * @return bool|Indexable The indexable, false if none could be found.
314 */
315 public function find_for_post_type_archive( $post_type, $auto_create = true ) {
316 /**
317 * Indexable instance.
318 *
319 * @var Indexable $indexable
320 */
321 $indexable = $this->query()
322 ->where( 'object_type', 'post-type-archive' )
323 ->where( 'object_sub_type', $post_type )
324 ->find_one();
325
326 if ( $auto_create && ! $indexable ) {
327 $indexable = $this->builder->build_for_post_type_archive( $post_type );
328 }
329
330 return $this->upgrade_indexable( $indexable );
331 }
332
333 /**
334 * Retrieves the indexable for a system page.
335 *
336 * @param string $object_sub_type The type of system page.
337 * @param bool $auto_create Optional. Create the indexable if it does not exist.
338 *
339 * @return bool|Indexable Instance of indexable.
340 */
341 public function find_for_system_page( $object_sub_type, $auto_create = true ) {
342 /**
343 * Indexable instance.
344 *
345 * @var Indexable $indexable
346 */
347 $indexable = $this->query()
348 ->where( 'object_type', 'system-page' )
349 ->where( 'object_sub_type', $object_sub_type )
350 ->find_one();
351
352 if ( $auto_create && ! $indexable ) {
353 $indexable = $this->builder->build_for_system_page( $object_sub_type );
354 }
355
356 return $this->upgrade_indexable( $indexable );
357 }
358
359 /**
360 * Retrieves an indexable by its ID and type.
361 *
362 * @param int $object_id The indexable object ID.
363 * @param string $object_type The indexable object type.
364 * @param bool $auto_create Optional. Create the indexable if it does not exist.
365 *
366 * @return bool|Indexable Instance of indexable.
367 */
368 public function find_by_id_and_type( $object_id, $object_type, $auto_create = true ) {
369 $indexable = $this->query()
370 ->where( 'object_id', $object_id )
371 ->where( 'object_type', $object_type )
372 ->find_one();
373
374 if ( $auto_create && ! $indexable ) {
375 $indexable = $this->builder->build_for_id_and_type( $object_id, $object_type );
376 }
377 else {
378 $indexable = $this->upgrade_indexable( $indexable );
379 }
380
381 return $indexable;
382 }
383
384 /**
385 * Retrieves multiple indexables at once by their id's and type.
386 *
387 * @param int[] $object_ids The array of indexable object id's.
388 * @param string $object_type The indexable object type.
389 * @param bool $auto_create Optional. Create the indexable if it does not exist.
390 *
391 * @return Indexable[] An array of indexables.
392 */
393 public function find_by_multiple_ids_and_type( $object_ids, $object_type, $auto_create = true ) {
394 if ( empty( $object_ids ) ) {
395 return [];
396 }
397
398 /**
399 * Represents an array of indexable objects.
400 *
401 * @var Indexable[] $indexables
402 */
403 $indexables = $this->query()
404 ->where_in( 'object_id', $object_ids )
405 ->where( 'object_type', $object_type )
406 ->find_many();
407
408 if ( $auto_create ) {
409 $indexables_available = [];
410 foreach ( $indexables as $indexable ) {
411 $indexables_available[] = $indexable->object_id;
412 }
413
414 $indexables_to_create = \array_diff( $object_ids, $indexables_available );
415
416 if ( ! empty( $indexables_to_create ) ) {
417 // Warm the object caches for the whole batch, so each build below does not trigger its own uncached queries.
418 if ( $object_type === 'post' ) {
419 \_prime_post_caches( $indexables_to_create );
420 }
421 elseif ( $object_type === 'term' ) {
422 \_prime_term_caches( $indexables_to_create );
423 }
424 }
425
426 foreach ( $indexables_to_create as $indexable_to_create ) {
427 $indexables[] = $this->builder->build_for_id_and_type( $indexable_to_create, $object_type );
428 }
429 }
430
431 return \array_map( [ $this, 'upgrade_indexable' ], $indexables );
432 }
433
434 /**
435 * Finds the indexables by id's.
436 *
437 * @param int[] $indexable_ids The indexable id's.
438 *
439 * @return Indexable[] The found indexables.
440 */
441 public function find_by_ids( array $indexable_ids ) {
442 if ( empty( $indexable_ids ) ) {
443 return [];
444 }
445
446 $indexables = $this
447 ->query()
448 ->where_in( 'id', $indexable_ids )
449 ->find_many();
450
451 return \array_map( [ $this, 'upgrade_indexable' ], $indexables );
452 }
453
454 /**
455 * Returns all ancestors of a given indexable.
456 *
457 * @param Indexable $indexable The indexable to find the ancestors of.
458 *
459 * @return Indexable[] All ancestors of the given indexable.
460 */
461 public function get_ancestors( Indexable $indexable ) {
462 // If we've already set ancestors on the indexable no need to get them again.
463 if ( \is_array( $indexable->ancestors ) && ! empty( $indexable->ancestors ) ) {
464 return \array_map( [ $this, 'upgrade_indexable' ], $indexable->ancestors );
465 }
466
467 $indexable_ids = $this->hierarchy_repository->find_ancestors( $indexable );
468
469 // If we've set ancestors on the indexable because we had to build them to find them.
470 if ( \is_array( $indexable->ancestors ) && ! empty( $indexable->ancestors ) ) {
471 return \array_map( [ $this, 'upgrade_indexable' ], $indexable->ancestors );
472 }
473
474 if ( empty( $indexable_ids ) ) {
475 return [];
476 }
477
478 if ( $indexable_ids[0] === 0 && \count( $indexable_ids ) === 1 ) {
479 return [];
480 }
481
482 $indexables = $this->query()
483 ->where_in( 'id', $indexable_ids )
484 ->order_by_expr( 'FIELD(id,' . \implode( ',', $indexable_ids ) . ')' )
485 ->find_many();
486
487 return \array_map( [ $this, 'upgrade_indexable' ], $indexables );
488 }
489
490 /**
491 * Returns all subpages with a given post_parent.
492 *
493 * @param int $post_parent The post parent.
494 * @param int[] $exclude_ids The id's to exclude.
495 *
496 * @return Indexable[] array of indexables.
497 */
498 public function get_subpages_by_post_parent( $post_parent, $exclude_ids = [] ) {
499 $query = $this->query()
500 ->where( 'post_parent', $post_parent )
501 ->where( 'object_type', 'post' )
502 ->where( 'post_status', 'publish' );
503
504 if ( ! empty( $exclude_ids ) ) {
505 $query->where_not_in( 'object_id', $exclude_ids );
506 }
507 return $query->find_many();
508 }
509
510 /**
511 * Returns most recently modified posts of a post type.
512 *
513 * @param string $post_type The post type.
514 * @param int $limit The maximum number of posts to return.
515 * @param bool $exclude_older_than_one_year Whether to exclude posts older than one year.
516 * @param string $search_filter Optional. A search filter to apply to the breadcrumb title.
517 *
518 * @return Indexable[] array of indexables.
519 */
520 public function get_recently_modified_posts( string $post_type, int $limit, bool $exclude_older_than_one_year, string $search_filter = '' ) {
521 $query = $this->query()
522 ->where( 'object_type', 'post' )
523 ->where( 'object_sub_type', $post_type )
524 ->where_raw( '( is_public IS NULL OR is_public = 1 )' )
525 ->order_by_desc( 'object_last_modified' )
526 ->limit( $limit );
527
528 if ( $exclude_older_than_one_year === true ) {
529 $query->where_gte( 'object_published_at', \gmdate( 'Y-m-d H:i:s', \strtotime( '-1 year' ) ) );
530 }
531
532 if ( $search_filter !== '' ) {
533 $query->where_like( 'breadcrumb_title', '%' . $search_filter . '%' );
534 }
535
536 $query->order_by_desc( 'object_last_modified' )
537 ->limit( $limit );
538
539 return $query->find_many();
540 }
541
542 /**
543 * Finds posts whose breadcrumb title contains any of the given comma-separated phrases.
544 *
545 * The search string is a comma-separated list. Each value is matched as a whole
546 * contiguous substring of the breadcrumb title, and a post is returned when it
547 * contains any one of the values (a logical OR between values). For example,
548 * "hiking boots, trail" returns posts whose title contains "hiking boots" or "trail".
549 *
550 * Results are paginated and ordered most recently modified first (with the indexable
551 * id as a stable tiebreaker), so requesting a later page returns older matches.
552 *
553 * At most self::MAX_TITLE_KEYWORD_PHRASES phrases are honoured; any beyond that are ignored.
554 * The page size is clamped to the range 1..self::MAX_TITLE_KEYWORD_PAGE_SIZE.
555 *
556 * @param string $keywords The comma-separated phrases to match against the breadcrumb title.
557 * @param int $page The page of results to return, 1-based.
558 * @param int $page_size The number of posts per page.
559 * @param string $post_type The post type to restrict the search to.
560 *
561 * @return Indexable[] The matching indexables for the requested page, ordered by most recently modified.
562 */
563 public function find_posts_by_title_keywords( string $keywords, int $page = 1, int $page_size = 10, string $post_type = 'post' ) {
564 $phrases = \array_map( 'trim', \explode( ',', $keywords ) );
565 $phrases = \array_filter(
566 $phrases,
567 static function ( $phrase ) {
568 return $phrase !== '';
569 },
570 );
571 $phrases = \array_slice( $phrases, 0, self::MAX_TITLE_KEYWORD_PHRASES );
572
573 // An empty search must not degrade into matching every post.
574 if ( empty( $phrases ) ) {
575 return [];
576 }
577
578 $likes = \array_fill( 0, \count( $phrases ), 'breadcrumb_title LIKE %s' );
579 $params = \array_map(
580 function ( $phrase ) {
581 return '%' . $this->wpdb->esc_like( $phrase ) . '%';
582 },
583 $phrases,
584 );
585
586 $page_size = \min( \max( 1, $page_size ), self::MAX_TITLE_KEYWORD_PAGE_SIZE );
587 $offset = ( ( \max( 1, $page ) - 1 ) * $page_size );
588
589 $indexables = $this->query()
590 ->where( 'object_type', 'post' )
591 ->where( 'object_sub_type', $post_type )
592 ->where_raw( '( ' . \implode( ' OR ', $likes ) . ' )', \array_values( $params ) )
593 ->order_by_desc( 'object_last_modified' )
594 ->order_by_desc( 'id' )
595 ->limit( $page_size )
596 ->offset( $offset )
597 ->find_many();
598
599 return \array_map( [ $this, 'upgrade_indexable' ], $indexables );
600 }
601
602 /**
603 * Returns the most recently modified cornerstone content of a post type.
604 *
605 * @param string $post_type The post type.
606 * @param int|null $limit The maximum number of posts to return.
607 *
608 * @return Indexable[] array of indexables.
609 */
610 public function get_recent_cornerstone_for_post_type( string $post_type, ?int $limit ) {
611 $query = $this->query()
612 ->where( 'object_type', 'post' )
613 ->where( 'object_sub_type', $post_type )
614 ->where_raw( '( is_public IS NULL OR is_public = 1 )' )
615 ->where( 'is_cornerstone', 1 )
616 ->order_by_desc( 'object_last_modified' );
617
618 if ( $limit !== null ) {
619 $query->limit( $limit );
620 }
621
622 return $query->find_many();
623 }
624
625 /**
626 * Returns the most recently modified about page based on schema_page_type.
627 *
628 * @param string $post_type The post type.
629 *
630 * @return Indexable|false The about page if its there.
631 */
632 public function get_most_recent_about_page( $post_type ) {
633 $query = $this->query()
634 ->where( 'object_type', 'post' )
635 ->where( 'object_sub_type', $post_type )
636 ->where( 'schema_page_type', 'AboutPage' )
637 ->where_raw( '( is_public IS NULL OR is_public = 1 )' )
638 ->order_by_desc( 'object_last_modified' );
639
640 return $query->find_one();
641 }
642
643 /**
644 * Returns the most recently modified posts with keywords of a post type.
645 *
646 * @param string $post_type The post type.
647 * @param int|null $limit The maximum number of posts to return.
648 * @param string|null $date_limit Only include content modified after this date.
649 *
650 * @return array<array<string, string>>|false The array of indexable columns. False if the query failed.
651 */
652 public function get_recent_posts_with_keywords_for_post_type( string $post_type, ?int $limit = null, ?string $date_limit = null ) {
653 $query = $this->query()
654 ->select( 'object_id' )
655 ->select( 'primary_focus_keyword_score' )
656 ->select( 'breadcrumb_title' )
657 ->where( 'object_type', 'post' )
658 ->where( 'object_sub_type', $post_type )
659 ->where_not_equal( 'primary_focus_keyword_score', 0 )
660 ->where_not_null( 'primary_focus_keyword_score' )
661 ->where_raw( "( post_status = 'publish' OR post_status IS NULL )" )
662 ->where_raw( '( is_robots_noindex IS NULL OR is_robots_noindex <> 1 )' )
663 ->order_by_desc( 'object_last_modified' );
664
665 if ( $limit !== null ) {
666 $query->limit( $limit );
667 }
668
669 if ( $date_limit !== null ) {
670 $query->where_gte( 'object_last_modified', $date_limit );
671 }
672
673 return $query->find_array();
674 }
675
676 /**
677 * Returns the most recently modified posts with readability scores of a post type.
678 *
679 * @param string $post_type The post type.
680 * @param int|null $limit The maximum number of posts to return.
681 * @param string|null $date_limit Only include content modified after this date.
682 *
683 * @return array<array<string, string|null>>|false The array of indexable columns. False if the query failed.
684 */
685 public function get_recent_posts_with_readability_scores_for_post_type( string $post_type, ?int $limit = null, ?string $date_limit = null ) {
686 $query = $this->query()
687 ->select( 'object_id' )
688 ->select( 'readability_score' )
689 ->select( 'breadcrumb_title' )
690 ->where( 'object_type', 'post' )
691 ->where( 'object_sub_type', $post_type )
692 ->where_not_null( 'estimated_reading_time_minutes' )
693 ->where_raw( "( post_status = 'publish' OR post_status IS NULL )" )
694 ->order_by_desc( 'object_last_modified' );
695
696 if ( $limit !== null ) {
697 $query->limit( $limit );
698 }
699
700 if ( $date_limit !== null ) {
701 $query->where_gte( 'object_last_modified', $date_limit );
702 }
703
704 return $query->find_array();
705 }
706
707 /**
708 * Returns the most recently modified posts for a post type.
709 *
710 * @param string $post_type The post type.
711 * @param int|null $limit The maximum number of posts to return.
712 * @param string|null $date_limit Only include content modified after this date.
713 *
714 * @return array<array<string, string|null>>|false The array of indexable columns. False if the query failed.
715 */
716 public function get_recent_posts_for_post_type( string $post_type, ?int $limit = null, ?string $date_limit = null ) {
717 $query = $this->query()
718 ->select( 'object_id' )
719 ->select( 'breadcrumb_title' )
720 ->select( 'description' )
721 ->where( 'object_type', 'post' )
722 ->where( 'object_sub_type', $post_type )
723 ->where_raw( "( post_status = 'publish' OR post_status IS NULL )" )
724 ->where_raw( '( is_robots_noindex IS NULL OR is_robots_noindex <> 1 )' )
725 ->order_by_desc( 'object_last_modified' );
726
727 if ( $limit !== null ) {
728 $query->limit( $limit );
729 }
730
731 if ( $date_limit !== null ) {
732 $query->where_gte( 'object_last_modified', $date_limit );
733 }
734
735 return $query->find_array();
736 }
737
738 /**
739 * Updates the incoming link count for an indexable without first fetching it.
740 *
741 * @param int $indexable_id The indexable id.
742 * @param int $count The incoming link count.
743 *
744 * @return bool Whether or not the update was succeful.
745 */
746 public function update_incoming_link_count( $indexable_id, $count ) {
747 return (bool) $this->query()
748 ->set( 'incoming_link_count', $count )
749 ->where( 'id', $indexable_id )
750 ->update_many();
751 }
752
753 /**
754 * Ensures that the given indexable has a permalink.
755 *
756 * Will be deprecated in 17.3 - Use upgrade_indexable instead.
757 *
758 * @codeCoverageIgnore
759 *
760 * @param Indexable $indexable The indexable.
761 *
762 * @return bool|Indexable The indexable.
763 */
764 public function ensure_permalink( $indexable ) {
765 // @phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- self::class is safe.
766 // @phpcs:ignore Squiz.PHP.CommentedOutCode.Found
767 // _deprecated_function( __METHOD__, 'Yoast SEO 17.3', self::class . '::upgrade_indexable' );
768
769 return $this->upgrade_indexable( $indexable );
770 }
771
772 /**
773 * Checks if an Indexable is outdated, and rebuilds it when necessary.
774 *
775 * @param Indexable $indexable The indexable.
776 *
777 * @return Indexable The indexable.
778 */
779 public function upgrade_indexable( $indexable ) {
780 if ( $this->version_manager->indexable_needs_upgrade( $indexable ) ) {
781 $indexable = $this->builder->build( $indexable );
782 }
783 return $indexable;
784 }
785
786 /**
787 * Resets the permalinks of the passed object type and subtype.
788 *
789 * @param string|null $type The type of the indexable. Can be null.
790 * @param string|null $subtype The subtype. Can be null.
791 * @param int|null $object_id The object ID. Can be null.
792 *
793 * @return int|bool The number of permalinks changed if the query was succesful. False otherwise.
794 */
795 public function reset_permalink( $type = null, $subtype = null, $object_id = null ) {
796 $query = $this->query()->set(
797 [
798 'permalink' => null,
799 'permalink_hash' => null,
800 'version' => 0,
801 ],
802 );
803
804 if ( $type !== null ) {
805 $query->where( 'object_type', $type );
806 }
807
808 if ( $type !== null && $subtype !== null ) {
809 $query->where( 'object_sub_type', $subtype );
810 }
811
812 if ( $object_id !== null ) {
813 $query->where( 'object_id', $object_id );
814 }
815
816 return $query->update_many();
817 }
818
819 /**
820 * Gets the total number of stored indexables.
821 *
822 * @return int The total number of stored indexables.
823 */
824 public function get_total_number_of_indexables() {
825 return $this->query()->count();
826 }
827 }
828