PluginProbe
Yoast SEO – Advanced SEO with real-time guidance and built-in AI / 28.4
Yoast SEO – Advanced SEO with real-time guidance and built-in AI v28.4
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-taxonomy-change-watcher.php

indexable-taxonomy-change-watcher.php in Yoast SEO – Advanced SEO with real-time guidance and built-in AI 28.4, at src/integrations/watchers/indexable-taxonomy-change-watcher.php

162 lines 5.1 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\Actions\Indexing\Indexable_Term_Indexation_Action;
6 use Yoast\WP\SEO\Conditionals\Admin_Conditional;
7 use Yoast\WP\SEO\Conditionals\Migrations_Conditional;
8 use Yoast\WP\SEO\Conditionals\Not_Admin_Ajax_Conditional;
9 use Yoast\WP\SEO\Config\Indexing_Reasons;
10 use Yoast\WP\SEO\Helpers\Indexable_Helper;
11 use Yoast\WP\SEO\Helpers\Indexing_Helper;
12 use Yoast\WP\SEO\Helpers\Options_Helper;
13 use Yoast\WP\SEO\Helpers\Taxonomy_Helper;
14 use Yoast\WP\SEO\Integrations\Cleanup_Integration;
15 use Yoast\WP\SEO\Integrations\Integration_Interface;
16 use Yoast_Notification_Center;
17
18 /**
19 * Taxonomy watcher.
20 *
21 * Responds to changes in taxonomies public availability.
22 */
23 class Indexable_Taxonomy_Change_Watcher implements Integration_Interface {
24
25 /**
26 * The indexing helper.
27 *
28 * @var Indexing_Helper
29 */
30 protected $indexing_helper;
31
32 /**
33 * The indexable helper.
34 *
35 * @var Indexable_Helper
36 */
37 protected $indexable_helper;
38
39 /**
40 * Holds the Options_Helper instance.
41 *
42 * @var Options_Helper
43 */
44 private $options;
45
46 /**
47 * Holds the Taxonomy_Helper instance.
48 *
49 * @var Taxonomy_Helper
50 */
51 private $taxonomy_helper;
52
53 /**
54 * The notifications center.
55 *
56 * @var Yoast_Notification_Center
57 */
58 private $notification_center;
59
60 /**
61 * Returns the conditionals based on which this loadable should be active.
62 *
63 * @return array<string> The conditionals.
64 */
65 public static function get_conditionals() {
66 return [ Not_Admin_Ajax_Conditional::class, Admin_Conditional::class, Migrations_Conditional::class ];
67 }
68
69 /**
70 * Indexable_Taxonomy_Change_Watcher constructor.
71 *
72 * @param Indexing_Helper $indexing_helper The indexing helper.
73 * @param Options_Helper $options The options helper.
74 * @param Taxonomy_Helper $taxonomy_helper The taxonomy helper.
75 * @param Yoast_Notification_Center $notification_center The notification center.
76 * @param Indexable_Helper $indexable_helper The indexable helper.
77 */
78 public function __construct(
79 Indexing_Helper $indexing_helper,
80 Options_Helper $options,
81 Taxonomy_Helper $taxonomy_helper,
82 Yoast_Notification_Center $notification_center,
83 Indexable_Helper $indexable_helper
84 ) {
85 $this->indexing_helper = $indexing_helper;
86 $this->options = $options;
87 $this->taxonomy_helper = $taxonomy_helper;
88 $this->notification_center = $notification_center;
89 $this->indexable_helper = $indexable_helper;
90 }
91
92 /**
93 * Initializes the integration.
94 *
95 * This is the place to register hooks and filters.
96 *
97 * @return void
98 */
99 public function register_hooks() {
100 \add_action( 'admin_init', [ $this, 'check_taxonomy_public_availability' ] );
101 }
102
103 /**
104 * Checks if one or more taxonomies change visibility.
105 *
106 * @return void
107 */
108 public function check_taxonomy_public_availability() {
109
110 // We have to make sure this is just a plain http request, no ajax/REST.
111 if ( \wp_is_json_request() ) {
112 return;
113 }
114
115 $public_taxonomies = $this->taxonomy_helper->get_indexable_taxonomies();
116
117 $last_known_public_taxonomies = $this->options->get( 'last_known_public_taxonomies', [] );
118
119 // Initializing the option on the first run.
120 if ( empty( $last_known_public_taxonomies ) ) {
121 $this->options->set( 'last_known_public_taxonomies', $public_taxonomies );
122 return;
123 }
124
125 // We look for new public taxonomies.
126 $newly_made_public_taxonomies = \array_diff( $public_taxonomies, $last_known_public_taxonomies );
127
128 // We look fortaxonomies that from public have been made private.
129 $newly_made_non_public_taxonomies = \array_diff( $last_known_public_taxonomies, $public_taxonomies );
130
131 // Nothing to be done if no changes has been made to taxonomies.
132 if ( empty( $newly_made_public_taxonomies ) && ( empty( $newly_made_non_public_taxonomies ) ) ) {
133 return;
134 }
135
136 // Update the list of last known public taxonomies in the database.
137 $this->options->set( 'last_known_public_taxonomies', $public_taxonomies );
138
139 // There are new taxonomies that have been made public.
140 if ( ! empty( $newly_made_public_taxonomies ) ) {
141
142 // Force a notification requesting to start the SEO data optimization.
143 \delete_transient( Indexable_Term_Indexation_Action::UNINDEXED_COUNT_TRANSIENT );
144 \delete_transient( Indexable_Term_Indexation_Action::UNINDEXED_LIMITED_COUNT_TRANSIENT );
145
146 $this->indexing_helper->set_reason( Indexing_Reasons::REASON_TAXONOMY_MADE_PUBLIC );
147 \do_action( 'new_public_taxonomy_notifications', $newly_made_public_taxonomies );
148 }
149
150 // There are taxonomies that have been made private.
151 if ( ! empty( $newly_made_non_public_taxonomies ) && $this->indexable_helper->should_index_indexables() ) {
152 // Schedule a cron job to remove all the terms whose taxonomy has been made private.
153 $cleanup_not_yet_scheduled = ! \wp_next_scheduled( Cleanup_Integration::START_HOOK );
154 if ( $cleanup_not_yet_scheduled ) {
155 \wp_schedule_single_event( ( \time() + ( \MINUTE_IN_SECONDS * 5 ) ), Cleanup_Integration::START_HOOK );
156 }
157
158 \do_action( 'clean_new_public_taxonomy_notifications', $newly_made_non_public_taxonomies );
159 }
160 }
161 }
162