diff --git a/includes/Checker/Checks/Plugin_Repo/Plugin_Updater_Check.php b/includes/Checker/Checks/Plugin_Repo/Plugin_Updater_Check.php index 8a1ed69b5..d57cef112 100644 --- a/includes/Checker/Checks/Plugin_Repo/Plugin_Updater_Check.php +++ b/includes/Checker/Checks/Plugin_Repo/Plugin_Updater_Check.php @@ -98,7 +98,11 @@ protected function check_files( Check_Result $result, array $files ) { } /** - * Looks for UpdateURI in plugin header and amends the given result with an error if found. + * Looks for UpdateURI in plugin header and amends the given result with an error if invalid. + * + * Plugins on WordPress.org should not use this header, but the same URI formats as in the + * directory API are accepted here: a wordpress.org or w.org plugin URL whose slug matches + * this plugin passes; any other value is flagged. * * @since 1.0.0 * @@ -110,19 +114,34 @@ protected function look_for_update_uri_header( Check_Result $result ) { } $plugin_main_file = $result->plugin()->main_file(); + $plugin_slug = $result->plugin()->slug(); $plugin_header = get_plugin_data( $plugin_main_file ); - if ( ! empty( $plugin_header['UpdateURI'] ) ) { - $this->add_result_error_for_file( - $result, - __( 'Including An Update Checker / Changing Updates functionality.
Plugin Updater detected. Use of the Update URI header is not allowed in plugins hosted on WordPress.org.', 'plugin-check' ), - 'plugin_updater_detected', - $plugin_main_file, - 0, - 0, - 'https://developer.wordpress.org/plugins/wordpress-org/common-issues/#update-checker', - 9 - ); + + if ( empty( $plugin_header['UpdateURI'] ) ) { + return; + } + + $update_uri_matches = array(); + $update_uri_valid = (bool) preg_match( + '!^(https?://)?(wordpress.org|w.org)/plugins?/(?P[^/]+)/?$!i', + $plugin_header['UpdateURI'], + $update_uri_matches + ); + + if ( $update_uri_valid && isset( $update_uri_matches['slug'] ) && $update_uri_matches['slug'] === $plugin_slug ) { + return; } + + $this->add_result_error_for_file( + $result, + __( 'Including An Update Checker / Changing Updates functionality.
Plugin Updater detected. Use of the Update URI header is not allowed in plugins hosted on WordPress.org.', 'plugin-check' ), + 'plugin_updater_detected', + $plugin_main_file, + 0, + 0, + 'https://developer.wordpress.org/plugins/wordpress-org/common-issues/#update-checker', + 9 + ); } /** diff --git a/tests/phpunit/testdata/plugins/test-plugin-update-uri-w-org-ok/load.php b/tests/phpunit/testdata/plugins/test-plugin-update-uri-w-org-ok/load.php new file mode 100644 index 000000000..a2f1f0aab --- /dev/null +++ b/tests/phpunit/testdata/plugins/test-plugin-update-uri-w-org-ok/load.php @@ -0,0 +1,17 @@ +assertEquals( 0, $check_result->get_error_count() ); $this->assertEquals( 0, $check_result->get_warning_count() ); } + + /** + * Update URI may point at this plugin’s WordPress.org URL; that must not be flagged. + */ + public function test_run_update_uri_wordpress_org_matching_slug_no_error() { + $check_context = new Check_Context( UNIT_TESTS_PLUGIN_DIR . 'test-plugin-update-uri-w-org-ok/load.php' ); + $check_result = new Check_Result( $check_context ); + + $check = new Plugin_Updater_Check( Plugin_Updater_Check::TYPE_PLUGIN_UPDATE_URI_HEADER ); + $check->run( $check_result ); + + $this->assertEmpty( $check_result->get_errors() ); + $this->assertSame( 0, $check_result->get_error_count() ); + } }