Skip to content
Open
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
16 changes: 15 additions & 1 deletion assets/js/plugin-check-admin.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
const categoriesList = document.querySelectorAll(
'input[name=categories]'
);
const typesList = document.querySelectorAll( 'input[name=types]' );
const templates = {};

// Return early if the elements cannot be found on the page.
Expand All @@ -16,7 +17,8 @@
! pluginsList ||
! resultsContainer ||
! spinner ||
! categoriesList.length
! categoriesList.length ||
! typesList.length
) {
console.error( 'Missing form elements on page' );
return;
Expand Down Expand Up @@ -73,6 +75,9 @@
for ( let i = 0; i < categoriesList.length; i++ ) {
categoriesList[ i ].disabled = true;
}
for ( let i = 0; i < typesList.length; i++ ) {
typesList[ i ].disabled = true;
}

getChecksToRun()
.then( setUpEnvironment )
Expand Down Expand Up @@ -112,6 +117,9 @@
for ( let i = 0; i < categoriesList.length; i++ ) {
categoriesList[ i ].disabled = false;
}
for ( let i = 0; i < typesList.length; i++ ) {
typesList[ i ].disabled = false;
}
}

/**
Expand Down Expand Up @@ -308,6 +316,12 @@
includeExperimental && includeExperimental.checked ? 1 : 0
);

for ( let i = 0; i < typesList.length; i++ ) {
if ( typesList[ i ].checked ) {
pluginCheckData.append( 'types[]', typesList[ i ].value );
}
}

return fetch( ajaxurl, {
method: 'POST',
credentials: 'same-origin',
Expand Down
3 changes: 3 additions & 0 deletions includes/Admin/Admin_AJAX.php
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,8 @@ public function run_checks() {

$checks = filter_input( INPUT_POST, 'checks', FILTER_DEFAULT, FILTER_FORCE_ARRAY );
$checks = is_null( $checks ) ? array() : $checks;
$types = filter_input( INPUT_POST, 'types', FILTER_DEFAULT, FILTER_FORCE_ARRAY );
$types = is_null( $types ) ? array() : $types;
$plugin = filter_input( INPUT_POST, 'plugin', FILTER_SANITIZE_FULL_SPECIAL_CHARS );

$include_experimental = 1 === filter_input( INPUT_POST, 'include-experimental', FILTER_VALIDATE_INT );
Expand All @@ -266,6 +268,7 @@ public function run_checks() {
$runner->set_experimental_flag( $include_experimental );
$runner->set_check_slugs( $checks );
$runner->set_plugin( $plugin );
$runner->set_types( $types );
$results = $runner->run();
} catch ( Exception $error ) {
wp_send_json_error(
Expand Down
2 changes: 2 additions & 0 deletions includes/Admin/Admin_Page.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use WordPress\Plugin_Check\Checker\Check;
use WordPress\Plugin_Check\Checker\Check_Categories;
use WordPress\Plugin_Check\Checker\Check_Repository;
use WordPress\Plugin_Check\Checker\Check_Types;
use WordPress\Plugin_Check\Checker\Default_Check_Repository;

/**
Expand Down Expand Up @@ -279,6 +280,7 @@ public function render_page() {
$selected_plugin_basename = filter_input( INPUT_GET, 'plugin', FILTER_SANITIZE_FULL_SPECIAL_CHARS );

$categories = Check_Categories::get_categories();
$types = Check_Types::get_types();

// Get user settings for category preferences.
$user_enabled_categories = get_user_setting( 'plugin_check_category_preferences', implode( '__', $this->get_default_check_categories_to_be_selected() ) );
Expand Down
14 changes: 14 additions & 0 deletions includes/Checker/AJAX_Runner.php
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,20 @@ protected function get_categories_param() {
return $categories;
}

/**
* Returns an array of types for filtering the checks.
*
* @since 1.7.0
*
* @return array An array of types for filtering the checks.
*/
protected function get_types_param() {
$types = filter_input( INPUT_POST, 'types', FILTER_DEFAULT, FILTER_FORCE_ARRAY );
$types = is_null( $types ) ? array() : $types;

return $types;
}

