Skip to content

Commit

Permalink
Merge pull request #304 from WordPress/fix/299-save-categories-value
Browse files Browse the repository at this point in the history
Implement persisting the enabled check categories checkboxes in user settings
  • Loading branch information
felixarntz authored Oct 26, 2023
2 parents 38c1559 + 423147b commit f526b60
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 3 deletions.
23 changes: 23 additions & 0 deletions assets/js/plugin-check-admin.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,29 @@
canRunChecks();
pluginsList.addEventListener( 'change', canRunChecks );

function saveUserSettings() {
const selectedCategories = [];

// Assuming you have a list of category checkboxes, find the selected ones.
categoriesList.forEach( function ( checkbox ) {
if ( checkbox.checked ) {
selectedCategories.push( checkbox.value );
}
} );

// Join the selected category slugs with '__' and save it as a user setting.
const settingValue = selectedCategories.join( '__' );
window.setUserSetting(
'plugin_check_category_preferences',
settingValue
);
}

// Attach the saveUserSettings function when a category checkbox is clicked.
categoriesList.forEach( function ( checkbox ) {
checkbox.addEventListener( 'change', saveUserSettings );
} );

// When the Check it button is clicked.
checkItButton.addEventListener( 'click', ( e ) => {
e.preventDefault();
Expand Down
8 changes: 6 additions & 2 deletions includes/Admin/Admin_Page.php
Original file line number Diff line number Diff line change
Expand Up @@ -150,16 +150,20 @@ private function get_available_plugins() {
* Renders the "Plugin Check" page.
*
* @since n.e.x.t
*
* @SuppressWarnings(PHPMD.UnusedLocalVariable)
*/
public function render_page() {
global $available_plugins, $selected_plugin_basename, $categories;

$available_plugins = $this->get_available_plugins();

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

$categories = Check_Categories::get_categories();

// 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 );

require WP_PLUGIN_CHECK_PLUGIN_DIR_PATH . 'templates/admin-page.php';
}

Expand Down
2 changes: 1 addition & 1 deletion templates/admin-page.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
<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 ); ?>" checked="checked" />
<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 ) ) ); ?>
</label>
</fieldset>
Expand Down

0 comments on commit f526b60

Please sign in to comment.