| Conditions | 12 |
| Paths | 22 |
| Total Lines | 68 |
| Code Lines | 42 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 82 | public function maybe_track_purchases( $command_array ) { |
||
| 83 | global $wp; |
||
| 84 | |||
| 85 | if ( ! Jetpack_Google_Analytics_Options::track_purchases_is_enabled() ) { |
||
| 86 | return $command_array; |
||
| 87 | } |
||
| 88 | |||
| 89 | if ( ! class_exists( 'WooCommerce' ) ) { |
||
| 90 | return $command_array; |
||
| 91 | } |
||
| 92 | |||
| 93 | $minimum_woocommerce_active = class_exists( 'WooCommerce' ) && version_compare( WC_VERSION, '3.0', '>=' ); |
||
| 94 | if ( ! $minimum_woocommerce_active ) { |
||
| 95 | return $command_array; |
||
| 96 | } |
||
| 97 | |||
| 98 | if ( ! is_order_received_page() ) { |
||
| 99 | return $command_array; |
||
| 100 | } |
||
| 101 | |||
| 102 | $order_id = isset( $wp->query_vars['order-received'] ) ? $wp->query_vars['order-received'] : 0; |
||
| 103 | if ( 0 == $order_id ) { |
||
| 104 | return $command_array; |
||
| 105 | } |
||
| 106 | |||
| 107 | // A 1 indicates we've already tracked this order - don't do it again |
||
| 108 | if ( 1 == get_post_meta( $order_id, '_ga_tracked', true ) ) { |
||
| 109 | return $command_array; |
||
| 110 | } |
||
| 111 | |||
| 112 | $order = new WC_Order( $order_id ); |
||
| 113 | $order_currency = $order->get_currency(); |
||
| 114 | $command = "ga( 'set', '&cu', '" . esc_js( $order_currency ) . "' );"; |
||
| 115 | array_push( $command_array, $command ); |
||
| 116 | |||
| 117 | // Order items |
||
| 118 | if ( $order->get_items() ) { |
||
| 119 | foreach ( $order->get_items() as $item ) { |
||
| 120 | $product = $order->get_product_from_item( $item ); |
||
| 121 | $sku_or_id = $product->get_sku() ? $product->get_sku() : '#' . $product->get_id(); |
||
| 122 | |||
| 123 | $item_details = array( |
||
| 124 | 'id' => $sku_or_id, |
||
| 125 | 'name' => $item['name'], |
||
| 126 | 'category' => Jetpack_Google_Analytics_Utils::get_product_categories_concatenated( $product ), |
||
| 127 | 'price' => $order->get_item_total( $item ), |
||
| 128 | 'quantity' => $item['qty'], |
||
| 129 | ); |
||
| 130 | $command = "ga( 'ec:addProduct', " . wp_json_encode( $item_details ) . " );"; |
||
| 131 | array_push( $command_array, $command ); |
||
| 132 | } |
||
| 133 | } |
||
| 134 | |||
| 135 | // Order summary |
||
| 136 | $summary = array( |
||
| 137 | 'id' => $order->get_order_number(), |
||
| 138 | 'affiliation' => get_bloginfo( 'name' ), |
||
| 139 | 'revenue' => $order->get_total(), |
||
| 140 | 'tax' => $order->get_total_tax(), |
||
| 141 | 'shipping' => $order->get_total_shipping() |
||
| 142 | ); |
||
| 143 | $command = "ga( 'ec:setAction', 'purchase', " . wp_json_encode( $summary ) . " );"; |
||
| 144 | array_push( $command_array, $command ); |
||
| 145 | |||
| 146 | update_post_meta( $order_id, '_ga_tracked', 1 ); |
||
| 147 | |||
| 148 | return $command_array; |
||
| 149 | } |
||
| 150 | |||
| 233 | } |