diff --git a/.distignore b/.distignore index a11fedba5..f1a136678 100644 --- a/.distignore +++ b/.distignore @@ -23,6 +23,7 @@ tests .gitignore .nvmrc .phpunit.result.cache +.typos.toml .wp-env.json .wp-env.override.json behat.yml diff --git a/.gitattributes b/.gitattributes index 324d86dc8..653802841 100644 --- a/.gitattributes +++ b/.gitattributes @@ -15,6 +15,7 @@ /.gherkin-lintignore export-ignore /.gherkin-lintrc export-ignore /.nvmrc export-ignore +/.typos.toml export-ignore /.wp-env.json export-ignore /composer.lock export-ignore /package.json export-ignore diff --git a/.typos.toml b/.typos.toml new file mode 100644 index 000000000..a46c4964e --- /dev/null +++ b/.typos.toml @@ -0,0 +1,4 @@ +[default] +extend-ignore-re = [ + "ba", +] diff --git a/includes/Checker/Checks/Plugin_Repo/File_Type_Check.php b/includes/Checker/Checks/Plugin_Repo/File_Type_Check.php index 1ac6a06fd..9853d53c6 100644 --- a/includes/Checker/Checks/Plugin_Repo/File_Type_Check.php +++ b/includes/Checker/Checks/Plugin_Repo/File_Type_Check.php @@ -24,13 +24,14 @@ class File_Type_Check extends Abstract_File_Check { use Amend_Check_Result; use Stable_Check; - const TYPE_COMPRESSED = 1; - const TYPE_PHAR = 2; - const TYPE_VCS = 4; - const TYPE_HIDDEN = 8; - const TYPE_APPLICATION = 16; - const TYPE_BADLY_NAMED = 32; - const TYPE_ALL = 63; // Same as all of the above with bitwise OR. + const TYPE_COMPRESSED = 1; + const TYPE_PHAR = 2; + const TYPE_VCS = 4; + const TYPE_HIDDEN = 8; + const TYPE_APPLICATION = 16; + const TYPE_BADLY_NAMED = 32; + const TYPE_LIBRARY_CORE = 64; + const TYPE_ALL = 127; // Same as all of the above with bitwise OR. /** * Bitwise flags to control check behavior. @@ -95,6 +96,9 @@ protected function check_files( Check_Result $result, array $files ) { // Check for badly named files. $this->look_for_badly_named_files( $result, $files ); } + if ( $this->flags & self::TYPE_LIBRARY_CORE ) { + $this->look_for_library_core_files( $result, $files ); + } } /** @@ -294,6 +298,80 @@ function ( $file ) use ( $plugin_path ) { } } + /** + * Looks for library core files and amends the given result with an error if found. + * + * @since 1.3.0 + * + * @param Check_Result $result The check result to amend, including the plugin context to check. + * @param array $files List of absolute file paths. + */ + protected function look_for_library_core_files( Check_Result $result, array $files ) { + // Known libraries that are part of WordPress core. + // https://meta.trac.wordpress.org/browser/sites/trunk/api.wordpress.org/public_html/core/credits/wp-59.php#L739 . + $look_known_libraries_core_services = array( + '(?plugin()->path(); + + $files = array_map( + function ( $file ) use ( $plugin_path ) { + return str_replace( $plugin_path, '', $file ); + }, + $files + ); + + foreach ( $files as $file ) { + if ( preg_match( $combined_pattern, $file ) ) { + $this->add_result_error_for_file( + $result, + __( 'Library files that are already in the WordPress core are not permitted.', 'plugin-check' ), + 'library_core_files', + $file, + 0, + 0, + '', + 8 + ); + } + } + } + /** * Gets the description for the check. * @@ -304,7 +382,7 @@ function ( $file ) use ( $plugin_path ) { * @return string Description. */ public function get_description(): string { - return __( 'Detects the usage of hidden and compressed files, VCS directories, application files and badly named files.', 'plugin-check' ); + return __( 'Detects the usage of hidden and compressed files, VCS directories, application files, badly named files and Library Core Files.', 'plugin-check' ); } /** diff --git a/tests/phpunit/testdata/plugins/test-plugin-file-type-library-core-errors/PHPMailer.php b/tests/phpunit/testdata/plugins/test-plugin-file-type-library-core-errors/PHPMailer.php new file mode 100644 index 000000000..b3d9bbc7f --- /dev/null +++ b/tests/phpunit/testdata/plugins/test-plugin-file-type-library-core-errors/PHPMailer.php @@ -0,0 +1 @@ +assertArrayHasKey( 0, $errors['badly|file%name!@#$%^&*()+=[]{};:"\'<>,?|`~.php'][0] ); $this->assertCount( 1, wp_list_filter( $errors['badly|file%name!@#$%^&*()+=[]{};:"\'<>,?|`~.php'][0][0], array( 'code' => 'badly_named_files' ) ) ); } + + public function test_run_with_library_core_errors() { + $check_context = new Check_Context( UNIT_TESTS_PLUGIN_DIR . 'test-plugin-file-type-library-core-errors/load.php' ); + $check_result = new Check_Result( $check_context ); + + $check = new File_Type_Check( File_Type_Check::TYPE_LIBRARY_CORE ); + $check->run( $check_result ); + + $errors = $check_result->get_errors(); + + $this->assertNotEmpty( $errors ); + $this->assertEquals( 2, $check_result->get_error_count() ); + + // Check for core PHPMailer. + $this->assertArrayHasKey( 0, $errors['PHPMailer.php'] ); + $this->assertArrayHasKey( 0, $errors['PHPMailer.php'][0] ); + $this->assertCount( 1, wp_list_filter( $errors['PHPMailer.php'][0][0], array( 'code' => 'library_core_files' ) ) ); + + // Check for core jquery. + $this->assertArrayHasKey( 0, $errors['jquery.js'] ); + $this->assertArrayHasKey( 0, $errors['jquery.js'][0] ); + $this->assertCount( 1, wp_list_filter( $errors['jquery.js'][0][0], array( 'code' => 'library_core_files' ) ) ); + } }