From 1bc96bcc00076180937b4926bd3a78080f725af2 Mon Sep 17 00:00:00 2001 From: Mukesh Panchal Date: Tue, 24 Oct 2023 16:56:51 +0530 Subject: [PATCH 1/6] 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 @@
From 678e5341421ec207b56bd1c6c2cd4db93a3fb157 Mon Sep 17 00:00:00 2001 From: Mukesh Panchal Date: Wed, 25 Oct 2023 10:16:38 +0530 Subject: [PATCH 2/6] Address review feedback --- assets/js/plugin-check-admin.js | 7 +------ includes/Admin/Admin_Page.php | 4 +++- templates/admin-page.php | 13 +------------ 3 files changed, 5 insertions(+), 19 deletions(-) diff --git a/assets/js/plugin-check-admin.js b/assets/js/plugin-check-admin.js index 962b0a359..9b971588e 100644 --- a/assets/js/plugin-check-admin.js +++ b/assets/js/plugin-check-admin.js @@ -35,11 +35,6 @@ canRunChecks(); pluginsList.addEventListener( 'change', canRunChecks ); - /** - * Saves the user settings. - * - * @since n.e.x.t - */ function saveUserSettings() { const selectedCategories = []; @@ -52,7 +47,7 @@ // Join the selected category slugs with '__' and save it as a user setting. const settingValue = selectedCategories.join( '__' ); - window.setUserSetting( 'category_preferences', settingValue ); + window.setUserSetting( 'plugin_check_category_preferences', settingValue ); } // Attach the saveUserSettings function when a category checkbox is clicked. diff --git a/includes/Admin/Admin_Page.php b/includes/Admin/Admin_Page.php index 40ef5765a..9b070c74a 100644 --- a/includes/Admin/Admin_Page.php +++ b/includes/Admin/Admin_Page.php @@ -165,7 +165,9 @@ public function render_page() { $categories = Check_Categories::get_categories(); - $user_settings = get_user_setting( 'category_preferences' ); + // Get user settings for category preferences and set a default value to check all categories by default. + $user_settings = get_user_setting( 'plugin_check_category_preferences', 'all_categories' ); + $user_settings = explode( '__', $user_settings ); require WP_PLUGIN_CHECK_PLUGIN_DIR_PATH . 'templates/admin-page.php'; } diff --git a/templates/admin-page.php b/templates/admin-page.php index a9dd14648..905a5b138 100644 --- a/templates/admin-page.php +++ b/templates/admin-page.php @@ -45,18 +45,7 @@
From 9ffdd9da75d5895b93715e8c2813f03365b98475 Mon Sep 17 00:00:00 2001 From: Mukesh Panchal Date: Wed, 25 Oct 2023 10:20:00 +0530 Subject: [PATCH 3/6] Fix JS lint --- assets/js/plugin-check-admin.js | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/assets/js/plugin-check-admin.js b/assets/js/plugin-check-admin.js index 9b971588e..086eef5c4 100644 --- a/assets/js/plugin-check-admin.js +++ b/assets/js/plugin-check-admin.js @@ -47,7 +47,10 @@ // Join the selected category slugs with '__' and save it as a user setting. const settingValue = selectedCategories.join( '__' ); - window.setUserSetting( 'plugin_check_category_preferences', settingValue ); + window.setUserSetting( + 'plugin_check_category_preferences', + settingValue + ); } // Attach the saveUserSettings function when a category checkbox is clicked. From 8a7bea21d60e18d0e5c9bbbb9149d1828ca9ba17 Mon Sep 17 00:00:00 2001 From: Mukesh Panchal Date: Wed, 25 Oct 2023 10:25:42 +0530 Subject: [PATCH 4/6] Remove global variable --- includes/Admin/Admin_Page.php | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/includes/Admin/Admin_Page.php b/includes/Admin/Admin_Page.php index 9b070c74a..bf5c3d020 100644 --- a/includes/Admin/Admin_Page.php +++ b/includes/Admin/Admin_Page.php @@ -151,14 +151,9 @@ private function get_available_plugins() { * * @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. + * @SuppressWarnings(PHPMD.UnusedLocalVariable) */ public function render_page() { - 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 ); From 5b63e14e8e8469fd15286c2af858c1929e758a72 Mon Sep 17 00:00:00 2001 From: Mukesh Panchal Date: Thu, 26 Oct 2023 11:41:12 +0530 Subject: [PATCH 5/6] Thanks @felixarntz for suggestion Co-authored-by: Felix Arntz --- includes/Admin/Admin_Page.php | 2 +- templates/admin-page.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/includes/Admin/Admin_Page.php b/includes/Admin/Admin_Page.php index bf5c3d020..c36f81c00 100644 --- a/includes/Admin/Admin_Page.php +++ b/includes/Admin/Admin_Page.php @@ -162,7 +162,7 @@ public function render_page() { // Get user settings for category preferences and set a default value to check all categories by default. $user_settings = get_user_setting( 'plugin_check_category_preferences', 'all_categories' ); - $user_settings = explode( '__', $user_settings ); + $user_settings = 'all_categories' === $user_settings ? $categories : explode( '__', $user_settings ); require WP_PLUGIN_CHECK_PLUGIN_DIR_PATH . 'templates/admin-page.php'; } diff --git a/templates/admin-page.php b/templates/admin-page.php index 905a5b138..0f11986ba 100644 --- a/templates/admin-page.php +++ b/templates/admin-page.php @@ -45,7 +45,7 @@
From 423147bee5eeaa78931197ad22d4629c1705c56a Mon Sep 17 00:00:00 2001 From: Mukesh Panchal Date: Thu, 26 Oct 2023 11:45:33 +0530 Subject: [PATCH 6/6] Use a more descriptive variable name --- includes/Admin/Admin_Page.php | 4 ++-- templates/admin-page.php | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/includes/Admin/Admin_Page.php b/includes/Admin/Admin_Page.php index c36f81c00..4e6b61c6b 100644 --- a/includes/Admin/Admin_Page.php +++ b/includes/Admin/Admin_Page.php @@ -161,8 +161,8 @@ public function render_page() { $categories = Check_Categories::get_categories(); // Get user settings for category preferences and set a default value to check all categories by default. - $user_settings = get_user_setting( 'plugin_check_category_preferences', 'all_categories' ); - $user_settings = 'all_categories' === $user_settings ? $categories : explode( '__', $user_settings ); + $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'; } diff --git a/templates/admin-page.php b/templates/admin-page.php index 0f11986ba..37445e1c8 100644 --- a/templates/admin-page.php +++ b/templates/admin-page.php @@ -45,7 +45,7 @@