Skip to content

Commit 8e38e4b

Browse files
Merge pull request #955 from WordPress/953-unsupported-textdomain-characters
Disallow unsupported characters for `Text Domain` plugin header
2 parents 5741c1d + 8544947 commit 8e38e4b

File tree

3 files changed

+31
-12
lines changed

3 files changed

+31
-12
lines changed

includes/Checker/Checks/Plugin_Repo/Plugin_Header_Fields_Check.php

Lines changed: 29 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -411,25 +411,43 @@ public function run( Check_Result $result ) {
411411

412412
if ( ! $result->plugin()->is_single_file_plugin() ) {
413413
if ( ! empty( $plugin_header['TextDomain'] ) ) {
414-
$plugin_slug = $result->plugin()->slug();
415-
416-
if ( $plugin_slug !== $plugin_header['TextDomain'] ) {
417-
$this->add_result_warning_for_file(
414+
if ( ! preg_match( '/^[a-z0-9]+(?:-[a-z0-9]+)*$/', $plugin_header['TextDomain'] ) ) {
415+
$this->add_result_error_for_file(
418416
$result,
419417
sprintf(
420-
/* translators: 1: plugin header field, 2: plugin header text domain, 3: plugin slug */
421-
__( 'The "%1$s" header in the plugin file does not match the slug. Found "%2$s", expected "%3$s".', 'plugin-check' ),
418+
/* translators: 1: plugin header field, 2: text domain */
419+
__( 'The "%1$s" header in the plugin file should only contain lowercase letters, numbers, and hyphens. Found "%2$s".', 'plugin-check' ),
422420
esc_html( $labels['TextDomain'] ),
423-
esc_html( $plugin_header['TextDomain'] ),
424-
esc_html( $plugin_slug )
421+
esc_html( $plugin_header['TextDomain'] )
425422
),
426-
'textdomain_mismatch',
423+
'textdomain_invalid_format',
427424
$plugin_main_file,
428425
0,
429426
0,
430-
'https://developer.wordpress.org/plugins/internationalization/how-to-internationalize-your-plugin/',
431-
6
427+
'https://developer.wordpress.org/plugins/internationalization/how-to-internationalize-your-plugin/#text-domains',
428+
7
432429
);
430+
} else {
431+
$plugin_slug = $result->plugin()->slug();
432+
433+
if ( $plugin_slug !== $plugin_header['TextDomain'] ) {
434+
$this->add_result_warning_for_file(
435+
$result,
436+
sprintf(
437+
/* translators: 1: plugin header field, 2: plugin header text domain, 3: plugin slug */
438+
__( 'The "%1$s" header in the plugin file does not match the slug. Found "%2$s", expected "%3$s".', 'plugin-check' ),
439+
esc_html( $labels['TextDomain'] ),
440+
esc_html( $plugin_header['TextDomain'] ),
441+
esc_html( $plugin_slug )
442+
),
443+
'textdomain_mismatch',
444+
$plugin_main_file,
445+
0,
446+
0,
447+
'https://developer.wordpress.org/plugins/internationalization/how-to-internationalize-your-plugin/',
448+
6
449+
);
450+
}
433451
}
434452
}
435453

tests/phpunit/testdata/plugins/test-plugin-late-escaping-errors/load.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
* Author URI: https://make.wordpress.org/performance/
1010
* License: GPLv2 or later
1111
* License URI: https://www.gnu.org/licenses/old-licenses/gpl-2.0.html
12-
* Text Domain: test-plugin-check-errors
12+
* Text Domain: Test-plugin-check_errors
1313
* Domain Path: /languages
1414
*
1515
* @package test-plugin-check-errors

tests/phpunit/tests/Checker/Checks/Plugin_Header_Fields_Check_Tests.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@ public function test_run_with_invalid_header_fields() {
109109

110110
$this->assertCount( 1, wp_list_filter( $errors['load.php'][0][0], array( 'code' => 'plugin_header_missing_plugin_description' ) ) );
111111
$this->assertCount( 1, wp_list_filter( $errors['load.php'][0][0], array( 'code' => 'plugin_header_invalid_plugin_version' ) ) );
112+
$this->assertCount( 1, wp_list_filter( $errors['load.php'][0][0], array( 'code' => 'textdomain_invalid_format' ) ) );
112113
}
113114

114115
public function test_run_with_errors_requires_at_least_latest_plus_two_version() {

0 commit comments

Comments
 (0)