/**
* Returns plugin slug parameter.
*
Expand Down
59 changes: 58 additions & 1 deletion includes/Checker/Abstract_Check_Runner.php
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,14 @@ abstract class Abstract_Check_Runner implements Check_Runner {
*/
protected $check_categories;

/**
* Checks types for the filter.
*
* @since 1.7.0
* @var array
*/
protected $check_types;

/**
* The mode to run checks in.
*
Expand Down Expand Up @@ -173,6 +181,15 @@ abstract protected function get_include_experimental_param();
*/
abstract protected function get_categories_param();

/**
* Returns an array of types for filtering the checks.
*
* @since 1.7.0
*
* @return array An array of types.
*/
abstract protected function get_types_param();

/**
* Returns plugin slug parameter.
*
Expand Down Expand Up @@ -268,6 +285,30 @@ final public function set_plugin( $plugin ) {
$this->plugin = $plugin;
}

/**
* Sets types for filtering the checks.
*
* @since 1.7.0
*
* @param array $types An array of types for filtering.
*
* @throws Exception Thrown if the types do not match the original request parameter.
*/
final public function set_types( $types ) {
if ( $this->initialized_early ) {
if ( $types !== $this->get_types_param() ) {
throw new Exception(
sprintf(
/* translators: %s: categories */
__( 'Invalid types: The %s value does not match the original request parameter.', 'plugin-check' ),
'types'
)
);
}
}
$this->check_types = $types;
}

/**
* Sets whether to include experimental checks in the process.
*
Expand Down Expand Up @@ -381,14 +422,15 @@ final public function run() {
$checks = $this->get_checks_to_run();
$preparations = $this->get_shared_preparations( $checks );
$cleanups = array();
$check_types = $this->get_types();

// Prepare all shared preparations.
foreach ( $preparations as $preparation ) {
$instance = new $preparation['class']( ...$preparation['args'] );
$cleanups[] = $instance->prepare();
}

$results = $this->get_checks_instance()->run_checks( $this->get_check_context(), $checks, $this );
$results = $this->get_checks_instance()->run_checks( $this->get_check_context(), $checks, $this, $check_types );

if ( ! empty( $cleanups ) ) {
foreach ( $cleanups as $cleanup ) {
Expand Down Expand Up @@ -620,6 +662,21 @@ final protected function get_categories() {
return $this->get_categories_param();
}

/**
* Returns an array of types for filtering the checks.
*
* @since 1.7.0
*
* @return array An array of types.
*/
final protected function get_types() {
if ( null !== $this->check_types ) {
return $this->check_types;
}

return $this->get_types_param();
}

/**
* Returns plugin slug.
*
Expand Down
12 changes: 12 additions & 0 deletions includes/Checker/CLI_Runner.php
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,18 @@ protected function get_categories_param() {
return $categories;
}

/**
* Returns an array of types for filtering the checks.
*
* @since 1.7.0
*
* @return array An array of types.
*/
protected function get_types_param() {
// Return all check types since the cli already has arguments to filter results by type.
return Check_Types::get_type_slugs();
}

/**
* Returns plugin slug parameter.
*
Expand Down
60 changes: 37 additions & 23 deletions includes/Checker/Check_Result.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,14 @@ final class Check_Result {
*/
protected $check_context;

/**
* List of types to filter.
*
* @since 1.7.0
* @var array
*/
protected $check_types = array();

