Completed
Pull Request — master (#2165)
by Justin
06:52 queued 01:11
created

WPSC_Purchase_Log_Page   F

Complexity

Total Complexity 113

Size/Duplication

Total Lines 663
Duplicated Lines 0 %

Coupling/Cohesion

Components 5
Dependencies 6

Importance

Changes 0
Metric Value
dl 0
loc 663
rs 1.3798
c 0
b 0
f 0
wmc 113
lcom 5
cbo 6

33 Methods

Rating   Name   Duplication   Size   Complexity  
C __construct() 0 27 8
A needs_update() 0 13 3
A controller_upgrade_purchase_logs_3_7() 0 3 1
A purchase_logs_fix_options() 0 23 1
B display_upgrade_purchase_logs_3_7() 0 41 5
A display_upgrade_purchase_logs_3_8() 0 10 1
A controller_upgrade_purchase_logs_3_8() 0 7 2
A purchase_logs_pagination() 0 16 3
B purchase_logs_checkout_fields() 0 33 5
B purchase_log_custom_fields() 0 28 6
A edit_contact_details_form() 0 6 1
A purchase_log_cart_items() 0 5 2
B purchase_log_cart_item() 0 35 4
A notes_output() 0 5 2
A note_output() 0 14 1
B shipping_address_output() 0 11 6
B billing_address_output() 0 11 6
A payment_details_output() 0 14 4
C controller_item_details() 0 51 10
A maybe_update_contact_details_for_log() 0 13 2
A maybe_add_note_to_log() 0 10 2
A maybe_delete_note_from_log() 0 12 2
A include_te_v2_resources() 0 10 2
B enqueue_te_v2_resources() 0 33 3
A doc_title() 0 7 1
B controller_packing_slip() 0 37 6
A controller_default() 0 7 1
B display_purchase_log() 0 25 5
A download_csv() 0 3 1
C process_bulk_action() 0 75 13
A action_list_table_before() 0 3 1
A display_list_table() 0 8 2
A get_purchase_log_url() 0 9 1

How to fix   Complexity   

Complex Class

Complex classes like WPSC_Purchase_Log_Page 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_Purchase_Log_Page, and based on these observations, apply Extract Interface, too.

1
<?php
2
/**
3
 * WP eCommerce edit and view sales page functions
4
 *
5
 * These are the main WPSC sales page functions
6
 *
7
 * @package wp-e-commerce
8
 * @since 3.8.8
9
 */
10
11
class WPSC_Purchase_Log_Page {
12
	private $list_table;
13
	private $output;
14
	public $log_id = 0;
15
16
	/**
17
	 * WPSC_Purchase_Log
18
	 *
19
	 * @var WPSC_Purchase_Log object.
20
	 */
21
	public $log = null;
22
23
	/**
24
	 * Whether the purchase log can be modified.
25
	 *
26
	 * @var boolean
27
	 */
28
	protected $can_edit = false;
29
30
	public function __construct() {
31
		$controller = 'default';
32
		$controller_method = 'controller_default';
33
34
		// If individual purchase log, setup ID and action links.
35
		if ( isset( $_REQUEST['id'] ) && is_numeric( $_REQUEST['id'] ) ) {
36
			$this->log_id = (int) $_REQUEST['id'];
37
			$this->log = new WPSC_Purchase_Log( $this->log_id );
38
			$this->notes = new WPSC_Purchase_Log_Notes( $this->log );
0 ignored issues
show
Bug introduced by
The property notes does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
39
			$this->can_edit = $this->log->can_edit();
40
		}
41
42
		if ( isset( $_REQUEST['c'] ) && method_exists( $this, 'controller_' . $_REQUEST['c'] ) ) {
43
			$controller = $_REQUEST['c'];
44
			$controller_method = 'controller_' . $controller;
45
		} elseif ( isset( $_REQUEST['id'] ) && is_numeric( $_REQUEST['id'] ) ) {
46
			$controller = 'item_details';
47
			$controller_method = 'controller_item_details';
48
		}
49
50
		// Can only edit in the item details view.
51
		if ( 'controller_item_details' !== $controller_method ) {
52
			$this->can_edit = false;
53
		}
54
55
		$this->$controller_method();
56
	}
57
58
	private function needs_update() {
59
		global $wpdb;
60
61
		if ( get_option( '_wpsc_purchlogs_3.8_updated' ) )
62
			return false;
63
64
		$c = $wpdb->get_var( "SELECT COUNT(*) FROM " . WPSC_TABLE_PURCHASE_LOGS . " WHERE plugin_version IN ('3.6', '3.7')" );
65
		if ( $c > 0 )
66
			return true;
67
68
		update_option( '_wpsc_purchlogs_3.8_updated', true );
69
		return false;
70
	}
71
72
	public function controller_upgrade_purchase_logs_3_7() {
73
		add_action( 'wpsc_display_purchase_logs_page', array( $this, 'display_upgrade_purchase_logs_3_7' ) );
74
	}
75
76
	private function purchase_logs_fix_options( $id ) {
77
		?>
78
		<select name='<?php echo $id; ?>'>
79
			<option value='-1'><?php echo esc_html_x( 'Select an Option', 'Dropdown default when called in uniquename dropdown', 'wp-e-commerce' ); ?></option>
80
			<option value='billingfirstname'><?php esc_html_e( 'Billing First Name', 'wp-e-commerce' ); ?></option>
81
			<option value='billinglastname'><?php esc_html_e( 'Billing Last Name', 'wp-e-commerce' ); ?></option>
82
			<option value='billingaddress'><?php esc_html_e( 'Billing Address', 'wp-e-commerce' ); ?></option>
83
			<option value='billingcity'><?php esc_html_e( 'Billing City', 'wp-e-commerce' ); ?></option>
84
			<option value='billingstate'><?php esc_html_e( 'Billing State', 'wp-e-commerce' ); ?></option>
85
			<option value='billingcountry'><?php esc_html_e( 'Billing Country', 'wp-e-commerce' ); ?></option>
86
			<option value='billingemail'><?php esc_html_e( 'Billing Email', 'wp-e-commerce' ); ?></option>
87
			<option value='billingphone'><?php esc_html_e( 'Billing Phone', 'wp-e-commerce' ); ?></option>
88
			<option value='billingpostcode'><?php esc_html_e( 'Billing Post Code', 'wp-e-commerce' ); ?></option>
89
			<option value='shippingfirstname'><?php esc_html_e( 'Shipping First Name', 'wp-e-commerce' ); ?></option>
90
			<option value='shippinglastname'><?php esc_html_e( 'Shipping Last Name', 'wp-e-commerce' ); ?></option>
91
			<option value='shippingaddress'><?php esc_html_e( 'Shipping Address', 'wp-e-commerce' ); ?></option>
92
			<option value='shippingcity'><?php esc_html_e( 'Shipping City', 'wp-e-commerce' ); ?></option>
93
			<option value='shippingstate'><?php esc_html_e( 'Shipping State', 'wp-e-commerce' ); ?></option>
94
			<option value='shippingcountry'><?php esc_html_e( 'Shipping Country', 'wp-e-commerce' ); ?></option>
95
			<option value='shippingpostcode'><?php esc_html_e( 'Shipping Post Code', 'wp-e-commerce' ); ?></option>
96
		</select>
97
		<?php
98
	}
99
100
	public function display_upgrade_purchase_logs_3_7() {
101
		global $wpdb;
102
		$numChanged = 0;
103
		$numQueries = 0;
104
		$purchlog =  "SELECT DISTINCT id FROM `".WPSC_TABLE_PURCHASE_LOGS."` LIMIT 1";
0 ignored issues
show
introduced by
Expected 1 space after "="; 2 found
Loading history...
105
		$id = $wpdb->get_var($purchlog);
106
		$usersql = "SELECT DISTINCT `".WPSC_TABLE_SUBMITTED_FORM_DATA."`.value, `".WPSC_TABLE_CHECKOUT_FORMS."`.* FROM `".WPSC_TABLE_CHECKOUT_FORMS."` LEFT JOIN `".WPSC_TABLE_SUBMITTED_FORM_DATA."` ON `".WPSC_TABLE_CHECKOUT_FORMS."`.id = `".WPSC_TABLE_SUBMITTED_FORM_DATA."`.`form_id` WHERE `".WPSC_TABLE_SUBMITTED_FORM_DATA."`.log_id=".$id." ORDER BY `".WPSC_TABLE_CHECKOUT_FORMS."`.`checkout_order`" ;
107
		$formfields = $wpdb->get_results($usersql);
108
109
		if(count($formfields) < 1){
110
			$usersql = "SELECT DISTINCT  `".WPSC_TABLE_CHECKOUT_FORMS."`.* FROM `".WPSC_TABLE_CHECKOUT_FORMS."` WHERE `type` != 'heading'";
111
			$formfields = $wpdb->get_results($usersql);
112
		}
113
114
		if(isset($_POST)){
115
			foreach($_POST as $key=>$value){
116
				if($value != '-1'){
117
					$complete = $wpdb->update(
118
				 WPSC_TABLE_CHECKOUT_FORMS,
119
				 array(
120
				'unique_name' => $value
121
				 ),
122
				 array(
123
				'id' => $key
124
				  ),
125
				 '%s',
126
				 '%d'
127
				 );
128
				}
129
				$numChanged++;
130
				$numQueries ++;
131
			}
132
133
			$sql = "UPDATE `".WPSC_TABLE_CHECKOUT_FORMS."` SET `unique_name`='delivertoafriend' WHERE `name` = '2. Shipping details'";
134
			$wpdb->query($sql);
135
136
			add_option('wpsc_purchaselogs_fixed',true);
137
		}
138
139
		include( 'includes/purchase-logs-page/upgrade.php' );
140
	}
141
142
	public function display_upgrade_purchase_logs_3_8() {
143
		?>
144
			<div class="wrap">
145
				<h2><?php echo esc_html( __('Sales', 'wp-e-commerce') ); ?> </h2>
146
				<div class="updated">
147
					<p><?php printf( __( 'Your purchase logs have been updated! <a href="%s">Click here</a> to return.' , 'wp-e-commerce' ), esc_url( remove_query_arg( 'c' ) ) ); ?></p>
148
				</div>
149
			</div>
150
		<?php
151
	}
152
153
	public function controller_upgrade_purchase_logs_3_8() {
154
		if ( $this->needs_update() ) {
155
			wpsc_update_purchase_logs();
156
		}
157
158
		add_action( 'wpsc_display_purchase_logs_page', array( $this, 'display_upgrade_purchase_logs_3_8' ) );
159
	}
160
161
	function purchase_logs_pagination() {
162
		global $wpdb, $purchlogitem;
163
		$prev_id = $this->log->get_previous_log_id();
164
		$next_id = $this->log->get_next_log_id();
165
		?>
166
		<span class='tablenav'><span class='tablenav-pages'><span class='pagination-links'>
167
			<?php if ( $prev_id ) : ?>
168
				<a href='<?php echo esc_url( $this->get_purchase_log_url( $prev_id ) ); ?>' class='prev-page'>&lsaquo; <?php _e( 'Previous', 'wp-e-commerce' ); ?></a>
169
			<?php endif; ?>
170
171
			<?php if ( $next_id ) : ?>
172
				<a href='<?php echo esc_url( $this->get_purchase_log_url( $next_id ) ); ?>' class='next-page'><?php _e( 'Next', 'wp-e-commerce' ); ?> &rsaquo;</a>
173
			<?php endif; ?>
174
		</span></span></span>
175
		<?php
176
	}
177
178
	function purchase_logs_checkout_fields(){
179
		global $purchlogitem;
180
181
		if ( ! empty( $purchlogitem->additional_fields ) ) {
182
		?>
183
			<div class="metabox-holder">
184
				<div id="custom_checkout_fields" class="postbox">
185
					<h3 class='hndle'><?php esc_html_e( 'Additional Checkout Fields' , 'wp-e-commerce' ); ?></h3>
186
					<div class='inside'>
187
						<?php
188
						foreach( (array) $purchlogitem->additional_fields as $value ) {
189
							$value['value'] = maybe_unserialize ( $value['value'] );
0 ignored issues
show
Coding Style introduced by
Space before opening parenthesis of function call prohibited
Loading history...
190
							if ( is_array( $value['value'] ) ) {
191
								?>
192
									<p><strong><?php echo $value['name']; ?> :</strong> <?php echo implode( stripslashes( $value['value'] ), ',' ); ?></p>
193
								<?php
194
							} else {
195
								$thevalue = esc_html( stripslashes( $value['value'] ));
196
								if ( empty( $thevalue ) ) {
197
									$thevalue = __( '<em>blank</em>', 'wp-e-commerce' );
198
								}
199
								?>
200
									<p><strong><?php echo $value['name']; ?> :</strong> <?php echo $thevalue; ?></p>
201
								<?php
202
							}
203
						}
204
						?>
205
					</div>
206
				</div>
207
			</div>
208
		<?php
209
		}
210
	}
211
212
	public function purchase_log_custom_fields(){
213
		if( wpsc_purchlogs_has_customfields() ){?>
214
			<div class='metabox-holder'>
215
				<div id='purchlogs_customfields' class='postbox'>
216
					<h3 class='hndle'><?php esc_html_e( 'Users Custom Fields' , 'wp-e-commerce' ); ?></h3>
217
					<div class='inside'>
218
						<?php $messages = wpsc_purchlogs_custommessages(); ?>
219
						<?php $files = wpsc_purchlogs_customfiles(); ?>
220
						<?php if(count($files) > 0){ ?>
221
							<h4><?php esc_html_e( 'Cart Items with Custom Files' , 'wp-e-commerce' ); ?>:</h4>
222
							<?php
223
							foreach($files as $file){
224
								echo $file;
225
							}
226
						}?>
227
						<?php if(count($messages) > 0){ ?>
228
							<h4><?php esc_html_e( 'Cart Items with Custom Messages' , 'wp-e-commerce' ); ?>:</h4>
229
							<?php
230
							foreach($messages as $message){
231
								echo esc_html( $message['title'] ) . ':<br />' . nl2br( esc_html( $message['message'] ) );
232
							}
233
						} ?>
234
					</div>
235
				</div>
236
			</div>
237
		<?php
238
		}
239
	}
240
241
	private function edit_contact_details_form() {
242
		$args = wpsc_get_customer_settings_form_args( $this->log->form_data() );
243
		$args['form_actions'][0]['class'] = 'button';
244
		$args['form_actions'][0]['title'] = __( 'Update', 'wp-e-commerce' );
245
		echo wpsc_get_form_output( $args );
246
	}
247
248
	private function purchase_log_cart_items() {
249
		while( wpsc_have_purchaselog_details() ) : wpsc_the_purchaselog_item();
250
			self::purchase_log_cart_item( $this->can_edit );
251
		endwhile;
252
	}
253
254
	public static function purchase_log_cart_item( $can_edit = false ) {
255
		?>
256
		<tr class="purchase-log-line-item" id="purchase-log-item-<?php echo wpsc_purchaselog_details_id(); ?>" data-id="<?php echo wpsc_purchaselog_details_id(); ?>" data-productid="<?php echo wpsc_purchaselog_product_id(); ?>">
257
			<td><?php echo wpsc_purchaselog_details_name(); ?></td> <!-- NAME! -->
258
			<td><?php echo wpsc_purchaselog_details_SKU(); ?></td> <!-- SKU! -->
259
			<td>
260
				<?php if ( $can_edit ) : ?>
261
					<input type="number" step="1" min="0" autocomplete="off" name="wpsc_item_qty" class="wpsc_item_qty" placeholder="0" value="<?php echo wpsc_purchaselog_details_quantity(); ?>" size="4" class="quantity">
262
				<?php else: ?>
263
					<?php echo wpsc_purchaselog_details_quantity(); ?>
264
				<?php endif; ?>
265
			</td> <!-- QUANTITY! -->
266
			<td>
267
		 <?php
268
		echo wpsc_currency_display( wpsc_purchaselog_details_price() );
269
		do_action( 'wpsc_additional_sales_amount_info', wpsc_purchaselog_details_id() );
270
		 ?>
271
	 </td> <!-- PRICE! -->
272
			<td><?php echo wpsc_currency_display( wpsc_purchaselog_details_shipping() ); ?></td> <!-- SHIPPING! -->
273
			<?php if( wpec_display_product_tax() ): ?>
274
				<td><?php echo wpsc_currency_display( wpsc_purchaselog_details_tax() ); ?></td> <!-- TAX! -->
275
			<?php endif; ?>
276
			<!-- <td><?php echo wpsc_currency_display( wpsc_purchaselog_details_discount() ); ?></td> --> <!-- DISCOUNT! -->
277
			<td class="amount"><?php echo wpsc_currency_display( wpsc_purchaselog_details_total() ); ?></td> <!-- TOTAL! -->
278
			<?php if ( $can_edit ) : ?>
279
				<td class="remove">
280
					<div class="wpsc-remove-row">
281
						<button type="button" class="wpsc-remove-button wpsc-remove-item-button"><span class="dashicons dashicons-dismiss"></span> <?php esc_html_e( 'Remove Item', 'wp-e-commerce' ); ?></button>
282
					</div>
283
				</td> <!-- REMOVE! -->
284
			<?php endif; ?>
285
		</tr>
286
		<?php
287
		do_action( 'wpsc_additional_sales_item_info', wpsc_purchaselog_details_id() );
288
	}
289
290
	public function notes_output() {
291
		foreach ( $this->notes as $note_id => $note_args ) : ?>
292
			<?php self::note_output( $this->notes, $note_id, $note_args ); ?>
293
		<?php endforeach;
294
	}
295
296
	public static function note_output( WPSC_Purchase_Log_Notes $notes, $note_id, array $note_args ) {
297
		?>
298
		<div class="wpsc-note" id="wpsc-note-<?php echo absint( $note_id ); ?>" data-id="<?php echo absint( $note_id ); ?>">
299
			<p>
300
				<strong class="note-date"><?php echo $notes->get_formatted_date( $note_args ); ?></strong>
301
				<a href="#wpsc-note-<?php echo absint( $note_id ); ?>" class="note-number">#<?php echo ( $note_id ); ?></a>
302
				<a href="<?php echo wp_nonce_url( add_query_arg( 'note', absint( $note_id ) ), 'delete-note', 'delete-note' ); ?>" class="wpsc-remove-button wpsc-remove-note-button"><span class="dashicons dashicons-dismiss"></span> <?php esc_html_e( 'Delete Note', 'wp-e-commerce' ); ?></a>
303
			</p>
304
			<div class="wpsc-note-content">
305
				<?php echo wpautop( $note_args['content'] ); ?>
306
			</div>
307
		</div>
308
		<?php
309
	}
310
311
	public static function shipping_address_output() {
312
		?>
313
		<strong>
314
			<?php echo ( wpsc_display_purchlog_shipping_name() != ""           ) ? wpsc_display_purchlog_shipping_name() . "<br />"               : '<span class="field-blank">' . __( 'Anonymous', 'wp-e-commerce' ) . '</span>' ; ?>
315
		</strong>
316
		<?php echo ( wpsc_display_purchlog_shipping_address() != ""            ) ? wpsc_display_purchlog_shipping_address() . "<br />"            : '' ; ?>
317
		<?php echo ( wpsc_display_purchlog_shipping_city() != ""               ) ? wpsc_display_purchlog_shipping_city() . ", "               : '' ; ?>
318
		<?php echo ( wpsc_display_purchlog_shipping_state_and_postcode() != "" ) ? wpsc_display_purchlog_shipping_state_and_postcode() . "<br />" : '' ; ?>
319
		<?php echo ( wpsc_display_purchlog_shipping_country() != ""            ) ? wpsc_display_purchlog_shipping_country() . "<br />"            : '<span class="field-blank">' . __( 'Country not specified', 'wp-e-commerce' ) . '</span>' ; ?>
320
		<?php
321
	}
322
323
	public static function billing_address_output() {
324
		?>
325
		<strong>
326
			<?php echo ( wpsc_display_purchlog_buyers_name() != ""           ) ? wpsc_display_purchlog_buyers_name() . "<br />"               : '<span class="field-blank">' . __( 'Anonymous', 'wp-e-commerce' ) . '</span>' ; ?>
327
		</strong>
328
		<?php echo ( wpsc_display_purchlog_buyers_address() != ""            ) ? wpsc_display_purchlog_buyers_address() . "<br />"            : '' ; ?>
329
		<?php echo ( wpsc_display_purchlog_buyers_city() != ""               ) ? wpsc_display_purchlog_buyers_city() . ", "               : '' ; ?>
330
		<?php echo ( wpsc_display_purchlog_buyers_state_and_postcode() != "" ) ? wpsc_display_purchlog_buyers_state_and_postcode() . "<br />" : '' ; ?>
331
		<?php echo ( wpsc_display_purchlog_buyers_country() != ""            ) ? wpsc_display_purchlog_buyers_country() . "<br />"            : '<span class="field-blank">' . __( 'Country not specified', 'wp-e-commerce' ) . '</span>' ; ?>
332
		<?php
333
	}
334
335
	public static function payment_details_output() {
336
		?>
337
		<strong><?php esc_html_e( 'Phone:', 'wp-e-commerce' ); ?> </strong><?php echo ( wpsc_display_purchlog_buyers_phone() != "" ) ? wpsc_display_purchlog_buyers_phone() : __( '<em class="field-blank">not provided</em>', 'wp-e-commerce' ); ?><br />
338
		<strong><?php esc_html_e( 'Email:', 'wp-e-commerce' ); ?> </strong>
339
			<a href="mailto:<?php echo wpsc_display_purchlog_buyers_email(); ?>?subject=<?php echo rawurlencode( sprintf( __( 'Message from %s', 'wp-e-commerce' ), site_url() ) ); ?>">
340
				<?php echo ( wpsc_display_purchlog_buyers_email() != "" ) ? wpsc_display_purchlog_buyers_email() : __( '<em class="field-blank">not provided</em>', 'wp-e-commerce' ); ?>
341
			</a>
342
		<br />
343
		<strong><?php esc_html_e( 'Payment Method:', 'wp-e-commerce' ); ?> </strong><?php echo wpsc_display_purchlog_paymentmethod(); ?><br />
344
		<?php if ( wpsc_display_purchlog_display_howtheyfoundus() ) : ?>
345
			<strong><?php esc_html_e( 'How User Found Us:', 'wp-e-commerce' ); ?> </strong><?php echo wpsc_display_purchlog_howtheyfoundus(); ?><br />
346
		<?php endif; ?>
347
		<?php
348
	}
349
350
	public function controller_item_details() {
351
		if (
352
			! isset( $_REQUEST['id'] )
353
			|| ( isset( $_REQUEST['id'] ) && ! is_numeric( $_REQUEST['id'] ) )
354
			|| ! $this->log->exists()
355
		) {
356
			wp_die( __( 'Invalid sales log ID', 'wp-e-commerce'  ) );
357
		}
358
359
		if ( isset( $_POST['wpsc_checkout_details'], $_POST['_wp_nonce'] ) ) {
360
			self::maybe_update_contact_details_for_log( $this->log, wp_unslash( $_POST['wpsc_checkout_details'] ) );
361
		}
362
363
		if ( isset( $_POST['wpsc_log_add_notes_nonce'], $_POST['purchlog_notes'] ) ) {
364
			self::maybe_add_note_to_log( $this->log, wp_unslash( $_POST['purchlog_notes'] ) );
365
		}
366
367
		if ( isset( $_REQUEST['delete-note'], $_REQUEST['note'] ) ) {
368
			self::maybe_delete_note_from_log( $this->log, absint( $_REQUEST['note'] ) );
369
		}
370
371
		$this->log->init_items();
372
373
		$columns = array(
374
			'title'    => __( 'Name', 'wp-e-commerce' ),
375
			'sku'      => __( 'SKU', 'wp-e-commerce' ),
376
			'quantity' => __( 'Quantity','wp-e-commerce' ),
377
			'price'    => __( 'Price', 'wp-e-commerce' ),
378
			'shipping' => __( 'Item Shipping', 'wp-e-commerce'),
379
		);
380
381
		if ( wpec_display_product_tax() ) {
382
			$columns['tax'] = __( 'Item Tax', 'wp-e-commerce' );
383
		}
384
385
		$columns['total'] = __( 'Item Total','wp-e-commerce' );
386
387
		if ( $this->can_edit ) {
388
			$columns['remove'] = '';
389
390
			$this->include_te_v2_resources();
391
			$this->enqueue_te_v2_resources();
392
		}
393
394
		add_filter( 'admin_title', array( $this, 'doc_title' ), 10, 2 );
395
396
		register_column_headers( 'wpsc_purchase_log_item_details', $columns );
397
398
		add_action( 'wpsc_display_purchase_logs_page', array( $this, 'display_purchase_log' ) );
399
		add_action( 'wpsc_purchlogitem_metabox_start', array( $this, 'purchase_log_custom_fields' ) );
400
	}
401
402
	public static function maybe_update_contact_details_for_log( WPSC_Purchase_Log $log, $details ) {
403
		if ( is_array( $details ) ) {
404
405
			check_admin_referer( 'wpsc-customer-settings-form', '_wp_nonce' );
406
407
			return WPSC_Checkout_Form_Data::save_form(
408
				$log,
409
				WPSC_Checkout_Form::get()->get_fields(),
410
				array_map( 'sanitize_text_field', $details ),
411
				false
412
			);
413
		}
414
	}
415
416
	/**
417
	 * Update Purchase Log Notes
418
	 *
419
	 * @param  WPSC_Purchase_Log  $log log object.
420
	 */
421
	public static function maybe_add_note_to_log( WPSC_Purchase_Log $log, $note ) {
422
		if ( $note ) {
423
			check_admin_referer( 'wpsc_log_add_notes_nonce', 'wpsc_log_add_notes_nonce' );
424
425
			wpsc_purchlogs_update_notes( $log, wp_kses_post( $note ) );
426
427
			wp_safe_redirect( esc_url_raw( remove_query_arg( 'wpsc_log_add_notes_nonce' ) ) );
428
			exit;
429
		}
430
	}
431
432
	public static function maybe_delete_note_from_log( WPSC_Purchase_Log $log, $note_id ) {
433
		if ( is_numeric( $note_id ) ) {
434
			check_admin_referer( 'delete-note', 'delete-note' );
435
436
			$notes = new WPSC_Purchase_Log_Notes( $log );
437
438
			$notes->remove( $note_id )->save();
439
440
			wp_safe_redirect( esc_url_raw( remove_query_arg( 'delete-note', remove_query_arg( 'note' ) ) ) . '#purchlogs_notes' );
441
			exit;
442
		}
443
	}
444
445
	public function include_te_v2_resources() {
446
		if ( ! defined( 'WPSC_TE_V2_CLASSES_PATH' ) ) {
447
			require_once WPSC_FILE_PATH . '/wpsc-components/theme-engine-v2/core.php';
448
			_wpsc_te_v2_includes();
449
		}
450
451
		require_once( WPSC_TE_V2_CLASSES_PATH . '/message-collection.php' );
452
		require_once( WPSC_TE_V2_HELPERS_PATH . '/message-collection.php' );
453
		require_once( WPSC_TE_V2_HELPERS_PATH . '/template-tags/form.php' );
454
	}
455
456
	public function enqueue_te_v2_resources() {
457
		_wpsc_te2_register_styles();
458
		wp_enqueue_style( 'wpsc-common' );
459
460
		$engine     = WPSC_Template_Engine::get_instance();
461
		$scripts    = $engine->get_core_scripts_data();
462
		$to_enqueue = array(
463
			'wpsc-select-autocomplete',
464
			'wpsc-country-region',
465
			'wpsc-copy-billing-info'
466
		);
467
468
		foreach ( $to_enqueue as $handle ) {
469
			wp_register_script(
470
				$handle,
471
				WPSC_TE_V2_URL . '/theming/assets/' . $scripts[ $handle ]['path'],
472
				$scripts[ $handle ]['dependencies'],
473
				$scripts[ $handle ]['version'],
474
				true
475
			);
476
			wpsc_enqueue_script( $handle );
477
		}
478
479
		wp_localize_script( 'wpsc-copy-billing-info', 'WPSC', array(
480
			'is_admin' => true,
481
		) );
482
483
		_wpsc_action_enqueue_shipping_billing_scripts();
484
485
		foreach ( $engine->get_queued_scripts() as $handle => $data ) {
486
			_wpsc_enqueue_and_localize_script( $handle, $data );
487
		}
488
	}
489
490
	public function doc_title( $admin_title, $title ) {
491
		/* translators: #%d represents the sales log id. */
492
		$this_title = sprintf( esc_html__( 'Sales Log #%d', 'wp-e-commerce' ), $this->log_id );
493
		$admin_title = str_replace( $title, $this_title, $admin_title );
494
495
		return $admin_title;
496
	}
497
498
	public function controller_packing_slip() {
499
		if ( ! isset( $_REQUEST['id'] ) || ( isset( $_REQUEST['id'] ) && ! is_numeric( $_REQUEST['id'] ) ) ) {
500
			wp_die( __( 'Invalid sales log ID', 'wp-e-commerce'  ) );
501
		}
502
503
		$this->log->init_items();
504
505
		$columns = array(
506
			'title'    => __( 'Item Name', 'wp-e-commerce' ),
507
			'sku'      => __( 'SKU', 'wp-e-commerce' ),
508
			'quantity' => __( 'Quantity', 'wp-e-commerce' ),
509
			'price'    => __( 'Price', 'wp-e-commerce' ),
510
			'shipping' => __( 'Item Shipping','wp-e-commerce' ),
511
		);
512
513
		if ( wpec_display_product_tax() ) {
514
			$columns['tax'] = __( 'Item Tax', 'wp-e-commerce' );
515
		}
516
517
		$columns['total'] = __( 'Item Total','wp-e-commerce' );
518
519
		$cols = count( $columns ) - 2;
520
521
		register_column_headers( 'wpsc_purchase_log_item_details', $columns );
522
523
		if ( file_exists( get_stylesheet_directory() . '/wpsc-packing-slip.php' ) ) {
524
			$packing_slip_file = get_stylesheet_directory() . '/wpsc-packing-slip.php';
525
		} else {
526
			$packing_slip_file = 'includes/purchase-logs-page/packing-slip.php';
527
		}
528
529
		$packing_slip_file = apply_filters( 'wpsc_packing_packing_slip_path', $packing_slip_file );
530
531
		include( $packing_slip_file );
532
533
		exit;
534
	}
535
536
	public function controller_default() {
537
		// Create an instance of our package class...
538
		$this->list_table = new WPSC_Purchase_Log_List_Table();
539
		$this->process_bulk_action();
540
		$this->list_table->prepare_items();
541
		add_action( 'wpsc_display_purchase_logs_page', array( $this, 'display_list_table' ) );
542
	}
543
544
	public function display_purchase_log() {
545
		$cols = 4;
546
		if ( wpec_display_product_tax() ) {
547
			$cols++;
548
		}
549
550
		if ( $this->can_edit ) {
551
			$cols++;
552
		}
553
554
		$receipt_sent = ! empty( $_GET['sent'] );
555
		$receipt_not_sent = isset( $_GET['sent'] ) && ! $_GET['sent'];
556
		include( 'includes/purchase-logs-page/item-details.php' );
557
558
		global $wp_scripts;
559
560
		wp_enqueue_script( 'wp-backbone' );
561
562
		if ( isset( $wp_scripts->registered['wp-e-commerce-purchase-logs'] ) ) {
563
			// JS needed for modal
564
			$wp_scripts->registered['wp-e-commerce-purchase-logs']->deps[] = 'wp-backbone';
565
		}
566
567
		add_action( 'admin_footer', 'find_posts_div' );
568
	}
569
570
	public function download_csv() {
571
		_wpsc_download_purchase_log_csv();
572
	}
573
574
	public function process_bulk_action() {
575
		global $wpdb;
576
		$current_action = $this->list_table->current_action();
577
578
		do_action( 'wpsc_sales_log_process_bulk_action', $current_action );
579
580
		if ( ! $current_action || ( 'download_csv' != $current_action && empty( $_REQUEST['post'] ) ) ) {
581
			if ( ! empty( $_REQUEST['_wp_http_referer'] ) ) {
582
				wp_redirect( esc_url_raw( remove_query_arg( array( '_wp_http_referer', '_wpnonce', 'action', 'action2' ), stripslashes( $_SERVER['REQUEST_URI'] ) ) ) );
583
				exit;
584
			}
585
586
			unset( $_REQUEST['post'] );
587
			return;
588
		}
589
590
		if ( 'download_csv' == $current_action ) {
591
			$this->download_csv();
592
		}
593
594
		$sendback = remove_query_arg( array(
595
			'_wpnonce',
596
			'_wp_http_referer',
597
			'action',
598
			'action2',
599
			'confirm',
600
			'post',
601
			'last_paged'
602
		) );
603
604
		if ( 'delete' == $current_action ) {
605
606
			// delete action
607
			if ( empty( $_REQUEST['confirm'] ) ) {
608
				$this->list_table->disable_search_box();
609
				$this->list_table->disable_bulk_actions();
610
				$this->list_table->disable_sortable();
611
				$this->list_table->disable_month_filter();
612
				$this->list_table->disable_views();
613
				$this->list_table->set_per_page(0);
614
				add_action( 'wpsc_purchase_logs_list_table_before', array( $this, 'action_list_table_before' ) );
615
				return;
616
			} else {
617
				if ( empty( $_REQUEST['post'] ) )
618
					return;
619
620
				$ids = array_map( 'intval', $_REQUEST['post'] );
621
622
				foreach ( $ids as $id ) {
623
					$log = new WPSC_Purchase_Log( $id );
624
					$log->delete();
625
				}
626
627
				$sendback = add_query_arg( array(
628
					'paged'   => $_REQUEST['last_paged'],
629
					'deleted' => count( $_REQUEST['post'] ),
630
				), $sendback );
631
632
			}
633
		}
634
635
		// change status actions
636
		if ( is_numeric( $current_action ) && ! empty( $_REQUEST['post'] ) ) {
637
638
			foreach ( $_REQUEST['post'] as $id )
639
				wpsc_purchlog_edit_status( $id, $current_action );
640
641
			$sendback = add_query_arg( array(
642
				'updated' => count( $_REQUEST['post'] ),
643
			), $sendback );
644
		}
645
646
		wp_redirect( esc_url_raw( $sendback ) );
647
		exit;
648
	}
649
650
	public function action_list_table_before() {
651
		include( 'includes/purchase-logs-page/bulk-delete-confirm.php' );
652
	}
653
654
	public function display_list_table() {
655
		if ( ! empty( $this->output ) ) {
656
			echo $this->output;
657
			return;
658
		}
659
660
		include( 'includes/purchase-logs-page/list-table.php' );
661
	}
662
663
	private function get_purchase_log_url( $id ) {
664
		$location = add_query_arg( array(
665
			'page' => 'wpsc-purchase-logs',
666
			'c'    => 'item_details',
667
			'id'   => $id,
668
		), admin_url( 'index.php' ) );
669
670
		return esc_url( $location );
671
	}
672
673
}
674