Completed
Push — master ( a97791...d8f22b )
by Justin
10:13 queued 05:09
created

WPSC_Purchase_Log_Page::download_csv()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

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