PluginProbe
Yoast SEO – Advanced SEO with real-time guidance and built-in AI / 18.1
Yoast SEO – Advanced SEO with real-time guidance and built-in AI v18.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 / admin / class-yoast-input-validation.php

class-yoast-input-validation.php in Yoast SEO – Advanced SEO with real-time guidance and built-in AI 18.1, at admin/class-yoast-input-validation.php

327 lines 10.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\Admin
6 */
7
8 /**
9 * Implements server-side user input validation.
10 *
11 * @since 12.0
12 */
13 class Yoast_Input_Validation {
14
15 /**
16 * The error descriptions.
17 *
18 * @since 12.1
19 *
20 * @var array
21 */
22 private static $error_descriptions = [];
23
24 /**
25 * Check whether an option group is a Yoast SEO setting.
26 *
27 * The normal pattern is 'yoast' . $option_name . 'options'.
28 *
29 * @since 12.0
30 *
31 * @param string $group_name The option group name.
32 *
33 * @return bool Whether or not it's an Yoast SEO option group.
34 */
35 public static function is_yoast_option_group_name( $group_name ) {
36 return ( strpos( $group_name, 'yoast' ) !== false );
37 }
38
39 /**
40 * Adds an error message to the document title when submitting a settings
41 * form and errors are returned.
42 *
43 * Uses the WordPress `admin_title` filter in the WPSEO_Option subclasses.
44 *
45 * @since 12.0
46 *
47 * @param string $admin_title The page title, with extra context added.
48 *
49 * @return string The modified or original admin title.
50 */
51 public static function add_yoast_admin_document_title_errors( $admin_title ) {
52 $errors = get_settings_errors();
53 $error_count = 0;
54
55 foreach ( $errors as $error ) {
56 // For now, filter the admin title only in the Yoast SEO settings pages.
57 if ( self::is_yoast_option_group_name( $error['setting'] ) && $error['code'] !== 'settings_updated' ) {
58 ++$error_count;
59 }
60 }
61
62 if ( $error_count > 0 ) {
63 return sprintf(
64 /* translators: %1$s: amount of errors, %2$s: the admin page title */
65 _n( 'The form contains %1$s error. %2$s', 'The form contains %1$s errors. %2$s', $error_count, 'wordpress-seo' ),
66 number_format_i18n( $error_count ),
67 $admin_title
68 );
69 }
70
71 return $admin_title;
72 }
73
74 /**
75 * Checks whether a specific form input field was submitted with an invalid value.
76 *
77 * @since 12.1
78 *
79 * @param string $error_code Must be the same slug-name used for the field variable and for `add_settings_error()`.
80 *
81 * @return bool Whether or not the submitted input field contained an invalid value.
82 */
83 public static function yoast_form_control_has_error( $error_code ) {
84 $errors = get_settings_errors();
85
86 foreach ( $errors as $error ) {
87 if ( $error['code'] === $error_code ) {
88 return true;
89 }
90 }
91
92 return false;
93 }
94
95 /**
96 * Sets the error descriptions.
97 *
98 * @since 12.1
99 *
100 * @param array $descriptions An associative array of error descriptions.
101 * For each entry, the key must be the setting variable.
102 */
103 public static function set_error_descriptions( $descriptions = [] ) {
104 $defaults = [
105 'baiduverify' => sprintf(
106 /* translators: %s: additional message with the submitted invalid value */
107 esc_html__( 'Baidu verification codes can only contain letters, numbers, hyphens, and underscores. %s', 'wordpress-seo' ),
108 self::get_dirty_value_message( 'baiduverify' )
109 ),
110 'facebook_site' => sprintf(
111 /* translators: %s: additional message with the submitted invalid value */
112 esc_html__( 'Please check the format of the Facebook Page URL you entered. %s', 'wordpress-seo' ),
113 self::get_dirty_value_message( 'facebook_site' )
114 ),
115 'googleverify' => sprintf(
116 /* translators: %s: additional message with the submitted invalid value */
117 esc_html__( 'Google verification codes can only contain letters, numbers, hyphens, and underscores. %s', 'wordpress-seo' ),
118 self::get_dirty_value_message( 'googleverify' )
119 ),
120 'instagram_url' => sprintf(
121 /* translators: %s: additional message with the submitted invalid value */
122 esc_html__( 'Please check the format of the Instagram URL you entered. %s', 'wordpress-seo' ),
123 self::get_dirty_value_message( 'instagram_url' )
124 ),
125 'linkedin_url' => sprintf(
126 /* translators: %s: additional message with the submitted invalid value */
127 esc_html__( 'Please check the format of the Linkedin URL you entered. %s', 'wordpress-seo' ),
128 self::get_dirty_value_message( 'linkedin_url' )
129 ),
130 'msverify' => sprintf(
131 /* translators: %s: additional message with the submitted invalid value */
132 esc_html__( 'Bing confirmation codes can only contain letters from A to F, numbers, hyphens, and underscores. %s', 'wordpress-seo' ),
133 self::get_dirty_value_message( 'msverify' )
134 ),
135 'myspace_url' => sprintf(
136 /* translators: %s: additional message with the submitted invalid value */
137 esc_html__( 'Please check the format of the MySpace URL you entered. %s', 'wordpress-seo' ),
138 self::get_dirty_value_message( 'myspace_url' )
139 ),
140 'pinterest_url' => sprintf(
141 /* translators: %s: additional message with the submitted invalid value */
142 esc_html__( 'Please check the format of the Pinterest URL you entered. %s', 'wordpress-seo' ),
143 self::get_dirty_value_message( 'pinterest_url' )
144 ),
145 'pinterestverify' => sprintf(
146 /* translators: %s: additional message with the submitted invalid value */
147 esc_html__( 'Pinterest confirmation codes can only contain letters from A to F, numbers, hyphens, and underscores. %s', 'wordpress-seo' ),
148 self::get_dirty_value_message( 'pinterestverify' )
149 ),
150 'twitter_site' => sprintf(
151 /* translators: %s: additional message with the submitted invalid value */
152 esc_html__( 'Twitter usernames can only contain letters, numbers, and underscores. %s', 'wordpress-seo' ),
153 self::get_dirty_value_message( 'twitter_site' )
154 ),
155 'wikipedia_url' => sprintf(
156 /* translators: %s: additional message with the submitted invalid value */
157 esc_html__( 'Please check the format of the Wikipedia URL you entered. %s', 'wordpress-seo' ),
158 self::get_dirty_value_message( 'wikipedia_url' )
159 ),
160 'yandexverify' => sprintf(
161 /* translators: %s: additional message with the submitted invalid value */
162 esc_html__( 'Yandex confirmation codes can only contain letters from A to F, numbers, hyphens, and underscores. %s', 'wordpress-seo' ),
163 self::get_dirty_value_message( 'yandexverify' )
164 ),
165 'youtube_url' => sprintf(
166 /* translators: %s: additional message with the submitted invalid value */
167 esc_html__( 'Please check the format of the Youtube URL you entered. %s', 'wordpress-seo' ),
168 self::get_dirty_value_message( 'youtube_url' )
169 ),
170 ];
171
172 $descriptions = wp_parse_args( $descriptions, $defaults );
173
174 self::$error_descriptions = $descriptions;
175 }
176
177 /**
178 * Gets all the error descriptions.
179 *
180 * @since 12.1
181 *
182 * @return array An associative array of error descriptions.
183 */
184 public static function get_error_descriptions() {
185 return self::$error_descriptions;
186 }
187
188 /**
189 * Gets a specific error description.
190 *
191 * @since 12.1
192 *
193 * @param string $error_code Code of the error set via `add_settings_error()`, normally the variable name.
194 *
195 * @return string|null The error description.
196 */
197 public static function get_error_description( $error_code ) {
198 if ( ! isset( self::$error_descriptions[ $error_code ] ) ) {
199 return null;
200 }
201
202 return self::$error_descriptions[ $error_code ];
203 }
204
205 /**
206 * Gets the aria-invalid HTML attribute based on the submitted invalid value.
207 *
208 * @since 12.1
209 *
210 * @param string $error_code Code of the error set via `add_settings_error()`, normally the variable name.
211 *
212 * @return string The aria-invalid HTML attribute or empty string.
213 */
214 public static function get_the_aria_invalid_attribute( $error_code ) {
215 if ( self::yoast_form_control_has_error( $error_code ) ) {
216 return ' aria-invalid="true"';
217 }
218
219 return '';
220 }
221
222 /**
223 * Gets the aria-describedby HTML attribute based on the submitted invalid value.
224 *
225 * @since 12.1
226 *
227 * @param string $error_code Code of the error set via `add_settings_error()`, normally the variable name.
228 *
229 * @return string The aria-describedby HTML attribute or empty string.
230 */
231 public static function get_the_aria_describedby_attribute( $error_code ) {
232 if ( self::yoast_form_control_has_error( $error_code ) && self::get_error_description( $error_code ) ) {
233 return ' aria-describedby="' . esc_attr( $error_code ) . '-error-description"';
234 }
235
236 return '';
237 }
238
239 /**
240 * Gets the error description wrapped in a HTML paragraph.
241 *
242 * @since 12.1
243 *
244 * @param string $error_code Code of the error set via `add_settings_error()`, normally the variable name.
245 *
246 * @return string The error description HTML or empty string.
247 */
248 public static function get_the_error_description( $error_code ) {
249 $error_description = self::get_error_description( $error_code );
250
251 if ( self::yoast_form_control_has_error( $error_code ) && $error_description ) {
252 return '<p id="' . esc_attr( $error_code ) . '-error-description" class="yoast-input-validation__error-description">' . $error_description . '</p>';
253 }
254
255 return '';
256 }
257
258 /**
259 * Adds the submitted invalid value to the WordPress `$wp_settings_errors` global.
260 *
261 * @since 12.1
262 *
263 * @param string $error_code Code of the error set via `add_settings_error()`, normally the variable name.
264 * @param string $dirty_value The submitted invalid value.
265 *
266 * @return void
267 */
268 public static function add_dirty_value_to_settings_errors( $error_code, $dirty_value ) {
269 global $wp_settings_errors;
270
271 if ( ! is_array( $wp_settings_errors ) ) {
272 return;
273 }
274
275 foreach ( $wp_settings_errors as $index => $error ) {
276 if ( $error['code'] === $error_code ) {
277 // phpcs:ignore WordPress.WP.GlobalVariablesOverride -- This is a deliberate action.
278 $wp_settings_errors[ $index ]['yoast_dirty_value'] = $dirty_value;
279 }
280 }
281 }
282
283 /**
284 * Gets an invalid submitted value.
285 *
286 * @since 12.1
287 *
288 * @param string $error_code Code of the error set via `add_settings_error()`, normally the variable name.
289 *
290 * @return string The submitted invalid input field value.
291 */
292 public static function get_dirty_value( $error_code ) {
293 $errors = get_settings_errors();
294
295 foreach ( $errors as $error ) {
296 if ( $error['code'] === $error_code && isset( $error['yoast_dirty_value'] ) ) {
297 return $error['yoast_dirty_value'];
298 }
299 }
300
301 return '';
302 }
303
304 /**
305 * Gets a specific invalid value message.
306 *
307 * @since 12.1
308 *
309 * @param string $error_code Code of the error set via `add_settings_error()`, normally the variable name.
310 *
311 * @return string The error invalid value message or empty string.
312 */
313 public static function get_dirty_value_message( $error_code ) {
314 $dirty_value = self::get_dirty_value( $error_code );
315
316 if ( $dirty_value ) {
317 return sprintf(
318 /* translators: %s: form value as submitted. */
319 esc_html__( 'The submitted value was: %s', 'wordpress-seo' ),
320 esc_html( $dirty_value )
321 );
322 }
323
324 return '';
325 }
326 }
327