Skip to content

Commit

Permalink
Save categories value
Browse files Browse the repository at this point in the history
  • Loading branch information
mukeshpanchal27 committed Oct 24, 2023
1 parent cc51eb5 commit 1bc96bc
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 2 deletions.
25 changes: 25 additions & 0 deletions assets/js/plugin-check-admin.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,31 @@
canRunChecks();
pluginsList.addEventListener( 'change', canRunChecks );

/**
* Saves the user settings.
*
* @since n.e.x.t
*/
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( '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
9 changes: 8 additions & 1 deletion includes/Admin/Admin_Page.php
Original file line number Diff line number Diff line change
Expand Up @@ -150,16 +150,23 @@ private function get_available_plugins() {
* Renders the "Plugin Check" page.
*
* @since n.e.x.t
*
* @global array $available_plugins The list of available plugins.
* @global string $selected_plugin_basename The selected plugin basename.
* @global array $categories An array of categories.
* @global mixed $user_settings The user interface setting value.
*/
public function render_page() {
global $available_plugins, $selected_plugin_basename, $categories;
global $available_plugins, $selected_plugin_basename, $categories, $user_settings;

$available_plugins = $this->get_available_plugins();

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

$categories = Check_Categories::get_categories();

$user_settings = get_user_setting( 'category_preferences' );

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

Expand Down
13 changes: 12 additions & 1 deletion templates/admin-page.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,18 @@
<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" />
<?php
$checked = false;
if ( $user_settings ) {
$selected_categories = explode( '__', $user_settings );
if ( in_array( $category, $selected_categories, true ) ) {
$checked = true;
}
} else {
$checked = true;
}
?>
<input type="checkbox" id="<?php echo esc_attr( $category ); ?>" name="categories" value="<?php echo esc_attr( $category ); ?>"<?php echo ( $checked ? ' checked="checked"' : '' ); ?> />
<?php echo esc_html( ucfirst( str_replace( '_', ' ', $category ) ) ); ?>
</label>
</fieldset>
Expand Down

0 comments on commit 1bc96bc

Please sign in to comment.