Completed
Pull Request — master (#2165)
by Justin
05:08
created

WPSC_Purchase_Log_Page   F

Complexity

Total Complexity 97

Size/Duplication

Total Lines 624
Duplicated Lines 0 %

Coupling/Cohesion

Components 5
Dependencies 6

Importance

Changes 0
Metric Value
dl 0
loc 624
rs 1.5031
c 0
b 0
f 0
wmc 97
lcom 5
cbo 6

30 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
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 function controller_item_details() {
312
		if (
313
			! isset( $_REQUEST['id'] )
314
			|| ( isset( $_REQUEST['id'] ) && ! is_numeric( $_REQUEST['id'] ) )
315
			|| ! $this->log->exists()
316
		) {
317
			wp_die( __( 'Invalid sales log ID', 'wp-e-commerce'  ) );
318
		}
319
320
		if ( isset( $_POST['wpsc_checkout_details'], $_POST['_wp_nonce'] ) ) {
321
			self::maybe_update_contact_details_for_log( $this->log, wp_unslash( $_POST['wpsc_checkout_details'] ) );
322
		}
323
324
		if ( isset( $_POST['wpsc_log_add_notes_nonce'], $_POST['purchlog_notes'] ) ) {
325
			self::maybe_add_note_to_log( $this->log, wp_unslash( $_POST['purchlog_notes'] ) );
326
		}
327
328
		if ( isset( $_REQUEST['delete-note'], $_REQUEST['note'] ) ) {
329
			self::maybe_delete_note_from_log( $this->log, absint( $_REQUEST['note'] ) );
330
		}
331
332
		$this->log->init_items();
333
334
		$columns = array(
335
			'title'    => __( 'Name', 'wp-e-commerce' ),
336
			'sku'      => __( 'SKU', 'wp-e-commerce' ),
337
			'quantity' => __( 'Quantity','wp-e-commerce' ),
338
			'price'    => __( 'Price', 'wp-e-commerce' ),
339
			'shipping' => __( 'Item Shipping', 'wp-e-commerce'),
340
		);
341
342
		if ( wpec_display_product_tax() ) {
343
			$columns['tax'] = __( 'Item Tax', 'wp-e-commerce' );
344
		}
345
346
		$columns['total'] = __( 'Item Total','wp-e-commerce' );
347
348
		if ( $this->can_edit ) {
349
			$columns['remove'] = '';
350
351
			$this->include_te_v2_resources();
352
			$this->enqueue_te_v2_resources();
353
		}
354
355
		add_filter( 'admin_title', array( $this, 'doc_title' ), 10, 2 );
356
357
		register_column_headers( 'wpsc_purchase_log_item_details', $columns );
358
359
		add_action( 'wpsc_display_purchase_logs_page', array( $this, 'display_purchase_log' ) );
360
		add_action( 'wpsc_purchlogitem_metabox_start', array( $this, 'purchase_log_custom_fields' ) );
361
	}
362
363
	public static function maybe_update_contact_details_for_log( WPSC_Purchase_Log $log, $details ) {
364
		if ( is_array( $details ) ) {
365
366
			check_admin_referer( 'wpsc-customer-settings-form', '_wp_nonce' );
367
368
			return WPSC_Checkout_Form_Data::save_form(
369
				$log,
370
				WPSC_Checkout_Form::get()->get_fields(),
371
				array_map( 'sanitize_text_field', $details ),
372
				false
373
			);
374
		}
375
	}
376
377
	/**
378
	 * Update Purchase Log Notes
379
	 *
380
	 * @param  WPSC_Purchase_Log  $log log object.
381
	 */
382
	public static function maybe_add_note_to_log( WPSC_Purchase_Log $log, $note ) {
383
		if ( $note ) {
384
			check_admin_referer( 'wpsc_log_add_notes_nonce', 'wpsc_log_add_notes_nonce' );
385
386
			wpsc_purchlogs_update_notes( $log, wp_kses_post( $note ) );
387
388
			wp_safe_redirect( esc_url_raw( remove_query_arg( 'wpsc_log_add_notes_nonce' ) ) );
389
			exit;
390
		}
391
	}
392
393
	public static function maybe_delete_note_from_log( WPSC_Purchase_Log $log, $note_id ) {
394
		if ( is_numeric( $note_id ) ) {
395
			check_admin_referer( 'delete-note', 'delete-note' );
396
397
			$notes = new WPSC_Purchase_Log_Notes( $log );
398
399
			$notes->remove( $note_id )->save();
400
401
			wp_safe_redirect( esc_url_raw( remove_query_arg( 'delete-note', remove_query_arg( 'note' ) ) ) . '#purchlogs_notes' );
402
			exit;
403
		}
404
	}
405
406
	public function include_te_v2_resources() {
407
		if ( ! defined( 'WPSC_TE_V2_CLASSES_PATH' ) ) {
408
			require_once WPSC_FILE_PATH . '/wpsc-components/theme-engine-v2/core.php';
409
			_wpsc_te_v2_includes();
410
		}
411
412
		require_once( WPSC_TE_V2_CLASSES_PATH . '/message-collection.php' );
413
		require_once( WPSC_TE_V2_HELPERS_PATH . '/message-collection.php' );
414
		require_once( WPSC_TE_V2_HELPERS_PATH . '/template-tags/form.php' );
415
	}
416
417
	public function enqueue_te_v2_resources() {
418
		_wpsc_te2_register_styles();
419
		wp_enqueue_style( 'wpsc-common' );
420
421
		$engine     = WPSC_Template_Engine::get_instance();
422
		$scripts    = $engine->get_core_scripts_data();
423
		$to_enqueue = array(
424
			'wpsc-select-autocomplete',
425
			'wpsc-country-region',
426
			'wpsc-copy-billing-info'
427
		);
428
429
		foreach ( $to_enqueue as $handle ) {
430
			wp_register_script(
431
				$handle,
432
				WPSC_TE_V2_URL . '/theming/assets/' . $scripts[ $handle ]['path'],
433
				$scripts[ $handle ]['dependencies'],
434
				$scripts[ $handle ]['version'],
435
				true
436
			);
437
			wpsc_enqueue_script( $handle );
438
		}
439
440
		wp_localize_script( 'wpsc-copy-billing-info', 'WPSC', array(
441
			'is_admin' => true,
442
		) );
443
444
		_wpsc_action_enqueue_shipping_billing_scripts();
445
446
		foreach ( $engine->get_queued_scripts() as $handle => $data ) {
447
			_wpsc_enqueue_and_localize_script( $handle, $data );
448
		}
449
	}
450
451
	public function doc_title( $admin_title, $title ) {
452
		/* translators: #%d represents the sales log id. */
453
		$this_title = sprintf( esc_html__( 'Sales Log #%d', 'wp-e-commerce' ), $this->log_id );
454
		$admin_title = str_replace( $title, $this_title, $admin_title );
455
456
		return $admin_title;
457
	}
458
459
	public function controller_packing_slip() {
460
		if ( ! isset( $_REQUEST['id'] ) || ( isset( $_REQUEST['id'] ) && ! is_numeric( $_REQUEST['id'] ) ) ) {
461
			wp_die( __( 'Invalid sales log ID', 'wp-e-commerce'  ) );
462
		}
463
464
		$this->log->init_items();
465
466
		$columns = array(
467
			'title'    => __( 'Item Name', 'wp-e-commerce' ),
468
			'sku'      => __( 'SKU', 'wp-e-commerce' ),
469
			'quantity' => __( 'Quantity', 'wp-e-commerce' ),
470
			'price'    => __( 'Price', 'wp-e-commerce' ),
471
			'shipping' => __( 'Item Shipping','wp-e-commerce' ),
472
		);
473
474
		if ( wpec_display_product_tax() ) {
475
			$columns['tax'] = __( 'Item Tax', 'wp-e-commerce' );
476
		}
477
478
		$columns['total'] = __( 'Item Total','wp-e-commerce' );
479
480
		$cols = count( $columns ) - 2;
481
482
		register_column_headers( 'wpsc_purchase_log_item_details', $columns );
483
484
		if ( file_exists( get_stylesheet_directory() . '/wpsc-packing-slip.php' ) ) {
485
			$packing_slip_file = get_stylesheet_directory() . '/wpsc-packing-slip.php';
486
		} else {
487
			$packing_slip_file = 'includes/purchase-logs-page/packing-slip.php';
488
		}
489
490
		$packing_slip_file = apply_filters( 'wpsc_packing_packing_slip_path', $packing_slip_file );
491
492
		include( $packing_slip_file );
493
494
		exit;
495
	}
496
497
	public function controller_default() {
498
		// Create an instance of our package class...
499
		$this->list_table = new WPSC_Purchase_Log_List_Table();
500
		$this->process_bulk_action();
501
		$this->list_table->prepare_items();
502
		add_action( 'wpsc_display_purchase_logs_page', array( $this, 'display_list_table' ) );
503
	}
504
505
	public function display_purchase_log() {
506
		$cols = 4;
507
		if ( wpec_display_product_tax() ) {
508
			$cols++;
509
		}
510
511
		if ( $this->can_edit ) {
512
			$cols++;
513
		}
514
515
		$receipt_sent = ! empty( $_GET['sent'] );
516
		$receipt_not_sent = isset( $_GET['sent'] ) && ! $_GET['sent'];
517
		include( 'includes/purchase-logs-page/item-details.php' );
518
519
		global $wp_scripts;
520
521
		wp_enqueue_script( 'wp-backbone' );
522
523
		if ( isset( $wp_scripts->registered['wp-e-commerce-purchase-logs'] ) ) {
524
			// JS needed for modal
525
			$wp_scripts->registered['wp-e-commerce-purchase-logs']->deps[] = 'wp-backbone';
526
		}
527
528
		add_action( 'admin_footer', 'find_posts_div' );
529
	}
530
531
	public function download_csv() {
532
		_wpsc_download_purchase_log_csv();
533
	}
534
535
	public function process_bulk_action() {
536
		global $wpdb;
537
		$current_action = $this->list_table->current_action();
538
539
		do_action( 'wpsc_sales_log_process_bulk_action', $current_action );
540
541
		if ( ! $current_action || ( 'download_csv' != $current_action && empty( $_REQUEST['post'] ) ) ) {
542
			if ( ! empty( $_REQUEST['_wp_http_referer'] ) ) {
543
				wp_redirect( esc_url_raw( remove_query_arg( array( '_wp_http_referer', '_wpnonce', 'action', 'action2' ), stripslashes( $_SERVER['REQUEST_URI'] ) ) ) );
544
				exit;
545
			}
546
547
			unset( $_REQUEST['post'] );
548
			return;
549
		}
550
551
		if ( 'download_csv' == $current_action ) {
552
			$this->download_csv();
553
		}
554
555
		$sendback = remove_query_arg( array(
556
			'_wpnonce',
557
			'_wp_http_referer',
558
			'action',
559
			'action2',
560
			'confirm',
561
			'post',
562
			'last_paged'
563
		) );
564
565
		if ( 'delete' == $current_action ) {
566
567
			// delete action
568
			if ( empty( $_REQUEST['confirm'] ) ) {
569
				$this->list_table->disable_search_box();
570
				$this->list_table->disable_bulk_actions();
571
				$this->list_table->disable_sortable();
572
				$this->list_table->disable_month_filter();
573
				$this->list_table->disable_views();
574
				$this->list_table->set_per_page(0);
575
				add_action( 'wpsc_purchase_logs_list_table_before', array( $this, 'action_list_table_before' ) );
576
				return;
577
			} else {
578
				if ( empty( $_REQUEST['post'] ) )
579
					return;
580
581
				$ids = array_map( 'intval', $_REQUEST['post'] );
582
583
				foreach ( $ids as $id ) {
584
					$log = new WPSC_Purchase_Log( $id );
585
					$log->delete();
586
				}
587
588
				$sendback = add_query_arg( array(
589
					'paged'   => $_REQUEST['last_paged'],
590
					'deleted' => count( $_REQUEST['post'] ),
591
				), $sendback );
592
593
			}
594
		}
595
596
		// change status actions
597
		if ( is_numeric( $current_action ) && ! empty( $_REQUEST['post'] ) ) {
598
599
			foreach ( $_REQUEST['post'] as $id )
600
				wpsc_purchlog_edit_status( $id, $current_action );
601
602
			$sendback = add_query_arg( array(
603
				'updated' => count( $_REQUEST['post'] ),
604
			), $sendback );
605
		}
606
607
		wp_redirect( esc_url_raw( $sendback ) );
608
		exit;
609
	}
610
611
	public function action_list_table_before() {
612
		include( 'includes/purchase-logs-page/bulk-delete-confirm.php' );
613
	}
614
615
	public function display_list_table() {
616
		if ( ! empty( $this->output ) ) {
617
			echo $this->output;
618
			return;
619
		}
620
621
		include( 'includes/purchase-logs-page/list-table.php' );
622
	}
623
624
	private function get_purchase_log_url( $id ) {
625
		$location = add_query_arg( array(
626
			'page' => 'wpsc-purchase-logs',
627
			'c'    => 'item_details',
628
			'id'   => $id,
629
		), admin_url( 'index.php' ) );
630
631
		return esc_url( $location );
632
	}
633
634
}
635