11<?php
2+
23/**
34 * Main Algolia Woo Indexer class
45 * Called from main plugin file algolia-woo-indexer.php
1516/**
1617 * Abort if this file is called directly
1718 */
18- if ( ! defined ( 'ABSPATH ' ) ) {
19- exit ;
19+ if (! defined ('ABSPATH ' ) ) {
20+ exit ;
2021}
2122
2223/**
2324 * Include plugin file if function is_plugin_active does not exist
2425 */
25- if (! function_exists ('is_plugin_active ' )) {
26+ if (!function_exists ('is_plugin_active ' )) {
2627 require_once (ABSPATH . '/wp-admin/includes/plugin.php ' );
2728}
2829
29- if (! class_exists ('Algolia_Woo_Indexer ' )) {
30+ if (!class_exists ('Algolia_Woo_Indexer ' )) {
3031 /**
3132 * Algolia WooIndexer main class
3233 */
3334 // TODO Rename class "Algolia_Woo_Indexer" to match the regular expression ^[A-Z][a-zA-Z0-9]*$.
34- class Algolia_Woo_Indexer
35+ class Algolia_Woo_Indexer
3536 {
3637 const PLUGIN_NAME = 'Algolia Woo Indexer ' ;
3738 const PLUGIN_TRANSIENT = 'algowoo-plugin-notice ' ;
@@ -68,10 +69,10 @@ public function __construct()
6869 public static function setup_settings_sections ()
6970 {
7071 /**
71- * Setup arguments for settings sections and fields
72- *
73- * @see https://developer.wordpress.org/reference/functions/register_setting/
74- */
72+ * Setup arguments for settings sections and fields
73+ *
74+ * @see https://developer.wordpress.org/reference/functions/register_setting/
75+ */
7576 if (is_admin ()) {
7677 $ arguments = array (
7778 'type ' => 'string ' ,
@@ -92,34 +93,34 @@ public static function setup_settings_sections()
9293 add_settings_section (
9394 'algolia_woo_indexer_main ' ,
9495 esc_html__ ('Algolia Woo Plugin Settings ' , 'algolia-woo-indexer ' ),
95- array ( $ algowooindexer , 'algolia_woo_indexer_section_text ' ),
96+ array ($ algowooindexer , 'algolia_woo_indexer_section_text ' ),
9697 'algolia_woo_indexer '
9798 );
9899 add_settings_field (
99100 'algolia_woo_indexer_application_id ' ,
100101 esc_html__ ('Application ID ' , 'algolia-woo-indexer ' ),
101- array ( $ algowooindexer , 'algolia_woo_indexer_application_id_output ' ),
102+ array ($ algowooindexer , 'algolia_woo_indexer_application_id_output ' ),
102103 'algolia_woo_indexer ' ,
103104 'algolia_woo_indexer_main '
104105 );
105106 add_settings_field (
106107 'algolia_woo_indexer_admin_api_key ' ,
107108 esc_html__ ('Admin API Key ' , 'algolia-woo-indexer ' ),
108- array ( $ algowooindexer , 'algolia_woo_indexer_admin_api_key_output ' ),
109+ array ($ algowooindexer , 'algolia_woo_indexer_admin_api_key_output ' ),
109110 'algolia_woo_indexer ' ,
110111 'algolia_woo_indexer_main '
111112 );
112113 add_settings_field (
113114 'algolia_woo_indexer_index_name ' ,
114115 esc_html__ ('Index name (will be created if not existing) ' , 'algolia-woo-indexer ' ),
115- array ( $ algowooindexer , 'algolia_woo_indexer_index_name_output ' ),
116+ array ($ algowooindexer , 'algolia_woo_indexer_index_name_output ' ),
116117 'algolia_woo_indexer ' ,
117118 'algolia_woo_indexer_main '
118119 );
119120 add_settings_field (
120121 'algolia_woo_indexer_automatically_send_new_products ' ,
121122 esc_html__ ('Automatically index new products ' , 'algolia-woo-indexer ' ),
122- array ( $ algowooindexer , 'algolia_woo_indexer_automatically_send_new_products_output ' ),
123+ array ($ algowooindexer , 'algolia_woo_indexer_automatically_send_new_products_output ' ),
123124 'algolia_woo_indexer ' ,
124125 'algolia_woo_indexer_main '
125126 );
@@ -171,7 +172,7 @@ public static function algolia_woo_indexer_index_name_output()
171172 echo "<input id='algolia_woo_indexer_index_name' name='algolia_woo_indexer_index_name[name]'
172173 type='text' value=' " . esc_attr ($ index_name ) . "' /> " ;
173174 }
174-
175+
175176 /**
176177 * Output for checkbox to check if we automatically send new products to Algolia
177178 *
@@ -184,10 +185,9 @@ public static function algolia_woo_indexer_automatically_send_new_products_outpu
184185 * But I have still done it to be 100% safe
185186 */
186187 $ auto_send = get_option (ALGOWOO_DB_OPTION . AUTOMATICALLY_SEND_NEW_PRODUCTS );
187- $ auto_send = (! empty ($ auto_send )) ? 1 : 0 ; ?>
188- <input id="algolia_woo_indexer_automatically_send_new_products" name="algolia_woo_indexer_automatically_send_new_products[checked]"
189- type="checkbox" <?php checked (1 , $ auto_send ); ?> />
190- <?php
188+ $ auto_send = (!empty ($ auto_send )) ? 1 : 0 ; ?>
189+ <input id="algolia_woo_indexer_automatically_send_new_products" name="algolia_woo_indexer_automatically_send_new_products[checked]" type="checkbox" <?php checked (1 , $ auto_send ); ?> />
190+ <?php
191191 }
192192
193193 /**
@@ -230,7 +230,7 @@ public static function init()
230230 */
231231 Algolia_Check_Requirements::check_unmet_requirements ();
232232
233- if (! Algolia_Check_Requirements::algolia_wp_version_check () || ! Algolia_Check_Requirements::algolia_php_version_check ()) {
233+ if (!Algolia_Check_Requirements::algolia_wp_version_check () || !Algolia_Check_Requirements::algolia_php_version_check ()) {
234234 add_action (
235235 'admin_notices ' ,
236236 function () {
@@ -246,28 +246,28 @@ function () {
246246 /**
247247 * Setup translations
248248 */
249- add_action ('plugins_loaded ' , array ( $ ob_class , 'load_textdomain ' ));
249+ add_action ('plugins_loaded ' , array ($ ob_class , 'load_textdomain ' ));
250250
251251 /**
252252 * Add actions to setup admin menu
253253 */
254254 if (is_admin ()) {
255- add_action ('admin_menu ' , array ( $ ob_class , 'admin_menu ' ));
256- add_action ('admin_init ' , array ( $ ob_class , 'setup_settings_sections ' ));
257- add_action ('admin_init ' , array ( $ ob_class , 'update_settings_options ' ));
258- add_action ('admin_init ' , array ( $ ob_class , 'maybe_send_products ' ));
255+ add_action ('admin_menu ' , array ($ ob_class , 'admin_menu ' ));
256+ add_action ('admin_init ' , array ($ ob_class , 'setup_settings_sections ' ));
257+ add_action ('admin_init ' , array ($ ob_class , 'update_settings_options ' ));
258+ add_action ('admin_init ' , array ($ ob_class , 'maybe_send_products ' ));
259259
260260 /**
261261 * Register hook to automatically send new products if the option is set
262262 */
263263
264264 if ('1 ' === $ auto_send ) {
265- add_action ('save_post ' , array ( $ ob_class , 'send_new_product_to_algolia ' ), 10 , 3 );
265+ add_action ('save_post ' , array ($ ob_class , 'send_new_product_to_algolia ' ), 10 , 3 );
266266 }
267267
268268 self ::$ plugin_url = admin_url ('options-general.php?page=algolia-woo-indexer-settings ' );
269269
270- if (! is_plugin_active ('woocommerce/woocommerce.php ' )) {
270+ if (!is_plugin_active ('woocommerce/woocommerce.php ' )) {
271271 add_action (
272272 'admin_notices ' ,
273273 function () {
@@ -306,71 +306,36 @@ public static function update_settings_options()
306306 {
307307 Algolia_Verify_Nonces::verify_settings_nonce ();
308308
309- /**
310- * Do not proceed if we are going to send products
311- */
312- if (true === Algolia_Verify_Nonces::verify_send_products_nonce ()) {
309+ if (Algolia_Verify_Nonces::verify_send_products_nonce ()) {
313310 return ;
314311 }
315312
316- /**
317- * Filter the application id, api key, index name and verify that the input is an array
318- *
319- * @see https://www.php.net/manual/en/function.filter-input.php
320- */
321- $ post_application_id = filter_input (INPUT_POST , 'algolia_woo_indexer_application_id ' , FILTER_DEFAULT , FILTER_REQUIRE_ARRAY );
322- $ post_api_key = filter_input (INPUT_POST , 'algolia_woo_indexer_admin_api_key ' , FILTER_DEFAULT , FILTER_REQUIRE_ARRAY );
323- $ post_index_name = filter_input (INPUT_POST , 'algolia_woo_indexer_index_name ' , FILTER_DEFAULT , FILTER_REQUIRE_ARRAY );
313+ $ application_id = filter_input (INPUT_POST , 'algolia_woo_indexer_application_id ' , FILTER_DEFAULT , FILTER_REQUIRE_ARRAY );
314+ $ api_key = filter_input (INPUT_POST , 'algolia_woo_indexer_admin_api_key ' , FILTER_DEFAULT , FILTER_REQUIRE_ARRAY );
315+ $ index_name = filter_input (INPUT_POST , 'algolia_woo_indexer_index_name ' , FILTER_DEFAULT , FILTER_REQUIRE_ARRAY );
324316 $ auto_send = filter_input (INPUT_POST , 'algolia_woo_indexer_automatically_send_new_products ' , FILTER_DEFAULT , FILTER_REQUIRE_ARRAY );
325317
326- /**
327- * Properly sanitize text fields before updating data
328- *
329- * @see https://developer.wordpress.org/reference/functions/sanitize_text_field/
330- */
331- $ filtered_app_id = sanitize_text_field ($ post_application_id ['id ' ]);
332- $ filtered_api_key = sanitize_text_field ($ post_api_key ['key ' ]);
333- $ filtered_index_name = sanitize_text_field ($ post_index_name ['name ' ]);
334-
335- /**
336- * Sanitizing by setting the value to either 1 or 0
337- */
338- $ filtered_product = (! empty ($ auto_send )) ? 1 : 0 ;
339-
340- /**
341- * Values have been filtered and sanitized
342- * Check if set and not empty and update the database
343- *
344- * @see https://developer.wordpress.org/reference/functions/update_option/
345- */
346- if (isset ($ filtered_app_id ) && (! empty ($ filtered_app_id ))) {
347- update_option (
348- ALGOWOO_DB_OPTION . ALGOLIA_APP_ID ,
349- $ filtered_app_id
350- );
318+ $ sanitized_app_id = sanitize_text_field ($ application_id ['id ' ]);
319+ $ sanitized_api_key = sanitize_text_field ($ api_key ['key ' ]);
320+ $ sanitized_index_name = sanitize_text_field ($ index_name ['name ' ]);
321+ $ sanitized_auto_send = (!empty ($ auto_send )) ? 1 : 0 ;
322+
323+ $ options = [
324+ ALGOWOO_DB_OPTION . ALGOLIA_APP_ID => $ sanitized_app_id ,
325+ ALGOWOO_DB_OPTION . ALGOLIA_API_KEY => $ sanitized_api_key ,
326+ ALGOWOO_DB_OPTION . INDEX_NAME => $ sanitized_index_name ,
327+ ALGOWOO_DB_OPTION . AUTOMATICALLY_SEND_NEW_PRODUCTS => $ sanitized_auto_send ,
328+ ];
329+
330+ foreach ($ options as $ option_key => $ option_value ) {
331+ if (isset ($ option_value ) && (!empty ($ option_value ))) {
332+ update_option ($ option_key , $ option_value );
333+ }
351334 }
335+ }
352336
353- if (isset ($ filtered_api_key ) && (! empty ($ filtered_api_key ))) {
354- update_option (
355- ALGOWOO_DB_OPTION . ALGOLIA_API_KEY ,
356- $ filtered_api_key
357- );
358- }
359337
360- if (isset ($ filtered_index_name ) && (! empty ($ filtered_index_name ))) {
361- update_option (
362- ALGOWOO_DB_OPTION . INDEX_NAME ,
363- $ filtered_index_name
364- );
365- }
366338
367- if (isset ($ filtered_product ) && (! empty ($ filtered_product ))) {
368- update_option (
369- ALGOWOO_DB_OPTION . AUTOMATICALLY_SEND_NEW_PRODUCTS ,
370- $ filtered_product
371- );
372- }
373- }
374339
375340 /**
376341 * 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()
412377 esc_html__ ('Algolia Woo Indexer Settings ' , 'algolia-woo-indexer ' ),
413378 'manage_options ' ,
414379 'algolia-woo-indexer-settings ' ,
415- array ( get_called_class (), 'algolia_woo_indexer_settings ' )
380+ array (get_called_class (), 'algolia_woo_indexer_settings ' )
416381 );
417382 }
418383
@@ -424,26 +389,26 @@ public static function admin_menu()
424389 public static function algolia_woo_indexer_settings ()
425390 {
426391 /**
427- * Verify that the user can access the settings page
428- */
429- if (! current_user_can ('manage_options ' )) {
392+ * Verify that the user can access the settings page
393+ */
394+ if (!current_user_can ('manage_options ' )) {
430395 wp_die (esc_html__ ('Action not allowed. ' , 'algolia_woo_indexer_settings ' ));
431396 } ?>
432- <div class="wrap">
433- <h1><?php esc_html__ ('Algolia Woo Indexer Settings ' , 'algolia-woo-indexer ' ); ?> </h1>
434- <form action="<?php echo esc_url (self ::$ plugin_url ); ?> " method="POST">
435- <?php
436- settings_fields ('algolia_woo_options ' );
437- do_settings_sections ('algolia_woo_indexer ' );
438- submit_button ('' , 'primary wide ' ); ?>
439- </form>
440- <form action="<?php echo esc_url (self ::$ plugin_url ); ?> " method="POST">
441- <?php wp_nonce_field ('send_products_to_algolia_nonce_action ' , 'send_products_to_algolia_nonce_name ' ); ?>
442- <input type="hidden" name="send_products_to_algolia" id="send_products_to_algolia" value="true" />
443- <?php submit_button (esc_html__ ('Send products to Algolia ' , 'algolia_woo_indexer_settings ' ), 'primary wide ' , '' , false ); ?>
444- </form>
445- </div>
446- <?php
397+ <div class="wrap">
398+ <h1><?php esc_html__ ('Algolia Woo Indexer Settings ' , 'algolia-woo-indexer ' ); ?> </h1>
399+ <form action="<?php echo esc_url (self ::$ plugin_url ); ?> " method="POST">
400+ <?php
401+ settings_fields ('algolia_woo_options ' );
402+ do_settings_sections ('algolia_woo_indexer ' );
403+ submit_button ('' , 'primary wide ' ); ?>
404+ </form>
405+ <form action="<?php echo esc_url (self ::$ plugin_url ); ?> " method="POST">
406+ <?php wp_nonce_field ('send_products_to_algolia_nonce_action ' , 'send_products_to_algolia_nonce_name ' ); ?>
407+ <input type="hidden" name="send_products_to_algolia" id="send_products_to_algolia" value="true" />
408+ <?php submit_button (esc_html__ ('Send products to Algolia ' , 'algolia_woo_indexer_settings ' ), 'primary wide ' , '' , false ); ?>
409+ </form>
410+ </div>
411+ <?php
447412 }
448413
449414 /**
@@ -453,7 +418,7 @@ public static function algolia_woo_indexer_settings()
453418 */
454419 public static function get_instance ()
455420 {
456- if (! self ::$ instance ) {
421+ if (!self ::$ instance ) {
457422 self ::$ instance = new Algolia_Woo_Indexer ();
458423 }
459424 return self ::$ instance ;
@@ -474,7 +439,7 @@ public static function activate_plugin()
474439 $ algolia_application_id = get_option (ALGOWOO_DB_OPTION . ALGOLIA_APP_ID );
475440 $ algolia_api_key = get_option (ALGOWOO_DB_OPTION . ALGOLIA_API_KEY );
476441 $ algolia_index_name = get_option (ALGOWOO_DB_OPTION . INDEX_NAME );
477-
442+
478443 if (empty ($ auto_send )) {
479444 add_option (
480445 ALGOWOO_DB_OPTION . AUTOMATICALLY_SEND_NEW_PRODUCTS ,
@@ -515,4 +480,4 @@ public static function deactivate_plugin()
515480 delete_transient (self ::PLUGIN_TRANSIENT );
516481 }
517482 }
518- }
483+ }
0 commit comments