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

Open
wants to merge 16 commits into
base: trunk
Choose a base branch
from
Open
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 @@
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 @@
// 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 @@
}
}

/**
* Looks for library core files and amends the given result with an error if found.
*
* @since 1.2.0
davidperezgar marked this conversation as resolved.
Show resolved Hide resolved
*
* @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(?!\/)', // spellchecker:disable-line

Check warning on line 320 in includes/Checker/Checks/Plugin_Repo/File_Type_Check.php

View workflow job for this annotation

GitHub Actions / Spell Check with Typos

"ba" should be "by" or "be".
'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 @@
* @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' );

Check warning on line 385 in includes/Checker/Checks/Plugin_Repo/File_Type_Check.php

View check run for this annotation

Codecov / codecov/patch

includes/Checker/Checks/Plugin_Repo/File_Type_Check.php#L385

Added line #L385 was not covered by tests
davidperezgar marked this conversation as resolved.
Show resolved Hide resolved
}

/**
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,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.
*/
18 changes: 18 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,22 @@ 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( 1, $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
}
}
Loading