• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

Yoast / wordpress-seo / b78c91bd80d89b034df841d284a428c954224385

03 Sep 2024 07:50AM UTC coverage: 54.503% (+0.4%) from 54.072%
b78c91bd80d89b034df841d284a428c954224385

push

github

YoastBot
Bump version to 23.4 on free

7504 of 13559 branches covered (55.34%)

Branch coverage included in aggregate %.

29831 of 54942 relevant lines covered (54.3%)

41571.6 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

33.33
/admin/class-yoast-input-validation.php
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 ) {
8✔
36
                return ( strpos( $group_name, 'yoast' ) !== false );
8✔
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 ) {
8✔
52
                $errors      = get_settings_errors();
8✔
53
                $error_count = 0;
8✔
54

55
                foreach ( $errors as $error ) {
8✔
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' ) {
8✔
58
                                ++$error_count;
4✔
59
                        }
60
                }
61

62
                if ( $error_count > 0 ) {
8✔
63
                        return sprintf(
4✔
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' ),
4✔
66
                                number_format_i18n( $error_count ),
4✔
67
                                $admin_title
4✔
68
                        );
2✔
69
                }
70

71
                return $admin_title;
4✔
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<string, string> $descriptions An associative array of error descriptions.
101
         *                            For each entry, the key must be the setting variable.
102
         *
103
         * @return void
104
         *
105
         * @deprecated 23.3
106
         * @codeCoverageIgnore
107
         */
108
        public static function set_error_descriptions( $descriptions = [] ) { // @phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable, Generic.CodeAnalysis.UnusedFunctionParameter.Found -- Needed for BC.
109
                _deprecated_function( __METHOD__, 'Yoast SEO 23.3' );
110
        }
111

112
        /**
113
         * Gets all the error descriptions.
114
         *
115
         * @since 12.1
116
         *
117
         * @deprecated 23.3
118
         * @codeCoverageIgnore
119
         *
120
         * @return array<string, string> An associative array of error descriptions.
121
         */
122
        public static function get_error_descriptions() {
123
                _deprecated_function( __METHOD__, 'Yoast SEO 23.3' );
124
                return [];
125
        }
126

127
        /**
128
         * Gets a specific error description.
129
         *
130
         * @since 12.1
131
         *
132
         * @param string $error_code Code of the error set via `add_settings_error()`, normally the variable name.
133
         *
134
         * @return string|null The error description.
135
         */
136
        public static function get_error_description( $error_code ) {
×
137
                if ( ! isset( self::$error_descriptions[ $error_code ] ) ) {
×
138
                        return null;
×
139
                }
140

141
                return self::$error_descriptions[ $error_code ];
×
142
        }
143

144
        /**
145
         * Gets the aria-invalid HTML attribute based on the submitted invalid value.
146
         *
147
         * @since 12.1
148
         *
149
         * @param string $error_code Code of the error set via `add_settings_error()`, normally the variable name.
150
         *
151
         * @return string The aria-invalid HTML attribute or empty string.
152
         */
153
        public static function get_the_aria_invalid_attribute( $error_code ) {
×
154
                if ( self::yoast_form_control_has_error( $error_code ) ) {
×
155
                        return ' aria-invalid="true"';
×
156
                }
157

158
                return '';
×
159
        }
160

161
        /**
162
         * Gets the aria-describedby HTML attribute based on the submitted invalid value.
163
         *
164
         * @since 12.1
165
         *
166
         * @param string $error_code Code of the error set via `add_settings_error()`, normally the variable name.
167
         *
168
         * @return string The aria-describedby HTML attribute or empty string.
169
         */
170
        public static function get_the_aria_describedby_attribute( $error_code ) {
×
171
                if ( self::yoast_form_control_has_error( $error_code ) && self::get_error_description( $error_code ) ) {
×
172
                        return ' aria-describedby="' . esc_attr( $error_code ) . '-error-description"';
×
173
                }
174

175
                return '';
×
176
        }
177

178
        /**
179
         * Gets the error description wrapped in a HTML paragraph.
180
         *
181
         * @since 12.1
182
         *
183
         * @param string $error_code Code of the error set via `add_settings_error()`, normally the variable name.
184
         *
185
         * @return string The error description HTML or empty string.
186
         */
187
        public static function get_the_error_description( $error_code ) {
×
188
                $error_description = self::get_error_description( $error_code );
×
189

190
                if ( self::yoast_form_control_has_error( $error_code ) && $error_description ) {
×
191
                        return '<p id="' . esc_attr( $error_code ) . '-error-description" class="yoast-input-validation__error-description">' . $error_description . '</p>';
×
192
                }
193

194
                return '';
×
195
        }
196

197
        /**
198
         * Adds the submitted invalid value to the WordPress `$wp_settings_errors` global.
199
         *
200
         * @since 12.1
201
         *
202
         * @param string $error_code  Code of the error set via `add_settings_error()`, normally the variable name.
203
         * @param string $dirty_value The submitted invalid value.
204
         *
205
         * @return void
206
         */
207
        public static function add_dirty_value_to_settings_errors( $error_code, $dirty_value ) {
×
208
                global $wp_settings_errors;
×
209

210
                if ( ! is_array( $wp_settings_errors ) ) {
×
211
                        return;
×
212
                }
213

214
                foreach ( $wp_settings_errors as $index => $error ) {
×
215
                        if ( $error['code'] === $error_code ) {
×
216
                                // phpcs:ignore WordPress.WP.GlobalVariablesOverride -- This is a deliberate action.
217
                                $wp_settings_errors[ $index ]['yoast_dirty_value'] = $dirty_value;
×
218
                        }
219
                }
220
        }
221

222
        /**
223
         * Gets an invalid submitted 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 submitted invalid input field value.
230
         *
231
         * @deprecated 23.3
232
         * @codeCoverageIgnore
233
         */
234
        public static function get_dirty_value( $error_code ) {  // @phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable, Generic.CodeAnalysis.UnusedFunctionParameter.Found -- Needed for BC.
235
                _deprecated_function( __METHOD__, 'Yoast SEO 23.3' );
236
                return '';
237
        }
238

239
        /**
240
         * Gets a specific invalid value message.
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 invalid value message or empty string.
247
         *
248
         * @deprecated 23.3
249
         * @codeCoverageIgnore
250
         */
251
        public static function get_dirty_value_message( $error_code ) { // @phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable, Generic.CodeAnalysis.UnusedFunctionParameter.Found -- Needed for BC.
252
                _deprecated_function( __METHOD__, 'Yoast SEO 23.3' );
253

254
                return '';
255
        }
256
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc