| Total Complexity | 57 |
| Total Lines | 457 |
| Duplicated Lines | 0 % |
| Changes | 1 | ||
| 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 |
||
| 57 | class Algolia_Attributes |
||
| 58 | { |
||
| 59 | |||
| 60 | |||
| 61 | /** |
||
| 62 | * Class instance |
||
| 63 | * |
||
| 64 | * @var object |
||
| 65 | */ |
||
| 66 | private static $instance; |
||
| 67 | |||
| 68 | |||
| 69 | /** |
||
| 70 | * Setup sections and fields to store and retrieve values from Settings API |
||
| 71 | * |
||
| 72 | * @return void |
||
| 73 | */ |
||
| 74 | public static function setup_attributes_settings() |
||
| 75 | { |
||
| 76 | /**" |
||
| 77 | * Make sure we reference the instance of the current class by using self::get_instance() |
||
| 78 | * This way we can setup the correct callback function for add_settings_section and add_settings_field |
||
| 79 | */ |
||
| 80 | $algowoo_attributes = self::get_instance(); |
||
| 81 | |||
| 82 | /** |
||
| 83 | * Add sections and fields for the attributes |
||
| 84 | */ |
||
| 85 | add_settings_section( |
||
| 86 | 'algolia_woo_indexer_attributes', |
||
| 87 | esc_html__('Attributes indexing settings', 'algolia-woo-indexer'), |
||
| 88 | array($algowoo_attributes, 'algolia_woo_indexer_attributes_section_text'), |
||
| 89 | 'algolia_woo_indexer' |
||
| 90 | ); |
||
| 91 | |||
| 92 | /** |
||
| 93 | * Add fields based on ATTRIBUTES_SETTINGS |
||
| 94 | */ |
||
| 95 | foreach (ATTRIBUTES_SETTINGS as $key => $description) { |
||
| 96 | add_settings_field( |
||
| 97 | 'algolia_woo_indexer_attributes_' . $key, |
||
| 98 | esc_html__($description, 'algolia-woo-indexer'), |
||
| 99 | array($algowoo_attributes, 'algolia_woo_indexer_attributes_' . $key . '_output'), |
||
| 100 | 'algolia_woo_indexer', |
||
| 101 | 'algolia_woo_indexer_attributes' |
||
| 102 | ); |
||
| 103 | } |
||
| 104 | } |
||
| 105 | |||
| 106 | /** |
||
| 107 | * Output for attributes if functionality is enabled |
||
| 108 | * |
||
| 109 | * @return void |
||
| 110 | */ |
||
| 111 | public static function algolia_woo_indexer_attributes_enabled_output() |
||
| 112 | { |
||
| 113 | $value = get_option(ALGOWOO_DB_OPTION . ATTRIBUTES_ENABLED); |
||
| 114 | $isChecked = (!empty($value)) ? 1 : 0; |
||
| 115 | ?> |
||
| 116 | <input id="algolia_woo_indexer_attributes_enabled" name="algolia_woo_indexer_attributes_enabled[checked]" type="checkbox" <?php checked(1, $isChecked); ?> /> |
||
| 117 | <?php |
||
| 118 | } |
||
| 119 | |||
| 120 | /** |
||
| 121 | * Output for attributes how to handle visibility setting |
||
| 122 | * |
||
| 123 | * @return void |
||
| 124 | */ |
||
| 125 | public static function algolia_woo_indexer_attributes_visibility_output() |
||
| 126 | { |
||
| 127 | $value = get_option(ALGOWOO_DB_OPTION . ATTRIBUTES_VISIBILITY); |
||
| 128 | foreach (ATTRIBUTES_VISIBILITY_STATES as $state) { |
||
| 129 | $id = 'algolia_woo_indexer_attributes_visibility_' . $state; |
||
| 130 | ?> |
||
| 131 | <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> |
||
| 132 | <?php |
||
| 133 | } |
||
| 134 | } |
||
| 135 | |||
| 136 | /** |
||
| 137 | * Output for attributes how to handle variant setting |
||
| 138 | * |
||
| 139 | * @return void |
||
| 140 | */ |
||
| 141 | public static function algolia_woo_indexer_attributes_variation_output() |
||
| 142 | { |
||
| 143 | $value = get_option(ALGOWOO_DB_OPTION . ATTRIBUTES_VARIATION); |
||
| 144 | foreach (ATTRIBUTES_VARIATION_STATES as $state) { |
||
| 145 | $id = 'algolia_woo_indexer_attributes_variation_' . $state; |
||
| 146 | ?> |
||
| 147 | <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> |
||
| 148 | <?php |
||
| 149 | } |
||
| 150 | } |
||
| 151 | |||
| 152 | /** |
||
| 153 | * Output for attributes list which attributes are whitelisted |
||
| 154 | * |
||
| 155 | * @return void |
||
| 156 | */ |
||
| 157 | public static function algolia_woo_indexer_attributes_list_output() |
||
| 158 | { |
||
| 159 | $value = get_option(ALGOWOO_DB_OPTION . ATTRIBUTES_LIST); |
||
| 160 | $selectedIds = explode(",", $value); |
||
|
|
|||
| 161 | $name = "algolia_woo_indexer_attributes_list[list]"; |
||
| 162 | $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'); |
||
| 163 | self::generic_attributes_select_output($name, $selectedIds, $description); |
||
| 164 | } |
||
| 165 | |||
| 166 | /** |
||
| 167 | * Output for attributes list which are using a numeric interpolation |
||
| 168 | * |
||
| 169 | * @return void |
||
| 170 | */ |
||
| 171 | public static function algolia_woo_indexer_attributes_interp_output() |
||
| 172 | { |
||
| 173 | $value = get_option(ALGOWOO_DB_OPTION . ATTRIBUTES_INTERP); |
||
| 174 | $selectedIds = explode(",", $value); |
||
| 175 | $name = "algolia_woo_indexer_attributes_interp[list]"; |
||
| 176 | $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'); |
||
| 177 | self::generic_attributes_select_output($name, $selectedIds, $description); |
||
| 178 | } |
||
| 179 | |||
| 180 | /** |
||
| 181 | * Output for attributes list which are using a numeric interpolation |
||
| 182 | * |
||
| 183 | * @return void |
||
| 184 | */ |
||
| 185 | public static function algolia_woo_indexer_attributes_tax_fields_output() |
||
| 186 | { |
||
| 187 | $selected_raw = get_option(ALGOWOO_DB_OPTION . ATTRIBUTES_TAX_FIELDS); |
||
| 188 | $selected_entries = explode(",", $selected_raw); |
||
| 189 | $name = "algolia_woo_indexer_attributes_tax_fields[list]"; |
||
| 190 | $description = __('Select which taxonomy fields for each attribute shall be indexed', 'algolia-woo-indexer'); |
||
| 191 | $values = ALLOWED_TAXONOMIES; |
||
| 192 | ?> |
||
| 193 | <p><?php echo $description; ?></p> |
||
| 194 | <select multiple="multiple" name="<?php echo $name; ?>[]" size="<?php echo count($values); ?>"> |
||
| 195 | <?php |
||
| 196 | foreach ($values as $tax) { |
||
| 197 | $selected = in_array($tax, $selected_entries) ? ' selected="selected" ' : ''; |
||
| 198 | ?> |
||
| 199 | <option value="<?php echo $tax; ?>" <?php echo $selected; ?>> |
||
| 200 | <?php echo __($tax, 'algolia-woo-indexer'); ?> |
||
| 201 | </option> |
||
| 202 | <?php |
||
| 203 | } |
||
| 204 | ?> |
||
| 205 | </select> |
||
| 206 | <?php |
||
| 207 | } |
||
| 208 | |||
| 209 | /** |
||
| 210 | * Generic Output for attributes list where attributes are whitelisted using Woocommerce attributes taxonomies |
||
| 211 | * @param string $name id and name for select |
||
| 212 | * @param array $selected_entries will be preselected if matching with WC taxonomies |
||
| 213 | * @param string $description will be displayed on top |
||
| 214 | * |
||
| 215 | */ |
||
| 216 | public static function generic_attributes_select_output($name, $selected_entries, $description) |
||
| 244 | <?php |
||
| 245 | |||
| 246 | } |
||
| 247 | |||
| 248 | /** |
||
| 249 | * Section text for attributes settings section text |
||
| 250 | * |
||
| 251 | * @return void |
||
| 252 | */ |
||
| 253 | public static function algolia_woo_indexer_attributes_section_text() |
||
| 254 | { |
||
| 255 | echo esc_html__('Control if and how the attributes shall be indexed.', 'algolia-woo-indexer'); |
||
| 256 | } |
||
| 257 | |||
| 258 | |||
| 259 | /** |
||
| 260 | * parse, sanitize and update attribute settings in DB |
||
| 261 | * |
||
| 262 | * @return void |
||
| 263 | */ |
||
| 264 | public static function update_attribute_options() |
||
| 337 | ); |
||
| 338 | } |
||
| 339 | } |
||
| 340 | } |
||
| 341 | |||
| 342 | |||
| 343 | /** |
||
| 344 | * The actions to execute when the plugin is activated. |
||
| 345 | * |
||
| 346 | * @return void |
||
| 347 | */ |
||
| 348 | public static function activate_attributes() |
||
| 349 | { |
||
| 350 | |||
| 351 | /** |
||
| 352 | * Set default values for options if not already set |
||
| 353 | */ |
||
| 354 | $attributes_enabled = get_option(ALGOWOO_DB_OPTION . ATTRIBUTES_ENABLED); |
||
| 355 | $attributes_visibility = get_option(ALGOWOO_DB_OPTION . ATTRIBUTES_VISIBILITY); |
||
| 356 | $attributes_variation = get_option(ALGOWOO_DB_OPTION . ATTRIBUTES_VARIATION); |
||
| 357 | $attributes_list = get_option(ALGOWOO_DB_OPTION . ATTRIBUTES_LIST); |
||
| 358 | $attributes_interp = get_option(ALGOWOO_DB_OPTION . ATTRIBUTES_INTERP); |
||
| 359 | $attributes_tax_fields = get_option(ALGOWOO_DB_OPTION . ATTRIBUTES_TAX_FIELDS); |
||
| 360 | |||
| 361 | |||
| 362 | if (empty($attributes_enabled)) { |
||
| 363 | add_option( |
||
| 364 | ALGOWOO_DB_OPTION . ATTRIBUTES_ENABLED, |
||
| 365 | 1 |
||
| 366 | ); |
||
| 367 | } |
||
| 368 | |||
| 369 | if (empty($attributes_visibility)) { |
||
| 370 | add_option( |
||
| 371 | ALGOWOO_DB_OPTION . ATTRIBUTES_VISIBILITY, |
||
| 372 | ATTRIBUTES_VISIBILITY_STATES[0] |
||
| 373 | ); |
||
| 374 | } |
||
| 375 | if (empty($attributes_variation)) { |
||
| 376 | add_option( |
||
| 377 | ALGOWOO_DB_OPTION . ATTRIBUTES_VARIATION, |
||
| 378 | ATTRIBUTES_VARIATION_STATES[0] |
||
| 379 | ); |
||
| 380 | } |
||
| 381 | if (empty($attributes_list)) { |
||
| 382 | add_option( |
||
| 383 | ALGOWOO_DB_OPTION . ATTRIBUTES_LIST, |
||
| 384 | '' |
||
| 385 | ); |
||
| 386 | } |
||
| 387 | if (empty($attributes_interp)) { |
||
| 388 | add_option( |
||
| 389 | ALGOWOO_DB_OPTION . ATTRIBUTES_INTERP, |
||
| 390 | '' |
||
| 391 | ); |
||
| 392 | } |
||
| 393 | if (empty($attributes_tax_fields)) { |
||
| 394 | add_option( |
||
| 395 | ALGOWOO_DB_OPTION . ATTRIBUTES_TAX_FIELDS, |
||
| 396 | 'name,slug' |
||
| 397 | ); |
||
| 398 | } |
||
| 399 | } |
||
| 400 | /** |
||
| 401 | * Get active object instance |
||
| 402 | * |
||
| 403 | * @return object |
||
| 404 | */ |
||
| 405 | public static function get_instance() |
||
| 406 | { |
||
| 407 | if (!self::$instance) { |
||
| 408 | self::$instance = new Algolia_Attributes(); |
||
| 409 | } |
||
| 410 | return self::$instance; |
||
| 411 | } |
||
| 412 | |||
| 413 | /** |
||
| 414 | * format attributes terms according to settings in ATTRIBUTES_TAX_FIELDS |
||
| 415 | * |
||
| 416 | * @param array $terms list of woocommerce attribute taxonomy |
||
| 417 | * @return array Array with fields set in config as defined in ATTRIBUTEX_TAX_FIELDS. |
||
| 418 | */ |
||
| 419 | public static function format_product_attribute_terms($terms, $interpolateValues) |
||
| 420 | { |
||
| 421 | $allowed_keys_raw = get_option(ALGOWOO_DB_OPTION . ATTRIBUTES_TAX_FIELDS); |
||
| 422 | $allowed_keys = explode(',', $allowed_keys_raw); |
||
| 423 | $final_terms = array(); |
||
| 424 | |||
| 425 | switch ($interpolateValues) { |
||
| 426 | case true: |
||
| 427 | $integers = array(); |
||
| 428 | foreach ($terms as $term) { |
||
| 429 | array_push($integers, (int) $term->name); |
||
| 430 | } |
||
| 431 | if (count($integers) > 0) { |
||
| 432 | for ($i = min($integers); $i <= max($integers); $i++) { |
||
| 433 | array_push($final_terms, $i); |
||
| 434 | } |
||
| 435 | } |
||
| 436 | break; |
||
| 437 | /** |
||
| 438 | * normal mixed content case |
||
| 439 | */ |
||
| 440 | default: |
||
| 441 | foreach ($terms as $term) { |
||
| 442 | $final_term = array(); |
||
| 443 | foreach ($allowed_keys as $key) { |
||
| 444 | $final_term[$key] = esc_html($term->{$key}); |
||
| 445 | } |
||
| 446 | array_push($final_terms, $final_term); |
||
| 447 | } |
||
| 448 | } |
||
| 449 | return $final_terms; |
||
| 450 | } |
||
| 451 | /** |
||
| 452 | * Get attributes from product |
||
| 453 | * |
||
| 454 | * @param mixed $product Product to check |
||
| 455 | * @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. |
||
| 456 | */ |
||
| 457 | public static function get_product_attributes($product) |
||
| 514 | } |
||
| 515 | } |
||
| 516 | } |
||
| 517 |