@@ -11,11 +11,11 @@ discard block |
||
11 | 11 | /** |
12 | 12 | * Abort if this file is called directly |
13 | 13 | */ |
14 | -if ( ! defined( 'ABSPATH' ) ) { |
|
14 | +if ( ! defined('ABSPATH')) { |
|
15 | 15 | exit; |
16 | 16 | } |
17 | 17 | |
18 | -if ( ! class_exists( 'Algolia_Verify_Nonces' ) ) { |
|
18 | +if ( ! class_exists('Algolia_Verify_Nonces')) { |
|
19 | 19 | /** |
20 | 20 | * Verify submitted nonces |
21 | 21 | */ |
@@ -29,12 +29,12 @@ discard block |
||
29 | 29 | /** |
30 | 30 | * Filter incoming nonces and values |
31 | 31 | */ |
32 | - $settings_nonce = filter_input( INPUT_POST, 'algolia_woo_indexer_admin_api_nonce_name', FILTER_DEFAULT ); |
|
32 | + $settings_nonce = filter_input(INPUT_POST, 'algolia_woo_indexer_admin_api_nonce_name', FILTER_DEFAULT); |
|
33 | 33 | |
34 | 34 | /** |
35 | 35 | * Return boolean depending on if the nonce has been set |
36 | 36 | */ |
37 | - return isset( $settings_nonce ); |
|
37 | + return isset($settings_nonce); |
|
38 | 38 | |
39 | 39 | } |
40 | 40 | |
@@ -47,22 +47,22 @@ discard block |
||
47 | 47 | /** |
48 | 48 | * Filter incoming nonces and values |
49 | 49 | */ |
50 | - $send_products_nonce = filter_input( INPUT_POST, 'send_products_to_algolia_nonce_name', FILTER_DEFAULT ); |
|
51 | - $send_products_to_algolia = filter_input( INPUT_POST, 'send_products_to_algolia', FILTER_DEFAULT ); |
|
50 | + $send_products_nonce = filter_input(INPUT_POST, 'send_products_to_algolia_nonce_name', FILTER_DEFAULT); |
|
51 | + $send_products_to_algolia = filter_input(INPUT_POST, 'send_products_to_algolia', FILTER_DEFAULT); |
|
52 | 52 | |
53 | 53 | /** |
54 | 54 | * Display error and die if nonce is not verified and does not pass security check |
55 | 55 | * Also check if the hidden value field send_products_to_algolia is set |
56 | 56 | */ |
57 | 57 | |
58 | - if ( ! wp_verify_nonce( $send_products_nonce, 'send_products_to_algolia_nonce_action' ) && isset( $send_products_to_algolia ) ) { |
|
59 | - wp_die( esc_html__( 'Action is not allowed.', 'algolia-woo-indexer' ), esc_html__( 'Error!', 'algolia-woo-indexer' ) ); |
|
58 | + if ( ! wp_verify_nonce($send_products_nonce, 'send_products_to_algolia_nonce_action') && isset($send_products_to_algolia)) { |
|
59 | + wp_die(esc_html__('Action is not allowed.', 'algolia-woo-indexer'), esc_html__('Error!', 'algolia-woo-indexer')); |
|
60 | 60 | } |
61 | 61 | |
62 | 62 | /** |
63 | 63 | * If we have verified the send_products_nonce and the send_products hidden field is set, return true |
64 | 64 | */ |
65 | - if ( wp_verify_nonce( $send_products_nonce, 'send_products_to_algolia_nonce_action' ) && isset( $send_products_to_algolia ) ) { |
|
65 | + if (wp_verify_nonce($send_products_nonce, 'send_products_to_algolia_nonce_action') && isset($send_products_to_algolia)) { |
|
66 | 66 | return true; |
67 | 67 | } |
68 | 68 | } |
@@ -17,18 +17,18 @@ discard block |
||
17 | 17 | /** |
18 | 18 | * Abort if this file is called directly |
19 | 19 | */ |
20 | -if (!defined('ABSPATH')) { |
|
20 | +if ( ! defined('ABSPATH')) { |
|
21 | 21 | exit; |
22 | 22 | } |
23 | 23 | |
24 | 24 | /** |
25 | 25 | * Include plugin file if function is_plugin_active does not exist |
26 | 26 | */ |
27 | -if (!function_exists('is_plugin_active')) { |
|
27 | +if ( ! function_exists('is_plugin_active')) { |
|
28 | 28 | require_once(ABSPATH . '/wp-admin/includes/plugin.php'); |
29 | 29 | } |
30 | 30 | |
31 | -if (!class_exists('Algolia_Woo_Indexer')) { |
|
31 | +if ( ! class_exists('Algolia_Woo_Indexer')) { |
|
32 | 32 | /** |
33 | 33 | * Algolia WooIndexer main class |
34 | 34 | */ |
@@ -222,7 +222,7 @@ discard block |
||
222 | 222 | * But I have still done it to be 100% safe |
223 | 223 | */ |
224 | 224 | $auto_send = get_option(ALGOWOO_DB_OPTION . AUTOMATICALLY_SEND_NEW_PRODUCTS); |
225 | - $auto_send = (!empty($auto_send)) ? 1 : 0; ?> |
|
225 | + $auto_send = ( ! empty($auto_send)) ? 1 : 0; ?> |
|
226 | 226 | <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); ?> /> |
227 | 227 | <?php |
228 | 228 | } |
@@ -235,7 +235,7 @@ discard block |
||
235 | 235 | public static function algolia_woo_indexer_field_output($args) |
236 | 236 | { |
237 | 237 | $value = get_option(ALGOWOO_DB_OPTION . BASIC_FIELD_PREFIX . $args["name"]); |
238 | - $isChecked = (!empty($value)) ? 1 : 0; |
|
238 | + $isChecked = ( ! empty($value)) ? 1 : 0; |
|
239 | 239 | ?> |
240 | 240 | <input id="<?php echo $args["label_for"] ?>" name="<?php echo $args["label_for"] ?>[checked]" type="checkbox" <?php checked(1, $isChecked); ?> /> |
241 | 241 | <?php |
@@ -305,10 +305,10 @@ discard block |
||
305 | 305 | */ |
306 | 306 | Algolia_Check_Requirements::check_unmet_requirements(); |
307 | 307 | |
308 | - if (!Algolia_Check_Requirements::algolia_wp_version_check() || !Algolia_Check_Requirements::algolia_php_version_check()) { |
|
308 | + if ( ! Algolia_Check_Requirements::algolia_wp_version_check() || ! Algolia_Check_Requirements::algolia_php_version_check()) { |
|
309 | 309 | add_action( |
310 | 310 | 'admin_notices', |
311 | - function () { |
|
311 | + function() { |
|
312 | 312 | echo '<div class="error notice"> |
313 | 313 | <p>' . esc_html__('Please check the server requirements for Algolia Woo Indexer. <br/> It requires minimum PHP version 7.2 and WordPress version 5.0', 'algolia-woo-indexer') . '</p> |
314 | 314 | </div>'; |
@@ -342,10 +342,10 @@ discard block |
||
342 | 342 | |
343 | 343 | self::$plugin_url = admin_url('options-general.php?page=algolia-woo-indexer-settings'); |
344 | 344 | |
345 | - if (!is_plugin_active('woocommerce/woocommerce.php')) { |
|
345 | + if ( ! is_plugin_active('woocommerce/woocommerce.php')) { |
|
346 | 346 | add_action( |
347 | 347 | 'admin_notices', |
348 | - function () { |
|
348 | + function() { |
|
349 | 349 | echo '<div class="error notice"> |
350 | 350 | <p>' . esc_html__('WooCommerce plugin must be enabled for Algolia Woo Indexer to work.', 'algolia-woo-indexer') . '</p> |
351 | 351 | </div>'; |
@@ -418,7 +418,7 @@ discard block |
||
418 | 418 | /** |
419 | 419 | * Sanitizing by setting the value to either 1 or 0 |
420 | 420 | */ |
421 | - $sanitized['product'] = (!empty($auto_send)) ? 1 : 0; |
|
421 | + $sanitized['product'] = ( ! empty($auto_send)) ? 1 : 0; |
|
422 | 422 | |
423 | 423 | /** |
424 | 424 | * Filter the data fields checkboxes and verify that the input is an array and assign it to an associative array |
@@ -428,7 +428,7 @@ discard block |
||
428 | 428 | $sanitized['fields'] = array(); |
429 | 429 | foreach (BASIC_FIELDS as $field) { |
430 | 430 | $raw_field = filter_input(INPUT_POST, 'algolia_woo_indexer_field_' . $field, FILTER_DEFAULT, FILTER_REQUIRE_ARRAY); |
431 | - $filtered_field = (!empty($raw_field)) ? 1 : 0; |
|
431 | + $filtered_field = ( ! empty($raw_field)) ? 1 : 0; |
|
432 | 432 | $sanitized['fields'][$field] = $filtered_field; |
433 | 433 | } |
434 | 434 | |
@@ -438,21 +438,21 @@ discard block |
||
438 | 438 | * |
439 | 439 | * @see https://developer.wordpress.org/reference/functions/update_option/ |
440 | 440 | */ |
441 | - if (isset($sanitized['app_id']) && (!empty($sanitized['app_id']))) { |
|
441 | + if (isset($sanitized['app_id']) && ( ! empty($sanitized['app_id']))) { |
|
442 | 442 | update_option( |
443 | 443 | ALGOWOO_DB_OPTION . ALGOLIA_APP_ID, |
444 | 444 | $sanitized['app_id'] |
445 | 445 | ); |
446 | 446 | } |
447 | 447 | |
448 | - if (isset($sanitized['api_key']) && (!empty($sanitized['api_key']))) { |
|
448 | + if (isset($sanitized['api_key']) && ( ! empty($sanitized['api_key']))) { |
|
449 | 449 | update_option( |
450 | 450 | ALGOWOO_DB_OPTION . ALGOLIA_API_KEY, |
451 | 451 | $sanitized['api_key'] |
452 | 452 | ); |
453 | 453 | } |
454 | 454 | |
455 | - if (isset($sanitized['index_name']) && (!empty($sanitized['index_name']))) { |
|
455 | + if (isset($sanitized['index_name']) && ( ! empty($sanitized['index_name']))) { |
|
456 | 456 | update_option( |
457 | 457 | ALGOWOO_DB_OPTION . INDEX_NAME, |
458 | 458 | $sanitized['index_name'] |
@@ -466,7 +466,7 @@ discard block |
||
466 | 466 | ); |
467 | 467 | } |
468 | 468 | |
469 | - if (isset($sanitized['fields']) && (!empty($sanitized['fields']))) { |
|
469 | + if (isset($sanitized['fields']) && ( ! empty($sanitized['fields']))) { |
|
470 | 470 | foreach ($sanitized['fields'] as $key => $value) { |
471 | 471 | update_option( |
472 | 472 | ALGOWOO_DB_OPTION . BASIC_FIELD_PREFIX . $key, |
@@ -545,7 +545,7 @@ discard block |
||
545 | 545 | /** |
546 | 546 | * Verify that the user can access the settings page |
547 | 547 | */ |
548 | - if (!current_user_can('manage_options')) { |
|
548 | + if ( ! current_user_can('manage_options')) { |
|
549 | 549 | wp_die(esc_html__('Action not allowed.', 'algolia_woo_indexer_settings')); |
550 | 550 | } ?> |
551 | 551 | <div class="wrap"> |
@@ -572,7 +572,7 @@ discard block |
||
572 | 572 | */ |
573 | 573 | public static function get_instance() |
574 | 574 | { |
575 | - if (!self::$instance) { |
|
575 | + if ( ! self::$instance) { |
|
576 | 576 | self::$instance = new Algolia_Woo_Indexer(); |
577 | 577 | } |
578 | 578 | return self::$instance; |
@@ -18,543 +18,543 @@ discard block |
||
18 | 18 | * Abort if this file is called directly |
19 | 19 | */ |
20 | 20 | if (!defined('ABSPATH')) { |
21 | - exit; |
|
21 | + exit; |
|
22 | 22 | } |
23 | 23 | |
24 | 24 | /** |
25 | 25 | * Include plugin file if function is_plugin_active does not exist |
26 | 26 | */ |
27 | 27 | if (!function_exists('is_plugin_active')) { |
28 | - require_once(ABSPATH . '/wp-admin/includes/plugin.php'); |
|
28 | + require_once(ABSPATH . '/wp-admin/includes/plugin.php'); |
|
29 | 29 | } |
30 | 30 | |
31 | 31 | if (!class_exists('Algolia_Woo_Indexer')) { |
32 | - /** |
|
33 | - * Algolia WooIndexer main class |
|
34 | - */ |
|
35 | - // TODO Rename class "Algolia_Woo_Indexer" to match the regular expression ^[A-Z][a-zA-Z0-9]*$. |
|
36 | - class Algolia_Woo_Indexer |
|
37 | - { |
|
38 | - const PLUGIN_NAME = 'Algolia Woo Indexer'; |
|
39 | - const PLUGIN_TRANSIENT = 'algowoo-plugin-notice'; |
|
40 | - |
|
41 | - /** |
|
42 | - * Class instance |
|
43 | - * |
|
44 | - * @var object |
|
45 | - */ |
|
46 | - private static $instance; |
|
47 | - |
|
48 | - /** |
|
49 | - * The plugin URL |
|
50 | - * |
|
51 | - * @var string |
|
52 | - */ |
|
53 | - private static $plugin_url = ''; |
|
54 | - |
|
55 | - /** |
|
56 | - * Class constructor |
|
57 | - * |
|
58 | - * @return void |
|
59 | - */ |
|
60 | - public function __construct() |
|
61 | - { |
|
62 | - $this->init(); |
|
63 | - } |
|
64 | - |
|
65 | - /** |
|
66 | - * Setup sections and fields to store and retrieve values from Settings API |
|
67 | - * |
|
68 | - * @return void |
|
69 | - */ |
|
70 | - public static function setup_settings_sections() |
|
71 | - { |
|
72 | - /** |
|
73 | - * Setup arguments for settings sections and fields |
|
74 | - * |
|
75 | - * @see https://developer.wordpress.org/reference/functions/register_setting/ |
|
76 | - */ |
|
77 | - if (is_admin()) { |
|
78 | - $arguments = array( |
|
79 | - 'type' => 'string', |
|
80 | - 'sanitize_callback' => 'settings_fields_validate_options', |
|
81 | - 'default' => null, |
|
82 | - ); |
|
83 | - register_setting('algolia_woo_options', 'algolia_woo_options', $arguments); |
|
84 | - |
|
85 | - /** |
|
86 | - * Make sure we reference the instance of the current class by using self::get_instance() |
|
87 | - * This way we can setup the correct callback function for add_settings_section and add_settings_field |
|
88 | - */ |
|
89 | - $algowooindexer = self::get_instance(); |
|
90 | - |
|
91 | - /** |
|
92 | - * Add our necessary settings sections and fields |
|
93 | - */ |
|
94 | - add_settings_section( |
|
95 | - 'algolia_woo_indexer_main', |
|
96 | - esc_html__('Algolia Woo Plugin Settings', 'algolia-woo-indexer'), |
|
97 | - array($algowooindexer, 'algolia_woo_indexer_section_text'), |
|
98 | - 'algolia_woo_indexer' |
|
99 | - ); |
|
100 | - add_settings_field( |
|
101 | - 'algolia_woo_indexer_application_id', |
|
102 | - esc_html__('Application ID', 'algolia-woo-indexer'), |
|
103 | - array($algowooindexer, 'algolia_woo_indexer_application_id_output'), |
|
104 | - 'algolia_woo_indexer', |
|
105 | - 'algolia_woo_indexer_main' |
|
106 | - ); |
|
107 | - add_settings_field( |
|
108 | - 'algolia_woo_indexer_admin_api_key', |
|
109 | - esc_html__('Admin API Key', 'algolia-woo-indexer'), |
|
110 | - array($algowooindexer, 'algolia_woo_indexer_admin_api_key_output'), |
|
111 | - 'algolia_woo_indexer', |
|
112 | - 'algolia_woo_indexer_main' |
|
113 | - ); |
|
114 | - add_settings_field( |
|
115 | - 'algolia_woo_indexer_index_name', |
|
116 | - esc_html__('Index name (will be created if not existing)', 'algolia-woo-indexer'), |
|
117 | - array($algowooindexer, 'algolia_woo_indexer_index_name_output'), |
|
118 | - 'algolia_woo_indexer', |
|
119 | - 'algolia_woo_indexer_main' |
|
120 | - ); |
|
121 | - add_settings_field( |
|
122 | - 'algolia_woo_indexer_automatically_send_new_products', |
|
123 | - esc_html__('Automatically index new products', 'algolia-woo-indexer'), |
|
124 | - array($algowooindexer, 'algolia_woo_indexer_automatically_send_new_products_output'), |
|
125 | - 'algolia_woo_indexer', |
|
126 | - 'algolia_woo_indexer_main' |
|
127 | - ); |
|
128 | - |
|
129 | - /** |
|
130 | - * Add sections and fields for the basic fields |
|
131 | - */ |
|
132 | - add_settings_section( |
|
133 | - 'algolia_woo_indexer_fields', |
|
134 | - esc_html__('Fields indexing settings', 'algolia-woo-indexer'), |
|
135 | - array($algowooindexer, 'algolia_woo_indexer_fields_section_text'), |
|
136 | - 'algolia_woo_indexer' |
|
137 | - ); |
|
138 | - foreach (BASIC_FIELDS as $field) { |
|
139 | - add_settings_field( |
|
140 | - 'algolia_woo_indexer_field_' . $field, |
|
141 | - esc_html__($field, 'algolia-woo-indexer'), |
|
142 | - array($algowooindexer, 'algolia_woo_indexer_field_output'), |
|
143 | - 'algolia_woo_indexer', |
|
144 | - 'algolia_woo_indexer_fields', |
|
145 | - array( |
|
146 | - 'label_for' => 'algolia_woo_indexer_field_' . $field, |
|
147 | - 'name' => $field |
|
148 | - ) |
|
149 | - ); |
|
150 | - } |
|
151 | - add_settings_field( |
|
152 | - 'algolia_woo_indexer_custom_fields', |
|
153 | - esc_html__('Custom Fields', 'algolia-woo-indexer'), |
|
154 | - array($algowooindexer, 'algolia_woo_indexer_custom_fields_output'), |
|
155 | - 'algolia_woo_indexer', |
|
156 | - 'algolia_woo_indexer_fields' |
|
157 | - ); |
|
158 | - |
|
159 | - /** |
|
160 | - * add settings for Attributes |
|
161 | - */ |
|
162 | - Algolia_Attributes::setup_attributes_settings(); |
|
163 | - |
|
164 | - } |
|
165 | - } |
|
166 | - |
|
167 | - /** |
|
168 | - * Output for admin API key field |
|
169 | - * |
|
170 | - * @see https://developer.wordpress.org/reference/functions/wp_nonce_field/ |
|
171 | - * |
|
172 | - * @return void |
|
173 | - */ |
|
174 | - public static function algolia_woo_indexer_admin_api_key_output() |
|
175 | - { |
|
176 | - $api_key = get_option(ALGOWOO_DB_OPTION . ALGOLIA_API_KEY); |
|
177 | - $api_key = is_string($api_key) ? $api_key : CHANGE_ME; |
|
178 | - |
|
179 | - wp_nonce_field('algolia_woo_indexer_admin_api_nonce_action', 'algolia_woo_indexer_admin_api_nonce_name'); |
|
180 | - |
|
181 | - echo "<input id='algolia_woo_indexer_admin_api_key' name='algolia_woo_indexer_admin_api_key[key]' |
|
32 | + /** |
|
33 | + * Algolia WooIndexer main class |
|
34 | + */ |
|
35 | + // TODO Rename class "Algolia_Woo_Indexer" to match the regular expression ^[A-Z][a-zA-Z0-9]*$. |
|
36 | + class Algolia_Woo_Indexer |
|
37 | + { |
|
38 | + const PLUGIN_NAME = 'Algolia Woo Indexer'; |
|
39 | + const PLUGIN_TRANSIENT = 'algowoo-plugin-notice'; |
|
40 | + |
|
41 | + /** |
|
42 | + * Class instance |
|
43 | + * |
|
44 | + * @var object |
|
45 | + */ |
|
46 | + private static $instance; |
|
47 | + |
|
48 | + /** |
|
49 | + * The plugin URL |
|
50 | + * |
|
51 | + * @var string |
|
52 | + */ |
|
53 | + private static $plugin_url = ''; |
|
54 | + |
|
55 | + /** |
|
56 | + * Class constructor |
|
57 | + * |
|
58 | + * @return void |
|
59 | + */ |
|
60 | + public function __construct() |
|
61 | + { |
|
62 | + $this->init(); |
|
63 | + } |
|
64 | + |
|
65 | + /** |
|
66 | + * Setup sections and fields to store and retrieve values from Settings API |
|
67 | + * |
|
68 | + * @return void |
|
69 | + */ |
|
70 | + public static function setup_settings_sections() |
|
71 | + { |
|
72 | + /** |
|
73 | + * Setup arguments for settings sections and fields |
|
74 | + * |
|
75 | + * @see https://developer.wordpress.org/reference/functions/register_setting/ |
|
76 | + */ |
|
77 | + if (is_admin()) { |
|
78 | + $arguments = array( |
|
79 | + 'type' => 'string', |
|
80 | + 'sanitize_callback' => 'settings_fields_validate_options', |
|
81 | + 'default' => null, |
|
82 | + ); |
|
83 | + register_setting('algolia_woo_options', 'algolia_woo_options', $arguments); |
|
84 | + |
|
85 | + /** |
|
86 | + * Make sure we reference the instance of the current class by using self::get_instance() |
|
87 | + * This way we can setup the correct callback function for add_settings_section and add_settings_field |
|
88 | + */ |
|
89 | + $algowooindexer = self::get_instance(); |
|
90 | + |
|
91 | + /** |
|
92 | + * Add our necessary settings sections and fields |
|
93 | + */ |
|
94 | + add_settings_section( |
|
95 | + 'algolia_woo_indexer_main', |
|
96 | + esc_html__('Algolia Woo Plugin Settings', 'algolia-woo-indexer'), |
|
97 | + array($algowooindexer, 'algolia_woo_indexer_section_text'), |
|
98 | + 'algolia_woo_indexer' |
|
99 | + ); |
|
100 | + add_settings_field( |
|
101 | + 'algolia_woo_indexer_application_id', |
|
102 | + esc_html__('Application ID', 'algolia-woo-indexer'), |
|
103 | + array($algowooindexer, 'algolia_woo_indexer_application_id_output'), |
|
104 | + 'algolia_woo_indexer', |
|
105 | + 'algolia_woo_indexer_main' |
|
106 | + ); |
|
107 | + add_settings_field( |
|
108 | + 'algolia_woo_indexer_admin_api_key', |
|
109 | + esc_html__('Admin API Key', 'algolia-woo-indexer'), |
|
110 | + array($algowooindexer, 'algolia_woo_indexer_admin_api_key_output'), |
|
111 | + 'algolia_woo_indexer', |
|
112 | + 'algolia_woo_indexer_main' |
|
113 | + ); |
|
114 | + add_settings_field( |
|
115 | + 'algolia_woo_indexer_index_name', |
|
116 | + esc_html__('Index name (will be created if not existing)', 'algolia-woo-indexer'), |
|
117 | + array($algowooindexer, 'algolia_woo_indexer_index_name_output'), |
|
118 | + 'algolia_woo_indexer', |
|
119 | + 'algolia_woo_indexer_main' |
|
120 | + ); |
|
121 | + add_settings_field( |
|
122 | + 'algolia_woo_indexer_automatically_send_new_products', |
|
123 | + esc_html__('Automatically index new products', 'algolia-woo-indexer'), |
|
124 | + array($algowooindexer, 'algolia_woo_indexer_automatically_send_new_products_output'), |
|
125 | + 'algolia_woo_indexer', |
|
126 | + 'algolia_woo_indexer_main' |
|
127 | + ); |
|
128 | + |
|
129 | + /** |
|
130 | + * Add sections and fields for the basic fields |
|
131 | + */ |
|
132 | + add_settings_section( |
|
133 | + 'algolia_woo_indexer_fields', |
|
134 | + esc_html__('Fields indexing settings', 'algolia-woo-indexer'), |
|
135 | + array($algowooindexer, 'algolia_woo_indexer_fields_section_text'), |
|
136 | + 'algolia_woo_indexer' |
|
137 | + ); |
|
138 | + foreach (BASIC_FIELDS as $field) { |
|
139 | + add_settings_field( |
|
140 | + 'algolia_woo_indexer_field_' . $field, |
|
141 | + esc_html__($field, 'algolia-woo-indexer'), |
|
142 | + array($algowooindexer, 'algolia_woo_indexer_field_output'), |
|
143 | + 'algolia_woo_indexer', |
|
144 | + 'algolia_woo_indexer_fields', |
|
145 | + array( |
|
146 | + 'label_for' => 'algolia_woo_indexer_field_' . $field, |
|
147 | + 'name' => $field |
|
148 | + ) |
|
149 | + ); |
|
150 | + } |
|
151 | + add_settings_field( |
|
152 | + 'algolia_woo_indexer_custom_fields', |
|
153 | + esc_html__('Custom Fields', 'algolia-woo-indexer'), |
|
154 | + array($algowooindexer, 'algolia_woo_indexer_custom_fields_output'), |
|
155 | + 'algolia_woo_indexer', |
|
156 | + 'algolia_woo_indexer_fields' |
|
157 | + ); |
|
158 | + |
|
159 | + /** |
|
160 | + * add settings for Attributes |
|
161 | + */ |
|
162 | + Algolia_Attributes::setup_attributes_settings(); |
|
163 | + |
|
164 | + } |
|
165 | + } |
|
166 | + |
|
167 | + /** |
|
168 | + * Output for admin API key field |
|
169 | + * |
|
170 | + * @see https://developer.wordpress.org/reference/functions/wp_nonce_field/ |
|
171 | + * |
|
172 | + * @return void |
|
173 | + */ |
|
174 | + public static function algolia_woo_indexer_admin_api_key_output() |
|
175 | + { |
|
176 | + $api_key = get_option(ALGOWOO_DB_OPTION . ALGOLIA_API_KEY); |
|
177 | + $api_key = is_string($api_key) ? $api_key : CHANGE_ME; |
|
178 | + |
|
179 | + wp_nonce_field('algolia_woo_indexer_admin_api_nonce_action', 'algolia_woo_indexer_admin_api_nonce_name'); |
|
180 | + |
|
181 | + echo "<input id='algolia_woo_indexer_admin_api_key' name='algolia_woo_indexer_admin_api_key[key]' |
|
182 | 182 | type='text' value='" . esc_attr($api_key) . "' />"; |
183 | - } |
|
184 | - |
|
185 | - /** |
|
186 | - * Output for application ID field |
|
187 | - * |
|
188 | - * @return void |
|
189 | - */ |
|
190 | - public static function algolia_woo_indexer_application_id_output() |
|
191 | - { |
|
192 | - $application_id = get_option(ALGOWOO_DB_OPTION . ALGOLIA_APP_ID); |
|
193 | - $application_id = is_string($application_id) ? $application_id : CHANGE_ME; |
|
194 | - |
|
195 | - echo "<input id='algolia_woo_indexer_application_id' name='algolia_woo_indexer_application_id[id]' |
|
183 | + } |
|
184 | + |
|
185 | + /** |
|
186 | + * Output for application ID field |
|
187 | + * |
|
188 | + * @return void |
|
189 | + */ |
|
190 | + public static function algolia_woo_indexer_application_id_output() |
|
191 | + { |
|
192 | + $application_id = get_option(ALGOWOO_DB_OPTION . ALGOLIA_APP_ID); |
|
193 | + $application_id = is_string($application_id) ? $application_id : CHANGE_ME; |
|
194 | + |
|
195 | + echo "<input id='algolia_woo_indexer_application_id' name='algolia_woo_indexer_application_id[id]' |
|
196 | 196 | type='text' value='" . esc_attr($application_id) . "' />"; |
197 | - } |
|
198 | - |
|
199 | - /** |
|
200 | - * Output for index name field |
|
201 | - * |
|
202 | - * @return void |
|
203 | - */ |
|
204 | - public static function algolia_woo_indexer_index_name_output() |
|
205 | - { |
|
206 | - $index_name = get_option(ALGOWOO_DB_OPTION . INDEX_NAME); |
|
207 | - $index_name = is_string($index_name) ? $index_name : CHANGE_ME; |
|
208 | - |
|
209 | - echo "<input id='algolia_woo_indexer_index_name' name='algolia_woo_indexer_index_name[name]' |
|
197 | + } |
|
198 | + |
|
199 | + /** |
|
200 | + * Output for index name field |
|
201 | + * |
|
202 | + * @return void |
|
203 | + */ |
|
204 | + public static function algolia_woo_indexer_index_name_output() |
|
205 | + { |
|
206 | + $index_name = get_option(ALGOWOO_DB_OPTION . INDEX_NAME); |
|
207 | + $index_name = is_string($index_name) ? $index_name : CHANGE_ME; |
|
208 | + |
|
209 | + echo "<input id='algolia_woo_indexer_index_name' name='algolia_woo_indexer_index_name[name]' |
|
210 | 210 | type='text' value='" . esc_attr($index_name) . "' />"; |
211 | - } |
|
212 | - |
|
213 | - /** |
|
214 | - * Output for checkbox to check if we automatically send new products to Algolia |
|
215 | - * |
|
216 | - * @return void |
|
217 | - */ |
|
218 | - public static function algolia_woo_indexer_automatically_send_new_products_output() |
|
219 | - { |
|
220 | - /** |
|
221 | - * Sanitization is not really needed as the variable is not directly echoed |
|
222 | - * But I have still done it to be 100% safe |
|
223 | - */ |
|
224 | - $auto_send = get_option(ALGOWOO_DB_OPTION . AUTOMATICALLY_SEND_NEW_PRODUCTS); |
|
225 | - $auto_send = (!empty($auto_send)) ? 1 : 0; ?> |
|
211 | + } |
|
212 | + |
|
213 | + /** |
|
214 | + * Output for checkbox to check if we automatically send new products to Algolia |
|
215 | + * |
|
216 | + * @return void |
|
217 | + */ |
|
218 | + public static function algolia_woo_indexer_automatically_send_new_products_output() |
|
219 | + { |
|
220 | + /** |
|
221 | + * Sanitization is not really needed as the variable is not directly echoed |
|
222 | + * But I have still done it to be 100% safe |
|
223 | + */ |
|
224 | + $auto_send = get_option(ALGOWOO_DB_OPTION . AUTOMATICALLY_SEND_NEW_PRODUCTS); |
|
225 | + $auto_send = (!empty($auto_send)) ? 1 : 0; ?> |
|
226 | 226 | <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); ?> /> |
227 | 227 | <?php |
228 | - } |
|
229 | - |
|
230 | - /** |
|
231 | - * Output for fields which data shall be sent to Algolia |
|
232 | - * |
|
233 | - * @return void |
|
234 | - */ |
|
235 | - public static function algolia_woo_indexer_field_output($args) |
|
236 | - { |
|
237 | - $value = get_option(ALGOWOO_DB_OPTION . BASIC_FIELD_PREFIX . $args["name"]); |
|
238 | - $isChecked = (!empty($value)) ? 1 : 0; |
|
239 | - ?> |
|
228 | + } |
|
229 | + |
|
230 | + /** |
|
231 | + * Output for fields which data shall be sent to Algolia |
|
232 | + * |
|
233 | + * @return void |
|
234 | + */ |
|
235 | + public static function algolia_woo_indexer_field_output($args) |
|
236 | + { |
|
237 | + $value = get_option(ALGOWOO_DB_OPTION . BASIC_FIELD_PREFIX . $args["name"]); |
|
238 | + $isChecked = (!empty($value)) ? 1 : 0; |
|
239 | + ?> |
|
240 | 240 | <input id="<?php echo $args["label_for"] ?>" name="<?php echo $args["label_for"] ?>[checked]" type="checkbox" <?php checked(1, $isChecked); ?> /> |
241 | 241 | <?php |
242 | - } |
|
243 | - |
|
244 | - /** |
|
245 | - * Output textarea for custom fields |
|
246 | - * |
|
247 | - * @return void |
|
248 | - */ |
|
249 | - public static function algolia_woo_indexer_custom_fields_output() |
|
250 | - { |
|
251 | - $custom_fields = get_option(ALGOWOO_DB_OPTION . CUSTOM_FIELDS); |
|
252 | - $asLines = str_replace(',', "\r", $custom_fields); |
|
253 | - ?> |
|
242 | + } |
|
243 | + |
|
244 | + /** |
|
245 | + * Output textarea for custom fields |
|
246 | + * |
|
247 | + * @return void |
|
248 | + */ |
|
249 | + public static function algolia_woo_indexer_custom_fields_output() |
|
250 | + { |
|
251 | + $custom_fields = get_option(ALGOWOO_DB_OPTION . CUSTOM_FIELDS); |
|
252 | + $asLines = str_replace(',', "\r", $custom_fields); |
|
253 | + ?> |
|
254 | 254 | <p><?php echo esc_html__('Add some custom fields names, for example from ACF (Advanced custom fields) here. create a new line for each field or separate with comma.', 'algolia-woo-indexer'); ?></p> |
255 | 255 | <textarea id="algolia_woo_indexer_custom_fields" name="algolia_woo_indexer_custom_fields" rows="6" cols="50"><?php echo $asLines; ?></textarea> |
256 | 256 | <?php |
257 | - } |
|
258 | - |
|
259 | - /** |
|
260 | - * Section text for plugin settings section text |
|
261 | - * |
|
262 | - * @return void |
|
263 | - */ |
|
264 | - public static function algolia_woo_indexer_section_text() |
|
265 | - { |
|
266 | - echo esc_html__('Enter your API settings here.', 'algolia-woo-indexer'); |
|
267 | - } |
|
268 | - |
|
269 | - /** |
|
270 | - * Section text for basic fields settings section text |
|
271 | - * |
|
272 | - * @return void |
|
273 | - */ |
|
274 | - public static function algolia_woo_indexer_fields_section_text() |
|
275 | - { |
|
276 | - echo esc_html__('Choose which basic fields shall be indexed.', 'algolia-woo-indexer'); |
|
277 | - } |
|
257 | + } |
|
258 | + |
|
259 | + /** |
|
260 | + * Section text for plugin settings section text |
|
261 | + * |
|
262 | + * @return void |
|
263 | + */ |
|
264 | + public static function algolia_woo_indexer_section_text() |
|
265 | + { |
|
266 | + echo esc_html__('Enter your API settings here.', 'algolia-woo-indexer'); |
|
267 | + } |
|
268 | + |
|
269 | + /** |
|
270 | + * Section text for basic fields settings section text |
|
271 | + * |
|
272 | + * @return void |
|
273 | + */ |
|
274 | + public static function algolia_woo_indexer_fields_section_text() |
|
275 | + { |
|
276 | + echo esc_html__('Choose which basic fields shall be indexed.', 'algolia-woo-indexer'); |
|
277 | + } |
|
278 | 278 | |
279 | - /** |
|
280 | - * Check if we are going to send products by verifying send products nonce |
|
281 | - * |
|
282 | - * @return void |
|
283 | - */ |
|
284 | - public static function maybe_send_products() |
|
285 | - { |
|
286 | - if (true === Algolia_Verify_Nonces::verify_send_products_nonce()) { |
|
287 | - Algolia_Send_Products::send_products_to_algolia(); |
|
288 | - } |
|
289 | - } |
|
290 | - |
|
291 | - /** |
|
292 | - * Initialize class, setup settings sections and fields |
|
293 | - * |
|
294 | - * @return void |
|
295 | - */ |
|
296 | - public static function init() |
|
297 | - { |
|
298 | - /** |
|
299 | - * Fetch the option to see if we are going to automatically send new products |
|
300 | - */ |
|
301 | - $auto_send = get_option(ALGOWOO_DB_OPTION . AUTOMATICALLY_SEND_NEW_PRODUCTS); |
|
302 | - |
|
303 | - /** |
|
304 | - * Check that we have the minimum versions required and all of the required PHP extensions |
|
305 | - */ |
|
306 | - Algolia_Check_Requirements::check_unmet_requirements(); |
|
307 | - |
|
308 | - if (!Algolia_Check_Requirements::algolia_wp_version_check() || !Algolia_Check_Requirements::algolia_php_version_check()) { |
|
309 | - add_action( |
|
310 | - 'admin_notices', |
|
311 | - function () { |
|
312 | - echo '<div class="error notice"> |
|
279 | + /** |
|
280 | + * Check if we are going to send products by verifying send products nonce |
|
281 | + * |
|
282 | + * @return void |
|
283 | + */ |
|
284 | + public static function maybe_send_products() |
|
285 | + { |
|
286 | + if (true === Algolia_Verify_Nonces::verify_send_products_nonce()) { |
|
287 | + Algolia_Send_Products::send_products_to_algolia(); |
|
288 | + } |
|
289 | + } |
|
290 | + |
|
291 | + /** |
|
292 | + * Initialize class, setup settings sections and fields |
|
293 | + * |
|
294 | + * @return void |
|
295 | + */ |
|
296 | + public static function init() |
|
297 | + { |
|
298 | + /** |
|
299 | + * Fetch the option to see if we are going to automatically send new products |
|
300 | + */ |
|
301 | + $auto_send = get_option(ALGOWOO_DB_OPTION . AUTOMATICALLY_SEND_NEW_PRODUCTS); |
|
302 | + |
|
303 | + /** |
|
304 | + * Check that we have the minimum versions required and all of the required PHP extensions |
|
305 | + */ |
|
306 | + Algolia_Check_Requirements::check_unmet_requirements(); |
|
307 | + |
|
308 | + if (!Algolia_Check_Requirements::algolia_wp_version_check() || !Algolia_Check_Requirements::algolia_php_version_check()) { |
|
309 | + add_action( |
|
310 | + 'admin_notices', |
|
311 | + function () { |
|
312 | + echo '<div class="error notice"> |
|
313 | 313 | <p>' . esc_html__('Please check the server requirements for Algolia Woo Indexer. <br/> It requires minimum PHP version 7.2 and WordPress version 5.0', 'algolia-woo-indexer') . '</p> |
314 | 314 | </div>'; |
315 | - } |
|
316 | - ); |
|
317 | - } |
|
318 | - |
|
319 | - $ob_class = get_called_class(); |
|
320 | - |
|
321 | - /** |
|
322 | - * Setup translations |
|
323 | - */ |
|
324 | - add_action('plugins_loaded', array($ob_class, 'load_textdomain')); |
|
325 | - |
|
326 | - /** |
|
327 | - * Add actions to setup admin menu |
|
328 | - */ |
|
329 | - if (is_admin()) { |
|
330 | - add_action('admin_menu', array($ob_class, 'admin_menu')); |
|
331 | - add_action('admin_init', array($ob_class, 'setup_settings_sections')); |
|
332 | - add_action('admin_init', array($ob_class, 'update_settings_options')); |
|
333 | - add_action('admin_init', array($ob_class, 'maybe_send_products')); |
|
334 | - |
|
335 | - /** |
|
336 | - * Register hook to automatically send new products if the option is set |
|
337 | - */ |
|
338 | - |
|
339 | - if ('1' === $auto_send) { |
|
340 | - add_action('save_post', array($ob_class, 'send_new_product_to_algolia'), 10, 3); |
|
341 | - } |
|
342 | - |
|
343 | - self::$plugin_url = admin_url('options-general.php?page=algolia-woo-indexer-settings'); |
|
344 | - |
|
345 | - if (!is_plugin_active('woocommerce/woocommerce.php')) { |
|
346 | - add_action( |
|
347 | - 'admin_notices', |
|
348 | - function () { |
|
349 | - echo '<div class="error notice"> |
|
315 | + } |
|
316 | + ); |
|
317 | + } |
|
318 | + |
|
319 | + $ob_class = get_called_class(); |
|
320 | + |
|
321 | + /** |
|
322 | + * Setup translations |
|
323 | + */ |
|
324 | + add_action('plugins_loaded', array($ob_class, 'load_textdomain')); |
|
325 | + |
|
326 | + /** |
|
327 | + * Add actions to setup admin menu |
|
328 | + */ |
|
329 | + if (is_admin()) { |
|
330 | + add_action('admin_menu', array($ob_class, 'admin_menu')); |
|
331 | + add_action('admin_init', array($ob_class, 'setup_settings_sections')); |
|
332 | + add_action('admin_init', array($ob_class, 'update_settings_options')); |
|
333 | + add_action('admin_init', array($ob_class, 'maybe_send_products')); |
|
334 | + |
|
335 | + /** |
|
336 | + * Register hook to automatically send new products if the option is set |
|
337 | + */ |
|
338 | + |
|
339 | + if ('1' === $auto_send) { |
|
340 | + add_action('save_post', array($ob_class, 'send_new_product_to_algolia'), 10, 3); |
|
341 | + } |
|
342 | + |
|
343 | + self::$plugin_url = admin_url('options-general.php?page=algolia-woo-indexer-settings'); |
|
344 | + |
|
345 | + if (!is_plugin_active('woocommerce/woocommerce.php')) { |
|
346 | + add_action( |
|
347 | + 'admin_notices', |
|
348 | + function () { |
|
349 | + echo '<div class="error notice"> |
|
350 | 350 | <p>' . esc_html__('WooCommerce plugin must be enabled for Algolia Woo Indexer to work.', 'algolia-woo-indexer') . '</p> |
351 | 351 | </div>'; |
352 | - } |
|
353 | - ); |
|
354 | - } |
|
355 | - } |
|
356 | - } |
|
357 | - |
|
358 | - /** |
|
359 | - * Send a single product to Algolia once a new product has been published |
|
360 | - * |
|
361 | - * @param int $post_id ID of the product. |
|
362 | - * @param array $post Post array. |
|
363 | - * |
|
364 | - * @return void |
|
365 | - */ |
|
366 | - public static function send_new_product_to_algolia($post_id, $post) |
|
367 | - { |
|
368 | - if ('publish' !== $post->post_status || 'product' !== $post->post_type) { |
|
369 | - return; |
|
370 | - } |
|
371 | - Algolia_Send_Products::send_products_to_algolia($post_id); |
|
372 | - } |
|
373 | - |
|
374 | - /** |
|
375 | - * Verify nonces before we update options and settings |
|
376 | - * Also retrieve the value from the send_products_to_algolia hidden field to check if we are sending products to Algolia |
|
377 | - * |
|
378 | - * @return void |
|
379 | - */ |
|
380 | - public static function update_settings_options() |
|
381 | - { |
|
382 | - /** |
|
383 | - * Do not proceed if nonce for settings is not set |
|
384 | - */ |
|
385 | - if (false === Algolia_Verify_Nonces::verify_settings_nonce()) { |
|
386 | - return; |
|
387 | - } |
|
388 | - |
|
389 | - /** |
|
390 | - * Do not proceed if we are going to send products |
|
391 | - */ |
|
392 | - if (true === Algolia_Verify_Nonces::verify_send_products_nonce()) { |
|
393 | - return; |
|
394 | - } |
|
395 | - |
|
396 | - /** |
|
397 | - * Filter the application id, api key, index name, custom_fields and verify that the input is an array |
|
398 | - * |
|
399 | - * @see https://www.php.net/manual/en/function.filter-input.php |
|
400 | - */ |
|
401 | - $post_application_id = filter_input(INPUT_POST, 'algolia_woo_indexer_application_id', FILTER_DEFAULT, FILTER_REQUIRE_ARRAY); |
|
402 | - $post_api_key = filter_input(INPUT_POST, 'algolia_woo_indexer_admin_api_key', FILTER_DEFAULT, FILTER_REQUIRE_ARRAY); |
|
403 | - $post_index_name = filter_input(INPUT_POST, 'algolia_woo_indexer_index_name', FILTER_DEFAULT, FILTER_REQUIRE_ARRAY); |
|
404 | - $auto_send = filter_input(INPUT_POST, 'algolia_woo_indexer_automatically_send_new_products', FILTER_DEFAULT, FILTER_REQUIRE_ARRAY); |
|
405 | - $custom_fields = filter_input(INPUT_POST, 'algolia_woo_indexer_custom_fields', FILTER_DEFAULT); |
|
406 | - |
|
407 | - /** |
|
408 | - * Properly sanitize text fields before updating data |
|
409 | - * |
|
410 | - * @see https://developer.wordpress.org/reference/functions/sanitize_text_field/ |
|
411 | - */ |
|
412 | - $sanitized = array(); |
|
413 | - $sanitized['app_id'] = sanitize_text_field($post_application_id['id']); |
|
414 | - $sanitized['api_key'] = sanitize_text_field($post_api_key['key']); |
|
415 | - $sanitized['index_name'] = sanitize_text_field($post_index_name['name']); |
|
416 | - $sanitized['custom_fields'] = sanitize_textarea_field($custom_fields); |
|
417 | - |
|
418 | - /** |
|
419 | - * Sanitizing by setting the value to either 1 or 0 |
|
420 | - */ |
|
421 | - $sanitized['product'] = (!empty($auto_send)) ? 1 : 0; |
|
422 | - |
|
423 | - /** |
|
424 | - * Filter the data fields checkboxes and verify that the input is an array and assign it to an associative array |
|
425 | - * |
|
426 | - * @see https://www.php.net/manual/en/function.filter-input.php |
|
427 | - */ |
|
428 | - $sanitized['fields'] = array(); |
|
429 | - foreach (BASIC_FIELDS as $field) { |
|
430 | - $raw_field = filter_input(INPUT_POST, 'algolia_woo_indexer_field_' . $field, FILTER_DEFAULT, FILTER_REQUIRE_ARRAY); |
|
431 | - $filtered_field = (!empty($raw_field)) ? 1 : 0; |
|
432 | - $sanitized['fields'][$field] = $filtered_field; |
|
433 | - } |
|
434 | - |
|
435 | - /** |
|
436 | - * Values have been filtered and sanitized |
|
437 | - * Check if set and not empty and update the database |
|
438 | - * |
|
439 | - * @see https://developer.wordpress.org/reference/functions/update_option/ |
|
440 | - */ |
|
441 | - if (isset($sanitized['app_id']) && (!empty($sanitized['app_id']))) { |
|
442 | - update_option( |
|
443 | - ALGOWOO_DB_OPTION . ALGOLIA_APP_ID, |
|
444 | - $sanitized['app_id'] |
|
445 | - ); |
|
446 | - } |
|
447 | - |
|
448 | - if (isset($sanitized['api_key']) && (!empty($sanitized['api_key']))) { |
|
449 | - update_option( |
|
450 | - ALGOWOO_DB_OPTION . ALGOLIA_API_KEY, |
|
451 | - $sanitized['api_key'] |
|
452 | - ); |
|
453 | - } |
|
454 | - |
|
455 | - if (isset($sanitized['index_name']) && (!empty($sanitized['index_name']))) { |
|
456 | - update_option( |
|
457 | - ALGOWOO_DB_OPTION . INDEX_NAME, |
|
458 | - $sanitized['index_name'] |
|
459 | - ); |
|
460 | - } |
|
461 | - |
|
462 | - if (isset($sanitized['product'])) { |
|
463 | - update_option( |
|
464 | - ALGOWOO_DB_OPTION . AUTOMATICALLY_SEND_NEW_PRODUCTS, |
|
465 | - $sanitized['product'] |
|
466 | - ); |
|
467 | - } |
|
468 | - |
|
469 | - if (isset($sanitized['fields']) && (!empty($sanitized['fields']))) { |
|
470 | - foreach ($sanitized['fields'] as $key => $value) { |
|
471 | - update_option( |
|
472 | - ALGOWOO_DB_OPTION . BASIC_FIELD_PREFIX . $key, |
|
473 | - $value |
|
474 | - ); |
|
475 | - } |
|
476 | - } |
|
477 | - |
|
478 | - if (isset($sanitized['custom_fields'])) { |
|
479 | - $custom_fields_array = preg_split("/[\r\n,]+/", $sanitized['custom_fields'], -1, PREG_SPLIT_NO_EMPTY); |
|
480 | - $custom_fields_string = implode(",", $custom_fields_array); |
|
481 | - update_option( |
|
482 | - ALGOWOO_DB_OPTION . CUSTOM_FIELDS, |
|
483 | - $custom_fields_string |
|
484 | - ); |
|
485 | - } |
|
486 | - |
|
487 | - /** |
|
488 | - * Update Attributes as well |
|
489 | - */ |
|
490 | - Algolia_Attributes::update_attribute_options(); |
|
491 | - |
|
492 | - } |
|
493 | - |
|
494 | - /** |
|
495 | - * Sanitize input in settings fields and filter through regex to accept only a-z and A-Z |
|
496 | - * |
|
497 | - * @param string $input Settings text data |
|
498 | - * @return array |
|
499 | - */ |
|
500 | - public static function settings_fields_validate_options($input) |
|
501 | - { |
|
502 | - $valid = array(); |
|
503 | - $valid['name'] = preg_replace( |
|
504 | - '/[^a-zA-Z\s]/', |
|
505 | - '', |
|
506 | - $input['name'] |
|
507 | - ); |
|
508 | - return $valid; |
|
509 | - } |
|
510 | - |
|
511 | - /** |
|
512 | - * Load text domain for translations |
|
513 | - * |
|
514 | - * @return void |
|
515 | - */ |
|
516 | - public static function load_textdomain() |
|
517 | - { |
|
518 | - load_plugin_textdomain('algolia-woo-indexer', false, basename(dirname(__FILE__)) . '/languages/'); |
|
519 | - } |
|
520 | - |
|
521 | - /** |
|
522 | - * Add the new menu to settings section so that we can configure the plugin |
|
523 | - * |
|
524 | - * @return void |
|
525 | - */ |
|
526 | - public static function admin_menu() |
|
527 | - { |
|
528 | - add_submenu_page( |
|
529 | - 'options-general.php', |
|
530 | - esc_html__('Algolia Woo Indexer Settings', 'algolia-woo-indexer'), |
|
531 | - esc_html__('Algolia Woo Indexer Settings', 'algolia-woo-indexer'), |
|
532 | - 'manage_options', |
|
533 | - 'algolia-woo-indexer-settings', |
|
534 | - array(get_called_class(), 'algolia_woo_indexer_settings') |
|
535 | - ); |
|
536 | - } |
|
537 | - |
|
538 | - /** |
|
539 | - * Display settings and allow user to modify them |
|
540 | - * |
|
541 | - * @return void |
|
542 | - */ |
|
543 | - public static function algolia_woo_indexer_settings() |
|
544 | - { |
|
545 | - /** |
|
546 | - * Verify that the user can access the settings page |
|
547 | - */ |
|
548 | - if (!current_user_can('manage_options')) { |
|
549 | - wp_die(esc_html__('Action not allowed.', 'algolia_woo_indexer_settings')); |
|
550 | - } ?> |
|
352 | + } |
|
353 | + ); |
|
354 | + } |
|
355 | + } |
|
356 | + } |
|
357 | + |
|
358 | + /** |
|
359 | + * Send a single product to Algolia once a new product has been published |
|
360 | + * |
|
361 | + * @param int $post_id ID of the product. |
|
362 | + * @param array $post Post array. |
|
363 | + * |
|
364 | + * @return void |
|
365 | + */ |
|
366 | + public static function send_new_product_to_algolia($post_id, $post) |
|
367 | + { |
|
368 | + if ('publish' !== $post->post_status || 'product' !== $post->post_type) { |
|
369 | + return; |
|
370 | + } |
|
371 | + Algolia_Send_Products::send_products_to_algolia($post_id); |
|
372 | + } |
|
373 | + |
|
374 | + /** |
|
375 | + * Verify nonces before we update options and settings |
|
376 | + * Also retrieve the value from the send_products_to_algolia hidden field to check if we are sending products to Algolia |
|
377 | + * |
|
378 | + * @return void |
|
379 | + */ |
|
380 | + public static function update_settings_options() |
|
381 | + { |
|
382 | + /** |
|
383 | + * Do not proceed if nonce for settings is not set |
|
384 | + */ |
|
385 | + if (false === Algolia_Verify_Nonces::verify_settings_nonce()) { |
|
386 | + return; |
|
387 | + } |
|
388 | + |
|
389 | + /** |
|
390 | + * Do not proceed if we are going to send products |
|
391 | + */ |
|
392 | + if (true === Algolia_Verify_Nonces::verify_send_products_nonce()) { |
|
393 | + return; |
|
394 | + } |
|
395 | + |
|
396 | + /** |
|
397 | + * Filter the application id, api key, index name, custom_fields and verify that the input is an array |
|
398 | + * |
|
399 | + * @see https://www.php.net/manual/en/function.filter-input.php |
|
400 | + */ |
|
401 | + $post_application_id = filter_input(INPUT_POST, 'algolia_woo_indexer_application_id', FILTER_DEFAULT, FILTER_REQUIRE_ARRAY); |
|
402 | + $post_api_key = filter_input(INPUT_POST, 'algolia_woo_indexer_admin_api_key', FILTER_DEFAULT, FILTER_REQUIRE_ARRAY); |
|
403 | + $post_index_name = filter_input(INPUT_POST, 'algolia_woo_indexer_index_name', FILTER_DEFAULT, FILTER_REQUIRE_ARRAY); |
|
404 | + $auto_send = filter_input(INPUT_POST, 'algolia_woo_indexer_automatically_send_new_products', FILTER_DEFAULT, FILTER_REQUIRE_ARRAY); |
|
405 | + $custom_fields = filter_input(INPUT_POST, 'algolia_woo_indexer_custom_fields', FILTER_DEFAULT); |
|
406 | + |
|
407 | + /** |
|
408 | + * Properly sanitize text fields before updating data |
|
409 | + * |
|
410 | + * @see https://developer.wordpress.org/reference/functions/sanitize_text_field/ |
|
411 | + */ |
|
412 | + $sanitized = array(); |
|
413 | + $sanitized['app_id'] = sanitize_text_field($post_application_id['id']); |
|
414 | + $sanitized['api_key'] = sanitize_text_field($post_api_key['key']); |
|
415 | + $sanitized['index_name'] = sanitize_text_field($post_index_name['name']); |
|
416 | + $sanitized['custom_fields'] = sanitize_textarea_field($custom_fields); |
|
417 | + |
|
418 | + /** |
|
419 | + * Sanitizing by setting the value to either 1 or 0 |
|
420 | + */ |
|
421 | + $sanitized['product'] = (!empty($auto_send)) ? 1 : 0; |
|
422 | + |
|
423 | + /** |
|
424 | + * Filter the data fields checkboxes and verify that the input is an array and assign it to an associative array |
|
425 | + * |
|
426 | + * @see https://www.php.net/manual/en/function.filter-input.php |
|
427 | + */ |
|
428 | + $sanitized['fields'] = array(); |
|
429 | + foreach (BASIC_FIELDS as $field) { |
|
430 | + $raw_field = filter_input(INPUT_POST, 'algolia_woo_indexer_field_' . $field, FILTER_DEFAULT, FILTER_REQUIRE_ARRAY); |
|
431 | + $filtered_field = (!empty($raw_field)) ? 1 : 0; |
|
432 | + $sanitized['fields'][$field] = $filtered_field; |
|
433 | + } |
|
434 | + |
|
435 | + /** |
|
436 | + * Values have been filtered and sanitized |
|
437 | + * Check if set and not empty and update the database |
|
438 | + * |
|
439 | + * @see https://developer.wordpress.org/reference/functions/update_option/ |
|
440 | + */ |
|
441 | + if (isset($sanitized['app_id']) && (!empty($sanitized['app_id']))) { |
|
442 | + update_option( |
|
443 | + ALGOWOO_DB_OPTION . ALGOLIA_APP_ID, |
|
444 | + $sanitized['app_id'] |
|
445 | + ); |
|
446 | + } |
|
447 | + |
|
448 | + if (isset($sanitized['api_key']) && (!empty($sanitized['api_key']))) { |
|
449 | + update_option( |
|
450 | + ALGOWOO_DB_OPTION . ALGOLIA_API_KEY, |
|
451 | + $sanitized['api_key'] |
|
452 | + ); |
|
453 | + } |
|
454 | + |
|
455 | + if (isset($sanitized['index_name']) && (!empty($sanitized['index_name']))) { |
|
456 | + update_option( |
|
457 | + ALGOWOO_DB_OPTION . INDEX_NAME, |
|
458 | + $sanitized['index_name'] |
|
459 | + ); |
|
460 | + } |
|
461 | + |
|
462 | + if (isset($sanitized['product'])) { |
|
463 | + update_option( |
|
464 | + ALGOWOO_DB_OPTION . AUTOMATICALLY_SEND_NEW_PRODUCTS, |
|
465 | + $sanitized['product'] |
|
466 | + ); |
|
467 | + } |
|
468 | + |
|
469 | + if (isset($sanitized['fields']) && (!empty($sanitized['fields']))) { |
|
470 | + foreach ($sanitized['fields'] as $key => $value) { |
|
471 | + update_option( |
|
472 | + ALGOWOO_DB_OPTION . BASIC_FIELD_PREFIX . $key, |
|
473 | + $value |
|
474 | + ); |
|
475 | + } |
|
476 | + } |
|
477 | + |
|
478 | + if (isset($sanitized['custom_fields'])) { |
|
479 | + $custom_fields_array = preg_split("/[\r\n,]+/", $sanitized['custom_fields'], -1, PREG_SPLIT_NO_EMPTY); |
|
480 | + $custom_fields_string = implode(",", $custom_fields_array); |
|
481 | + update_option( |
|
482 | + ALGOWOO_DB_OPTION . CUSTOM_FIELDS, |
|
483 | + $custom_fields_string |
|
484 | + ); |
|
485 | + } |
|
486 | + |
|
487 | + /** |
|
488 | + * Update Attributes as well |
|
489 | + */ |
|
490 | + Algolia_Attributes::update_attribute_options(); |
|
491 | + |
|
492 | + } |
|
493 | + |
|
494 | + /** |
|
495 | + * Sanitize input in settings fields and filter through regex to accept only a-z and A-Z |
|
496 | + * |
|
497 | + * @param string $input Settings text data |
|
498 | + * @return array |
|
499 | + */ |
|
500 | + public static function settings_fields_validate_options($input) |
|
501 | + { |
|
502 | + $valid = array(); |
|
503 | + $valid['name'] = preg_replace( |
|
504 | + '/[^a-zA-Z\s]/', |
|
505 | + '', |
|
506 | + $input['name'] |
|
507 | + ); |
|
508 | + return $valid; |
|
509 | + } |
|
510 | + |
|
511 | + /** |
|
512 | + * Load text domain for translations |
|
513 | + * |
|
514 | + * @return void |
|
515 | + */ |
|
516 | + public static function load_textdomain() |
|
517 | + { |
|
518 | + load_plugin_textdomain('algolia-woo-indexer', false, basename(dirname(__FILE__)) . '/languages/'); |
|
519 | + } |
|
520 | + |
|
521 | + /** |
|
522 | + * Add the new menu to settings section so that we can configure the plugin |
|
523 | + * |
|
524 | + * @return void |
|
525 | + */ |
|
526 | + public static function admin_menu() |
|
527 | + { |
|
528 | + add_submenu_page( |
|
529 | + 'options-general.php', |
|
530 | + esc_html__('Algolia Woo Indexer Settings', 'algolia-woo-indexer'), |
|
531 | + esc_html__('Algolia Woo Indexer Settings', 'algolia-woo-indexer'), |
|
532 | + 'manage_options', |
|
533 | + 'algolia-woo-indexer-settings', |
|
534 | + array(get_called_class(), 'algolia_woo_indexer_settings') |
|
535 | + ); |
|
536 | + } |
|
537 | + |
|
538 | + /** |
|
539 | + * Display settings and allow user to modify them |
|
540 | + * |
|
541 | + * @return void |
|
542 | + */ |
|
543 | + public static function algolia_woo_indexer_settings() |
|
544 | + { |
|
545 | + /** |
|
546 | + * Verify that the user can access the settings page |
|
547 | + */ |
|
548 | + if (!current_user_can('manage_options')) { |
|
549 | + wp_die(esc_html__('Action not allowed.', 'algolia_woo_indexer_settings')); |
|
550 | + } ?> |
|
551 | 551 | <div class="wrap"> |
552 | 552 | <h1><?php esc_html__('Algolia Woo Indexer Settings', 'algolia-woo-indexer'); ?></h1> |
553 | 553 | <form action="<?php echo esc_url(self::$plugin_url); ?>" method="POST"> |
554 | 554 | <?php |
555 | - settings_fields('algolia_woo_options'); |
|
556 | - do_settings_sections('algolia_woo_indexer'); |
|
557 | - submit_button('', 'primary wide'); ?> |
|
555 | + settings_fields('algolia_woo_options'); |
|
556 | + do_settings_sections('algolia_woo_indexer'); |
|
557 | + submit_button('', 'primary wide'); ?> |
|
558 | 558 | </form> |
559 | 559 | <form action="<?php echo esc_url(self::$plugin_url); ?>" method="POST"> |
560 | 560 | <?php wp_nonce_field('send_products_to_algolia_nonce_action', 'send_products_to_algolia_nonce_name'); ?> |
@@ -563,101 +563,101 @@ discard block |
||
563 | 563 | </form> |
564 | 564 | </div> |
565 | 565 | <?php |
566 | - } |
|
567 | - |
|
568 | - /** |
|
569 | - * Get active object instance |
|
570 | - * |
|
571 | - * @return object |
|
572 | - */ |
|
573 | - public static function get_instance() |
|
574 | - { |
|
575 | - if (!self::$instance) { |
|
576 | - self::$instance = new Algolia_Woo_Indexer(); |
|
577 | - } |
|
578 | - return self::$instance; |
|
579 | - } |
|
580 | - |
|
581 | - /** |
|
582 | - * The actions to execute when the plugin is activated. |
|
583 | - * |
|
584 | - * @return void |
|
585 | - */ |
|
586 | - public static function activate_plugin() |
|
587 | - { |
|
588 | - |
|
589 | - /** |
|
590 | - * Set default values for options if not already set |
|
591 | - */ |
|
592 | - $auto_send = get_option(ALGOWOO_DB_OPTION . AUTOMATICALLY_SEND_NEW_PRODUCTS); |
|
593 | - $algolia_application_id = get_option(ALGOWOO_DB_OPTION . ALGOLIA_APP_ID); |
|
594 | - $algolia_api_key = get_option(ALGOWOO_DB_OPTION . ALGOLIA_API_KEY); |
|
595 | - $algolia_index_name = get_option(ALGOWOO_DB_OPTION . INDEX_NAME); |
|
596 | - $custom_fields = get_option(ALGOWOO_DB_OPTION . CUSTOM_FIELDS); |
|
597 | - |
|
598 | - if (empty($auto_send)) { |
|
599 | - add_option( |
|
600 | - ALGOWOO_DB_OPTION . AUTOMATICALLY_SEND_NEW_PRODUCTS, |
|
601 | - '0' |
|
602 | - ); |
|
603 | - } |
|
604 | - |
|
605 | - if (empty($algolia_application_id)) { |
|
606 | - add_option( |
|
607 | - ALGOWOO_DB_OPTION . ALGOLIA_APP_ID, |
|
608 | - 'Change me' |
|
609 | - ); |
|
610 | - } |
|
611 | - |
|
612 | - if (empty($algolia_api_key)) { |
|
613 | - add_option( |
|
614 | - ALGOWOO_DB_OPTION . ALGOLIA_API_KEY, |
|
615 | - 'Change me' |
|
616 | - ); |
|
617 | - } |
|
618 | - |
|
619 | - if (empty($algolia_index_name)) { |
|
620 | - add_option( |
|
621 | - ALGOWOO_DB_OPTION . INDEX_NAME, |
|
622 | - 'Change me' |
|
623 | - ); |
|
624 | - } |
|
625 | - if (empty($custom_fields)) { |
|
626 | - add_option( |
|
627 | - ALGOWOO_DB_OPTION . CUSTOM_FIELDS, |
|
628 | - '' |
|
629 | - ); |
|
630 | - } |
|
631 | - |
|
632 | - /** |
|
633 | - * Set default values for index fields if not already set |
|
634 | - */ |
|
635 | - foreach (BASIC_FIELDS as $field) { |
|
636 | - $value = get_option(ALGOWOO_DB_OPTION . BASIC_FIELD_PREFIX . $field); |
|
637 | - if (empty($value)) { |
|
638 | - update_option( |
|
639 | - ALGOWOO_DB_OPTION . BASIC_FIELD_PREFIX . $field, |
|
640 | - '1' |
|
641 | - ); |
|
642 | - } |
|
643 | - } |
|
644 | - |
|
645 | - /** |
|
646 | - * activate attributes |
|
647 | - */ |
|
648 | - Algolia_Attributes::activate_attributes(); |
|
649 | - |
|
650 | - set_transient(self::PLUGIN_TRANSIENT, true); |
|
651 | - } |
|
652 | - |
|
653 | - /** |
|
654 | - * The actions to execute when the plugin is deactivated. |
|
655 | - * |
|
656 | - * @return void |
|
657 | - */ |
|
658 | - public static function deactivate_plugin() |
|
659 | - { |
|
660 | - delete_transient(self::PLUGIN_TRANSIENT); |
|
661 | - } |
|
662 | - } |
|
566 | + } |
|
567 | + |
|
568 | + /** |
|
569 | + * Get active object instance |
|
570 | + * |
|
571 | + * @return object |
|
572 | + */ |
|
573 | + public static function get_instance() |
|
574 | + { |
|
575 | + if (!self::$instance) { |
|
576 | + self::$instance = new Algolia_Woo_Indexer(); |
|
577 | + } |
|
578 | + return self::$instance; |
|
579 | + } |
|
580 | + |
|
581 | + /** |
|
582 | + * The actions to execute when the plugin is activated. |
|
583 | + * |
|
584 | + * @return void |
|
585 | + */ |
|
586 | + public static function activate_plugin() |
|
587 | + { |
|
588 | + |
|
589 | + /** |
|
590 | + * Set default values for options if not already set |
|
591 | + */ |
|
592 | + $auto_send = get_option(ALGOWOO_DB_OPTION . AUTOMATICALLY_SEND_NEW_PRODUCTS); |
|
593 | + $algolia_application_id = get_option(ALGOWOO_DB_OPTION . ALGOLIA_APP_ID); |
|
594 | + $algolia_api_key = get_option(ALGOWOO_DB_OPTION . ALGOLIA_API_KEY); |
|
595 | + $algolia_index_name = get_option(ALGOWOO_DB_OPTION . INDEX_NAME); |
|
596 | + $custom_fields = get_option(ALGOWOO_DB_OPTION . CUSTOM_FIELDS); |
|
597 | + |
|
598 | + if (empty($auto_send)) { |
|
599 | + add_option( |
|
600 | + ALGOWOO_DB_OPTION . AUTOMATICALLY_SEND_NEW_PRODUCTS, |
|
601 | + '0' |
|
602 | + ); |
|
603 | + } |
|
604 | + |
|
605 | + if (empty($algolia_application_id)) { |
|
606 | + add_option( |
|
607 | + ALGOWOO_DB_OPTION . ALGOLIA_APP_ID, |
|
608 | + 'Change me' |
|
609 | + ); |
|
610 | + } |
|
611 | + |
|
612 | + if (empty($algolia_api_key)) { |
|
613 | + add_option( |
|
614 | + ALGOWOO_DB_OPTION . ALGOLIA_API_KEY, |
|
615 | + 'Change me' |
|
616 | + ); |
|
617 | + } |
|
618 | + |
|
619 | + if (empty($algolia_index_name)) { |
|
620 | + add_option( |
|
621 | + ALGOWOO_DB_OPTION . INDEX_NAME, |
|
622 | + 'Change me' |
|
623 | + ); |
|
624 | + } |
|
625 | + if (empty($custom_fields)) { |
|
626 | + add_option( |
|
627 | + ALGOWOO_DB_OPTION . CUSTOM_FIELDS, |
|
628 | + '' |
|
629 | + ); |
|
630 | + } |
|
631 | + |
|
632 | + /** |
|
633 | + * Set default values for index fields if not already set |
|
634 | + */ |
|
635 | + foreach (BASIC_FIELDS as $field) { |
|
636 | + $value = get_option(ALGOWOO_DB_OPTION . BASIC_FIELD_PREFIX . $field); |
|
637 | + if (empty($value)) { |
|
638 | + update_option( |
|
639 | + ALGOWOO_DB_OPTION . BASIC_FIELD_PREFIX . $field, |
|
640 | + '1' |
|
641 | + ); |
|
642 | + } |
|
643 | + } |
|
644 | + |
|
645 | + /** |
|
646 | + * activate attributes |
|
647 | + */ |
|
648 | + Algolia_Attributes::activate_attributes(); |
|
649 | + |
|
650 | + set_transient(self::PLUGIN_TRANSIENT, true); |
|
651 | + } |
|
652 | + |
|
653 | + /** |
|
654 | + * The actions to execute when the plugin is deactivated. |
|
655 | + * |
|
656 | + * @return void |
|
657 | + */ |
|
658 | + public static function deactivate_plugin() |
|
659 | + { |
|
660 | + delete_transient(self::PLUGIN_TRANSIENT); |
|
661 | + } |
|
662 | + } |
|
663 | 663 | } |
@@ -16,14 +16,14 @@ discard block |
||
16 | 16 | * Abort if this file is called directly |
17 | 17 | */ |
18 | 18 | if (!defined('ABSPATH')) { |
19 | - exit; |
|
19 | + exit; |
|
20 | 20 | } |
21 | 21 | |
22 | 22 | /** |
23 | 23 | * Include plugin file if function is_plugin_active does not exist |
24 | 24 | */ |
25 | 25 | if (!function_exists('is_plugin_active')) { |
26 | - require_once(ABSPATH . '/wp-admin/includes/plugin.php'); |
|
26 | + require_once(ABSPATH . '/wp-admin/includes/plugin.php'); |
|
27 | 27 | } |
28 | 28 | |
29 | 29 | /** |
@@ -41,19 +41,19 @@ discard block |
||
41 | 41 | * Define list of fields available to index |
42 | 42 | */ |
43 | 43 | define('BASIC_FIELDS', array( |
44 | - 'product_name', |
|
45 | - 'permalink', |
|
46 | - 'tags', |
|
47 | - 'categories', |
|
48 | - 'short_description', |
|
49 | - 'long_description', |
|
50 | - 'excerpt', |
|
51 | - 'product_image', |
|
52 | - 'regular_price', |
|
53 | - 'sale_price', |
|
54 | - 'on_sale', |
|
55 | - "stock_quantity", |
|
56 | - "stock_status" |
|
44 | + 'product_name', |
|
45 | + 'permalink', |
|
46 | + 'tags', |
|
47 | + 'categories', |
|
48 | + 'short_description', |
|
49 | + 'long_description', |
|
50 | + 'excerpt', |
|
51 | + 'product_image', |
|
52 | + 'regular_price', |
|
53 | + 'sale_price', |
|
54 | + 'on_sale', |
|
55 | + "stock_quantity", |
|
56 | + "stock_status" |
|
57 | 57 | )); |
58 | 58 | |
59 | 59 | /** |
@@ -68,339 +68,339 @@ discard block |
||
68 | 68 | |
69 | 69 | |
70 | 70 | if (!class_exists('Algolia_Send_Products')) { |
71 | - /** |
|
72 | - * Algolia WooIndexer main class |
|
73 | - */ |
|
74 | - // TODO Rename class "Algolia_Send_Products" to match the regular expression ^[A-Z][a-zA-Z0-9]*$. |
|
75 | - class Algolia_Send_Products |
|
76 | - { |
|
77 | - const PLUGIN_NAME = 'Algolia Woo Indexer'; |
|
78 | - const PLUGIN_TRANSIENT = 'algowoo-plugin-notice'; |
|
79 | - |
|
80 | - /** |
|
81 | - * The Algolia instance |
|
82 | - * |
|
83 | - * @var \Algolia\AlgoliaSearch\SearchClient |
|
84 | - */ |
|
85 | - private static $algolia = null; |
|
86 | - |
|
87 | - /** |
|
88 | - * Check if we can connect to Algolia, if not, handle the exception, display an error and then return |
|
89 | - */ |
|
90 | - public static function can_connect_to_algolia() |
|
91 | - { |
|
92 | - try { |
|
93 | - self::$algolia->listApiKeys(); |
|
94 | - } catch (\Algolia\AlgoliaSearch\Exceptions\UnreachableException $error) { |
|
95 | - add_action( |
|
96 | - 'admin_notices', |
|
97 | - function () { |
|
98 | - echo '<div class="error notice"> |
|
71 | + /** |
|
72 | + * Algolia WooIndexer main class |
|
73 | + */ |
|
74 | + // TODO Rename class "Algolia_Send_Products" to match the regular expression ^[A-Z][a-zA-Z0-9]*$. |
|
75 | + class Algolia_Send_Products |
|
76 | + { |
|
77 | + const PLUGIN_NAME = 'Algolia Woo Indexer'; |
|
78 | + const PLUGIN_TRANSIENT = 'algowoo-plugin-notice'; |
|
79 | + |
|
80 | + /** |
|
81 | + * The Algolia instance |
|
82 | + * |
|
83 | + * @var \Algolia\AlgoliaSearch\SearchClient |
|
84 | + */ |
|
85 | + private static $algolia = null; |
|
86 | + |
|
87 | + /** |
|
88 | + * Check if we can connect to Algolia, if not, handle the exception, display an error and then return |
|
89 | + */ |
|
90 | + public static function can_connect_to_algolia() |
|
91 | + { |
|
92 | + try { |
|
93 | + self::$algolia->listApiKeys(); |
|
94 | + } catch (\Algolia\AlgoliaSearch\Exceptions\UnreachableException $error) { |
|
95 | + add_action( |
|
96 | + 'admin_notices', |
|
97 | + function () { |
|
98 | + echo '<div class="error notice"> |
|
99 | 99 | <p>' . esc_html__('An error has been encountered. Please check your application ID and API key. ', 'algolia-woo-indexer') . '</p> |
100 | 100 | </div>'; |
101 | - } |
|
102 | - ); |
|
103 | - return; |
|
104 | - } |
|
105 | - } |
|
106 | - |
|
107 | - /** |
|
108 | - * check if the field is enabled and shall be sent |
|
109 | - * |
|
110 | - * @param mixed $field name of field to be checked according to BASIC_FIELDS |
|
111 | - * @return boolean true if enable, false is not enabled |
|
112 | - */ |
|
113 | - public static function is_basic_field_enabled($field) |
|
114 | - { |
|
115 | - $fieldValue = get_option(ALGOWOO_DB_OPTION . BASIC_FIELD_PREFIX . $field); |
|
116 | - return $fieldValue; |
|
117 | - } |
|
118 | - |
|
119 | - /** |
|
120 | - * helper function to add a field to a record while checking their state |
|
121 | - * |
|
122 | - * @param array $record existing record where the field and value shall be added to |
|
123 | - * @param string $field name of field to be checked according to BASIC_FIELDS |
|
124 | - * @param mixed $value data to be added to the record array named to $field |
|
125 | - * @param boolean $skip_basic_field_validation set to true if it is not a basic field to skip validation |
|
126 | - * @return array $record previous passed $record with added field data |
|
127 | - */ |
|
128 | - public static function add_to_record($record, $field, $value, $skip_basic_field_validation = false) |
|
129 | - { |
|
130 | - /** |
|
131 | - * only if enabled or validation skipped and not empty |
|
132 | - */ |
|
133 | - if ((!self::is_basic_field_enabled($field) && !$skip_basic_field_validation) || empty($value)) { |
|
134 | - return $record; |
|
135 | - } |
|
136 | - |
|
137 | - $record[$field] = $value; |
|
138 | - return $record; |
|
139 | - } |
|
140 | - |
|
141 | - /** |
|
142 | - * Get sale price or regular price based on product type |
|
143 | - * |
|
144 | - * @param mixed $product Product to check |
|
145 | - * @return array ['sale_price' => $sale_price,'regular_price' => $regular_price] Array with regular price and sale price |
|
146 | - */ |
|
147 | - public static function get_product_type_price($product) |
|
148 | - { |
|
149 | - $sale_price = 0; |
|
150 | - $regular_price = 0; |
|
151 | - if ($product->is_type('simple')) { |
|
152 | - $sale_price = $product->get_sale_price(); |
|
153 | - $regular_price = $product->get_regular_price(); |
|
154 | - } elseif ($product->is_type('variable')) { |
|
155 | - $sale_price = $product->get_variation_sale_price('min', true); |
|
156 | - $regular_price = $product->get_variation_regular_price('max', true); |
|
157 | - } |
|
158 | - return array( |
|
159 | - 'sale_price' => $sale_price, |
|
160 | - 'regular_price' => $regular_price |
|
161 | - ); |
|
162 | - } |
|
163 | - |
|
164 | - |
|
165 | - /** |
|
166 | - * Checks if stock management is enabled and if so, returns quantity and status |
|
167 | - * |
|
168 | - * @param mixed $product Product to check |
|
169 | - * @return array ['stock_quantity' => $stock_quantity,'stock_status' => $stock_status] Array with quantity and status. if stock management is disabled, false will be returned, |
|
170 | - */ |
|
171 | - public static function get_product_stock_data($product) |
|
172 | - { |
|
173 | - if ($product->get_manage_stock()) { |
|
174 | - return array( |
|
175 | - 'stock_quantity' => $product->get_stock_quantity(), |
|
176 | - 'stock_status' => $product->get_stock_status() |
|
177 | - ); |
|
178 | - } |
|
179 | - return false; |
|
180 | - } |
|
181 | - |
|
182 | - /** |
|
183 | - * Checks if stock management is enabled and if so, returns quantity and status |
|
184 | - * |
|
185 | - * @param mixed $product Product to check |
|
186 | - * @return array ['stock_quantity' => $stock_quantity,'stock_status' => $stock_status] Array with quantity and status. if stock management is disabled, false will be returned, |
|
187 | - */ |
|
188 | - public static function get_custom_fields($product) |
|
189 | - { |
|
190 | - $custom_fields_string = get_option(ALGOWOO_DB_OPTION . CUSTOM_FIELDS); |
|
191 | - $custom_fields_array = explode(",", $custom_fields_string); |
|
192 | - $custom_field_with_values = array(); |
|
193 | - foreach ($custom_fields_array as $custom_field) { |
|
194 | - $value = get_post_meta($product->get_id(), $custom_field); |
|
195 | - if (!empty($value)) { |
|
196 | - $custom_field_with_values[$custom_field] = $value; |
|
197 | - } |
|
198 | - } |
|
199 | - return $custom_field_with_values; |
|
200 | - } |
|
201 | - |
|
202 | - /** |
|
203 | - * Get product tags |
|
204 | - * |
|
205 | - * @param mixed $product Product to check |
|
206 | - * @return array ['tag1', 'tag2', ...] simple array with associated tags |
|
207 | - */ |
|
208 | - public static function get_product_tags($product) |
|
209 | - { |
|
210 | - $tags = get_the_terms($product->get_id(), 'product_tag'); |
|
211 | - $term_array = array(); |
|
212 | - if (is_array($tags)) { |
|
213 | - foreach ($tags as $tag) { |
|
214 | - $name = get_term($tag)->name; |
|
215 | - array_push($term_array, $name); |
|
216 | - } |
|
217 | - } |
|
218 | - return $term_array; |
|
219 | - } |
|
220 | - |
|
221 | - /** |
|
222 | - * Get product categories |
|
223 | - * |
|
224 | - * @param mixed $product Product to check |
|
225 | - * @return array ['tag1', 'tag2', ...] simple array with associated categories |
|
226 | - */ |
|
227 | - public static function get_product_categories($product) |
|
228 | - { |
|
229 | - $categories = get_the_terms($product->get_id(), 'product_cat'); |
|
230 | - $term_array = array(); |
|
231 | - foreach ($categories as $category) { |
|
232 | - $name = get_term($category)->name; |
|
233 | - $slug = get_term($category)->slug; |
|
234 | - array_push($term_array, array( |
|
235 | - "name" => $name, |
|
236 | - "slug" => $slug |
|
237 | - )); |
|
238 | - } |
|
239 | - return $term_array; |
|
240 | - } |
|
241 | - |
|
242 | - |
|
243 | - /** |
|
244 | - * Send WooCommerce products to Algolia |
|
245 | - * |
|
246 | - * @param Int $id Product to send to Algolia if we send only a single product |
|
247 | - * @return void |
|
248 | - */ |
|
249 | - public static function send_products_to_algolia($id = '') |
|
250 | - { |
|
251 | - /** |
|
252 | - * Remove classes from plugin URL and autoload Algolia with Composer |
|
253 | - */ |
|
254 | - |
|
255 | - $base_plugin_directory = str_replace('classes', '', dirname(__FILE__)); |
|
256 | - require_once $base_plugin_directory . '/vendor/autoload.php'; |
|
257 | - |
|
258 | - /** |
|
259 | - * Fetch the required variables from the Settings API |
|
260 | - */ |
|
261 | - |
|
262 | - $algolia_application_id = get_option(ALGOWOO_DB_OPTION . ALGOLIA_APP_ID); |
|
263 | - $algolia_application_id = is_string($algolia_application_id) ? $algolia_application_id : CHANGE_ME; |
|
264 | - |
|
265 | - $algolia_api_key = get_option(ALGOWOO_DB_OPTION . ALGOLIA_API_KEY); |
|
266 | - $algolia_api_key = is_string($algolia_api_key) ? $algolia_api_key : CHANGE_ME; |
|
267 | - |
|
268 | - $algolia_index_name = get_option(ALGOWOO_DB_OPTION . INDEX_NAME); |
|
269 | - $algolia_index_name = is_string($algolia_index_name) ? $algolia_index_name : CHANGE_ME; |
|
270 | - |
|
271 | - /** |
|
272 | - * Display admin notice and return if not all values have been set |
|
273 | - */ |
|
274 | - |
|
275 | - Algolia_Check_Requirements::check_algolia_input_values($algolia_application_id, $algolia_api_key, $algolia_index_name); |
|
276 | - |
|
277 | - /** |
|
278 | - * Initiate the Algolia client |
|
279 | - */ |
|
280 | - self::$algolia = \Algolia\AlgoliaSearch\SearchClient::create($algolia_application_id, $algolia_api_key); |
|
281 | - |
|
282 | - /** |
|
283 | - * Check if we can connect, if not, handle the exception, display an error and then return |
|
284 | - */ |
|
285 | - self::can_connect_to_algolia(); |
|
286 | - |
|
287 | - /** |
|
288 | - * Initialize the search index and set the name to the option from the database |
|
289 | - */ |
|
290 | - $index = self::$algolia->initIndex($algolia_index_name); |
|
291 | - |
|
292 | - /** |
|
293 | - * Setup arguments for sending all products to Algolia |
|
294 | - * |
|
295 | - * Limit => -1 means we send all products |
|
296 | - */ |
|
297 | - $arguments = array( |
|
298 | - 'status' => 'publish', |
|
299 | - 'limit' => -1, |
|
300 | - 'paginate' => false, |
|
301 | - ); |
|
302 | - |
|
303 | - /** |
|
304 | - * Setup arguments for sending only a single product |
|
305 | - */ |
|
306 | - if (isset($id) && '' !== $id) { |
|
307 | - $arguments = array( |
|
308 | - 'status' => 'publish', |
|
309 | - 'include' => array($id), |
|
310 | - 'paginate' => false, |
|
311 | - ); |
|
312 | - } |
|
313 | - |
|
314 | - /** |
|
315 | - * Fetch all products from WooCommerce |
|
316 | - * |
|
317 | - * @see https://docs.woocommerce.com/wc-apidocs/function-wc_get_products.html |
|
318 | - */ |
|
319 | - $products = |
|
320 | - /** @scrutinizer ignore-call */ |
|
321 | - wc_get_products($arguments); |
|
322 | - |
|
323 | - if (empty($products)) { |
|
324 | - return; |
|
325 | - } |
|
326 | - $records = array(); |
|
327 | - $record = array(); |
|
328 | - |
|
329 | - foreach ($products as $product) { |
|
330 | - /** |
|
331 | - * Set sale price or regular price based on product type |
|
332 | - */ |
|
333 | - $product_type_price = self::get_product_type_price($product); |
|
334 | - $sale_price = $product_type_price['sale_price']; |
|
335 | - $regular_price = $product_type_price['regular_price']; |
|
336 | - |
|
337 | - |
|
338 | - |
|
339 | - |
|
340 | - /** |
|
341 | - * always add objectID (mandatory field for algolia) |
|
342 | - */ |
|
343 | - $record['objectID'] = $product->get_id(); |
|
344 | - |
|
345 | - /** |
|
346 | - * Extract image from $product->get_image() |
|
347 | - */ |
|
348 | - if (self::is_basic_field_enabled("product_image")) { |
|
349 | - preg_match('/<img(.*)src(.*)=(.*)"(.*)"/U', $product->get_image(), $result); |
|
350 | - $record["product_image"] = array_pop($result); |
|
351 | - } |
|
352 | - |
|
353 | - $record = self::add_to_record($record, 'product_name', $product->get_name()); |
|
354 | - $record = self::add_to_record($record, 'short_description', $product->get_short_description()); |
|
355 | - $record = self::add_to_record($record, 'long_description', $product->get_description()); |
|
356 | - $record = self::add_to_record($record, 'excerpt', get_the_excerpt($product->get_id())); |
|
357 | - $record = self::add_to_record($record, 'regular_price', $regular_price); |
|
358 | - $record = self::add_to_record($record, 'sale_price', $sale_price); |
|
359 | - $record = self::add_to_record($record, 'on_sale', $product->is_on_sale()); |
|
360 | - $record = self::add_to_record($record, 'permalink', $product->get_permalink()); |
|
361 | - $record = self::add_to_record($record, 'categories', self::get_product_categories($product)); |
|
362 | - $record = self::add_to_record($record, 'tags', self::get_product_tags($product)); |
|
363 | - $record = self::add_to_record($record, 'attributes', Algolia_Attributes::get_product_attributes($product), true); |
|
364 | - |
|
365 | - /** |
|
366 | - * get custom fields and merge |
|
367 | - */ |
|
368 | - $custom_fields = self::get_custom_fields($product); |
|
369 | - if (!empty($custom_fields)) { |
|
370 | - $record = array_merge($record, $custom_fields); |
|
371 | - } |
|
372 | - |
|
373 | - |
|
374 | - /** |
|
375 | - * Add stock information if stock management is on |
|
376 | - */ |
|
377 | - $stock_data = self::get_product_stock_data($product); |
|
378 | - if (is_array($stock_data)) { |
|
379 | - $record = self::add_to_record($record, 'stock_quantity', $stock_data['stock_quantity']); |
|
380 | - $record = self::add_to_record($record, 'stock_status', $stock_data['stock_status']); |
|
381 | - } |
|
382 | - |
|
383 | - $records[] = $record; |
|
384 | - } |
|
385 | - |
|
386 | - wp_reset_postdata(); |
|
387 | - |
|
388 | - /** |
|
389 | - * Send the information to Algolia and save the result |
|
390 | - * If result is NullResponse, print an error message |
|
391 | - */ |
|
392 | - $result = $index->saveObjects($records); |
|
393 | - |
|
394 | - if ('Algolia\AlgoliaSearch\Response\NullResponse' === get_class($result)) { |
|
395 | - wp_die(esc_html__('No response from the server. Please check your settings and try again', 'algolia_woo_indexer_settings')); |
|
396 | - } |
|
397 | - |
|
398 | - /** |
|
399 | - * Display success message |
|
400 | - */ |
|
401 | - echo '<div class="notice notice-success is-dismissible"> |
|
101 | + } |
|
102 | + ); |
|
103 | + return; |
|
104 | + } |
|
105 | + } |
|
106 | + |
|
107 | + /** |
|
108 | + * check if the field is enabled and shall be sent |
|
109 | + * |
|
110 | + * @param mixed $field name of field to be checked according to BASIC_FIELDS |
|
111 | + * @return boolean true if enable, false is not enabled |
|
112 | + */ |
|
113 | + public static function is_basic_field_enabled($field) |
|
114 | + { |
|
115 | + $fieldValue = get_option(ALGOWOO_DB_OPTION . BASIC_FIELD_PREFIX . $field); |
|
116 | + return $fieldValue; |
|
117 | + } |
|
118 | + |
|
119 | + /** |
|
120 | + * helper function to add a field to a record while checking their state |
|
121 | + * |
|
122 | + * @param array $record existing record where the field and value shall be added to |
|
123 | + * @param string $field name of field to be checked according to BASIC_FIELDS |
|
124 | + * @param mixed $value data to be added to the record array named to $field |
|
125 | + * @param boolean $skip_basic_field_validation set to true if it is not a basic field to skip validation |
|
126 | + * @return array $record previous passed $record with added field data |
|
127 | + */ |
|
128 | + public static function add_to_record($record, $field, $value, $skip_basic_field_validation = false) |
|
129 | + { |
|
130 | + /** |
|
131 | + * only if enabled or validation skipped and not empty |
|
132 | + */ |
|
133 | + if ((!self::is_basic_field_enabled($field) && !$skip_basic_field_validation) || empty($value)) { |
|
134 | + return $record; |
|
135 | + } |
|
136 | + |
|
137 | + $record[$field] = $value; |
|
138 | + return $record; |
|
139 | + } |
|
140 | + |
|
141 | + /** |
|
142 | + * Get sale price or regular price based on product type |
|
143 | + * |
|
144 | + * @param mixed $product Product to check |
|
145 | + * @return array ['sale_price' => $sale_price,'regular_price' => $regular_price] Array with regular price and sale price |
|
146 | + */ |
|
147 | + public static function get_product_type_price($product) |
|
148 | + { |
|
149 | + $sale_price = 0; |
|
150 | + $regular_price = 0; |
|
151 | + if ($product->is_type('simple')) { |
|
152 | + $sale_price = $product->get_sale_price(); |
|
153 | + $regular_price = $product->get_regular_price(); |
|
154 | + } elseif ($product->is_type('variable')) { |
|
155 | + $sale_price = $product->get_variation_sale_price('min', true); |
|
156 | + $regular_price = $product->get_variation_regular_price('max', true); |
|
157 | + } |
|
158 | + return array( |
|
159 | + 'sale_price' => $sale_price, |
|
160 | + 'regular_price' => $regular_price |
|
161 | + ); |
|
162 | + } |
|
163 | + |
|
164 | + |
|
165 | + /** |
|
166 | + * Checks if stock management is enabled and if so, returns quantity and status |
|
167 | + * |
|
168 | + * @param mixed $product Product to check |
|
169 | + * @return array ['stock_quantity' => $stock_quantity,'stock_status' => $stock_status] Array with quantity and status. if stock management is disabled, false will be returned, |
|
170 | + */ |
|
171 | + public static function get_product_stock_data($product) |
|
172 | + { |
|
173 | + if ($product->get_manage_stock()) { |
|
174 | + return array( |
|
175 | + 'stock_quantity' => $product->get_stock_quantity(), |
|
176 | + 'stock_status' => $product->get_stock_status() |
|
177 | + ); |
|
178 | + } |
|
179 | + return false; |
|
180 | + } |
|
181 | + |
|
182 | + /** |
|
183 | + * Checks if stock management is enabled and if so, returns quantity and status |
|
184 | + * |
|
185 | + * @param mixed $product Product to check |
|
186 | + * @return array ['stock_quantity' => $stock_quantity,'stock_status' => $stock_status] Array with quantity and status. if stock management is disabled, false will be returned, |
|
187 | + */ |
|
188 | + public static function get_custom_fields($product) |
|
189 | + { |
|
190 | + $custom_fields_string = get_option(ALGOWOO_DB_OPTION . CUSTOM_FIELDS); |
|
191 | + $custom_fields_array = explode(",", $custom_fields_string); |
|
192 | + $custom_field_with_values = array(); |
|
193 | + foreach ($custom_fields_array as $custom_field) { |
|
194 | + $value = get_post_meta($product->get_id(), $custom_field); |
|
195 | + if (!empty($value)) { |
|
196 | + $custom_field_with_values[$custom_field] = $value; |
|
197 | + } |
|
198 | + } |
|
199 | + return $custom_field_with_values; |
|
200 | + } |
|
201 | + |
|
202 | + /** |
|
203 | + * Get product tags |
|
204 | + * |
|
205 | + * @param mixed $product Product to check |
|
206 | + * @return array ['tag1', 'tag2', ...] simple array with associated tags |
|
207 | + */ |
|
208 | + public static function get_product_tags($product) |
|
209 | + { |
|
210 | + $tags = get_the_terms($product->get_id(), 'product_tag'); |
|
211 | + $term_array = array(); |
|
212 | + if (is_array($tags)) { |
|
213 | + foreach ($tags as $tag) { |
|
214 | + $name = get_term($tag)->name; |
|
215 | + array_push($term_array, $name); |
|
216 | + } |
|
217 | + } |
|
218 | + return $term_array; |
|
219 | + } |
|
220 | + |
|
221 | + /** |
|
222 | + * Get product categories |
|
223 | + * |
|
224 | + * @param mixed $product Product to check |
|
225 | + * @return array ['tag1', 'tag2', ...] simple array with associated categories |
|
226 | + */ |
|
227 | + public static function get_product_categories($product) |
|
228 | + { |
|
229 | + $categories = get_the_terms($product->get_id(), 'product_cat'); |
|
230 | + $term_array = array(); |
|
231 | + foreach ($categories as $category) { |
|
232 | + $name = get_term($category)->name; |
|
233 | + $slug = get_term($category)->slug; |
|
234 | + array_push($term_array, array( |
|
235 | + "name" => $name, |
|
236 | + "slug" => $slug |
|
237 | + )); |
|
238 | + } |
|
239 | + return $term_array; |
|
240 | + } |
|
241 | + |
|
242 | + |
|
243 | + /** |
|
244 | + * Send WooCommerce products to Algolia |
|
245 | + * |
|
246 | + * @param Int $id Product to send to Algolia if we send only a single product |
|
247 | + * @return void |
|
248 | + */ |
|
249 | + public static function send_products_to_algolia($id = '') |
|
250 | + { |
|
251 | + /** |
|
252 | + * Remove classes from plugin URL and autoload Algolia with Composer |
|
253 | + */ |
|
254 | + |
|
255 | + $base_plugin_directory = str_replace('classes', '', dirname(__FILE__)); |
|
256 | + require_once $base_plugin_directory . '/vendor/autoload.php'; |
|
257 | + |
|
258 | + /** |
|
259 | + * Fetch the required variables from the Settings API |
|
260 | + */ |
|
261 | + |
|
262 | + $algolia_application_id = get_option(ALGOWOO_DB_OPTION . ALGOLIA_APP_ID); |
|
263 | + $algolia_application_id = is_string($algolia_application_id) ? $algolia_application_id : CHANGE_ME; |
|
264 | + |
|
265 | + $algolia_api_key = get_option(ALGOWOO_DB_OPTION . ALGOLIA_API_KEY); |
|
266 | + $algolia_api_key = is_string($algolia_api_key) ? $algolia_api_key : CHANGE_ME; |
|
267 | + |
|
268 | + $algolia_index_name = get_option(ALGOWOO_DB_OPTION . INDEX_NAME); |
|
269 | + $algolia_index_name = is_string($algolia_index_name) ? $algolia_index_name : CHANGE_ME; |
|
270 | + |
|
271 | + /** |
|
272 | + * Display admin notice and return if not all values have been set |
|
273 | + */ |
|
274 | + |
|
275 | + Algolia_Check_Requirements::check_algolia_input_values($algolia_application_id, $algolia_api_key, $algolia_index_name); |
|
276 | + |
|
277 | + /** |
|
278 | + * Initiate the Algolia client |
|
279 | + */ |
|
280 | + self::$algolia = \Algolia\AlgoliaSearch\SearchClient::create($algolia_application_id, $algolia_api_key); |
|
281 | + |
|
282 | + /** |
|
283 | + * Check if we can connect, if not, handle the exception, display an error and then return |
|
284 | + */ |
|
285 | + self::can_connect_to_algolia(); |
|
286 | + |
|
287 | + /** |
|
288 | + * Initialize the search index and set the name to the option from the database |
|
289 | + */ |
|
290 | + $index = self::$algolia->initIndex($algolia_index_name); |
|
291 | + |
|
292 | + /** |
|
293 | + * Setup arguments for sending all products to Algolia |
|
294 | + * |
|
295 | + * Limit => -1 means we send all products |
|
296 | + */ |
|
297 | + $arguments = array( |
|
298 | + 'status' => 'publish', |
|
299 | + 'limit' => -1, |
|
300 | + 'paginate' => false, |
|
301 | + ); |
|
302 | + |
|
303 | + /** |
|
304 | + * Setup arguments for sending only a single product |
|
305 | + */ |
|
306 | + if (isset($id) && '' !== $id) { |
|
307 | + $arguments = array( |
|
308 | + 'status' => 'publish', |
|
309 | + 'include' => array($id), |
|
310 | + 'paginate' => false, |
|
311 | + ); |
|
312 | + } |
|
313 | + |
|
314 | + /** |
|
315 | + * Fetch all products from WooCommerce |
|
316 | + * |
|
317 | + * @see https://docs.woocommerce.com/wc-apidocs/function-wc_get_products.html |
|
318 | + */ |
|
319 | + $products = |
|
320 | + /** @scrutinizer ignore-call */ |
|
321 | + wc_get_products($arguments); |
|
322 | + |
|
323 | + if (empty($products)) { |
|
324 | + return; |
|
325 | + } |
|
326 | + $records = array(); |
|
327 | + $record = array(); |
|
328 | + |
|
329 | + foreach ($products as $product) { |
|
330 | + /** |
|
331 | + * Set sale price or regular price based on product type |
|
332 | + */ |
|
333 | + $product_type_price = self::get_product_type_price($product); |
|
334 | + $sale_price = $product_type_price['sale_price']; |
|
335 | + $regular_price = $product_type_price['regular_price']; |
|
336 | + |
|
337 | + |
|
338 | + |
|
339 | + |
|
340 | + /** |
|
341 | + * always add objectID (mandatory field for algolia) |
|
342 | + */ |
|
343 | + $record['objectID'] = $product->get_id(); |
|
344 | + |
|
345 | + /** |
|
346 | + * Extract image from $product->get_image() |
|
347 | + */ |
|
348 | + if (self::is_basic_field_enabled("product_image")) { |
|
349 | + preg_match('/<img(.*)src(.*)=(.*)"(.*)"/U', $product->get_image(), $result); |
|
350 | + $record["product_image"] = array_pop($result); |
|
351 | + } |
|
352 | + |
|
353 | + $record = self::add_to_record($record, 'product_name', $product->get_name()); |
|
354 | + $record = self::add_to_record($record, 'short_description', $product->get_short_description()); |
|
355 | + $record = self::add_to_record($record, 'long_description', $product->get_description()); |
|
356 | + $record = self::add_to_record($record, 'excerpt', get_the_excerpt($product->get_id())); |
|
357 | + $record = self::add_to_record($record, 'regular_price', $regular_price); |
|
358 | + $record = self::add_to_record($record, 'sale_price', $sale_price); |
|
359 | + $record = self::add_to_record($record, 'on_sale', $product->is_on_sale()); |
|
360 | + $record = self::add_to_record($record, 'permalink', $product->get_permalink()); |
|
361 | + $record = self::add_to_record($record, 'categories', self::get_product_categories($product)); |
|
362 | + $record = self::add_to_record($record, 'tags', self::get_product_tags($product)); |
|
363 | + $record = self::add_to_record($record, 'attributes', Algolia_Attributes::get_product_attributes($product), true); |
|
364 | + |
|
365 | + /** |
|
366 | + * get custom fields and merge |
|
367 | + */ |
|
368 | + $custom_fields = self::get_custom_fields($product); |
|
369 | + if (!empty($custom_fields)) { |
|
370 | + $record = array_merge($record, $custom_fields); |
|
371 | + } |
|
372 | + |
|
373 | + |
|
374 | + /** |
|
375 | + * Add stock information if stock management is on |
|
376 | + */ |
|
377 | + $stock_data = self::get_product_stock_data($product); |
|
378 | + if (is_array($stock_data)) { |
|
379 | + $record = self::add_to_record($record, 'stock_quantity', $stock_data['stock_quantity']); |
|
380 | + $record = self::add_to_record($record, 'stock_status', $stock_data['stock_status']); |
|
381 | + } |
|
382 | + |
|
383 | + $records[] = $record; |
|
384 | + } |
|
385 | + |
|
386 | + wp_reset_postdata(); |
|
387 | + |
|
388 | + /** |
|
389 | + * Send the information to Algolia and save the result |
|
390 | + * If result is NullResponse, print an error message |
|
391 | + */ |
|
392 | + $result = $index->saveObjects($records); |
|
393 | + |
|
394 | + if ('Algolia\AlgoliaSearch\Response\NullResponse' === get_class($result)) { |
|
395 | + wp_die(esc_html__('No response from the server. Please check your settings and try again', 'algolia_woo_indexer_settings')); |
|
396 | + } |
|
397 | + |
|
398 | + /** |
|
399 | + * Display success message |
|
400 | + */ |
|
401 | + echo '<div class="notice notice-success is-dismissible"> |
|
402 | 402 | <p>' . esc_html__('Product(s) sent to Algolia.', 'algolia-woo-indexer') . '</p> |
403 | 403 | </div>'; |
404 | - } |
|
405 | - } |
|
404 | + } |
|
405 | + } |
|
406 | 406 | } |
@@ -15,14 +15,14 @@ discard block |
||
15 | 15 | /** |
16 | 16 | * Abort if this file is called directly |
17 | 17 | */ |
18 | -if (!defined('ABSPATH')) { |
|
18 | +if ( ! defined('ABSPATH')) { |
|
19 | 19 | exit; |
20 | 20 | } |
21 | 21 | |
22 | 22 | /** |
23 | 23 | * Include plugin file if function is_plugin_active does not exist |
24 | 24 | */ |
25 | -if (!function_exists('is_plugin_active')) { |
|
25 | +if ( ! function_exists('is_plugin_active')) { |
|
26 | 26 | require_once(ABSPATH . '/wp-admin/includes/plugin.php'); |
27 | 27 | } |
28 | 28 | |
@@ -67,7 +67,7 @@ discard block |
||
67 | 67 | define('CUSTOM_FIELDS', '_custom_fields'); |
68 | 68 | |
69 | 69 | |
70 | -if (!class_exists('Algolia_Send_Products')) { |
|
70 | +if ( ! class_exists('Algolia_Send_Products')) { |
|
71 | 71 | /** |
72 | 72 | * Algolia WooIndexer main class |
73 | 73 | */ |
@@ -94,7 +94,7 @@ discard block |
||
94 | 94 | } catch (\Algolia\AlgoliaSearch\Exceptions\UnreachableException $error) { |
95 | 95 | add_action( |
96 | 96 | 'admin_notices', |
97 | - function () { |
|
97 | + function() { |
|
98 | 98 | echo '<div class="error notice"> |
99 | 99 | <p>' . esc_html__('An error has been encountered. Please check your application ID and API key. ', 'algolia-woo-indexer') . '</p> |
100 | 100 | </div>'; |
@@ -130,7 +130,7 @@ discard block |
||
130 | 130 | /** |
131 | 131 | * only if enabled or validation skipped and not empty |
132 | 132 | */ |
133 | - if ((!self::is_basic_field_enabled($field) && !$skip_basic_field_validation) || empty($value)) { |
|
133 | + if (( ! self::is_basic_field_enabled($field) && ! $skip_basic_field_validation) || empty($value)) { |
|
134 | 134 | return $record; |
135 | 135 | } |
136 | 136 | |
@@ -149,11 +149,11 @@ discard block |
||
149 | 149 | $sale_price = 0; |
150 | 150 | $regular_price = 0; |
151 | 151 | if ($product->is_type('simple')) { |
152 | - $sale_price = $product->get_sale_price(); |
|
153 | - $regular_price = $product->get_regular_price(); |
|
152 | + $sale_price = $product->get_sale_price(); |
|
153 | + $regular_price = $product->get_regular_price(); |
|
154 | 154 | } elseif ($product->is_type('variable')) { |
155 | - $sale_price = $product->get_variation_sale_price('min', true); |
|
156 | - $regular_price = $product->get_variation_regular_price('max', true); |
|
155 | + $sale_price = $product->get_variation_sale_price('min', true); |
|
156 | + $regular_price = $product->get_variation_regular_price('max', true); |
|
157 | 157 | } |
158 | 158 | return array( |
159 | 159 | 'sale_price' => $sale_price, |
@@ -192,7 +192,7 @@ discard block |
||
192 | 192 | $custom_field_with_values = array(); |
193 | 193 | foreach ($custom_fields_array as $custom_field) { |
194 | 194 | $value = get_post_meta($product->get_id(), $custom_field); |
195 | - if (!empty($value)) { |
|
195 | + if ( ! empty($value)) { |
|
196 | 196 | $custom_field_with_values[$custom_field] = $value; |
197 | 197 | } |
198 | 198 | } |
@@ -266,7 +266,7 @@ discard block |
||
266 | 266 | $algolia_api_key = is_string($algolia_api_key) ? $algolia_api_key : CHANGE_ME; |
267 | 267 | |
268 | 268 | $algolia_index_name = get_option(ALGOWOO_DB_OPTION . INDEX_NAME); |
269 | - $algolia_index_name = is_string($algolia_index_name) ? $algolia_index_name : CHANGE_ME; |
|
269 | + $algolia_index_name = is_string($algolia_index_name) ? $algolia_index_name : CHANGE_ME; |
|
270 | 270 | |
271 | 271 | /** |
272 | 272 | * Display admin notice and return if not all values have been set |
@@ -366,7 +366,7 @@ discard block |
||
366 | 366 | * get custom fields and merge |
367 | 367 | */ |
368 | 368 | $custom_fields = self::get_custom_fields($product); |
369 | - if (!empty($custom_fields)) { |
|
369 | + if ( ! empty($custom_fields)) { |
|
370 | 370 | $record = array_merge($record, $custom_fields); |
371 | 371 | } |
372 | 372 |
@@ -18,36 +18,36 @@ |
||
18 | 18 | /** |
19 | 19 | * Abort if this file is called directly |
20 | 20 | */ |
21 | -if ( ! defined( 'ABSPATH' ) ) { |
|
21 | +if ( ! defined('ABSPATH')) { |
|
22 | 22 | exit; |
23 | 23 | } |
24 | 24 | |
25 | 25 | /** |
26 | 26 | * Main class file |
27 | 27 | */ |
28 | -require_once plugin_dir_path( __FILE__ ) . '/classes/class-algolia-woo-indexer.php'; |
|
28 | +require_once plugin_dir_path(__FILE__) . '/classes/class-algolia-woo-indexer.php'; |
|
29 | 29 | |
30 | 30 | /** |
31 | 31 | * Class for checking plugin requirements |
32 | 32 | */ |
33 | -require_once plugin_dir_path( __FILE__ ) . '/classes/class-check-requirements.php'; |
|
33 | +require_once plugin_dir_path(__FILE__) . '/classes/class-check-requirements.php'; |
|
34 | 34 | |
35 | 35 | /** |
36 | 36 | * Class for verifying nonces |
37 | 37 | */ |
38 | -require_once plugin_dir_path( __FILE__ ) . '/classes/class-verify-nonces.php'; |
|
38 | +require_once plugin_dir_path(__FILE__) . '/classes/class-verify-nonces.php'; |
|
39 | 39 | |
40 | 40 | /** |
41 | 41 | * Class for attributes |
42 | 42 | */ |
43 | -require_once plugin_dir_path( __FILE__ ) . '/classes/class-attributes.php'; |
|
43 | +require_once plugin_dir_path(__FILE__) . '/classes/class-attributes.php'; |
|
44 | 44 | |
45 | 45 | /** |
46 | 46 | * Class for sending products |
47 | 47 | */ |
48 | -require_once plugin_dir_path( __FILE__ ) . '/classes/class-send-products.php'; |
|
48 | +require_once plugin_dir_path(__FILE__) . '/classes/class-send-products.php'; |
|
49 | 49 | |
50 | 50 | $algowooindexer = \Algowoo\Algolia_Woo_Indexer::get_instance(); |
51 | 51 | |
52 | -register_activation_hook( __FILE__, array( $algowooindexer, 'activate_plugin' ) ); |
|
53 | -register_deactivation_hook( __FILE__, array( $algowooindexer, 'deactivate_plugin' ) ); |
|
52 | +register_activation_hook(__FILE__, array($algowooindexer, 'activate_plugin')); |
|
53 | +register_deactivation_hook(__FILE__, array($algowooindexer, 'deactivate_plugin')); |
@@ -19,466 +19,466 @@ |
||
19 | 19 | define('ATTRIBUTES_INTERP', '_attributes_interp'); |
20 | 20 | define('ATTRIBUTES_TAX_FIELDS', '_attributes_tax_fields'); |
21 | 21 | define('ALLOWED_TAXONOMIES', array( |
22 | - 'term_id', |
|
23 | - 'name', |
|
24 | - 'slug', |
|
25 | - 'term_group', |
|
26 | - 'description', |
|
27 | - 'count', |
|
28 | - 'filter' |
|
22 | + 'term_id', |
|
23 | + 'name', |
|
24 | + 'slug', |
|
25 | + 'term_group', |
|
26 | + 'description', |
|
27 | + 'count', |
|
28 | + 'filter' |
|
29 | 29 | )); |
30 | 30 | |
31 | 31 | /** |
32 | 32 | * definitions of available settings |
33 | 33 | */ |
34 | 34 | define('ATTRIBUTES_SETTINGS', array( |
35 | - 'enabled' => 'Enable indexing of attributes', |
|
36 | - 'visibility' => 'Visibility', |
|
37 | - 'list' => 'Valid Attributes', |
|
38 | - 'interp' => 'Numeric Interpolation', |
|
39 | - 'tax_fields' => 'Content of each attribute term' |
|
35 | + 'enabled' => 'Enable indexing of attributes', |
|
36 | + 'visibility' => 'Visibility', |
|
37 | + 'list' => 'Valid Attributes', |
|
38 | + 'interp' => 'Numeric Interpolation', |
|
39 | + 'tax_fields' => 'Content of each attribute term' |
|
40 | 40 | )); |
41 | 41 | |
42 | 42 | /** |
43 | 43 | * Abort if this file is called directly |
44 | 44 | */ |
45 | 45 | if (!defined('ABSPATH')) { |
46 | - exit; |
|
46 | + exit; |
|
47 | 47 | } |
48 | 48 | |
49 | 49 | |
50 | 50 | if (!class_exists('Algolia_Attributes')) { |
51 | - /** |
|
52 | - * Algolia WooIndexer Attributes |
|
53 | - */ |
|
54 | - class Algolia_Attributes |
|
55 | - { |
|
56 | - |
|
57 | - |
|
58 | - /** |
|
59 | - * Class instance |
|
60 | - * |
|
61 | - * @var object |
|
62 | - */ |
|
63 | - private static $instance; |
|
64 | - |
|
65 | - |
|
66 | - /** |
|
67 | - * Setup sections and fields to store and retrieve values from Settings API |
|
68 | - * |
|
69 | - * @return void |
|
70 | - */ |
|
71 | - public static function setup_attributes_settings() |
|
72 | - { |
|
73 | - /**" |
|
51 | + /** |
|
52 | + * Algolia WooIndexer Attributes |
|
53 | + */ |
|
54 | + class Algolia_Attributes |
|
55 | + { |
|
56 | + |
|
57 | + |
|
58 | + /** |
|
59 | + * Class instance |
|
60 | + * |
|
61 | + * @var object |
|
62 | + */ |
|
63 | + private static $instance; |
|
64 | + |
|
65 | + |
|
66 | + /** |
|
67 | + * Setup sections and fields to store and retrieve values from Settings API |
|
68 | + * |
|
69 | + * @return void |
|
70 | + */ |
|
71 | + public static function setup_attributes_settings() |
|
72 | + { |
|
73 | + /**" |
|
74 | 74 | * Make sure we reference the instance of the current class by using self::get_instance() |
75 | 75 | * This way we can setup the correct callback function for add_settings_section and add_settings_field |
76 | 76 | */ |
77 | - $algowoo_attributes = self::get_instance(); |
|
78 | - |
|
79 | - /** |
|
80 | - * Add sections and fields for the attributes |
|
81 | - */ |
|
82 | - add_settings_section( |
|
83 | - 'algolia_woo_indexer_attributes', |
|
84 | - esc_html__('Attributes indexing settings', 'algolia-woo-indexer'), |
|
85 | - array($algowoo_attributes, 'algolia_woo_indexer_attributes_section_text'), |
|
86 | - 'algolia_woo_indexer' |
|
87 | - ); |
|
88 | - |
|
89 | - /** |
|
90 | - * Add fields based on ATTRIBUTES_SETTINGS |
|
91 | - */ |
|
92 | - foreach (ATTRIBUTES_SETTINGS as $key => $description) { |
|
93 | - add_settings_field( |
|
94 | - 'algolia_woo_indexer_attributes_' . $key, |
|
95 | - esc_html__($description, 'algolia-woo-indexer'), |
|
96 | - array($algowoo_attributes, 'algolia_woo_indexer_attributes_' . $key . '_output'), |
|
97 | - 'algolia_woo_indexer', |
|
98 | - 'algolia_woo_indexer_attributes' |
|
99 | - ); |
|
100 | - } |
|
101 | - } |
|
102 | - |
|
103 | - /** |
|
104 | - * Output for attributes if functionality is enabled |
|
105 | - * |
|
106 | - * @return void |
|
107 | - */ |
|
108 | - public static function algolia_woo_indexer_attributes_enabled_output() |
|
109 | - { |
|
110 | - $value = get_option(ALGOWOO_DB_OPTION . ATTRIBUTES_ENABLED); |
|
111 | - $isChecked = (!empty($value)) ? 1 : 0; |
|
77 | + $algowoo_attributes = self::get_instance(); |
|
78 | + |
|
79 | + /** |
|
80 | + * Add sections and fields for the attributes |
|
81 | + */ |
|
82 | + add_settings_section( |
|
83 | + 'algolia_woo_indexer_attributes', |
|
84 | + esc_html__('Attributes indexing settings', 'algolia-woo-indexer'), |
|
85 | + array($algowoo_attributes, 'algolia_woo_indexer_attributes_section_text'), |
|
86 | + 'algolia_woo_indexer' |
|
87 | + ); |
|
88 | + |
|
89 | + /** |
|
90 | + * Add fields based on ATTRIBUTES_SETTINGS |
|
91 | + */ |
|
92 | + foreach (ATTRIBUTES_SETTINGS as $key => $description) { |
|
93 | + add_settings_field( |
|
94 | + 'algolia_woo_indexer_attributes_' . $key, |
|
95 | + esc_html__($description, 'algolia-woo-indexer'), |
|
96 | + array($algowoo_attributes, 'algolia_woo_indexer_attributes_' . $key . '_output'), |
|
97 | + 'algolia_woo_indexer', |
|
98 | + 'algolia_woo_indexer_attributes' |
|
99 | + ); |
|
100 | + } |
|
101 | + } |
|
102 | + |
|
103 | + /** |
|
104 | + * Output for attributes if functionality is enabled |
|
105 | + * |
|
106 | + * @return void |
|
107 | + */ |
|
108 | + public static function algolia_woo_indexer_attributes_enabled_output() |
|
109 | + { |
|
110 | + $value = get_option(ALGOWOO_DB_OPTION . ATTRIBUTES_ENABLED); |
|
111 | + $isChecked = (!empty($value)) ? 1 : 0; |
|
112 | 112 | ?> |
113 | 113 | <input id="algolia_woo_indexer_attributes_enabled" name="algolia_woo_indexer_attributes_enabled[checked]" type="checkbox" <?php checked(1, $isChecked); ?> /> |
114 | 114 | <?php |
115 | - } |
|
116 | - |
|
117 | - /** |
|
118 | - * Output for attributes how to handle visibility setting |
|
119 | - * |
|
120 | - * @return void |
|
121 | - */ |
|
122 | - public static function algolia_woo_indexer_attributes_visibility_output() |
|
123 | - { |
|
124 | - $value = get_option(ALGOWOO_DB_OPTION . ATTRIBUTES_VISIBILITY); |
|
125 | - foreach (ATTRIBUTES_VISIBILITY_STATES as $state) { |
|
126 | - $id = 'algolia_woo_indexer_attributes_visibility_' . $state; |
|
127 | - ?> |
|
115 | + } |
|
116 | + |
|
117 | + /** |
|
118 | + * Output for attributes how to handle visibility setting |
|
119 | + * |
|
120 | + * @return void |
|
121 | + */ |
|
122 | + public static function algolia_woo_indexer_attributes_visibility_output() |
|
123 | + { |
|
124 | + $value = get_option(ALGOWOO_DB_OPTION . ATTRIBUTES_VISIBILITY); |
|
125 | + foreach (ATTRIBUTES_VISIBILITY_STATES as $state) { |
|
126 | + $id = 'algolia_woo_indexer_attributes_visibility_' . $state; |
|
127 | + ?> |
|
128 | 128 | <p><input id="<?php echo $id; ?>" name="algolia_woo_indexer_attributes_visibility[value]" type="radio" value="<?php echo $state; ?>" <?php checked($state, $value); ?> /><label for="<?php echo $id; ?>"><?php echo esc_html__($state, 'algolia-woo-indexer'); ?></label></p> |
129 | 129 | <?php |
130 | - } |
|
131 | - } |
|
132 | - |
|
133 | - /** |
|
134 | - * Output for attributes list which attributes are whitelisted |
|
135 | - * |
|
136 | - * @return void |
|
137 | - */ |
|
138 | - public static function algolia_woo_indexer_attributes_list_output() |
|
139 | - { |
|
140 | - $value = get_option(ALGOWOO_DB_OPTION . ATTRIBUTES_LIST); |
|
141 | - $selectedIds = explode(",", $value); |
|
142 | - $name = "algolia_woo_indexer_attributes_list[list]"; |
|
143 | - $description = __('Here you can whitelist all the attributes. Use the <b>shift</b> or <b>control</b> buttons to select multiple attributes.', 'algolia-woo-indexer'); |
|
144 | - self::generic_attributes_select_output($name, $selectedIds, $description); |
|
145 | - } |
|
146 | - |
|
147 | - /** |
|
148 | - * Output for attributes list which are using a numeric interpolation |
|
149 | - * |
|
150 | - * @return void |
|
151 | - */ |
|
152 | - public static function algolia_woo_indexer_attributes_interp_output() |
|
153 | - { |
|
154 | - $value = get_option(ALGOWOO_DB_OPTION . ATTRIBUTES_INTERP); |
|
155 | - $selectedIds = explode(",", $value); |
|
156 | - $name = "algolia_woo_indexer_attributes_interp[list]"; |
|
157 | - $description = __('If you have some attributes based on number which shall be interpd between the lowest to the highest number, you can select it here. A common usecase for this is if you want to have a <b>range slider</b> in aloglia which works for a certain range. Example: a plant grows between 20 and 25cm tall. for this you enter 20 and 25 as attribute values to your product and it will automatically extend the data to [20,21,22,23,24,25]', 'algolia-woo-indexer'); |
|
158 | - self::generic_attributes_select_output($name, $selectedIds, $description); |
|
159 | - } |
|
160 | - |
|
161 | - /** |
|
162 | - * Output for attributes list which are using a numeric interpolation |
|
163 | - * |
|
164 | - * @return void |
|
165 | - */ |
|
166 | - public static function algolia_woo_indexer_attributes_tax_fields_output() |
|
167 | - { |
|
168 | - $selected_raw = get_option(ALGOWOO_DB_OPTION . ATTRIBUTES_TAX_FIELDS); |
|
169 | - $selected_entries = explode(",", $selected_raw); |
|
170 | - $name = "algolia_woo_indexer_attributes_tax_fields[list]"; |
|
171 | - $description = __('Select which taxonomy fields for each attribute shall be indexed', 'algolia-woo-indexer'); |
|
172 | - $values = ALLOWED_TAXONOMIES; |
|
173 | - ?> |
|
130 | + } |
|
131 | + } |
|
132 | + |
|
133 | + /** |
|
134 | + * Output for attributes list which attributes are whitelisted |
|
135 | + * |
|
136 | + * @return void |
|
137 | + */ |
|
138 | + public static function algolia_woo_indexer_attributes_list_output() |
|
139 | + { |
|
140 | + $value = get_option(ALGOWOO_DB_OPTION . ATTRIBUTES_LIST); |
|
141 | + $selectedIds = explode(",", $value); |
|
142 | + $name = "algolia_woo_indexer_attributes_list[list]"; |
|
143 | + $description = __('Here you can whitelist all the attributes. Use the <b>shift</b> or <b>control</b> buttons to select multiple attributes.', 'algolia-woo-indexer'); |
|
144 | + self::generic_attributes_select_output($name, $selectedIds, $description); |
|
145 | + } |
|
146 | + |
|
147 | + /** |
|
148 | + * Output for attributes list which are using a numeric interpolation |
|
149 | + * |
|
150 | + * @return void |
|
151 | + */ |
|
152 | + public static function algolia_woo_indexer_attributes_interp_output() |
|
153 | + { |
|
154 | + $value = get_option(ALGOWOO_DB_OPTION . ATTRIBUTES_INTERP); |
|
155 | + $selectedIds = explode(",", $value); |
|
156 | + $name = "algolia_woo_indexer_attributes_interp[list]"; |
|
157 | + $description = __('If you have some attributes based on number which shall be interpd between the lowest to the highest number, you can select it here. A common usecase for this is if you want to have a <b>range slider</b> in aloglia which works for a certain range. Example: a plant grows between 20 and 25cm tall. for this you enter 20 and 25 as attribute values to your product and it will automatically extend the data to [20,21,22,23,24,25]', 'algolia-woo-indexer'); |
|
158 | + self::generic_attributes_select_output($name, $selectedIds, $description); |
|
159 | + } |
|
160 | + |
|
161 | + /** |
|
162 | + * Output for attributes list which are using a numeric interpolation |
|
163 | + * |
|
164 | + * @return void |
|
165 | + */ |
|
166 | + public static function algolia_woo_indexer_attributes_tax_fields_output() |
|
167 | + { |
|
168 | + $selected_raw = get_option(ALGOWOO_DB_OPTION . ATTRIBUTES_TAX_FIELDS); |
|
169 | + $selected_entries = explode(",", $selected_raw); |
|
170 | + $name = "algolia_woo_indexer_attributes_tax_fields[list]"; |
|
171 | + $description = __('Select which taxonomy fields for each attribute shall be indexed', 'algolia-woo-indexer'); |
|
172 | + $values = ALLOWED_TAXONOMIES; |
|
173 | + ?> |
|
174 | 174 | <p><?php echo $description; ?></p> |
175 | 175 | <select multiple="multiple" name="<?php echo $name; ?>[]" size="<?php echo count($values); ?>"> |
176 | 176 | <?php |
177 | - foreach ($values as $tax) { |
|
178 | - $selected = in_array($tax, $selected_entries) ? ' selected="selected" ' : ''; |
|
179 | - ?> |
|
177 | + foreach ($values as $tax) { |
|
178 | + $selected = in_array($tax, $selected_entries) ? ' selected="selected" ' : ''; |
|
179 | + ?> |
|
180 | 180 | <option value="<?php echo $tax; ?>" <?php echo $selected; ?>> |
181 | 181 | <?php echo __($tax, 'algolia-woo-indexer'); ?> |
182 | 182 | </option> |
183 | 183 | <?php |
184 | - } |
|
185 | - ?> |
|
184 | + } |
|
185 | + ?> |
|
186 | 186 | </select> |
187 | 187 | <?php |
188 | - } |
|
189 | - |
|
190 | - /** |
|
191 | - * Generic Output for attributes list where attributes are whitelisted using Woocommerce attributes taxonomies |
|
192 | - * @param string $name id and name for select |
|
193 | - * @param array $selected_entries will be preselected if matching with WC taxonomies |
|
194 | - * @param string $description will be displayed on top |
|
195 | - * |
|
196 | - */ |
|
197 | - public static function generic_attributes_select_output($name, $selected_entries, $description) |
|
198 | - { |
|
199 | - |
|
200 | - |
|
201 | - $values = wc_get_attribute_taxonomies(); |
|
202 | - if (!$values) { |
|
203 | - echo esc_html__('You don\'t have any attributes defined yet. Go to WooCommerce and add some to use this feature.', 'algolia-woo-indexer'); |
|
204 | - return; |
|
205 | - } |
|
206 | - |
|
207 | - ?> |
|
188 | + } |
|
189 | + |
|
190 | + /** |
|
191 | + * Generic Output for attributes list where attributes are whitelisted using Woocommerce attributes taxonomies |
|
192 | + * @param string $name id and name for select |
|
193 | + * @param array $selected_entries will be preselected if matching with WC taxonomies |
|
194 | + * @param string $description will be displayed on top |
|
195 | + * |
|
196 | + */ |
|
197 | + public static function generic_attributes_select_output($name, $selected_entries, $description) |
|
198 | + { |
|
199 | + |
|
200 | + |
|
201 | + $values = wc_get_attribute_taxonomies(); |
|
202 | + if (!$values) { |
|
203 | + echo esc_html__('You don\'t have any attributes defined yet. Go to WooCommerce and add some to use this feature.', 'algolia-woo-indexer'); |
|
204 | + return; |
|
205 | + } |
|
206 | + |
|
207 | + ?> |
|
208 | 208 | <p><?php echo $description; ?></p> |
209 | 209 | <select multiple="multiple" name="<?php echo $name; ?>[]" size="<?php echo count($values); ?>"> |
210 | 210 | <?php |
211 | - foreach ($values as $tax) { |
|
211 | + foreach ($values as $tax) { |
|
212 | 212 | |
213 | - $id = $tax->attribute_id; |
|
214 | - $label = $tax->attribute_label; |
|
215 | - $name = $tax->attribute_name; |
|
216 | - $selected = in_array($id, $selected_entries) ? ' selected="selected" ' : ''; |
|
217 | - ?> |
|
213 | + $id = $tax->attribute_id; |
|
214 | + $label = $tax->attribute_label; |
|
215 | + $name = $tax->attribute_name; |
|
216 | + $selected = in_array($id, $selected_entries) ? ' selected="selected" ' : ''; |
|
217 | + ?> |
|
218 | 218 | <option value="<?php echo $id; ?>" <?php echo $selected; ?>> |
219 | 219 | <?php echo $label . ' (' . $name . ')'; ?> |
220 | 220 | </option> |
221 | 221 | <?php |
222 | - } |
|
223 | - ?> |
|
222 | + } |
|
223 | + ?> |
|
224 | 224 | </select> |
225 | 225 | <?php |
226 | 226 | |
227 | - } |
|
227 | + } |
|
228 | 228 | |
229 | - /** |
|
230 | - * Section text for attributes settings section text |
|
231 | - * |
|
232 | - * @return void |
|
233 | - */ |
|
234 | - public static function algolia_woo_indexer_attributes_section_text() |
|
235 | - { |
|
229 | + /** |
|
230 | + * Section text for attributes settings section text |
|
231 | + * |
|
232 | + * @return void |
|
233 | + */ |
|
234 | + public static function algolia_woo_indexer_attributes_section_text() |
|
235 | + { |
|
236 | 236 | |
237 | - } |
|
238 | - |
|
239 | - |
|
240 | - /** |
|
241 | - * parse, sanitize and update attribute settings in DB |
|
242 | - * |
|
243 | - * @return void |
|
244 | - */ |
|
245 | - public static function update_attribute_options() |
|
246 | - { |
|
247 | - |
|
248 | - /** |
|
249 | - * Filter the inputs |
|
250 | - * |
|
251 | - * @see https://www.php.net/manual/en/function.filter-input.php |
|
252 | - */ |
|
253 | - $attributes_enabled = filter_input(INPUT_POST, 'algolia_woo_indexer_attributes_enabled', FILTER_DEFAULT, FILTER_REQUIRE_ARRAY); |
|
254 | - $attributes_visibility = filter_input(INPUT_POST, 'algolia_woo_indexer_attributes_visibility', FILTER_DEFAULT, FILTER_REQUIRE_ARRAY); |
|
255 | - $attributes_list = filter_input(INPUT_POST, 'algolia_woo_indexer_attributes_list', FILTER_DEFAULT, FILTER_REQUIRE_ARRAY); |
|
256 | - $attributes_interp = filter_input(INPUT_POST, 'algolia_woo_indexer_attributes_interp', FILTER_DEFAULT, FILTER_REQUIRE_ARRAY); |
|
257 | - $attributes_tax_fields = filter_input(INPUT_POST, 'algolia_woo_indexer_attributes_tax_fields', FILTER_DEFAULT, FILTER_REQUIRE_ARRAY); |
|
258 | - |
|
259 | - /** |
|
260 | - * Properly sanitize text fields before updating data |
|
261 | - * |
|
262 | - * @see https://developer.wordpress.org/reference/functions/sanitize_text_field/ |
|
263 | - */ |
|
264 | - $sanitized = array(); |
|
265 | - $sanitized['attributes_visibility'] = sanitize_text_field($attributes_visibility['value']); |
|
266 | - |
|
267 | - /** |
|
268 | - * sanitize select list of id's by getting integers and them implode seperated with comma |
|
269 | - */ |
|
270 | - |
|
271 | - $attributes_list_integers = []; |
|
272 | - foreach ($attributes_list['list'] as $id) { |
|
273 | - $sanitizedId = sanitize_text_field($id); |
|
274 | - array_push($attributes_list_integers, (int) $sanitizedId); |
|
275 | - } |
|
276 | - $sanitized['attributes_list'] = implode(',', $attributes_list_integers); |
|
277 | - |
|
278 | - $attributes_interp_int = []; |
|
279 | - foreach ($attributes_interp['list'] as $id) { |
|
280 | - $sanitizedId = sanitize_text_field($id); |
|
281 | - array_push($attributes_interp_int, (int) $sanitizedId); |
|
282 | - } |
|
283 | - $sanitized['attributes_interp'] = implode(',', $attributes_interp_int); |
|
284 | - |
|
285 | - /** |
|
286 | - * only allow values from the ALLOWED_TAXONOMIES to be saved |
|
287 | - */ |
|
288 | - $sanitized['attributes_tax_fields'] = []; |
|
289 | - foreach ($attributes_tax_fields['list'] as $name) { |
|
290 | - if (in_array($name, ALLOWED_TAXONOMIES)) { |
|
291 | - array_push($sanitized['attributes_tax_fields'], $name); |
|
292 | - } |
|
293 | - } |
|
294 | - $sanitized['attributes_tax_fields'] = implode(',', $sanitized['attributes_tax_fields']); |
|
295 | - |
|
296 | - /** |
|
297 | - * Sanitizing by setting the value to either 1 or 0 |
|
298 | - */ |
|
299 | - $sanitized['attributes_enabled'] = (!empty($attributes_enabled)) ? 1 : 0; |
|
300 | - |
|
301 | - |
|
302 | - /** |
|
303 | - * Values have been filtered and sanitized |
|
304 | - * Check if set and not empty and update the database |
|
305 | - * |
|
306 | - * @see https://developer.wordpress.org/reference/functions/update_option/ |
|
307 | - */ |
|
308 | - |
|
309 | - foreach (array_keys(ATTRIBUTES_SETTINGS) as $key) { |
|
310 | - $value = $sanitized['attributes_' . $key]; |
|
311 | - if (isset($value)) { |
|
312 | - $extension = constant('ATTRIBUTES_' . strtoupper($key)); |
|
313 | - update_option( |
|
314 | - ALGOWOO_DB_OPTION . $extension, |
|
315 | - $value |
|
316 | - ); |
|
317 | - } |
|
318 | - } |
|
319 | - } |
|
320 | - |
|
321 | - |
|
322 | - /** |
|
323 | - * The actions to execute when the plugin is activated. |
|
324 | - * |
|
325 | - * @return void |
|
326 | - */ |
|
327 | - public static function activate_attributes() |
|
328 | - { |
|
329 | - |
|
330 | - /** |
|
331 | - * Set default values for options if not already set |
|
332 | - */ |
|
333 | - $attributes_enabled = get_option(ALGOWOO_DB_OPTION . ATTRIBUTES_ENABLED); |
|
334 | - $attributes_visibility = get_option(ALGOWOO_DB_OPTION . ATTRIBUTES_VISIBILITY); |
|
335 | - $attributes_list = get_option(ALGOWOO_DB_OPTION . ATTRIBUTES_LIST); |
|
336 | - $attributes_interp = get_option(ALGOWOO_DB_OPTION . ATTRIBUTES_INTERP); |
|
337 | - $attributes_tax_fields = get_option(ALGOWOO_DB_OPTION . ATTRIBUTES_TAX_FIELDS); |
|
338 | - |
|
339 | - |
|
340 | - if (empty($attributes_enabled)) { |
|
341 | - add_option( |
|
342 | - ALGOWOO_DB_OPTION . ATTRIBUTES_ENABLED, |
|
343 | - 1 |
|
344 | - ); |
|
345 | - } |
|
346 | - if (empty($attributes_visibility)) { |
|
347 | - add_option( |
|
348 | - ALGOWOO_DB_OPTION . ATTRIBUTES_VISIBILITY, |
|
349 | - ATTRIBUTES_VISIBILITY_STATES[0] |
|
350 | - ); |
|
351 | - } |
|
352 | - if (empty($attributes_list)) { |
|
353 | - add_option( |
|
354 | - ALGOWOO_DB_OPTION . ATTRIBUTES_LIST, |
|
355 | - '' |
|
356 | - ); |
|
357 | - } |
|
358 | - if (empty($attributes_interp)) { |
|
359 | - add_option( |
|
360 | - ALGOWOO_DB_OPTION . ATTRIBUTES_INTERP, |
|
361 | - '' |
|
362 | - ); |
|
363 | - } |
|
364 | - if (empty($attributes_tax_fields)) { |
|
365 | - add_option( |
|
366 | - ALGOWOO_DB_OPTION . ATTRIBUTES_TAX_FIELDS, |
|
367 | - 'name,slug' |
|
368 | - ); |
|
369 | - } |
|
370 | - } |
|
371 | - /** |
|
372 | - * Get active object instance |
|
373 | - * |
|
374 | - * @return object |
|
375 | - */ |
|
376 | - public static function get_instance() |
|
377 | - { |
|
378 | - if (!self::$instance) { |
|
379 | - self::$instance = new Algolia_Attributes(); |
|
380 | - } |
|
381 | - return self::$instance; |
|
382 | - } |
|
383 | - |
|
384 | - /** |
|
385 | - * format attributes terms according to settings in ATTRIBUTES_TAX_FIELDS |
|
386 | - * |
|
387 | - * @param array $terms list of woocommerce attribute taxonomy |
|
388 | - * @return array Array with fields set in config as defined in ATTRIBUTEX_TAX_FIELDS. |
|
389 | - */ |
|
390 | - public static function format_product_attribute_terms($terms, $interpolateValues) |
|
391 | - { |
|
392 | - $allowed_keys_raw = get_option(ALGOWOO_DB_OPTION . ATTRIBUTES_TAX_FIELDS); |
|
393 | - $allowed_keys = explode(',', $allowed_keys_raw); |
|
394 | - $final_terms = array(); |
|
395 | - |
|
396 | - switch ($interpolateValues) { |
|
397 | - case true: |
|
398 | - $integers = array(); |
|
399 | - foreach ($terms as $term) { |
|
400 | - array_push($integers, (int) $term->name); |
|
401 | - } |
|
402 | - if (count($integers) > 0) { |
|
403 | - for ($i = min($integers); $i <= max($integers); $i++) { |
|
404 | - array_push($final_terms, $i); |
|
405 | - } |
|
406 | - } |
|
407 | - break; |
|
408 | - /** |
|
409 | - * normal mixed content case |
|
410 | - */ |
|
411 | - default: |
|
412 | - foreach ($terms as $term) { |
|
413 | - $final_term = array(); |
|
414 | - foreach ($allowed_keys as $key) { |
|
415 | - array_push($final_term, esc_html($term->{$key})); |
|
416 | - } |
|
417 | - $string_with_Separator = implode("|", $final_term); |
|
418 | - array_push($final_terms, $string_with_Separator); |
|
419 | - } |
|
420 | - } |
|
421 | - return $final_terms; |
|
422 | - } |
|
423 | - |
|
424 | - /** |
|
425 | - * skip variable related attributes, |
|
426 | - * ensure it is a taxonomy, |
|
427 | - * ensure that taxonomy is whitelisted and |
|
428 | - * ensure that the visibility and variation is respected |
|
429 | - * @param mixed $attribute Woocommerce attribute |
|
430 | - */ |
|
431 | - private static function is_attribute_not_allowed($attribute) |
|
432 | - { |
|
433 | - /** |
|
434 | - * gather settings |
|
435 | - */ |
|
436 | - $setting_visibility = get_option(ALGOWOO_DB_OPTION . ATTRIBUTES_VISIBILITY); |
|
437 | - $setting_ids = get_option(ALGOWOO_DB_OPTION . ATTRIBUTES_LIST); |
|
438 | - $setting_ids = explode(",", $setting_ids); |
|
439 | - $visibility = $attribute["visible"]; |
|
440 | - $attribute_id = $attribute->get_id(); |
|
441 | - |
|
442 | - return ($attribute->get_variation() || |
|
443 | - !$attribute->is_taxonomy() || |
|
444 | - !in_array($attribute_id, $setting_ids) || |
|
445 | - ($setting_visibility === "visible" && $visibility === false) || |
|
446 | - ($setting_visibility === "hidden" && $visibility === true) |
|
447 | - ); |
|
448 | - } |
|
449 | - |
|
450 | - /** |
|
451 | - * Get attributes from product |
|
452 | - * |
|
453 | - * @param mixed $product Product to check |
|
454 | - * @return array ['pa_name' => ['value1', 'value2']] Array with key set to the product attribute internal name and values as array. returns false if not attributes found. |
|
455 | - */ |
|
456 | - public static function get_product_attributes($product) |
|
457 | - { |
|
458 | - /** |
|
459 | - * ensure that attributes are actually enabled and we having data |
|
460 | - */ |
|
461 | - $attributes_enabled = (int) get_option(ALGOWOO_DB_OPTION . ATTRIBUTES_ENABLED); |
|
462 | - $rawAttributes = $product->get_attributes("edit"); |
|
463 | - if ($attributes_enabled !== 1 || !$rawAttributes) { |
|
464 | - return false; |
|
465 | - } |
|
466 | - |
|
467 | - |
|
468 | - $setting_ids_interp = get_option(ALGOWOO_DB_OPTION . ATTRIBUTES_INTERP); |
|
469 | - $setting_ids_interp = explode(",", $setting_ids_interp); |
|
470 | - |
|
471 | - |
|
472 | - $attributes = []; |
|
473 | - foreach ($rawAttributes as $attribute) { |
|
474 | - if (!self::is_attribute_not_allowed($attribute)) { |
|
475 | - $name = $attribute->get_name(); |
|
476 | - $terms = wp_get_post_terms($product->get_id(), $name, 'all'); |
|
477 | - $is_interpolation = in_array($attribute->get_id(), $setting_ids_interp); |
|
478 | - $attributes[$name] = self::format_product_attribute_terms($terms, $is_interpolation); |
|
479 | - } |
|
480 | - } |
|
481 | - return $attributes; |
|
482 | - } |
|
483 | - } |
|
237 | + } |
|
238 | + |
|
239 | + |
|
240 | + /** |
|
241 | + * parse, sanitize and update attribute settings in DB |
|
242 | + * |
|
243 | + * @return void |
|
244 | + */ |
|
245 | + public static function update_attribute_options() |
|
246 | + { |
|
247 | + |
|
248 | + /** |
|
249 | + * Filter the inputs |
|
250 | + * |
|
251 | + * @see https://www.php.net/manual/en/function.filter-input.php |
|
252 | + */ |
|
253 | + $attributes_enabled = filter_input(INPUT_POST, 'algolia_woo_indexer_attributes_enabled', FILTER_DEFAULT, FILTER_REQUIRE_ARRAY); |
|
254 | + $attributes_visibility = filter_input(INPUT_POST, 'algolia_woo_indexer_attributes_visibility', FILTER_DEFAULT, FILTER_REQUIRE_ARRAY); |
|
255 | + $attributes_list = filter_input(INPUT_POST, 'algolia_woo_indexer_attributes_list', FILTER_DEFAULT, FILTER_REQUIRE_ARRAY); |
|
256 | + $attributes_interp = filter_input(INPUT_POST, 'algolia_woo_indexer_attributes_interp', FILTER_DEFAULT, FILTER_REQUIRE_ARRAY); |
|
257 | + $attributes_tax_fields = filter_input(INPUT_POST, 'algolia_woo_indexer_attributes_tax_fields', FILTER_DEFAULT, FILTER_REQUIRE_ARRAY); |
|
258 | + |
|
259 | + /** |
|
260 | + * Properly sanitize text fields before updating data |
|
261 | + * |
|
262 | + * @see https://developer.wordpress.org/reference/functions/sanitize_text_field/ |
|
263 | + */ |
|
264 | + $sanitized = array(); |
|
265 | + $sanitized['attributes_visibility'] = sanitize_text_field($attributes_visibility['value']); |
|
266 | + |
|
267 | + /** |
|
268 | + * sanitize select list of id's by getting integers and them implode seperated with comma |
|
269 | + */ |
|
270 | + |
|
271 | + $attributes_list_integers = []; |
|
272 | + foreach ($attributes_list['list'] as $id) { |
|
273 | + $sanitizedId = sanitize_text_field($id); |
|
274 | + array_push($attributes_list_integers, (int) $sanitizedId); |
|
275 | + } |
|
276 | + $sanitized['attributes_list'] = implode(',', $attributes_list_integers); |
|
277 | + |
|
278 | + $attributes_interp_int = []; |
|
279 | + foreach ($attributes_interp['list'] as $id) { |
|
280 | + $sanitizedId = sanitize_text_field($id); |
|
281 | + array_push($attributes_interp_int, (int) $sanitizedId); |
|
282 | + } |
|
283 | + $sanitized['attributes_interp'] = implode(',', $attributes_interp_int); |
|
284 | + |
|
285 | + /** |
|
286 | + * only allow values from the ALLOWED_TAXONOMIES to be saved |
|
287 | + */ |
|
288 | + $sanitized['attributes_tax_fields'] = []; |
|
289 | + foreach ($attributes_tax_fields['list'] as $name) { |
|
290 | + if (in_array($name, ALLOWED_TAXONOMIES)) { |
|
291 | + array_push($sanitized['attributes_tax_fields'], $name); |
|
292 | + } |
|
293 | + } |
|
294 | + $sanitized['attributes_tax_fields'] = implode(',', $sanitized['attributes_tax_fields']); |
|
295 | + |
|
296 | + /** |
|
297 | + * Sanitizing by setting the value to either 1 or 0 |
|
298 | + */ |
|
299 | + $sanitized['attributes_enabled'] = (!empty($attributes_enabled)) ? 1 : 0; |
|
300 | + |
|
301 | + |
|
302 | + /** |
|
303 | + * Values have been filtered and sanitized |
|
304 | + * Check if set and not empty and update the database |
|
305 | + * |
|
306 | + * @see https://developer.wordpress.org/reference/functions/update_option/ |
|
307 | + */ |
|
308 | + |
|
309 | + foreach (array_keys(ATTRIBUTES_SETTINGS) as $key) { |
|
310 | + $value = $sanitized['attributes_' . $key]; |
|
311 | + if (isset($value)) { |
|
312 | + $extension = constant('ATTRIBUTES_' . strtoupper($key)); |
|
313 | + update_option( |
|
314 | + ALGOWOO_DB_OPTION . $extension, |
|
315 | + $value |
|
316 | + ); |
|
317 | + } |
|
318 | + } |
|
319 | + } |
|
320 | + |
|
321 | + |
|
322 | + /** |
|
323 | + * The actions to execute when the plugin is activated. |
|
324 | + * |
|
325 | + * @return void |
|
326 | + */ |
|
327 | + public static function activate_attributes() |
|
328 | + { |
|
329 | + |
|
330 | + /** |
|
331 | + * Set default values for options if not already set |
|
332 | + */ |
|
333 | + $attributes_enabled = get_option(ALGOWOO_DB_OPTION . ATTRIBUTES_ENABLED); |
|
334 | + $attributes_visibility = get_option(ALGOWOO_DB_OPTION . ATTRIBUTES_VISIBILITY); |
|
335 | + $attributes_list = get_option(ALGOWOO_DB_OPTION . ATTRIBUTES_LIST); |
|
336 | + $attributes_interp = get_option(ALGOWOO_DB_OPTION . ATTRIBUTES_INTERP); |
|
337 | + $attributes_tax_fields = get_option(ALGOWOO_DB_OPTION . ATTRIBUTES_TAX_FIELDS); |
|
338 | + |
|
339 | + |
|
340 | + if (empty($attributes_enabled)) { |
|
341 | + add_option( |
|
342 | + ALGOWOO_DB_OPTION . ATTRIBUTES_ENABLED, |
|
343 | + 1 |
|
344 | + ); |
|
345 | + } |
|
346 | + if (empty($attributes_visibility)) { |
|
347 | + add_option( |
|
348 | + ALGOWOO_DB_OPTION . ATTRIBUTES_VISIBILITY, |
|
349 | + ATTRIBUTES_VISIBILITY_STATES[0] |
|
350 | + ); |
|
351 | + } |
|
352 | + if (empty($attributes_list)) { |
|
353 | + add_option( |
|
354 | + ALGOWOO_DB_OPTION . ATTRIBUTES_LIST, |
|
355 | + '' |
|
356 | + ); |
|
357 | + } |
|
358 | + if (empty($attributes_interp)) { |
|
359 | + add_option( |
|
360 | + ALGOWOO_DB_OPTION . ATTRIBUTES_INTERP, |
|
361 | + '' |
|
362 | + ); |
|
363 | + } |
|
364 | + if (empty($attributes_tax_fields)) { |
|
365 | + add_option( |
|
366 | + ALGOWOO_DB_OPTION . ATTRIBUTES_TAX_FIELDS, |
|
367 | + 'name,slug' |
|
368 | + ); |
|
369 | + } |
|
370 | + } |
|
371 | + /** |
|
372 | + * Get active object instance |
|
373 | + * |
|
374 | + * @return object |
|
375 | + */ |
|
376 | + public static function get_instance() |
|
377 | + { |
|
378 | + if (!self::$instance) { |
|
379 | + self::$instance = new Algolia_Attributes(); |
|
380 | + } |
|
381 | + return self::$instance; |
|
382 | + } |
|
383 | + |
|
384 | + /** |
|
385 | + * format attributes terms according to settings in ATTRIBUTES_TAX_FIELDS |
|
386 | + * |
|
387 | + * @param array $terms list of woocommerce attribute taxonomy |
|
388 | + * @return array Array with fields set in config as defined in ATTRIBUTEX_TAX_FIELDS. |
|
389 | + */ |
|
390 | + public static function format_product_attribute_terms($terms, $interpolateValues) |
|
391 | + { |
|
392 | + $allowed_keys_raw = get_option(ALGOWOO_DB_OPTION . ATTRIBUTES_TAX_FIELDS); |
|
393 | + $allowed_keys = explode(',', $allowed_keys_raw); |
|
394 | + $final_terms = array(); |
|
395 | + |
|
396 | + switch ($interpolateValues) { |
|
397 | + case true: |
|
398 | + $integers = array(); |
|
399 | + foreach ($terms as $term) { |
|
400 | + array_push($integers, (int) $term->name); |
|
401 | + } |
|
402 | + if (count($integers) > 0) { |
|
403 | + for ($i = min($integers); $i <= max($integers); $i++) { |
|
404 | + array_push($final_terms, $i); |
|
405 | + } |
|
406 | + } |
|
407 | + break; |
|
408 | + /** |
|
409 | + * normal mixed content case |
|
410 | + */ |
|
411 | + default: |
|
412 | + foreach ($terms as $term) { |
|
413 | + $final_term = array(); |
|
414 | + foreach ($allowed_keys as $key) { |
|
415 | + array_push($final_term, esc_html($term->{$key})); |
|
416 | + } |
|
417 | + $string_with_Separator = implode("|", $final_term); |
|
418 | + array_push($final_terms, $string_with_Separator); |
|
419 | + } |
|
420 | + } |
|
421 | + return $final_terms; |
|
422 | + } |
|
423 | + |
|
424 | + /** |
|
425 | + * skip variable related attributes, |
|
426 | + * ensure it is a taxonomy, |
|
427 | + * ensure that taxonomy is whitelisted and |
|
428 | + * ensure that the visibility and variation is respected |
|
429 | + * @param mixed $attribute Woocommerce attribute |
|
430 | + */ |
|
431 | + private static function is_attribute_not_allowed($attribute) |
|
432 | + { |
|
433 | + /** |
|
434 | + * gather settings |
|
435 | + */ |
|
436 | + $setting_visibility = get_option(ALGOWOO_DB_OPTION . ATTRIBUTES_VISIBILITY); |
|
437 | + $setting_ids = get_option(ALGOWOO_DB_OPTION . ATTRIBUTES_LIST); |
|
438 | + $setting_ids = explode(",", $setting_ids); |
|
439 | + $visibility = $attribute["visible"]; |
|
440 | + $attribute_id = $attribute->get_id(); |
|
441 | + |
|
442 | + return ($attribute->get_variation() || |
|
443 | + !$attribute->is_taxonomy() || |
|
444 | + !in_array($attribute_id, $setting_ids) || |
|
445 | + ($setting_visibility === "visible" && $visibility === false) || |
|
446 | + ($setting_visibility === "hidden" && $visibility === true) |
|
447 | + ); |
|
448 | + } |
|
449 | + |
|
450 | + /** |
|
451 | + * Get attributes from product |
|
452 | + * |
|
453 | + * @param mixed $product Product to check |
|
454 | + * @return array ['pa_name' => ['value1', 'value2']] Array with key set to the product attribute internal name and values as array. returns false if not attributes found. |
|
455 | + */ |
|
456 | + public static function get_product_attributes($product) |
|
457 | + { |
|
458 | + /** |
|
459 | + * ensure that attributes are actually enabled and we having data |
|
460 | + */ |
|
461 | + $attributes_enabled = (int) get_option(ALGOWOO_DB_OPTION . ATTRIBUTES_ENABLED); |
|
462 | + $rawAttributes = $product->get_attributes("edit"); |
|
463 | + if ($attributes_enabled !== 1 || !$rawAttributes) { |
|
464 | + return false; |
|
465 | + } |
|
466 | + |
|
467 | + |
|
468 | + $setting_ids_interp = get_option(ALGOWOO_DB_OPTION . ATTRIBUTES_INTERP); |
|
469 | + $setting_ids_interp = explode(",", $setting_ids_interp); |
|
470 | + |
|
471 | + |
|
472 | + $attributes = []; |
|
473 | + foreach ($rawAttributes as $attribute) { |
|
474 | + if (!self::is_attribute_not_allowed($attribute)) { |
|
475 | + $name = $attribute->get_name(); |
|
476 | + $terms = wp_get_post_terms($product->get_id(), $name, 'all'); |
|
477 | + $is_interpolation = in_array($attribute->get_id(), $setting_ids_interp); |
|
478 | + $attributes[$name] = self::format_product_attribute_terms($terms, $is_interpolation); |
|
479 | + } |
|
480 | + } |
|
481 | + return $attributes; |
|
482 | + } |
|
483 | + } |
|
484 | 484 | } |
@@ -42,12 +42,12 @@ discard block |
||
42 | 42 | /** |
43 | 43 | * Abort if this file is called directly |
44 | 44 | */ |
45 | -if (!defined('ABSPATH')) { |
|
45 | +if ( ! defined('ABSPATH')) { |
|
46 | 46 | exit; |
47 | 47 | } |
48 | 48 | |
49 | 49 | |
50 | -if (!class_exists('Algolia_Attributes')) { |
|
50 | +if ( ! class_exists('Algolia_Attributes')) { |
|
51 | 51 | /** |
52 | 52 | * Algolia WooIndexer Attributes |
53 | 53 | */ |
@@ -108,7 +108,7 @@ discard block |
||
108 | 108 | public static function algolia_woo_indexer_attributes_enabled_output() |
109 | 109 | { |
110 | 110 | $value = get_option(ALGOWOO_DB_OPTION . ATTRIBUTES_ENABLED); |
111 | - $isChecked = (!empty($value)) ? 1 : 0; |
|
111 | + $isChecked = ( ! empty($value)) ? 1 : 0; |
|
112 | 112 | ?> |
113 | 113 | <input id="algolia_woo_indexer_attributes_enabled" name="algolia_woo_indexer_attributes_enabled[checked]" type="checkbox" <?php checked(1, $isChecked); ?> /> |
114 | 114 | <?php |
@@ -199,7 +199,7 @@ discard block |
||
199 | 199 | |
200 | 200 | |
201 | 201 | $values = wc_get_attribute_taxonomies(); |
202 | - if (!$values) { |
|
202 | + if ( ! $values) { |
|
203 | 203 | echo esc_html__('You don\'t have any attributes defined yet. Go to WooCommerce and add some to use this feature.', 'algolia-woo-indexer'); |
204 | 204 | return; |
205 | 205 | } |
@@ -262,7 +262,7 @@ discard block |
||
262 | 262 | * @see https://developer.wordpress.org/reference/functions/sanitize_text_field/ |
263 | 263 | */ |
264 | 264 | $sanitized = array(); |
265 | - $sanitized['attributes_visibility'] = sanitize_text_field($attributes_visibility['value']); |
|
265 | + $sanitized['attributes_visibility'] = sanitize_text_field($attributes_visibility['value']); |
|
266 | 266 | |
267 | 267 | /** |
268 | 268 | * sanitize select list of id's by getting integers and them implode seperated with comma |
@@ -296,7 +296,7 @@ discard block |
||
296 | 296 | /** |
297 | 297 | * Sanitizing by setting the value to either 1 or 0 |
298 | 298 | */ |
299 | - $sanitized['attributes_enabled'] = (!empty($attributes_enabled)) ? 1 : 0; |
|
299 | + $sanitized['attributes_enabled'] = ( ! empty($attributes_enabled)) ? 1 : 0; |
|
300 | 300 | |
301 | 301 | |
302 | 302 | /** |
@@ -375,7 +375,7 @@ discard block |
||
375 | 375 | */ |
376 | 376 | public static function get_instance() |
377 | 377 | { |
378 | - if (!self::$instance) { |
|
378 | + if ( ! self::$instance) { |
|
379 | 379 | self::$instance = new Algolia_Attributes(); |
380 | 380 | } |
381 | 381 | return self::$instance; |
@@ -440,10 +440,10 @@ discard block |
||
440 | 440 | $attribute_id = $attribute->get_id(); |
441 | 441 | |
442 | 442 | return ($attribute->get_variation() || |
443 | - !$attribute->is_taxonomy() || |
|
444 | - !in_array($attribute_id, $setting_ids) || |
|
445 | - ($setting_visibility === "visible" && $visibility === false) || |
|
446 | - ($setting_visibility === "hidden" && $visibility === true) |
|
443 | + ! $attribute->is_taxonomy() || |
|
444 | + ! in_array($attribute_id, $setting_ids) || |
|
445 | + ($setting_visibility === "visible" && $visibility === false) || |
|
446 | + ($setting_visibility === "hidden" && $visibility === true) |
|
447 | 447 | ); |
448 | 448 | } |
449 | 449 | |
@@ -460,7 +460,7 @@ discard block |
||
460 | 460 | */ |
461 | 461 | $attributes_enabled = (int) get_option(ALGOWOO_DB_OPTION . ATTRIBUTES_ENABLED); |
462 | 462 | $rawAttributes = $product->get_attributes("edit"); |
463 | - if ($attributes_enabled !== 1 || !$rawAttributes) { |
|
463 | + if ($attributes_enabled !== 1 || ! $rawAttributes) { |
|
464 | 464 | return false; |
465 | 465 | } |
466 | 466 | |
@@ -471,7 +471,7 @@ discard block |
||
471 | 471 | |
472 | 472 | $attributes = []; |
473 | 473 | foreach ($rawAttributes as $attribute) { |
474 | - if (!self::is_attribute_not_allowed($attribute)) { |
|
474 | + if ( ! self::is_attribute_not_allowed($attribute)) { |
|
475 | 475 | $name = $attribute->get_name(); |
476 | 476 | $terms = wp_get_post_terms($product->get_id(), $name, 'all'); |
477 | 477 | $is_interpolation = in_array($attribute->get_id(), $setting_ids_interp); |