Passed
Pull Request — master (#169)
by
unknown
02:05
created

algolia_woo_indexer_field_output()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 4
Bugs 2 Features 1
Metric Value
cc 2
eloc 6
c 4
b 2
f 1
nc 2
nop 1
dl 0
loc 6
rs 10
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;
0 ignored issues
show
Bug introduced by
The type \Algowoo\Algolia_Check_Requirements was not found. Maybe you did not declare it correctly or list all dependencies?

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:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
Bug introduced by
This use statement conflicts with another class in this namespace, Algowoo\Algolia_Check_Requirements. Consider defining an alias.

Let?s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let?s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
13
use \Algowoo\Algolia_Verify_Nonces as Algolia_Verify_Nonces;
0 ignored issues
show
Bug introduced by
The type \Algowoo\Algolia_Verify_Nonces was not found. Maybe you did not declare it correctly or list all dependencies?

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:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
Bug introduced by
This use statement conflicts with another class in this namespace, Algowoo\Algolia_Verify_Nonces. Consider defining an alias.

Let?s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let?s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
14
use \Algowoo\Algolia_Send_Products as Algolia_Send_Products;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, Algowoo\Algolia_Send_Products. Consider defining an alias.

Let?s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let?s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
Bug introduced by
The type \Algowoo\Algolia_Send_Products was not found. Maybe you did not declare it correctly or list all dependencies?

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:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
15
16
/**
17
 * Abort if this file is called directly
18
 */
19
if (!defined('ABSPATH')) {
20
    exit;
21
}
22
23
/**
24
 * Include plugin file if function is_plugin_active does not exist
25
 */
26
if (!function_exists('is_plugin_active')) {
27
    require_once(ABSPATH . '/wp-admin/includes/plugin.php');
28
}
29
30
if (!class_exists('Algolia_Woo_Indexer')) {
31
    /**
32
     * Algolia WooIndexer main class
33
     */
34
    // TODO Rename class "Algolia_Woo_Indexer" to match the regular expression ^[A-Z][a-zA-Z0-9]*$.
35
    class Algolia_Woo_Indexer
36
    {
37
        const PLUGIN_NAME      = 'Algolia Woo Indexer';
38
        const PLUGIN_TRANSIENT = 'algowoo-plugin-notice';
39
40
        /**
41
         * Class instance
42
         *
43
         * @var object
44
         */
45
        private static $instance;
46
47
        /**
48
         * The plugin URL
49
         *
50
         * @var string
51
         */
52
        private static $plugin_url = '';
53
54
        /**
55
         * Class constructor
56
         *
57
         * @return void
58
         */
59
        public function __construct()
60
        {
61
            $this->init();
62
        }
63
64
        /**
65
         * Setup sections and fields to store and retrieve values from Settings API
66
         *
67
         * @return void
68
         */
69
        public static function setup_settings_sections()
70
        {
71
            /**
72
             * Setup arguments for settings sections and fields
73
             *
74
             * @see https://developer.wordpress.org/reference/functions/register_setting/
75
             */
76
            if (is_admin()) {
77
                $arguments = array(
78
                    'type'              => 'string',
79
                    'sanitize_callback' => 'settings_fields_validate_options',
80
                    'default'           => null,
81
                );
82
                register_setting('algolia_woo_options', 'algolia_woo_options', $arguments);
83
84
                /**
85
                 * Make sure we reference the instance of the current class by using self::get_instance()
86
                 * This way we can setup the correct callback function for add_settings_section and add_settings_field
87
                 */
88
                $algowooindexer = self::get_instance();
89
90
                /**
91
                 * Add our necessary settings sections and fields
92
                 */
93
                add_settings_section(
94
                    'algolia_woo_indexer_main',
95
                    esc_html__('Algolia Woo Plugin Settings', 'algolia-woo-indexer'),
96
                    array($algowooindexer, 'algolia_woo_indexer_section_text'),
97
                    'algolia_woo_indexer'
98
                );
99
                add_settings_field(
100
                    'algolia_woo_indexer_application_id',
101
                    esc_html__('Application ID', 'algolia-woo-indexer'),
102
                    array($algowooindexer, 'algolia_woo_indexer_application_id_output'),
103
                    'algolia_woo_indexer',
104
                    'algolia_woo_indexer_main'
105
                );
106
                add_settings_field(
107
                    'algolia_woo_indexer_admin_api_key',
108
                    esc_html__('Admin API Key', 'algolia-woo-indexer'),
109
                    array($algowooindexer, 'algolia_woo_indexer_admin_api_key_output'),
110
                    'algolia_woo_indexer',
111
                    'algolia_woo_indexer_main'
112
                );
113
                add_settings_field(
114
                    'algolia_woo_indexer_index_name',
115
                    esc_html__('Index name (will be created if not existing)', 'algolia-woo-indexer'),
116
                    array($algowooindexer, 'algolia_woo_indexer_index_name_output'),
117
                    'algolia_woo_indexer',
118
                    'algolia_woo_indexer_main'
119
                );
120
                add_settings_field(
121
                    'algolia_woo_indexer_automatically_send_new_products',
122
                    esc_html__('Automatically index new products', 'algolia-woo-indexer'),
123
                    array($algowooindexer, 'algolia_woo_indexer_automatically_send_new_products_output'),
124
                    'algolia_woo_indexer',
125
                    'algolia_woo_indexer_main'
126
                );
127
128
                /**
129
                 * Add sections and fields for the basic fields
130
                 */
131
                add_settings_section(
132
                    'algolia_woo_indexer_fields',
133
                    esc_html__('Fields indexing settings', 'algolia-woo-indexer'),
134
                    array($algowooindexer, 'algolia_woo_indexer_fields_section_text'),
135
                    'algolia_woo_indexer'
136
                );
137
                foreach (BASIC_FIELDS as $field) {
138
                    add_settings_field(
139
                        'algolia_woo_indexer_field_' . $field,
140
                        esc_html__($field, 'algolia-woo-indexer'),
141
                        array($algowooindexer, 'algolia_woo_indexer_field_output'),
142
                        'algolia_woo_indexer',
143
                        'algolia_woo_indexer_fields',
144
                        array(
145
                            'label_for' => 'algolia_woo_indexer_field_' . $field,
146
                            'name' => $field
147
                        )
148
                    );
149
                }
150
151
                /**
152
                 * Add sections and fields for the attributes
153
                 */
154
                add_settings_section(
155
                    'algolia_woo_indexer_attributes',
156
                    esc_html__('Attributes indexing settings', 'algolia-woo-indexer'),
157
                    array($algowooindexer, 'algolia_woo_indexer_attributes_section_text'),
158
                    'algolia_woo_indexer'
159
                );
160
161
                foreach (ATTRIBUTES_SETTINGS as $key => $description) {
162
                    add_settings_field(
163
                        'algolia_woo_indexer_attributes_' . $key,
164
                        esc_html__($description, 'algolia-woo-indexer'),
165
                        array($algowooindexer, 'algolia_woo_indexer_attributes_' . $key . '_output'),
166
                        'algolia_woo_indexer',
167
                        'algolia_woo_indexer_attributes'
168
                    );
169
                }
170
            }
171
        }
172
173
        /**
174
         * Output for admin API key field
175
         *
176
         * @see https://developer.wordpress.org/reference/functions/wp_nonce_field/
177
         *
178
         * @return void
179
         */
180
        public static function algolia_woo_indexer_admin_api_key_output()
181
        {
182
            $api_key = get_option(ALGOWOO_DB_OPTION . ALGOLIA_API_KEY);
183
            $api_key = is_string($api_key) ? $api_key : CHANGE_ME;
184
185
            wp_nonce_field('algolia_woo_indexer_admin_api_nonce_action', 'algolia_woo_indexer_admin_api_nonce_name');
186
187
            echo "<input id='algolia_woo_indexer_admin_api_key' name='algolia_woo_indexer_admin_api_key[key]'
188
				type='text' value='" . esc_attr($api_key) . "' />";
189
        }
190
191
        /**
192
         * Output for application ID field
193
         *
194
         * @return void
195
         */
196
        public static function algolia_woo_indexer_application_id_output()
197
        {
198
            $application_id = get_option(ALGOWOO_DB_OPTION . ALGOLIA_APP_ID);
199
            $application_id = is_string($application_id) ? $application_id : CHANGE_ME;
200
201
            echo "<input id='algolia_woo_indexer_application_id' name='algolia_woo_indexer_application_id[id]'
202
				type='text' value='" . esc_attr($application_id) . "' />";
203
        }
204
205
        /**
206
         * Output for index name field
207
         *
208
         * @return void
209
         */
210
        public static function algolia_woo_indexer_index_name_output()
211
        {
212
            $index_name = get_option(ALGOWOO_DB_OPTION . INDEX_NAME);
213
            $index_name = is_string($index_name) ? $index_name : CHANGE_ME;
214
215
            echo "<input id='algolia_woo_indexer_index_name' name='algolia_woo_indexer_index_name[name]'
216
				type='text' value='" . esc_attr($index_name) . "' />";
217
        }
218
219
        /**
220
         * Output for checkbox to check if we automatically send new products to Algolia
221
         *
222
         * @return void
223
         */
224
        public static function algolia_woo_indexer_automatically_send_new_products_output()
225
        {
226
            /**
227
             * Sanitization is not really needed as the variable is not directly echoed
228
             * But I have still done it to be 100% safe
229
             */
230
            $auto_send = get_option(ALGOWOO_DB_OPTION . AUTOMATICALLY_SEND_NEW_PRODUCTS);
231
            $auto_send = (!empty($auto_send)) ? 1 : 0; ?>
232
            <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); ?> />
233
        <?php
234
        }
235
236
        /**
237
         * Output for fields which data shall be sent to Algolia
238
         *
239
         * @return void
240
         */
241
        public static function algolia_woo_indexer_field_output($args)
242
        {
243
            $value = get_option(ALGOWOO_DB_OPTION . BASIC_FIELD_PREFIX . $args["name"]);
244
            $isChecked = (!empty($value)) ? 1 : 0;
245
        ?>
246
            <input id="<?php echo $args["label_for"] ?>" name="<?php echo $args["label_for"] ?>[checked]" type="checkbox" <?php checked(1, $isChecked); ?> />
247
        <?php
248
        }
249
250
        /**
251
         * Output for attributes if functionality is enabled
252
         *
253
         * @return void
254
         */
255
        public static function algolia_woo_indexer_attributes_enabled_output()
256
        {
257
            $value = get_option(ALGOWOO_DB_OPTION . ATTRIBUTES_ENABLED);
258
            $isChecked = (!empty($value)) ? 1 : 0;
259
        ?>
260
            <input id="algolia_woo_indexer_attributes_enabled" name="algolia_woo_indexer_attributes_enabled[checked]" type="checkbox" <?php checked(1, $isChecked); ?> />
261
            <?php
262
        }
263
264
        /**
265
         * Output for attributes how to handle visibility setting
266
         *
267
         * @return void
268
         */
269
        public static function algolia_woo_indexer_attributes_visibility_output()
270
        {
271
            $value = get_option(ALGOWOO_DB_OPTION . ATTRIBUTES_VISIBILITY);
272
            foreach (ATTRIBUTES_VISIBILITY_STATES as $state) {
273
                $id = 'algolia_woo_indexer_attributes_visibility_' . $state;
274
            ?>
275
                <p><input id="<?php echo $id; ?>" name="algolia_woo_indexer_attributes_visibility[value]" type="radio" value="<?php echo $state; ?>" <?php checked($state, $value); ?> /><label for="<?php echo $id; ?>"><?php echo esc_html__($state, 'algolia-woo-indexer'); ?></label></p>
276
            <?php
277
            }
278
        }
279
280
        /**
281
         * Output for attributes how to handle variant setting
282
         *
283
         * @return void
284
         */
285
        public static function algolia_woo_indexer_attributes_variation_output()
286
        {
287
            $value = get_option(ALGOWOO_DB_OPTION . ATTRIBUTES_VARIATION);
288
            foreach (ATTRIBUTES_VARIATION_STATES as $state) {
289
                $id = 'algolia_woo_indexer_attributes_variation_' . $state;
290
            ?>
291
                <p><input id="<?php echo $id; ?>" name="algolia_woo_indexer_attributes_variation[value]" type="radio" value="<?php echo $state; ?>" <?php checked($state, $value); ?> /><label for="<?php echo $id; ?>"><?php echo esc_html__($state, 'algolia-woo-indexer'); ?></label></p>
292
            <?php
293
            }
294
        }
295
296
        /**
297
         * Output for attributes list which attributes are whitelisted
298
         *
299
         * @return void
300
         */
301
        public static function algolia_woo_indexer_attributes_list_output()
302
        {
303
            $value = get_option(ALGOWOO_DB_OPTION . ATTRIBUTES_LIST);
304
            $selectedIds = explode(",", $value);
0 ignored issues
show
Bug introduced by
It seems like $value can also be of type false; however, parameter $string of explode() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

304
            $selectedIds = explode(",", /** @scrutinizer ignore-type */ $value);
Loading history...
305
            $name = "algolia_woo_indexer_attributes_list[list]";
306
            $description = __('Here you can whitelist all the attributes. Use the <b>shift</b> or <b>control</b> buttons to select multiple attributes.', 'algolia-woo-indexer');
307
            self::generic_attributes_select_output($name, $selectedIds, $description);
308
        }
309
310
        /**
311
         * Output for attributes list which are using a numeric interpolation
312
         *
313
         * @return void
314
         */
315
        public static function algolia_woo_indexer_attributes_interp_output()
316
        {
317
            $value = get_option(ALGOWOO_DB_OPTION . ATTRIBUTES_INTERP);
318
            $selectedIds = explode(",", $value);
0 ignored issues
show
Bug introduced by
It seems like $value can also be of type false; however, parameter $string of explode() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

318
            $selectedIds = explode(",", /** @scrutinizer ignore-type */ $value);
Loading history...
319
            $name = "algolia_woo_indexer_attributes_interp[list]";
320
            $description = __('If you have some attributes based on number which shall be interpd between the lowest to the highest number, you can select it here. A common usecase for this is if you want to have a <b>range slider</b> in aloglia which works for a certain range. Example: a plant grows between 20 and 25cm tall. for this you enter 20 and 25 as attribute values to your product and it will automatically extend the data to [20,21,22,23,24,25]', 'algolia-woo-indexer');
321
            self::generic_attributes_select_output($name, $selectedIds, $description);
322
        }
323
324
        /**
325
         * Generic Output for attributes list where attributes are whitelisted
326
         *
327
         * @return void
328
         */
329
        public static function generic_attributes_select_output($name, $selectedIds, $description)
330
        {
331
            $attribute_taxonomies = wc_get_attribute_taxonomies();
0 ignored issues
show
Bug introduced by
The function wc_get_attribute_taxonomies was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

331
            $attribute_taxonomies = /** @scrutinizer ignore-call */ wc_get_attribute_taxonomies();
Loading history...
332
333
            if (!$attribute_taxonomies) {
334
                echo esc_html__('You don\'t have any attributes defined yet. Go to WooCommerce and add some to use this feature.', 'algolia-woo-indexer');
335
                return;
336
            }
337
            ?>
338
            <p><?php echo $description; ?></p>
339
            <select multiple="multiple" name="<?php echo $name; ?>[]" size="<?php echo count($attribute_taxonomies); ?>">
340
                <?php
341
                foreach ($attribute_taxonomies as $tax) {
342
343
                    $id = $tax->attribute_id;
344
                    $label = $tax->attribute_label;
345
                    $name = $tax->attribute_name;
346
                    $selected = in_array($id, $selectedIds) ? ' selected="selected" ' : '';
347
                ?>
348
                    <option value="<?php echo $id; ?>" <?php echo $selected; ?>>
349
                        <?php echo $label . ' (' . $name . ')'; ?>
350
                    </option>
351
                <?php
352
                }
353
                ?>
354
            </select>
355
        <?php
356
357
        }
358
359
        /**
360
         * Section text for plugin settings section text
361
         *
362
         * @return void
363
         */
364
        public static function algolia_woo_indexer_section_text()
365
        {
366
            echo esc_html__('Enter your API settings here.', 'algolia-woo-indexer');
367
        }
368
369
        /**
370
         * Section text for basic fields settings section text
371
         *
372
         * @return void
373
         */
374
        public static function algolia_woo_indexer_fields_section_text()
375
        {
376
            echo esc_html__('Choose which basic fields shall be indexed.', 'algolia-woo-indexer');
377
        }
378
379
        /**
380
         * Section text for attributes settings section text
381
         *
382
         * @return void
383
         */
384
        public static function algolia_woo_indexer_attributes_section_text()
385
        {
386
            echo esc_html__('Control if and how the attributes shall be indexed.', 'algolia-woo-indexer');
387
        }
388
389
        /**
390
         * Check if we are going to send products by verifying send products nonce
391
         *
392
         * @return void
393
         */
394
        public static function maybe_send_products()
395
        {
396
            if (true === Algolia_Verify_Nonces::verify_send_products_nonce()) {
397
                Algolia_Send_Products::send_products_to_algolia();
398
            }
399
        }
400
401
        /**
402
         * Initialize class, setup settings sections and fields
403
         *
404
         * @return void
405
         */
406
        public static function init()
407
        {
408
            /**
409
             * Fetch the option to see if we are going to automatically send new products
410
             */
411
            $auto_send = get_option(ALGOWOO_DB_OPTION . AUTOMATICALLY_SEND_NEW_PRODUCTS);
412
413
            /**
414
             * Check that we have the minimum versions required and all of the required PHP extensions
415
             */
416
            Algolia_Check_Requirements::check_unmet_requirements();
417
418
            if (!Algolia_Check_Requirements::algolia_wp_version_check() || !Algolia_Check_Requirements::algolia_php_version_check()) {
419
                add_action(
420
                    'admin_notices',
421
                    function () {
422
                        echo '<div class="error notice">
423
                                  <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>
424
                                </div>';
425
                    }
426
                );
427
            }
428
429
            $ob_class = get_called_class();
430
431
            /**
432
             * Setup translations
433
             */
434
            add_action('plugins_loaded', array($ob_class, 'load_textdomain'));
435
436
            /**
437
             * Add actions to setup admin menu
438
             */
439
            if (is_admin()) {
440
                add_action('admin_menu', array($ob_class, 'admin_menu'));
441
                add_action('admin_init', array($ob_class, 'setup_settings_sections'));
442
                add_action('admin_init', array($ob_class, 'update_settings_options'));
443
                add_action('admin_init', array($ob_class, 'maybe_send_products'));
444
445
                /**
446
                 * Register hook to automatically send new products if the option is set
447
                 */
448
449
                if ('1' === $auto_send) {
450
                    add_action('save_post', array($ob_class, 'send_new_product_to_algolia'), 10, 3);
451
                }
452
453
                self::$plugin_url = admin_url('options-general.php?page=algolia-woo-indexer-settings');
454
455
                if (!is_plugin_active('woocommerce/woocommerce.php')) {
456
                    add_action(
457
                        'admin_notices',
458
                        function () {
459
                            echo '<div class="error notice">
460
								  <p>' . esc_html__('WooCommerce plugin must be enabled for Algolia Woo Indexer to work.', 'algolia-woo-indexer') . '</p>
461
								</div>';
462
                        }
463
                    );
464
                }
465
            }
466
        }
467
468
        /**
469
         * Send a single product to Algolia once a new product has been published
470
         *
471
         * @param int   $post_id ID of the product.
472
         * @param array $post Post array.
473
         *
474
         * @return void
475
         */
476
        public static function send_new_product_to_algolia($post_id, $post)
477
        {
478
            if ('publish' !== $post->post_status || 'product' !== $post->post_type) {
479
                return;
480
            }
481
            Algolia_Send_Products::send_products_to_algolia($post_id);
482
        }
483
484
        /**
485
         * Verify nonces before we update options and settings
486
         * Also retrieve the value from the send_products_to_algolia hidden field to check if we are sending products to Algolia
487
         *
488
         * @return void
489
         */
490
        public static function update_settings_options()
491
        {
492
            /**
493
             * Do not proceed if nonce for settings is not set
494
             */
495
            if (false === Algolia_Verify_Nonces::verify_settings_nonce()) {
496
                return;
497
            }
498
499
            /**
500
             * Do not proceed if we are going to send products
501
             */
502
            if (true === Algolia_Verify_Nonces::verify_send_products_nonce()) {
503
                return;
504
            }
505
506
            /**
507
             * Filter the application id, api key, index name and verify that the input is an array
508
             *
509
             * @see https://www.php.net/manual/en/function.filter-input.php
510
             */
511
            $post_application_id             = filter_input(INPUT_POST, 'algolia_woo_indexer_application_id', FILTER_DEFAULT, FILTER_REQUIRE_ARRAY);
512
            $post_api_key                    = filter_input(INPUT_POST, 'algolia_woo_indexer_admin_api_key', FILTER_DEFAULT, FILTER_REQUIRE_ARRAY);
513
            $post_index_name                 = filter_input(INPUT_POST, 'algolia_woo_indexer_index_name', FILTER_DEFAULT, FILTER_REQUIRE_ARRAY);
514
            $auto_send                       = filter_input(INPUT_POST, 'algolia_woo_indexer_automatically_send_new_products', FILTER_DEFAULT, FILTER_REQUIRE_ARRAY);
515
            $attributes_enabled              = filter_input(INPUT_POST, 'algolia_woo_indexer_attributes_enabled', FILTER_DEFAULT, FILTER_REQUIRE_ARRAY);
516
            $attributes_visibility           = filter_input(INPUT_POST, 'algolia_woo_indexer_attributes_visibility', FILTER_DEFAULT, FILTER_REQUIRE_ARRAY);
517
            $attributes_variation            = filter_input(INPUT_POST, 'algolia_woo_indexer_attributes_variation', FILTER_DEFAULT, FILTER_REQUIRE_ARRAY);
518
            $attributes_list                 = filter_input(INPUT_POST, 'algolia_woo_indexer_attributes_list', FILTER_DEFAULT, FILTER_REQUIRE_ARRAY);
519
            $attributes_interp               = filter_input(INPUT_POST, 'algolia_woo_indexer_attributes_interp', FILTER_DEFAULT, FILTER_REQUIRE_ARRAY);
520
521
            /**
522
             * Properly sanitize text fields before updating data
523
             *
524
             * @see https://developer.wordpress.org/reference/functions/sanitize_text_field/
525
             */
526
            $sanitized = array();
527
            $sanitized['app_id']                    = sanitize_text_field($post_application_id['id']);
528
            $sanitized['api_key']                   = sanitize_text_field($post_api_key['key']);
529
            $sanitized['index_name']                = sanitize_text_field($post_index_name['name']);
530
            $sanitized['attributes_visibility']     = sanitize_text_field($attributes_visibility['value']);
531
            $sanitized['attributes_variation']      = sanitize_text_field($attributes_variation['value']);
532
533
            /**
534
             * sanitize select list of id's by getting integers and them implode seperated with comma
535
             */
536
537
            $attributes_list_integers = [];
538
            foreach ($attributes_list['list'] as $id) {
539
                array_push($attributes_list_integers, (int) $id);
540
            }
541
            $sanitized['attributes_list'] = implode(',', $attributes_list_integers);
542
            $attributes_interp_int = [];
543
            foreach ($attributes_interp['list'] as $id) {
544
                array_push($attributes_interp_int, (int) $id);
545
            }
546
            $sanitized['attributes_interp'] = implode(',', $attributes_interp_int);
547
548
            /**
549
             * Sanitizing by setting the value to either 1 or 0
550
             */
551
            $sanitized['product'] = (!empty($auto_send)) ? 1 : 0;
552
            $sanitized['attributes_enabled'] = (!empty($attributes_enabled)) ? 1 : 0;
553
554
            /**
555
             * Filter the data fields checkboxes and verify that the input is an array and assign it to an associative array
556
             *
557
             * @see https://www.php.net/manual/en/function.filter-input.php
558
             */
559
            $sanitized['fields'] = array();
560
            foreach (BASIC_FIELDS as $field) {
561
                $raw_field = filter_input(INPUT_POST, 'algolia_woo_indexer_field_' . $field, FILTER_DEFAULT, FILTER_REQUIRE_ARRAY);
562
                $filtered_field = (!empty($raw_field)) ? 1 : 0;
563
                $sanitized['fields'][$field] = $filtered_field;
564
            }
565
566
            /**
567
             * Values have been filtered and sanitized
568
             * Check if set and not empty and update the database
569
             *
570
             * @see https://developer.wordpress.org/reference/functions/update_option/
571
             */
572
            if (isset($sanitized['app_id']) && (!empty($sanitized['app_id']))) {
573
                update_option(
574
                    ALGOWOO_DB_OPTION . ALGOLIA_APP_ID,
575
                    $sanitized['app_id']
576
                );
577
            }
578
579
            if (isset($sanitized['api_key']) && (!empty($sanitized['api_key']))) {
580
                update_option(
581
                    ALGOWOO_DB_OPTION . ALGOLIA_API_KEY,
582
                    $sanitized['api_key']
583
                );
584
            }
585
586
            if (isset($sanitized['index_name']) && (!empty($sanitized['index_name']))) {
587
                update_option(
588
                    ALGOWOO_DB_OPTION . INDEX_NAME,
589
                    $sanitized['index_name']
590
                );
591
            }
592
593
            if (isset($sanitized['product'])) {
594
                update_option(
595
                    ALGOWOO_DB_OPTION . AUTOMATICALLY_SEND_NEW_PRODUCTS,
596
                    $sanitized['product']
597
                );
598
            }
599
600
            foreach (array_keys(ATTRIBUTES_SETTINGS) as $key) {
601
                $value = $sanitized['attributes_'.$key];
602
                if (isset($value)) {
603
                    $extension = constant('ATTRIBUTES_' . strtoupper($key));
604
                    update_option(
605
                        ALGOWOO_DB_OPTION . $extension,
606
                        $value
607
                    );
608
                }
609
            }
610
611
            if (isset($sanitized['fields']) && (!empty($sanitized['fields']))) {
612
                foreach ($sanitized['fields'] as $key => $value) {
613
                    update_option(
614
                        ALGOWOO_DB_OPTION . BASIC_FIELD_PREFIX . $key,
615
                        $value
616
                    );
617
                }
618
            }
619
        }
620
621
        /**
622
         * Sanitize input in settings fields and filter through regex to accept only a-z and A-Z
623
         *
624
         * @param string $input Settings text data
625
         * @return array
626
         */
627
        public static function settings_fields_validate_options($input)
628
        {
629
            $valid         = array();
630
            $valid['name'] = preg_replace(
631
                '/[^a-zA-Z\s]/',
632
                '',
633
                $input['name']
634
            );
635
            return $valid;
636
        }
637
638
        /**
639
         * Load text domain for translations
640
         *
641
         * @return void
642
         */
643
        public static function load_textdomain()
644
        {
645
            load_plugin_textdomain('algolia-woo-indexer', false, basename(dirname(__FILE__)) . '/languages/');
646
        }
647
648
        /**
649
         * Add the new menu to settings section so that we can configure the plugin
650
         *
651
         * @return void
652
         */
653
        public static function admin_menu()
654
        {
655
            add_submenu_page(
656
                'options-general.php',
657
                esc_html__('Algolia Woo Indexer Settings', 'algolia-woo-indexer'),
658
                esc_html__('Algolia Woo Indexer Settings', 'algolia-woo-indexer'),
659
                'manage_options',
660
                'algolia-woo-indexer-settings',
661
                array(get_called_class(), 'algolia_woo_indexer_settings')
662
            );
663
        }
664
665
        /**
666
         * Display settings and allow user to modify them
667
         *
668
         * @return void
669
         */
670
        public static function algolia_woo_indexer_settings()
671
        {
672
            /**
673
             * Verify that the user can access the settings page
674
             */
675
            if (!current_user_can('manage_options')) {
676
                wp_die(esc_html__('Action not allowed.', 'algolia_woo_indexer_settings'));
677
            } ?>
678
            <div class="wrap">
679
                <h1><?php esc_html__('Algolia Woo Indexer Settings', 'algolia-woo-indexer'); ?></h1>
680
                <form action="<?php echo esc_url(self::$plugin_url); ?>" method="POST">
681
                    <?php
682
                    settings_fields('algolia_woo_options');
683
                    do_settings_sections('algolia_woo_indexer');
684
                    submit_button('', 'primary wide'); ?>
685
                </form>
686
                <form action="<?php echo esc_url(self::$plugin_url); ?>" method="POST">
687
                    <?php wp_nonce_field('send_products_to_algolia_nonce_action', 'send_products_to_algolia_nonce_name'); ?>
688
                    <input type="hidden" name="send_products_to_algolia" id="send_products_to_algolia" value="true" />
689
                    <?php submit_button(esc_html__('Send products to Algolia', 'algolia_woo_indexer_settings'), 'primary wide', '', false); ?>
690
                </form>
691
            </div>
692
<?php
693
        }
694
695
        /**
696
         * Get active object instance
697
         *
698
         * @return object
699
         */
700
        public static function get_instance()
701
        {
702
            if (!self::$instance) {
703
                self::$instance = new Algolia_Woo_Indexer();
704
            }
705
            return self::$instance;
706
        }
707
708
        /**
709
         * The actions to execute when the plugin is activated.
710
         *
711
         * @return void
712
         */
713
        public static function activate_plugin()
714
        {
715
716
            /**
717
             * Set default values for options if not already set
718
             */
719
            $auto_send = get_option(ALGOWOO_DB_OPTION . AUTOMATICALLY_SEND_NEW_PRODUCTS);
720
            $algolia_application_id          = get_option(ALGOWOO_DB_OPTION . ALGOLIA_APP_ID);
721
            $algolia_api_key                 = get_option(ALGOWOO_DB_OPTION . ALGOLIA_API_KEY);
722
            $algolia_index_name              = get_option(ALGOWOO_DB_OPTION . INDEX_NAME);
723
            $attributes_enabled              = get_option(ALGOWOO_DB_OPTION . ATTRIBUTES_ENABLED);
724
            $attributes_visibility           = get_option(ALGOWOO_DB_OPTION . ATTRIBUTES_VISIBILITY);
725
            $attributes_variation            = get_option(ALGOWOO_DB_OPTION . ATTRIBUTES_VARIATION);
726
            $attributes_list                 = get_option(ALGOWOO_DB_OPTION . ATTRIBUTES_LIST);
727
            $attributes_interp               = get_option(ALGOWOO_DB_OPTION . ATTRIBUTES_INTERP);
728
729
            if (empty($auto_send)) {
730
                add_option(
731
                    ALGOWOO_DB_OPTION . AUTOMATICALLY_SEND_NEW_PRODUCTS,
732
                    '0'
733
                );
734
            }
735
736
            if (empty($algolia_application_id)) {
737
                add_option(
738
                    ALGOWOO_DB_OPTION . ALGOLIA_APP_ID,
739
                    'Change me'
740
                );
741
            }
742
743
            if (empty($algolia_api_key)) {
744
                add_option(
745
                    ALGOWOO_DB_OPTION . ALGOLIA_API_KEY,
746
                    'Change me'
747
                );
748
            }
749
750
            if (empty($algolia_index_name)) {
751
                add_option(
752
                    ALGOWOO_DB_OPTION . INDEX_NAME,
753
                    'Change me'
754
                );
755
            }
756
757
            if (empty($attributes_enabled)) {
758
                add_option(
759
                    ALGOWOO_DB_OPTION . ATTRIBUTES_ENABLED,
760
                    0
761
                );
762
            }
763
764
            if (empty($attributes_visibility)) {
765
                add_option(
766
                    ALGOWOO_DB_OPTION . ATTRIBUTES_VISIBILITY,
767
                    ATTRIBUTES_VISIBILITY_STATES[1]
768
                );
769
            }
770
            if (empty($attributes_variation)) {
771
                add_option(
772
                    ALGOWOO_DB_OPTION . ATTRIBUTES_VARIATION,
773
                    ATTRIBUTES_VARIATION_STATES[0]
774
                );
775
            }
776
            if (empty($attributes_list)) {
777
                add_option(
778
                    ALGOWOO_DB_OPTION . ATTRIBUTES_LIST,
779
                    ''
780
                );
781
            }
782
            if (empty($attributes_interp)) {
783
                add_option(
784
                    ALGOWOO_DB_OPTION . ATTRIBUTES_INTERP,
785
                    ''
786
                );
787
            }
788
789
            /**
790
             * Set default values for index fields if not already set
791
             */
792
            foreach (BASIC_FIELDS as $field) {
793
                $value = get_option(ALGOWOO_DB_OPTION . BASIC_FIELD_PREFIX . $field);
794
                if (empty($value)) {
795
                    update_option(
796
                        ALGOWOO_DB_OPTION . BASIC_FIELD_PREFIX . $field,
797
                        '1'
798
                    );
799
                }
800
            }
801
802
803
804
            set_transient(self::PLUGIN_TRANSIENT, true);
805
        }
806
807
        /**
808
         * The actions to execute when the plugin is deactivated.
809
         *
810
         * @return void
811
         */
812
        public static function deactivate_plugin()
813
        {
814
            delete_transient(self::PLUGIN_TRANSIENT);
815
        }
816
    }
817
}
818