| Total Complexity | 84 |
| Total Lines | 378 |
| Duplicated Lines | 0 % |
| Changes | 2 | ||
| Bugs | 0 | Features | 0 |
Complex classes like Post_Connections 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 Post_Connections, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 8 | class Post_Connections { |
||
|
|
|||
| 9 | |||
| 10 | /** |
||
| 11 | * Holds class instance |
||
| 12 | * |
||
| 13 | * @since 1.0.0 |
||
| 14 | * |
||
| 15 | * @var object \lsx\search\classes\facetwp\Post_Connections() |
||
| 16 | */ |
||
| 17 | protected static $instance = null; |
||
| 18 | |||
| 19 | /** |
||
| 20 | * Holds the plugin options. |
||
| 21 | * |
||
| 22 | * @var array |
||
| 23 | */ |
||
| 24 | public $options = array(); |
||
| 25 | |||
| 26 | /** |
||
| 27 | * Constructor |
||
| 28 | */ |
||
| 29 | public function __construct() { |
||
| 30 | $this->get_cmb2_options(); |
||
| 31 | add_filter( 'facetwp_indexer_row_data', array( $this, 'facetwp_index_row_data' ), 10, 2 ); |
||
| 32 | add_filter( 'facetwp_index_row', array( $this, 'facetwp_index_row' ), 10, 2 ); |
||
| 33 | add_filter( 'facetwp_facet_html', array( $this, 'destination_facet_html' ), 10, 2 ); |
||
| 34 | } |
||
| 35 | |||
| 36 | /** |
||
| 37 | * Gets the cmb2 options. |
||
| 38 | * |
||
| 39 | * @return void |
||
| 40 | */ |
||
| 41 | private function get_cmb2_options() { |
||
| 42 | $cmb2_options = get_option( 'lsx-search-settings' ); |
||
| 43 | if ( false !== $cmb2_options && ! empty( $cmb2_options ) ) { |
||
| 44 | $this->options['display'] = $cmb2_options; |
||
| 45 | foreach ( $this->options['display'] as $option_key => $option_value ) { |
||
| 46 | if ( is_array( $option_value ) && ! empty( $option_value ) ) { |
||
| 47 | $new_values = array(); |
||
| 48 | foreach ( $option_value as $empty_key => $key_value ) { |
||
| 49 | $new_values[ $key_value ] = 'on'; |
||
| 50 | } |
||
| 51 | $this->options['display'][ $option_key ] = $new_values; |
||
| 52 | } |
||
| 53 | } |
||
| 54 | } |
||
| 55 | |||
| 56 | /*print_r('<pre>'); |
||
| 57 | print_r($this->options); |
||
| 58 | print_r('</pre>'); |
||
| 59 | die();*/ |
||
| 60 | } |
||
| 61 | |||
| 62 | /** |
||
| 63 | * Return an instance of this class. |
||
| 64 | * |
||
| 65 | * @since 1.0.0 |
||
| 66 | * |
||
| 67 | * @return object \lsx\search\classes\facetwp\Post_Connections() A single instance of this class. |
||
| 68 | */ |
||
| 69 | public static function get_instance() { |
||
| 70 | // If the single instance hasn't been set, set it now. |
||
| 71 | if ( null === self::$instance ) { |
||
| 72 | self::$instance = new self(); |
||
| 73 | } |
||
| 74 | return self::$instance; |
||
| 75 | } |
||
| 76 | |||
| 77 | /** |
||
| 78 | * Alter the rows and include extra facets rows for the continents |
||
| 79 | */ |
||
| 80 | public function facetwp_index_row_data( $rows, $params ) { |
||
| 81 | switch ( $params['facet']['source'] ) { |
||
| 82 | case 'cf/destination_to_tour': |
||
| 83 | case 'cf/destination_to_accommodation': |
||
| 84 | $countries = array(); |
||
| 85 | |||
| 86 | foreach ( $rows as $r_index => $row ) { |
||
| 87 | $parent = wp_get_post_parent_id( $row['facet_value'] ); |
||
| 88 | $rows[ $r_index ]['parent_id'] = $parent; |
||
| 89 | |||
| 90 | if ( 0 === $parent || '0' === $parent ) { |
||
| 91 | if ( ! isset( $countries[ $r_index ] ) ) { |
||
| 92 | $countries[ $r_index ] = $row['facet_value']; |
||
| 93 | } |
||
| 94 | |||
| 95 | if ( ! empty( $this->options['display']['engine_search_continent_filter'] ) ) { |
||
| 96 | $rows[ $r_index ]['depth'] = 1; |
||
| 97 | } else { |
||
| 98 | $rows[ $r_index ]['depth'] = 0; |
||
| 99 | } |
||
| 100 | } else { |
||
| 101 | if ( ! empty( $this->options['display']['engine_search_continent_filter'] ) ) { |
||
| 102 | $rows[ $r_index ]['depth'] = 2; |
||
| 103 | } else { |
||
| 104 | $rows[ $r_index ]['depth'] = 1; |
||
| 105 | } |
||
| 106 | } |
||
| 107 | } |
||
| 108 | if ( ! empty( $this->options['display']['enable_search_continent_filter'] ) ) { |
||
| 109 | if ( ! empty( $countries ) ) { |
||
| 110 | foreach ( $countries as $row_index => $country ) { |
||
| 111 | $continents = wp_get_object_terms( $country, 'continent' ); |
||
| 112 | $continent_id = 0; |
||
| 113 | |||
| 114 | if ( ! is_wp_error( $continents ) ) { |
||
| 115 | $new_row = $params['defaults']; |
||
| 116 | if ( ! is_array( $continents ) ) { |
||
| 117 | $continents = array( $continents ); |
||
| 118 | } |
||
| 119 | |||
| 120 | foreach ( $continents as $continent ) { |
||
| 121 | $new_row['facet_value'] = $continent->slug; |
||
| 122 | $new_row['facet_display_value'] = $continent->name; |
||
| 123 | $continent_id = $continent->term_id; |
||
| 124 | $new_row['depth'] = 0; |
||
| 125 | } |
||
| 126 | $rows[] = $new_row; |
||
| 127 | $rows[ $row_index ]['parent_id'] = $continent_id; |
||
| 128 | } |
||
| 129 | } |
||
| 130 | } |
||
| 131 | } |
||
| 132 | |||
| 133 | break; |
||
| 134 | |||
| 135 | default: |
||
| 136 | break; |
||
| 137 | } |
||
| 138 | |||
| 139 | return $rows; |
||
| 140 | } |
||
| 141 | |||
| 142 | /** |
||
| 143 | * Displays the destination specific settings |
||
| 144 | */ |
||
| 145 | public function facetwp_index_row( $params, $class ) { |
||
| 146 | $custom_field = false; |
||
| 147 | $meta_key = false; |
||
| 148 | |||
| 149 | preg_match( '/cf\//', $class->facet['source'], $custom_field ); |
||
| 150 | preg_match( '/_to_/', $class->facet['source'], $meta_key ); |
||
| 151 | |||
| 152 | if ( ! empty( $custom_field ) && ! empty( $meta_key ) ) { |
||
| 153 | |||
| 154 | if ( ( 'cf/destination_to_accommodation' === $class->facet['source'] || 'cf/destination_to_tour' === $class->facet['source'] ) && ! empty( $this->options['display']['engine_search_continent_filter'] ) && ( '0' === (string) $params['depth'] ) ) { |
||
| 155 | $title = ''; |
||
| 156 | } else { |
||
| 157 | $title = get_the_title( $params['facet_value'] ); |
||
| 158 | if ( '' !== $title ) { |
||
| 159 | $params['facet_display_value'] = $title; |
||
| 160 | } |
||
| 161 | if ( '' === $title && ! empty( $meta_key ) ) { |
||
| 162 | $params['facet_value'] = ''; |
||
| 163 | } |
||
| 164 | } |
||
| 165 | } |
||
| 166 | |||
| 167 | // If its a price, save the value as a standard number. |
||
| 168 | if ( 'cf/price' === $class->facet['source'] ) { |
||
| 169 | $params['facet_value'] = preg_replace( '/[^0-9.]/', '', $params['facet_value'] ); |
||
| 170 | $params['facet_value'] = ltrim( $params['facet_value'], '.' ); |
||
| 171 | #$params['facet_value'] = number_format( (int) $params['facet_value'], 2 ); |
||
| 172 | $params['facet_display_value'] = $params['facet_value']; |
||
| 173 | } |
||
| 174 | |||
| 175 | // If its a duration, save the value as a standard number. |
||
| 176 | if ( 'cf/duration' === $class->facet['source'] ) { |
||
| 177 | $params['facet_value'] = preg_replace( '/[^0-9 ]/', '', $params['facet_value'] ); |
||
| 178 | $params['facet_value'] = trim( $params['facet_value'] ); |
||
| 179 | $params['facet_value'] = explode( ' ', $params['facet_value'] ); |
||
| 180 | $params['facet_value'] = $params['facet_value'][0]; |
||
| 181 | #$params['facet_value'] = (int) $params['facet_value']; |
||
| 182 | $params['facet_display_value'] = $params['facet_value']; |
||
| 183 | } |
||
| 184 | |||
| 185 | return $params; |
||
| 186 | } |
||
| 187 | |||
| 188 | /** |
||
| 189 | * Checks the facet source value and outputs the destination facet HTML if needed. |
||
| 190 | * |
||
| 191 | * @param string $output |
||
| 192 | * @param array $params |
||
| 193 | * @return string |
||
| 194 | */ |
||
| 195 | public function destination_facet_html( $output, $params ) { |
||
| 196 | $possible_keys = array( |
||
| 197 | 'cf/destination_to_accommodation', |
||
| 198 | 'cf/destination_to_tour', |
||
| 199 | 'cf/destination_to_special', |
||
| 200 | 'cf/destination_to_activity', |
||
| 201 | 'cf/destination_to_review', |
||
| 202 | 'cf/destination_to_vehicle', |
||
| 203 | ); |
||
| 204 | if ( in_array( $params['facet']['source'], $possible_keys ) ) { |
||
| 205 | $output = $this->destination_facet_render( $params ); |
||
| 206 | } |
||
| 207 | return $output; |
||
| 208 | } |
||
| 209 | |||
| 210 | /** |
||
| 211 | * Generate the facet HTML |
||
| 212 | */ |
||
| 213 | public function destination_facet_render( $params ) { |
||
| 214 | $facet = $params['facet']; |
||
| 215 | |||
| 216 | $output = ''; |
||
| 217 | $values = (array) $params['values']; |
||
| 218 | $selected_values = (array) $params['selected_values']; |
||
| 219 | $soft_limit = empty( $facet['soft_limit'] ) ? 0 : (int) $facet['soft_limit']; |
||
| 220 | $countries = array(); |
||
| 221 | $continents = array(); |
||
| 222 | |||
| 223 | $continent_terms = get_terms( |
||
| 224 | array( |
||
| 225 | 'taxonomy' => 'continent', |
||
| 226 | ) |
||
| 227 | ); |
||
| 228 | |||
| 229 | if ( ! is_wp_error( $continent_terms ) ) { |
||
| 230 | foreach ( $continent_terms as $continent ) { |
||
| 231 | $continents[ $continent->term_id ] = $continent->slug; |
||
| 232 | } |
||
| 233 | } |
||
| 234 | |||
| 235 | //Create a relationship of the facet value and the their depths |
||
| 236 | $depths = array(); |
||
| 237 | $parents = array(); |
||
| 238 | foreach ( $values as $value ) { |
||
| 239 | $depths[ $value['facet_value'] ] = (int) $value['depth']; |
||
| 240 | $parents[ $value['facet_value'] ] = (int) $value['parent_id']; |
||
| 241 | } |
||
| 242 | |||
| 243 | //Determine the current depth and check if the selected values parents are in the selected array. |
||
| 244 | $current_depth = 0; |
||
| 245 | $additional_values = array(); |
||
| 246 | if ( ! empty( $selected_values ) ) { |
||
| 247 | foreach ( $selected_values as $selected ) { |
||
| 248 | if ( $depths[ $selected ] > $current_depth ) { |
||
| 249 | $current_depth = $depths[ $selected ]; |
||
| 250 | } |
||
| 251 | } |
||
| 252 | $current_depth++; |
||
| 253 | } |
||
| 254 | |||
| 255 | if ( ! empty( $additional_values ) ) { |
||
| 256 | $selected_values = array_merge( $selected_values, $additional_values ); |
||
| 257 | } |
||
| 258 | |||
| 259 | // This is where the items are sorted by their depth |
||
| 260 | $sorted_values = array(); |
||
| 261 | $stored = $values; |
||
| 262 | |||
| 263 | //sort the options so |
||
| 264 | foreach ( $values as $key => $result ) { |
||
| 265 | if ( ! empty( $this->options['display']['engine_search_continent_filter'] ) ) { |
||
| 266 | if ( in_array( $result['facet_value'], $continents ) ) { |
||
| 267 | $sorted_values[] = $result; |
||
| 268 | $destinations = $this->get_countries( $stored, $result['facet_value'], $continents, '1' ); |
||
| 269 | |||
| 270 | if ( ! empty( $destinations ) ) { |
||
| 271 | foreach ( $destinations as $destination ) { |
||
| 272 | $sorted_values[] = $destination; |
||
| 273 | } |
||
| 274 | } |
||
| 275 | } |
||
| 276 | } else { |
||
| 277 | if ( '0' === $result['depth'] || 0 === $result['depth'] ) { |
||
| 278 | $sorted_values[] = $result; |
||
| 279 | $destinations = $this->get_regions( $stored, $result['facet_value'], '1' ); |
||
| 280 | |||
| 281 | if ( ! empty( $destinations ) ) { |
||
| 282 | foreach ( $destinations as $destination ) { |
||
| 283 | $sorted_values[] = $destination; |
||
| 284 | } |
||
| 285 | } |
||
| 286 | } |
||
| 287 | } |
||
| 288 | } |
||
| 289 | $values = $sorted_values; |
||
| 290 | |||
| 291 | $continent_class = ''; |
||
| 292 | $country_class = ''; |
||
| 293 | |||
| 294 | // Run through each value and output the values. |
||
| 295 | foreach ( $values as $key => $facet ) { |
||
| 296 | $depth_type = ''; |
||
| 297 | |||
| 298 | if ( ! empty( $this->options['display']['engine_search_continent_filter'] ) ) { |
||
| 299 | switch ( $facet['depth'] ) { |
||
| 300 | case '0': |
||
| 301 | $depth_type = ''; |
||
| 302 | $continent_class = in_array( $facet['facet_value'], $selected_values ) ? $depth_type .= ' continent-checked' : ''; |
||
| 303 | break; |
||
| 304 | |||
| 305 | case '1': |
||
| 306 | $depth_type = 'country' . $continent_class; |
||
| 307 | $country_class = in_array( $facet['facet_value'], $selected_values ) ? $depth_type .= ' country-checked' : ''; |
||
| 308 | break; |
||
| 309 | |||
| 310 | case '2': |
||
| 311 | $depth_type = 'region' . $continent_class . $country_class; |
||
| 312 | break; |
||
| 313 | } |
||
| 314 | } else { |
||
| 315 | switch ( $facet['depth'] ) { |
||
| 316 | case '0': |
||
| 317 | $depth_type = 'country continent-checked'; |
||
| 318 | $country_class = in_array( $facet['facet_value'], $selected_values ) ? $depth_type .= ' country-checked' : ''; |
||
| 319 | break; |
||
| 320 | |||
| 321 | case '1': |
||
| 322 | $depth_type = 'region continent-checked' . $country_class; |
||
| 323 | break; |
||
| 324 | } |
||
| 325 | } |
||
| 326 | |||
| 327 | if ( $facet['depth'] <= $current_depth ) { |
||
| 328 | $options[] = $this->format_single_facet( $key, $facet, $selected_values, $depth_type ); |
||
| 329 | } |
||
| 330 | } |
||
| 331 | |||
| 332 | if ( ! empty( $options ) ) { |
||
| 333 | $output = implode( '', $options ); |
||
| 334 | } |
||
| 335 | |||
| 336 | return $output; |
||
| 337 | } |
||
| 338 | |||
| 339 | /** |
||
| 340 | * Gets the direct countries from the array. |
||
| 341 | */ |
||
| 342 | public function get_countries( $values, $parent, $continents, $depth ) { |
||
| 343 | $children = array(); |
||
| 344 | $stored = $values; |
||
| 345 | |||
| 346 | foreach ( $values as $value ) { |
||
| 347 | if ( isset( $continents[ $value['parent_id'] ] ) && $continents[ $value['parent_id'] ] === $parent && $value['depth'] === $depth ) { |
||
| 348 | $children[] = $value; |
||
| 349 | |||
| 350 | $destinations = $this->get_regions( $stored, $value['facet_value'], '2' ); |
||
| 351 | if ( ! empty( $destinations ) ) { |
||
| 352 | foreach ( $destinations as $destination ) { |
||
| 353 | $children[] = $destination; |
||
| 354 | } |
||
| 355 | } |
||
| 356 | } |
||
| 357 | } |
||
| 358 | return $children; |
||
| 359 | } |
||
| 360 | |||
| 361 | /** |
||
| 362 | * Gets the direct regions from the array. |
||
| 363 | */ |
||
| 364 | public function get_regions( $values, $parent, $depth ) { |
||
| 365 | $children = array(); |
||
| 366 | foreach ( $values as $value ) { |
||
| 367 | if ( $value['parent_id'] === $parent && $value['depth'] === $depth ) { |
||
| 368 | $children[] = $value; |
||
| 369 | } |
||
| 370 | } |
||
| 371 | return $children; |
||
| 372 | } |
||
| 373 | |||
| 374 | public function format_single_facet( $key, $result, $selected_values, $region = '' ) { |
||
| 386 | } |
||
| 387 | } |
||
| 388 |
Classes in PHP are usually named in CamelCase.
In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. The whole name starts with a capital letter as well.
Thus the name database provider becomes
DatabaseProvider.