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

WPSC_Purchase_Log_Page::notes_output()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

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