-
Notifications
You must be signed in to change notification settings - Fork 54
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #518 from ernilambar/add/tests-for-filters
- Loading branch information
Showing
9 changed files
with
361 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 ''; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 ''; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(), | ||
) | ||
); | ||
} | ||
); |
Oops, something went wrong.