PluginProbe
Yoast SEO – Advanced SEO with real-time guidance and built-in AI / 28.0
Yoast SEO – Advanced SEO with real-time guidance and built-in AI v28.0
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 / inc / options / class-wpseo-options.php

class-wpseo-options.php in Yoast SEO – Advanced SEO with real-time guidance and built-in AI 28.0, at inc/options/class-wpseo-options.php

607 lines 17.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * WPSEO plugin file.
4 *
5 * @package WPSEO\Internals\Options
6 */
7
8 /**
9 * Overall Option Management class.
10 *
11 * Instantiates all the options and offers a number of utility methods to work with the options.
12 */
13 class WPSEO_Options {
14
15 /**
16 * The option values.
17 *
18 * @var array|null
19 */
20 protected static $option_values = null;
21
22 /**
23 * Options this class uses.
24 *
25 * @var array Array format: (string) option_name => (string) name of concrete class for the option.
26 */
27 public static $options = [
28 'wpseo' => 'WPSEO_Option_Wpseo',
29 'wpseo_titles' => 'WPSEO_Option_Titles',
30 'wpseo_social' => 'WPSEO_Option_Social',
31 'wpseo_ms' => 'WPSEO_Option_MS',
32 'wpseo_taxonomy_meta' => 'WPSEO_Taxonomy_Meta',
33 'wpseo_llmstxt' => 'WPSEO_Option_Llmstxt',
34 'wpseo_tracking_only' => 'WPSEO_Option_Tracking_Only',
35 ];
36
37 /**
38 * Array of instantiated option objects.
39 *
40 * @var array
41 */
42 protected static $option_instances = [];
43
44 /**
45 * Array with the option names.
46 *
47 * @var array
48 */
49 protected static $option_names = [];
50
51 /**
52 * Instance of this class.
53 *
54 * @var WPSEO_Options
55 */
56 protected static $instance;
57
58 /**
59 * Instantiate all the WPSEO option management classes.
60 */
61 protected function __construct() {
62 $this->register_hooks();
63
64 foreach ( static::$options as $option_class ) {
65 static::register_option( call_user_func( [ $option_class, 'get_instance' ] ) );
66 }
67 }
68
69 /**
70 * Register our hooks.
71 *
72 * @return void
73 */
74 public function register_hooks() {
75 add_action( 'registered_taxonomy', [ $this, 'clear_cache' ] );
76 add_action( 'unregistered_taxonomy', [ $this, 'clear_cache' ] );
77 add_action( 'registered_post_type', [ $this, 'clear_cache' ] );
78 add_action( 'unregistered_post_type', [ $this, 'clear_cache' ] );
79 }
80
81 /**
82 * Get the singleton instance of this class.
83 *
84 * @return object
85 */
86 public static function get_instance() {
87 if ( ! ( static::$instance instanceof self ) ) {
88 static::$instance = new self();
89 }
90
91 return static::$instance;
92 }
93
94 /**
95 * Registers an option to the options list.
96 *
97 * @param WPSEO_Option $option_instance Instance of the option.
98 *
99 * @return void
100 */
101 public static function register_option( WPSEO_Option $option_instance ) {
102 $option_name = $option_instance->get_option_name();
103
104 if ( $option_instance->multisite_only && ! static::is_multisite() ) {
105 unset( static::$options[ $option_name ], static::$option_names[ $option_name ] );
106
107 return;
108 }
109
110 $is_already_registered = array_key_exists( $option_name, static::$options );
111 if ( ! $is_already_registered ) {
112 static::$options[ $option_name ] = get_class( $option_instance );
113 }
114
115 if ( $option_instance->include_in_all === true ) {
116 static::$option_names[ $option_name ] = $option_name;
117 }
118
119 static::$option_instances[ $option_name ] = $option_instance;
120
121 if ( ! $is_already_registered ) {
122 static::clear_cache();
123 }
124 }
125
126 /**
127 * Get the group name of an option for use in the settings form.
128 *
129 * @param string $option_name The option for which you want to retrieve the option group name.
130 *
131 * @return string|bool
132 */
133 public static function get_group_name( $option_name ) {
134 if ( isset( static::$option_instances[ $option_name ] ) ) {
135 return static::$option_instances[ $option_name ]->group_name;
136 }
137
138 return false;
139 }
140
141 /**
142 * Get a specific default value for an option.
143 *
144 * @param string $option_name The option for which you want to retrieve a default.
145 * @param string $key The key within the option who's default you want.
146 *
147 * @return mixed
148 */
149 public static function get_default( $option_name, $key ) {
150 if ( isset( static::$option_instances[ $option_name ] ) ) {
151 $defaults = static::$option_instances[ $option_name ]->get_defaults();
152 if ( isset( $defaults[ $key ] ) ) {
153 return $defaults[ $key ];
154 }
155 }
156
157 return null;
158 }
159
160 /**
161 * Update a site_option.
162 *
163 * @param string $option_name The option name of the option to save.
164 * @param mixed $value The new value for the option.
165 *
166 * @return bool
167 */
168 public static function update_site_option( $option_name, $value ) {
169 if ( is_multisite() && isset( static::$option_instances[ $option_name ] ) ) {
170 return static::$option_instances[ $option_name ]->update_site_option( $value );
171 }
172
173 return false;
174 }
175
176 /**
177 * Get the instantiated option instance.
178 *
179 * @param string $option_name The option for which you want to retrieve the instance.
180 *
181 * @return object|bool
182 */
183 public static function get_option_instance( $option_name ) {
184 if ( isset( static::$option_instances[ $option_name ] ) ) {
185 return static::$option_instances[ $option_name ];
186 }
187
188 return false;
189 }
190
191 /**
192 * Retrieve an array of the options which should be included in get_all() and reset().
193 *
194 * @return array Array of option names.
195 */
196 public static function get_option_names() {
197 $option_names = array_values( static::$option_names );
198 if ( $option_names === [] ) {
199 foreach ( static::$option_instances as $option_name => $option_object ) {
200 if ( $option_object->include_in_all === true ) {
201 $option_names[] = $option_name;
202 }
203 }
204 }
205
206 /**
207 * Filter: wpseo_options - Allow developers to change the option name to include.
208 *
209 * @param array $option_names The option names to include in get_all and reset().
210 */
211 return apply_filters( 'wpseo_options', $option_names );
212 }
213
214 /**
215 * Retrieve all the options for the SEO plugin in one go.
216 *
217 * @param array<string> $specific_options The option groups of the option you want to get.
218 *
219 * @return array Array combining the values of all the options.
220 */
221 public static function get_all( $specific_options = [] ) {
222 $option_names = ( empty( $specific_options ) ) ? static::get_option_names() : $specific_options;
223 static::$option_values = static::get_options( $option_names );
224
225 return static::$option_values;
226 }
227
228 /**
229 * Retrieve one or more options for the SEO plugin.
230 *
231 * @param array $option_names An array of option names of the options you want to get.
232 *
233 * @return array Array combining the values of the requested options.
234 */
235 public static function get_options( array $option_names ) {
236 $options = [];
237 $option_names = array_filter( $option_names, 'is_string' );
238 foreach ( $option_names as $option_name ) {
239 if ( isset( static::$option_instances[ $option_name ] ) ) {
240 $option = static::get_option( $option_name );
241
242 if ( $option !== null ) {
243 $options = array_merge( $options, $option );
244 }
245 }
246 }
247
248 return $options;
249 }
250
251 /**
252 * Retrieve a single option for the SEO plugin.
253 *
254 * @param string $option_name The name of the option you want to get.
255 *
256 * @return array Array containing the requested option.
257 */
258 public static function get_option( $option_name ) {
259 $option = null;
260 if ( is_string( $option_name ) && ! empty( $option_name ) ) {
261 if ( isset( static::$option_instances[ $option_name ] ) ) {
262 if ( static::$option_instances[ $option_name ]->multisite_only !== true ) {
263 $option = get_option( $option_name );
264 }
265 else {
266 $option = get_site_option( $option_name );
267 }
268 }
269 }
270
271 return $option;
272 }
273
274 /**
275 * Retrieve a single field from any option for the SEO plugin. Keys are always unique.
276 *
277 * @param string $key The key it should return.
278 * @param mixed $default_value The default value that should be returned if the key isn't set.
279 * @param array<string> $option_groups The option groups to retrieve the option from.
280 *
281 * @return mixed Returns value if found, $default_value if not.
282 */
283 public static function get( $key, $default_value = null, $option_groups = [] ) {
284 if ( ! isset( static::$option_values[ $key ] ) ) {
285 static::prime_cache( $option_groups );
286 }
287 if ( isset( static::$option_values[ $key ] ) ) {
288 return static::$option_values[ $key ];
289 }
290
291 return $default_value;
292 }
293
294 /**
295 * Resets the cache to null.
296 *
297 * @return void
298 */
299 public static function clear_cache() {
300 static::$option_values = null;
301 }
302
303 /**
304 * Primes our cache.
305 *
306 * @param array<string> $option_groups The option groups to prime the cache with.
307 *
308 * @return void
309 */
310 private static function prime_cache( $option_groups = [] ) {
311 static::$option_values = static::get_all( $option_groups );
312 static::$option_values = static::add_ms_option( static::$option_values );
313 }
314
315 /**
316 * Retrieve a single field from an option for the SEO plugin.
317 *
318 * @param string $key The key to set.
319 * @param mixed $value The value to set.
320 * @param string $option_group The lookup table which represents the option_group where the key is stored.
321 *
322 * @return mixed|null Returns value if found, $default if not.
323 */
324 public static function set( $key, $value, $option_group = '' ) {
325 $lookup_table = static::get_lookup_table( $option_group );
326
327 if ( isset( $lookup_table[ $key ] ) ) {
328 return static::save_option( $lookup_table[ $key ], $key, $value );
329 }
330
331 $patterns = static::get_pattern_table();
332 foreach ( $patterns as $pattern => $option ) {
333 if ( strpos( $key, $pattern ) === 0 ) {
334 return static::save_option( $option, $key, $value );
335 }
336 }
337
338 static::$option_values[ $key ] = $value;
339 }
340
341 /**
342 * Get an option only if it's been auto-loaded.
343 *
344 * @param string $option The option to retrieve.
345 * @param mixed $default_value A default value to return.
346 *
347 * @return mixed
348 */
349 public static function get_autoloaded_option( $option, $default_value = false ) {
350 $value = wp_cache_get( $option, 'options' );
351 if ( $value === false ) {
352 $passed_default = func_num_args() > 1;
353
354 // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals -- Using WP native filter.
355 return apply_filters( "default_option_{$option}", $default_value, $option, $passed_default );
356 }
357
358 // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals -- Using WP native filter.
359 return apply_filters( "option_{$option}", maybe_unserialize( $value ), $option );
360 }
361
362 /**
363 * Run the clean up routine for one or all options.
364 *
365 * @param array|string|null $option_name Optional. the option you want to clean or an array of
366 * option names for the options you want to clean.
367 * If not set, all options will be cleaned.
368 * @param string|null $current_version Optional. Version from which to upgrade, if not set,
369 * version specific upgrades will be disregarded.
370 *
371 * @return void
372 */
373 public static function clean_up( $option_name = null, $current_version = null ) {
374 if ( isset( $option_name ) && is_string( $option_name ) && $option_name !== '' ) {
375 if ( isset( static::$option_instances[ $option_name ] ) ) {
376 static::$option_instances[ $option_name ]->clean( $current_version );
377 }
378 }
379 elseif ( isset( $option_name ) && is_array( $option_name ) && $option_name !== [] ) {
380 foreach ( $option_name as $option ) {
381 if ( isset( static::$option_instances[ $option ] ) ) {
382 static::$option_instances[ $option ]->clean( $current_version );
383 }
384 }
385 unset( $option );
386 }
387 else {
388 foreach ( static::$option_instances as $instance ) {
389 $instance->clean( $current_version );
390 }
391 unset( $instance );
392
393 // If we've done a full clean-up, we can safely remove this really old option.
394 delete_option( 'wpseo_indexation' );
395 }
396 }
397
398 /**
399 * Check that all options exist in the database and add any which don't.
400 *
401 * @return void
402 */
403 public static function ensure_options_exist() {
404 foreach ( static::$option_instances as $instance ) {
405 $instance->maybe_add_option();
406 }
407 }
408
409 /**
410 * Initialize some options on first install/activate/reset.
411 *
412 * @return void
413 */
414 public static function initialize() {
415 /* Force WooThemes to use Yoast SEO data. */
416 if ( function_exists( 'woo_version_init' ) ) {
417 update_option( 'seo_woo_use_third_party_data', 'true' );
418 }
419 }
420
421 /**
422 * Reset all options to their default values and rerun some tests.
423 *
424 * @return void
425 */
426 public static function reset() {
427 if ( ! is_multisite() ) {
428 $option_names = static::get_option_names();
429 if ( is_array( $option_names ) && $option_names !== [] ) {
430 foreach ( $option_names as $option_name ) {
431 delete_option( $option_name );
432 update_option( $option_name, get_option( $option_name ) );
433 }
434 }
435 unset( $option_names );
436 }
437 else {
438 // Reset MS blog based on network default blog setting.
439 static::reset_ms_blog( get_current_blog_id() );
440 }
441
442 static::initialize();
443 }
444
445 /**
446 * Initialize default values for a new multisite blog.
447 *
448 * @param bool $force_init Whether to always do the initialization routine (title/desc test).
449 *
450 * @return void
451 */
452 public static function maybe_set_multisite_defaults( $force_init = false ) {
453 $option = get_option( 'wpseo' );
454
455 if ( is_multisite() ) {
456 if ( $option['ms_defaults_set'] === false ) {
457 static::reset_ms_blog( get_current_blog_id() );
458 static::initialize();
459 }
460 elseif ( $force_init === true ) {
461 static::initialize();
462 }
463 }
464 }
465
466 /**
467 * Reset all options for a specific multisite blog to their default values based upon a
468 * specified default blog if one was chosen on the network page or the plugin defaults if it was not.
469 *
470 * @param int|string $blog_id Blog id of the blog for which to reset the options.
471 *
472 * @return void
473 */
474 public static function reset_ms_blog( $blog_id ) {
475 if ( is_multisite() ) {
476 $options = get_site_option( 'wpseo_ms' );
477 $option_names = static::get_option_names();
478
479 if ( is_array( $option_names ) && $option_names !== [] ) {
480 $base_blog_id = $blog_id;
481 if ( $options['defaultblog'] !== '' && $options['defaultblog'] !== 0 ) {
482 $base_blog_id = $options['defaultblog'];
483 }
484
485 foreach ( $option_names as $option_name ) {
486 delete_blog_option( $blog_id, $option_name );
487
488 $new_option = get_blog_option( $base_blog_id, $option_name );
489
490 /* Remove sensitive, theme dependent and site dependent info. */
491 if ( isset( static::$option_instances[ $option_name ] ) && static::$option_instances[ $option_name ]->ms_exclude !== [] ) {
492 foreach ( static::$option_instances[ $option_name ]->ms_exclude as $key ) {
493 unset( $new_option[ $key ] );
494 }
495 }
496
497 if ( $option_name === 'wpseo' ) {
498 $new_option['ms_defaults_set'] = true;
499 }
500
501 update_blog_option( $blog_id, $option_name, $new_option );
502 }
503 }
504 }
505 }
506
507 /**
508 * Saves the option to the database.
509 *
510 * @param string $wpseo_options_group_name The name for the wpseo option group in the database.
511 * @param string $option_name The name for the option to set.
512 * @param mixed $option_value The value for the option.
513 *
514 * @return bool Returns true if the option is successfully saved in the database.
515 */
516 public static function save_option( $wpseo_options_group_name, $option_name, $option_value ) {
517 $options = static::get_option( $wpseo_options_group_name );
518 $options[ $option_name ] = $option_value;
519
520 if ( isset( static::$option_instances[ $wpseo_options_group_name ] ) && static::$option_instances[ $wpseo_options_group_name ]->multisite_only === true ) {
521 static::update_site_option( $wpseo_options_group_name, $options );
522 }
523 else {
524 update_option( $wpseo_options_group_name, $options );
525 }
526
527 // Check if everything got saved properly.
528 $saved_option = static::get_option( $wpseo_options_group_name );
529
530 // Clear our cache.
531 static::clear_cache();
532
533 return $saved_option[ $option_name ] === $options[ $option_name ];
534 }
535
536 /**
537 * Adds the multisite options to the option stack if relevant.
538 *
539 * @param array $option The currently present options settings.
540 *
541 * @return array Options possibly including multisite.
542 */
543 protected static function add_ms_option( $option ) {
544 if ( ! is_multisite() ) {
545 return $option;
546 }
547
548 $ms_option = static::get_option( 'wpseo_ms' );
549 if ( $ms_option === null ) {
550 return $option;
551 }
552
553 return array_merge( $option, $ms_option );
554 }
555
556 /**
557 * Checks if installation is multisite.
558 *
559 * @return bool True when is multisite.
560 */
561 protected static function is_multisite() {
562 static $is_multisite;
563
564 $is_multisite ??= is_multisite();
565
566 return $is_multisite;
567 }
568
569 /**
570 * Retrieves a lookup table to find in which option_group a key is stored.
571 *
572 * @param string $option_group The option_group where the key is stored.
573 *
574 * @return array The lookup table.
575 */
576 private static function get_lookup_table( $option_group = '' ) {
577 $lookup_table = [];
578 $option_groups = ( $option_group === '' ) ? static::$options : [ $option_group => static::$options[ $option_group ] ];
579
580 foreach ( array_keys( $option_groups ) as $option_name ) {
581 $full_option = static::get_option( $option_name );
582 foreach ( $full_option as $key => $value ) {
583 $lookup_table[ $key ] = $option_name;
584 }
585 }
586
587 return $lookup_table;
588 }
589
590 /**
591 * Retrieves a lookup table to find in which option_group a key is stored.
592 *
593 * @return array The lookup table.
594 */
595 private static function get_pattern_table() {
596 $pattern_table = [];
597 foreach ( static::$options as $option_name => $option_class ) {
598 $instance = call_user_func( [ $option_class, 'get_instance' ] );
599 foreach ( $instance->get_patterns() as $key ) {
600 $pattern_table[ $key ] = $option_name;
601 }
602 }
603
604 return $pattern_table;
605 }
606 }
607