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

WPSC_Purchase_Log_Page   F

Complexity

Total Complexity 109

Size/Duplication

Total Lines 648
Duplicated Lines 0 %

Coupling/Cohesion

Components 5
Dependencies 6

Importance

Changes 0
Metric Value
dl 0
loc 648
rs 1.4273
c 0
b 0
f 0
wmc 109
lcom 5
cbo 6

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