Completed
Push — master ( 37d4aa...f30c53 )
by Dan
05:58
created

SSI_API::disable_woocommerce_checkout_scripts()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6
Metric Value
dl 0
loc 5
ccs 0
cts 5
cp 0
rs 9.4286
cc 2
eloc 3
nc 2
nop 0
crap 6
1
<?php
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 21 and the first side effect is on line 12.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

Loading history...
2
/**
3
 * SSI API Connector
4
 *
5
 * @package     PrintCenter\SSI_API
6
 * @since       1.0.0
7
 */
8
9
10
// Exit if accessed directly
11
if( ! defined( 'ABSPATH' ) ) {
12
	exit;
13
}
14
15
16
/**
17
 * Main SSI_API class
18
 *
19
 * @since       1.0.0
20
 */
21
class SSI_API {
22
23
24
	/**
25
	 * @access      public
26
	 * @since       1.0.0
27
	 * @var         string $shipping The defined shipping method
28
	 */
29
	public $shipping = 'Priority Mail';
30
31
32
	/**
33
	 * Get things started
34
	 *
35
	 * @access      public
36
	 * @since       1.0.0
37
	 * @return      void
0 ignored issues
show
Comprehensibility Best Practice introduced by
Adding a @return annotation to constructors is generally not recommended as a constructor does not have a meaningful return value.

Adding a @return annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.

Please refer to the PHP core documentation on constructors.

Loading history...
38
	 */
39
	public function __construct() {
40
		$this->hooks();
41
	}
42
43
44
	/**
45
	 * Run action and filter hooks
46
	 *
47
	 * @access      public
48
	 * @since       1.0.0
49
	 * @return      void
50
	 */
51
	public function hooks() {
52
		add_action( 'woocommerce_checkout_order_processed', array( $this, 'process_api_order' ), 10, 2 );
53
		add_action( 'wp_enqueue_scripts', array( $this, 'disable_woocommerce_checkout_scripts' ) );
54
	}
55
56
57
	/**
58
	 * Disable WooCommerce checkout scripts if test mode is active
59
	 *
60
	 * @access      public
61
	 * @since       1.0.0
62
	 * @return      void
63
	 */
64
	public function disable_woocommerce_checkout_scripts() {
65
		if( printcenter()->loader->settings->get_option( 'ssi_mode', 'live' ) == 'test' ) {
66
			wp_dequeue_script( 'wc-checkout' );
67
		}
68
	}
69
70
71
	/**
72
	 * Process new orders and send to API
73
	 *
74
	 * @access      public
75
	 * @since       1.0.0
76
	 * @param       int $order_id The ID of this order
77
	 * @param       array $posted The data posted for the order
78
	 * @return      void
79
	 */
80
	public function process_api_order( $order_id = 0, $posted ) {
81
		global $the_order;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
82
83
		if( empty( $the_order ) || $the_order->id != $post->ID ) {
0 ignored issues
show
Bug introduced by
The variable $post does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
84
			$the_order = wc_get_order( $order_id );
85
		}
86
87
		$ssi_mode = printcenter()->loader->settings->get_option( 'ssi_mode', 'live' );
88
89
		if( $ssi_mode == 'capture' ) {
90
			$custid   = printcenter()->loader->settings->get_option( 'ssi_test_custid', '1013' );
91
			$custzip  = printcenter()->loader->settings->get_option( 'ssi_test_custzip', '99999' );
92
			$endpoint = 'https://orders.silkscreenink.com/capture.asp';
93
		} elseif( $ssi_mode == 'test' ) {
94
			$custid   = printcenter()->loader->settings->get_option( 'ssi_test_custid', '1013' );
95
			$custzip  = printcenter()->loader->settings->get_option( 'ssi_test_custzip', '99999' );
96
			$endpoint = 'https://orders.silkscreenink.com/orderstest/default.asp';
97
		} else {
98
			$custid   = printcenter()->loader->settings->get_option( 'ssi_custid', '1024' );
99
			$custzip  = printcenter()->loader->settings->get_option( 'ssi_custzip', '80304' );
100
			$endpoint = 'https://orders.silkscreenink.com/orderslive/';
101
		}
102
103
		$ssi_order = array(
104
			'DocType'          => 'Order',
105
			'GarmentsProvided' => 'No',
106
			'CustID'           => $custid,
107
			'CustZip'          => $custzip,
108
			'PO'               => $order_id,
109
			'ShipTo'           => array(
110
				'FirstName' => ( $posted['billing_first_name'] ? $posted['billing_first_name'] : '' ),
111
				'LastName'  => ( $posted['billing_last_name'] ? $posted['billing_last_name'] : '' ),
112
				'Adrx1'     => ( $posted['billing_address_1'] ? $posted['billing_address_1'] : '' ),
113
				'City'      => ( $posted['billing_city'] ? $posted['billing_city'] : '' ),
114
				'State'     => ( $posted['billing_state'] ? $posted['billing_state'] : '' ),
115
				'Zip'       => ( $posted['billing_postcode'] ? $posted['billing_postcode'] : '' ),
116
				'Country'   => ( $posted['billing_country'] ? $posted['billing_country'] : '' ),
117
				'Email'     => ( $posted['billing_email'] ? $posted['billing_email'] : '' ),
118
				'Phone'     => ( $posted['billing_phone'] ? $posted['billing_phone'] : '' ),
119
			),
120
			'ShipMethod'         => $this->shipping,
121
			'ProductionPriority' => 'Normal',
122
		);
123
124
		foreach( $the_order->get_items() as $item ) {
125
			$product   = $the_order->get_product_from_item( $item );
126
			$item_meta = new WC_Order_Item_Meta( $item, $product );
127
			$item_meta = $item_meta->get_formatted();
128
129
			foreach( $item_meta as $item_id => $meta ) {
130
				switch( $meta['key'] ) {
131
					case 'pa_size' :
132
						$size = $meta['value'];
133
						break;
134
					case 'pa_color' :
135
						$color = $meta['value'];
136
						break;
137
				}
138
			}
139
140
			$sku      = get_post_meta( $item['product_id'] , '_ssi_sku', true );
141
			$location = get_post_meta( $item['product_id'] , '_ssi_location', true );
142
			$sizing   = get_post_meta( $item['product_id'] , '_ssi_sizing', true );
143
			$art      = get_post_meta( $item['product_id'] , '_ssi_art', true );
144
			$thumb    = wp_get_attachment_image_src( get_post_thumbnail_id( $item['product_id'] ), 'full' );
145
			$thumb    = $thumb[0];
146
147
			if( $location == 'front' ) {
148
				$location = 'Full Front';
149
			} elseif( $location == 'back' ) {
150
				$location = 'Full Back';
151
			} else {
152
				$location = 'Left Chest';
153
			}
154
155
			$ssi_order['Item'][] = array(
156
				'SKU'            => $sku,
157
				'Color'          => ( isset( $color ) ? $color : false ),
158
				'Size'           => ( isset( $size ) ? $size : false ),
159
				'Qty'            => $item['qty'],
160
				'DesignLocation' => $location,
161
				'DesignType'     => 1,
162
				'DesignArt'      => ( isset( $art ) ? $art : false ),
163
				'DesignThumb'    => $thumb,
164
				'DesignCategory' => $sizing
165
			);
166
167
			$xml = Array2XML::createXML( 'Request', $ssi_order );
168
			$xml = $xml->saveXML();
169
170
			$content = array(
171
				'headers' => array(
172
					'content-type' => 'text/xml'
173
				),
174
				'body' => $xml
175
			);
176
177
			$response = wp_remote_post( $endpoint, $content );
178
179
			if( $ssi_mode == 'test' ) {
180
				$response = wp_remote_retrieve_body( $response );
181
182
				echo '<pre>' . printcenter_prettify_xml( $response, true ) . '</pre>';
183
				exit;
0 ignored issues
show
Coding Style Compatibility introduced by
The method process_api_order() contains an exit expression.

An exit expression should only be used in rare cases. For example, if you write a short command line script.

In most cases however, using an exit expression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.

Loading history...
184
			}
185
		}
186
	}
187
}