Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement persisting the enabled check categories checkboxes in user settings #304

Merged
merged 6 commits into from
Oct 26, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
mukeshpanchal27 marked this conversation as resolved.
Show resolved Hide resolved
*/
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( '__' );
felixarntz marked this conversation as resolved.
Show resolved Hide resolved
window.setUserSetting( 'category_preferences', settingValue );
mukeshpanchal27 marked this conversation as resolved.
Show resolved Hide resolved
}

// 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;
mukeshpanchal27 marked this conversation as resolved.
Show resolved Hide resolved

$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' );
mukeshpanchal27 marked this conversation as resolved.
Show resolved Hide resolved

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;
}
felixarntz marked this conversation as resolved.
Show resolved Hide resolved
?>
<input type="checkbox" id="<?php echo esc_attr( $category ); ?>" name="categories" value="<?php echo esc_attr( $category ); ?>"<?php echo ( $checked ? ' checked="checked"' : '' ); ?> />
mukeshpanchal27 marked this conversation as resolved.
Show resolved Hide resolved
<?php echo esc_html( ucfirst( str_replace( '_', ' ', $category ) ) ); ?>
</label>
</fieldset>
Expand Down