Skip to content

Commit f7ca912

Browse files
committed
feat: Add check type filter
1 parent d1acdd6 commit f7ca912

36 files changed

+394
-115
lines changed

assets/js/plugin-check-admin.js

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
const categoriesList = document.querySelectorAll(
99
'input[name=categories]'
1010
);
11+
const typesList = document.querySelectorAll( 'input[name=types]' );
1112
const templates = {};
1213

1314
// Return early if the elements cannot be found on the page.
@@ -16,7 +17,8 @@
1617
! pluginsList ||
1718
! resultsContainer ||
1819
! spinner ||
19-
! categoriesList.length
20+
! categoriesList.length ||
21+
! typesList.length
2022
) {
2123
console.error( 'Missing form elements on page' );
2224
return;
@@ -73,6 +75,9 @@
7375
for ( let i = 0; i < categoriesList.length; i++ ) {
7476
categoriesList[ i ].disabled = true;
7577
}
78+
for ( let i = 0; i < typesList.length; i++ ) {
79+
typesList[ i ].disabled = true;
80+
}
7681

7782
getChecksToRun()
7883
.then( setUpEnvironment )
@@ -112,6 +117,9 @@
112117
for ( let i = 0; i < categoriesList.length; i++ ) {
113118
categoriesList[ i ].disabled = false;
114119
}
120+
for ( let i = 0; i < typesList.length; i++ ) {
121+
typesList[ i ].disabled = false;
122+
}
115123
}
116124

117125
/**
@@ -308,6 +316,12 @@
308316
includeExperimental && includeExperimental.checked ? 1 : 0
309317
);
310318

319+
for ( let i = 0; i < typesList.length; i++ ) {
320+
if ( typesList[ i ].checked ) {
321+
pluginCheckData.append( 'types[]', typesList[ i ].value );
322+
}
323+
}
324+
311325
return fetch( ajaxurl, {
312326
method: 'POST',
313327
credentials: 'same-origin',

includes/Admin/Admin_AJAX.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,8 @@ public function run_checks() {
258258

259259
$checks = filter_input( INPUT_POST, 'checks', FILTER_DEFAULT, FILTER_FORCE_ARRAY );
260260
$checks = is_null( $checks ) ? array() : $checks;
261+
$types = filter_input( INPUT_POST, 'types', FILTER_DEFAULT, FILTER_FORCE_ARRAY );
262+
$types = is_null( $types ) ? array() : $types;
261263
$plugin = filter_input( INPUT_POST, 'plugin', FILTER_SANITIZE_FULL_SPECIAL_CHARS );
262264

263265
$include_experimental = 1 === filter_input( INPUT_POST, 'include-experimental', FILTER_VALIDATE_INT );
@@ -266,6 +268,7 @@ public function run_checks() {
266268
$runner->set_experimental_flag( $include_experimental );
267269
$runner->set_check_slugs( $checks );
268270
$runner->set_plugin( $plugin );
271+
$runner->set_types( $types );
269272
$results = $runner->run();
270273
} catch ( Exception $error ) {
271274
wp_send_json_error(

includes/Admin/Admin_Page.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use WordPress\Plugin_Check\Checker\Check;
1111
use WordPress\Plugin_Check\Checker\Check_Categories;
1212
use WordPress\Plugin_Check\Checker\Check_Repository;
13+
use WordPress\Plugin_Check\Checker\Check_Types;
1314
use WordPress\Plugin_Check\Checker\Default_Check_Repository;
1415

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

281282
$categories = Check_Categories::get_categories();
283+
$types = Check_Types::get_types();
282284

283285
// Get user settings for category preferences.
284286
$user_enabled_categories = get_user_setting( 'plugin_check_category_preferences', implode( '__', $this->get_default_check_categories_to_be_selected() ) );

includes/Checker/AJAX_Runner.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,20 @@ protected function get_categories_param() {
122122
return $categories;
123123
}
124124

125+
/**
126+
* Returns an array of types for filtering the checks.
127+
*
128+
* @since 1.7.0
129+
*
130+
* @return array An array of types for filtering the checks.
131+
*/
132+
protected function get_types_param() {
133+
$types = filter_input( INPUT_POST, 'types', FILTER_DEFAULT, FILTER_FORCE_ARRAY );
134+
$types = is_null( $types ) ? array() : $types;
135+
136+
return $types;
137+
}
138+
125139
/**
126140
* Returns plugin slug parameter.
127141
*

includes/Checker/Abstract_Check_Runner.php

Lines changed: 58 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,14 @@ abstract class Abstract_Check_Runner implements Check_Runner {
120120
*/
121121
protected $check_categories;
122122

