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-static-home-page-watcher.php

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

98 lines 2.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\Integrations\Watchers;
4
5 use Yoast\WP\SEO\Conditionals\Admin_Conditional;
6 use Yoast\WP\SEO\Integrations\Integration_Interface;
7 use Yoast\WP\SEO\Repositories\Indexable_Repository;
8
9 /**
10 * Watcher that checks for changes in the page used as homepage.
11 *
12 * Watches the static homepage option and updates the permalinks accordingly.
13 */
14 class Indexable_Static_Home_Page_Watcher implements Integration_Interface {
15
16 /**
17 * The indexable repository.
18 *
19 * @var Indexable_Repository
20 */
21 protected $repository;
22
23 /**
24 * Returns the conditionals based on which this loadable should be active.
25 *
26 * @return array
27 */
28 public static function get_conditionals() {
29 return [ Admin_Conditional::class ];
30 }
31
32 /**
33 * Indexable_Static_Home_Page_Watcher constructor.
34 *
35 * @codeCoverageIgnore
36 *
37 * @param Indexable_Repository $repository The repository to use.
38 */
39 public function __construct( Indexable_Repository $repository ) {
40 $this->repository = $repository;
41 }
42
43 /**
44 * Initializes the integration.
45 *
46 * This is the place to register hooks and filters.
47 *
48 * @return void
49 */
50 public function register_hooks() {
51 \add_action( 'update_option_page_on_front', [ $this, 'update_static_homepage_permalink' ], 10, 2 );
52 }
53
54 /**
55 * Updates the new and previous homepage's permalink when the static home page is updated.
56 *
57 * @param string $old_value The previous homepage's ID.
58 * @param int $value The new homepage's ID.
59 *
60 * @return void
61 */
62 public function update_static_homepage_permalink( $old_value, $value ) {
63 if ( \is_string( $old_value ) ) {
64 $old_value = (int) $old_value;
65 }
66
67 if ( $old_value === $value ) {
68 return;
69 }
70
71 $this->update_permalink_for_page( $old_value );
72 $this->update_permalink_for_page( $value );
73 }
74
75 /**
76 * Updates the permalink based on the selected homepage settings.
77 *
78 * @param int $page_id The page's id.
79 *
80 * @return void
81 */
82 private function update_permalink_for_page( $page_id ) {
83 if ( $page_id === 0 ) {
84 return;
85 }
86
87 $indexable = $this->repository->find_by_id_and_type( $page_id, 'post', false );
88
89 if ( $indexable === false ) {
90 return;
91 }
92
93 $indexable->permalink = \get_permalink( $page_id );
94
95 $indexable->save();
96 }
97 }
98