| 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_Categories { |
| 16 |
|
| 17 |
// Constants for available categories. |
| 18 |
const CATEGORY_GENERAL = 'general'; |
| 19 |
const CATEGORY_PLUGIN_REPO = 'plugin_repo'; |
| 20 |
const CATEGORY_SECURITY = 'security'; |
| 21 |
const CATEGORY_PERFORMANCE = 'performance'; |
| 22 |
const CATEGORY_ACCESSIBILITY = 'accessibility'; |
| 23 |
|
| 24 |
/** |
| 25 |
* Returns an array of available categories. |
| 26 |
* |
| 27 |
* @since 1.0.0 |
| 28 |
* |
| 29 |
* @return array An array of available categories. |
| 30 |
*/ |
| 31 |
public static function get_category_slugs() { |
| 32 |
return array_keys( self::get_categories() ); |
| 33 |
} |
| 34 |
|
| 35 |
/** |
| 36 |
* Returns an array of category labels. |
| 37 |
* |
| 38 |
* @since 1.0.0 |
| 39 |
* |
| 40 |
* @return array An array of category labels. |
| 41 |
*/ |
| 42 |
public static function get_category_labels() { |
| 43 |
return array_values( self::get_categories() ); |
| 44 |
} |
| 45 |
|
| 46 |
/** |
| 47 |
* Returns an array of check categories. |
| 48 |
* |
| 49 |
* @since 1.0.2 |
| 50 |
* |
| 51 |
* @return array An array of check categories. |
| 52 |
*/ |
| 53 |
public static function get_categories() { |
| 54 |
$default_categories = array( |
| 55 |
self::CATEGORY_GENERAL => __( 'General', 'plugin-check' ), |
| 56 |
self::CATEGORY_PLUGIN_REPO => __( 'Plugin Repo', 'plugin-check' ), |
| 57 |
self::CATEGORY_SECURITY => __( 'Security', 'plugin-check' ), |
| 58 |
self::CATEGORY_PERFORMANCE => __( 'Performance', 'plugin-check' ), |
| 59 |
self::CATEGORY_ACCESSIBILITY => __( 'Accessibility', 'plugin-check' ), |
| 60 |
); |
| 61 |
|
| 62 |
/** |
| 63 |
* Filters the check categories. |
| 64 |
* |
| 65 |
* @since 1.0.2 |
| 66 |
* |
| 67 |
* @param array<string, string> $default_categories Associative array of category slugs to labels. |
| 68 |
*/ |
| 69 |
$check_categories = (array) apply_filters( 'wp_plugin_check_categories', $default_categories ); |
| 70 |
|
| 71 |
return $check_categories; |
| 72 |
} |
| 73 |
|
| 74 |
/** |
| 75 |
* Returns an array of checks. |
| 76 |
* |
| 77 |
* @since 1.0.0 |
| 78 |
* |
| 79 |
* @param Check_Collection $collection Check collection. |
| 80 |
* @param array $categories An array of categories to filter by. |
| 81 |
* @return Check_Collection Filtered check collection. |
| 82 |
*/ |
| 83 |
public static function filter_checks_by_categories( Check_Collection $collection, array $categories ): Check_Collection { |
| 84 |
return $collection->filter( |
| 85 |
static function ( $check ) use ( $categories ) { |
| 86 |
// Return true if at least one of the check categories is among the filter categories. |
| 87 |
return (bool) array_intersect( $check->get_categories(), $categories ); |
| 88 |
} |
| 89 |
); |
| 90 |
} |
| 91 |
} |
| 92 |
|