Skip to content

Commit 0da399e

Browse files
author
Andy
authored
Merge pull request #13404 from Yoast/552-add-error-descriptions-to-url-fields
Add error descriptions to the validated URL fields
2 parents fa0b50b + e746d27 commit 0da399e

File tree

3 files changed

+57
-22
lines changed

3 files changed

+57
-22
lines changed

admin/class-yoast-input-validation.php

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,8 @@ public static function is_yoast_option_group_name( $group_name ) {
4040
* @return string $admin_title The modified or original admin title.
4141
*/
4242
public static function add_yoast_admin_document_title_errors( $admin_title ) {
43-
$errors = get_settings_errors();
44-
$error_count = 0;
43+
$errors = get_settings_errors();
44+
$error_count = 0;
4545

4646
foreach ( $errors as $error ) {
4747
// For now, filter the admin title only in the Yoast SEO settings pages.
@@ -106,6 +106,11 @@ public static function set_error_descriptions( $descriptions = array() ) {
106106
esc_html__( 'Baidu verification codes can only contain letters, numbers, hyphens, and underscores. %s', 'wordpress-seo' ),
107107
self::get_dirty_value_message( 'baiduverify' )
108108
),
109+
'facebook_site' => sprintf(
110+
/* translators: %s: additional message with the submitted invalid value */
111+
esc_html__( 'Please check the format of the Facebook Page URL you entered. %s', 'wordpress-seo' ),
112+
self::get_dirty_value_message( 'facebook_site' )
113+
),
109114
'fbadminapp' => sprintf(
110115
/* translators: %s: additional message with the submitted invalid value */
111116
esc_html__( 'The Facebook App ID you entered doesn\'t exist. %s', 'wordpress-seo' ),
@@ -116,11 +121,31 @@ public static function set_error_descriptions( $descriptions = array() ) {
116121
esc_html__( 'Google verification codes can only contain letters, numbers, hyphens, and underscores. %s', 'wordpress-seo' ),
117122
self::get_dirty_value_message( 'googleverify' )
118123
),
124+
'instagram_url' => sprintf(
125+
/* translators: %s: additional message with the submitted invalid value */
126+
esc_html__( 'Please check the format of the Instagram URL you entered. %s', 'wordpress-seo' ),
127+
self::get_dirty_value_message( 'instagram_url' )
128+
),
129+
'linkedin_url' => sprintf(
130+
/* translators: %s: additional message with the submitted invalid value */
131+
esc_html__( 'Please check the format of the Linkedin URL you entered. %s', 'wordpress-seo' ),
132+
self::get_dirty_value_message( 'linkedin_url' )
133+
),
119134
'msverify' => sprintf(
120135
/* translators: %s: additional message with the submitted invalid value */
121136
esc_html__( 'Bing confirmation codes can only contain letters from A to F, numbers, hyphens, and underscores. %s', 'wordpress-seo' ),
122137
self::get_dirty_value_message( 'msverify' )
123138
),
139+
'myspace_url' => sprintf(
140+
/* translators: %s: additional message with the submitted invalid value */
141+
esc_html__( 'Please check the format of the MySpace URL you entered. %s', 'wordpress-seo' ),
142+
self::get_dirty_value_message( 'myspace_url' )
143+
),
144+
'pinterest_url' => sprintf(
145+
/* translators: %s: additional message with the submitted invalid value */
146+
esc_html__( 'Please check the format of the Pinterest URL you entered. %s', 'wordpress-seo' ),
147+
self::get_dirty_value_message( 'pinterest_url' )
148+
),
124149
'pinterestverify' => sprintf(
125150
/* translators: %s: additional message with the submitted invalid value */
126151
esc_html__( 'Pinterest confirmation codes can only contain letters from A to F, numbers, hyphens, and underscores. %s', 'wordpress-seo' ),
@@ -131,11 +156,21 @@ public static function set_error_descriptions( $descriptions = array() ) {
131156
esc_html__( 'Twitter usernames can only contain letters, numbers, and underscores. %s', 'wordpress-seo' ),
132157
self::get_dirty_value_message( 'twitter_site' )
133158
),
159+
'wikipedia_url' => sprintf(
160+
/* translators: %s: additional message with the submitted invalid value */
161+
esc_html__( 'Please check the format of the Wikipedia URL you entered. %s', 'wordpress-seo' ),
162+
self::get_dirty_value_message( 'wikipedia_url' )
163+
),
134164
'yandexverify' => sprintf(
135165
/* translators: %s: additional message with the submitted invalid value */
136166
esc_html__( 'Yandex confirmation codes can only contain letters from A to F, numbers, hyphens, and underscores. %s', 'wordpress-seo' ),
137167
self::get_dirty_value_message( 'yandexverify' )
138168
),
169+
'youtube_url' => sprintf(
170+
/* translators: %s: additional message with the submitted invalid value */
171+
esc_html__( 'Please check the format of the Youtube URL you entered. %s', 'wordpress-seo' ),
172+
self::get_dirty_value_message( 'youtube_url' )
173+
),
139174
);
140175

141176
$descriptions = wp_parse_args( $descriptions, $defaults );

inc/options/class-wpseo-option.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -335,7 +335,7 @@ public function validate_url( $key, $dirty, $old, &$clean ) {
335335
if ( isset( $dirty[ $key ] ) && $dirty[ $key ] !== '' ) {
336336

337337
$submitted_url = trim( htmlspecialchars( $dirty[ $key ] ) );
338-
$validated_url = filter_var( $submitted_url, FILTER_VALIDATE_URL );
338+
$validated_url = filter_var( WPSEO_Utils::sanitize_url( $submitted_url ), FILTER_VALIDATE_URL );
339339

340340
if ( $validated_url === false ) {
341341
if ( function_exists( 'add_settings_error' ) ) {
@@ -362,6 +362,8 @@ public function validate_url( $key, $dirty, $old, &$clean ) {
362362
}
363363
}
364364

365+
Yoast_Input_Validation::add_dirty_value_to_settings_errors( $key, $submitted_url );
366+
365367
return;
366368
}
367369

tests/inc/options/option-social-test.php

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use Yoast\WP\Free\Tests\Doubles\Inc\Options\Option_Social_Double;
88
use Yoast\WP\Free\Tests\TestCase;
99
use WPSEO_Utils;
10+
use Yoast_Input_Validation;
1011

1112
/**
1213
* Unit Test Class.
@@ -49,16 +50,31 @@ public function test_validate_option_with_valid_data( $expected, $dirty, $clean,
4950
* @covers WPSEO_Option_Social::validate_option
5051
*/
5152
public function test_validate_option_with_invalid_data( $expected, $dirty, $clean, $old, $slug_name ) {
53+
$message = "<strong>{$dirty[ $slug_name ]}</strong> does not seem to be a valid url. Please correct.";
54+
5255
Monkey\Functions\expect( 'add_settings_error' )
5356
->once()
54-
->with( 'yoast_wpseo_social_options', $slug_name, "<strong>{$dirty[ $slug_name ]}</strong> does not seem to be a valid url. Please correct.", 'notice-error' );
57+
->with( 'yoast_wpseo_social_options', $slug_name, $message, 'notice-error' );
5558

5659
$instance = new Option_Social_Double();
5760

61+
$GLOBALS['wp_settings_errors'] = [
62+
[
63+
'setting' => 'yoast_wpseo_social_options',
64+
'code' => $slug_name,
65+
'message' => $message,
66+
'type' => 'notice-error',
67+
],
68+
];
69+
70+
Yoast_Input_Validation::add_dirty_value_to_settings_errors( $slug_name, 'Invalid submitted value' );
71+
5872
$this->assertEquals(
5973
$expected,
6074
$instance->validate_option( $dirty, $clean, $old )
6175
);
76+
77+
unset( $GLOBALS['wp_settings_errors'] );
6278
}
6379

6480
/**
@@ -143,24 +159,6 @@ public function validate_option_invalid_data_provider() {
143159
);
144160
}
145161

146-
/**
147-
* Tests that submitting an option with an invalid URL adds a WordPress settings error notice.
148-
*
149-
* @covers WPSEO_Option_Social::validate_url
150-
*/
151-
public function test_validate_url_adds_settings_error() {
152-
$instance = new Option_Social_Double();
153-
154-
Monkey\Functions\expect( 'add_settings_error' )
155-
->once()
156-
->with( 'yoast_wpseo_social_options', 'instagram_url', '<strong>invalidurl</strong> does not seem to be a valid url. Please correct.', 'notice-error' );
157-
158-
$clean = array( 'instagram_url' => '' );
159-
$dirty = array( 'instagram_url' => 'invalidurl' );
160-
161-
$instance->validate_url( 'instagram_url', $dirty, '', $clean );
162-
}
163-
164162
/**
165163
* Tests the Facebook App ID validation method with an invalid ID.
166164
*

0 commit comments

Comments
 (0)