Passed
Branch master (c4f2b7)
by Daniel
02:01
created

Algolia_Woo_Indexer   A

Complexity

Total Complexity 42

Size/Duplication

Total Lines 446
Duplicated Lines 0 %

Importance

Changes 40
Bugs 0 Features 1
Metric Value
eloc 166
c 40
b 0
f 1
dl 0
loc 446
rs 9.0399
wmc 42

18 Methods

Rating   Name   Duplication   Size   Complexity  
A send_new_product_to_algolia() 0 6 3
A algolia_woo_indexer_section_text() 0 3 1
A maybe_send_products() 0 4 2
A __construct() 0 3 1
A algolia_woo_indexer_index_name_output() 0 7 2
A get_instance() 0 6 2
B init() 0 56 6
A setup_settings_sections() 0 57 2
A deactivate_plugin() 0 3 1
A algolia_woo_indexer_application_id_output() 0 7 2
A admin_menu() 0 9 1
A load_textdomain() 0 3 1
A algolia_woo_indexer_settings() 0 21 2
A settings_fields_validate_options() 0 9 1
A algolia_woo_indexer_automatically_send_new_products_output() 0 9 2
A update_settings_options() 0 28 6
A activate_plugin() 0 39 5
A algolia_woo_indexer_admin_api_key_output() 0 9 2

How to fix   Complexity   

Complex Class

Complex classes like Algolia_Woo_Indexer often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use Algolia_Woo_Indexer, and based on these observations, apply Extract Interface, too.

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
130
        /**
131
         * Output for admin API key field
132
         *
133
         * @see https://developer.wordpress.org/reference/functions/wp_nonce_field/
134
         *
135
         * @return void
136
         */
137
        public static function algolia_woo_indexer_admin_api_key_output()
138
        {
139
            $api_key = get_option(ALGOWOO_DB_OPTION . ALGOLIA_API_KEY);
140
            $api_key = is_string($api_key) ? $api_key : CHANGE_ME;
141
142
            wp_nonce_field('algolia_woo_indexer_admin_api_nonce_action', 'algolia_woo_indexer_admin_api_nonce_name');
143
144
            echo "<input id='algolia_woo_indexer_admin_api_key' name='algolia_woo_indexer_admin_api_key[key]'
145
				type='text' value='" . esc_attr($api_key) . "' />";
146
        }
147
148
        /**
149
         * Output for application ID field
150
         *
151
         * @return void
152
         */
153
        public static function algolia_woo_indexer_application_id_output()
154
        {
155
            $application_id = get_option(ALGOWOO_DB_OPTION . ALGOLIA_APP_ID);
156
            $application_id = is_string($application_id) ? $application_id : CHANGE_ME;
157
158
            echo "<input id='algolia_woo_indexer_application_id' name='algolia_woo_indexer_application_id[id]'
159
				type='text' value='" . esc_attr($application_id) . "' />";
160
        }
161
162
        /**
163
         * Output for index name field
164
         *
165
         * @return void
166
         */
167
        public static function algolia_woo_indexer_index_name_output()
168
        {
169
            $index_name = get_option(ALGOWOO_DB_OPTION . INDEX_NAME);
170
            $index_name = is_string($index_name) ? $index_name : CHANGE_ME;
171
172
            echo "<input id='algolia_woo_indexer_index_name' name='algolia_woo_indexer_index_name[name]'
173
				type='text' value='" . esc_attr($index_name) . "' />";
174
        }
175
176
        /**
177
         * Output for checkbox to check if we automatically send new products to Algolia
178
         *
179
         * @return void
180
         */
181
        public static function algolia_woo_indexer_automatically_send_new_products_output()
182
        {
183
            /**
184
             * Sanitization is not really needed as the variable is not directly echoed
185
             * But I have still done it to be 100% safe
186
             */
187
            $auto_send = get_option(ALGOWOO_DB_OPTION . AUTOMATICALLY_SEND_NEW_PRODUCTS);
188
            $auto_send = (!empty($auto_send)) ? 1 : 0; ?>
189
            <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); ?> />
190
        <?php
191
        }
192
193
        /**
194
         * Section text for plugin settings section text
195
         *
196
         * @return void
197
         */
198
        public static function algolia_woo_indexer_section_text()
199
        {
200
            echo esc_html__('Enter your settings here', 'algolia-woo-indexer');
201
        }
202
203
        /**
204
         * Check if we are going to send products by verifying send products nonce
205
         *
206
         * @return void
207
         */
208
        public static function maybe_send_products()
209
        {
210
            if (true === Algolia_Verify_Nonces::verify_send_products_nonce()) {
211
                Algolia_Send_Products::send_products_to_algolia();
212
            }
213
        }
214
215
        /**
216
         * Initialize class, setup settings sections and fields
217
         *
218
         * @return void
219
         */
220
        public static function init()
