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