diff --git a/includes/Checker/Checks/Plugin_Repo/Premium_Only_Function_Check.php b/includes/Checker/Checks/Plugin_Repo/Premium_Only_Function_Check.php new file mode 100644 index 000000000..08bdf319d --- /dev/null +++ b/includes/Checker/Checks/Plugin_Repo/Premium_Only_Function_Check.php @@ -0,0 +1,91 @@ +add_result_error_for_file( + $result, + __( 'Function declarations ending in __premium_only look like premium/pro-only code and should not be included in a WordPress.org plugin.', 'plugin-check' ), + 'premium_only_function_found', + $match['file'], + $match['line'], + $match['column'], + 'https://developer.wordpress.org/plugins/wordpress-org/detailed-plugin-guidelines/' + ); + } + } + } + + /** + * Gets the description for the check. + * + * Every check must have a short description explaining what the check does. + * + * @since x.x.x + * + * @return string Description. + */ + public function get_description(): string { + return __( 'Detects function declarations ending in __premium_only.', 'plugin-check' ); + } + + /** + * Gets the documentation URL for the check. + * + * Every check must have a URL with further information about the check. + * + * @since x.x.x + * + * @return string The documentation URL. + */ + public function get_documentation_url(): string { + return __( 'https://developer.wordpress.org/plugins/wordpress-org/detailed-plugin-guidelines/', 'plugin-check' ); + } +} diff --git a/includes/Checker/Default_Check_Repository.php b/includes/Checker/Default_Check_Repository.php index d4d5c548d..3c335614e 100644 --- a/includes/Checker/Default_Check_Repository.php +++ b/includes/Checker/Default_Check_Repository.php @@ -14,6 +14,7 @@ */ class Default_Check_Repository extends Empty_Check_Repository { + /** * True if the class was fully initialized. * @@ -105,6 +106,7 @@ private function register_default_checks() { 'external_admin_menu_links' => new Checks\Plugin_Repo\External_Admin_Menu_Links_Check(), 'wp_functions_compatibility' => new Checks\Plugin_Repo\WP_Functions_Compatibility_Check(), 'ai_provider' => new Checks\General\AI_Provider_Check(), + 'premium_only_function' => new Checks\Plugin_Repo\Premium_Only_Function_Check(), ) ); diff --git a/tests/phpunit/testdata/plugins/test-plugin-premium-only-function-with-errors/load.php b/tests/phpunit/testdata/plugins/test-plugin-premium-only-function-with-errors/load.php new file mode 100644 index 000000000..ff1d13a2f --- /dev/null +++ b/tests/phpunit/testdata/plugins/test-plugin-premium-only-function-with-errors/load.php @@ -0,0 +1,18 @@ +

Premium-only test notice.

'; +} diff --git a/tests/phpunit/testdata/plugins/test-plugin-premium-only-function-without-errors/load.php b/tests/phpunit/testdata/plugins/test-plugin-premium-only-function-without-errors/load.php new file mode 100644 index 000000000..58209ef5a --- /dev/null +++ b/tests/phpunit/testdata/plugins/test-plugin-premium-only-function-without-errors/load.php @@ -0,0 +1,18 @@ +

Standard test notice.

'; +} diff --git a/tests/phpunit/tests/Checker/Checks/Premium_Only_Function_Check_Tests.php b/tests/phpunit/tests/Checker/Checks/Premium_Only_Function_Check_Tests.php new file mode 100644 index 000000000..b4abe9aef --- /dev/null +++ b/tests/phpunit/tests/Checker/Checks/Premium_Only_Function_Check_Tests.php @@ -0,0 +1,45 @@ +run( $check_result ); + + $errors = $check_result->get_errors(); + + $this->assertNotEmpty( $errors ); + $this->assertArrayHasKey( 'load.php', $errors ); + $this->assertSame( 2, $check_result->get_error_count() ); + + $this->assertArrayHasKey( 12, $errors['load.php'] ); + $this->assertArrayHasKey( 16, $errors['load.php'] ); + $this->assertSame( 'premium_only_function_found', $errors['load.php'][12][1][0]['code'] ); + $this->assertSame( 'premium_only_function_found', $errors['load.php'][16][1][0]['code'] ); + } + + public function test_run_without_errors() { + $check = new Premium_Only_Function_Check(); + $check_context = new Check_Context( UNIT_TESTS_PLUGIN_DIR . 'test-plugin-premium-only-function-without-errors/load.php' ); + $check_result = new Check_Result( $check_context ); + + $check->run( $check_result ); + + $errors = $check_result->get_errors(); + + $this->assertEmpty( $errors ); + $this->assertSame( 0, $check_result->get_error_count() ); + } +}