123+
/**
124+
* Checks types for the filter.
125+
*
126+
* @since 1.7.0
127+
* @var array
128+
*/
129+
protected $check_types;
130+
123131
/**
124132
* The mode to run checks in.
125133
*
@@ -173,6 +181,15 @@ abstract protected function get_include_experimental_param();
173181
*/
174182
abstract protected function get_categories_param();
175183

184+
/**
185+
* Returns an array of types for filtering the checks.
186+
*
187+
* @since 1.7.0
188+
*
189+
* @return array An array of types.
190+
*/
191+
abstract protected function get_types_param();
192+
176193
/**
177194
* Returns plugin slug parameter.
178195
*
@@ -268,6 +285,30 @@ final public function set_plugin( $plugin ) {
268285
$this->plugin = $plugin;
269286
}
270287

288+
/**
289+
* Sets types for filtering the checks.
290+
*
291+
* @since 1.7.0
292+
*
293+
* @param array $types An array of types for filtering.
294+
*
295+
* @throws Exception Thrown if the types do not match the original request parameter.
296+
*/
297+
final public function set_types( $types ) {
298+
if ( $this->initialized_early ) {
299+
if ( $types !== $this->get_types_param() ) {
300+
throw new Exception(
301+
sprintf(
302+
/* translators: %s: categories */
303+
__( 'Invalid types: The %s value does not match the original request parameter.', 'plugin-check' ),
304+
'types'
305+
)
306+
);
307+
}
308+
}
309+
$this->check_types = $types;
310+
}
311+
271312
/**
272313
* Sets whether to include experimental checks in the process.
273314
*
@@ -381,14 +422,15 @@ final public function run() {
381422
$checks = $this->get_checks_to_run();
382423
$preparations = $this->get_shared_preparations( $checks );
383424
$cleanups = array();
425+
$check_types = $this->get_types();
384426

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

391-
$results = $this->get_checks_instance()->run_checks( $this->get_check_context(), $checks, $this );
433+
$results = $this->get_checks_instance()->run_checks( $this->get_check_context(), $checks, $this, $check_types );
392434

393435
if ( ! empty( $cleanups ) ) {
394436
foreach ( $cleanups as $cleanup ) {
@@ -620,6 +662,21 @@ final protected function get_categories() {
620662
return $this->get_categories_param();
621663
}
622664

665+
/**
666+
* Returns an array of types for filtering the checks.
667+
*
668+
* @since 1.7.0
669+
*
670+
* @return array An array of types.
671+
*/
672+
final protected function get_types() {
673+
if ( null !== $this->check_types ) {
674+
return $this->check_types;
675+
}
676+
677+
return $this->get_types_param();
678+
}
679+
623680
/**
624681
* Returns plugin slug.
625682
*

includes/Checker/CLI_Runner.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,18 @@ protected function get_categories_param() {
158158
return $categories;
159159
}
160160

161+
/**
162+
* Returns an array of types for filtering the checks.
163+
*
164+
* @since 1.7.0
165+
*
166+
* @return array An array of types.
167+
*/
168+
protected function get_types_param() {
169+
// Return all check types since the cli already has arguments to filter results by type.
170+
return Check_Types::get_type_slugs();
171+
}
172+
161173
/**
162174
* Returns plugin slug parameter.
163175
*

includes/Checker/Check_Result.php

Lines changed: 37 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,14 @@ final class Check_Result {
2222
*/
2323
protected $check_context;
2424

