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 / option-titles-watcher.php

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

131 lines 3.3 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\Lib\Model;
6 use Yoast\WP\SEO\Conditionals\Migrations_Conditional;
7 use Yoast\WP\SEO\Integrations\Integration_Interface;
8 use Yoast\WP\SEO\WordPress\Wrapper;
9
10 /**
11 * Watcher for the titles option.
12 *
13 * Represents the option titles watcher.
14 */
15 class Option_Titles_Watcher implements Integration_Interface {
16
17 /**
18 * Initializes the integration.
19 *
20 * This is the place to register hooks and filters.
21 *
22 * @return void
23 */
24 public function register_hooks() {
25 \add_action( 'update_option_wpseo_titles', [ $this, 'check_option' ], 10, 2 );
26 }
27
28 /**
29 * Returns the conditionals based on which this loadable should be active.
30 *
31 * @return array<Migrations_Conditional>
32 */
33 public static function get_conditionals() {
34 return [ Migrations_Conditional::class ];
35 }
36
37 /**
38 * Checks if one of the relevant options has been changed.
39 *
40 * @param array<string|int|bool> $old_value The old value of the option.
41 * @param array<string|int|bool> $new_value The new value of the option.
42 *
43 * @return bool Whether or not the ancestors are removed.
44 */
45 public function check_option( $old_value, $new_value ) {
46 // If this is the first time saving the option, thus when value is false.
47 if ( $old_value === false ) {
48 $old_value = [];
49 }
50
51 if ( ! \is_array( $old_value ) || ! \is_array( $new_value ) ) {
52 return false;
53 }
54
55 $relevant_keys = $this->get_relevant_keys();
56 if ( empty( $relevant_keys ) ) {
57 return false;
58 }
59
60 $post_types = [];
61
62 foreach ( $relevant_keys as $post_type => $relevant_option ) {
63 // If both values aren't set they haven't changed.
64 if ( ! isset( $old_value[ $relevant_option ] ) && ! isset( $new_value[ $relevant_option ] ) ) {
65 continue;
66 }
67
68 if ( $old_value[ $relevant_option ] !== $new_value[ $relevant_option ] ) {
69 $post_types[] = $post_type;
70 }
71 }
72
73 return $this->delete_ancestors( $post_types );
74 }
75
76 /**
77 * Retrieves the relevant keys.
78 *
79 * @return array<string> Array with the relevant keys.
80 */
81 protected function get_relevant_keys() {
82 $post_types = \get_post_types( [ 'public' => true ], 'names' );
83 if ( ! \is_array( $post_types ) || $post_types === [] ) {
84 return [];
85 }
86
87 $relevant_keys = [];
88 foreach ( $post_types as $post_type ) {
89 $relevant_keys[ $post_type ] = 'post_types-' . $post_type . '-maintax';
90 }
91
92 return $relevant_keys;
93 }
94
95 /**
96 * Removes the ancestors for given post types.
97 *
98 * @param array<string> $post_types The post types to remove hierarchy for.
99 *
100 * @return bool True when delete query was successful.
101 */
102 protected function delete_ancestors( $post_types ) {
103 if ( empty( $post_types ) ) {
104 return false;
105 }
106
107 $wpdb = Wrapper::get_wpdb();
108 $hierarchy_table = Model::get_table_name( 'Indexable_Hierarchy' );
109 $indexable_table = Model::get_table_name( 'Indexable' );
110
111 // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery,WordPress.DB.DirectDatabaseQuery.NoCaching -- Delete query.
112 $result = $wpdb->query(
113 $wpdb->prepare(
114 "
115 DELETE FROM %i
116 WHERE indexable_id IN(
117 SELECT id
118 FROM %i
119 WHERE object_type = 'post'
120 AND object_sub_type IN( " . \implode( ', ', \array_fill( 0, \count( $post_types ), '%s' ) ) . ' )
121 )',
122 $hierarchy_table,
123 $indexable_table,
124 ...$post_types,
125 ),
126 );
127
128 return $result !== false;
129 }
130 }
131