PluginProbe
Yoast SEO – Advanced SEO with real-time guidance and built-in AI / 28.3
Yoast SEO – Advanced SEO with real-time guidance and built-in AI v28.3
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 19.1 All 128 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 28.3, at admin/class-yoast-input-validation.php

253 lines 7.2 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<string, string>
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 * @deprecated 23.3
100 * @codeCoverageIgnore
101 *
102 * @param array<string, string> $descriptions An associative array of error descriptions.
103 * For each entry, the key must be the setting variable.
104 *
105 * @return void
106 */
107 public static function set_error_descriptions( $descriptions = [] ) { // @phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable, Generic.CodeAnalysis.UnusedFunctionParameter.Found -- Needed for BC.
108 _deprecated_function( __METHOD__, 'Yoast SEO 23.3' );
109 }
110
111 /**
112 * Gets all the error descriptions.
113 *
114 * @since 12.1
115 * @deprecated 23.3
116 * @codeCoverageIgnore
117 *
118 * @return array<string, string> An associative array of error descriptions.
119 */
120 public static function get_error_descriptions() {
121 _deprecated_function( __METHOD__, 'Yoast SEO 23.3' );
122 return [];
123 }
124
125 /**
126 * Gets a specific error description.
127 *
128 * @since 12.1
129 *
130 * @param string $error_code Code of the error set via `add_settings_error()`, normally the variable name.
131 *
132 * @return string|null The error description.
133 */
134 public static function get_error_description( $error_code ) {
135 if ( ! isset( self::$error_descriptions[ $error_code ] ) ) {
136 return null;
137 }
138
139 return self::$error_descriptions[ $error_code ];
140 }
141
142 /**
143 * Gets the aria-invalid HTML attribute based on the submitted invalid value.
144 *
145 * @since 12.1
146 *
147 * @param string $error_code Code of the error set via `add_settings_error()`, normally the variable name.
148 *
149 * @return string The aria-invalid HTML attribute or empty string.
150 */
151 public static function get_the_aria_invalid_attribute( $error_code ) {
152 if ( self::yoast_form_control_has_error( $error_code ) ) {
153 return ' aria-invalid="true"';
154 }
155
156 return '';
157 }
158
159 /**
160 * Gets the aria-describedby HTML attribute based on the submitted invalid value.
161 *
162 * @since 12.1
163 *
164 * @param string $error_code Code of the error set via `add_settings_error()`, normally the variable name.
165 *
166 * @return string The aria-describedby HTML attribute or empty string.
167 */
168 public static function get_the_aria_describedby_attribute( $error_code ) {
169 if ( self::yoast_form_control_has_error( $error_code ) && self::get_error_description( $error_code ) ) {
170 return ' aria-describedby="' . esc_attr( $error_code ) . '-error-description"';
171 }
172
173 return '';
174 }
175
176 /**
177 * Gets the error description wrapped in a HTML paragraph.
178 *
179 * @since 12.1
180 *
181 * @param string $error_code Code of the error set via `add_settings_error()`, normally the variable name.
182 *
183 * @return string The error description HTML or empty string.
184 */
185 public static function get_the_error_description( $error_code ) {
186 $error_description = self::get_error_description( $error_code );
187
188 if ( self::yoast_form_control_has_error( $error_code ) && $error_description ) {
189 return '<p id="' . esc_attr( $error_code ) . '-error-description" class="yoast-input-validation__error-description">' . $error_description . '</p>';
190 }
191
192 return '';
193 }
194
195 /**
196 * Adds the submitted invalid value to the WordPress `$wp_settings_errors` global.
197 *
198 * @since 12.1
199 *
200 * @param string $error_code Code of the error set via `add_settings_error()`, normally the variable name.
201 * @param string $dirty_value The submitted invalid value.
202 *
203 * @return void
204 */
205 public static function add_dirty_value_to_settings_errors( $error_code, $dirty_value ) {
206 global $wp_settings_errors;
207
208 if ( ! is_array( $wp_settings_errors ) ) {
209 return;
210 }
211
212 foreach ( $wp_settings_errors as $index => $error ) {
213 if ( $error['code'] === $error_code ) {
214 // phpcs:ignore WordPress.WP.GlobalVariablesOverride -- This is a deliberate action.
215 $wp_settings_errors[ $index ]['yoast_dirty_value'] = $dirty_value;
216 }
217 }
218 }
219
220 /**
221 * Gets an invalid submitted value.
222 *
223 * @since 12.1
224 * @deprecated 23.3
225 * @codeCoverageIgnore
226 *
227 * @param string $error_code Code of the error set via `add_settings_error()`, normally the variable name.
228 *
229 * @return string The submitted invalid input field value.
230 */
231 public static function get_dirty_value( $error_code ) { // @phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable, Generic.CodeAnalysis.UnusedFunctionParameter.Found -- Needed for BC.
232 _deprecated_function( __METHOD__, 'Yoast SEO 23.3' );
233 return '';
234 }
235
236 /**
237 * Gets a specific invalid value message.
238 *
239 * @since 12.1
240 * @deprecated 23.3
241 * @codeCoverageIgnore
242 *
243 * @param string $error_code Code of the error set via `add_settings_error()`, normally the variable name.
244 *
245 * @return string The error invalid value message or empty string.
246 */
247 public static function get_dirty_value_message( $error_code ) { // @phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable, Generic.CodeAnalysis.UnusedFunctionParameter.Found -- Needed for BC.
248 _deprecated_function( __METHOD__, 'Yoast SEO 23.3' );
249
250 return '';
251 }
252 }
253