From 1bc96bcc00076180937b4926bd3a78080f725af2 Mon Sep 17 00:00:00 2001 From: Mukesh Panchal Date: Tue, 24 Oct 2023 16:56:51 +0530 Subject: [PATCH] Save categories value --- assets/js/plugin-check-admin.js | 25 +++++++++++++++++++++++++ includes/Admin/Admin_Page.php | 9 ++++++++- templates/admin-page.php | 13 ++++++++++++- 3 files changed, 45 insertions(+), 2 deletions(-) diff --git a/assets/js/plugin-check-admin.js b/assets/js/plugin-check-admin.js index 089b32bf8..962b0a359 100644 --- a/assets/js/plugin-check-admin.js +++ b/assets/js/plugin-check-admin.js @@ -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(); diff --git a/includes/Admin/Admin_Page.php b/includes/Admin/Admin_Page.php index 51588bdef..40ef5765a 100644 --- a/includes/Admin/Admin_Page.php +++ b/includes/Admin/Admin_Page.php @@ -150,9 +150,14 @@ 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(); @@ -160,6 +165,8 @@ public function render_page() { $categories = Check_Categories::get_categories(); + $user_settings = get_user_setting( 'category_preferences' ); + require WP_PLUGIN_CHECK_PLUGIN_DIR_PATH . 'templates/admin-page.php'; } diff --git a/templates/admin-page.php b/templates/admin-page.php index 2188fa656..a9dd14648 100644 --- a/templates/admin-page.php +++ b/templates/admin-page.php @@ -45,7 +45,18 @@