Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Check including libraries already in wp core including jquery #715

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .distignore
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ tests
.gitignore
.nvmrc
.phpunit.result.cache
.typos.toml
.wp-env.json
.wp-env.override.json
behat.yml
Expand Down
1 change: 1 addition & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions .typos.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
[default]
extend-ignore-re = [
"ba",
]
94 changes: 86 additions & 8 deletions includes/Checker/Checks/Plugin_Repo/File_Type_Check.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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 );
}
}

/**
Expand Down Expand Up @@ -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(
'(?<![\.|-])jquery(-[0-9|\.]*)?(\.slim)?(\.min)?\.js(?!\/)',
'jquery-ui(-[0-9|\.]*)?(\.slim)?(\.min)?\.js(?!\/)',
'jquery.color(\.slim)?(\.min)?\.js(?!\/)',
'jquery.ui.touch-punch(?!\/)',
'jquery.hoverintent(?!\/)',
'jquery.imgareaselect(?!\/)',
'jquery.hotkeys(?!\/)',
'jquery.ba-serializeobject(?!\/)',
'jquery.query-object(?!\/)',
'jquery.suggest(?!\/)',
'polyfill(\.min)?\.js(?!\/)',
'iris(\.min)?\.js(?!\/)',
'backbone(\.min)?\.js(?!\/)',
'clipboard(\.min)?\.js(?!\/)',
'closest(\.min)?\.js(?!\/)',
'codemirror(\.min)?\.js(?!\/)',
'formdata(\.min)?\.js(?!\/)',
'json2(\.min)?\.js(?!\/)',
'lodash(\.min)?\.js(?!\/)',
'masonry(\.pkgd)(\.min)?\.js(?!\/)',
'mediaelement-and-player(\.min)?\.js(?!\/)',
'moment(\.min)?\.js(?!\/)',
'plupload(\.full)(\.min)?\.js(?!\/)',
'thickbox(\.min)?\.js(?!\/)',
'twemoji(\.min)?\.js(?!\/)',
'underscore([\.|-]min)?\.js(?!\/)',
'moxie(\.min)?\.js(?!\/)',
'zxcvbn(\.min)?\.js(?!\/)',
'getid3\.php(?!\/)',
'pclzip\.lib\.php(?!\/)',
'PasswordHash\.php(?!\/)',
'PHPMailer\.php(?!\/)',
'SimplePie\.php(?!\/)',
);

$combined_pattern = '/(' . implode( ')|(', $look_known_libraries_core_services ) . ')/i';

$plugin_path = $result->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.
*
Expand All @@ -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' );
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<?php
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
// For testing purposes, this file is empty.
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php
/**
* Plugin Name: Test Plugin File Type Library Core Files
* Plugin URI: https://github.com/WordPress/plugin-check
* Description: Some plugin description.
* Author: WordPress Performance Team
* Author URI: https://make.wordpress.org/plugins/
* License: GPLv2 or later
* License URI: https://www.gnu.org/licenses/old-licenses/gpl-2.0.html
* Text Domain: test-plugin-file-type-library-core-errors
*
* @package test-plugin-file-type-library-core-errors
*/

/**
* Plugin folder contains a library core file which is not allowed.
*/
23 changes: 23 additions & 0 deletions tests/phpunit/tests/Checker/Checks/File_Type_Check_Tests.php
Original file line number Diff line number Diff line change
Expand Up @@ -127,4 +127,27 @@ public function test_run_with_badly_named_errors() {
$this->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' ) ) );
davidperezgar marked this conversation as resolved.
Show resolved Hide resolved

// 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' ) ) );
}
}