PluginProbe
Yoast SEO – Advanced SEO with real-time guidance and built-in AI / 27.5
Yoast SEO – Advanced SEO with real-time guidance and built-in AI v27.5
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-attachment-watcher.php

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

145 lines 4.8 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_Post_Indexation_Action;
6 use Yoast\WP\SEO\Conditionals\Migrations_Conditional;
7 use Yoast\WP\SEO\Config\Indexing_Reasons;
8 use Yoast\WP\SEO\Helpers\Attachment_Cleanup_Helper;
9 use Yoast\WP\SEO\Helpers\Indexable_Helper;
10 use Yoast\WP\SEO\Helpers\Indexing_Helper;
11 use Yoast\WP\SEO\Integrations\Cleanup_Integration;
12 use Yoast\WP\SEO\Integrations\Integration_Interface;
13 use Yoast_Notification_Center;
14
15 /**
16 * Watches the disable-attachment key in wpseo_titles, in order to clear the permalink of the category indexables.
17 */
18 class Indexable_Attachment_Watcher implements Integration_Interface {
19
20 /**
21 * The indexing helper.
22 *
23 * @var Indexing_Helper
24 */
25 protected $indexing_helper;
26
27 /**
28 * The attachment cleanup helper.
29 *
30 * @var Attachment_Cleanup_Helper
31 */
32 protected $attachment_cleanup;
33
34 /**
35 * The indexable helper.
36 *
37 * @var Indexable_Helper
38 */
39 protected $indexable_helper;
40
41 /**
42 * The notifications center.
43 *
44 * @var Yoast_Notification_Center
45 */
46 private $notification_center;
47
48 /**
49 * Returns the conditionals based on which this loadable should be active.
50 *
51 * @return array<string> The conditionals.
52 */
53 public static function get_conditionals() {
54 return [ Migrations_Conditional::class ];
55 }
56
57 /**
58 * Indexable_Attachment_Watcher constructor.
59 *
60 * @param Indexing_Helper $indexing_helper The indexing helper.
61 * @param Attachment_Cleanup_Helper $attachment_cleanup The attachment cleanup helper.
62 * @param Yoast_Notification_Center $notification_center The notification center.
63 * @param Indexable_Helper $indexable_helper The indexable helper.
64 */
65 public function __construct(
66 Indexing_Helper $indexing_helper,
67 Attachment_Cleanup_Helper $attachment_cleanup,
68 Yoast_Notification_Center $notification_center,
69 Indexable_Helper $indexable_helper
70 ) {
71 $this->indexing_helper = $indexing_helper;
72 $this->attachment_cleanup = $attachment_cleanup;
73 $this->notification_center = $notification_center;
74 $this->indexable_helper = $indexable_helper;
75 }
76
77 /**
78 * Initializes the integration.
79 *
80 * This is the place to register hooks and filters.
81 *
82 * @return void
83 */
84 public function register_hooks() {
85 \add_action( 'update_option_wpseo_titles', [ $this, 'check_option' ], 20, 2 );
86 }
87
88 /**
89 * Checks if the disable-attachment key in wpseo_titles has a change in value, and if so,
90 * either it cleans up attachment indexables when it has been toggled to true,
91 * or it starts displaying a notification for the user to start a new SEO optimization.
92 *
93 * @phpcs:disable SlevomatCodingStandard.TypeHints.ParameterTypeHint.MissingTraversableTypeHintSpecification
94 *
95 * @param array $old_value The old value of the wpseo_titles option.
96 * @param array $new_value The new value of the wpseo_titles option.
97 *
98 * @phpcs:enable
99 * @return void
100 */
101 public function check_option( $old_value, $new_value ) {
102 // If this is the first time saving the option, in which case its value would be false.
103 if ( $old_value === false ) {
104 $old_value = [];
105 }
106
107 // If either value is not an array, return.
108 if ( ! \is_array( $old_value ) || ! \is_array( $new_value ) ) {
109 return;
110 }
111
112 // If both values aren't set, they haven't changed.
113 if ( ! isset( $old_value['disable-attachment'] ) && ! isset( $new_value['disable-attachment'] ) ) {
114 return;
115 }
116
117 // If a new value has been set for 'disable-attachment', there's two things we might need to do, depending on what's the new value.
118 if ( $old_value['disable-attachment'] !== $new_value['disable-attachment'] ) {
119 // Delete cache because we now might have new stuff to index or old unindexed stuff don't need indexing anymore.
120 \delete_transient( Indexable_Post_Indexation_Action::UNINDEXED_COUNT_TRANSIENT );
121 \delete_transient( Indexable_Post_Indexation_Action::UNINDEXED_LIMITED_COUNT_TRANSIENT );
122
123 // Set this core option (introduced in WP 6.4) to ensure consistency.
124 if ( \get_option( 'wp_attachment_pages_enabled' ) !== false ) {
125 \update_option( 'wp_attachment_pages_enabled', (int) ! $new_value['disable-attachment'] );
126 }
127
128 switch ( $new_value['disable-attachment'] ) {
129 case false:
130 $this->indexing_helper->set_reason( Indexing_Reasons::REASON_ATTACHMENTS_MADE_ENABLED );
131 return;
132 case true:
133 $this->attachment_cleanup->remove_attachment_indexables( false );
134 $this->attachment_cleanup->clean_attachment_links_from_target_indexable_ids( false );
135
136 if ( $this->indexable_helper->should_index_indexables() && ! \wp_next_scheduled( Cleanup_Integration::START_HOOK ) ) {
137 // This just schedules the cleanup routine cron again.
138 \wp_schedule_single_event( ( \time() + ( \MINUTE_IN_SECONDS * 5 ) ), Cleanup_Integration::START_HOOK );
139 }
140 return;
141 }
142 }
143 }
144 }
145