/**
* List of errors.
*
Expand Down Expand Up @@ -57,12 +65,14 @@ final class Check_Result {
/**
* Sets the context for the plugin to check.
*
* @since 1.0.0
*
* @param Check_Context $check_context Check context instance for the plugin.
* @param array $check_types An array of check types to filter.
*
* @since 1.0.0
*/
public function __construct( Check_Context $check_context ) {
public function __construct( Check_Context $check_context, array $check_types = array() ) {
$this->check_context = $check_context;
$this->check_types = $check_types;
}

/**
Expand Down Expand Up @@ -118,29 +128,33 @@ public function add_message( $error, $message, $args = array() ) {
unset( $data['line'], $data['column'], $data['file'] );

if ( $error ) {
if ( ! isset( $this->errors[ $file ] ) ) {
$this->errors[ $file ] = array();
}
if ( ! isset( $this->errors[ $file ][ $line ] ) ) {
$this->errors[ $file ][ $line ] = array();
if ( in_array( 'error', $this->check_types, true ) ) {
if ( ! isset( $this->errors[ $file ] ) ) {
$this->errors[ $file ] = array();
}
if ( ! isset( $this->errors[ $file ][ $line ] ) ) {
$this->errors[ $file ][ $line ] = array();
}
if ( ! isset( $this->errors[ $file ][ $line ][ $column ] ) ) {
$this->errors[ $file ][ $line ][ $column ] = array();
}
$this->errors[ $file ][ $line ][ $column ][] = $data;
++$this->error_count;
}
if ( ! isset( $this->errors[ $file ][ $line ][ $column ] ) ) {
$this->errors[ $file ][ $line ][ $column ] = array();
}
$this->errors[ $file ][ $line ][ $column ][] = $data;
++$this->error_count;
} else {
if ( ! isset( $this->warnings[ $file ] ) ) {
$this->warnings[ $file ] = array();
}
if ( ! isset( $this->warnings[ $file ][ $line ] ) ) {
$this->warnings[ $file ][ $line ] = array();
}
if ( ! isset( $this->warnings[ $file ][ $line ][ $column ] ) ) {
$this->warnings[ $file ][ $line ][ $column ] = array();
if ( in_array( 'warning', $this->check_types, true ) ) {
if ( ! isset( $this->warnings[ $file ] ) ) {
$this->warnings[ $file ] = array();
}
if ( ! isset( $this->warnings[ $file ][ $line ] ) ) {
$this->warnings[ $file ][ $line ] = array();
}
if ( ! isset( $this->warnings[ $file ][ $line ][ $column ] ) ) {
$this->warnings[ $file ][ $line ][ $column ] = array();
}
$this->warnings[ $file ][ $line ][ $column ][] = $data;
++$this->warning_count;
}
$this->warnings[ $file ][ $line ][ $column ][] = $data;
++$this->warning_count;
}
}

Expand Down
57 changes: 57 additions & 0 deletions includes/Checker/Check_Types.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php
/**
* Class WordPress\Plugin_Check\Checker\Check_Categories
*
* @package plugin-check
*/

namespace WordPress\Plugin_Check\Checker;

/**
* Check Categories class.
*
* @since 1.0.0
*/
class Check_Types {

// Constants for available check types.
const TYPE_ERROR = 'error';
const TYPE_WARNING = 'warning';

/**
* Returns an array of check types.
*
* @since 1.7.0
*
* @return array An array of check types.
*/
public static function get_types() {
$default_types = array(
self::TYPE_ERROR => __( 'Error', 'plugin-check' ),
self::TYPE_WARNING => __( 'Warning', 'plugin-check' ),
);

/**
* Filters the check types.
*
* @since 1.7.0
*
* @param array<string, string> $default_categories Associative array of type slugs to labels.
*/
$check_types = (array) apply_filters( 'wp_plugin_check_types', $default_types );

return $check_types;
}


/**
* Returns an array of available types.
*
* @since 1.7.0
*
* @return array An array of available types.
*/
public static function get_type_slugs() {
return array_keys( self::get_types() );
}
}
10 changes: 4 additions & 6 deletions includes/Checker/Checks.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,17 +27,15 @@ final class Checks {
/**
* Runs checks against the plugin.
*
* @since 1.0.0
*
* @param Check_Context $context The check context for the plugin to be checked.
* @param array $checks An array of Check objects to run.
* @param Check_Runner $runner The runner instance that created this result.
* @return Check_Result Object containing all check results.
*
* @param array $check_types An array of check types to filter.
* @throws Exception Thrown when check fails with critical error.
* @since 1.0.0
*/
public function run_checks( Check_Context $context, array $checks, ?Check_Runner $runner = null ) {
$result = new Check_Result( $context );
public function run_checks( Check_Context $context, array $checks, ?Check_Runner $runner = null, array $check_types = array() ) {
$result = new Check_Result( $context, $check_types );

// Run the checks.
array_walk(
Expand Down
Loading
Loading