Complex classes like wpsc_checkout 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. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
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 wpsc_checkout, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 149 | class wpsc_checkout { |
||
| 150 | |||
| 151 | // The checkout loop variables |
||
| 152 | var $checkout_items = array( ); |
||
| 153 | var $checkout_item; |
||
| 154 | var $checkout_item_count = 0; |
||
| 155 | var $current_checkout_item = -1; |
||
| 156 | var $in_the_loop = false; |
||
| 157 | //the ticket additions |
||
| 158 | var $additional_fields = array(); |
||
| 159 | var $formfield_count = 0; |
||
| 160 | |||
| 161 | /** |
||
| 162 | * wpsc_checkout method, gets the tax rate as a percentage, based on the selected country and region |
||
| 163 | * @access public |
||
| 164 | */ |
||
| 165 | public function __construct( $checkout_set = 0 ) { |
||
| 166 | global $wpdb; |
||
| 167 | $this->checkout_items = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM `" . WPSC_TABLE_CHECKOUT_FORMS . "` WHERE `active` = '1' AND `checkout_set`= %s ORDER BY `checkout_order`;", $checkout_set ) ); |
||
| 168 | |||
| 169 | $GLOBALS['wpsc_checkout_error_messages' ] = wpsc_get_customer_meta( 'checkout_error_messages' ); |
||
| 170 | $GLOBALS['wpsc_gateway_error_messages' ] = wpsc_get_customer_meta( 'gateway_error_messages' ); |
||
| 171 | $GLOBALS['wpsc_registration_error_messages'] = wpsc_get_customer_meta( 'registration_error_messages' ); |
||
| 172 | $GLOBALS['wpsc_customer_checkout_details' ] = apply_filters( 'wpsc_get_customer_checkout_details', wpsc_get_customer_meta( 'checkout_details' ) ); |
||
| 173 | |||
| 174 | // legacy filter |
||
| 175 | if ( is_user_logged_in() ) |
||
| 176 | $GLOBALS['wpsc_customer_checkout_details'] = apply_filters( 'wpsc_checkout_user_profile_get', $GLOBALS['wpsc_customer_checkout_details'], get_current_user_id() ); |
||
| 177 | |||
| 178 | if ( ! is_array( $GLOBALS['wpsc_customer_checkout_details'] ) ) |
||
| 179 | $GLOBALS['wpsc_customer_checkout_details'] = array(); |
||
| 180 | |||
| 181 | $category_list = wpsc_cart_item_categories( true ); |
||
| 182 | $additional_form_list = array( ); |
||
| 183 | foreach ( $category_list as $category_id ) { |
||
| 184 | $additional_form_list[] = wpsc_get_categorymeta( $category_id, 'use_additional_form_set' ); |
||
| 185 | } |
||
| 186 | if ( function_exists( 'wpsc_get_ticket_checkout_set' ) ) { |
||
| 187 | $checkout_form_fields_id = array_search( wpsc_get_ticket_checkout_set(), $additional_form_list ); |
||
| 188 | unset( $additional_form_list[$checkout_form_fields_id] ); |
||
| 189 | } |
||
| 190 | if ( count( $additional_form_list ) > 0 ) { |
||
| 191 | $this->category_checkout_items = $wpdb->get_results( "SELECT * FROM `" . WPSC_TABLE_CHECKOUT_FORMS . "` WHERE `active` = '1' AND `checkout_set` IN ('" . implode( "','", $additional_form_list ) . "') ORDER BY `checkout_set`, `checkout_order`;" ); |
||
| 192 | $this->checkout_items = array_merge( (array)$this->checkout_items, (array)$this->category_checkout_items ); |
||
| 193 | } |
||
| 194 | if ( function_exists( 'wpsc_get_ticket_checkout_set' ) ) { |
||
| 195 | $sql = "SELECT * FROM `" . WPSC_TABLE_CHECKOUT_FORMS . "` WHERE `active` = '1' AND `checkout_set`='" . wpsc_get_ticket_checkout_set() . "' ORDER BY `checkout_order`;"; |
||
| 196 | $this->additional_fields = $wpdb->get_results( $sql ); |
||
| 197 | $count = wpsc_ticket_checkoutfields(); |
||
| 198 | $j = 1; |
||
| 199 | $fields = $this->additional_fields; |
||
| 200 | $this->formfield_count = count( $fields ) + $this->checkout_item_count; |
||
| 201 | while ( $j < $count ) { |
||
| 202 | $this->additional_fields = array_merge( (array)$this->additional_fields, (array)$fields ); |
||
| 203 | $j++; |
||
| 204 | } |
||
| 205 | if ( wpsc_ticket_checkoutfields() > 0 ) { |
||
| 206 | $this->checkout_items = array_merge( (array)$this->checkout_items, (array)$this->additional_fields ); |
||
| 207 | } |
||
| 208 | } |
||
| 209 | |||
| 210 | $this->checkout_items = apply_filters( 'wpsc_checkout_fields', $this->checkout_items, $this ); |
||
| 211 | |||
| 212 | $this->checkout_item_count = count( $this->checkout_items ); |
||
| 213 | } |
||
| 214 | |||
| 215 | function form_name() { |
||
| 216 | if ( $this->form_name_is_required() && ($this->checkout_item->type != 'heading') ) |
||
| 217 | return esc_html( apply_filters( 'wpsc_checkout_field_name', $this->checkout_item->name ) ) . ' <span class="asterix">*</span> '; |
||
| 218 | else |
||
| 219 | return esc_html( apply_filters( 'wpsc_checkout_field_name', $this->checkout_item->name ) ); |
||
| 220 | } |
||
| 221 | |||
| 222 | function form_name_is_required() { |
||
| 225 | |||
| 226 | function form_element_active() { |
||
| 229 | |||
| 230 | /** |
||
| 231 | * form_element_id method, returns the form html ID |
||
| 232 | * @access public |
||
| 233 | */ |
||
| 234 | function form_element_id() { |
||
| 237 | |||
| 238 | /** |
||
| 239 | * form_item_id method, returns the form html ID |
||
| 240 | * @access public |
||
| 241 | */ |
||
| 242 | function form_item_id() { |
||
| 245 | |||
| 246 | /** |
||
| 247 | * returns the unqiue name for the current checkout item |
||
| 248 | * |
||
| 249 | * @since 3.8.14 |
||
| 250 | * |
||
| 251 | * @access public |
||
| 252 | * |
||
| 253 | * @return string unqiue name associated with the current checkout item |
||
| 254 | */ |
||
| 255 | function form_item_unique_name() { |
||
| 258 | |||
| 259 | /** |
||
| 260 | * get_checkout_options, returns the form field options |
||
| 261 | * @access public |
||
| 262 | */ |
||
| 263 | function get_checkout_options( $id ) { |
||
| 264 | global $wpdb; |
||
| 265 | $sql = $wpdb->prepare( 'SELECT `options` FROM `' . WPSC_TABLE_CHECKOUT_FORMS . '` WHERE `id` = %d', $id ); |
||
| 266 | $options = $wpdb->get_var( $sql ); |
||
| 267 | $options = unserialize( $options ); |
||
| 268 | return $options; |
||
| 269 | } |
||
| 270 | |||
| 271 | /** |
||
| 272 | * form_field method, returns the form html |
||
| 273 | * @access public |
||
| 274 | */ |
||
| 275 | function form_field() { |
||
| 276 | |||
| 277 | global $wpdb, $user_ID, $wpsc_customer_checkout_details; |
||
| 278 | |||
| 279 | $saved_form_data = empty( $wpsc_customer_checkout_details[$this->checkout_item->id] ) ? null : $wpsc_customer_checkout_details[$this->checkout_item->id]; |
||
| 280 | $an_array = ''; |
||
| 281 | if ( function_exists( 'wpsc_get_ticket_checkout_set' ) ) { |
||
| 282 | if ( $this->checkout_item->checkout_set == wpsc_get_ticket_checkout_set() ) |
||
| 283 | $an_array = '[]'; |
||
| 284 | } |
||
| 285 | |||
| 286 | $output = ''; |
||
| 287 | $delivery_country = wpsc_get_customer_meta( 'shippingcountry' ); |
||
| 288 | $billing_country = wpsc_get_customer_meta( 'billingcountry' ); |
||
| 289 | $delivery_region = wpsc_get_customer_meta( 'shippingregion' ); |
||
| 290 | $billing_region = wpsc_get_customer_meta( 'billingregion' ); |
||
| 291 | |||
| 292 | $meta_key = ! empty( $this->checkout_item->unique_name ) ? $this->checkout_item->unique_name : sanitize_title( $this->checkout_item->name ) . '_' . $this->checkout_item->id; |
||
| 293 | |||
| 294 | switch ( $this->checkout_item->type ) { |
||
| 295 | |||
| 296 | case "address": |
||
| 297 | case "delivery_address": |
||
| 298 | case "textarea": |
||
| 299 | $placeholder = apply_filters( 'wpsc_checkout_field_placeholder', apply_filters( 'wpsc_checkout_field_name', $this->checkout_item->name ), $this->checkout_item ); |
||
| 300 | $output .= '<textarea data-wpsc-meta-key="' . $meta_key . '" title="' . $this->checkout_item->unique_name |
||
| 301 | . '" class="text wpsc-visitor-meta" id="' . $this->form_element_id() |
||
| 302 | . '" name="collected_data[' . $this->checkout_item->id . ']' . $an_array . '" placeholder="' |
||
| 303 | . esc_attr( $placeholder ) . '" rows="3" cols="40" >' |
||
| 304 | . esc_textarea( (string) $saved_form_data ) . '</textarea>'; |
||
| 305 | break; |
||
| 306 | |||
| 307 | case "checkbox": |
||
| 308 | $options = $this->get_checkout_options( $this->checkout_item->id ); |
||
| 309 | if ( $options != '' ) { |
||
| 310 | $i = mt_rand(); |
||
| 311 | foreach ( $options as $label => $value ) { |
||
| 312 | ?> |
||
| 313 | <label> |
||
| 314 | <input class="wpsc-visitor-meta" data-wpsc-meta-key="<?php echo $meta_key; ?>" <?php checked( in_array( $value, (array) $saved_form_data ) ); ?> type="checkbox" name="collected_data[<?php echo esc_attr( $this->checkout_item->id ); ?>]<?php echo $an_array; ?>[]" value="<?php echo esc_attr( $value ); ?>" /> |
||
| 315 | <?php echo esc_html( $label ); ?> |
||
| 316 | </label> |
||
| 317 | <?php |
||
| 318 | } |
||
| 319 | } |
||
| 320 | break; |
||
| 321 | |||
| 322 | case "country": |
||
| 323 | $output = wpsc_country_list( $this->checkout_item->id, null, $billing_country, $billing_region, $this->form_element_id() ); |
||
| 324 | break; |
||
| 325 | |||
| 326 | case "delivery_country": |
||
| 327 | $checkoutfields = true; |
||
| 328 | $output = wpsc_country_list( $this->checkout_item->id, null, $delivery_country, $delivery_region, $this->form_element_id(), $checkoutfields ); |
||
| 329 | break; |
||
| 330 | |||
| 331 | case "select": |
||
| 332 | $options = $this->get_checkout_options( $this->checkout_item->id ); |
||
| 333 | if ( $options != '' ) { |
||
| 334 | $output = '<select class="wpsc-visitor-meta" data-wpsc-meta-key="' . $meta_key . '" name="collected_data[' . $this->checkout_item->id . ']"' . $an_array . '">'; |
||
| 335 | $output .= "<option value='-1'>" . _x( 'Select an Option', 'Dropdown default when called within checkout class' , 'wp-e-commerce' ) . "</option>"; |
||
| 336 | foreach ( (array)$options as $label => $value ) { |
||
| 337 | $value = esc_attr(str_replace( ' ', '', $value ) ); |
||
| 338 | $output .="<option " . selected( $value, $saved_form_data, false ) . " value='" . esc_attr( $value ) . "'>" . esc_html( $label ) . "</option>\n\r"; |
||
| 339 | } |
||
| 340 | $output .="</select>"; |
||
| 341 | } |
||
| 342 | break; |
||
| 343 | case "radio": |
||
| 344 | $options = $this->get_checkout_options( $this->checkout_item->id ); |
||
| 345 | if ( $options != '' ) { |
||
| 346 | foreach ( (array)$options as $label => $value ) { |
||
| 347 | ?> |
||
| 348 | <label> |
||
| 349 | <input class="wpsc-visitor-meta" data-wpsc-meta-key="<?php echo $meta_key; ?>" type="radio" <?php checked( $value, $saved_form_data ); ?> name="collected_data[<?php echo esc_attr( $this->checkout_item->id ); ?>]<?php echo $an_array; ?>" value="<?php echo esc_attr( $value ); ?>" /> |
||
| 350 | <?php echo esc_html( $label ); ?> |
||
| 351 | </label> |
||
| 352 | <?php |
||
| 353 | } |
||
| 354 | } |
||
| 355 | break; |
||
| 356 | |||
| 357 | case "text": |
||
| 358 | case "city": |
||
| 359 | case "delivery_city": |
||
| 360 | case "email": |
||
| 361 | case "coupon": |
||
| 362 | default: |
||
| 363 | if ( $this->checkout_item->unique_name == 'shippingstate' ) { |
||
| 364 | $output .= wpsc_checkout_shipping_state_and_region( $this ); |
||
| 365 | } elseif ( $this->checkout_item->unique_name == 'billingstate' ) { |
||
| 366 | $output .= wpsc_checkout_billing_state_and_region( $this ); |
||
| 367 | } else { |
||
| 368 | $placeholder = apply_filters( 'wpsc_checkout_field_placeholder', apply_filters( 'wpsc_checkout_field_name', $this->checkout_item->name ), $this->checkout_item ); |
||
| 369 | $output = '<input data-wpsc-meta-key="' . $meta_key . '" title="' . $meta_key . '" type="text" id="' . $this->form_element_id() . '" class="text wpsc-visitor-meta" placeholder="' . esc_attr( $placeholder ) . '" value="' . esc_attr( $saved_form_data ) . '" name="collected_data[' . $this->checkout_item->id . ']' . $an_array . '" />'; |
||
| 370 | } |
||
| 371 | |||
| 372 | break; |
||
| 373 | } |
||
| 374 | return $output; |
||
| 375 | } |
||
| 376 | |||
| 377 | /** |
||
| 378 | * validate_forms method, validates the input from the checkout page |
||
| 379 | * @access public |
||
| 380 | */ |
||
| 381 | function validate_forms() { |
||
| 555 | |||
| 556 | /** |
||
| 557 | * validate_forms method, validates the input from the checkout page |
||
| 558 | * @access public |
||
| 559 | */ |
||
| 560 | function save_forms_to_db( $purchase_id ) { |
||
| 561 | |||
| 562 | foreach ( $this->checkout_items as $form_data ) { |
||
| 563 | |||
| 564 | if ( $form_data->type == 'heading' ) { |
||
| 565 | continue; |
||
| 566 | } |
||
| 567 | |||
| 568 | $customer_meta_key = ! empty( $form_data->unique_name ) ? $form_data->unique_name : sanitize_title( $form_data->name ) . '_' . $form_data->id; |
||
| 569 | $checkout_item_values = wpsc_get_customer_meta( $customer_meta_key ); |
||
| 570 | |||
| 571 | // Prior to release 3.8.14 the billingstate and shippingstate checkout items were used |
||
| 572 | // differently depending on if the billingcountry and shippingcountry values contained countries |
||
| 573 | // that used regions. When countries with regions were present, the billing state field was |
||
| 574 | // set to the numeric region id, rather than the string name of the region. A better long term |
||
| 575 | // solution may be to have a distinct checkout item to hold the billingregion or shippingregion |
||
| 576 | // code when available. |
||
| 577 | if ( $customer_meta_key == 'billingstate' ) { |
||
| 578 | $current_country = wpsc_get_customer_meta( 'billingcountry' ); |
||
| 579 | if ( ! empty( $current_country) ) { |
||
| 580 | $wpsc_country = new WPSC_Country( $current_country ); |
||
| 581 | if ( $wpsc_country->has_regions() ) { |
||
| 582 | $region = wpsc_get_customer_meta( 'billingregion' ); |
||
| 583 | if ( ! empty( $region ) ) { |
||
| 584 | $wpsc_region = $wpsc_country->get_region( $region ); |
||
| 585 | $checkout_item_values = $wpsc_region->get_name(); |
||
| 586 | } |
||
| 587 | } |
||
| 588 | } |
||
| 589 | } elseif ( $customer_meta_key == 'shippingstate' ) { |
||
| 590 | $current_country = wpsc_get_customer_meta( 'shippingcountry' ); |
||
| 591 | if ( ! empty( $current_country) ) { |
||
| 592 | $wpsc_country = new WPSC_Country( $current_country ); |
||
| 593 | if ( $wpsc_country->has_regions() ) { |
||
| 594 | $region = wpsc_get_customer_meta( 'shippingregion' ); |
||
| 595 | if ( ! empty( $region ) ) { |
||
| 596 | $wpsc_region = $wpsc_country->get_region( $region ); |
||
| 597 | $checkout_item_values = $wpsc_region->get_name(); |
||
| 598 | } |
||
| 599 | } |
||
| 600 | } |
||
| 601 | } |
||
| 602 | |||
| 603 | if ( ! is_array( $checkout_item_values ) ) { |
||
| 604 | $checkout_item_values = array( $checkout_item_values ); |
||
| 605 | } |
||
| 606 | |||
| 607 | global $wpdb; |
||
| 608 | |||
| 609 | foreach ( $checkout_item_values as $checkout_item_value ) { |
||
| 610 | $prepared_query = $wpdb->insert( |
||
| 611 | WPSC_TABLE_SUBMITTED_FORM_DATA, |
||
| 612 | array( |
||
| 613 | 'log_id' => $purchase_id, |
||
| 614 | 'form_id' => $form_data->id, |
||
| 615 | 'value' => $checkout_item_value, |
||
| 616 | ), |
||
| 617 | array( |
||
| 618 | '%d', |
||
| 619 | '%d', |
||
| 620 | '%s', |
||
| 621 | ) |
||
| 622 | ); |
||
| 623 | } |
||
| 624 | } |
||
| 625 | } |
||
| 626 | |||
| 627 | /** |
||
| 628 | * Function that checks how many checkout fields are stored in checkout form fields table |
||
| 629 | */ |
||
| 630 | function get_count_checkout_fields() { |
||
| 631 | $checkout = new WPSC_Checkout_Form(); |
||
| 632 | $count = $checkout->get_field_count(); |
||
| 633 | return $count; |
||
| 634 | } |
||
| 635 | |||
| 636 | /** |
||
| 637 | * checkout loop methods |
||
| 638 | */ |
||
| 639 | function next_checkout_item() { |
||
| 640 | $this->current_checkout_item++; |
||
| 641 | $this->checkout_item = $this->checkout_items[$this->current_checkout_item]; |
||
| 642 | return $this->checkout_item; |
||
| 643 | } |
||
| 644 | |||
| 645 | function the_checkout_item() { |
||
| 646 | $this->in_the_loop = true; |
||
| 647 | $this->checkout_item = $this->next_checkout_item(); |
||
| 648 | if ( $this->current_checkout_item == 0 ) { |
||
| 649 | // loop has just started |
||
| 650 | do_action( 'wpsc_checkout_loop_start', $this ); |
||
| 651 | } |
||
| 652 | return $this->checkout_item; |
||
| 653 | } |
||
| 654 | |||
| 655 | function have_checkout_items() { |
||
| 667 | |||
| 668 | function rewind_checkout_items() { |
||
| 669 | global $wpsc_checkout_error_messages; |
||
| 670 | $wpsc_checkout_error_messages = array(); |
||
| 671 | wpsc_delete_customer_meta( 'checkout_error_messages' ); |
||
| 672 | $this->current_checkout_item = -1; |
||
| 673 | if ( $this->checkout_item_count > 0 ) { |
||
| 674 | $this->checkout_item = $this->checkout_items[0]; |
||
| 677 | |||
| 678 | /** |
||
| 679 | * find the checkout item that corresponsds to the identifier |
||
| 680 | * |
||
| 681 | * @param int|string $id the checkout item identifier, if a numeric the checkout item id, if a string the checkout item unique name |
||
| 682 | * |
||
| 683 | * @return cehckout item found, or false if not found |
||
| 684 | */ |
||
| 685 | function get_checkout_item( $id ) { |
||
| 700 | |||
| 701 | |||
| 702 | |||
| 703 | |||
| 704 | } |
||
| 705 |
This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.