@@ -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 | } |
@@ -15,14 +15,14 @@ discard block |
||
15 | 15 | * Abort if this file is called directly |
16 | 16 | */ |
17 | 17 | if (!defined('ABSPATH')) { |
18 | - exit; |
|
18 | + exit; |
|
19 | 19 | } |
20 | 20 | |
21 | 21 | /** |
22 | 22 | * Include plugin file if function is_plugin_active does not exist |
23 | 23 | */ |
24 | 24 | if (!function_exists('is_plugin_active')) { |
25 | - require_once(ABSPATH . '/wp-admin/includes/plugin.php'); |
|
25 | + require_once(ABSPATH . '/wp-admin/includes/plugin.php'); |
|
26 | 26 | } |
27 | 27 | |
28 | 28 | /** |
@@ -40,19 +40,19 @@ discard block |
||
40 | 40 | * Define list of fields available to index |
41 | 41 | */ |
42 | 42 | define('BASIC_FIELDS', array( |
43 | - 'product_name', |
|
44 | - 'permalink', |
|
45 | - 'tags', |
|
46 | - 'categories', |
|
47 | - 'short_description', |
|
48 | - 'long_description', |
|
49 | - 'excerpt', |
|
50 | - 'product_image', |
|
51 | - 'regular_price', |
|
52 | - 'sale_price', |
|
53 | - 'on_sale', |
|
54 | - "stock_quantity", |
|
55 | - "stock_status" |
|
43 | + 'product_name', |
|
44 | + 'permalink', |
|
45 | + 'tags', |
|
46 | + 'categories', |
|
47 | + 'short_description', |
|
48 | + 'long_description', |
|
49 | + 'excerpt', |
|
50 | + 'product_image', |
|
51 | + 'regular_price', |
|
52 | + 'sale_price', |
|
53 | + 'on_sale', |
|
54 | + "stock_quantity", |
|
55 | + "stock_status" |
|
56 | 56 | )); |
57 | 57 | |
58 | 58 | /** |
@@ -77,416 +77,416 @@ discard block |
||
77 | 77 | |
78 | 78 | |
79 | 79 | if (!class_exists('Algolia_Send_Products')) { |
80 | - /** |
|
81 | - * Algolia WooIndexer main class |
|
82 | - */ |
|
83 | - // TODO Rename class "Algolia_Send_Products" to match the regular expression ^[A-Z][a-zA-Z0-9]*$. |
|
84 | - class Algolia_Send_Products |
|
85 | - { |
|
86 | - const PLUGIN_NAME = 'Algolia Woo Indexer'; |
|
87 | - const PLUGIN_TRANSIENT = 'algowoo-plugin-notice'; |
|
88 | - |
|
89 | - /** |
|
90 | - * The Algolia instance |
|
91 | - * |
|
92 | - * @var \Algolia\AlgoliaSearch\SearchClient |
|
93 | - */ |
|
94 | - private static $algolia = null; |
|
95 | - |
|
96 | - /** |
|
97 | - * Check if we can connect to Algolia, if not, handle the exception, display an error and then return |
|
98 | - */ |
|
99 | - public static function can_connect_to_algolia() |
|
100 | - { |
|
101 | - try { |
|
102 | - self::$algolia->listApiKeys(); |
|
103 | - } catch (\Algolia\AlgoliaSearch\Exceptions\UnreachableException $error) { |
|
104 | - add_action( |
|
105 | - 'admin_notices', |
|
106 | - function () { |
|
107 | - echo '<div class="error notice"> |
|
80 | + /** |
|
81 | + * Algolia WooIndexer main class |
|
82 | + */ |
|
83 | + // TODO Rename class "Algolia_Send_Products" to match the regular expression ^[A-Z][a-zA-Z0-9]*$. |
|
84 | + class Algolia_Send_Products |
|
85 | + { |
|
86 | + const PLUGIN_NAME = 'Algolia Woo Indexer'; |
|
87 | + const PLUGIN_TRANSIENT = 'algowoo-plugin-notice'; |
|
88 | + |
|
89 | + /** |
|
90 | + * The Algolia instance |
|
91 | + * |
|
92 | + * @var \Algolia\AlgoliaSearch\SearchClient |
|
93 | + */ |
|
94 | + private static $algolia = null; |
|
95 | + |
|
96 | + /** |
|
97 | + * Check if we can connect to Algolia, if not, handle the exception, display an error and then return |
|
98 | + */ |
|
99 | + public static function can_connect_to_algolia() |
|
100 | + { |
|
101 | + try { |
|
102 | + self::$algolia->listApiKeys(); |
|
103 | + } catch (\Algolia\AlgoliaSearch\Exceptions\UnreachableException $error) { |
|
104 | + add_action( |
|
105 | + 'admin_notices', |
|
106 | + function () { |
|
107 | + echo '<div class="error notice"> |
|
108 | 108 | <p>' . esc_html__('An error has been encountered. Please check your application ID and API key. ', 'algolia-woo-indexer') . '</p> |
109 | 109 | </div>'; |
110 | - } |
|
111 | - ); |
|
112 | - return; |
|
113 | - } |
|
114 | - } |
|
115 | - |
|
116 | - /** |
|
117 | - * check if the field is enabled and shall be sent |
|
118 | - * |
|
119 | - * @param mixed $field name of field to be checked according to BASIC_FIELDS |
|
120 | - * @return boolean true if enable, false is not enabled |
|
121 | - */ |
|
122 | - public static function is_basic_field_enabled($field) |
|
123 | - { |
|
124 | - $fieldValue = get_option(ALGOWOO_DB_OPTION . BASIC_FIELD_PREFIX . $field); |
|
125 | - return $fieldValue; |
|
126 | - } |
|
127 | - |
|
128 | - /** |
|
129 | - * helper function to add a field to a record while checking their state |
|
130 | - * |
|
131 | - * @param array $record existing record where the field and value shall be added to |
|
132 | - * @param string $field name of field to be checked according to BASIC_FIELDS |
|
133 | - * @param mixed $value data to be added to the record array named to $field |
|
134 | - * @param boolean $skip_basic_field_validation set to true if it is not a basic field to skip validation |
|
135 | - * @return array $record previous passed $record with added field data |
|
136 | - */ |
|
137 | - public static function add_to_record($record, $field, $value, $skip_basic_field_validation = false) |
|
138 | - { |
|
139 | - /** |
|
140 | - * only if enabled or validation skipped and not empty |
|
141 | - */ |
|
142 | - if ((!self::is_basic_field_enabled($field) && !$skip_basic_field_validation) || empty($value)) { |
|
143 | - return $record; |
|
144 | - } |
|
145 | - |
|
146 | - $record[$field] = $value; |
|
147 | - return $record; |
|
148 | - } |
|
149 | - |
|
150 | - /** |
|
151 | - * Get sale price or regular price based on product type |
|
152 | - * |
|
153 | - * @param mixed $product Product to check |
|
154 | - * @return array ['sale_price' => $sale_price,'regular_price' => $regular_price] Array with regular price and sale price |
|
155 | - */ |
|
156 | - public static function get_product_type_price($product) |
|
157 | - { |
|
158 | - $sale_price = 0; |
|
159 | - $regular_price = 0; |
|
160 | - if ($product->is_type('simple')) { |
|
161 | - $sale_price = $product->get_sale_price(); |
|
162 | - $regular_price = $product->get_regular_price(); |
|
163 | - } elseif ($product->is_type('variable')) { |
|
164 | - $sale_price = $product->get_variation_sale_price('min', true); |
|
165 | - $regular_price = $product->get_variation_regular_price('max', true); |
|
166 | - } |
|
167 | - return array( |
|
168 | - 'sale_price' => $sale_price, |
|
169 | - 'regular_price' => $regular_price |
|
170 | - ); |
|
171 | - } |
|
172 | - |
|
173 | - |
|
174 | - /** |
|
175 | - * Checks if stock management is enabled and if so, returns quantity and status |
|
176 | - * |
|
177 | - * @param mixed $product Product to check |
|
178 | - * @return array ['stock_quantity' => $stock_quantity,'stock_status' => $stock_status] Array with quantity and status. if stock management is disabled, false will be returned, |
|
179 | - */ |
|
180 | - public static function get_product_stock_data($product) |
|
181 | - { |
|
182 | - if ($product->get_manage_stock()) { |
|
183 | - return array( |
|
184 | - 'stock_quantity' => $product->get_stock_quantity(), |
|
185 | - 'stock_status' => $product->get_stock_status() |
|
186 | - ); |
|
187 | - } |
|
188 | - return false; |
|
189 | - } |
|
190 | - |
|
191 | - /** |
|
192 | - * Get product tags |
|
193 | - * |
|
194 | - * @param mixed $product Product to check |
|
195 | - * @return array ['tag1', 'tag2', ...] simple array with associated tags |
|
196 | - */ |
|
197 | - public static function get_product_tags($product) |
|
198 | - { |
|
199 | - $tags = get_the_terms($product->get_id(), 'product_tag'); |
|
200 | - $term_array = array(); |
|
201 | - if (is_array($tags)) { |
|
202 | - foreach ($tags as $tag) { |
|
203 | - $name = get_term($tag)->name; |
|
204 | - array_push($term_array, $name); |
|
205 | - } |
|
206 | - } |
|
207 | - return $term_array; |
|
208 | - } |
|
209 | - |
|
210 | - /** |
|
211 | - * Get product categories |
|
212 | - * |
|
213 | - * @param mixed $product Product to check |
|
214 | - * @return array ['tag1', 'tag2', ...] simple array with associated categories |
|
215 | - */ |
|
216 | - public static function get_product_categories($product) |
|
217 | - { |
|
218 | - $categories = get_the_terms($product->get_id(), 'product_cat'); |
|
219 | - $term_array = array(); |
|
220 | - foreach ($categories as $category) { |
|
221 | - $name = get_term($category)->name; |
|
222 | - $slug = get_term($category)->slug; |
|
223 | - array_push($term_array, array( |
|
224 | - "name" => $name, |
|
225 | - "slug" => $slug |
|
226 | - )); |
|
227 | - } |
|
228 | - return $term_array; |
|
229 | - } |
|
230 | - |
|
231 | - /** |
|
232 | - * Get attributes from product |
|
233 | - * |
|
234 | - * @param mixed $product Product to check |
|
235 | - * @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. |
|
236 | - */ |
|
237 | - public static function get_product_attributes($product) |
|
238 | - { |
|
239 | - /** |
|
240 | - * ensure that attrobutes are actually enabled |
|
241 | - */ |
|
242 | - $attributes_enabled = (int) get_option(ALGOWOO_DB_OPTION . ATTRIBUTES_ENABLED); |
|
243 | - if ($attributes_enabled !== 1) { |
|
244 | - return false; |
|
245 | - } |
|
246 | - |
|
247 | - /** |
|
248 | - * gather data and settings |
|
249 | - */ |
|
250 | - $rawAttributes = $product->get_attributes("edit"); |
|
251 | - $setting_visibility = get_option(ALGOWOO_DB_OPTION . ATTRIBUTES_VISIBILITY); |
|
252 | - $setting_variation = get_option(ALGOWOO_DB_OPTION . ATTRIBUTES_VARIATION); |
|
253 | - $setting_ids = get_option(ALGOWOO_DB_OPTION . ATTRIBUTES_LIST); |
|
254 | - $setting_ids = explode(",", $setting_ids); |
|
255 | - $setting_ids_interpolate = get_option(ALGOWOO_DB_OPTION . ATTRIBUTES_LIST_INTERPOLATE); |
|
256 | - $setting_ids_interpolate = explode(",", $setting_ids_interpolate); |
|
257 | - |
|
258 | - if (!$rawAttributes) { |
|
259 | - return false; |
|
260 | - } |
|
261 | - |
|
262 | - $attributes = []; |
|
263 | - foreach ($rawAttributes as $attribute) { |
|
264 | - /** |
|
265 | - * skip variation attributes |
|
266 | - */ |
|
267 | - if ($attribute->get_variation()) { |
|
268 | - continue; |
|
269 | - } |
|
270 | - |
|
271 | - /** |
|
272 | - * ensure that the setting_visibility is respected |
|
273 | - */ |
|
274 | - $visibility = $attribute["visible"]; |
|
275 | - if ( |
|
276 | - ($setting_visibility === "visible" && $visibility === false) || |
|
277 | - ($setting_visibility === "hidden" && $visibility === true) |
|
278 | - ) { |
|
279 | - continue; |
|
280 | - } |
|
281 | - |
|
282 | - /** |
|
283 | - * ensure that the variation is respected |
|
284 | - */ |
|
285 | - $variation = $attribute["variation"]; |
|
286 | - if ( |
|
287 | - ($setting_variation === "used" && $variation === false) || |
|
288 | - ($setting_variation === "notused" && $variation === true) |
|
289 | - ) { |
|
290 | - continue; |
|
291 | - } |
|
292 | - |
|
293 | - /** |
|
294 | - * ensure that taxonomy is whitelisted |
|
295 | - */ |
|
296 | - $id = $attribute->get_id(); |
|
297 | - if (!in_array($id, $setting_ids)) { |
|
298 | - continue; |
|
299 | - } |
|
300 | - |
|
301 | - $name = $attribute->get_name(); |
|
302 | - if ($attribute->is_taxonomy()) { |
|
303 | - $terms = wp_get_post_terms($product->get_id(), $name, 'all'); |
|
304 | - $tax_terms = array(); |
|
305 | - |
|
306 | - /** |
|
307 | - * interpolate all values when specified to interpolate |
|
308 | - */ |
|
309 | - if (in_array($id, $setting_ids_interpolate)) { |
|
310 | - $integers = array(); |
|
311 | - foreach ($terms as $term) { |
|
312 | - array_push($integers, (int) $term->name); |
|
313 | - } |
|
314 | - if (count($integers) > 0) { |
|
315 | - for ($i = min($integers); $i <= max($integers); $i++) { |
|
316 | - array_push($tax_terms, $i); |
|
317 | - } |
|
318 | - } |
|
319 | - } |
|
320 | - |
|
321 | - /** |
|
322 | - * normal mixed content case |
|
323 | - */ |
|
324 | - if (!in_array($id, $setting_ids_interpolate)) { |
|
325 | - foreach ($terms as $term) { |
|
326 | - $single_term = esc_html($term->name); |
|
327 | - array_push($tax_terms, $single_term); |
|
328 | - } |
|
329 | - } |
|
330 | - } |
|
331 | - $attributes[$name] = $tax_terms; |
|
332 | - } |
|
333 | - return $attributes; |
|
334 | - } |
|
335 | - |
|
336 | - /** |
|
337 | - * Send WooCommerce products to Algolia |
|
338 | - * |
|
339 | - * @param Int $id Product to send to Algolia if we send only a single product |
|
340 | - * @return void |
|
341 | - */ |
|
342 | - public static function send_products_to_algolia($id = '') |
|
343 | - { |
|
344 | - /** |
|
345 | - * Remove classes from plugin URL and autoload Algolia with Composer |
|
346 | - */ |
|
347 | - |
|
348 | - $base_plugin_directory = str_replace('classes', '', dirname(__FILE__)); |
|
349 | - require_once $base_plugin_directory . '/vendor/autoload.php'; |
|
350 | - |
|
351 | - /** |
|
352 | - * Fetch the required variables from the Settings API |
|
353 | - */ |
|
354 | - |
|
355 | - $algolia_application_id = get_option(ALGOWOO_DB_OPTION . ALGOLIA_APP_ID); |
|
356 | - $algolia_application_id = is_string($algolia_application_id) ? $algolia_application_id : CHANGE_ME; |
|
357 | - |
|
358 | - $algolia_api_key = get_option(ALGOWOO_DB_OPTION . ALGOLIA_API_KEY); |
|
359 | - $algolia_api_key = is_string($algolia_api_key) ? $algolia_api_key : CHANGE_ME; |
|
360 | - |
|
361 | - $algolia_index_name = get_option(ALGOWOO_DB_OPTION . INDEX_NAME); |
|
362 | - $algolia_index_name = is_string($algolia_index_name) ? $algolia_index_name : CHANGE_ME; |
|
363 | - |
|
364 | - /** |
|
365 | - * Display admin notice and return if not all values have been set |
|
366 | - */ |
|
367 | - |
|
368 | - Algolia_Check_Requirements::check_algolia_input_values($algolia_application_id, $algolia_api_key, $algolia_index_name); |
|
369 | - |
|
370 | - /** |
|
371 | - * Initiate the Algolia client |
|
372 | - */ |
|
373 | - self::$algolia = \Algolia\AlgoliaSearch\SearchClient::create($algolia_application_id, $algolia_api_key); |
|
374 | - |
|
375 | - /** |
|
376 | - * Check if we can connect, if not, handle the exception, display an error and then return |
|
377 | - */ |
|
378 | - self::can_connect_to_algolia(); |
|
379 | - |
|
380 | - /** |
|
381 | - * Initialize the search index and set the name to the option from the database |
|
382 | - */ |
|
383 | - $index = self::$algolia->initIndex($algolia_index_name); |
|
384 | - |
|
385 | - /** |
|
386 | - * Setup arguments for sending all products to Algolia |
|
387 | - * |
|
388 | - * Limit => -1 means we send all products |
|
389 | - */ |
|
390 | - $arguments = array( |
|
391 | - 'status' => 'publish', |
|
392 | - 'limit' => -1, |
|
393 | - 'paginate' => false, |
|
394 | - ); |
|
395 | - |
|
396 | - /** |
|
397 | - * Setup arguments for sending only a single product |
|
398 | - */ |
|
399 | - if (isset($id) && '' !== $id) { |
|
400 | - $arguments = array( |
|
401 | - 'status' => 'publish', |
|
402 | - 'include' => array($id), |
|
403 | - 'paginate' => false, |
|
404 | - ); |
|
405 | - } |
|
406 | - |
|
407 | - /** |
|
408 | - * Fetch all products from WooCommerce |
|
409 | - * |
|
410 | - * @see https://docs.woocommerce.com/wc-apidocs/function-wc_get_products.html |
|
411 | - */ |
|
412 | - $products = |
|
413 | - /** @scrutinizer ignore-call */ |
|
414 | - wc_get_products($arguments); |
|
415 | - |
|
416 | - if (empty($products)) { |
|
417 | - return; |
|
418 | - } |
|
419 | - $records = array(); |
|
420 | - $record = array(); |
|
421 | - |
|
422 | - foreach ($products as $product) { |
|
423 | - /** |
|
424 | - * Set sale price or regular price based on product type |
|
425 | - */ |
|
426 | - $product_type_price = self::get_product_type_price($product); |
|
427 | - $sale_price = $product_type_price['sale_price']; |
|
428 | - $regular_price = $product_type_price['regular_price']; |
|
429 | - |
|
430 | - |
|
431 | - |
|
432 | - |
|
433 | - /** |
|
434 | - * always add objectID (mandatory field for algolia) |
|
435 | - */ |
|
436 | - $record['objectID'] = $product->get_id(); |
|
437 | - |
|
438 | - /** |
|
439 | - * Extract image from $product->get_image() |
|
440 | - */ |
|
441 | - if (self::is_basic_field_enabled("product_image")) { |
|
442 | - preg_match('/<img(.*)src(.*)=(.*)"(.*)"/U', $product->get_image(), $result); |
|
443 | - $record["product_image"] = array_pop($result); |
|
444 | - } |
|
445 | - |
|
446 | - $record = self::add_to_record($record, 'product_name', $product->get_name()); |
|
447 | - $record = self::add_to_record($record, 'short_description', $product->get_short_description()); |
|
448 | - $record = self::add_to_record($record, 'long_description', $product->get_description()); |
|
449 | - $record = self::add_to_record($record, 'excerpt', get_the_excerpt($product->get_id())); |
|
450 | - $record = self::add_to_record($record, 'regular_price', $regular_price); |
|
451 | - $record = self::add_to_record($record, 'sale_price', $sale_price); |
|
452 | - $record = self::add_to_record($record, 'on_sale', $product->is_on_sale()); |
|
453 | - $record = self::add_to_record($record, 'permalink', $product->get_permalink()); |
|
454 | - $record = self::add_to_record($record, 'categories', self::get_product_categories($product)); |
|
455 | - $record = self::add_to_record($record, 'tags', self::get_product_tags($product)); |
|
456 | - $record = self::add_to_record($record, 'attributes', self::get_product_attributes($product), true); |
|
457 | - |
|
458 | - |
|
459 | - |
|
460 | - /** |
|
461 | - * Add stock information if stock management is on |
|
462 | - */ |
|
463 | - $stock_data = self::get_product_stock_data($product); |
|
464 | - if (is_array($stock_data)) { |
|
465 | - $record = self::add_to_record($record, 'stock_quantity', $stock_data['stock_quantity']); |
|
466 | - $record = self::add_to_record($record, 'stock_status', $stock_data['stock_status']); |
|
467 | - } |
|
468 | - |
|
469 | - $records[] = $record; |
|
470 | - } |
|
471 | - |
|
472 | - wp_reset_postdata(); |
|
473 | - |
|
474 | - /** |
|
475 | - * Send the information to Algolia and save the result |
|
476 | - * If result is NullResponse, print an error message |
|
477 | - */ |
|
478 | - $result = $index->saveObjects($records); |
|
479 | - |
|
480 | - if ('Algolia\AlgoliaSearch\Response\NullResponse' === get_class($result)) { |
|
481 | - wp_die(esc_html__('No response from the server. Please check your settings and try again', 'algolia_woo_indexer_settings')); |
|
482 | - } |
|
483 | - |
|
484 | - /** |
|
485 | - * Display success message |
|
486 | - */ |
|
487 | - echo '<div class="notice notice-success is-dismissible"> |
|
110 | + } |
|
111 | + ); |
|
112 | + return; |
|
113 | + } |
|
114 | + } |
|
115 | + |
|
116 | + /** |
|
117 | + * check if the field is enabled and shall be sent |
|
118 | + * |
|
119 | + * @param mixed $field name of field to be checked according to BASIC_FIELDS |
|
120 | + * @return boolean true if enable, false is not enabled |
|
121 | + */ |
|
122 | + public static function is_basic_field_enabled($field) |
|
123 | + { |
|
124 | + $fieldValue = get_option(ALGOWOO_DB_OPTION . BASIC_FIELD_PREFIX . $field); |
|
125 | + return $fieldValue; |
|
126 | + } |
|
127 | + |
|
128 | + /** |
|
129 | + * helper function to add a field to a record while checking their state |
|
130 | + * |
|
131 | + * @param array $record existing record where the field and value shall be added to |
|
132 | + * @param string $field name of field to be checked according to BASIC_FIELDS |
|
133 | + * @param mixed $value data to be added to the record array named to $field |
|
134 | + * @param boolean $skip_basic_field_validation set to true if it is not a basic field to skip validation |
|
135 | + * @return array $record previous passed $record with added field data |
|
136 | + */ |
|
137 | + public static function add_to_record($record, $field, $value, $skip_basic_field_validation = false) |
|
138 | + { |
|
139 | + /** |
|
140 | + * only if enabled or validation skipped and not empty |
|
141 | + */ |
|
142 | + if ((!self::is_basic_field_enabled($field) && !$skip_basic_field_validation) || empty($value)) { |
|
143 | + return $record; |
|
144 | + } |
|
145 | + |
|
146 | + $record[$field] = $value; |
|
147 | + return $record; |
|
148 | + } |
|
149 | + |
|
150 | + /** |
|
151 | + * Get sale price or regular price based on product type |
|
152 | + * |
|
153 | + * @param mixed $product Product to check |
|
154 | + * @return array ['sale_price' => $sale_price,'regular_price' => $regular_price] Array with regular price and sale price |
|
155 | + */ |
|
156 | + public static function get_product_type_price($product) |
|
157 | + { |
|
158 | + $sale_price = 0; |
|
159 | + $regular_price = 0; |
|
160 | + if ($product->is_type('simple')) { |
|
161 | + $sale_price = $product->get_sale_price(); |
|
162 | + $regular_price = $product->get_regular_price(); |
|
163 | + } elseif ($product->is_type('variable')) { |
|
164 | + $sale_price = $product->get_variation_sale_price('min', true); |
|
165 | + $regular_price = $product->get_variation_regular_price('max', true); |
|
166 | + } |
|
167 | + return array( |
|
168 | + 'sale_price' => $sale_price, |
|
169 | + 'regular_price' => $regular_price |
|
170 | + ); |
|
171 | + } |
|
172 | + |
|
173 | + |
|
174 | + /** |
|
175 | + * Checks if stock management is enabled and if so, returns quantity and status |
|
176 | + * |
|
177 | + * @param mixed $product Product to check |
|
178 | + * @return array ['stock_quantity' => $stock_quantity,'stock_status' => $stock_status] Array with quantity and status. if stock management is disabled, false will be returned, |
|
179 | + */ |
|
180 | + public static function get_product_stock_data($product) |
|
181 | + { |
|
182 | + if ($product->get_manage_stock()) { |
|
183 | + return array( |
|
184 | + 'stock_quantity' => $product->get_stock_quantity(), |
|
185 | + 'stock_status' => $product->get_stock_status() |
|
186 | + ); |
|
187 | + } |
|
188 | + return false; |
|
189 | + } |
|
190 | + |
|
191 | + /** |
|
192 | + * Get product tags |
|
193 | + * |
|
194 | + * @param mixed $product Product to check |
|
195 | + * @return array ['tag1', 'tag2', ...] simple array with associated tags |
|
196 | + */ |
|
197 | + public static function get_product_tags($product) |
|
198 | + { |
|
199 | + $tags = get_the_terms($product->get_id(), 'product_tag'); |
|
200 | + $term_array = array(); |
|
201 | + if (is_array($tags)) { |
|
202 | + foreach ($tags as $tag) { |
|
203 | + $name = get_term($tag)->name; |
|
204 | + array_push($term_array, $name); |
|
205 | + } |
|
206 | + } |
|
207 | + return $term_array; |
|
208 | + } |
|
209 | + |
|
210 | + /** |
|
211 | + * Get product categories |
|
212 | + * |
|
213 | + * @param mixed $product Product to check |
|
214 | + * @return array ['tag1', 'tag2', ...] simple array with associated categories |
|
215 | + */ |
|
216 | + public static function get_product_categories($product) |
|
217 | + { |
|
218 | + $categories = get_the_terms($product->get_id(), 'product_cat'); |
|
219 | + $term_array = array(); |
|
220 | + foreach ($categories as $category) { |
|
221 | + $name = get_term($category)->name; |
|
222 | + $slug = get_term($category)->slug; |
|
223 | + array_push($term_array, array( |
|
224 | + "name" => $name, |
|
225 | + "slug" => $slug |
|
226 | + )); |
|
227 | + } |
|
228 | + return $term_array; |
|
229 | + } |
|
230 | + |
|
231 | + /** |
|
232 | + * Get attributes from product |
|
233 | + * |
|
234 | + * @param mixed $product Product to check |
|
235 | + * @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. |
|
236 | + */ |
|
237 | + public static function get_product_attributes($product) |
|
238 | + { |
|
239 | + /** |
|
240 | + * ensure that attrobutes are actually enabled |
|
241 | + */ |
|
242 | + $attributes_enabled = (int) get_option(ALGOWOO_DB_OPTION . ATTRIBUTES_ENABLED); |
|
243 | + if ($attributes_enabled !== 1) { |
|
244 | + return false; |
|
245 | + } |
|
246 | + |
|
247 | + /** |
|
248 | + * gather data and settings |
|
249 | + */ |
|
250 | + $rawAttributes = $product->get_attributes("edit"); |
|
251 | + $setting_visibility = get_option(ALGOWOO_DB_OPTION . ATTRIBUTES_VISIBILITY); |
|
252 | + $setting_variation = get_option(ALGOWOO_DB_OPTION . ATTRIBUTES_VARIATION); |
|
253 | + $setting_ids = get_option(ALGOWOO_DB_OPTION . ATTRIBUTES_LIST); |
|
254 | + $setting_ids = explode(",", $setting_ids); |
|
255 | + $setting_ids_interpolate = get_option(ALGOWOO_DB_OPTION . ATTRIBUTES_LIST_INTERPOLATE); |
|
256 | + $setting_ids_interpolate = explode(",", $setting_ids_interpolate); |
|
257 | + |
|
258 | + if (!$rawAttributes) { |
|
259 | + return false; |
|
260 | + } |
|
261 | + |
|
262 | + $attributes = []; |
|
263 | + foreach ($rawAttributes as $attribute) { |
|
264 | + /** |
|
265 | + * skip variation attributes |
|
266 | + */ |
|
267 | + if ($attribute->get_variation()) { |
|
268 | + continue; |
|
269 | + } |
|
270 | + |
|
271 | + /** |
|
272 | + * ensure that the setting_visibility is respected |
|
273 | + */ |
|
274 | + $visibility = $attribute["visible"]; |
|
275 | + if ( |
|
276 | + ($setting_visibility === "visible" && $visibility === false) || |
|
277 | + ($setting_visibility === "hidden" && $visibility === true) |
|
278 | + ) { |
|
279 | + continue; |
|
280 | + } |
|
281 | + |
|
282 | + /** |
|
283 | + * ensure that the variation is respected |
|
284 | + */ |
|
285 | + $variation = $attribute["variation"]; |
|
286 | + if ( |
|
287 | + ($setting_variation === "used" && $variation === false) || |
|
288 | + ($setting_variation === "notused" && $variation === true) |
|
289 | + ) { |
|
290 | + continue; |
|
291 | + } |
|
292 | + |
|
293 | + /** |
|
294 | + * ensure that taxonomy is whitelisted |
|
295 | + */ |
|
296 | + $id = $attribute->get_id(); |
|
297 | + if (!in_array($id, $setting_ids)) { |
|
298 | + continue; |
|
299 | + } |
|
300 | + |
|
301 | + $name = $attribute->get_name(); |
|
302 | + if ($attribute->is_taxonomy()) { |
|
303 | + $terms = wp_get_post_terms($product->get_id(), $name, 'all'); |
|
304 | + $tax_terms = array(); |
|
305 | + |
|
306 | + /** |
|
307 | + * interpolate all values when specified to interpolate |
|
308 | + */ |
|
309 | + if (in_array($id, $setting_ids_interpolate)) { |
|
310 | + $integers = array(); |
|
311 | + foreach ($terms as $term) { |
|
312 | + array_push($integers, (int) $term->name); |
|
313 | + } |
|
314 | + if (count($integers) > 0) { |
|
315 | + for ($i = min($integers); $i <= max($integers); $i++) { |
|
316 | + array_push($tax_terms, $i); |
|
317 | + } |
|
318 | + } |
|
319 | + } |
|
320 | + |
|
321 | + /** |
|
322 | + * normal mixed content case |
|
323 | + */ |
|
324 | + if (!in_array($id, $setting_ids_interpolate)) { |
|
325 | + foreach ($terms as $term) { |
|
326 | + $single_term = esc_html($term->name); |
|
327 | + array_push($tax_terms, $single_term); |
|
328 | + } |
|
329 | + } |
|
330 | + } |
|
331 | + $attributes[$name] = $tax_terms; |
|
332 | + } |
|
333 | + return $attributes; |
|
334 | + } |
|
335 | + |
|
336 | + /** |
|
337 | + * Send WooCommerce products to Algolia |
|
338 | + * |
|
339 | + * @param Int $id Product to send to Algolia if we send only a single product |
|
340 | + * @return void |
|
341 | + */ |
|
342 | + public static function send_products_to_algolia($id = '') |
|
343 | + { |
|
344 | + /** |
|
345 | + * Remove classes from plugin URL and autoload Algolia with Composer |
|
346 | + */ |
|
347 | + |
|
348 | + $base_plugin_directory = str_replace('classes', '', dirname(__FILE__)); |
|
349 | + require_once $base_plugin_directory . '/vendor/autoload.php'; |
|
350 | + |
|
351 | + /** |
|
352 | + * Fetch the required variables from the Settings API |
|
353 | + */ |
|
354 | + |
|
355 | + $algolia_application_id = get_option(ALGOWOO_DB_OPTION . ALGOLIA_APP_ID); |
|
356 | + $algolia_application_id = is_string($algolia_application_id) ? $algolia_application_id : CHANGE_ME; |
|
357 | + |
|
358 | + $algolia_api_key = get_option(ALGOWOO_DB_OPTION . ALGOLIA_API_KEY); |
|
359 | + $algolia_api_key = is_string($algolia_api_key) ? $algolia_api_key : CHANGE_ME; |
|
360 | + |
|
361 | + $algolia_index_name = get_option(ALGOWOO_DB_OPTION . INDEX_NAME); |
|
362 | + $algolia_index_name = is_string($algolia_index_name) ? $algolia_index_name : CHANGE_ME; |
|
363 | + |
|
364 | + /** |
|
365 | + * Display admin notice and return if not all values have been set |
|
366 | + */ |
|
367 | + |
|
368 | + Algolia_Check_Requirements::check_algolia_input_values($algolia_application_id, $algolia_api_key, $algolia_index_name); |
|
369 | + |
|
370 | + /** |
|
371 | + * Initiate the Algolia client |
|
372 | + */ |
|
373 | + self::$algolia = \Algolia\AlgoliaSearch\SearchClient::create($algolia_application_id, $algolia_api_key); |
|
374 | + |
|
375 | + /** |
|
376 | + * Check if we can connect, if not, handle the exception, display an error and then return |
|
377 | + */ |
|
378 | + self::can_connect_to_algolia(); |
|
379 | + |
|
380 | + /** |
|
381 | + * Initialize the search index and set the name to the option from the database |
|
382 | + */ |
|
383 | + $index = self::$algolia->initIndex($algolia_index_name); |
|
384 | + |
|
385 | + /** |
|
386 | + * Setup arguments for sending all products to Algolia |
|
387 | + * |
|
388 | + * Limit => -1 means we send all products |
|
389 | + */ |
|
390 | + $arguments = array( |
|
391 | + 'status' => 'publish', |
|
392 | + 'limit' => -1, |
|
393 | + 'paginate' => false, |
|
394 | + ); |
|
395 | + |
|
396 | + /** |
|
397 | + * Setup arguments for sending only a single product |
|
398 | + */ |
|
399 | + if (isset($id) && '' !== $id) { |
|
400 | + $arguments = array( |
|
401 | + 'status' => 'publish', |
|
402 | + 'include' => array($id), |
|
403 | + 'paginate' => false, |
|
404 | + ); |
|
405 | + } |
|
406 | + |
|
407 | + /** |
|
408 | + * Fetch all products from WooCommerce |
|
409 | + * |
|
410 | + * @see https://docs.woocommerce.com/wc-apidocs/function-wc_get_products.html |
|
411 | + */ |
|
412 | + $products = |
|
413 | + /** @scrutinizer ignore-call */ |
|
414 | + wc_get_products($arguments); |
|
415 | + |
|
416 | + if (empty($products)) { |
|
417 | + return; |
|
418 | + } |
|
419 | + $records = array(); |
|
420 | + $record = array(); |
|
421 | + |
|
422 | + foreach ($products as $product) { |
|
423 | + /** |
|
424 | + * Set sale price or regular price based on product type |
|
425 | + */ |
|
426 | + $product_type_price = self::get_product_type_price($product); |
|
427 | + $sale_price = $product_type_price['sale_price']; |
|
428 | + $regular_price = $product_type_price['regular_price']; |
|
429 | + |
|
430 | + |
|
431 | + |
|
432 | + |
|
433 | + /** |
|
434 | + * always add objectID (mandatory field for algolia) |
|
435 | + */ |
|
436 | + $record['objectID'] = $product->get_id(); |
|
437 | + |
|
438 | + /** |
|
439 | + * Extract image from $product->get_image() |
|
440 | + */ |
|
441 | + if (self::is_basic_field_enabled("product_image")) { |
|
442 | + preg_match('/<img(.*)src(.*)=(.*)"(.*)"/U', $product->get_image(), $result); |
|
443 | + $record["product_image"] = array_pop($result); |
|
444 | + } |
|
445 | + |
|
446 | + $record = self::add_to_record($record, 'product_name', $product->get_name()); |
|
447 | + $record = self::add_to_record($record, 'short_description', $product->get_short_description()); |
|
448 | + $record = self::add_to_record($record, 'long_description', $product->get_description()); |
|
449 | + $record = self::add_to_record($record, 'excerpt', get_the_excerpt($product->get_id())); |
|
450 | + $record = self::add_to_record($record, 'regular_price', $regular_price); |
|
451 | + $record = self::add_to_record($record, 'sale_price', $sale_price); |
|
452 | + $record = self::add_to_record($record, 'on_sale', $product->is_on_sale()); |
|
453 | + $record = self::add_to_record($record, 'permalink', $product->get_permalink()); |
|
454 | + $record = self::add_to_record($record, 'categories', self::get_product_categories($product)); |
|
455 | + $record = self::add_to_record($record, 'tags', self::get_product_tags($product)); |
|
456 | + $record = self::add_to_record($record, 'attributes', self::get_product_attributes($product), true); |
|
457 | + |
|
458 | + |
|
459 | + |
|
460 | + /** |
|
461 | + * Add stock information if stock management is on |
|
462 | + */ |
|
463 | + $stock_data = self::get_product_stock_data($product); |
|
464 | + if (is_array($stock_data)) { |
|
465 | + $record = self::add_to_record($record, 'stock_quantity', $stock_data['stock_quantity']); |
|
466 | + $record = self::add_to_record($record, 'stock_status', $stock_data['stock_status']); |
|
467 | + } |
|
468 | + |
|
469 | + $records[] = $record; |
|
470 | + } |
|
471 | + |
|
472 | + wp_reset_postdata(); |
|
473 | + |
|
474 | + /** |
|
475 | + * Send the information to Algolia and save the result |
|
476 | + * If result is NullResponse, print an error message |
|
477 | + */ |
|
478 | + $result = $index->saveObjects($records); |
|
479 | + |
|
480 | + if ('Algolia\AlgoliaSearch\Response\NullResponse' === get_class($result)) { |
|
481 | + wp_die(esc_html__('No response from the server. Please check your settings and try again', 'algolia_woo_indexer_settings')); |
|
482 | + } |
|
483 | + |
|
484 | + /** |
|
485 | + * Display success message |
|
486 | + */ |
|
487 | + echo '<div class="notice notice-success is-dismissible"> |
|
488 | 488 | <p>' . esc_html__('Product(s) sent to Algolia.', 'algolia-woo-indexer') . '</p> |
489 | 489 | </div>'; |
490 | - } |
|
491 | - } |
|
490 | + } |
|
491 | + } |
|
492 | 492 | } |
@@ -14,14 +14,14 @@ discard block |
||
14 | 14 | /** |
15 | 15 | * Abort if this file is called directly |
16 | 16 | */ |
17 | -if (!defined('ABSPATH')) { |
|
17 | +if ( ! defined('ABSPATH')) { |
|
18 | 18 | exit; |
19 | 19 | } |
20 | 20 | |
21 | 21 | /** |
22 | 22 | * Include plugin file if function is_plugin_active does not exist |
23 | 23 | */ |
24 | -if (!function_exists('is_plugin_active')) { |
|
24 | +if ( ! function_exists('is_plugin_active')) { |
|
25 | 25 | require_once(ABSPATH . '/wp-admin/includes/plugin.php'); |
26 | 26 | } |
27 | 27 | |
@@ -76,7 +76,7 @@ discard block |
||
76 | 76 | define('ATTRIBUTES_LIST_INTERPOLATE', '_attributes_list_interpolate'); |
77 | 77 | |
78 | 78 | |
79 | -if (!class_exists('Algolia_Send_Products')) { |
|
79 | +if ( ! class_exists('Algolia_Send_Products')) { |
|
80 | 80 | /** |
81 | 81 | * Algolia WooIndexer main class |
82 | 82 | */ |
@@ -103,7 +103,7 @@ discard block |
||
103 | 103 | } catch (\Algolia\AlgoliaSearch\Exceptions\UnreachableException $error) { |
104 | 104 | add_action( |
105 | 105 | 'admin_notices', |
106 | - function () { |
|
106 | + function() { |
|
107 | 107 | echo '<div class="error notice"> |
108 | 108 | <p>' . esc_html__('An error has been encountered. Please check your application ID and API key. ', 'algolia-woo-indexer') . '</p> |
109 | 109 | </div>'; |
@@ -139,7 +139,7 @@ discard block |
||
139 | 139 | /** |
140 | 140 | * only if enabled or validation skipped and not empty |
141 | 141 | */ |
142 | - if ((!self::is_basic_field_enabled($field) && !$skip_basic_field_validation) || empty($value)) { |
|
142 | + if (( ! self::is_basic_field_enabled($field) && ! $skip_basic_field_validation) || empty($value)) { |
|
143 | 143 | return $record; |
144 | 144 | } |
145 | 145 | |
@@ -158,11 +158,11 @@ discard block |
||
158 | 158 | $sale_price = 0; |
159 | 159 | $regular_price = 0; |
160 | 160 | if ($product->is_type('simple')) { |
161 | - $sale_price = $product->get_sale_price(); |
|
162 | - $regular_price = $product->get_regular_price(); |
|
161 | + $sale_price = $product->get_sale_price(); |
|
162 | + $regular_price = $product->get_regular_price(); |
|
163 | 163 | } elseif ($product->is_type('variable')) { |
164 | - $sale_price = $product->get_variation_sale_price('min', true); |
|
165 | - $regular_price = $product->get_variation_regular_price('max', true); |
|
164 | + $sale_price = $product->get_variation_sale_price('min', true); |
|
165 | + $regular_price = $product->get_variation_regular_price('max', true); |
|
166 | 166 | } |
167 | 167 | return array( |
168 | 168 | 'sale_price' => $sale_price, |
@@ -255,7 +255,7 @@ discard block |
||
255 | 255 | $setting_ids_interpolate = get_option(ALGOWOO_DB_OPTION . ATTRIBUTES_LIST_INTERPOLATE); |
256 | 256 | $setting_ids_interpolate = explode(",", $setting_ids_interpolate); |
257 | 257 | |
258 | - if (!$rawAttributes) { |
|
258 | + if ( ! $rawAttributes) { |
|
259 | 259 | return false; |
260 | 260 | } |
261 | 261 | |
@@ -273,8 +273,8 @@ discard block |
||
273 | 273 | */ |
274 | 274 | $visibility = $attribute["visible"]; |
275 | 275 | if ( |
276 | - ($setting_visibility === "visible" && $visibility === false) || |
|
277 | - ($setting_visibility === "hidden" && $visibility === true) |
|
276 | + ($setting_visibility === "visible" && $visibility === false) || |
|
277 | + ($setting_visibility === "hidden" && $visibility === true) |
|
278 | 278 | ) { |
279 | 279 | continue; |
280 | 280 | } |
@@ -284,8 +284,8 @@ discard block |
||
284 | 284 | */ |
285 | 285 | $variation = $attribute["variation"]; |
286 | 286 | if ( |
287 | - ($setting_variation === "used" && $variation === false) || |
|
288 | - ($setting_variation === "notused" && $variation === true) |
|
287 | + ($setting_variation === "used" && $variation === false) || |
|
288 | + ($setting_variation === "notused" && $variation === true) |
|
289 | 289 | ) { |
290 | 290 | continue; |
291 | 291 | } |
@@ -294,7 +294,7 @@ discard block |
||
294 | 294 | * ensure that taxonomy is whitelisted |
295 | 295 | */ |
296 | 296 | $id = $attribute->get_id(); |
297 | - if (!in_array($id, $setting_ids)) { |
|
297 | + if ( ! in_array($id, $setting_ids)) { |
|
298 | 298 | continue; |
299 | 299 | } |
300 | 300 | |
@@ -321,7 +321,7 @@ discard block |
||
321 | 321 | /** |
322 | 322 | * normal mixed content case |
323 | 323 | */ |
324 | - if (!in_array($id, $setting_ids_interpolate)) { |
|
324 | + if ( ! in_array($id, $setting_ids_interpolate)) { |
|
325 | 325 | foreach ($terms as $term) { |
326 | 326 | $single_term = esc_html($term->name); |
327 | 327 | array_push($tax_terms, $single_term); |
@@ -359,7 +359,7 @@ discard block |
||
359 | 359 | $algolia_api_key = is_string($algolia_api_key) ? $algolia_api_key : CHANGE_ME; |
360 | 360 | |
361 | 361 | $algolia_index_name = get_option(ALGOWOO_DB_OPTION . INDEX_NAME); |
362 | - $algolia_index_name = is_string($algolia_index_name) ? $algolia_index_name : CHANGE_ME; |
|
362 | + $algolia_index_name = is_string($algolia_index_name) ? $algolia_index_name : CHANGE_ME; |
|
363 | 363 | |
364 | 364 | /** |
365 | 365 | * Display admin notice and return if not all values have been set |
@@ -17,720 +17,720 @@ discard block |
||
17 | 17 | * Abort if this file is called directly |
18 | 18 | */ |
19 | 19 | if (!defined('ABSPATH')) { |
20 | - exit; |
|
20 | + exit; |
|
21 | 21 | } |
22 | 22 | |
23 | 23 | /** |
24 | 24 | * Include plugin file if function is_plugin_active does not exist |
25 | 25 | */ |
26 | 26 | if (!function_exists('is_plugin_active')) { |
27 | - require_once(ABSPATH . '/wp-admin/includes/plugin.php'); |
|
27 | + require_once(ABSPATH . '/wp-admin/includes/plugin.php'); |
|
28 | 28 | } |
29 | 29 | |
30 | 30 | if (!class_exists('Algolia_Woo_Indexer')) { |
31 | - /** |
|
32 | - * Algolia WooIndexer main class |
|
33 | - */ |
|
34 | - // TODO Rename class "Algolia_Woo_Indexer" to match the regular expression ^[A-Z][a-zA-Z0-9]*$. |
|
35 | - class Algolia_Woo_Indexer |
|
36 | - { |
|
37 | - const PLUGIN_NAME = 'Algolia Woo Indexer'; |
|
38 | - const PLUGIN_TRANSIENT = 'algowoo-plugin-notice'; |
|
39 | - |
|
40 | - /** |
|
41 | - * Class instance |
|
42 | - * |
|
43 | - * @var object |
|
44 | - */ |
|
45 | - private static $instance; |
|
46 | - |
|
47 | - /** |
|
48 | - * The plugin URL |
|
49 | - * |
|
50 | - * @var string |
|
51 | - */ |
|
52 | - private static $plugin_url = ''; |
|
53 | - |
|
54 | - /** |
|
55 | - * Class constructor |
|
56 | - * |
|
57 | - * @return void |
|
58 | - */ |
|
59 | - public function __construct() |
|
60 | - { |
|
61 | - $this->init(); |
|
62 | - } |
|
63 | - |
|
64 | - /** |
|
65 | - * Setup sections and fields to store and retrieve values from Settings API |
|
66 | - * |
|
67 | - * @return void |
|
68 | - */ |
|
69 | - public static function setup_settings_sections() |
|
70 | - { |
|
71 | - /** |
|
72 | - * Setup arguments for settings sections and fields |
|
73 | - * |
|
74 | - * @see https://developer.wordpress.org/reference/functions/register_setting/ |
|
75 | - */ |
|
76 | - if (is_admin()) { |
|
77 | - $arguments = array( |
|
78 | - 'type' => 'string', |
|
79 | - 'sanitize_callback' => 'settings_fields_validate_options', |
|
80 | - 'default' => null, |
|
81 | - ); |
|
82 | - register_setting('algolia_woo_options', 'algolia_woo_options', $arguments); |
|
83 | - |
|
84 | - /** |
|
85 | - * Make sure we reference the instance of the current class by using self::get_instance() |
|
86 | - * This way we can setup the correct callback function for add_settings_section and add_settings_field |
|
87 | - */ |
|
88 | - $algowooindexer = self::get_instance(); |
|
89 | - |
|
90 | - /** |
|
91 | - * Add our necessary settings sections and fields |
|
92 | - */ |
|
93 | - add_settings_section( |
|
94 | - 'algolia_woo_indexer_main', |
|
95 | - esc_html__('Algolia Woo Plugin Settings', 'algolia-woo-indexer'), |
|
96 | - array($algowooindexer, 'algolia_woo_indexer_section_text'), |
|
97 | - 'algolia_woo_indexer' |
|
98 | - ); |
|
99 | - add_settings_field( |
|
100 | - 'algolia_woo_indexer_application_id', |
|
101 | - esc_html__('Application ID', 'algolia-woo-indexer'), |
|
102 | - array($algowooindexer, 'algolia_woo_indexer_application_id_output'), |
|
103 | - 'algolia_woo_indexer', |
|
104 | - 'algolia_woo_indexer_main' |
|
105 | - ); |
|
106 | - add_settings_field( |
|
107 | - 'algolia_woo_indexer_admin_api_key', |
|
108 | - esc_html__('Admin API Key', 'algolia-woo-indexer'), |
|
109 | - array($algowooindexer, 'algolia_woo_indexer_admin_api_key_output'), |
|
110 | - 'algolia_woo_indexer', |
|
111 | - 'algolia_woo_indexer_main' |
|
112 | - ); |
|
113 | - add_settings_field( |
|
114 | - 'algolia_woo_indexer_index_name', |
|
115 | - esc_html__('Index name (will be created if not existing)', 'algolia-woo-indexer'), |
|
116 | - array($algowooindexer, 'algolia_woo_indexer_index_name_output'), |
|
117 | - 'algolia_woo_indexer', |
|
118 | - 'algolia_woo_indexer_main' |
|
119 | - ); |
|
120 | - add_settings_field( |
|
121 | - 'algolia_woo_indexer_automatically_send_new_products', |
|
122 | - esc_html__('Automatically index new products', 'algolia-woo-indexer'), |
|
123 | - array($algowooindexer, 'algolia_woo_indexer_automatically_send_new_products_output'), |
|
124 | - 'algolia_woo_indexer', |
|
125 | - 'algolia_woo_indexer_main' |
|
126 | - ); |
|
127 | - |
|
128 | - /** |
|
129 | - * Add sections and fields for the basic fields |
|
130 | - */ |
|
131 | - add_settings_section( |
|
132 | - 'algolia_woo_indexer_fields', |
|
133 | - esc_html__('Fields indexing settings', 'algolia-woo-indexer'), |
|
134 | - array($algowooindexer, 'algolia_woo_indexer_fields_section_text'), |
|
135 | - 'algolia_woo_indexer' |
|
136 | - ); |
|
137 | - foreach (BASIC_FIELDS as $field) { |
|
138 | - add_settings_field( |
|
139 | - 'algolia_woo_indexer_field_' . $field, |
|
140 | - esc_html__($field, 'algolia-woo-indexer'), |
|
141 | - array($algowooindexer, 'algolia_woo_indexer_field_output'), |
|
142 | - 'algolia_woo_indexer', |
|
143 | - 'algolia_woo_indexer_fields', |
|
144 | - array( |
|
145 | - 'label_for' => 'algolia_woo_indexer_field_' . $field, |
|
146 | - 'name' => $field |
|
147 | - ) |
|
148 | - ); |
|
149 | - } |
|
150 | - |
|
151 | - /** |
|
152 | - * Add sections and fields for the attributes |
|
153 | - */ |
|
154 | - add_settings_section( |
|
155 | - 'algolia_woo_indexer_attributes', |
|
156 | - esc_html__('Attributes indexing settings', 'algolia-woo-indexer'), |
|
157 | - array($algowooindexer, 'algolia_woo_indexer_attributes_section_text'), |
|
158 | - 'algolia_woo_indexer' |
|
159 | - ); |
|
160 | - add_settings_field( |
|
161 | - 'algolia_woo_indexer_attributes_enabled', |
|
162 | - esc_html__('Enable indexing of attributes', 'algolia-woo-indexer'), |
|
163 | - array($algowooindexer, 'algolia_woo_indexer_attributes_enabled_output'), |
|
164 | - 'algolia_woo_indexer', |
|
165 | - 'algolia_woo_indexer_attributes' |
|
166 | - ); |
|
167 | - add_settings_field( |
|
168 | - 'algolia_woo_indexer_attributes_visibility', |
|
169 | - esc_html__('Visibility', 'algolia-woo-indexer'), |
|
170 | - array($algowooindexer, 'algolia_woo_indexer_attributes_visibility_output'), |
|
171 | - 'algolia_woo_indexer', |
|
172 | - 'algolia_woo_indexer_attributes' |
|
173 | - ); |
|
174 | - |
|
175 | - add_settings_field( |
|
176 | - 'algolia_woo_indexer_attributes_variation', |
|
177 | - esc_html__('Used for variations', 'algolia-woo-indexer'), |
|
178 | - array($algowooindexer, 'algolia_woo_indexer_attributes_variation_output'), |
|
179 | - 'algolia_woo_indexer', |
|
180 | - 'algolia_woo_indexer_attributes' |
|
181 | - ); |
|
182 | - add_settings_field( |
|
183 | - 'algolia_woo_indexer_attributes_list', |
|
184 | - esc_html__('Valid Attributes', 'algolia-woo-indexer'), |
|
185 | - array($algowooindexer, 'algolia_woo_indexer_attributes_list_output'), |
|
186 | - 'algolia_woo_indexer', |
|
187 | - 'algolia_woo_indexer_attributes' |
|
188 | - ); |
|
189 | - add_settings_field( |
|
190 | - 'algolia_woo_indexer_attributes_list_interpolate', |
|
191 | - esc_html__('Numeric Interpolation', 'algolia-woo-indexer'), |
|
192 | - array($algowooindexer, 'algolia_woo_indexer_attributes_list_interpolate_output'), |
|
193 | - 'algolia_woo_indexer', |
|
194 | - 'algolia_woo_indexer_attributes' |
|
195 | - ); |
|
196 | - } |
|
197 | - } |
|
198 | - |
|
199 | - /** |
|
200 | - * Output for admin API key field |
|
201 | - * |
|
202 | - * @see https://developer.wordpress.org/reference/functions/wp_nonce_field/ |
|
203 | - * |
|
204 | - * @return void |
|
205 | - */ |
|
206 | - public static function algolia_woo_indexer_admin_api_key_output() |
|
207 | - { |
|
208 | - $api_key = get_option(ALGOWOO_DB_OPTION . ALGOLIA_API_KEY); |
|
209 | - $api_key = is_string($api_key) ? $api_key : CHANGE_ME; |
|
210 | - |
|
211 | - wp_nonce_field('algolia_woo_indexer_admin_api_nonce_action', 'algolia_woo_indexer_admin_api_nonce_name'); |
|
212 | - |
|
213 | - echo "<input id='algolia_woo_indexer_admin_api_key' name='algolia_woo_indexer_admin_api_key[key]' |
|
31 | + /** |
|
32 | + * Algolia WooIndexer main class |
|
33 | + */ |
|
34 | + // TODO Rename class "Algolia_Woo_Indexer" to match the regular expression ^[A-Z][a-zA-Z0-9]*$. |
|
35 | + class Algolia_Woo_Indexer |
|
36 | + { |
|
37 | + const PLUGIN_NAME = 'Algolia Woo Indexer'; |
|
38 | + const PLUGIN_TRANSIENT = 'algowoo-plugin-notice'; |
|
39 | + |
|
40 | + /** |
|
41 | + * Class instance |
|
42 | + * |
|
43 | + * @var object |
|
44 | + */ |
|
45 | + private static $instance; |
|
46 | + |
|
47 | + /** |
|
48 | + * The plugin URL |
|
49 | + * |
|
50 | + * @var string |
|
51 | + */ |
|
52 | + private static $plugin_url = ''; |
|
53 | + |
|
54 | + /** |
|
55 | + * Class constructor |
|
56 | + * |
|
57 | + * @return void |
|
58 | + */ |
|
59 | + public function __construct() |
|
60 | + { |
|
61 | + $this->init(); |
|
62 | + } |
|
63 | + |
|
64 | + /** |
|
65 | + * Setup sections and fields to store and retrieve values from Settings API |
|
66 | + * |
|
67 | + * @return void |
|
68 | + */ |
|
69 | + public static function setup_settings_sections() |
|
70 | + { |
|
71 | + /** |
|
72 | + * Setup arguments for settings sections and fields |
|
73 | + * |
|
74 | + * @see https://developer.wordpress.org/reference/functions/register_setting/ |
|
75 | + */ |
|
76 | + if (is_admin()) { |
|
77 | + $arguments = array( |
|
78 | + 'type' => 'string', |
|
79 | + 'sanitize_callback' => 'settings_fields_validate_options', |
|
80 | + 'default' => null, |
|
81 | + ); |
|
82 | + register_setting('algolia_woo_options', 'algolia_woo_options', $arguments); |
|
83 | + |
|
84 | + /** |
|
85 | + * Make sure we reference the instance of the current class by using self::get_instance() |
|
86 | + * This way we can setup the correct callback function for add_settings_section and add_settings_field |
|
87 | + */ |
|
88 | + $algowooindexer = self::get_instance(); |
|
89 | + |
|
90 | + /** |
|
91 | + * Add our necessary settings sections and fields |
|
92 | + */ |
|
93 | + add_settings_section( |
|
94 | + 'algolia_woo_indexer_main', |
|
95 | + esc_html__('Algolia Woo Plugin Settings', 'algolia-woo-indexer'), |
|
96 | + array($algowooindexer, 'algolia_woo_indexer_section_text'), |
|
97 | + 'algolia_woo_indexer' |
|
98 | + ); |
|
99 | + add_settings_field( |
|
100 | + 'algolia_woo_indexer_application_id', |
|
101 | + esc_html__('Application ID', 'algolia-woo-indexer'), |
|
102 | + array($algowooindexer, 'algolia_woo_indexer_application_id_output'), |
|
103 | + 'algolia_woo_indexer', |
|
104 | + 'algolia_woo_indexer_main' |
|
105 | + ); |
|
106 | + add_settings_field( |
|
107 | + 'algolia_woo_indexer_admin_api_key', |
|
108 | + esc_html__('Admin API Key', 'algolia-woo-indexer'), |
|
109 | + array($algowooindexer, 'algolia_woo_indexer_admin_api_key_output'), |
|
110 | + 'algolia_woo_indexer', |
|
111 | + 'algolia_woo_indexer_main' |
|
112 | + ); |
|
113 | + add_settings_field( |
|
114 | + 'algolia_woo_indexer_index_name', |
|
115 | + esc_html__('Index name (will be created if not existing)', 'algolia-woo-indexer'), |
|
116 | + array($algowooindexer, 'algolia_woo_indexer_index_name_output'), |
|
117 | + 'algolia_woo_indexer', |
|
118 | + 'algolia_woo_indexer_main' |
|
119 | + ); |
|
120 | + add_settings_field( |
|
121 | + 'algolia_woo_indexer_automatically_send_new_products', |
|
122 | + esc_html__('Automatically index new products', 'algolia-woo-indexer'), |
|
123 | + array($algowooindexer, 'algolia_woo_indexer_automatically_send_new_products_output'), |
|
124 | + 'algolia_woo_indexer', |
|
125 | + 'algolia_woo_indexer_main' |
|
126 | + ); |
|
127 | + |
|
128 | + /** |
|
129 | + * Add sections and fields for the basic fields |
|
130 | + */ |
|
131 | + add_settings_section( |
|
132 | + 'algolia_woo_indexer_fields', |
|
133 | + esc_html__('Fields indexing settings', 'algolia-woo-indexer'), |
|
134 | + array($algowooindexer, 'algolia_woo_indexer_fields_section_text'), |
|
135 | + 'algolia_woo_indexer' |
|
136 | + ); |
|
137 | + foreach (BASIC_FIELDS as $field) { |
|
138 | + add_settings_field( |
|
139 | + 'algolia_woo_indexer_field_' . $field, |
|
140 | + esc_html__($field, 'algolia-woo-indexer'), |
|
141 | + array($algowooindexer, 'algolia_woo_indexer_field_output'), |
|
142 | + 'algolia_woo_indexer', |
|
143 | + 'algolia_woo_indexer_fields', |
|
144 | + array( |
|
145 | + 'label_for' => 'algolia_woo_indexer_field_' . $field, |
|
146 | + 'name' => $field |
|
147 | + ) |
|
148 | + ); |
|
149 | + } |
|
150 | + |
|
151 | + /** |
|
152 | + * Add sections and fields for the attributes |
|
153 | + */ |
|
154 | + add_settings_section( |
|
155 | + 'algolia_woo_indexer_attributes', |
|
156 | + esc_html__('Attributes indexing settings', 'algolia-woo-indexer'), |
|
157 | + array($algowooindexer, 'algolia_woo_indexer_attributes_section_text'), |
|
158 | + 'algolia_woo_indexer' |
|
159 | + ); |
|
160 | + add_settings_field( |
|
161 | + 'algolia_woo_indexer_attributes_enabled', |
|
162 | + esc_html__('Enable indexing of attributes', 'algolia-woo-indexer'), |
|
163 | + array($algowooindexer, 'algolia_woo_indexer_attributes_enabled_output'), |
|
164 | + 'algolia_woo_indexer', |
|
165 | + 'algolia_woo_indexer_attributes' |
|
166 | + ); |
|
167 | + add_settings_field( |
|
168 | + 'algolia_woo_indexer_attributes_visibility', |
|
169 | + esc_html__('Visibility', 'algolia-woo-indexer'), |
|
170 | + array($algowooindexer, 'algolia_woo_indexer_attributes_visibility_output'), |
|
171 | + 'algolia_woo_indexer', |
|
172 | + 'algolia_woo_indexer_attributes' |
|
173 | + ); |
|
174 | + |
|
175 | + add_settings_field( |
|
176 | + 'algolia_woo_indexer_attributes_variation', |
|
177 | + esc_html__('Used for variations', 'algolia-woo-indexer'), |
|
178 | + array($algowooindexer, 'algolia_woo_indexer_attributes_variation_output'), |
|
179 | + 'algolia_woo_indexer', |
|
180 | + 'algolia_woo_indexer_attributes' |
|
181 | + ); |
|
182 | + add_settings_field( |
|
183 | + 'algolia_woo_indexer_attributes_list', |
|
184 | + esc_html__('Valid Attributes', 'algolia-woo-indexer'), |
|
185 | + array($algowooindexer, 'algolia_woo_indexer_attributes_list_output'), |
|
186 | + 'algolia_woo_indexer', |
|
187 | + 'algolia_woo_indexer_attributes' |
|
188 | + ); |
|
189 | + add_settings_field( |
|
190 | + 'algolia_woo_indexer_attributes_list_interpolate', |
|
191 | + esc_html__('Numeric Interpolation', 'algolia-woo-indexer'), |
|
192 | + array($algowooindexer, 'algolia_woo_indexer_attributes_list_interpolate_output'), |
|
193 | + 'algolia_woo_indexer', |
|
194 | + 'algolia_woo_indexer_attributes' |
|
195 | + ); |
|
196 | + } |
|
197 | + } |
|
198 | + |
|
199 | + /** |
|
200 | + * Output for admin API key field |
|
201 | + * |
|
202 | + * @see https://developer.wordpress.org/reference/functions/wp_nonce_field/ |
|
203 | + * |
|
204 | + * @return void |
|
205 | + */ |
|
206 | + public static function algolia_woo_indexer_admin_api_key_output() |
|
207 | + { |
|
208 | + $api_key = get_option(ALGOWOO_DB_OPTION . ALGOLIA_API_KEY); |
|
209 | + $api_key = is_string($api_key) ? $api_key : CHANGE_ME; |
|
210 | + |
|
211 | + wp_nonce_field('algolia_woo_indexer_admin_api_nonce_action', 'algolia_woo_indexer_admin_api_nonce_name'); |
|
212 | + |
|
213 | + echo "<input id='algolia_woo_indexer_admin_api_key' name='algolia_woo_indexer_admin_api_key[key]' |
|
214 | 214 | type='text' value='" . esc_attr($api_key) . "' />"; |
215 | - } |
|
216 | - |
|
217 | - /** |
|
218 | - * Output for application ID field |
|
219 | - * |
|
220 | - * @return void |
|
221 | - */ |
|
222 | - public static function algolia_woo_indexer_application_id_output() |
|
223 | - { |
|
224 | - $application_id = get_option(ALGOWOO_DB_OPTION . ALGOLIA_APP_ID); |
|
225 | - $application_id = is_string($application_id) ? $application_id : CHANGE_ME; |
|
226 | - |
|
227 | - echo "<input id='algolia_woo_indexer_application_id' name='algolia_woo_indexer_application_id[id]' |
|
215 | + } |
|
216 | + |
|
217 | + /** |
|
218 | + * Output for application ID field |
|
219 | + * |
|
220 | + * @return void |
|
221 | + */ |
|
222 | + public static function algolia_woo_indexer_application_id_output() |
|
223 | + { |
|
224 | + $application_id = get_option(ALGOWOO_DB_OPTION . ALGOLIA_APP_ID); |
|
225 | + $application_id = is_string($application_id) ? $application_id : CHANGE_ME; |
|
226 | + |
|
227 | + echo "<input id='algolia_woo_indexer_application_id' name='algolia_woo_indexer_application_id[id]' |
|
228 | 228 | type='text' value='" . esc_attr($application_id) . "' />"; |
229 | - } |
|
230 | - |
|
231 | - /** |
|
232 | - * Output for index name field |
|
233 | - * |
|
234 | - * @return void |
|
235 | - */ |
|
236 | - public static function algolia_woo_indexer_index_name_output() |
|
237 | - { |
|
238 | - $index_name = get_option(ALGOWOO_DB_OPTION . INDEX_NAME); |
|
239 | - $index_name = is_string($index_name) ? $index_name : CHANGE_ME; |
|
240 | - |
|
241 | - echo "<input id='algolia_woo_indexer_index_name' name='algolia_woo_indexer_index_name[name]' |
|
229 | + } |
|
230 | + |
|
231 | + /** |
|
232 | + * Output for index name field |
|
233 | + * |
|
234 | + * @return void |
|
235 | + */ |
|
236 | + public static function algolia_woo_indexer_index_name_output() |
|
237 | + { |
|
238 | + $index_name = get_option(ALGOWOO_DB_OPTION . INDEX_NAME); |
|
239 | + $index_name = is_string($index_name) ? $index_name : CHANGE_ME; |
|
240 | + |
|
241 | + echo "<input id='algolia_woo_indexer_index_name' name='algolia_woo_indexer_index_name[name]' |
|
242 | 242 | type='text' value='" . esc_attr($index_name) . "' />"; |
243 | - } |
|
244 | - |
|
245 | - /** |
|
246 | - * Output for checkbox to check if we automatically send new products to Algolia |
|
247 | - * |
|
248 | - * @return void |
|
249 | - */ |
|
250 | - public static function algolia_woo_indexer_automatically_send_new_products_output() |
|
251 | - { |
|
252 | - /** |
|
253 | - * Sanitization is not really needed as the variable is not directly echoed |
|
254 | - * But I have still done it to be 100% safe |
|
255 | - */ |
|
256 | - $auto_send = get_option(ALGOWOO_DB_OPTION . AUTOMATICALLY_SEND_NEW_PRODUCTS); |
|
257 | - $auto_send = (!empty($auto_send)) ? 1 : 0; ?> |
|
243 | + } |
|
244 | + |
|
245 | + /** |
|
246 | + * Output for checkbox to check if we automatically send new products to Algolia |
|
247 | + * |
|
248 | + * @return void |
|
249 | + */ |
|
250 | + public static function algolia_woo_indexer_automatically_send_new_products_output() |
|
251 | + { |
|
252 | + /** |
|
253 | + * Sanitization is not really needed as the variable is not directly echoed |
|
254 | + * But I have still done it to be 100% safe |
|
255 | + */ |
|
256 | + $auto_send = get_option(ALGOWOO_DB_OPTION . AUTOMATICALLY_SEND_NEW_PRODUCTS); |
|
257 | + $auto_send = (!empty($auto_send)) ? 1 : 0; ?> |
|
258 | 258 | <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); ?> /> |
259 | 259 | <?php |
260 | - } |
|
261 | - |
|
262 | - /** |
|
263 | - * Output for fields which data shall be sent to Algolia |
|
264 | - * |
|
265 | - * @return void |
|
266 | - */ |
|
267 | - public static function algolia_woo_indexer_field_output() |
|
268 | - { |
|
269 | - $value = get_option(ALGOWOO_DB_OPTION . BASIC_FIELD_PREFIX . $args["name"]); |
|
270 | - $isChecked = (!empty($value)) ? 1 : 0; |
|
271 | - ?> |
|
260 | + } |
|
261 | + |
|
262 | + /** |
|
263 | + * Output for fields which data shall be sent to Algolia |
|
264 | + * |
|
265 | + * @return void |
|
266 | + */ |
|
267 | + public static function algolia_woo_indexer_field_output() |
|
268 | + { |
|
269 | + $value = get_option(ALGOWOO_DB_OPTION . BASIC_FIELD_PREFIX . $args["name"]); |
|
270 | + $isChecked = (!empty($value)) ? 1 : 0; |
|
271 | + ?> |
|
272 | 272 | <input id="<?php echo $args["label_for"] ?>" name="<?php echo $args["label_for"] ?>[checked]" type="checkbox" <?php checked(1, $isChecked); ?> /> |
273 | 273 | <?php |
274 | - } |
|
275 | - |
|
276 | - /** |
|
277 | - * Output for attributes if functionality is enabled |
|
278 | - * |
|
279 | - * @return void |
|
280 | - */ |
|
281 | - public static function algolia_woo_indexer_attributes_enabled_output() |
|
282 | - { |
|
283 | - $value = get_option(ALGOWOO_DB_OPTION . ATTRIBUTES_ENABLED); |
|
284 | - $isChecked = (!empty($value)) ? 1 : 0; |
|
285 | - ?> |
|
274 | + } |
|
275 | + |
|
276 | + /** |
|
277 | + * Output for attributes if functionality is enabled |
|
278 | + * |
|
279 | + * @return void |
|
280 | + */ |
|
281 | + public static function algolia_woo_indexer_attributes_enabled_output() |
|
282 | + { |
|
283 | + $value = get_option(ALGOWOO_DB_OPTION . ATTRIBUTES_ENABLED); |
|
284 | + $isChecked = (!empty($value)) ? 1 : 0; |
|
285 | + ?> |
|
286 | 286 | <input id="algolia_woo_indexer_attributes_enabled" name="algolia_woo_indexer_attributes_enabled[checked]" type="checkbox" <?php checked(1, $isChecked); ?> /> |
287 | 287 | <?php |
288 | - } |
|
289 | - |
|
290 | - /** |
|
291 | - * Output for attributes how to handle visibility setting |
|
292 | - * |
|
293 | - * @return void |
|
294 | - */ |
|
295 | - public static function algolia_woo_indexer_attributes_visibility_output() |
|
296 | - { |
|
297 | - $value = get_option(ALGOWOO_DB_OPTION . ATTRIBUTES_VISIBILITY); |
|
298 | - foreach (ATTRIBUTES_VISIBILITY_STATES as $state) { |
|
299 | - $id = 'algolia_woo_indexer_attributes_visibility_' . $state; |
|
300 | - ?> |
|
288 | + } |
|
289 | + |
|
290 | + /** |
|
291 | + * Output for attributes how to handle visibility setting |
|
292 | + * |
|
293 | + * @return void |
|
294 | + */ |
|
295 | + public static function algolia_woo_indexer_attributes_visibility_output() |
|
296 | + { |
|
297 | + $value = get_option(ALGOWOO_DB_OPTION . ATTRIBUTES_VISIBILITY); |
|
298 | + foreach (ATTRIBUTES_VISIBILITY_STATES as $state) { |
|
299 | + $id = 'algolia_woo_indexer_attributes_visibility_' . $state; |
|
300 | + ?> |
|
301 | 301 | <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> |
302 | 302 | <?php |
303 | - } |
|
304 | - } |
|
305 | - |
|
306 | - /** |
|
307 | - * Output for attributes how to handle variant setting |
|
308 | - * |
|
309 | - * @return void |
|
310 | - */ |
|
311 | - public static function algolia_woo_indexer_attributes_variation_output() |
|
312 | - { |
|
313 | - $value = get_option(ALGOWOO_DB_OPTION . ATTRIBUTES_VARIATION); |
|
314 | - foreach (ATTRIBUTES_VARIATION_STATES as $state) { |
|
315 | - $id = 'algolia_woo_indexer_attributes_variation_' . $state; |
|
316 | - ?> |
|
303 | + } |
|
304 | + } |
|
305 | + |
|
306 | + /** |
|
307 | + * Output for attributes how to handle variant setting |
|
308 | + * |
|
309 | + * @return void |
|
310 | + */ |
|
311 | + public static function algolia_woo_indexer_attributes_variation_output() |
|
312 | + { |
|
313 | + $value = get_option(ALGOWOO_DB_OPTION . ATTRIBUTES_VARIATION); |
|
314 | + foreach (ATTRIBUTES_VARIATION_STATES as $state) { |
|
315 | + $id = 'algolia_woo_indexer_attributes_variation_' . $state; |
|
316 | + ?> |
|
317 | 317 | <p><input id="<?php echo $id; ?>" name="algolia_woo_indexer_attributes_variation[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> |
318 | 318 | <?php |
319 | - } |
|
320 | - } |
|
321 | - |
|
322 | - /** |
|
323 | - * Output for attributes list which attributes are whitelisted |
|
324 | - * |
|
325 | - * @return void |
|
326 | - */ |
|
327 | - public static function algolia_woo_indexer_attributes_list_output() |
|
328 | - { |
|
329 | - $value = get_option(ALGOWOO_DB_OPTION . ATTRIBUTES_LIST); |
|
330 | - $selectedIds = explode(",", $value); |
|
331 | - $name = "algolia_woo_indexer_attributes_list[list]"; |
|
332 | - $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'); |
|
333 | - self::generic_attributes_select_output($name, $selectedIds, $description); |
|
334 | - } |
|
335 | - |
|
336 | - /** |
|
337 | - * Output for attributes list which are using a numeric interpolation |
|
338 | - * |
|
339 | - * @return void |
|
340 | - */ |
|
341 | - public static function algolia_woo_indexer_attributes_list_interpolate_output() |
|
342 | - { |
|
343 | - $value = get_option(ALGOWOO_DB_OPTION . ATTRIBUTES_LIST_INTERPOLATE); |
|
344 | - $selectedIds = explode(",", $value); |
|
345 | - $name = "algolia_woo_indexer_attributes_list_interpolate[list]"; |
|
346 | - $description = __('If you have some attributes based on number which shall be interpolated 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'); |
|
347 | - self::generic_attributes_select_output($name, $selectedIds, $description); |
|
348 | - } |
|
349 | - |
|
350 | - /** |
|
351 | - * Generic Output for attributes list where attributes are whitelisted |
|
352 | - * |
|
353 | - * @return void |
|
354 | - */ |
|
355 | - public static function generic_attributes_select_output($name, $selectedIds, $description) |
|
356 | - { |
|
357 | - $attribute_taxonomies = wc_get_attribute_taxonomies(); |
|
358 | - |
|
359 | - if (!$attribute_taxonomies) { |
|
360 | - echo esc_html__('You don\'t have any attributes defined yet. Go to WooCommerce and add some to use this feature.', 'algolia-woo-indexer'); |
|
361 | - return; |
|
362 | - } |
|
363 | - ?> |
|
319 | + } |
|
320 | + } |
|
321 | + |
|
322 | + /** |
|
323 | + * Output for attributes list which attributes are whitelisted |
|
324 | + * |
|
325 | + * @return void |
|
326 | + */ |
|
327 | + public static function algolia_woo_indexer_attributes_list_output() |
|
328 | + { |
|
329 | + $value = get_option(ALGOWOO_DB_OPTION . ATTRIBUTES_LIST); |
|
330 | + $selectedIds = explode(",", $value); |
|
331 | + $name = "algolia_woo_indexer_attributes_list[list]"; |
|
332 | + $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'); |
|
333 | + self::generic_attributes_select_output($name, $selectedIds, $description); |
|
334 | + } |
|
335 | + |
|
336 | + /** |
|
337 | + * Output for attributes list which are using a numeric interpolation |
|
338 | + * |
|
339 | + * @return void |
|
340 | + */ |
|
341 | + public static function algolia_woo_indexer_attributes_list_interpolate_output() |
|
342 | + { |
|
343 | + $value = get_option(ALGOWOO_DB_OPTION . ATTRIBUTES_LIST_INTERPOLATE); |
|
344 | + $selectedIds = explode(",", $value); |
|
345 | + $name = "algolia_woo_indexer_attributes_list_interpolate[list]"; |
|
346 | + $description = __('If you have some attributes based on number which shall be interpolated 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'); |
|
347 | + self::generic_attributes_select_output($name, $selectedIds, $description); |
|
348 | + } |
|
349 | + |
|
350 | + /** |
|
351 | + * Generic Output for attributes list where attributes are whitelisted |
|
352 | + * |
|
353 | + * @return void |
|
354 | + */ |
|
355 | + public static function generic_attributes_select_output($name, $selectedIds, $description) |
|
356 | + { |
|
357 | + $attribute_taxonomies = wc_get_attribute_taxonomies(); |
|
358 | + |
|
359 | + if (!$attribute_taxonomies) { |
|
360 | + echo esc_html__('You don\'t have any attributes defined yet. Go to WooCommerce and add some to use this feature.', 'algolia-woo-indexer'); |
|
361 | + return; |
|
362 | + } |
|
363 | + ?> |
|
364 | 364 | <p><?php echo $description; ?></p> |
365 | 365 | <select multiple="multiple" name="<?php echo $name; ?>[]" size="<?php echo count($attribute_taxonomies); ?>"> |
366 | 366 | <?php |
367 | - foreach ($attribute_taxonomies as $tax) { |
|
367 | + foreach ($attribute_taxonomies as $tax) { |
|
368 | 368 | |
369 | - $id = $tax->attribute_id; |
|
370 | - $label = $tax->attribute_label; |
|
371 | - $name = $tax->attribute_name; |
|
372 | - $selected = in_array($id, $selectedIds) ? ' selected="selected" ' : ''; |
|
373 | - ?> |
|
369 | + $id = $tax->attribute_id; |
|
370 | + $label = $tax->attribute_label; |
|
371 | + $name = $tax->attribute_name; |
|
372 | + $selected = in_array($id, $selectedIds) ? ' selected="selected" ' : ''; |
|
373 | + ?> |
|
374 | 374 | <option value="<?php echo $id; ?>" <?php echo $selected; ?>> |
375 | 375 | <?php echo $label . ' (' . $name . ')'; ?> |
376 | 376 | </option> |
377 | 377 | <?php |
378 | - } |
|
379 | - ?> |
|
378 | + } |
|
379 | + ?> |
|
380 | 380 | </select> |
381 | 381 | <?php |
382 | 382 | |
383 | - } |
|
384 | - |
|
385 | - /** |
|
386 | - * Section text for plugin settings section text |
|
387 | - * |
|
388 | - * @return void |
|
389 | - */ |
|
390 | - public static function algolia_woo_indexer_section_text() |
|
391 | - { |
|
392 | - echo esc_html__('Enter your API settings here.', 'algolia-woo-indexer'); |
|
393 | - } |
|
394 | - |
|
395 | - /** |
|
396 | - * Section text for basic fields settings section text |
|
397 | - * |
|
398 | - * @return void |
|
399 | - */ |
|
400 | - public static function algolia_woo_indexer_fields_section_text() |
|
401 | - { |
|
402 | - echo esc_html__('Choose which basic fields shall be indexed.', 'algolia-woo-indexer'); |
|
403 | - } |
|
404 | - |
|
405 | - /** |
|
406 | - * Section text for attributes settings section text |
|
407 | - * |
|
408 | - * @return void |
|
409 | - */ |
|
410 | - public static function algolia_woo_indexer_attributes_section_text() |
|
411 | - { |
|
412 | - echo esc_html__('Control if and how the attributes shall be indexed.', 'algolia-woo-indexer'); |
|
413 | - } |
|
414 | - |
|
415 | - /** |
|
416 | - * Check if we are going to send products by verifying send products nonce |
|
417 | - * |
|
418 | - * @return void |
|
419 | - */ |
|
420 | - public static function maybe_send_products() |
|
421 | - { |
|
422 | - if (true === Algolia_Verify_Nonces::verify_send_products_nonce()) { |
|
423 | - Algolia_Send_Products::send_products_to_algolia(); |
|
424 | - } |
|
425 | - } |
|
426 | - |
|
427 | - /** |
|
428 | - * Initialize class, setup settings sections and fields |
|
429 | - * |
|
430 | - * @return void |
|
431 | - */ |
|
432 | - public static function init() |
|
433 | - { |
|
434 | - /** |
|
435 | - * Fetch the option to see if we are going to automatically send new products |
|
436 | - */ |
|
437 | - $auto_send = get_option(ALGOWOO_DB_OPTION . AUTOMATICALLY_SEND_NEW_PRODUCTS); |
|
438 | - |
|
439 | - /** |
|
440 | - * Check that we have the minimum versions required and all of the required PHP extensions |
|
441 | - */ |
|
442 | - Algolia_Check_Requirements::check_unmet_requirements(); |
|
443 | - |
|
444 | - if (!Algolia_Check_Requirements::algolia_wp_version_check() || !Algolia_Check_Requirements::algolia_php_version_check()) { |
|
445 | - add_action( |
|
446 | - 'admin_notices', |
|
447 | - function () { |
|
448 | - echo '<div class="error notice"> |
|
383 | + } |
|
384 | + |
|
385 | + /** |
|
386 | + * Section text for plugin settings section text |
|
387 | + * |
|
388 | + * @return void |
|
389 | + */ |
|
390 | + public static function algolia_woo_indexer_section_text() |
|
391 | + { |
|
392 | + echo esc_html__('Enter your API settings here.', 'algolia-woo-indexer'); |
|
393 | + } |
|
394 | + |
|
395 | + /** |
|
396 | + * Section text for basic fields settings section text |
|
397 | + * |
|
398 | + * @return void |
|
399 | + */ |
|
400 | + public static function algolia_woo_indexer_fields_section_text() |
|
401 | + { |
|
402 | + echo esc_html__('Choose which basic fields shall be indexed.', 'algolia-woo-indexer'); |
|
403 | + } |
|
404 | + |
|
405 | + /** |
|
406 | + * Section text for attributes settings section text |
|
407 | + * |
|
408 | + * @return void |
|
409 | + */ |
|
410 | + public static function algolia_woo_indexer_attributes_section_text() |
|
411 | + { |
|
412 | + echo esc_html__('Control if and how the attributes shall be indexed.', 'algolia-woo-indexer'); |
|
413 | + } |
|
414 | + |
|
415 | + /** |
|
416 | + * Check if we are going to send products by verifying send products nonce |
|
417 | + * |
|
418 | + * @return void |
|
419 | + */ |
|
420 | + public static function maybe_send_products() |
|
421 | + { |
|
422 | + if (true === Algolia_Verify_Nonces::verify_send_products_nonce()) { |
|
423 | + Algolia_Send_Products::send_products_to_algolia(); |
|
424 | + } |
|
425 | + } |
|
426 | + |
|
427 | + /** |
|
428 | + * Initialize class, setup settings sections and fields |
|
429 | + * |
|
430 | + * @return void |
|
431 | + */ |
|
432 | + public static function init() |
|
433 | + { |
|
434 | + /** |
|
435 | + * Fetch the option to see if we are going to automatically send new products |
|
436 | + */ |
|
437 | + $auto_send = get_option(ALGOWOO_DB_OPTION . AUTOMATICALLY_SEND_NEW_PRODUCTS); |
|
438 | + |
|
439 | + /** |
|
440 | + * Check that we have the minimum versions required and all of the required PHP extensions |
|
441 | + */ |
|
442 | + Algolia_Check_Requirements::check_unmet_requirements(); |
|
443 | + |
|
444 | + if (!Algolia_Check_Requirements::algolia_wp_version_check() || !Algolia_Check_Requirements::algolia_php_version_check()) { |
|
445 | + add_action( |
|
446 | + 'admin_notices', |
|
447 | + function () { |
|
448 | + echo '<div class="error notice"> |
|
449 | 449 | <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> |
450 | 450 | </div>'; |
451 | - } |
|
452 | - ); |
|
453 | - } |
|
454 | - |
|
455 | - $ob_class = get_called_class(); |
|
456 | - |
|
457 | - /** |
|
458 | - * Setup translations |
|
459 | - */ |
|
460 | - add_action('plugins_loaded', array($ob_class, 'load_textdomain')); |
|
461 | - |
|
462 | - /** |
|
463 | - * Add actions to setup admin menu |
|
464 | - */ |
|
465 | - if (is_admin()) { |
|
466 | - add_action('admin_menu', array($ob_class, 'admin_menu')); |
|
467 | - add_action('admin_init', array($ob_class, 'setup_settings_sections')); |
|
468 | - add_action('admin_init', array($ob_class, 'update_settings_options')); |
|
469 | - add_action('admin_init', array($ob_class, 'maybe_send_products')); |
|
470 | - |
|
471 | - /** |
|
472 | - * Register hook to automatically send new products if the option is set |
|
473 | - */ |
|
474 | - |
|
475 | - if ('1' === $auto_send) { |
|
476 | - add_action('save_post', array($ob_class, 'send_new_product_to_algolia'), 10, 3); |
|
477 | - } |
|
478 | - |
|
479 | - self::$plugin_url = admin_url('options-general.php?page=algolia-woo-indexer-settings'); |
|
480 | - |
|
481 | - if (!is_plugin_active('woocommerce/woocommerce.php')) { |
|
482 | - add_action( |
|
483 | - 'admin_notices', |
|
484 | - function () { |
|
485 | - echo '<div class="error notice"> |
|
451 | + } |
|
452 | + ); |
|
453 | + } |
|
454 | + |
|
455 | + $ob_class = get_called_class(); |
|
456 | + |
|
457 | + /** |
|
458 | + * Setup translations |
|
459 | + */ |
|
460 | + add_action('plugins_loaded', array($ob_class, 'load_textdomain')); |
|
461 | + |
|
462 | + /** |
|
463 | + * Add actions to setup admin menu |
|
464 | + */ |
|
465 | + if (is_admin()) { |
|
466 | + add_action('admin_menu', array($ob_class, 'admin_menu')); |
|
467 | + add_action('admin_init', array($ob_class, 'setup_settings_sections')); |
|
468 | + add_action('admin_init', array($ob_class, 'update_settings_options')); |
|
469 | + add_action('admin_init', array($ob_class, 'maybe_send_products')); |
|
470 | + |
|
471 | + /** |
|
472 | + * Register hook to automatically send new products if the option is set |
|
473 | + */ |
|
474 | + |
|
475 | + if ('1' === $auto_send) { |
|
476 | + add_action('save_post', array($ob_class, 'send_new_product_to_algolia'), 10, 3); |
|
477 | + } |
|
478 | + |
|
479 | + self::$plugin_url = admin_url('options-general.php?page=algolia-woo-indexer-settings'); |
|
480 | + |
|
481 | + if (!is_plugin_active('woocommerce/woocommerce.php')) { |
|
482 | + add_action( |
|
483 | + 'admin_notices', |
|
484 | + function () { |
|
485 | + echo '<div class="error notice"> |
|
486 | 486 | <p>' . esc_html__('WooCommerce plugin must be enabled for Algolia Woo Indexer to work.', 'algolia-woo-indexer') . '</p> |
487 | 487 | </div>'; |
488 | - } |
|
489 | - ); |
|
490 | - } |
|
491 | - } |
|
492 | - } |
|
493 | - |
|
494 | - /** |
|
495 | - * Send a single product to Algolia once a new product has been published |
|
496 | - * |
|
497 | - * @param int $post_id ID of the product. |
|
498 | - * @param array $post Post array. |
|
499 | - * |
|
500 | - * @return void |
|
501 | - */ |
|
502 | - public static function send_new_product_to_algolia($post_id, $post) |
|
503 | - { |
|
504 | - if ('publish' !== $post->post_status || 'product' !== $post->post_type) { |
|
505 | - return; |
|
506 | - } |
|
507 | - Algolia_Send_Products::send_products_to_algolia($post_id); |
|
508 | - } |
|
509 | - |
|
510 | - /** |
|
511 | - * Verify nonces before we update options and settings |
|
512 | - * Also retrieve the value from the send_products_to_algolia hidden field to check if we are sending products to Algolia |
|
513 | - * |
|
514 | - * @return void |
|
515 | - */ |
|
516 | - public static function update_settings_options() |
|
517 | - { |
|
518 | - /** |
|
519 | - * Do not proceed if nonce for settings is not set |
|
520 | - */ |
|
521 | - if (false === Algolia_Verify_Nonces::verify_settings_nonce()) { |
|
522 | - return; |
|
523 | - } |
|
524 | - |
|
525 | - /** |
|
526 | - * Do not proceed if we are going to send products |
|
527 | - */ |
|
528 | - if (true === Algolia_Verify_Nonces::verify_send_products_nonce()) { |
|
529 | - return; |
|
530 | - } |
|
531 | - |
|
532 | - /** |
|
533 | - * Filter the application id, api key, index name and verify that the input is an array |
|
534 | - * |
|
535 | - * @see https://www.php.net/manual/en/function.filter-input.php |
|
536 | - */ |
|
537 | - $post_application_id = filter_input(INPUT_POST, 'algolia_woo_indexer_application_id', FILTER_DEFAULT, FILTER_REQUIRE_ARRAY); |
|
538 | - $post_api_key = filter_input(INPUT_POST, 'algolia_woo_indexer_admin_api_key', FILTER_DEFAULT, FILTER_REQUIRE_ARRAY); |
|
539 | - $post_index_name = filter_input(INPUT_POST, 'algolia_woo_indexer_index_name', FILTER_DEFAULT, FILTER_REQUIRE_ARRAY); |
|
540 | - $auto_send = filter_input(INPUT_POST, 'algolia_woo_indexer_automatically_send_new_products', FILTER_DEFAULT, FILTER_REQUIRE_ARRAY); |
|
541 | - $attributes_enabled = filter_input(INPUT_POST, 'algolia_woo_indexer_attributes_enabled', FILTER_DEFAULT, FILTER_REQUIRE_ARRAY); |
|
542 | - $attributes_visibility = filter_input(INPUT_POST, 'algolia_woo_indexer_attributes_visibility', FILTER_DEFAULT, FILTER_REQUIRE_ARRAY); |
|
543 | - $attributes_variation = filter_input(INPUT_POST, 'algolia_woo_indexer_attributes_variation', FILTER_DEFAULT, FILTER_REQUIRE_ARRAY); |
|
544 | - $attributes_list = filter_input(INPUT_POST, 'algolia_woo_indexer_attributes_list', FILTER_DEFAULT, FILTER_REQUIRE_ARRAY); |
|
545 | - $attributes_list_interpolate = filter_input(INPUT_POST, 'algolia_woo_indexer_attributes_list_interpolate', FILTER_DEFAULT, FILTER_REQUIRE_ARRAY); |
|
546 | - |
|
547 | - /** |
|
548 | - * Properly sanitize text fields before updating data |
|
549 | - * |
|
550 | - * @see https://developer.wordpress.org/reference/functions/sanitize_text_field/ |
|
551 | - */ |
|
552 | - $filtered_app_id = sanitize_text_field($post_application_id['id']); |
|
553 | - $filtered_api_key = sanitize_text_field($post_api_key['key']); |
|
554 | - $filtered_index_name = sanitize_text_field($post_index_name['name']); |
|
555 | - $filtered_attributes_visibility = sanitize_text_field($attributes_visibility['value']); |
|
556 | - $filtered_attributes_variation = sanitize_text_field($attributes_variation['value']); |
|
557 | - |
|
558 | - /** |
|
559 | - * sanitize select list of id's by getting integers and them implode seperated with comma |
|
560 | - */ |
|
561 | - |
|
562 | - $attributes_list_integers = []; |
|
563 | - foreach ($attributes_list['list'] as $id) { |
|
564 | - array_push($attributes_list_integers, (int) $id); |
|
565 | - } |
|
566 | - $filtered_attributes_list = implode(',', $attributes_list_integers); |
|
567 | - $attributes_list_interp_int = []; |
|
568 | - foreach ($attributes_list_interpolate['list'] as $id) { |
|
569 | - array_push($attributes_list_interp_int, (int) $id); |
|
570 | - } |
|
571 | - $clean_attributes_list_interp = implode(',', $attributes_list_interp_int); |
|
572 | - |
|
573 | - /** |
|
574 | - * Sanitizing by setting the value to either 1 or 0 |
|
575 | - */ |
|
576 | - $filtered_product = (!empty($auto_send)) ? 1 : 0; |
|
577 | - $filtered_attributes_enabled = (!empty($attributes_enabled)) ? 1 : 0; |
|
578 | - |
|
579 | - /** |
|
580 | - * Filter the data fields checkboxes and verify that the input is an array and assign it to an associative array |
|
581 | - * |
|
582 | - * @see https://www.php.net/manual/en/function.filter-input.php |
|
583 | - */ |
|
584 | - $filtered_fields = array(); |
|
585 | - foreach (BASIC_FIELDS as $field) { |
|
586 | - $raw_field = filter_input(INPUT_POST, 'algolia_woo_indexer_field_' . $field, FILTER_DEFAULT, FILTER_REQUIRE_ARRAY); |
|
587 | - $filtered_field = (!empty($raw_field)) ? 1 : 0; |
|
588 | - $filtered_fields[$field] = $filtered_field; |
|
589 | - } |
|
590 | - |
|
591 | - /** |
|
592 | - * Values have been filtered and sanitized |
|
593 | - * Check if set and not empty and update the database |
|
594 | - * |
|
595 | - * @see https://developer.wordpress.org/reference/functions/update_option/ |
|
596 | - */ |
|
597 | - if (isset($filtered_app_id) && (!empty($filtered_app_id))) { |
|
598 | - update_option( |
|
599 | - ALGOWOO_DB_OPTION . ALGOLIA_APP_ID, |
|
600 | - $filtered_app_id |
|
601 | - ); |
|
602 | - } |
|
603 | - |
|
604 | - if (isset($filtered_api_key) && (!empty($filtered_api_key))) { |
|
605 | - update_option( |
|
606 | - ALGOWOO_DB_OPTION . ALGOLIA_API_KEY, |
|
607 | - $filtered_api_key |
|
608 | - ); |
|
609 | - } |
|
610 | - |
|
611 | - if (isset($filtered_index_name) && (!empty($filtered_index_name))) { |
|
612 | - update_option( |
|
613 | - ALGOWOO_DB_OPTION . INDEX_NAME, |
|
614 | - $filtered_index_name |
|
615 | - ); |
|
616 | - } |
|
617 | - |
|
618 | - if (isset($filtered_product)) { |
|
619 | - update_option( |
|
620 | - ALGOWOO_DB_OPTION . AUTOMATICALLY_SEND_NEW_PRODUCTS, |
|
621 | - $filtered_product |
|
622 | - ); |
|
623 | - } |
|
624 | - |
|
625 | - if (isset($filtered_attributes_enabled)) { |
|
626 | - update_option( |
|
627 | - ALGOWOO_DB_OPTION . ATTRIBUTES_ENABLED, |
|
628 | - $filtered_attributes_enabled |
|
629 | - ); |
|
630 | - } |
|
631 | - |
|
632 | - if (isset($filtered_attributes_visibility) && (!empty($filtered_attributes_visibility))) { |
|
633 | - update_option( |
|
634 | - ALGOWOO_DB_OPTION . ATTRIBUTES_VISIBILITY, |
|
635 | - $filtered_attributes_visibility |
|
636 | - ); |
|
637 | - } |
|
638 | - |
|
639 | - if (isset($filtered_attributes_variation) && (!empty($filtered_attributes_variation))) { |
|
640 | - update_option( |
|
641 | - ALGOWOO_DB_OPTION . ATTRIBUTES_VARIATION, |
|
642 | - $filtered_attributes_variation |
|
643 | - ); |
|
644 | - } |
|
645 | - |
|
646 | - if (isset($filtered_attributes_list) && (!empty($filtered_attributes_list))) { |
|
647 | - update_option( |
|
648 | - ALGOWOO_DB_OPTION . ATTRIBUTES_LIST, |
|
649 | - $filtered_attributes_list |
|
650 | - ); |
|
651 | - } |
|
652 | - |
|
653 | - if (isset($clean_attributes_list_interp) && (!empty($clean_attributes_list_interp))) { |
|
654 | - update_option( |
|
655 | - ALGOWOO_DB_OPTION . ATTRIBUTES_LIST_INTERPOLATE, |
|
656 | - $clean_attributes_list_interp |
|
657 | - ); |
|
658 | - } |
|
659 | - |
|
660 | - if (isset($filtered_fields) && (!empty($filtered_fields))) { |
|
661 | - foreach ($filtered_fields as $key => $value) { |
|
662 | - update_option( |
|
663 | - ALGOWOO_DB_OPTION . BASIC_FIELD_PREFIX . $key, |
|
664 | - $value |
|
665 | - ); |
|
666 | - } |
|
667 | - } |
|
668 | - } |
|
669 | - |
|
670 | - /** |
|
671 | - * Sanitize input in settings fields and filter through regex to accept only a-z and A-Z |
|
672 | - * |
|
673 | - * @param string $input Settings text data |
|
674 | - * @return array |
|
675 | - */ |
|
676 | - public static function settings_fields_validate_options($input) |
|
677 | - { |
|
678 | - $valid = array(); |
|
679 | - $valid['name'] = preg_replace( |
|
680 | - '/[^a-zA-Z\s]/', |
|
681 | - '', |
|
682 | - $input['name'] |
|
683 | - ); |
|
684 | - return $valid; |
|
685 | - } |
|
686 | - |
|
687 | - /** |
|
688 | - * Load text domain for translations |
|
689 | - * |
|
690 | - * @return void |
|
691 | - */ |
|
692 | - public static function load_textdomain() |
|
693 | - { |
|
694 | - load_plugin_textdomain('algolia-woo-indexer', false, basename(dirname(__FILE__)) . '/languages/'); |
|
695 | - } |
|
696 | - |
|
697 | - /** |
|
698 | - * Add the new menu to settings section so that we can configure the plugin |
|
699 | - * |
|
700 | - * @return void |
|
701 | - */ |
|
702 | - public static function admin_menu() |
|
703 | - { |
|
704 | - add_submenu_page( |
|
705 | - 'options-general.php', |
|
706 | - esc_html__('Algolia Woo Indexer Settings', 'algolia-woo-indexer'), |
|
707 | - esc_html__('Algolia Woo Indexer Settings', 'algolia-woo-indexer'), |
|
708 | - 'manage_options', |
|
709 | - 'algolia-woo-indexer-settings', |
|
710 | - array(get_called_class(), 'algolia_woo_indexer_settings') |
|
711 | - ); |
|
712 | - } |
|
713 | - |
|
714 | - /** |
|
715 | - * Display settings and allow user to modify them |
|
716 | - * |
|
717 | - * @return void |
|
718 | - */ |
|
719 | - public static function algolia_woo_indexer_settings() |
|
720 | - { |
|
721 | - /** |
|
722 | - * Verify that the user can access the settings page |
|
723 | - */ |
|
724 | - if (!current_user_can('manage_options')) { |
|
725 | - wp_die(esc_html__('Action not allowed.', 'algolia_woo_indexer_settings')); |
|
726 | - } ?> |
|
488 | + } |
|
489 | + ); |
|
490 | + } |
|
491 | + } |
|
492 | + } |
|
493 | + |
|
494 | + /** |
|
495 | + * Send a single product to Algolia once a new product has been published |
|
496 | + * |
|
497 | + * @param int $post_id ID of the product. |
|
498 | + * @param array $post Post array. |
|
499 | + * |
|
500 | + * @return void |
|
501 | + */ |
|
502 | + public static function send_new_product_to_algolia($post_id, $post) |
|
503 | + { |
|
504 | + if ('publish' !== $post->post_status || 'product' !== $post->post_type) { |
|
505 | + return; |
|
506 | + } |
|
507 | + Algolia_Send_Products::send_products_to_algolia($post_id); |
|
508 | + } |
|
509 | + |
|
510 | + /** |
|
511 | + * Verify nonces before we update options and settings |
|
512 | + * Also retrieve the value from the send_products_to_algolia hidden field to check if we are sending products to Algolia |
|
513 | + * |
|
514 | + * @return void |
|
515 | + */ |
|
516 | + public static function update_settings_options() |
|
517 | + { |
|
518 | + /** |
|
519 | + * Do not proceed if nonce for settings is not set |
|
520 | + */ |
|
521 | + if (false === Algolia_Verify_Nonces::verify_settings_nonce()) { |
|
522 | + return; |
|
523 | + } |
|
524 | + |
|
525 | + /** |
|
526 | + * Do not proceed if we are going to send products |
|
527 | + */ |
|
528 | + if (true === Algolia_Verify_Nonces::verify_send_products_nonce()) { |
|
529 | + return; |
|
530 | + } |
|
531 | + |
|
532 | + /** |
|
533 | + * Filter the application id, api key, index name and verify that the input is an array |
|
534 | + * |
|
535 | + * @see https://www.php.net/manual/en/function.filter-input.php |
|
536 | + */ |
|
537 | + $post_application_id = filter_input(INPUT_POST, 'algolia_woo_indexer_application_id', FILTER_DEFAULT, FILTER_REQUIRE_ARRAY); |
|
538 | + $post_api_key = filter_input(INPUT_POST, 'algolia_woo_indexer_admin_api_key', FILTER_DEFAULT, FILTER_REQUIRE_ARRAY); |
|
539 | + $post_index_name = filter_input(INPUT_POST, 'algolia_woo_indexer_index_name', FILTER_DEFAULT, FILTER_REQUIRE_ARRAY); |
|
540 | + $auto_send = filter_input(INPUT_POST, 'algolia_woo_indexer_automatically_send_new_products', FILTER_DEFAULT, FILTER_REQUIRE_ARRAY); |
|
541 | + $attributes_enabled = filter_input(INPUT_POST, 'algolia_woo_indexer_attributes_enabled', FILTER_DEFAULT, FILTER_REQUIRE_ARRAY); |
|
542 | + $attributes_visibility = filter_input(INPUT_POST, 'algolia_woo_indexer_attributes_visibility', FILTER_DEFAULT, FILTER_REQUIRE_ARRAY); |
|
543 | + $attributes_variation = filter_input(INPUT_POST, 'algolia_woo_indexer_attributes_variation', FILTER_DEFAULT, FILTER_REQUIRE_ARRAY); |
|
544 | + $attributes_list = filter_input(INPUT_POST, 'algolia_woo_indexer_attributes_list', FILTER_DEFAULT, FILTER_REQUIRE_ARRAY); |
|
545 | + $attributes_list_interpolate = filter_input(INPUT_POST, 'algolia_woo_indexer_attributes_list_interpolate', FILTER_DEFAULT, FILTER_REQUIRE_ARRAY); |
|
546 | + |
|
547 | + /** |
|
548 | + * Properly sanitize text fields before updating data |
|
549 | + * |
|
550 | + * @see https://developer.wordpress.org/reference/functions/sanitize_text_field/ |
|
551 | + */ |
|
552 | + $filtered_app_id = sanitize_text_field($post_application_id['id']); |
|
553 | + $filtered_api_key = sanitize_text_field($post_api_key['key']); |
|
554 | + $filtered_index_name = sanitize_text_field($post_index_name['name']); |
|
555 | + $filtered_attributes_visibility = sanitize_text_field($attributes_visibility['value']); |
|
556 | + $filtered_attributes_variation = sanitize_text_field($attributes_variation['value']); |
|
557 | + |
|
558 | + /** |
|
559 | + * sanitize select list of id's by getting integers and them implode seperated with comma |
|
560 | + */ |
|
561 | + |
|
562 | + $attributes_list_integers = []; |
|
563 | + foreach ($attributes_list['list'] as $id) { |
|
564 | + array_push($attributes_list_integers, (int) $id); |
|
565 | + } |
|
566 | + $filtered_attributes_list = implode(',', $attributes_list_integers); |
|
567 | + $attributes_list_interp_int = []; |
|
568 | + foreach ($attributes_list_interpolate['list'] as $id) { |
|
569 | + array_push($attributes_list_interp_int, (int) $id); |
|
570 | + } |
|
571 | + $clean_attributes_list_interp = implode(',', $attributes_list_interp_int); |
|
572 | + |
|
573 | + /** |
|
574 | + * Sanitizing by setting the value to either 1 or 0 |
|
575 | + */ |
|
576 | + $filtered_product = (!empty($auto_send)) ? 1 : 0; |
|
577 | + $filtered_attributes_enabled = (!empty($attributes_enabled)) ? 1 : 0; |
|
578 | + |
|
579 | + /** |
|
580 | + * Filter the data fields checkboxes and verify that the input is an array and assign it to an associative array |
|
581 | + * |
|
582 | + * @see https://www.php.net/manual/en/function.filter-input.php |
|
583 | + */ |
|
584 | + $filtered_fields = array(); |
|
585 | + foreach (BASIC_FIELDS as $field) { |
|
586 | + $raw_field = filter_input(INPUT_POST, 'algolia_woo_indexer_field_' . $field, FILTER_DEFAULT, FILTER_REQUIRE_ARRAY); |
|
587 | + $filtered_field = (!empty($raw_field)) ? 1 : 0; |
|
588 | + $filtered_fields[$field] = $filtered_field; |
|
589 | + } |
|
590 | + |
|
591 | + /** |
|
592 | + * Values have been filtered and sanitized |
|
593 | + * Check if set and not empty and update the database |
|
594 | + * |
|
595 | + * @see https://developer.wordpress.org/reference/functions/update_option/ |
|
596 | + */ |
|
597 | + if (isset($filtered_app_id) && (!empty($filtered_app_id))) { |
|
598 | + update_option( |
|
599 | + ALGOWOO_DB_OPTION . ALGOLIA_APP_ID, |
|
600 | + $filtered_app_id |
|
601 | + ); |
|
602 | + } |
|
603 | + |
|
604 | + if (isset($filtered_api_key) && (!empty($filtered_api_key))) { |
|
605 | + update_option( |
|
606 | + ALGOWOO_DB_OPTION . ALGOLIA_API_KEY, |
|
607 | + $filtered_api_key |
|
608 | + ); |
|
609 | + } |
|
610 | + |
|
611 | + if (isset($filtered_index_name) && (!empty($filtered_index_name))) { |
|
612 | + update_option( |
|
613 | + ALGOWOO_DB_OPTION . INDEX_NAME, |
|
614 | + $filtered_index_name |
|
615 | + ); |
|
616 | + } |
|
617 | + |
|
618 | + if (isset($filtered_product)) { |
|
619 | + update_option( |
|
620 | + ALGOWOO_DB_OPTION . AUTOMATICALLY_SEND_NEW_PRODUCTS, |
|
621 | + $filtered_product |
|
622 | + ); |
|
623 | + } |
|
624 | + |
|
625 | + if (isset($filtered_attributes_enabled)) { |
|
626 | + update_option( |
|
627 | + ALGOWOO_DB_OPTION . ATTRIBUTES_ENABLED, |
|
628 | + $filtered_attributes_enabled |
|
629 | + ); |
|
630 | + } |
|
631 | + |
|
632 | + if (isset($filtered_attributes_visibility) && (!empty($filtered_attributes_visibility))) { |
|
633 | + update_option( |
|
634 | + ALGOWOO_DB_OPTION . ATTRIBUTES_VISIBILITY, |
|
635 | + $filtered_attributes_visibility |
|
636 | + ); |
|
637 | + } |
|
638 | + |
|
639 | + if (isset($filtered_attributes_variation) && (!empty($filtered_attributes_variation))) { |
|
640 | + update_option( |
|
641 | + ALGOWOO_DB_OPTION . ATTRIBUTES_VARIATION, |
|
642 | + $filtered_attributes_variation |
|
643 | + ); |
|
644 | + } |
|
645 | + |
|
646 | + if (isset($filtered_attributes_list) && (!empty($filtered_attributes_list))) { |
|
647 | + update_option( |
|
648 | + ALGOWOO_DB_OPTION . ATTRIBUTES_LIST, |
|
649 | + $filtered_attributes_list |
|
650 | + ); |
|
651 | + } |
|
652 | + |
|
653 | + if (isset($clean_attributes_list_interp) && (!empty($clean_attributes_list_interp))) { |
|
654 | + update_option( |
|
655 | + ALGOWOO_DB_OPTION . ATTRIBUTES_LIST_INTERPOLATE, |
|
656 | + $clean_attributes_list_interp |
|
657 | + ); |
|
658 | + } |
|
659 | + |
|
660 | + if (isset($filtered_fields) && (!empty($filtered_fields))) { |
|
661 | + foreach ($filtered_fields as $key => $value) { |
|
662 | + update_option( |
|
663 | + ALGOWOO_DB_OPTION . BASIC_FIELD_PREFIX . $key, |
|
664 | + $value |
|
665 | + ); |
|
666 | + } |
|
667 | + } |
|
668 | + } |
|
669 | + |
|
670 | + /** |
|
671 | + * Sanitize input in settings fields and filter through regex to accept only a-z and A-Z |
|
672 | + * |
|
673 | + * @param string $input Settings text data |
|
674 | + * @return array |
|
675 | + */ |
|
676 | + public static function settings_fields_validate_options($input) |
|
677 | + { |
|
678 | + $valid = array(); |
|
679 | + $valid['name'] = preg_replace( |
|
680 | + '/[^a-zA-Z\s]/', |
|
681 | + '', |
|
682 | + $input['name'] |
|
683 | + ); |
|
684 | + return $valid; |
|
685 | + } |
|
686 | + |
|
687 | + /** |
|
688 | + * Load text domain for translations |
|
689 | + * |
|
690 | + * @return void |
|
691 | + */ |
|
692 | + public static function load_textdomain() |
|
693 | + { |
|
694 | + load_plugin_textdomain('algolia-woo-indexer', false, basename(dirname(__FILE__)) . '/languages/'); |
|
695 | + } |
|
696 | + |
|
697 | + /** |
|
698 | + * Add the new menu to settings section so that we can configure the plugin |
|
699 | + * |
|
700 | + * @return void |
|
701 | + */ |
|
702 | + public static function admin_menu() |
|
703 | + { |
|
704 | + add_submenu_page( |
|
705 | + 'options-general.php', |
|
706 | + esc_html__('Algolia Woo Indexer Settings', 'algolia-woo-indexer'), |
|
707 | + esc_html__('Algolia Woo Indexer Settings', 'algolia-woo-indexer'), |
|
708 | + 'manage_options', |
|
709 | + 'algolia-woo-indexer-settings', |
|
710 | + array(get_called_class(), 'algolia_woo_indexer_settings') |
|
711 | + ); |
|
712 | + } |
|
713 | + |
|
714 | + /** |
|
715 | + * Display settings and allow user to modify them |
|
716 | + * |
|
717 | + * @return void |
|
718 | + */ |
|
719 | + public static function algolia_woo_indexer_settings() |
|
720 | + { |
|
721 | + /** |
|
722 | + * Verify that the user can access the settings page |
|
723 | + */ |
|
724 | + if (!current_user_can('manage_options')) { |
|
725 | + wp_die(esc_html__('Action not allowed.', 'algolia_woo_indexer_settings')); |
|
726 | + } ?> |
|
727 | 727 | <div class="wrap"> |
728 | 728 | <h1><?php esc_html__('Algolia Woo Indexer Settings', 'algolia-woo-indexer'); ?></h1> |
729 | 729 | <form action="<?php echo esc_url(self::$plugin_url); ?>" method="POST"> |
730 | 730 | <?php |
731 | - settings_fields('algolia_woo_options'); |
|
732 | - do_settings_sections('algolia_woo_indexer'); |
|
733 | - submit_button('', 'primary wide'); ?> |
|
731 | + settings_fields('algolia_woo_options'); |
|
732 | + do_settings_sections('algolia_woo_indexer'); |
|
733 | + submit_button('', 'primary wide'); ?> |
|
734 | 734 | </form> |
735 | 735 | <form action="<?php echo esc_url(self::$plugin_url); ?>" method="POST"> |
736 | 736 | <?php wp_nonce_field('send_products_to_algolia_nonce_action', 'send_products_to_algolia_nonce_name'); ?> |
@@ -739,128 +739,128 @@ discard block |
||
739 | 739 | </form> |
740 | 740 | </div> |
741 | 741 | <?php |
742 | - } |
|
743 | - |
|
744 | - /** |
|
745 | - * Get active object instance |
|
746 | - * |
|
747 | - * @return object |
|
748 | - */ |
|
749 | - public static function get_instance() |
|
750 | - { |
|
751 | - if (!self::$instance) { |
|
752 | - self::$instance = new Algolia_Woo_Indexer(); |
|
753 | - } |
|
754 | - return self::$instance; |
|
755 | - } |
|
756 | - |
|
757 | - /** |
|
758 | - * The actions to execute when the plugin is activated. |
|
759 | - * |
|
760 | - * @return void |
|
761 | - */ |
|
762 | - public static function activate_plugin() |
|
763 | - { |
|
764 | - |
|
765 | - /** |
|
766 | - * Set default values for options if not already set |
|
767 | - */ |
|
768 | - $auto_send = get_option(ALGOWOO_DB_OPTION . AUTOMATICALLY_SEND_NEW_PRODUCTS); |
|
769 | - $algolia_application_id = get_option(ALGOWOO_DB_OPTION . ALGOLIA_APP_ID); |
|
770 | - $algolia_api_key = get_option(ALGOWOO_DB_OPTION . ALGOLIA_API_KEY); |
|
771 | - $algolia_index_name = get_option(ALGOWOO_DB_OPTION . INDEX_NAME); |
|
772 | - $attributes_enabled = get_option(ALGOWOO_DB_OPTION . ATTRIBUTES_ENABLED); |
|
773 | - $attributes_visibility = get_option(ALGOWOO_DB_OPTION . ATTRIBUTES_VISIBILITY); |
|
774 | - $attributes_variation = get_option(ALGOWOO_DB_OPTION . ATTRIBUTES_VARIATION); |
|
775 | - $attributes_list = get_option(ALGOWOO_DB_OPTION . ATTRIBUTES_LIST); |
|
776 | - $attributes_list_interpolate = get_option(ALGOWOO_DB_OPTION . ATTRIBUTES_LIST_INTERPOLATE); |
|
777 | - |
|
778 | - if (empty($auto_send)) { |
|
779 | - add_option( |
|
780 | - ALGOWOO_DB_OPTION . AUTOMATICALLY_SEND_NEW_PRODUCTS, |
|
781 | - '0' |
|
782 | - ); |
|
783 | - } |
|
784 | - |
|
785 | - if (empty($algolia_application_id)) { |
|
786 | - add_option( |
|
787 | - ALGOWOO_DB_OPTION . ALGOLIA_APP_ID, |
|
788 | - 'Change me' |
|
789 | - ); |
|
790 | - } |
|
791 | - |
|
792 | - if (empty($algolia_api_key)) { |
|
793 | - add_option( |
|
794 | - ALGOWOO_DB_OPTION . ALGOLIA_API_KEY, |
|
795 | - 'Change me' |
|
796 | - ); |
|
797 | - } |
|
798 | - |
|
799 | - if (empty($algolia_index_name)) { |
|
800 | - add_option( |
|
801 | - ALGOWOO_DB_OPTION . INDEX_NAME, |
|
802 | - 'Change me' |
|
803 | - ); |
|
804 | - } |
|
805 | - |
|
806 | - if (empty($attributes_enabled)) { |
|
807 | - add_option( |
|
808 | - ALGOWOO_DB_OPTION . ATTRIBUTES_ENABLED, |
|
809 | - 0 |
|
810 | - ); |
|
811 | - } |
|
812 | - |
|
813 | - if (empty($attributes_visibility)) { |
|
814 | - add_option( |
|
815 | - ALGOWOO_DB_OPTION . ATTRIBUTES_VISIBILITY, |
|
816 | - ATTRIBUTES_VISIBILITY_STATES[1] |
|
817 | - ); |
|
818 | - } |
|
819 | - if (empty($attributes_variation)) { |
|
820 | - add_option( |
|
821 | - ALGOWOO_DB_OPTION . ATTRIBUTES_VARIATION, |
|
822 | - ATTRIBUTES_VARIATION_STATES[0] |
|
823 | - ); |
|
824 | - } |
|
825 | - if (empty($attributes_list)) { |
|
826 | - add_option( |
|
827 | - ALGOWOO_DB_OPTION . ATTRIBUTES_LIST, |
|
828 | - '' |
|
829 | - ); |
|
830 | - } |
|
831 | - if (empty($attributes_list_interpolate)) { |
|
832 | - add_option( |
|
833 | - ALGOWOO_DB_OPTION . ATTRIBUTES_LIST_INTERPOLATE, |
|
834 | - '' |
|
835 | - ); |
|
836 | - } |
|
837 | - |
|
838 | - /** |
|
839 | - * Set default values for index fields if not already set |
|
840 | - */ |
|
841 | - foreach (BASIC_FIELDS as $field) { |
|
842 | - $value = get_option(ALGOWOO_DB_OPTION . BASIC_FIELD_PREFIX . $field); |
|
843 | - if (empty($value)) { |
|
844 | - update_option( |
|
845 | - ALGOWOO_DB_OPTION . BASIC_FIELD_PREFIX . $field, |
|
846 | - '1' |
|
847 | - ); |
|
848 | - } |
|
849 | - } |
|
850 | - |
|
851 | - |
|
852 | - |
|
853 | - set_transient(self::PLUGIN_TRANSIENT, true); |
|
854 | - } |
|
855 | - |
|
856 | - /** |
|
857 | - * The actions to execute when the plugin is deactivated. |
|
858 | - * |
|
859 | - * @return void |
|
860 | - */ |
|
861 | - public static function deactivate_plugin() |
|
862 | - { |
|
863 | - delete_transient(self::PLUGIN_TRANSIENT); |
|
864 | - } |
|
865 | - } |
|
742 | + } |
|
743 | + |
|
744 | + /** |
|
745 | + * Get active object instance |
|
746 | + * |
|
747 | + * @return object |
|
748 | + */ |
|
749 | + public static function get_instance() |
|
750 | + { |
|
751 | + if (!self::$instance) { |
|
752 | + self::$instance = new Algolia_Woo_Indexer(); |
|
753 | + } |
|
754 | + return self::$instance; |
|
755 | + } |
|
756 | + |
|
757 | + /** |
|
758 | + * The actions to execute when the plugin is activated. |
|
759 | + * |
|
760 | + * @return void |
|
761 | + */ |
|
762 | + public static function activate_plugin() |
|
763 | + { |
|
764 | + |
|
765 | + /** |
|
766 | + * Set default values for options if not already set |
|
767 | + */ |
|
768 | + $auto_send = get_option(ALGOWOO_DB_OPTION . AUTOMATICALLY_SEND_NEW_PRODUCTS); |
|
769 | + $algolia_application_id = get_option(ALGOWOO_DB_OPTION . ALGOLIA_APP_ID); |
|
770 | + $algolia_api_key = get_option(ALGOWOO_DB_OPTION . ALGOLIA_API_KEY); |
|
771 | + $algolia_index_name = get_option(ALGOWOO_DB_OPTION . INDEX_NAME); |
|
772 | + $attributes_enabled = get_option(ALGOWOO_DB_OPTION . ATTRIBUTES_ENABLED); |
|
773 | + $attributes_visibility = get_option(ALGOWOO_DB_OPTION . ATTRIBUTES_VISIBILITY); |
|
774 | + $attributes_variation = get_option(ALGOWOO_DB_OPTION . ATTRIBUTES_VARIATION); |
|
775 | + $attributes_list = get_option(ALGOWOO_DB_OPTION . ATTRIBUTES_LIST); |
|
776 | + $attributes_list_interpolate = get_option(ALGOWOO_DB_OPTION . ATTRIBUTES_LIST_INTERPOLATE); |
|
777 | + |
|
778 | + if (empty($auto_send)) { |
|
779 | + add_option( |
|
780 | + ALGOWOO_DB_OPTION . AUTOMATICALLY_SEND_NEW_PRODUCTS, |
|
781 | + '0' |
|
782 | + ); |
|
783 | + } |
|
784 | + |
|
785 | + if (empty($algolia_application_id)) { |
|
786 | + add_option( |
|
787 | + ALGOWOO_DB_OPTION . ALGOLIA_APP_ID, |
|
788 | + 'Change me' |
|
789 | + ); |
|
790 | + } |
|
791 | + |
|
792 | + if (empty($algolia_api_key)) { |
|
793 | + add_option( |
|
794 | + ALGOWOO_DB_OPTION . ALGOLIA_API_KEY, |
|
795 | + 'Change me' |
|
796 | + ); |
|
797 | + } |
|
798 | + |
|
799 | + if (empty($algolia_index_name)) { |
|
800 | + add_option( |
|
801 | + ALGOWOO_DB_OPTION . INDEX_NAME, |
|
802 | + 'Change me' |
|
803 | + ); |
|
804 | + } |
|
805 | + |
|
806 | + if (empty($attributes_enabled)) { |
|
807 | + add_option( |
|
808 | + ALGOWOO_DB_OPTION . ATTRIBUTES_ENABLED, |
|
809 | + 0 |
|
810 | + ); |
|
811 | + } |
|
812 | + |
|
813 | + if (empty($attributes_visibility)) { |
|
814 | + add_option( |
|
815 | + ALGOWOO_DB_OPTION . ATTRIBUTES_VISIBILITY, |
|
816 | + ATTRIBUTES_VISIBILITY_STATES[1] |
|
817 | + ); |
|
818 | + } |
|
819 | + if (empty($attributes_variation)) { |
|
820 | + add_option( |
|
821 | + ALGOWOO_DB_OPTION . ATTRIBUTES_VARIATION, |
|
822 | + ATTRIBUTES_VARIATION_STATES[0] |
|
823 | + ); |
|
824 | + } |
|
825 | + if (empty($attributes_list)) { |
|
826 | + add_option( |
|
827 | + ALGOWOO_DB_OPTION . ATTRIBUTES_LIST, |
|
828 | + '' |
|
829 | + ); |
|
830 | + } |
|
831 | + if (empty($attributes_list_interpolate)) { |
|
832 | + add_option( |
|
833 | + ALGOWOO_DB_OPTION . ATTRIBUTES_LIST_INTERPOLATE, |
|
834 | + '' |
|
835 | + ); |
|
836 | + } |
|
837 | + |
|
838 | + /** |
|
839 | + * Set default values for index fields if not already set |
|
840 | + */ |
|
841 | + foreach (BASIC_FIELDS as $field) { |
|
842 | + $value = get_option(ALGOWOO_DB_OPTION . BASIC_FIELD_PREFIX . $field); |
|
843 | + if (empty($value)) { |
|
844 | + update_option( |
|
845 | + ALGOWOO_DB_OPTION . BASIC_FIELD_PREFIX . $field, |
|
846 | + '1' |
|
847 | + ); |
|
848 | + } |
|
849 | + } |
|
850 | + |
|
851 | + |
|
852 | + |
|
853 | + set_transient(self::PLUGIN_TRANSIENT, true); |
|
854 | + } |
|
855 | + |
|
856 | + /** |
|
857 | + * The actions to execute when the plugin is deactivated. |
|
858 | + * |
|
859 | + * @return void |
|
860 | + */ |
|
861 | + public static function deactivate_plugin() |
|
862 | + { |
|
863 | + delete_transient(self::PLUGIN_TRANSIENT); |
|
864 | + } |
|
865 | + } |
|
866 | 866 | } |
@@ -16,18 +16,18 @@ discard block |
||
16 | 16 | /** |
17 | 17 | * Abort if this file is called directly |
18 | 18 | */ |
19 | -if (!defined('ABSPATH')) { |
|
19 | +if ( ! defined('ABSPATH')) { |
|
20 | 20 | exit; |
21 | 21 | } |
22 | 22 | |
23 | 23 | /** |
24 | 24 | * Include plugin file if function is_plugin_active does not exist |
25 | 25 | */ |
26 | -if (!function_exists('is_plugin_active')) { |
|
26 | +if ( ! function_exists('is_plugin_active')) { |
|
27 | 27 | require_once(ABSPATH . '/wp-admin/includes/plugin.php'); |
28 | 28 | } |
29 | 29 | |
30 | -if (!class_exists('Algolia_Woo_Indexer')) { |
|
30 | +if ( ! class_exists('Algolia_Woo_Indexer')) { |
|
31 | 31 | /** |
32 | 32 | * Algolia WooIndexer main class |
33 | 33 | */ |
@@ -254,7 +254,7 @@ discard block |
||
254 | 254 | * But I have still done it to be 100% safe |
255 | 255 | */ |
256 | 256 | $auto_send = get_option(ALGOWOO_DB_OPTION . AUTOMATICALLY_SEND_NEW_PRODUCTS); |
257 | - $auto_send = (!empty($auto_send)) ? 1 : 0; ?> |
|
257 | + $auto_send = ( ! empty($auto_send)) ? 1 : 0; ?> |
|
258 | 258 | <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); ?> /> |
259 | 259 | <?php |
260 | 260 | } |
@@ -267,7 +267,7 @@ discard block |
||
267 | 267 | public static function algolia_woo_indexer_field_output() |
268 | 268 | { |
269 | 269 | $value = get_option(ALGOWOO_DB_OPTION . BASIC_FIELD_PREFIX . $args["name"]); |
270 | - $isChecked = (!empty($value)) ? 1 : 0; |
|
270 | + $isChecked = ( ! empty($value)) ? 1 : 0; |
|
271 | 271 | ?> |
272 | 272 | <input id="<?php echo $args["label_for"] ?>" name="<?php echo $args["label_for"] ?>[checked]" type="checkbox" <?php checked(1, $isChecked); ?> /> |
273 | 273 | <?php |
@@ -281,7 +281,7 @@ discard block |
||
281 | 281 | public static function algolia_woo_indexer_attributes_enabled_output() |
282 | 282 | { |
283 | 283 | $value = get_option(ALGOWOO_DB_OPTION . ATTRIBUTES_ENABLED); |
284 | - $isChecked = (!empty($value)) ? 1 : 0; |
|
284 | + $isChecked = ( ! empty($value)) ? 1 : 0; |
|
285 | 285 | ?> |
286 | 286 | <input id="algolia_woo_indexer_attributes_enabled" name="algolia_woo_indexer_attributes_enabled[checked]" type="checkbox" <?php checked(1, $isChecked); ?> /> |
287 | 287 | <?php |
@@ -356,7 +356,7 @@ discard block |
||
356 | 356 | { |
357 | 357 | $attribute_taxonomies = wc_get_attribute_taxonomies(); |
358 | 358 | |
359 | - if (!$attribute_taxonomies) { |
|
359 | + if ( ! $attribute_taxonomies) { |
|
360 | 360 | echo esc_html__('You don\'t have any attributes defined yet. Go to WooCommerce and add some to use this feature.', 'algolia-woo-indexer'); |
361 | 361 | return; |
362 | 362 | } |
@@ -441,10 +441,10 @@ discard block |
||
441 | 441 | */ |
442 | 442 | Algolia_Check_Requirements::check_unmet_requirements(); |
443 | 443 | |
444 | - if (!Algolia_Check_Requirements::algolia_wp_version_check() || !Algolia_Check_Requirements::algolia_php_version_check()) { |
|
444 | + if ( ! Algolia_Check_Requirements::algolia_wp_version_check() || ! Algolia_Check_Requirements::algolia_php_version_check()) { |
|
445 | 445 | add_action( |
446 | 446 | 'admin_notices', |
447 | - function () { |
|
447 | + function() { |
|
448 | 448 | echo '<div class="error notice"> |
449 | 449 | <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> |
450 | 450 | </div>'; |
@@ -478,10 +478,10 @@ discard block |
||
478 | 478 | |
479 | 479 | self::$plugin_url = admin_url('options-general.php?page=algolia-woo-indexer-settings'); |
480 | 480 | |
481 | - if (!is_plugin_active('woocommerce/woocommerce.php')) { |
|
481 | + if ( ! is_plugin_active('woocommerce/woocommerce.php')) { |
|
482 | 482 | add_action( |
483 | 483 | 'admin_notices', |
484 | - function () { |
|
484 | + function() { |
|
485 | 485 | echo '<div class="error notice"> |
486 | 486 | <p>' . esc_html__('WooCommerce plugin must be enabled for Algolia Woo Indexer to work.', 'algolia-woo-indexer') . '</p> |
487 | 487 | </div>'; |
@@ -552,8 +552,8 @@ discard block |
||
552 | 552 | $filtered_app_id = sanitize_text_field($post_application_id['id']); |
553 | 553 | $filtered_api_key = sanitize_text_field($post_api_key['key']); |
554 | 554 | $filtered_index_name = sanitize_text_field($post_index_name['name']); |
555 | - $filtered_attributes_visibility = sanitize_text_field($attributes_visibility['value']); |
|
556 | - $filtered_attributes_variation = sanitize_text_field($attributes_variation['value']); |
|
555 | + $filtered_attributes_visibility = sanitize_text_field($attributes_visibility['value']); |
|
556 | + $filtered_attributes_variation = sanitize_text_field($attributes_variation['value']); |
|
557 | 557 | |
558 | 558 | /** |
559 | 559 | * sanitize select list of id's by getting integers and them implode seperated with comma |
@@ -573,8 +573,8 @@ discard block |
||
573 | 573 | /** |
574 | 574 | * Sanitizing by setting the value to either 1 or 0 |
575 | 575 | */ |
576 | - $filtered_product = (!empty($auto_send)) ? 1 : 0; |
|
577 | - $filtered_attributes_enabled = (!empty($attributes_enabled)) ? 1 : 0; |
|
576 | + $filtered_product = ( ! empty($auto_send)) ? 1 : 0; |
|
577 | + $filtered_attributes_enabled = ( ! empty($attributes_enabled)) ? 1 : 0; |
|
578 | 578 | |
579 | 579 | /** |
580 | 580 | * Filter the data fields checkboxes and verify that the input is an array and assign it to an associative array |
@@ -584,7 +584,7 @@ discard block |
||
584 | 584 | $filtered_fields = array(); |
585 | 585 | foreach (BASIC_FIELDS as $field) { |
586 | 586 | $raw_field = filter_input(INPUT_POST, 'algolia_woo_indexer_field_' . $field, FILTER_DEFAULT, FILTER_REQUIRE_ARRAY); |
587 | - $filtered_field = (!empty($raw_field)) ? 1 : 0; |
|
587 | + $filtered_field = ( ! empty($raw_field)) ? 1 : 0; |
|
588 | 588 | $filtered_fields[$field] = $filtered_field; |
589 | 589 | } |
590 | 590 | |
@@ -594,21 +594,21 @@ discard block |
||
594 | 594 | * |
595 | 595 | * @see https://developer.wordpress.org/reference/functions/update_option/ |
596 | 596 | */ |
597 | - if (isset($filtered_app_id) && (!empty($filtered_app_id))) { |
|
597 | + if (isset($filtered_app_id) && ( ! empty($filtered_app_id))) { |
|
598 | 598 | update_option( |
599 | 599 | ALGOWOO_DB_OPTION . ALGOLIA_APP_ID, |
600 | 600 | $filtered_app_id |
601 | 601 | ); |
602 | 602 | } |
603 | 603 | |
604 | - if (isset($filtered_api_key) && (!empty($filtered_api_key))) { |
|
604 | + if (isset($filtered_api_key) && ( ! empty($filtered_api_key))) { |
|
605 | 605 | update_option( |
606 | 606 | ALGOWOO_DB_OPTION . ALGOLIA_API_KEY, |
607 | 607 | $filtered_api_key |
608 | 608 | ); |
609 | 609 | } |
610 | 610 | |
611 | - if (isset($filtered_index_name) && (!empty($filtered_index_name))) { |
|
611 | + if (isset($filtered_index_name) && ( ! empty($filtered_index_name))) { |
|
612 | 612 | update_option( |
613 | 613 | ALGOWOO_DB_OPTION . INDEX_NAME, |
614 | 614 | $filtered_index_name |
@@ -629,35 +629,35 @@ discard block |
||
629 | 629 | ); |
630 | 630 | } |
631 | 631 | |
632 | - if (isset($filtered_attributes_visibility) && (!empty($filtered_attributes_visibility))) { |
|
632 | + if (isset($filtered_attributes_visibility) && ( ! empty($filtered_attributes_visibility))) { |
|
633 | 633 | update_option( |
634 | 634 | ALGOWOO_DB_OPTION . ATTRIBUTES_VISIBILITY, |
635 | 635 | $filtered_attributes_visibility |
636 | 636 | ); |
637 | 637 | } |
638 | 638 | |
639 | - if (isset($filtered_attributes_variation) && (!empty($filtered_attributes_variation))) { |
|
639 | + if (isset($filtered_attributes_variation) && ( ! empty($filtered_attributes_variation))) { |
|
640 | 640 | update_option( |
641 | 641 | ALGOWOO_DB_OPTION . ATTRIBUTES_VARIATION, |
642 | 642 | $filtered_attributes_variation |
643 | 643 | ); |
644 | 644 | } |
645 | 645 | |
646 | - if (isset($filtered_attributes_list) && (!empty($filtered_attributes_list))) { |
|
646 | + if (isset($filtered_attributes_list) && ( ! empty($filtered_attributes_list))) { |
|
647 | 647 | update_option( |
648 | 648 | ALGOWOO_DB_OPTION . ATTRIBUTES_LIST, |
649 | 649 | $filtered_attributes_list |
650 | 650 | ); |
651 | 651 | } |
652 | 652 | |
653 | - if (isset($clean_attributes_list_interp) && (!empty($clean_attributes_list_interp))) { |
|
653 | + if (isset($clean_attributes_list_interp) && ( ! empty($clean_attributes_list_interp))) { |
|
654 | 654 | update_option( |
655 | 655 | ALGOWOO_DB_OPTION . ATTRIBUTES_LIST_INTERPOLATE, |
656 | 656 | $clean_attributes_list_interp |
657 | 657 | ); |
658 | 658 | } |
659 | 659 | |
660 | - if (isset($filtered_fields) && (!empty($filtered_fields))) { |
|
660 | + if (isset($filtered_fields) && ( ! empty($filtered_fields))) { |
|
661 | 661 | foreach ($filtered_fields as $key => $value) { |
662 | 662 | update_option( |
663 | 663 | ALGOWOO_DB_OPTION . BASIC_FIELD_PREFIX . $key, |
@@ -721,7 +721,7 @@ discard block |
||
721 | 721 | /** |
722 | 722 | * Verify that the user can access the settings page |
723 | 723 | */ |
724 | - if (!current_user_can('manage_options')) { |
|
724 | + if ( ! current_user_can('manage_options')) { |
|
725 | 725 | wp_die(esc_html__('Action not allowed.', 'algolia_woo_indexer_settings')); |
726 | 726 | } ?> |
727 | 727 | <div class="wrap"> |
@@ -748,7 +748,7 @@ discard block |
||
748 | 748 | */ |
749 | 749 | public static function get_instance() |
750 | 750 | { |
751 | - if (!self::$instance) { |
|
751 | + if ( ! self::$instance) { |
|
752 | 752 | self::$instance = new Algolia_Woo_Indexer(); |
753 | 753 | } |
754 | 754 | return self::$instance; |