From efe873b75ab61b6942f7e8f1098e9871a73f0daa Mon Sep 17 00:00:00 2001 From: Nilambar Sharma Date: Mon, 25 Nov 2024 11:30:54 +0545 Subject: [PATCH 1/2] Validate donate link --- .../Plugin_Repo/Plugin_Readme_Check.php | 38 +++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/includes/Checker/Checks/Plugin_Repo/Plugin_Readme_Check.php b/includes/Checker/Checks/Plugin_Repo/Plugin_Readme_Check.php index ba9c7b85..ab949b49 100644 --- a/includes/Checker/Checks/Plugin_Repo/Plugin_Readme_Check.php +++ b/includes/Checker/Checks/Plugin_Repo/Plugin_Readme_Check.php @@ -104,6 +104,9 @@ protected function check_files( Check_Result $result, array $files ) { // Check the readme file for warnings. $this->check_for_warnings( $result, $readme_file, $parser ); + // Check the readme file for donate link. + $this->check_for_donate_link( $result, $readme_file, $parser ); + // Check the readme file for contributors. $this->check_for_contributors( $result, $readme_file ); } @@ -601,6 +604,41 @@ private function check_for_warnings( Check_Result $result, string $readme_file, } } + /** + * Checks the readme file for donate link. + * + * @since 1.3.0 + * + * @param Check_Result $result The Check Result to amend. + * @param string $readme_file Readme file. + * @param Parser $parser The Parser object. + */ + private function check_for_donate_link( Check_Result $result, string $readme_file, Parser $parser ) { + $donate_link = $parser->donate_link; + + // Bail if empty donate link. + if ( empty( $donate_link ) ) { + return; + } + + if ( ! ( filter_var( $donate_link, FILTER_VALIDATE_URL ) === $donate_link && str_starts_with( $donate_link, 'http' ) ) ) { + $this->add_result_warning_for_file( + $result, + sprintf( + /* translators: %s: plugin header field */ + __( 'The "%s" header in the readme file must be a valid URL.', 'plugin-check' ), + 'Donate link' + ), + 'readme_invalid_donate_link', + $readme_file, + 0, + 0, + 'https://developer.wordpress.org/plugins/wordpress-org/how-your-readme-txt-works/#readme-header-information', + 6 + ); + } + } + /** * Checks the readme file for contributors. * From 095eb9dbebac528ff09b0e8d48d02ed9a09559d4 Mon Sep 17 00:00:00 2001 From: Nilambar Sharma Date: Mon, 25 Nov 2024 11:41:49 +0545 Subject: [PATCH 2/2] Add test for donate link --- .../plugins/test-plugin-plugin-readme-md-with-errors/readme.md | 1 + tests/phpunit/tests/Checker/Checks/Plugin_Readme_Check_Tests.php | 1 + 2 files changed, 2 insertions(+) diff --git a/tests/phpunit/testdata/plugins/test-plugin-plugin-readme-md-with-errors/readme.md b/tests/phpunit/testdata/plugins/test-plugin-plugin-readme-md-with-errors/readme.md index a6e6ce7e..68637e56 100644 --- a/tests/phpunit/testdata/plugins/test-plugin-plugin-readme-md-with-errors/readme.md +++ b/tests/phpunit/testdata/plugins/test-plugin-plugin-readme-md-with-errors/readme.md @@ -9,5 +9,6 @@ Stable tag: trunk License: Oculus VR Inc. Software Development Kit License License URI: https://www.gnu.org/licenses/gpl-2.0.html Tags: performance, testing, security +Donate link: not-a-valid-url Here is a short description of the plugin. diff --git a/tests/phpunit/tests/Checker/Checks/Plugin_Readme_Check_Tests.php b/tests/phpunit/tests/Checker/Checks/Plugin_Readme_Check_Tests.php index 275da69c..a478c22f 100644 --- a/tests/phpunit/tests/Checker/Checks/Plugin_Readme_Check_Tests.php +++ b/tests/phpunit/tests/Checker/Checks/Plugin_Readme_Check_Tests.php @@ -234,6 +234,7 @@ public function test_run_md_with_errors() { $this->assertCount( 1, wp_list_filter( $warnings['readme.md'][0][0], array( 'code' => 'mismatched_plugin_name' ) ) ); $this->assertCount( 1, wp_list_filter( $warnings['readme.md'][0][0], array( 'code' => 'readme_invalid_contributors' ) ) ); + $this->assertCount( 1, wp_list_filter( $warnings['readme.md'][0][0], array( 'code' => 'readme_invalid_donate_link' ) ) ); } public function test_single_file_plugin_without_error_for_trademarks() {