PluginProbe
Yoast SEO – Advanced SEO with real-time guidance and built-in AI / 18.4
Yoast SEO – Advanced SEO with real-time guidance and built-in AI v18.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 / inc / options / class-wpseo-option-ms.php

class-wpseo-option-ms.php in Yoast SEO – Advanced SEO with real-time guidance and built-in AI 18.4, at inc/options/class-wpseo-option-ms.php

235 lines 7.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * WPSEO plugin file.
4 *
5 * @package WPSEO\Internals\Options
6 */
7
8 /**
9 * Site option for Multisite installs only
10 *
11 * Overloads a number of methods of the abstract class to ensure the use of the correct site_option
12 * WP functions.
13 */
14 class WPSEO_Option_MS extends WPSEO_Option {
15
16 /**
17 * Option name.
18 *
19 * @var string
20 */
21 public $option_name = 'wpseo_ms';
22
23 /**
24 * Option group name for use in settings forms.
25 *
26 * @var string
27 */
28 public $group_name = 'yoast_wpseo_multisite_options';
29
30 /**
31 * Whether to include the option in the return for WPSEO_Options::get_all().
32 *
33 * @var bool
34 */
35 public $include_in_all = false;
36
37 /**
38 * Whether this option is only for when the install is multisite.
39 *
40 * @var bool
41 */
42 public $multisite_only = true;
43
44 /**
45 * Array of defaults for the option.
46 *
47 * Shouldn't be requested directly, use $this->get_defaults();
48 *
49 * @var array
50 */
51 protected $defaults = [];
52
53 /**
54 * Available options for the 'access' setting. Used for input validation.
55 *
56 * {@internal Important: Make sure the options added to the array here are in line
57 * with the keys for the options set for the select box in the
58 * admin/pages/network.php file.}}
59 *
60 * @var array
61 */
62 public static $allowed_access_options = [
63 'admin',
64 'superadmin',
65 ];
66
67 /**
68 * Get the singleton instance of this class.
69 *
70 * @return object
71 */
72 public static function get_instance() {
73 if ( ! ( self::$instance instanceof self ) ) {
74 self::$instance = new self();
75 }
76
77 return self::$instance;
78 }
79
80 /**
81 * Only run parent constructor in multisite context.
82 */
83 public function __construct() {
84 $allow_prefix = self::ALLOW_KEY_PREFIX;
85 $this->defaults = [
86 'access' => 'admin',
87 'defaultblog' => '', // Numeric blog ID or empty.
88 "{$allow_prefix}disableadvanced_meta" => true,
89 "{$allow_prefix}ryte_indexability" => true,
90 "{$allow_prefix}content_analysis_active" => true,
91 "{$allow_prefix}keyword_analysis_active" => true,
92 "{$allow_prefix}enable_admin_bar_menu" => true,
93 "{$allow_prefix}enable_cornerstone_content" => true,
94 "{$allow_prefix}enable_xml_sitemap" => true,
95 "{$allow_prefix}enable_text_link_counter" => true,
96 "{$allow_prefix}enable_headless_rest_endpoints" => true,
97 "{$allow_prefix}enable_metabox_insights" => true,
98 "{$allow_prefix}enable_link_suggestions" => true,
99 "{$allow_prefix}tracking" => true,
100 "{$allow_prefix}enable_enhanced_slack_sharing" => true,
101 "{$allow_prefix}semrush_integration_active" => true,
102 "{$allow_prefix}zapier_integration_active" => true,
103 "{$allow_prefix}wincher_integration_active" => true,
104 "{$allow_prefix}wordproof_integration_active" => false,
105 ];
106
107 if ( is_multisite() ) {
108 parent::__construct();
109
110 add_filter( 'admin_title', [ 'Yoast_Input_Validation', 'add_yoast_admin_document_title_errors' ] );
111 }
112 }
113
114 /**
115 * Add filters to make sure that the option default is returned if the option is not set
116 *
117 * @return void
118 */
119 public function add_default_filters() {
120 // Don't change, needs to check for false as could return prio 0 which would evaluate to false.
121 if ( has_filter( 'default_site_option_' . $this->option_name, [ $this, 'get_defaults' ] ) === false ) {
122 add_filter( 'default_site_option_' . $this->option_name, [ $this, 'get_defaults' ] );
123 }
124 }
125
126 /**
127 * Remove the default filters.
128 * Called from the validate() method to prevent failure to add new options.
129 *
130 * @return void
131 */
132 public function remove_default_filters() {
133 remove_filter( 'default_site_option_' . $this->option_name, [ $this, 'get_defaults' ] );
134 }
135
136 /**
137 * Add filters to make sure that the option is merged with its defaults before being returned.
138 *
139 * @return void
140 */
141 public function add_option_filters() {
142 // Don't change, needs to check for false as could return prio 0 which would evaluate to false.
143 if ( has_filter( 'site_option_' . $this->option_name, [ $this, 'get_option' ] ) === false ) {
144 add_filter( 'site_option_' . $this->option_name, [ $this, 'get_option' ] );
145 }
146 }
147
148 /**
149 * Remove the option filters.
150 * Called from the clean_up methods to make sure we retrieve the original old option.
151 *
152 * @return void
153 */
154 public function remove_option_filters() {
155 remove_filter( 'site_option_' . $this->option_name, [ $this, 'get_option' ] );
156 }
157
158 /* *********** METHODS influencing add_uption(), update_option() and saving from admin pages *********** */
159
160 /**
161 * Validate the option.
162 *
163 * @param array $dirty New value for the option.
164 * @param array $clean Clean value for the option, normally the defaults.
165 * @param array $old Old value of the option.
166 *
167 * @return array Validated clean value for the option to be saved to the database.
168 */
169 protected function validate_option( $dirty, $clean, $old ) {
170
171 foreach ( $clean as $key => $value ) {
172 switch ( $key ) {
173 case 'access':
174 if ( isset( $dirty[ $key ] ) && in_array( $dirty[ $key ], self::$allowed_access_options, true ) ) {
175 $clean[ $key ] = $dirty[ $key ];
176 }
177 elseif ( function_exists( 'add_settings_error' ) ) {
178 add_settings_error(
179 $this->group_name, // Slug title of the setting.
180 $key, // Suffix-ID for the error message box.
181 /* translators: %1$s expands to the option name and %2$sexpands to Yoast SEO */
182 sprintf( __( '%1$s is not a valid choice for who should be allowed access to the %2$s settings. Value reset to the default.', 'wordpress-seo' ), esc_html( sanitize_text_field( $dirty[ $key ] ) ), 'Yoast SEO' ), // The error message.
183 'error' // Message type.
184 );
185 }
186 break;
187
188
189 case 'defaultblog':
190 if ( isset( $dirty[ $key ] ) && ( $dirty[ $key ] !== '' && $dirty[ $key ] !== '-' ) ) {
191 $int = WPSEO_Utils::validate_int( $dirty[ $key ] );
192 if ( $int !== false && $int > 0 ) {
193 // Check if a valid blog number has been received.
194 $exists = get_blog_details( $int, false );
195 if ( $exists && $exists->deleted === '0' ) {
196 $clean[ $key ] = $int;
197 }
198 elseif ( function_exists( 'add_settings_error' ) ) {
199 add_settings_error(
200 $this->group_name, // Slug title of the setting.
201 $key, // Suffix-ID for the error message box.
202 esc_html__( 'The default blog setting must be the numeric blog id of the blog you want to use as default.', 'wordpress-seo' )
203 . '<br>'
204 . sprintf(
205 /* translators: %s is the ID number of a blog. */
206 esc_html__( 'This must be an existing blog. Blog %s does not exist or has been marked as deleted.', 'wordpress-seo' ),
207 '<strong>' . esc_html( sanitize_text_field( $dirty[ $key ] ) ) . '</strong>'
208 ), // The error message.
209 'error' // Message type.
210 );
211 }
212 unset( $exists );
213 }
214 elseif ( function_exists( 'add_settings_error' ) ) {
215 add_settings_error(
216 $this->group_name, // Slug title of the setting.
217 $key, // Suffix-ID for the error message box.
218 esc_html__( 'The default blog setting must be the numeric blog id of the blog you want to use as default.', 'wordpress-seo' ) . '<br>' . esc_html__( 'No numeric value was received.', 'wordpress-seo' ), // The error message.
219 'error' // Message type.
220 );
221 }
222 unset( $int );
223 }
224 break;
225
226 default:
227 $clean[ $key ] = ( isset( $dirty[ $key ] ) ? WPSEO_Utils::validate_bool( $dirty[ $key ] ) : false );
228 break;
229 }
230 }
231
232 return $clean;
233 }
234 }
235