Skip to content

Commit

Permalink
Merge pull request #375 from ernilambar/373-introduce-category-labels
Browse files Browse the repository at this point in the history
Introduce check category labels
  • Loading branch information
felixarntz authored Jan 6, 2024
2 parents 4df6899 + e79b2d4 commit 43c4af9
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 37 deletions.
4 changes: 2 additions & 2 deletions includes/Admin/Admin_Page.php
Original file line number Diff line number Diff line change
Expand Up @@ -194,11 +194,11 @@ public function render_page() {

$selected_plugin_basename = filter_input( INPUT_GET, 'plugin', FILTER_SANITIZE_FULL_SPECIAL_CHARS );

$categories = Check_Categories::get_categories();
$category_labels = Check_Categories::get_category_labels();

// Get user settings for category preferences and set a default value to check all categories by default.
$user_enabled_categories = get_user_setting( 'plugin_check_category_preferences', 'all_categories' );
$user_enabled_categories = 'all_categories' === $user_enabled_categories ? $categories : explode( '__', $user_enabled_categories );
$user_enabled_categories = 'all_categories' === $user_enabled_categories ? array_keys( $category_labels ) : explode( '__', $user_enabled_categories );

require WP_PLUGIN_CHECK_PLUGIN_DIR_PATH . 'templates/admin-page.php';
}
Expand Down
23 changes: 12 additions & 11 deletions includes/CLI/Plugin_Check_Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -354,17 +354,18 @@ public function list_check_categories( $args, $assoc_args ) {
*/
private function get_check_categories() {
$check_categories = new Check_Categories();
$categories_slugs = $check_categories->get_categories();

return array_map(
function ( $slug ) {
return array(
'slug' => $slug,
'name' => ucfirst( str_replace( '_', ' ', $slug ) ),
);
},
$categories_slugs
);
$category_labels = $check_categories->get_category_labels();

$categories = array();

foreach ( $category_labels as $slug => $label ) {
$categories[] = array(
'slug' => $slug,
'name' => $label,
);
}

return $categories;
}

/**
Expand Down
43 changes: 23 additions & 20 deletions includes/Checker/Check_Categories.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,27 +29,30 @@ class Check_Categories {
* @return array An array of available categories.
*/
public static function get_categories() {
static $categories = '';
if ( ! $categories ) {
$constants = ( new \ReflectionClass( __CLASS__ ) )->getConstants();

/**
* List of categories.
*
* @var string[] $categories
*/
$categories = array_values(
array_filter(
$constants,
static function ( $key ) {
return strpos( $key, 'CATEGORY_' ) === 0;
},
ARRAY_FILTER_USE_KEY
)
);
}
return array(
self::CATEGORY_GENERAL,
self::CATEGORY_PLUGIN_REPO,
self::CATEGORY_SECURITY,
self::CATEGORY_PERFORMANCE,
self::CATEGORY_ACCESSIBILITY,
);
}

return $categories;
/**
* Returns an array of category labels.
*
* @since n.e.x.t
*
* @return array An array of category labels.
*/
public static function get_category_labels() {
return array(
self::CATEGORY_GENERAL => __( 'General', 'plugin-check' ),
self::CATEGORY_PLUGIN_REPO => __( 'Plugin Repo', 'plugin-check' ),
self::CATEGORY_SECURITY => __( 'Security', 'plugin-check' ),
self::CATEGORY_PERFORMANCE => __( 'Performance', 'plugin-check' ),
self::CATEGORY_ACCESSIBILITY => __( 'Accessibility', 'plugin-check' ),
);
}

/**
Expand Down
6 changes: 3 additions & 3 deletions templates/admin-page.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,18 +37,18 @@
<span id="plugin-check__spinner" class="spinner" style="float: none;"></span>
<h4><?php esc_attr_e( 'Categories', 'plugin-check' ); ?></h4>
<?php
if ( ! empty( $categories ) ) {
if ( ! empty( $category_labels ) ) {
?>
<table>
<?php
foreach ( $categories as $category ) { ?>
foreach ( $category_labels as $category => $label ) { ?>
<tr>
<td>
<fieldset>
<legend class="screen-reader-text"><?php echo esc_html( $category ); ?></legend>
<label for="<?php echo esc_attr( $category ); ?>">
<input type="checkbox" id="<?php echo esc_attr( $category ); ?>" name="categories" value="<?php echo esc_attr( $category ); ?>" <?php checked( in_array( $category, $user_enabled_categories, true ) ); ?> />
<?php echo esc_html( ucfirst( str_replace( '_', ' ', $category ) ) ); ?>
<?php echo esc_html( $label ); ?>
</label>
</fieldset>
</td>
Expand Down
2 changes: 1 addition & 1 deletion tests/behat/features/plugin-list-check-categories.feature
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,5 @@ Feature: Test that the WP-CLI plugin list check categories command works.
When I run the WP-CLI command `plugin list-check-categories --format=csv --fields=slug,name`
Then STDOUT should contain:
"""
plugin_repo,"Plugin repo"
plugin_repo,"Plugin Repo"
"""
10 changes: 10 additions & 0 deletions tests/phpunit/tests/Checker/Check_Categories_Tests.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,16 @@ public function test_get_categories() {
}
}

public function test_get_category_labels() {
$check_categories = new Check_Categories();
$categories = $check_categories->get_categories();
$category_labels = $check_categories->get_category_labels();

$this->assertIsArray( $category_labels );
$this->assertNotEmpty( $category_labels );
$this->assertSame( $categories, array_keys( $category_labels ) );
}

/**
* @dataProvider data_checks_by_categories
*/
Expand Down

0 comments on commit 43c4af9

Please sign in to comment.