diff --git a/includes/Checker/Checks/General/I18n_Usage_Check.php b/includes/Checker/Checks/General/I18n_Usage_Check.php index 0b9b60e83..4521ba2a1 100644 --- a/includes/Checker/Checks/General/I18n_Usage_Check.php +++ b/includes/Checker/Checks/General/I18n_Usage_Check.php @@ -81,4 +81,28 @@ public function get_description(): string { public function get_documentation_url(): string { return __( 'https://developer.wordpress.org/plugins/internationalization/how-to-internationalize-your-plugin/', 'plugin-check' ); } + + /** + * Amends the given result with a message for the specified file, including error information. + * + * @since 1.3.0 + * + * @param Check_Result $result The check result to amend, including the plugin context to check. + * @param bool $error Whether it is an error or notice. + * @param string $message Error message. + * @param string $code Error code. + * @param string $file Absolute path to the file where the issue was found. + * @param int $line The line on which the message occurred. Default is 0 (unknown line). + * @param int $column The column on which the message occurred. Default is 0 (unknown column). + * @param string $docs URL for further information about the message. + * @param int $severity Severity level. Default is 5. + */ + protected function add_result_message_for_file( Check_Result $result, $error, $message, $code, $file, $line = 0, $column = 0, string $docs = '', $severity = 5 ) { + // Downgrade errors about usage of the 'default' text domain from WordPress Core to warnings. + if ( $error && str_ends_with( $message, " but got 'default'." ) ) { + $error = false; + } + + parent::add_result_message_for_file( $result, $error, $message, $code, $file, $line, $column, $docs, $severity ); + } } diff --git a/tests/phpunit/testdata/plugins/test-plugin-i18n-usage-with-default/load.php b/tests/phpunit/testdata/plugins/test-plugin-i18n-usage-with-default/load.php new file mode 100644 index 000000000..b90d80c4c --- /dev/null +++ b/tests/phpunit/testdata/plugins/test-plugin-i18n-usage-with-default/load.php @@ -0,0 +1,22 @@ +assertEquals( 0, $check_result->get_error_count() ); $this->assertEquals( 0, $check_result->get_warning_count() ); } + + public function test_run_without_default_textdomain() { + $i18n_usage_check = new I18n_Usage_Check(); + $check_context = new Check_Context( UNIT_TESTS_PLUGIN_DIR . 'test-plugin-i18n-usage-with-default/load.php' ); + $check_result = new Check_Result( $check_context ); + + $i18n_usage_check->run( $check_result ); + + // Explicitly using the 'default' text domain is a warning, omitting a text domain is an error. + $this->assertNotEmpty( $check_result->get_errors() ); + $this->assertNotEmpty( $check_result->get_warnings() ); + $this->assertEquals( 1, $check_result->get_error_count() ); + $this->assertEquals( 1, $check_result->get_warning_count() ); + } }