1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Main Algolia Woo Indexer class |
5
|
|
|
* Called from main plugin file algolia-woo-indexer.php |
6
|
|
|
* |
7
|
|
|
* @package algolia-woo-indexer |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
namespace Algowoo; |
11
|
|
|
|
12
|
|
|
use \Algowoo\Algolia_Check_Requirements as Algolia_Check_Requirements; |
|
|
|
|
13
|
|
|
use \Algowoo\Algolia_Verify_Nonces as Algolia_Verify_Nonces; |
|
|
|
|
14
|
|
|
use \Algowoo\Algolia_Send_Products as Algolia_Send_Products; |
|
|
|
|
15
|
|
|
use \Algowoo\Algolia_Attributes as Algolia_Attributes; |
|
|
|
|
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Abort if this file is called directly |
19
|
|
|
*/ |
20
|
|
|
if (!defined('ABSPATH')) { |
21
|
|
|
exit; |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Include plugin file if function is_plugin_active does not exist |
26
|
|
|
*/ |
27
|
|
|
if (!function_exists('is_plugin_active')) { |
28
|
|
|
require_once(ABSPATH . '/wp-admin/includes/plugin.php'); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
if (!class_exists('Algolia_Woo_Indexer')) { |
32
|
|
|
/** |
33
|
|
|
* Algolia WooIndexer main class |
34
|
|
|
*/ |
35
|
|
|
// TODO Rename class "Algolia_Woo_Indexer" to match the regular expression ^[A-Z][a-zA-Z0-9]*$. |
36
|
|
|
class Algolia_Woo_Indexer |
37
|
|
|
{ |
38
|
|
|
const PLUGIN_NAME = 'Algolia Woo Indexer'; |
39
|
|
|
const PLUGIN_TRANSIENT = 'algowoo-plugin-notice'; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Class instance |
43
|
|
|
* |
44
|
|
|
* @var object |
45
|
|
|
*/ |
46
|
|
|
private static $instance; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* The plugin URL |
50
|
|
|
* |
51
|
|
|
* @var string |
52
|
|
|
*/ |
53
|
|
|
private static $plugin_url = ''; |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Class constructor |
57
|
|
|
* |
58
|
|
|
* @return void |
59
|
|
|
*/ |
60
|
|
|
public function __construct() |
61
|
|
|
{ |
62
|
|
|
$this->init(); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Setup sections and fields to store and retrieve values from Settings API |
67
|
|
|
* |
68
|
|
|
* @return void |
69
|
|
|
*/ |
70
|
|
|
public static function setup_settings_sections() |
71
|
|
|
{ |
72
|
|
|
/** |
73
|
|
|
* Setup arguments for settings sections and fields |
74
|
|
|
* |
75
|
|
|
* @see https://developer.wordpress.org/reference/functions/register_setting/ |
76
|
|
|
*/ |
77
|
|
|
if (is_admin()) { |
78
|
|
|
$arguments = array( |
79
|
|
|
'type' => 'string', |
80
|
|
|
'sanitize_callback' => 'settings_fields_validate_options', |
81
|
|
|
'default' => null, |
82
|
|
|
); |
83
|
|
|
register_setting('algolia_woo_options', 'algolia_woo_options', $arguments); |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Make sure we reference the instance of the current class by using self::get_instance() |
87
|
|
|
* This way we can setup the correct callback function for add_settings_section and add_settings_field |
88
|
|
|
*/ |
89
|
|
|
$algowooindexer = self::get_instance(); |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Add our necessary settings sections and fields |
93
|
|
|
*/ |
94
|
|
|
add_settings_section( |
95
|
|
|
'algolia_woo_indexer_main', |
96
|
|
|
esc_html__('Algolia Woo Plugin Settings', 'algolia-woo-indexer'), |
97
|
|
|
array($algowooindexer, 'algolia_woo_indexer_section_text'), |
98
|
|
|
'algolia_woo_indexer' |
99
|
|
|
); |
100
|
|
|
add_settings_field( |
101
|
|
|
'algolia_woo_indexer_application_id', |
102
|
|
|
esc_html__('Application ID', 'algolia-woo-indexer'), |
103
|
|
|
array($algowooindexer, 'algolia_woo_indexer_application_id_output'), |
104
|
|
|
'algolia_woo_indexer', |
105
|
|
|
'algolia_woo_indexer_main' |
106
|
|
|
); |
107
|
|
|
add_settings_field( |
108
|
|
|
'algolia_woo_indexer_admin_api_key', |
109
|
|
|
esc_html__('Admin API Key', 'algolia-woo-indexer'), |
110
|
|
|
array($algowooindexer, 'algolia_woo_indexer_admin_api_key_output'), |
111
|
|
|
'algolia_woo_indexer', |
112
|
|
|
'algolia_woo_indexer_main' |
113
|
|
|
); |
114
|
|
|
add_settings_field( |
115
|
|
|
'algolia_woo_indexer_index_name', |
116
|
|
|
esc_html__('Index name (will be created if not existing)', 'algolia-woo-indexer'), |
117
|
|
|
array($algowooindexer, 'algolia_woo_indexer_index_name_output'), |
118
|
|
|
'algolia_woo_indexer', |
119
|
|
|
'algolia_woo_indexer_main' |
120
|
|
|
); |
121
|
|
|
add_settings_field( |
122
|
|
|
'algolia_woo_indexer_automatically_send_new_products', |
123
|
|
|
esc_html__('Automatically index new products', 'algolia-woo-indexer'), |
124
|
|
|
array($algowooindexer, 'algolia_woo_indexer_automatically_send_new_products_output'), |
125
|
|
|
'algolia_woo_indexer', |
126
|
|
|
'algolia_woo_indexer_main' |
127
|
|
|
); |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* Add sections and fields for the basic fields |
131
|
|
|
*/ |
132
|
|
|
add_settings_section( |
133
|
|
|
'algolia_woo_indexer_fields', |
134
|
|
|
esc_html__('Fields indexing settings', 'algolia-woo-indexer'), |
135
|
|
|
array($algowooindexer, 'algolia_woo_indexer_fields_section_text'), |
136
|
|
|
'algolia_woo_indexer' |
137
|
|
|
); |
138
|
|
|
foreach (BASIC_FIELDS as $field) { |
139
|
|
|
add_settings_field( |
140
|
|
|
'algolia_woo_indexer_field_' . $field, |
141
|
|
|
esc_html__($field, 'algolia-woo-indexer'), |
142
|
|
|
array($algowooindexer, 'algolia_woo_indexer_field_output'), |
143
|
|
|
'algolia_woo_indexer', |
144
|
|
|
'algolia_woo_indexer_fields', |
145
|
|
|
array( |
146
|
|
|
'label_for' => 'algolia_woo_indexer_field_' . $field, |
147
|
|
|
'name' => $field |
148
|
|
|
) |
149
|
|
|
); |
150
|
|
|
} |
151
|
|
|
add_settings_field( |
152
|
|
|
'algolia_woo_indexer_custom_fields', |
153
|
|
|
esc_html__('Custom Fields', 'algolia-woo-indexer'), |
154
|
|
|
array($algowooindexer, 'algolia_woo_indexer_custom_fields_output'), |
155
|
|
|
'algolia_woo_indexer', |
156
|
|
|
'algolia_woo_indexer_fields' |
157
|
|
|
); |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* add settings for Attributes |
161
|
|
|
*/ |
162
|
|
|
Algolia_Attributes::setup_attributes_settings(); |
163
|
|
|
|
164
|
|
|
} |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
/** |
168
|
|
|
* Output for admin API key field |
169
|
|
|
* |
170
|
|
|
* @see https://developer.wordpress.org/reference/functions/wp_nonce_field/ |
171
|
|
|
* |
172
|
|
|
* @return void |
173
|
|
|
*/ |
174
|
|
|
public static function algolia_woo_indexer_admin_api_key_output() |
175
|
|
|
{ |
176
|
|
|
$api_key = get_option(ALGOWOO_DB_OPTION . ALGOLIA_API_KEY); |
177
|
|
|
$api_key = is_string($api_key) ? $api_key : CHANGE_ME; |
178
|
|
|
|
179
|
|
|
wp_nonce_field('algolia_woo_indexer_admin_api_nonce_action', 'algolia_woo_indexer_admin_api_nonce_name'); |
180
|
|
|
|
181
|
|
|
echo "<input id='algolia_woo_indexer_admin_api_key' name='algolia_woo_indexer_admin_api_key[key]' |
182
|
|
|
type='text' value='" . esc_attr($api_key) . "' />"; |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
/** |
186
|
|
|
* Output for application ID field |
187
|
|
|
* |
188
|
|
|
* @return void |
189
|
|
|
*/ |
190
|
|
|
public static function algolia_woo_indexer_application_id_output() |
191
|
|
|
{ |
192
|
|
|
$application_id = get_option(ALGOWOO_DB_OPTION . ALGOLIA_APP_ID); |
193
|
|
|
$application_id = is_string($application_id) ? $application_id : CHANGE_ME; |
194
|
|
|
|
195
|
|
|
echo "<input id='algolia_woo_indexer_application_id' name='algolia_woo_indexer_application_id[id]' |
196
|
|
|
type='text' value='" . esc_attr($application_id) . "' />"; |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
/** |
200
|
|
|
* Output for index name field |
201
|
|
|
* |
202
|
|
|
* @return void |
203
|
|
|
*/ |
204
|
|
|
public static function algolia_woo_indexer_index_name_output() |
205
|
|
|
{ |
206
|
|
|
$index_name = get_option(ALGOWOO_DB_OPTION . INDEX_NAME); |
207
|
|
|
$index_name = is_string($index_name) ? $index_name : CHANGE_ME; |
208
|
|
|
|
209
|
|
|
echo "<input id='algolia_woo_indexer_index_name' name='algolia_woo_indexer_index_name[name]' |
210
|
|
|
type='text' value='" . esc_attr($index_name) . "' />"; |
211
|
|
|
} |
212
|
|
|
|
213
|
|
|
/** |
214
|
|
|
* Output for checkbox to check if we automatically send new products to Algolia |
215
|
|
|
* |
216
|
|
|
* @return void |
217
|
|
|
*/ |
218
|
|
|
public static function algolia_woo_indexer_automatically_send_new_products_output() |
219
|
|
|
{ |
220
|
|
|
/** |
221
|
|
|
* Sanitization is not really needed as the variable is not directly echoed |
222
|
|
|
* But I have still done it to be 100% safe |
223
|
|
|
*/ |
224
|
|
|
$auto_send = get_option(ALGOWOO_DB_OPTION . AUTOMATICALLY_SEND_NEW_PRODUCTS); |
225
|
|
|
$auto_send = (!empty($auto_send)) ? 1 : 0; ?> |
226
|
|
|
<input id="algolia_woo_indexer_automatically_send_new_products" name="algolia_woo_indexer_automatically_send_new_products[checked]" type="checkbox" <?php checked(1, $auto_send); ?> /> |
227
|
|
|
<?php |
228
|
|
|
} |
229
|
|
|
|
230
|
|
|
/** |
231
|
|
|
* Output for fields which data shall be sent to Algolia |
232
|
|
|
* |
233
|
|
|
* @return void |
234
|
|
|
*/ |
235
|
|
|
public static function algolia_woo_indexer_field_output($args) |
236
|
|
|
{ |
237
|
|
|
$value = get_option(ALGOWOO_DB_OPTION . BASIC_FIELD_PREFIX . $args["name"]); |
238
|
|
|
$isChecked = (!empty($value)) ? 1 : 0; |
239
|
|
|
?> |
240
|
|
|
<input id="<?php echo $args["label_for"] ?>" name="<?php echo $args["label_for"] ?>[checked]" type="checkbox" <?php checked(1, $isChecked); ?> /> |
241
|
|
|
<?php |
242
|
|
|
} |
243
|
|
|
|
244
|
|
|
/** |
245
|
|
|
* Output textarea for custom fields |
246
|
|
|
* |
247
|
|
|
* @return void |
248
|
|
|
*/ |
249
|
|
|
public static function algolia_woo_indexer_custom_fields_output() |
250
|
|
|
{ |
251
|
|
|
$custom_fields = get_option(ALGOWOO_DB_OPTION . CUSTOM_FIELDS); |
252
|
|
|
$asLines = str_replace(',', "\r", $custom_fields); |
253
|
|
|
?> |
254
|
|
|
<p><?php echo esc_html__('Add some custom fields names, for example from ACF (Advanced custom fields) here. create a new line for each field or separate with comma.', 'algolia-woo-indexer'); ?></p> |
255
|
|
|
<textarea id="algolia_woo_indexer_custom_fields" name="algolia_woo_indexer_custom_fields" rows="6" cols="50"><?php echo $asLines; ?></textarea> |
256
|
|
|
<?php |
257
|
|
|
} |
258
|
|
|
|
259
|
|
|
/** |
260
|
|
|
* Section text for plugin settings section text |
261
|
|
|
* |
262
|
|
|
* @return void |
263
|
|
|
*/ |
264
|
|
|
public static function algolia_woo_indexer_section_text() |
265
|
|
|
{ |
266
|
|
|
echo esc_html__('Enter your API settings here.', 'algolia-woo-indexer'); |
267
|
|
|
} |
268
|
|
|
|
269
|
|
|
/** |
270
|
|
|
* Section text for basic fields settings section text |
271
|
|
|
* |
272
|
|
|
* @return void |
273
|
|
|
*/ |
274
|
|
|
public static function algolia_woo_indexer_fields_section_text() |
275
|
|
|
{ |
276
|
|
|
echo esc_html__('Choose which basic fields shall be indexed.', 'algolia-woo-indexer'); |
277
|
|
|
} |
278
|
|
|
|
279
|
|
|
/** |
280
|
|
|
* Check if we are going to send products by verifying send products nonce |
281
|
|
|
* |
282
|
|
|
* @return void |
283
|
|
|
*/ |
284
|
|
|
public static function maybe_send_products() |
285
|
|
|
{ |
286
|
|
|
if (true === Algolia_Verify_Nonces::verify_send_products_nonce()) { |
287
|
|
|
Algolia_Send_Products::send_products_to_algolia(); |
288
|
|
|
} |
289
|
|
|
} |
290
|
|
|
|
291
|
|
|
/** |
292
|
|
|
* Initialize class, setup settings sections and fields |
293
|
|
|
* |
294
|
|
|
* @return void |
295
|
|
|
*/ |
296
|
|
|
public static function init() |
297
|
|
|
{ |
298
|
|
|
/** |
299
|
|
|
* Fetch the option to see if we are going to automatically send new products |
300
|
|
|
*/ |
301
|
|
|
$auto_send = get_option(ALGOWOO_DB_OPTION . AUTOMATICALLY_SEND_NEW_PRODUCTS); |
302
|
|
|
|
303
|
|
|
/** |
304
|
|
|
* Check that we have the minimum versions required and all of the required PHP extensions |
305
|
|
|
*/ |
306
|
|
|
Algolia_Check_Requirements::check_unmet_requirements(); |
307
|
|
|
|
308
|
|
|
if (!Algolia_Check_Requirements::algolia_wp_version_check() || !Algolia_Check_Requirements::algolia_php_version_check()) { |
309
|
|
|
add_action( |
310
|
|
|
'admin_notices', |
311
|
|
|
function () { |
312
|
|
|
echo '<div class="error notice"> |
313
|
|
|
<p>' . esc_html__('Please check the server requirements for Algolia Woo Indexer. <br/> It requires minimum PHP version 7.2 and WordPress version 5.0', 'algolia-woo-indexer') . '</p> |
314
|
|
|
</div>'; |
315
|
|
|
} |
316
|
|
|
); |
317
|
|
|
} |
318
|
|
|
|
319
|
|
|
$ob_class = get_called_class(); |
320
|
|
|
|
321
|
|
|
/** |
322
|
|
|
* Setup translations |
323
|
|
|
*/ |
324
|
|
|
add_action('plugins_loaded', array($ob_class, 'load_textdomain')); |
325
|
|
|
|
326
|
|
|
/** |
327
|
|
|
* Add actions to setup admin menu |
328
|
|
|
*/ |
329
|
|
|
if (is_admin()) { |
330
|
|
|
add_action('admin_menu', array($ob_class, 'admin_menu')); |
331
|
|
|
add_action('admin_init', array($ob_class, 'setup_settings_sections')); |
332
|
|
|
add_action('admin_init', array($ob_class, 'update_settings_options')); |
333
|
|
|
add_action('admin_init', array($ob_class, 'maybe_send_products')); |
334
|
|
|
|
335
|
|
|
/** |
336
|
|
|
* Register hook to automatically send new products if the option is set |
337
|
|
|
*/ |
338
|
|
|
|
339
|
|
|
if ('1' === $auto_send) { |
340
|
|
|
add_action('save_post', array($ob_class, 'send_new_product_to_algolia'), 10, 3); |
341
|
|
|
} |
342
|
|
|
|
343
|
|
|
self::$plugin_url = admin_url('options-general.php?page=algolia-woo-indexer-settings'); |
344
|
|
|
|
345
|
|
|
if (!is_plugin_active('woocommerce/woocommerce.php')) { |
346
|
|
|
add_action( |
347
|
|
|
'admin_notices', |
348
|
|
|
function () { |
349
|
|
|
echo '<div class="error notice"> |
350
|
|
|
<p>' . esc_html__('WooCommerce plugin must be enabled for Algolia Woo Indexer to work.', 'algolia-woo-indexer') . '</p> |
351
|
|
|
</div>'; |
352
|
|
|
} |
353
|
|
|
); |
354
|
|
|
} |
355
|
|
|
} |
356
|
|
|
} |
357
|
|
|
|
358
|
|
|
/** |
359
|
|
|
* Send a single product to Algolia once a new product has been published |
360
|
|
|
* |
361
|
|
|
* @param int $post_id ID of the product. |
362
|
|
|
* @param array $post Post array. |
363
|
|
|
* |
364
|
|
|
* @return void |
365
|
|
|
*/ |
366
|
|
|
public static function send_new_product_to_algolia($post_id, $post) |
367
|
|
|
{ |
368
|
|
|
if ('publish' !== $post->post_status || 'product' !== $post->post_type) { |
369
|
|
|
return; |
370
|
|
|
} |
371
|
|
|
Algolia_Send_Products::send_products_to_algolia($post_id); |
372
|
|
|
} |
373
|
|
|
|
374
|
|
|
/** |
375
|
|
|
* Verify nonces before we update options and settings |
376
|
|
|
* Also retrieve the value from the send_products_to_algolia hidden field to check if we are sending products to Algolia |
377
|
|
|
* |
378
|
|
|
* @return void |
379
|
|
|
*/ |
380
|
|
|
public static function update_settings_options() |
381
|
|
|
{ |
382
|
|
|
/** |
383
|
|
|
* Do not proceed if nonce for settings is not set |
384
|
|
|
*/ |
385
|
|
|
if (false === Algolia_Verify_Nonces::verify_settings_nonce()) { |
386
|
|
|
return; |
387
|
|
|
} |
388
|
|
|
|
389
|
|
|
/** |
390
|
|
|
* Do not proceed if we are going to send products |
391
|
|
|
*/ |
392
|
|
|
if (true === Algolia_Verify_Nonces::verify_send_products_nonce()) { |
393
|
|
|
return; |
394
|
|
|
} |
395
|
|
|
|
396
|
|
|
/** |
397
|
|
|
* Filter the application id, api key, index name, custom_fields and verify that the input is an array |
398
|
|
|
* |
399
|
|
|
* @see https://www.php.net/manual/en/function.filter-input.php |
400
|
|
|
*/ |
401
|
|
|
$post_application_id = filter_input(INPUT_POST, 'algolia_woo_indexer_application_id', FILTER_DEFAULT, FILTER_REQUIRE_ARRAY); |
402
|
|
|
$post_api_key = filter_input(INPUT_POST, 'algolia_woo_indexer_admin_api_key', FILTER_DEFAULT, FILTER_REQUIRE_ARRAY); |
403
|
|
|
$post_index_name = filter_input(INPUT_POST, 'algolia_woo_indexer_index_name', FILTER_DEFAULT, FILTER_REQUIRE_ARRAY); |
404
|
|
|
$auto_send = filter_input(INPUT_POST, 'algolia_woo_indexer_automatically_send_new_products', FILTER_DEFAULT, FILTER_REQUIRE_ARRAY); |
405
|
|
|
$custom_fields = filter_input(INPUT_POST, 'algolia_woo_indexer_custom_fields', FILTER_DEFAULT); |
406
|
|
|
|
407
|
|
|
/** |
408
|
|
|
* Properly sanitize text fields before updating data |
409
|
|
|
* |
410
|
|
|
* @see https://developer.wordpress.org/reference/functions/sanitize_text_field/ |
411
|
|
|
*/ |
412
|
|
|
$sanitized = array(); |
413
|
|
|
$sanitized['app_id'] = sanitize_text_field($post_application_id['id']); |
414
|
|
|
$sanitized['api_key'] = sanitize_text_field($post_api_key['key']); |
415
|
|
|
$sanitized['index_name'] = sanitize_text_field($post_index_name['name']); |
416
|
|
|
$sanitized['custom_fields'] = sanitize_textarea_field($custom_fields); |
417
|
|
|
|
418
|
|
|
/** |
419
|
|
|
* Sanitizing by setting the value to either 1 or 0 |
420
|
|
|
*/ |
421
|
|
|
$sanitized['product'] = (!empty($auto_send)) ? 1 : 0; |
422
|
|
|
|
423
|
|
|
/** |
424
|
|
|
* Filter the data fields checkboxes and verify that the input is an array and assign it to an associative array |
425
|
|
|
* |
426
|
|
|
* @see https://www.php.net/manual/en/function.filter-input.php |
427
|
|
|
*/ |
428
|
|
|
$sanitized['fields'] = array(); |
429
|
|
|
foreach (BASIC_FIELDS as $field) { |
430
|
|
|
$raw_field = filter_input(INPUT_POST, 'algolia_woo_indexer_field_' . $field, FILTER_DEFAULT, FILTER_REQUIRE_ARRAY); |
431
|
|
|
$filtered_field = (!empty($raw_field)) ? 1 : 0; |
432
|
|
|
$sanitized['fields'][$field] = $filtered_field; |
433
|
|
|
} |
434
|
|
|
|
435
|
|
|
/** |
436
|
|
|
* Values have been filtered and sanitized |
437
|
|
|
* Check if set and not empty and update the database |
438
|
|
|
* |
439
|
|
|
* @see https://developer.wordpress.org/reference/functions/update_option/ |
440
|
|
|
*/ |
441
|
|
|
if (isset($sanitized['app_id']) && (!empty($sanitized['app_id']))) { |
442
|
|
|
update_option( |
443
|
|
|
ALGOWOO_DB_OPTION . ALGOLIA_APP_ID, |
444
|
|
|
$sanitized['app_id'] |
445
|
|
|
); |
446
|
|
|
} |
447
|
|
|
|
448
|
|
|
if (isset($sanitized['api_key']) && (!empty($sanitized['api_key']))) { |
449
|
|
|
update_option( |
450
|
|
|
ALGOWOO_DB_OPTION . ALGOLIA_API_KEY, |
451
|
|
|
$sanitized['api_key'] |
452
|
|
|
); |
453
|
|
|
} |
454
|
|
|
|
455
|
|
|
if (isset($sanitized['index_name']) && (!empty($sanitized['index_name']))) { |
456
|
|
|
update_option( |
457
|
|
|
ALGOWOO_DB_OPTION . INDEX_NAME, |
458
|
|
|
$sanitized['index_name'] |
459
|
|
|
); |
460
|
|
|
} |
461
|
|
|
|
462
|
|
|
if (isset($sanitized['product'])) { |
463
|
|
|
update_option( |
464
|
|
|
ALGOWOO_DB_OPTION . AUTOMATICALLY_SEND_NEW_PRODUCTS, |
465
|
|
|
$sanitized['product'] |
466
|
|
|
); |
467
|
|
|
} |
468
|
|
|
|
469
|
|
|
if (isset($sanitized['fields']) && (!empty($sanitized['fields']))) { |
470
|
|
|
foreach ($sanitized['fields'] as $key => $value) { |
471
|
|
|
update_option( |
472
|
|
|
ALGOWOO_DB_OPTION . BASIC_FIELD_PREFIX . $key, |
473
|
|
|
$value |
474
|
|
|
); |
475
|
|
|
} |
476
|
|
|
} |
477
|
|
|
|
478
|
|
|
if (isset($sanitized['custom_fields'])) { |
479
|
|
|
$custom_fields_array = preg_split("/[\r\n,]+/", $sanitized['custom_fields'], -1, PREG_SPLIT_NO_EMPTY); |
480
|
|
|
$custom_fields_string = implode(",", $custom_fields_array); |
481
|
|
|
update_option( |
482
|
|
|
ALGOWOO_DB_OPTION . CUSTOM_FIELDS, |
483
|
|
|
$custom_fields_string |
484
|
|
|
); |
485
|
|
|
} |
486
|
|
|
|
487
|
|
|
/** |
488
|
|
|
* Update Attributes as well |
489
|
|
|
*/ |
490
|
|
|
Algolia_Attributes::update_attribute_options(); |
491
|
|
|
|
492
|
|
|
} |
493
|
|
|
|
494
|
|
|
/** |
495
|
|
|
* Sanitize input in settings fields and filter through regex to accept only a-z and A-Z |
496
|
|
|
* |
497
|
|
|
* @param string $input Settings text data |
498
|
|
|
* @return array |
499
|
|
|
*/ |
500
|
|
|
public static function settings_fields_validate_options($input) |
501
|
|
|
{ |
502
|
|
|
$valid = array(); |
503
|
|
|
$valid['name'] = preg_replace( |
504
|
|
|
'/[^a-zA-Z\s]/', |
505
|
|
|
'', |
506
|
|
|
$input['name'] |
507
|
|
|
); |
508
|
|
|
return $valid; |
509
|
|
|
} |
510
|
|
|
|
511
|
|
|
/** |
512
|
|
|
* Load text domain for translations |
513
|
|
|
* |
514
|
|
|
* @return void |
515
|
|
|
*/ |
516
|
|
|
public static function load_textdomain() |
517
|
|
|
{ |
518
|
|
|
load_plugin_textdomain('algolia-woo-indexer', false, basename(dirname(__FILE__)) . '/languages/'); |
519
|
|
|
} |
520
|
|
|
|
521
|
|
|
/** |
522
|
|
|
* Add the new menu to settings section so that we can configure the plugin |
523
|
|
|
* |
524
|
|
|
* @return void |
525
|
|
|
*/ |
526
|
|
|
public static function admin_menu() |
527
|
|
|
{ |
528
|
|
|
add_submenu_page( |
529
|
|
|
'options-general.php', |
530
|
|
|
esc_html__('Algolia Woo Indexer Settings', 'algolia-woo-indexer'), |
531
|
|
|
esc_html__('Algolia Woo Indexer Settings', 'algolia-woo-indexer'), |
532
|
|
|
'manage_options', |
533
|
|
|
'algolia-woo-indexer-settings', |
534
|
|
|
array(get_called_class(), 'algolia_woo_indexer_settings') |
535
|
|
|
); |
536
|
|
|
} |
537
|
|
|
|
538
|
|
|
/** |
539
|
|
|
* Display settings and allow user to modify them |
540
|
|
|
* |
541
|
|
|
* @return void |
542
|
|
|
*/ |
543
|
|
|
public static function algolia_woo_indexer_settings() |
544
|
|
|
{ |
545
|
|
|
/** |
546
|
|
|
* Verify that the user can access the settings page |
547
|
|
|
*/ |
548
|
|
|
if (!current_user_can('manage_options')) { |
549
|
|
|
wp_die(esc_html__('Action not allowed.', 'algolia_woo_indexer_settings')); |
550
|
|
|
} ?> |
551
|
|
|
<div class="wrap"> |
552
|
|
|
<h1><?php esc_html__('Algolia Woo Indexer Settings', 'algolia-woo-indexer'); ?></h1> |
553
|
|
|
<form action="<?php echo esc_url(self::$plugin_url); ?>" method="POST"> |
554
|
|
|
<?php |
555
|
|
|
settings_fields('algolia_woo_options'); |
556
|
|
|
do_settings_sections('algolia_woo_indexer'); |
557
|
|
|
submit_button('', 'primary wide'); ?> |
558
|
|
|
</form> |
559
|
|
|
<form action="<?php echo esc_url(self::$plugin_url); ?>" method="POST"> |
560
|
|
|
<?php wp_nonce_field('send_products_to_algolia_nonce_action', 'send_products_to_algolia_nonce_name'); ?> |
561
|
|
|
<input type="hidden" name="send_products_to_algolia" id="send_products_to_algolia" value="true" /> |
562
|
|
|
<?php submit_button(esc_html__('Send products to Algolia', 'algolia_woo_indexer_settings'), 'primary wide', '', false); ?> |
563
|
|
|
</form> |
564
|
|
|
</div> |
565
|
|
|
<?php |
566
|
|
|
} |
567
|
|
|
|
568
|
|
|
/** |
569
|
|
|
* Get active object instance |
570
|
|
|
* |
571
|
|
|
* @return object |
572
|
|
|
*/ |
573
|
|
|
public static function get_instance() |
574
|
|
|
{ |
575
|
|
|
if (!self::$instance) { |
576
|
|
|
self::$instance = new Algolia_Woo_Indexer(); |
577
|
|
|
} |
578
|
|
|
return self::$instance; |
579
|
|
|
} |
580
|
|
|
|
581
|
|
|
/** |
582
|
|
|
* The actions to execute when the plugin is activated. |
583
|
|
|
* |
584
|
|
|
* @return void |
585
|
|
|
*/ |
586
|
|
|
public static function activate_plugin() |
587
|
|
|
{ |
588
|
|
|
|
589
|
|
|
/** |
590
|
|
|
* Set default values for options if not already set |
591
|
|
|
*/ |
592
|
|
|
$auto_send = get_option(ALGOWOO_DB_OPTION . AUTOMATICALLY_SEND_NEW_PRODUCTS); |
593
|
|
|
$algolia_application_id = get_option(ALGOWOO_DB_OPTION . ALGOLIA_APP_ID); |
594
|
|
|
$algolia_api_key = get_option(ALGOWOO_DB_OPTION . ALGOLIA_API_KEY); |
595
|
|
|
$algolia_index_name = get_option(ALGOWOO_DB_OPTION . INDEX_NAME); |
596
|
|
|
$custom_fields = get_option(ALGOWOO_DB_OPTION . CUSTOM_FIELDS); |
597
|
|
|
|
598
|
|
|
if (empty($auto_send)) { |
599
|
|
|
add_option( |
600
|
|
|
ALGOWOO_DB_OPTION . AUTOMATICALLY_SEND_NEW_PRODUCTS, |
601
|
|
|
'0' |
602
|
|
|
); |
603
|
|
|
} |
604
|
|
|
|
605
|
|
|
if (empty($algolia_application_id)) { |
606
|
|
|
add_option( |
607
|
|
|
ALGOWOO_DB_OPTION . ALGOLIA_APP_ID, |
608
|
|
|
'Change me' |
609
|
|
|
); |
610
|
|
|
} |
611
|
|
|
|
612
|
|
|
if (empty($algolia_api_key)) { |
613
|
|
|
add_option( |
614
|
|
|
ALGOWOO_DB_OPTION . ALGOLIA_API_KEY, |
615
|
|
|
'Change me' |
616
|
|
|
); |
617
|
|
|
} |
618
|
|
|
|
619
|
|
|
if (empty($algolia_index_name)) { |
620
|
|
|
add_option( |
621
|
|
|
ALGOWOO_DB_OPTION . INDEX_NAME, |
622
|
|
|
'Change me' |
623
|
|
|
); |
624
|
|
|
} |
625
|
|
|
if (empty($custom_fields)) { |
626
|
|
|
add_option( |
627
|
|
|
ALGOWOO_DB_OPTION . CUSTOM_FIELDS, |
628
|
|
|
'' |
629
|
|
|
); |
630
|
|
|
} |
631
|
|
|
|
632
|
|
|
/** |
633
|
|
|
* Set default values for index fields if not already set |
634
|
|
|
*/ |
635
|
|
|
foreach (BASIC_FIELDS as $field) { |
636
|
|
|
$value = get_option(ALGOWOO_DB_OPTION . BASIC_FIELD_PREFIX . $field); |
637
|
|
|
if (empty($value)) { |
638
|
|
|
update_option( |
639
|
|
|
ALGOWOO_DB_OPTION . BASIC_FIELD_PREFIX . $field, |
640
|
|
|
'1' |
641
|
|
|
); |
642
|
|
|
} |
643
|
|
|
} |
644
|
|
|
|
645
|
|
|
/** |
646
|
|
|
* activate attributes |
647
|
|
|
*/ |
648
|
|
|
Algolia_Attributes::activate_attributes(); |
649
|
|
|
|
650
|
|
|
set_transient(self::PLUGIN_TRANSIENT, true); |
651
|
|
|
} |
652
|
|
|
|
653
|
|
|
/** |
654
|
|
|
* The actions to execute when the plugin is deactivated. |
655
|
|
|
* |
656
|
|
|
* @return void |
657
|
|
|
*/ |
658
|
|
|
public static function deactivate_plugin() |
659
|
|
|
{ |
660
|
|
|
delete_transient(self::PLUGIN_TRANSIENT); |
661
|
|
|
} |
662
|
|
|
} |
663
|
|
|
} |
664
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths