-
Notifications
You must be signed in to change notification settings - Fork 75
Add feature tests for checking plugin with addon enabled #518
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
Changes from 25 commits
8b6abd0
697526f
8bc5c7b
06a018d
7f43fdc
4139c9d
a5df7a7
82dc502
9c8e5cb
dd7e4ba
3b24b72
92dabcb
f37a830
3d7d8a3
82cc334
0fbf653
dd7827b
90db569
56eb443
b30b02f
16f1b31
2a8a2a8
714b53a
c07dc62
2ce4dea
9487cfe
10b39a9
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -34,16 +34,29 @@ | |
|
||
// Create the CLI command instance and add to WP CLI. | ||
$plugin_command = new Plugin_Check_Command( $context ); | ||
WP_CLI::add_command( 'plugin', $plugin_command ); | ||
|
||
WP_CLI::add_command( | ||
'plugin', | ||
$plugin_command, | ||
array( | ||
'after_invoke' => function () { | ||
if ( | ||
file_exists( ABSPATH . 'wp-content/object-cache.php' ) && | ||
false !== strpos( file_get_contents( ABSPATH . 'wp-content/object-cache.php' ), 'WP_PLUGIN_CHECK_OBJECT_CACHE_DROPIN_VERSION' ) | ||
) { | ||
unlink( ABSPATH . 'wp-content/object-cache.php' ); | ||
} | ||
}, | ||
) | ||
); | ||
|
||
/** | ||
* Adds hook to set up the object-cache.php drop-in file. | ||
/* | ||
* Add hook to set up the object-cache.php drop-in file. | ||
* | ||
* @since 1.0.0 | ||
* Runs after wp-config.php is loaded and thus ABSPATH is defined, | ||
* but before any plugins are actually loaded. | ||
*/ | ||
WP_CLI::add_hook( | ||
'before_wp_load', | ||
'after_wp_config_load', | ||
function () { | ||
if ( CLI_Runner::is_plugin_check() ) { | ||
if ( ! file_exists( ABSPATH . 'wp-content/object-cache.php' ) ) { | ||
Comment on lines
69
to
70
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In principle, this should only be placed if runtime checks are run. If only static checks should be run against a plugin, this isn't needed. But I'm not sure it's reasonably possible to detect that from here. FWIW at the moment users of WP-CLI have to know about the |
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
<?php | ||
|
||
use WordPress\Plugin_Check\Checker\Check_Result; | ||
use WordPress\Plugin_Check\Checker\Checks\Abstract_Runtime_Check; | ||
use WordPress\Plugin_Check\Traits\Amend_Check_Result; | ||
use WordPress\Plugin_Check\Traits\Stable_Check; | ||
use WordPress\Plugin_Check\Traits\URL_Aware; | ||
|
||
class Example_Runtime_Check extends Abstract_Runtime_Check { | ||
|
||
use Amend_Check_Result; | ||
use Stable_Check; | ||
use URL_Aware; | ||
|
||
public function get_categories() { | ||
return array( 'new_category' ); | ||
} | ||
|
||
public function prepare() { | ||
$orig_scripts = isset( $GLOBALS['wp_scripts'] ) ? $GLOBALS['wp_scripts'] : null; | ||
|
||
// Backup the original values for the global state. | ||
$this->backup_globals(); | ||
|
||
return function () use ( $orig_scripts ) { | ||
if ( is_null( $orig_scripts ) ) { | ||
unset( $GLOBALS['wp_scripts'] ); | ||
} else { | ||
$GLOBALS['wp_scripts'] = $orig_scripts; | ||
} | ||
|
||
$this->restore_globals(); | ||
}; | ||
} | ||
|
||
public function run( Check_Result $result ) { | ||
$this->run_for_urls( | ||
array( home_url() ), | ||
function ( $url ) use ( $result ) { | ||
$this->check_url( $result, $url ); | ||
} | ||
); | ||
} | ||
|
||
protected function check_url( Check_Result $result, $url ) { | ||
// Reset the WP_Scripts instance. | ||
unset( $GLOBALS['wp_scripts'] ); | ||
|
||
// Run the 'wp_enqueue_script' action, wrapped in an output buffer in case of any callbacks printing scripts | ||
// directly. This is discouraged, but some plugins or themes are still doing it. | ||
ob_start(); | ||
wp_enqueue_scripts(); | ||
wp_scripts()->do_head_items(); | ||
wp_scripts()->do_footer_items(); | ||
ob_end_clean(); | ||
|
||
foreach ( wp_scripts()->done as $handle ) { | ||
$script = wp_scripts()->registered[ $handle ]; | ||
|
||
if ( strpos( $script->src, $result->plugin()->url() ) !== 0 ) { | ||
continue; | ||
} | ||
|
||
$script_path = str_replace( $result->plugin()->url(), $result->plugin()->path(), $script->src ); | ||
|
||
$this->add_result_warning_for_file( | ||
$result, | ||
sprintf( | ||
'Not allowed to enqueue scripts. Found script handle "%s"', | ||
$handle | ||
), | ||
'ExampleRuntimeCheck.ForbiddenScript', | ||
$script_path | ||
); | ||
} | ||
} | ||
|
||
public function get_description(): string { | ||
return ''; | ||
} | ||
|
||
public function get_documentation_url(): string { | ||
return ''; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
<?php | ||
|
||
use WordPress\Plugin_Check\Checker\Check_Result; | ||
use WordPress\Plugin_Check\Checker\Checks\Abstract_File_Check; | ||
use WordPress\Plugin_Check\Traits\Amend_Check_Result; | ||
use WordPress\Plugin_Check\Traits\Stable_Check; | ||
|
||
class Example_Static_Check extends Abstract_File_Check { | ||
|
||
use Amend_Check_Result; | ||
use Stable_Check; | ||
|
||
public function get_categories() { | ||
return array( 'new_category' ); | ||
} | ||
|
||
protected function check_files( Check_Result $result, array $files ) { | ||
$php_files = self::filter_files_by_extension( $files, 'php' ); | ||
$file = self::file_preg_match( '#I\sam\sbad#', $php_files ); | ||
if ( $file ) { | ||
$this->add_result_error_for_file( | ||
$result, | ||
__( 'Prohibited text found.', 'pcp-addon' ), | ||
'prohibited_text_detected', | ||
$file, | ||
0, | ||
0, | ||
'', | ||
8 | ||
); | ||
} | ||
} | ||
|
||
public function get_description(): string { | ||
return ''; | ||
} | ||
|
||
public function get_documentation_url(): string { | ||
return ''; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
<?php | ||
/** | ||
* Plugin Name: PCP Addon | ||
* Plugin URI: https://example.com | ||
* Description: Plugin Check addon. | ||
* Version: 0.1.0 | ||
* Author: WordPress Performance Team | ||
* Author URI: https://make.wordpress.org/performance/ | ||
* License: GPL-2.0+ | ||
* License URI: http://www.gnu.org/licenses/gpl-2.0.txt | ||
* Requires Plugins: plugin-check | ||
*/ | ||
|
||
add_filter( | ||
'wp_plugin_check_categories', | ||
function ( array $categories ) { | ||
return array_merge( $categories, array( 'new_category' => esc_html__( 'New Category', 'pcp-addon' ) ) ); | ||
} | ||
); | ||
|
||
add_filter( | ||
'wp_plugin_check_checks', | ||
function ( array $checks ) { | ||
require_once plugin_dir_path( __FILE__ ) . 'Example_Static_Check.php'; | ||
require_once plugin_dir_path( __FILE__ ) . 'Example_Runtime_Check.php'; | ||
|
||
return array_merge( | ||
$checks, | ||
array( | ||
'example_static' => new Example_Static_Check(), | ||
'example_runtime' => new Example_Runtime_Check(), | ||
) | ||
); | ||
} | ||
); |
Uh oh!
There was an error while loading. Please reload this page.