221
        {
222
223
            /**
224
             * Fetch the option to see if we are going to automatically send new products
225
             */
226
            $auto_send = get_option(ALGOWOO_DB_OPTION . AUTOMATICALLY_SEND_NEW_PRODUCTS);
227
228
            /**
229
             * Check that we have the minimum versions required and all of the required PHP extensions
230
             */
231
            Algolia_Check_Requirements::check_unmet_requirements();
232
233
            if (!Algolia_Check_Requirements::algolia_wp_version_check() || !Algolia_Check_Requirements::algolia_php_version_check()) {
234
                add_action(
235
                    'admin_notices',
236
                    function () {
237
                        echo '<div class="error notice">
238
                                  <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>
239
                                </div>';
240
                    }
241
                );
242
            }
243
244
            $ob_class = get_called_class();
245
246
            /**
247
             * Setup translations
248
             */
249
            add_action('plugins_loaded', array($ob_class, 'load_textdomain'));
250
251
            /**
252
             * Add actions to setup admin menu
253
             */
254
            if (is_admin()) {
255
                add_action('admin_menu', array($ob_class, 'admin_menu'));
256
                add_action('admin_init', array($ob_class, 'setup_settings_sections'));
257
                add_action('admin_init', array($ob_class, 'update_settings_options'));
258
                add_action('admin_init', array($ob_class, 'maybe_send_products'));
259
260
                /**
261
                 * Register hook to automatically send new products if the option is set
262
                 */
263
264
                if ('1' === $auto_send) {
265
                    add_action('save_post', array($ob_class, 'send_new_product_to_algolia'), 10, 3);
266
                }
267
268
                self::$plugin_url = admin_url('options-general.php?page=algolia-woo-indexer-settings');
269
270
                if (!is_plugin_active('woocommerce/woocommerce.php')) {
271
                    add_action(
272
                        'admin_notices',
273
                        function () {
274
                            echo '<div class="error notice">
275
								  <p>' . esc_html__('WooCommerce plugin must be enabled for Algolia Woo Indexer to work.', 'algolia-woo-indexer') . '</p>
276
								</div>';
277
                        }
278
                    );
279
                }
280
            }
281
        }
282
283
        /**
284
         * Send a single product to Algolia once a new product has been published
285
         *
286
         * @param int   $post_id ID of the product.
287
         * @param array $post Post array.
288
         *
289
         * @return void
290
         */
291
        public static function send_new_product_to_algolia($post_id, $post)
292
        {
293
            if ('publish' !== $post->post_status || 'product' !== $post->post_type) {
294
                return;
295
            }
296
            Algolia_Send_Products::send_products_to_algolia($post_id);
297
        }
298
299
        /**
300
         * Verify nonces before we update options and settings
301
         * Also retrieve the value from the send_products_to_algolia hidden field to check if we are sending products to Algolia
302
         *
303
         * @return void
304
         */
305
        public static function update_settings_options()
306
        {
307
            Algolia_Verify_Nonces::verify_settings_nonce();
308
309
            if (Algolia_Verify_Nonces::verify_send_products_nonce()) {
310
                return;
311
            }
312
313
            $application_id = filter_input(INPUT_POST, 'algolia_woo_indexer_application_id', FILTER_DEFAULT, FILTER_REQUIRE_ARRAY);
314
            $api_key = filter_input(INPUT_POST, 'algolia_woo_indexer_admin_api_key', FILTER_DEFAULT, FILTER_REQUIRE_ARRAY);
315
            $index_name = filter_input(INPUT_POST, 'algolia_woo_indexer_index_name', FILTER_DEFAULT, FILTER_REQUIRE_ARRAY);
316
            $auto_send = filter_input(INPUT_POST, 'algolia_woo_indexer_automatically_send_new_products', FILTER_DEFAULT, FILTER_REQUIRE_ARRAY);
317
318
            $sanitized_app_id = sanitize_text_field($application_id['id']);
319
            $sanitized_api_key = sanitize_text_field($api_key['key']);
320
            $sanitized_index_name = sanitize_text_field($index_name['name']);
321
            $sanitized_auto_send = (!empty($auto_send)) ? 1 : 0;
322
323
            $options = [
324
                ALGOWOO_DB_OPTION . ALGOLIA_APP_ID => $sanitized_app_id,
325
                ALGOWOO_DB_OPTION . ALGOLIA_API_KEY => $sanitized_api_key,
326
                ALGOWOO_DB_OPTION . INDEX_NAME => $sanitized_index_name,
327
                ALGOWOO_DB_OPTION . AUTOMATICALLY_SEND_NEW_PRODUCTS => $sanitized_auto_send,
328
            ];
329
330
            foreach ($options as $option_key => $option_value) {
331
                if (isset($option_value) && (!empty($option_value))) {
332
                    update_option($option_key, $option_value);
333
                }
334
            }
335
        }
