PluginProbe
Yoast SEO – Advanced SEO with real-time guidance and built-in AI / 28.1
Yoast SEO – Advanced SEO with real-time guidance and built-in AI v28.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 / integrations / watchers / indexable-home-page-watcher.php

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

138 lines 3.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\Conditionals\Migrations_Conditional;
7 use Yoast\WP\SEO\Helpers\Indexable_Helper;
8 use Yoast\WP\SEO\Integrations\Integration_Interface;
9 use Yoast\WP\SEO\Repositories\Indexable_Repository;
10
11 /**
12 * Home page watcher to save the meta data to an Indexable.
13 *
14 * Watches the home page options to save the meta information when updated.
15 */
16 class Indexable_Home_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 helper.
27 *
28 * @var Indexable_Helper
29 */
30 protected $indexable_helper;
31
32 /**
33 * The indexable builder.
34 *
35 * @var Indexable_Builder
36 */
37 protected $builder;
38
39 /**
40 * Returns the conditionals based on which this loadable should be active.
41 *
42 * @return array
43 */
44 public static function get_conditionals() {
45 return [ Migrations_Conditional::class ];
46 }
47
48 /**
49 * Indexable_Home_Page_Watcher constructor.
50 *
51 * @param Indexable_Repository $repository The repository to use.
52 * @param Indexable_Helper $indexable_helper The indexable helper.
53 * @param Indexable_Builder $builder The post builder to use.
54 */
55 public function __construct(
56 Indexable_Repository $repository,
57 Indexable_Helper $indexable_helper,
58 Indexable_Builder $builder
59 ) {
60 $this->repository = $repository;
61 $this->indexable_helper = $indexable_helper;
62 $this->builder = $builder;
63 }
64
65 /**
66 * Initializes the integration.
67 *
68 * This is the place to register hooks and filters.
69 *
70 * @return void
71 */
72 public function register_hooks() {
73 \add_action( 'update_option_wpseo_titles', [ $this, 'check_option' ], 15, 3 );
74 \add_action( 'update_option_wpseo_social', [ $this, 'check_option' ], 15, 3 );
75 \add_action( 'update_option_blog_public', [ $this, 'build_indexable' ] );
76 \add_action( 'update_option_blogdescription', [ $this, 'build_indexable' ] );
77 }
78
79 /**
80 * Checks if the home page indexable needs to be rebuild based on option values.
81 *
82 * @param array $old_value The old value of the option.
83 * @param array $new_value The new value of the option.
84 * @param string $option The name of the option.
85 *
86 * @return void
87 */
88 public function check_option( $old_value, $new_value, $option ) {
89 $relevant_keys = [
90 'wpseo_titles' => [
91 'title-home-wpseo',
92 'breadcrumbs-home',
93 'metadesc-home-wpseo',
94 'open_graph_frontpage_title',
95 'open_graph_frontpage_desc',
96 'open_graph_frontpage_image',
97 ],
98 ];
99
100 if ( ! isset( $relevant_keys[ $option ] ) ) {
101 return;
102 }
103
104 foreach ( $relevant_keys[ $option ] as $key ) {
105 // If both values aren't set they haven't changed.
106 if ( ! isset( $old_value[ $key ] ) && ! isset( $new_value[ $key ] ) ) {
107 continue;
108 }
109
110 // If the value was set but now isn't, is set but wasn't or is not the same it has changed.
111 if ( ! isset( $old_value[ $key ] ) || ! isset( $new_value[ $key ] ) || $old_value[ $key ] !== $new_value[ $key ] ) {
112 $this->build_indexable();
113 return;
114 }
115 }
116 }
117
118 /**
119 * Saves the home page.
120 *
121 * @return void
122 */
123 public function build_indexable() {
124 $indexable = $this->repository->find_for_home_page( false );
125
126 if ( $indexable === false && ! $this->indexable_helper->should_index_indexables() ) {
127 return;
128 }
129
130 $indexable = $this->builder->build_for_home_page( $indexable );
131
132 if ( $indexable ) {
133 $indexable->object_last_modified = \max( $indexable->object_last_modified, \current_time( 'mysql' ) );
134 $indexable->save();
135 }
136 }
137 }
138