PluginProbe
Yoast SEO – Advanced SEO with real-time guidance and built-in AI / 28.5
Yoast SEO – Advanced SEO with real-time guidance and built-in AI v28.5
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 / helpers / indexable-helper.php

indexable-helper.php in Yoast SEO – Advanced SEO with real-time guidance and built-in AI 28.5, at src/helpers/indexable-helper.php

328 lines 9.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\Helpers;
4
5 use Yoast\WP\SEO\Actions\Indexing\Indexable_Post_Indexation_Action;
6 use Yoast\WP\SEO\Actions\Indexing\Indexable_Post_Type_Archive_Indexation_Action;
7 use Yoast\WP\SEO\Actions\Indexing\Indexable_Term_Indexation_Action;
8 use Yoast\WP\SEO\Config\Indexing_Reasons;
9 use Yoast\WP\SEO\Models\Indexable;
10 use Yoast\WP\SEO\Repositories\Indexable_Repository;
11
12 /**
13 * A helper object for indexables.
14 */
15 class Indexable_Helper {
16
17 /**
18 * Represents the indexable repository.
19 *
20 * @var Indexable_Repository
21 */
22 protected $repository;
23
24 /**
25 * Represents the options helper.
26 *
27 * @var Options_Helper
28 */
29 protected $options_helper;
30
31 /**
32 * Represents the environment helper.
33 *
34 * @var Environment_Helper
35 */
36 protected $environment_helper;
37
38 /**
39 * Represents the indexing helper.
40 *
41 * @var Indexing_Helper
42 */
43 protected $indexing_helper;
44
45 /**
46 * Default values of certain columns.
47 *
48 * @var array
49 */
50 protected $default_values = [
51 'title' => [
52 'default_value' => null,
53 ],
54 'description' => [
55 'default_value' => null,
56 ],
57 'open_graph_title' => [
58 'default_value' => null,
59 ],
60 'open_graph_description' => [
61 'default_value' => null,
62 ],
63 'twitter_title' => [
64 'default_value' => null,
65 ],
66 'twitter_description' => [
67 'default_value' => null,
68 ],
69 'canonical' => [
70 'default_value' => null,
71 ],
72 'primary_focus_keyword' => [
73 'default_value' => null,
74 ],
75 'is_robots_noindex' => [
76 'default_value' => null,
77 ],
78 'is_robots_nofollow' => [
79 'default_value' => false,
80 ],
81 'is_robots_noarchive' => [
82 'default_value' => null,
83 ],
84 'is_robots_noimageindex' => [
85 'default_value' => null,
86 ],
87 'is_robots_nosnippet' => [
88 'default_value' => null,
89 ],
90 ];
91
92 /**
93 * Indexable_Helper constructor.
94 *
95 * @param Options_Helper $options_helper The options helper.
96 * @param Environment_Helper $environment_helper The environment helper.
97 * @param Indexing_Helper $indexing_helper The indexing helper.
98 */
99 public function __construct( Options_Helper $options_helper, Environment_Helper $environment_helper, Indexing_Helper $indexing_helper ) {
100 $this->options_helper = $options_helper;
101 $this->environment_helper = $environment_helper;
102 $this->indexing_helper = $indexing_helper;
103 }
104
105 /**
106 * Sets the indexable repository. Done to avoid circular dependencies.
107 *
108 * @required
109 *
110 * @param Indexable_Repository $repository The indexable repository.
111 *
112 * @return void
113 */
114 public function set_indexable_repository( Indexable_Repository $repository ) {
115 $this->repository = $repository;
116 }
117
118 /**
119 * Returns the page type of an indexable.
120 *
121 * @param Indexable $indexable The indexable.
122 *
123 * @return string|false The page type. False if it could not be determined.
124 */
125 public function get_page_type_for_indexable( $indexable ) {
126 switch ( $indexable->object_type ) {
127 case 'post':
128 $front_page_id = (int) \get_option( 'page_on_front' );
129 if ( $indexable->object_id === $front_page_id ) {
130 return 'Static_Home_Page';
131 }
132 $posts_page_id = (int) \get_option( 'page_for_posts' );
133 if ( $indexable->object_id === $posts_page_id ) {
134 return 'Static_Posts_Page';
135 }
136
137 return 'Post_Type';
138 case 'term':
139 return 'Term_Archive';
140 case 'user':
141 return 'Author_Archive';
142 case 'home-page':
143 return 'Home_Page';
144 case 'post-type-archive':
145 return 'Post_Type_Archive';
146 case 'date-archive':
147 return 'Date_Archive';
148 case 'system-page':
149 if ( $indexable->object_sub_type === 'search-result' ) {
150 return 'Search_Result_Page';
151 }
152 if ( $indexable->object_sub_type === '404' ) {
153 return 'Error_Page';
154 }
155 }
156
157 return false;
158 }
159
160 /**
161 * Resets the permalinks of the indexables.
162 *
163 * @param string|null $type The type of the indexable.
164 * @param string|null $subtype The subtype. Can be null.
165 * @param string $reason The reason that the permalink has been changed.
166 *
167 * @return void
168 */
169 public function reset_permalink_indexables( $type = null, $subtype = null, $reason = Indexing_Reasons::REASON_PERMALINK_SETTINGS ) {
170 $result = $this->repository->reset_permalink( $type, $subtype );
171
172 $this->indexing_helper->set_reason( $reason );
173
174 if ( $result !== false && $result > 0 ) {
175 \delete_transient( Indexable_Post_Indexation_Action::UNINDEXED_COUNT_TRANSIENT );
176 \delete_transient( Indexable_Post_Type_Archive_Indexation_Action::UNINDEXED_COUNT_TRANSIENT );
177 \delete_transient( Indexable_Term_Indexation_Action::UNINDEXED_COUNT_TRANSIENT );
178 }
179 }
180
181 /**
182 * Determines whether indexing the specific indexable is appropriate at this time.
183 *
184 * @param Indexable $indexable The indexable.
185 *
186 * @return bool Whether indexing the specific indexable is appropriate at this time.
187 */
188 public function should_index_indexable( $indexable ) {
189 $intend_to_save = $this->should_index_indexables();
190
191 /**
192 * Filter: 'wpseo_should_save_indexable' - Allow developers to enable / disable
193 * saving the indexable when the indexable is updated. Warning: overriding
194 * the intended action may cause problems when moving from a staging to a
195 * production environment because indexable permalinks may get set incorrectly.
196 *
197 * @param bool $intend_to_save True if YoastSEO intends to save the indexable.
198 * @param Indexable $indexable The indexable to be saved.
199 */
200 return \apply_filters( 'wpseo_should_save_indexable', $intend_to_save, $indexable );
201 }
202
203 /**
204 * Determines whether indexing indexables is appropriate at this time.
205 *
206 * @return bool Whether the indexables should be indexed.
207 */
208 public function should_index_indexables() {
209 // Currently, the only reason to index is when we're on a production website.
210 $should_index = $this->environment_helper->is_production_mode();
211
212 /**
213 * Filter: 'Yoast\WP\SEO\should_index_indexables' - Allow developers to enable / disable
214 * creating indexables. Warning: overriding
215 * the intended action may cause problems when moving from a staging to a
216 * production environment because indexable permalinks may get set incorrectly.
217 *
218 * @since 18.2
219 *
220 * @param bool $should_index Whether the site's indexables should be created.
221 */
222 return (bool) \apply_filters( 'Yoast\WP\SEO\should_index_indexables', $should_index );
223 }
224
225 /**
226 * Returns whether or not dynamic permalinks should be used.
227 *
228 * @return bool Whether or not the dynamic permalinks should be used.
229 */
230 public function dynamic_permalinks_enabled() {
231 /**
232 * Filters the value of the `dynamic_permalinks` option.
233 *
234 * @param bool $value The value of the `dynamic_permalinks` option.
235 */
236 return (bool) \apply_filters( 'wpseo_dynamic_permalinks_enabled', $this->options_helper->get( 'dynamic_permalinks', false ) );
237 }
238
239 /**
240 * Sets a boolean to indicate that the indexing of the indexables has completed.
241 *
242 * @return void
243 */
244 public function finish_indexing() {
245 $this->options_helper->set( 'indexables_indexing_completed', true );
246 }
247
248 /**
249 * Checks whether the indexable has default values in given fields.
250 *
251 * @param Indexable $indexable The Yoast indexable that we're checking.
252 * @param array $fields The Yoast indexable fields that we're checking against.
253 *
254 * @return bool Whether the indexable has default values.
255 */
256 public function check_if_default_indexable( $indexable, $fields ) {
257 foreach ( $fields as $field ) {
258 $is_default = $this->check_if_default_field( $indexable, $field );
259 if ( ! $is_default ) {
260 break;
261 }
262 }
263
264 return $is_default;
265 }
266
267 /**
268 * Checks if an indexable field contains the default value.
269 *
270 * @param Indexable $indexable The Yoast indexable that we're checking.
271 * @param string $field The field that we're checking.
272 *
273 * @return bool True if default value.
274 */
275 public function check_if_default_field( $indexable, $field ) {
276 $defaults = $this->default_values;
277 if ( ! isset( $defaults[ $field ] ) ) {
278 return false;
279 }
280
281 if ( $indexable->$field === $defaults[ $field ]['default_value'] ) {
282 return true;
283 }
284
285 return false;
286 }
287
288 /**
289 * Saves and returns an indexable (on production environments only).
290 *
291 * Moved from Yoast\WP\SEO\Builders\Indexable_Builder.
292 *
293 * @param Indexable $indexable The indexable.
294 * @param Indexable|null $indexable_before The indexable before possible changes.
295 *
296 * @return bool True if default value.
297 */
298 public function save_indexable( $indexable, $indexable_before = null ) {
299 if ( ! $this->should_index_indexable( $indexable ) ) {
300 return $indexable;
301 }
302
303 // Save the indexable before running the WordPress hook.
304 $indexable->save();
305
306 if ( $indexable_before ) {
307 /**
308 * Action: 'wpseo_save_indexable' - Allow developers to perform an action
309 * when the indexable is updated.
310 *
311 * @param Indexable $indexable The saved indexable.
312 * @param Indexable $indexable_before The indexable before saving.
313 */
314 \do_action( 'wpseo_save_indexable', $indexable, $indexable_before );
315 }
316
317 /**
318 * Action: 'wpseo_save_indexable' - Allow developers to perform an action
319 * right after the indexable is created or updated.
320 *
321 * @param Indexable $indexable The saved indexable.
322 */
323 \do_action( 'wpseo_saved_indexable', $indexable );
324
325 return $indexable;
326 }
327 }
328