336
337
338
339
340
        /**
341
         * Sanitize input in settings fields and filter through regex to accept only a-z and A-Z
342
         *
343
         * @param string $input Settings text data
344
         * @return array
345
         */
346
        public static function settings_fields_validate_options($input)
347
        {
348
            $valid         = array();
349
            $valid['name'] = preg_replace(
350
                '/[^a-zA-Z\s]/',
351
                '',
352
                $input['name']
353
            );
354
            return $valid;
355
        }
356
357
        /**
358
         * Load text domain for translations
359
         *
360
         * @return void
361
         */
362
        public static function load_textdomain()
363
        {
364
            load_plugin_textdomain('algolia-woo-indexer', false, basename(dirname(__FILE__)) . '/languages/');
365
        }
366
367
        /**
368
         * Add the new menu to settings section so that we can configure the plugin
369
         *
370
         * @return void
371
         */
372
        public static function admin_menu()
373
        {
374
            add_submenu_page(
375
                'options-general.php',
376
                esc_html__('Algolia Woo Indexer Settings', 'algolia-woo-indexer'),
377
                esc_html__('Algolia Woo Indexer Settings', 'algolia-woo-indexer'),
378
                'manage_options',
379
                'algolia-woo-indexer-settings',
380
                array(get_called_class(), 'algolia_woo_indexer_settings')
381
            );
382
        }
383
384
        /**
385
         * Display settings and allow user to modify them
386
         *
387
         * @return void
388
         */
389
        public static function algolia_woo_indexer_settings()
390
        {
391
            /**
392
             * Verify that the user can access the settings page
393
             */
394
            if (!current_user_can('manage_options')) {
395
                wp_die(esc_html__('Action not allowed.', 'algolia_woo_indexer_settings'));
396
            } ?>
397
            <div class="wrap">
398
                <h1><?php esc_html__('Algolia Woo Indexer Settings', 'algolia-woo-indexer'); ?></h1>
399
                <form action="<?php echo esc_url(self::$plugin_url); ?>" method="POST">
400
                    <?php
401
                    settings_fields('algolia_woo_options');
402
                    do_settings_sections('algolia_woo_indexer');
403
                    submit_button('', 'primary wide'); ?>
404
                </form>
405
                <form action="<?php echo esc_url(self::$plugin_url); ?>" method="POST">
406
                    <?php wp_nonce_field('send_products_to_algolia_nonce_action', 'send_products_to_algolia_nonce_name'); ?>
407
                    <input type="hidden" name="send_products_to_algolia" id="send_products_to_algolia" value="true" />
408
                    <?php submit_button(esc_html__('Send products to Algolia', 'algolia_woo_indexer_settings'), 'primary wide', '', false); ?>
409
                </form>
410
            </div>
411
<?php
412
        }
413
414
        /**
415
         * Get active object instance
416
         *
417
         * @return object
418
         */
419
        public static function get_instance()
420
        {
421
            if (!self::$instance) {
422
                self::$instance = new Algolia_Woo_Indexer();
423
            }
424
            return self::$instance;
425
        }
426
427
        /**
428
         * The actions to execute when the plugin is activated.
429
         *
430
         * @return void
431
         */
432
        public static function activate_plugin()
433
        {
434
435
            /**
436
             * Set default values for options if not already set
437
             */
438
            $auto_send = get_option(ALGOWOO_DB_OPTION . AUTOMATICALLY_SEND_NEW_PRODUCTS);
439
            $algolia_application_id          = get_option(ALGOWOO_DB_OPTION . ALGOLIA_APP_ID);
440
            $algolia_api_key                 = get_option(ALGOWOO_DB_OPTION . ALGOLIA_API_KEY);
441
            $algolia_index_name              = get_option(ALGOWOO_DB_OPTION . INDEX_NAME);
442
443
            if (empty($auto_send)) {
444
                add_option(
445
                    ALGOWOO_DB_OPTION . AUTOMATICALLY_SEND_NEW_PRODUCTS,
446
                    '0'
447
                );
448
            }
449
450
            if (empty($algolia_application_id)) {
451
                add_option(
452
                    ALGOWOO_DB_OPTION . ALGOLIA_APP_ID,
453
                    'Change me'
454
                );
455
            }
456
457
            if (empty($algolia_api_key)) {
458
                add_option(
459
                    ALGOWOO_DB_OPTION . ALGOLIA_API_KEY,
460
                    'Change me'
461
                );
462
            }
463
464
            if (empty($algolia_index_name)) {
465
                add_option(
466
                    ALGOWOO_DB_OPTION . INDEX_NAME,
467
                    'Change me'
468
                );
469
            }
470
            set_transient(self::PLUGIN_TRANSIENT, true);
471
        }
472
473
        /**
474
         * The actions to execute when the plugin is deactivated.
475
         *
476
         * @return void
477
         */
478
        public static function deactivate_plugin()
479
        {
480
            delete_transient(self::PLUGIN_TRANSIENT);
481
        }
482
    }
483
}
484