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 / integrations / watchers / indexable-system-page-watcher.php

indexable-system-page-watcher.php in Yoast SEO – Advanced SEO with real-time guidance and built-in AI trunk, at src/integrations/watchers/indexable-system-page-watcher.php

103 lines 2.6 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\Integrations\Watchers;
4
5 use Yoast\WP\SEO\Builders\Indexable_Builder;
6 use Yoast\WP\SEO\Builders\Indexable_System_Page_Builder;
7 use Yoast\WP\SEO\Conditionals\Migrations_Conditional;
8 use Yoast\WP\SEO\Integrations\Integration_Interface;
9 use Yoast\WP\SEO\Repositories\Indexable_Repository;
10
11 /**
12 * Search result watcher to save the meta data to an Indexable.
13 *
14 * Watches the search result options to save the meta information when updated.
15 */
16 class Indexable_System_Page_Watcher implements Integration_Interface {
17
18 /**
19 * The indexable repository.
20 *
21 * @var Indexable_Repository
22 */
23 protected $repository;
24
25 /**
26 * The indexable builder.
27 *
28 * @var Indexable_Builder
29 */
30 protected $builder;
31
32 /**
33 * Returns the conditionals based on which this loadable should be active.
34 *
35 * @return array
36 */
37 public static function get_conditionals() {
38 return [ Migrations_Conditional::class ];
39 }
40
41 /**
42 * Indexable_System_Page_Watcher constructor.
43 *
44 * @param Indexable_Repository $repository The repository to use.
45 * @param Indexable_Builder $builder The post builder to use.
46 */
47 public function __construct( Indexable_Repository $repository, Indexable_Builder $builder ) {
48 $this->repository = $repository;
49 $this->builder = $builder;
50 }
51
52 /**
53 * Initializes the integration.
54 *
55 * This is the place to register hooks and filters.
56 *
57 * @return void
58 */
59 public function register_hooks() {
60 \add_action( 'update_option_wpseo_titles', [ $this, 'check_option' ], 10, 2 );
61 }
62
63 /**
64 * Checks if the home page indexable needs to be rebuild based on option values.
65 *
66 * @param array $old_value The old value of the option.
67 * @param array $new_value The new value of the option.
68 *
69 * @return void
70 */
71 public function check_option( $old_value, $new_value ) {
72 foreach ( Indexable_System_Page_Builder::OPTION_MAPPING as $type => $options ) {
73 foreach ( $options as $option ) {
74 // If both values aren't set they haven't changed.
75 if ( ! isset( $old_value[ $option ] ) && ! isset( $new_value[ $option ] ) ) {
76 return;
77 }
78
79 // If the value was set but now isn't, is set but wasn't or is not the same it has changed.
80 if (
81 ! isset( $old_value[ $option ] )
82 || ! isset( $new_value[ $option ] )
83 || $old_value[ $option ] !== $new_value[ $option ]
84 ) {
85 $this->build_indexable( $type );
86 }
87 }
88 }
89 }
90
91 /**
92 * Saves the search result.
93 *
94 * @param string $type The type of no index page.
95 *
96 * @return void
97 */
98 public function build_indexable( $type ) {
99 $indexable = $this->repository->find_for_system_page( $type, false );
100 $this->builder->build_for_system_page( $type, $indexable );
101 }
102 }
103