| 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 |
||
| 86 | public function maybe_track_purchases( $command_array ) { |
||
| 87 | global $wp; |
||
| 88 | |||
| 89 | if ( ! Jetpack_Google_Analytics_Options::track_purchases_is_enabled() ) { |
||
| 90 | return $command_array; |
||
| 91 | } |
||
| 92 | |||
| 93 | if ( ! class_exists( 'WooCommerce' ) ) { |
||
| 94 | return $command_array; |
||
| 95 | } |
||
| 96 | |||
| 97 | $minimum_woocommerce_active = class_exists( 'WooCommerce' ) && version_compare( WC_VERSION, '3.0', '>=' ); |
||
| 98 | if ( ! $minimum_woocommerce_active ) { |
||
| 99 | return $command_array; |
||
| 100 | } |
||
| 101 | |||
| 102 | if ( ! is_order_received_page() ) { |
||
| 103 | return $command_array; |
||
| 104 | } |
||
| 105 | |||
| 106 | $order_id = isset( $wp->query_vars['order-received'] ) ? $wp->query_vars['order-received'] : 0; |
||
| 107 | if ( 0 == $order_id ) { |
||
| 108 | return $command_array; |
||
| 109 | } |
||
| 110 | |||
| 111 | // A 1 indicates we've already tracked this order - don't do it again |
||
| 112 | if ( 1 == get_post_meta( $order_id, '_ga_tracked', true ) ) { |
||
| 113 | return $command_array; |
||
| 114 | } |
||
| 115 | |||
| 116 | $order = new WC_Order( $order_id ); |
||
| 117 | $order_currency = $order->get_currency(); |
||
| 118 | $command = "ga( 'set', '&cu', '" . esc_js( $order_currency ) . "' );"; |
||
| 119 | array_push( $command_array, $command ); |
||
| 120 | |||
| 121 | // Order items |
||
| 122 | if ( $order->get_items() ) { |
||
| 123 | foreach ( $order->get_items() as $item ) { |
||
| 124 | $product = $order->get_product_from_item( $item ); |
||
| 125 | $sku_or_id = $product->get_sku() ? $product->get_sku() : $product->get_id(); |
||
| 126 | |||
| 127 | $item_details = array( |
||
| 128 | 'id' => $sku_or_id, |
||
| 129 | 'name' => $item['name'], |
||
| 130 | 'category' => Jetpack_Google_Analytics_Utils::get_product_categories_concatenated( $product ), |
||
| 131 | 'price' => $order->get_item_total( $item ), |
||
| 132 | 'quantity' => $item['qty'], |
||
| 133 | ); |
||
| 134 | $command = "ga( 'ec:addProduct', " . wp_json_encode( $item_details ) . " );"; |
||
| 135 | array_push( $command_array, $command ); |
||
| 136 | } |
||
| 137 | } |
||
| 138 | |||
| 139 | // Order summary |
||
| 140 | $summary = array( |
||
| 141 | 'id' => $order->get_order_number(), |
||
| 142 | 'affiliation' => get_bloginfo( 'name' ), |
||
| 143 | 'revenue' => $order->get_total(), |
||
| 144 | 'tax' => $order->get_total_tax(), |
||
| 145 | 'shipping' => $order->get_total_shipping() |
||
| 146 | ); |
||
| 147 | $command = "ga( 'ec:setAction', 'purchase', " . wp_json_encode( $summary ) . " );"; |
||
| 148 | array_push( $command_array, $command ); |
||
| 149 | |||
| 150 | update_post_meta( $order_id, '_ga_tracked', 1 ); |
||
| 151 | |||
| 152 | return $command_array; |
||
| 153 | } |
||
| 154 | |||
| 174 | } |