PluginProbe
Yoast SEO – Advanced SEO with real-time guidance and built-in AI / 18.1
Yoast SEO – Advanced SEO with real-time guidance and built-in AI v18.1
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 / actions / indexing / indexable-general-indexation-action.php

indexable-general-indexation-action.php in Yoast SEO – Advanced SEO with real-time guidance and built-in AI 18.1, at src/actions/indexing/indexable-general-indexation-action.php

134 lines 3.7 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\Actions\Indexing;
4
5 use Yoast\WP\SEO\Models\Indexable;
6 use Yoast\WP\SEO\Repositories\Indexable_Repository;
7
8 /**
9 * General reindexing action for indexables.
10 *
11 * @phpcs:disable Yoast.NamingConventions.ObjectNameDepth.MaxExceeded
12 */
13 class Indexable_General_Indexation_Action implements Indexation_Action_Interface, Limited_Indexing_Action_Interface {
14
15 /**
16 * The transient cache key.
17 */
18 const UNINDEXED_COUNT_TRANSIENT = 'wpseo_total_unindexed_general_items';
19
20 /**
21 * Represents the indexables repository.
22 *
23 * @var Indexable_Repository
24 */
25 protected $indexable_repository;
26
27 /**
28 * Indexable_General_Indexation_Action constructor.
29 *
30 * @param Indexable_Repository $indexable_repository The indexables repository.
31 */
32 public function __construct( Indexable_Repository $indexable_repository ) {
33 $this->indexable_repository = $indexable_repository;
34 }
35
36 /**
37 * Returns the total number of unindexed objects.
38 *
39 * @return int The total number of unindexed objects.
40 */
41 public function get_total_unindexed() {
42 $transient = \get_transient( static::UNINDEXED_COUNT_TRANSIENT );
43 if ( $transient !== false ) {
44 return (int) $transient;
45 }
46
47 $indexables_to_create = $this->query();
48
49 $result = \count( $indexables_to_create );
50
51 \set_transient( static::UNINDEXED_COUNT_TRANSIENT, $result, \DAY_IN_SECONDS );
52
53 return $result;
54 }
55
56 /**
57 * Returns a limited number of unindexed posts.
58 *
59 * @param int $limit Limit the maximum number of unindexed posts that are counted.
60 *
61 * @return int|false The limited number of unindexed posts. False if the query fails.
62 */
63 public function get_limited_unindexed_count( $limit ) {
64 return $this->get_total_unindexed();
65 }
66
67 /**
68 * Creates indexables for unindexed system pages, the date archive, and the homepage.
69 *
70 * @return Indexable[] The created indexables.
71 */
72 public function index() {
73 $indexables = [];
74 $indexables_to_create = $this->query();
75
76 if ( isset( $indexables_to_create['404'] ) ) {
77 $indexables[] = $this->indexable_repository->find_for_system_page( '404' );
78 }
79
80 if ( isset( $indexables_to_create['search'] ) ) {
81 $indexables[] = $this->indexable_repository->find_for_system_page( 'search-result' );
82 }
83
84 if ( isset( $indexables_to_create['date_archive'] ) ) {
85 $indexables[] = $this->indexable_repository->find_for_date_archive();
86 }
87 if ( isset( $indexables_to_create['home_page'] ) ) {
88 $indexables[] = $this->indexable_repository->find_for_home_page();
89 }
90
91 \set_transient( static::UNINDEXED_COUNT_TRANSIENT, 0, \DAY_IN_SECONDS );
92
93 return $indexables;
94 }
95
96 /**
97 * Returns the number of objects that will be indexed in a single indexing pass.
98 *
99 * @return int The limit.
100 */
101 public function get_limit() {
102 // This matches the maximum number of indexables created by this action.
103 return 4;
104 }
105
106 /**
107 * Check which indexables already exist and return the values of the ones to create.
108 *
109 * @return array The indexable types to create.
110 */
111 private function query() {
112 $indexables_to_create = [];
113 if ( ! $this->indexable_repository->find_for_system_page( '404', false ) ) {
114 $indexables_to_create['404'] = true;
115 }
116
117 if ( ! $this->indexable_repository->find_for_system_page( 'search-result', false ) ) {
118 $indexables_to_create['search'] = true;
119 }
120
121 if ( ! $this->indexable_repository->find_for_date_archive( false ) ) {
122 $indexables_to_create['date_archive'] = true;
123 }
124
125 $need_home_page_indexable = ( (int) \get_option( 'page_on_front' ) === 0 && \get_option( 'show_on_front' ) === 'posts' );
126
127 if ( $need_home_page_indexable && ! $this->indexable_repository->find_for_home_page( false ) ) {
128 $indexables_to_create['home_page'] = true;
129 }
130
131 return $indexables_to_create;
132 }
133 }
134