| Total Complexity | 42 |
| Total Lines | 446 |
| Duplicated Lines | 0 % |
| Changes | 40 | ||
| Bugs | 0 | Features | 1 |
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 |
||
| 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) |
||
| 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() |
||
| 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() |
||
| 481 | } |
||
| 482 | } |
||
| 483 | } |
||
| 484 |
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths