From 4d0437e1c684bc0c21edb8556c63eb62a02a1c38 Mon Sep 17 00:00:00 2001 From: w3bdesign <45217974+w3bdesign@users.noreply.github.com> Date: Sun, 30 Apr 2023 00:17:00 +0200 Subject: [PATCH 1/2] Refactor --- classes/class-algolia-woo-indexer.php | 175 +++++++++++--------------- 1 file changed, 70 insertions(+), 105 deletions(-) diff --git a/classes/class-algolia-woo-indexer.php b/classes/class-algolia-woo-indexer.php index e098419..eeef80d 100644 --- a/classes/class-algolia-woo-indexer.php +++ b/classes/class-algolia-woo-indexer.php @@ -1,4 +1,5 @@ 'string', @@ -92,34 +93,34 @@ public static function setup_settings_sections() add_settings_section( 'algolia_woo_indexer_main', esc_html__('Algolia Woo Plugin Settings', 'algolia-woo-indexer'), - array( $algowooindexer, 'algolia_woo_indexer_section_text' ), + array($algowooindexer, 'algolia_woo_indexer_section_text'), 'algolia_woo_indexer' ); add_settings_field( 'algolia_woo_indexer_application_id', esc_html__('Application ID', 'algolia-woo-indexer'), - array( $algowooindexer, 'algolia_woo_indexer_application_id_output' ), + array($algowooindexer, 'algolia_woo_indexer_application_id_output'), 'algolia_woo_indexer', 'algolia_woo_indexer_main' ); add_settings_field( 'algolia_woo_indexer_admin_api_key', esc_html__('Admin API Key', 'algolia-woo-indexer'), - array( $algowooindexer, 'algolia_woo_indexer_admin_api_key_output' ), + array($algowooindexer, 'algolia_woo_indexer_admin_api_key_output'), 'algolia_woo_indexer', 'algolia_woo_indexer_main' ); add_settings_field( 'algolia_woo_indexer_index_name', esc_html__('Index name (will be created if not existing)', 'algolia-woo-indexer'), - array( $algowooindexer, 'algolia_woo_indexer_index_name_output' ), + array($algowooindexer, 'algolia_woo_indexer_index_name_output'), 'algolia_woo_indexer', 'algolia_woo_indexer_main' ); add_settings_field( 'algolia_woo_indexer_automatically_send_new_products', esc_html__('Automatically index new products', 'algolia-woo-indexer'), - array( $algowooindexer, 'algolia_woo_indexer_automatically_send_new_products_output' ), + array($algowooindexer, 'algolia_woo_indexer_automatically_send_new_products_output'), 'algolia_woo_indexer', 'algolia_woo_indexer_main' ); @@ -171,7 +172,7 @@ public static function algolia_woo_indexer_index_name_output() echo ""; } - + /** * Output for checkbox to check if we automatically send new products to Algolia * @@ -184,10 +185,9 @@ public static function algolia_woo_indexer_automatically_send_new_products_outpu * But I have still done it to be 100% safe */ $auto_send = get_option(ALGOWOO_DB_OPTION . AUTOMATICALLY_SEND_NEW_PRODUCTS); - $auto_send = (! empty($auto_send)) ? 1 : 0; ?> - /> - + /> + $sanitized_app_id, + ALGOWOO_DB_OPTION . ALGOLIA_API_KEY => $sanitized_api_key, + ALGOWOO_DB_OPTION . INDEX_NAME => $sanitized_index_name, + ALGOWOO_DB_OPTION . AUTOMATICALLY_SEND_NEW_PRODUCTS => $sanitized_auto_send, + ]; + + foreach ($options as $option_key => $option_value) { + if (isset($option_value) && (!empty($option_value))) { + update_option($option_key, $option_value); + } } + } - if (isset($filtered_api_key) && (! empty($filtered_api_key))) { - update_option( - ALGOWOO_DB_OPTION . ALGOLIA_API_KEY, - $filtered_api_key - ); - } - if (isset($filtered_index_name) && (! empty($filtered_index_name))) { - update_option( - ALGOWOO_DB_OPTION . INDEX_NAME, - $filtered_index_name - ); - } - if (isset($filtered_product) && (! empty($filtered_product))) { - update_option( - ALGOWOO_DB_OPTION . AUTOMATICALLY_SEND_NEW_PRODUCTS, - $filtered_product - ); - } - } /** * Sanitize input in settings fields and filter through regex to accept only a-z and A-Z @@ -412,7 +377,7 @@ public static function admin_menu() esc_html__('Algolia Woo Indexer Settings', 'algolia-woo-indexer'), 'manage_options', 'algolia-woo-indexer-settings', - array( get_called_class(), 'algolia_woo_indexer_settings' ) + array(get_called_class(), 'algolia_woo_indexer_settings') ); } @@ -424,26 +389,26 @@ public static function admin_menu() public static function algolia_woo_indexer_settings() { /** - * Verify that the user can access the settings page - */ - if (! current_user_can('manage_options')) { + * Verify that the user can access the settings page + */ + if (!current_user_can('manage_options')) { wp_die(esc_html__('Action not allowed.', 'algolia_woo_indexer_settings')); } ?> -