25+
/**
26+
* List of types to filter.
27+
*
28+
* @since 1.7.0
29+
* @var array
30+
*/
31+
protected $check_types = array();
32+
2533
/**
2634
* List of errors.
2735
*
@@ -57,12 +65,14 @@ final class Check_Result {
5765
/**
5866
* Sets the context for the plugin to check.
5967
*
60-
* @since 1.0.0
61-
*
6268
* @param Check_Context $check_context Check context instance for the plugin.
69+
* @param array $check_types An array of check types to filter.
70+
*
71+
* @since 1.0.0
6372
*/
64-
public function __construct( Check_Context $check_context ) {
73+
public function __construct( Check_Context $check_context, array $check_types = array() ) {
6574
$this->check_context = $check_context;
75+
$this->check_types = $check_types;
6676
}
6777

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

120130
if ( $error ) {
121-
if ( ! isset( $this->errors[ $file ] ) ) {
122-
$this->errors[ $file ] = array();
123-
}
124-
if ( ! isset( $this->errors[ $file ][ $line ] ) ) {
125-
$this->errors[ $file ][ $line ] = array();
131+
if ( in_array( 'error', $this->check_types, true ) ) {
132+
if ( ! isset( $this->errors[ $file ] ) ) {
133+
$this->errors[ $file ] = array();
134+
}
135+
if ( ! isset( $this->errors[ $file ][ $line ] ) ) {
136+
$this->errors[ $file ][ $line ] = array();
137+
}
138+
if ( ! isset( $this->errors[ $file ][ $line ][ $column ] ) ) {
139+
$this->errors[ $file ][ $line ][ $column ] = array();
140+
}
141+
$this->errors[ $file ][ $line ][ $column ][] = $data;
142+
++$this->error_count;
126143
}
127-
if ( ! isset( $this->errors[ $file ][ $line ][ $column ] ) ) {
128-
$this->errors[ $file ][ $line ][ $column ] = array();
129-
}
130-
$this->errors[ $file ][ $line ][ $column ][] = $data;
131-
++$this->error_count;
132144
} else {
133-
if ( ! isset( $this->warnings[ $file ] ) ) {
134-
$this->warnings[ $file ] = array();
135-
}
136-
if ( ! isset( $this->warnings[ $file ][ $line ] ) ) {
137-
$this->warnings[ $file ][ $line ] = array();
138-
}
139-
if ( ! isset( $this->warnings[ $file ][ $line ][ $column ] ) ) {
140-
$this->warnings[ $file ][ $line ][ $column ] = array();
145+
if ( in_array( 'warning', $this->check_types, true ) ) {
146+
if ( ! isset( $this->warnings[ $file ] ) ) {
147+
$this->warnings[ $file ] = array();
148+
}
149+
if ( ! isset( $this->warnings[ $file ][ $line ] ) ) {
150+
$this->warnings[ $file ][ $line ] = array();
151+
}
152+
if ( ! isset( $this->warnings[ $file ][ $line ][ $column ] ) ) {
153+
$this->warnings[ $file ][ $line ][ $column ] = array();
154+
}
155+
$this->warnings[ $file ][ $line ][ $column ][] = $data;
156+
++$this->warning_count;
141157
}
142-
$this->warnings[ $file ][ $line ][ $column ][] = $data;
143-
++$this->warning_count;
144158
}
145159
}
146160

includes/Checker/Check_Types.php

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
<?php
2+
/**
3+
* Class WordPress\Plugin_Check\Checker\Check_Categories
4+
*
5+
* @package plugin-check
6+
*/
7+
8+
namespace WordPress\Plugin_Check\Checker;
9+
10+
/**
11+
* Check Categories class.
12+
*
13+
* @since 1.0.0
14+
*/
15+
class Check_Types {
16+
17+
// Constants for available check types.
18+
const TYPE_ERROR = 'error';
19+
const TYPE_WARNING = 'warning';
20+
21+
/**
22+
* Returns an array of check types.
23+
*
24+
* @since 1.7.0
25+
*
26+
* @return array An array of check types.
27+
*/
28+
public static function get_types() {
29+
$default_types = array(
30+
self::TYPE_ERROR => __( 'Error', 'plugin-check' ),
31+
self::TYPE_WARNING => __( 'Warning', 'plugin-check' ),
32+
);
33+
34+
/**
35+
* Filters the check types.
36+
*
37+
* @since 1.7.0
38+
*
39+
* @param array<string, string> $default_categories Associative array of type slugs to labels.
40+
*/
41+
$check_types = (array) apply_filters( 'wp_plugin_check_types', $default_types );
42+
43+
return $check_types;
44+
}
45+
46+
47+
/**
48+
* Returns an array of available types.
49+
*
50+
* @since 1.7.0
51+
*
52+
* @return array An array of available types.
53+
*/
54+
public static function get_type_slugs() {
55+
return array_keys( self::get_types() );
56+
}
57+
}

includes/Checker/Checks.php

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,17 +27,15 @@ final class Checks {
2727
/**
2828
* Runs checks against the plugin.
2929
*
30-
* @since 1.0.0
31-
*
3230
* @param Check_Context $context The check context for the plugin to be checked.
3331
* @param array $checks An array of Check objects to run.
3432
* @param Check_Runner $runner The runner instance that created this result.
35-
* @return Check_Result Object containing all check results.
36-
*
33+
* @param array $check_types An array of check types to filter.
3734
* @throws Exception Thrown when check fails with critical error.
35+
* @since 1.0.0
3836
*/
39-
public function run_checks( Check_Context $context, array $checks, ?Check_Runner $runner = null ) {
40-
$result = new Check_Result( $context );
37+
public function run_checks( Check_Context $context, array $checks, ?Check_Runner $runner = null, array $check_types = array() ) {
38+
$result = new Check_Result( $context, $check_types );
4139

4240
// Run the checks.
4341
array_walk(

0 commit comments

Comments
 (0)