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