| Total Complexity | 51 |
| Total Lines | 428 |
| Duplicated Lines | 0 % |
| Changes | 3 | ||
| Bugs | 0 | Features | 0 |
Complex classes like Algolia_Attributes 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_Attributes, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 54 | class Algolia_Attributes |
||
| 55 | { |
||
| 56 | |||
| 57 | |||
| 58 | /** |
||
| 59 | * Class instance |
||
| 60 | * |
||
| 61 | * @var object |
||
| 62 | */ |
||
| 63 | private static $instance; |
||
| 64 | |||
| 65 | |||
| 66 | /** |
||
| 67 | * Setup sections and fields to store and retrieve values from Settings API |
||
| 68 | * |
||
| 69 | * @return void |
||
| 70 | */ |
||
| 71 | public static function setup_attributes_settings() |
||
| 72 | { |
||
| 73 | /**" |
||
| 74 | * Make sure we reference the instance of the current class by using self::get_instance() |
||
| 75 | * This way we can setup the correct callback function for add_settings_section and add_settings_field |
||
| 76 | */ |
||
| 77 | $algowoo_attributes = self::get_instance(); |
||
| 78 | |||
| 79 | /** |
||
| 80 | * Add sections and fields for the attributes |
||
| 81 | */ |
||
| 82 | add_settings_section( |
||
| 83 | 'algolia_woo_indexer_attributes', |
||
| 84 | esc_html__('Attributes indexing settings', 'algolia-woo-indexer'), |
||
| 85 | array($algowoo_attributes, 'algolia_woo_indexer_attributes_section_text'), |
||
| 86 | 'algolia_woo_indexer' |
||
| 87 | ); |
||
| 88 | |||
| 89 | /** |
||
| 90 | * Add fields based on ATTRIBUTES_SETTINGS |
||
| 91 | */ |
||
| 92 | foreach (ATTRIBUTES_SETTINGS as $key => $description) { |
||
| 93 | add_settings_field( |
||
| 94 | 'algolia_woo_indexer_attributes_' . $key, |
||
| 95 | esc_html__($description, 'algolia-woo-indexer'), |
||
| 96 | array($algowoo_attributes, 'algolia_woo_indexer_attributes_' . $key . '_output'), |
||
| 97 | 'algolia_woo_indexer', |
||
| 98 | 'algolia_woo_indexer_attributes' |
||
| 99 | ); |
||
| 100 | } |
||
| 101 | } |
||
| 102 | |||
| 103 | /** |
||
| 104 | * Output for attributes if functionality is enabled |
||
| 105 | * |
||
| 106 | * @return void |
||
| 107 | */ |
||
| 108 | public static function algolia_woo_indexer_attributes_enabled_output() |
||
| 109 | { |
||
| 110 | $value = get_option(ALGOWOO_DB_OPTION . ATTRIBUTES_ENABLED); |
||
| 111 | $isChecked = (!empty($value)) ? 1 : 0; |
||
| 112 | ?> |
||
| 113 | <input id="algolia_woo_indexer_attributes_enabled" name="algolia_woo_indexer_attributes_enabled[checked]" type="checkbox" <?php checked(1, $isChecked); ?> /> |
||
| 114 | <?php |
||
| 115 | } |
||
| 116 | |||
| 117 | /** |
||
| 118 | * Output for attributes how to handle visibility setting |
||
| 119 | * |
||
| 120 | * @return void |
||
| 121 | */ |
||
| 122 | public static function algolia_woo_indexer_attributes_visibility_output() |
||
| 123 | { |
||
| 124 | $value = get_option(ALGOWOO_DB_OPTION . ATTRIBUTES_VISIBILITY); |
||
| 125 | foreach (ATTRIBUTES_VISIBILITY_STATES as $state) { |
||
| 126 | $id = 'algolia_woo_indexer_attributes_visibility_' . $state; |
||
| 127 | ?> |
||
| 128 | <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> |
||
| 129 | <?php |
||
| 130 | } |
||
| 131 | } |
||
| 132 | |||
| 133 | /** |
||
| 134 | * Output for attributes list which attributes are whitelisted |
||
| 135 | * |
||
| 136 | * @return void |
||
| 137 | */ |
||
| 138 | public static function algolia_woo_indexer_attributes_list_output() |
||
| 139 | { |
||
| 140 | $value = get_option(ALGOWOO_DB_OPTION . ATTRIBUTES_LIST); |
||
| 141 | $selectedIds = explode(",", $value); |
||
|
|
|||
| 142 | $name = "algolia_woo_indexer_attributes_list[list]"; |
||
| 143 | $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'); |
||
| 144 | self::generic_attributes_select_output($name, $selectedIds, $description); |
||
| 145 | } |
||
| 146 | |||
| 147 | /** |
||
| 148 | * Output for attributes list which are using a numeric interpolation |
||
| 149 | * |
||
| 150 | * @return void |
||
| 151 | */ |
||
| 152 | public static function algolia_woo_indexer_attributes_interp_output() |
||
| 153 | { |
||
| 154 | $value = get_option(ALGOWOO_DB_OPTION . ATTRIBUTES_INTERP); |
||
| 155 | $selectedIds = explode(",", $value); |
||
| 156 | $name = "algolia_woo_indexer_attributes_interp[list]"; |
||
| 157 | $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'); |
||
| 158 | self::generic_attributes_select_output($name, $selectedIds, $description); |
||
| 159 | } |
||
| 160 | |||
| 161 | /** |
||
| 162 | * Output for attributes list which are using a numeric interpolation |
||
| 163 | * |
||
| 164 | * @return void |
||
| 165 | */ |
||
| 166 | public static function algolia_woo_indexer_attributes_tax_fields_output() |
||
| 167 | { |
||
| 168 | $selected_raw = get_option(ALGOWOO_DB_OPTION . ATTRIBUTES_TAX_FIELDS); |
||
| 169 | $selected_entries = explode(",", $selected_raw); |
||
| 170 | $name = "algolia_woo_indexer_attributes_tax_fields[list]"; |
||
| 171 | $description = __('Select which taxonomy fields for each attribute shall be indexed', 'algolia-woo-indexer'); |
||
| 172 | $values = ALLOWED_TAXONOMIES; |
||
| 173 | ?> |
||
| 174 | <p><?php echo $description; ?></p> |
||
| 175 | <select multiple="multiple" name="<?php echo $name; ?>[]" size="<?php echo count($values); ?>"> |
||
| 176 | <?php |
||
| 177 | foreach ($values as $tax) { |
||
| 178 | $selected = in_array($tax, $selected_entries) ? ' selected="selected" ' : ''; |
||
| 179 | ?> |
||
| 180 | <option value="<?php echo $tax; ?>" <?php echo $selected; ?>> |
||
| 181 | <?php echo __($tax, 'algolia-woo-indexer'); ?> |
||
| 182 | </option> |
||
| 183 | <?php |
||
| 184 | } |
||
| 185 | ?> |
||
| 186 | </select> |
||
| 187 | <?php |
||
| 188 | } |
||
| 189 | |||
| 190 | /** |
||
| 191 | * Generic Output for attributes list where attributes are whitelisted using Woocommerce attributes taxonomies |
||
| 192 | * @param string $name id and name for select |
||
| 193 | * @param array $selected_entries will be preselected if matching with WC taxonomies |
||
| 194 | * @param string $description will be displayed on top |
||
| 195 | * |
||
| 196 | */ |
||
| 197 | public static function generic_attributes_select_output($name, $selected_entries, $description) |
||
| 225 | <?php |
||
| 226 | |||
| 227 | } |
||
| 228 | |||
| 229 | /** |
||
| 230 | * Section text for attributes settings section text |
||
| 231 | * |
||
| 232 | * @return void |
||
| 233 | */ |
||
| 234 | public static function algolia_woo_indexer_attributes_section_text() |
||
| 235 | { |
||
| 236 | |||
| 237 | } |
||
| 238 | |||
| 239 | |||
| 240 | /** |
||
| 241 | * parse, sanitize and update attribute settings in DB |
||
| 242 | * |
||
| 243 | * @return void |
||
| 244 | */ |
||
| 245 | public static function update_attribute_options() |
||
| 316 | ); |
||
| 317 | } |
||
| 318 | } |
||
| 319 | } |
||
| 320 | |||
| 321 | |||
| 322 | /** |
||
| 323 | * The actions to execute when the plugin is activated. |
||
| 324 | * |
||
| 325 | * @return void |
||
| 326 | */ |
||
| 327 | public static function activate_attributes() |
||
| 328 | { |
||
| 329 | |||
| 330 | /** |
||
| 331 | * Set default values for options if not already set |
||
| 332 | */ |
||
| 333 | $attributes_enabled = get_option(ALGOWOO_DB_OPTION . ATTRIBUTES_ENABLED); |
||
| 334 | $attributes_visibility = get_option(ALGOWOO_DB_OPTION . ATTRIBUTES_VISIBILITY); |
||
| 335 | $attributes_list = get_option(ALGOWOO_DB_OPTION . ATTRIBUTES_LIST); |
||
| 336 | $attributes_interp = get_option(ALGOWOO_DB_OPTION . ATTRIBUTES_INTERP); |
||
| 337 | $attributes_tax_fields = get_option(ALGOWOO_DB_OPTION . ATTRIBUTES_TAX_FIELDS); |
||
| 338 | |||
| 339 | |||
| 340 | if (empty($attributes_enabled)) { |
||
| 341 | add_option( |
||
| 342 | ALGOWOO_DB_OPTION . ATTRIBUTES_ENABLED, |
||
| 343 | 1 |
||
| 344 | ); |
||
| 345 | } |
||
| 346 | if (empty($attributes_visibility)) { |
||
| 347 | add_option( |
||
| 348 | ALGOWOO_DB_OPTION . ATTRIBUTES_VISIBILITY, |
||
| 349 | ATTRIBUTES_VISIBILITY_STATES[0] |
||
| 350 | ); |
||
| 351 | } |
||
| 352 | if (empty($attributes_list)) { |
||
| 353 | add_option( |
||
| 354 | ALGOWOO_DB_OPTION . ATTRIBUTES_LIST, |
||
| 355 | '' |
||
| 356 | ); |
||
| 357 | } |
||
| 358 | if (empty($attributes_interp)) { |
||
| 359 | add_option( |
||
| 360 | ALGOWOO_DB_OPTION . ATTRIBUTES_INTERP, |
||
| 361 | '' |
||
| 362 | ); |
||
| 363 | } |
||
| 364 | if (empty($attributes_tax_fields)) { |
||
| 365 | add_option( |
||
| 366 | ALGOWOO_DB_OPTION . ATTRIBUTES_TAX_FIELDS, |
||
| 367 | 'name,slug' |
||
| 368 | ); |
||
| 369 | } |
||
| 370 | } |
||
| 371 | /** |
||
| 372 | * Get active object instance |
||
| 373 | * |
||
| 374 | * @return object |
||
| 375 | */ |
||
| 376 | public static function get_instance() |
||
| 377 | { |
||
| 378 | if (!self::$instance) { |
||
| 379 | self::$instance = new Algolia_Attributes(); |
||
| 380 | } |
||
| 381 | return self::$instance; |
||
| 382 | } |
||
| 383 | |||
| 384 | /** |
||
| 385 | * format attributes terms according to settings in ATTRIBUTES_TAX_FIELDS |
||
| 386 | * |
||
| 387 | * @param array $terms list of woocommerce attribute taxonomy |
||
| 388 | * @return array Array with fields set in config as defined in ATTRIBUTEX_TAX_FIELDS. |
||
| 389 | */ |
||
| 390 | public static function format_product_attribute_terms($terms, $interpolateValues) |
||
| 391 | { |
||
| 392 | $allowed_keys_raw = get_option(ALGOWOO_DB_OPTION . ATTRIBUTES_TAX_FIELDS); |
||
| 393 | $allowed_keys = explode(',', $allowed_keys_raw); |
||
| 394 | $final_terms = array(); |
||
| 395 | |||
| 396 | switch ($interpolateValues) { |
||
| 397 | case true: |
||
| 398 | $integers = array(); |
||
| 399 | foreach ($terms as $term) { |
||
| 400 | array_push($integers, (int) $term->name); |
||
| 401 | } |
||
| 402 | if (count($integers) > 0) { |
||
| 403 | for ($i = min($integers); $i <= max($integers); $i++) { |
||
| 404 | array_push($final_terms, $i); |
||
| 405 | } |
||
| 406 | } |
||
| 407 | break; |
||
| 408 | /** |
||
| 409 | * normal mixed content case |
||
| 410 | */ |
||
| 411 | default: |
||
| 412 | foreach ($terms as $term) { |
||
| 413 | $final_term = array(); |
||
| 414 | foreach ($allowed_keys as $key) { |
||
| 415 | array_push($final_term, esc_html($term->{$key})); |
||
| 416 | } |
||
| 417 | $string_with_Separator = implode("|", $final_term); |
||
| 418 | array_push($final_terms, $string_with_Separator); |
||
| 419 | } |
||
| 420 | } |
||
| 421 | return $final_terms; |
||
| 422 | } |
||
| 423 | |||
| 424 | /** |
||
| 425 | * skip variable related attributes, |
||
| 426 | * ensure it is a taxonomy, |
||
| 427 | * ensure that taxonomy is whitelisted and |
||
| 428 | * ensure that the visibility and variation is respected |
||
| 429 | * @param mixed $attribute Woocommerce attribute |
||
| 430 | */ |
||
| 431 | private static function is_attribute_not_allowed($attribute) |
||
| 432 | { |
||
| 433 | /** |
||
| 434 | * gather settings |
||
| 435 | */ |
||
| 436 | $setting_visibility = get_option(ALGOWOO_DB_OPTION . ATTRIBUTES_VISIBILITY); |
||
| 437 | $setting_ids = get_option(ALGOWOO_DB_OPTION . ATTRIBUTES_LIST); |
||
| 438 | $setting_ids = explode(",", $setting_ids); |
||
| 439 | $visibility = $attribute["visible"]; |
||
| 440 | $attribute_id = $attribute->get_id(); |
||
| 441 | |||
| 442 | return ($attribute->get_variation() || |
||
| 443 | !$attribute->is_taxonomy() || |
||
| 444 | !in_array($attribute_id, $setting_ids) || |
||
| 445 | ($setting_visibility === "visible" && $visibility === false) || |
||
| 446 | ($setting_visibility === "hidden" && $visibility === true) |
||
| 447 | ); |
||
| 448 | } |
||
| 449 | |||
| 450 | /** |
||
| 451 | * Get attributes from product |
||
| 452 | * |
||
| 453 | * @param mixed $product Product to check |
||
| 454 | * @return array ['pa_name' => ['value1', 'value2']] Array with key set to the product attribute internal name and values as array. returns false if not attributes found. |
||
| 455 | */ |
||
| 456 | public static function get_product_attributes($product) |
||
| 482 | } |
||
| 483 | } |
||
| 484 | } |
||
| 485 |