Skip to content

Commit f79412a

Browse files
committed
Fix check message
1 parent 2537930 commit f79412a

File tree

2 files changed

+69
-46
lines changed

2 files changed

+69
-46
lines changed

includes/Checker/Checks/Plugin_Repo/Plugin_Header_Fields_Check.php

Lines changed: 38 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -137,22 +137,25 @@ public function run( Check_Result $result ) {
137137
'https://developer.wordpress.org/plugins/plugin-basics/header-requirements/#header-fields',
138138
7
139139
);
140-
} elseif ( $this->has_discouraged_domain( $plugin_header['PluginURI'] ) ) {
141-
$this->add_result_error_for_file(
142-
$result,
143-
sprintf(
144-
/* translators: 1: plugin header field, 2: domain */
145-
__( 'The "%1$s" header in the plugin file is not valid. Discouraged domain "%2$s" found. This is the homepage of the plugin, which should be a unique URL, preferably on your own website. ', 'plugin-check' ),
146-
esc_html( $labels['PluginURI'] ),
147-
esc_html( $matches[1] ),
148-
),
149-
'plugin_header_invalid_plugin_uri_domain',
150-
$plugin_main_file,
151-
0,
152-
0,
153-
'https://developer.wordpress.org/plugins/plugin-basics/header-requirements/#header-fields',
154-
7
155-
);
140+
} else {
141+
$matched_domain = $this->find_discouraged_domain( $plugin_header['PluginURI'] );
142+
if ( $matched_domain ) {
143+
$this->add_result_error_for_file(
144+
$result,
145+
sprintf(
146+
/* translators: 1: plugin header field, 2: domain */
147+
__( 'The "%1$s" header in the plugin file is not valid. Discouraged domain "%2$s" found. This is the homepage of the plugin, which should be a unique URL, preferably on your own website.', 'plugin-check' ),
148+
esc_html( $labels['PluginURI'] ),
149+
esc_html( $matched_domain )
150+
),
151+
'plugin_header_invalid_plugin_uri_domain',
152+
$plugin_main_file,
153+
0,
154+
0,
155+
'https://developer.wordpress.org/plugins/plugin-basics/header-requirements/#header-fields',
156+
7
157+
);
158+
}
156159
}
157160
}
158161

@@ -244,22 +247,25 @@ public function run( Check_Result $result ) {
244247
'https://developer.wordpress.org/plugins/plugin-basics/header-requirements/#header-fields',
245248
7
246249
);
247-
} elseif ( $this->has_discouraged_domain( $plugin_header['AuthorURI'] ) ) {
248-
$this->add_result_error_for_file(
249-
$result,
250-
sprintf(
251-
/* translators: 1: plugin header field, 2: domain */
252-
__( 'The "%1$s" header in the plugin file is not valid. Discouraged domain "%2$s" found. This is the author\'s website or profile on another website.', 'plugin-check' ),
253-
esc_html( $labels['AuthorURI'] ),
254-
esc_html( $matches[1] ),
255-
),
256-
'plugin_header_invalid_author_uri_domain',
257-
$plugin_main_file,
258-
0,
259-
0,
260-
'https://developer.wordpress.org/plugins/plugin-basics/header-requirements/#header-fields',
261-
7
262-
);
250+
} else {
251+
$matched_domain = $this->find_discouraged_domain( $plugin_header['AuthorURI'] );
252+
if ( $matched_domain ) {
253+
$this->add_result_error_for_file(
254+
$result,
255+
sprintf(
256+
/* translators: 1: plugin header field, 2: domain */
257+
__( 'The "%1$s" header in the plugin file is not valid. Discouraged domain "%2$s" found. This is the author\'s website or profile on another website.', 'plugin-check' ),
258+
esc_html( $labels['AuthorURI'] ),
259+
esc_html( $matched_domain )
260+
),
261+
'plugin_header_invalid_author_uri_domain',
262+
$plugin_main_file,
263+
0,
264+
0,
265+
'https://developer.wordpress.org/plugins/plugin-basics/header-requirements/#header-fields',
266+
7
267+
);
268+
}
263269
}
264270
}
265271

includes/Traits/URL_Utils.php

Lines changed: 31 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -38,34 +38,51 @@ protected function is_valid_url( string $url ): bool {
3838
}
3939

4040
/**
41-
* Checks if URL has discouraged domain.
41+
* Finds and returns the discouraged domain matched in the URL's host, or null if none.
4242
*
4343
* @since 1.6.0
4444
*
45-
* @param string $url URL.
46-
* @return bool true if the URL has discouraged domain, otherwise false.
45+
* @param string $url The URL to check.
46+
* @return string|null The matched discouraged domain, or null if none matched.
4747
*/
48-
protected function has_discouraged_domain( string $url ): bool {
48+
protected function find_discouraged_domain( string $url ) {
4949
$discouraged_domains = $this->get_discouraged_domains();
5050

51+
if ( empty( $discouraged_domains ) ) {
52+
return null;
53+
}
54+
5155
$parsed_url = wp_parse_url( $url );
5256

5357
if ( empty( $parsed_url['host'] ) ) {
54-
return false;
58+
return null;
5559
}
5660

5761
$host = strtolower( rtrim( $parsed_url['host'], '.' ) );
5862

59-
$escaped_domains = array_map(
60-
function ( $domain ) {
61-
return preg_quote( strtolower( rtrim( $domain, '.' ) ), '/' );
62-
},
63-
$discouraged_domains
64-
);
63+
foreach ( $discouraged_domains as $domain ) {
64+
$domain = strtolower( rtrim( $domain, '.' ) );
65+
if (
66+
$host === $domain ||
67+
( strlen( $host ) > strlen( $domain ) && substr( $host, -strlen( $domain ) - 1 ) === '.' . $domain )
68+
) {
69+
return $domain;
70+
}
71+
}
6572

66-
$pattern = '/(^|\.)(' . implode( '|', $escaped_domains ) . ')$/i';
73+
return null;
74+
}
6775

68-
return (bool) preg_match( $pattern, $host );
76+
/**
77+
* Checks if URL has discouraged domain.
78+
*
79+
* @since 1.6.0
80+
*
81+
* @param string $url The URL to check.
82+
* @return bool True if the URL has a discouraged domain, false otherwise.
83+
*/
84+
protected function has_discouraged_domain( $url ) {
85+
return null !== $this->find_discouraged_domain( $url );
6986
}
7087

7188
/**
@@ -78,8 +95,8 @@ function ( $domain ) {
7895
private function get_discouraged_domains() {
7996
$discouraged_domains = array(
8097
'example.com',
81-
'example.org',
8298
'example.net',
99+
'example.org',
83100
'yourwebsite.com',
84101
);
85102

0 commit comments

Comments
 (0)