PluginProbe
Yoast SEO – Advanced SEO with real-time guidance and built-in AI / 18.2
Yoast SEO – Advanced SEO with real-time guidance and built-in AI v18.2
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 18.2, at src/integrations/watchers/indexable-system-page-watcher.php

101 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 public function register_hooks() {
58 \add_action( 'update_option_wpseo_titles', [ $this, 'check_option' ], 10, 2 );
59 }
60
61 /**
62 * Checks if the home page indexable needs to be rebuild based on option values.
63 *
64 * @param array $old_value The old value of the option.
65 * @param array $new_value The new value of the option.
66 *
67 * @return void
68 */
69 public function check_option( $old_value, $new_value ) {
70 foreach ( Indexable_System_Page_Builder::OPTION_MAPPING as $type => $options ) {
71 foreach ( $options as $option ) {
72 // If both values aren't set they haven't changed.
73 if ( ! isset( $old_value[ $option ] ) && ! isset( $new_value[ $option ] ) ) {
74 return;
75 }
76
77 // If the value was set but now isn't, is set but wasn't or is not the same it has changed.
78 if (
79 ! isset( $old_value[ $option ] )
80 || ! isset( $new_value[ $option ] )
81 || $old_value[ $option ] !== $new_value[ $option ]
82 ) {
83 $this->build_indexable( $type );
84 }
85 }
86 }
87 }
88
89 /**
90 * Saves the search result.
91 *
92 * @param string $type The type of no index page.
93 *
94 * @return void
95 */
96 public function build_indexable( $type ) {
97 $indexable = $this->repository->find_for_system_page( $type, false );
98 $this->builder->build_for_system_page( $type, $indexable );
99 }
100 }
101