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

WPSC_Purchase_Log_Page   D

Complexity

Total Complexity 119

Size/Duplication

Total Lines 750
Duplicated Lines 0 %

Coupling/Cohesion

Components 3
Dependencies 6

Importance

Changes 0
Metric Value
dl 0
loc 750
rs 4.4444
c 0
b 0
f 0
wmc 119
lcom 3
cbo 6

36 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_checkout_fields() 0 20 4
A purchase_logs_pagination() 0 16 3
B purchase_log_custom_fields() 0 19 5
B items_ordered_box() 0 64 4
B purch_notes_box() 0 24 1
A edit_contact_details_form() 0 6 1
A purchase_log_cart_items() 0 5 2
B purchase_log_cart_item() 0 35 4
A notes_output() 0 5 2
A note_output() 0 14 1
B shipping_address_output() 0 11 6
B billing_address_output() 0 11 6
A payment_details_output() 0 14 4
C controller_item_details() 0 51 10
A register_metaboxes() 0 17 3
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
	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
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
	public function purchase_logs_checkout_fields() {
180
		global $purchlogitem;
181
182
		foreach( (array) $purchlogitem->additional_fields as $value ) {
183
			$value['value'] = maybe_unserialize( $value['value'] );
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
204
		if ( count( $files ) > 0 ) { ?>
205
			<h4><?php esc_html_e( 'Cart Items with Custom Files' , 'wp-e-commerce' ); ?>:</h4>
206
			<?php
207
			foreach( $files as $file ) {
208
				echo $file;
209
			}
210
		}
211
		if ( count( $messages ) > 0 ) { ?>
212
			<h4><?php esc_html_e( 'Cart Items with Custom Messages' , 'wp-e-commerce' ); ?>:</h4>
213
			<?php
214
			foreach( $messages as $message ) {
215
				echo esc_html( $message['title'] ) . ':<br />' . nl2br( esc_html( $message['message'] ) );
216
			}
217
		}
218
	}
219
220
	public function items_ordered_box() {
221
		?>
222
		<?php do_action( 'wpsc_purchlogitem_metabox_start', $this->log_id ); ?>
223
224
		<form name="wpsc_items_ordered_form" method="post">
225
			<table class="widefat" cellspacing="0">
226
				<thead>
227
				<tr>
228
					<?php
229
						print_column_headers( 'wpsc_purchase_log_item_details' );
230
					 ?>
231
				</tr>
232
				</thead>
233
234
				<tbody>
235
					<?php $this->purchase_log_cart_items(); ?>
236
237
					<?php if ( $this->can_edit ) : ?>
238
						<tr class="wpsc_purchaselog_add_product">
239
							<td colspan="<?php echo $this->cols + 2; ?>">
240
								<p class="wpsc-add-row">
241
									<button type="button" class="wpsc-add-item-button button"><?php esc_html_e( 'Add Item', 'wp-e-commerce' ); ?></button>
242
								</p>
243
							</td>
244
						</tr>
245
					<?php endif; ?>
246
247
					<tr class="wpsc_purchaselog_start_totals" id="wpsc_discount_data">
248
						<td colspan="<?php echo $this->cols; ?>">
249
							<?php if ( wpsc_purchlog_has_discount_data() ): ?>
250
								<?php esc_html_e( 'Coupon Code', 'wp-e-commerce' ); ?>: <?php echo wpsc_display_purchlog_discount_data(); ?>
251
							<?php endif; ?>
252
						</td>
253
						<th class='right-col'><?php esc_html_e( 'Discount', 'wp-e-commerce' ); ?> </th>
254
						<td><?php echo wpsc_display_purchlog_discount(); ?></td>
255
					</tr>
256
257
					<?php if( ! wpec_display_product_tax() ): ?>
258
						<tr id="wpsc_total_taxes">
259
							<td colspan='<?php echo $this->cols; ?>'></td>
260
							<th class='right-col'><?php esc_html_e( 'Taxes', 'wp-e-commerce' ); ?> </th>
261
							<td><?php echo wpsc_display_purchlog_taxes(); ?></td>
262
						</tr>
263
					<?php endif; ?>
264
265
					<tr id="wpsc_total_shipping">
266
						<td colspan='<?php echo $this->cols; ?>'></td>
267
						<th class='right-col'><?php esc_html_e( 'Shipping', 'wp-e-commerce' ); ?> </th>
268
						<td><?php echo wpsc_display_purchlog_shipping( false, true ); ?></td>
269
					</tr>
270
					<tr id="wpsc_final_total">
271
						<td colspan='<?php echo $this->cols; ?>'></td>
272
						<th class='right-col'><?php esc_html_e( 'Total', 'wp-e-commerce' ); ?> </th>
273
						<td><span><?php echo wpsc_display_purchlog_totalprice(); ?></span> <div class="spinner"></div></td>
274
					</tr>
275
				</tbody>
276
			</table>
277
278
		</form>
279
280
		<?php do_action( 'wpsc_purchlogitem_metabox_end', $this->log_id ); ?>
281
282
		<?php
283
	}
284
285
	public function purch_notes_box() {
286
		?>
287
		<div class="wpsc-notes">
288
			<?php $this->notes_output(); ?>
289
		</div>
290
		<form method="post" action="" id="note-submit-form">
291
			<?php wp_nonce_field( 'wpsc_log_add_notes_nonce', 'wpsc_log_add_notes_nonce' ); ?>
292
			<input type='hidden' name='purchlog_id' value='<?php echo $this->log_id; ?>' />
293
			<p>
294
			<?php wp_editor( '', 'purchlog_notes', array(
295
				'textarea_name' => 'purchlog_notes',
296
				'textarea_rows' => 3,
297
				'teeny'         => true,
298
				'tinymce'       => false,
299
				'media_buttons' => false,
300
			) ); ?>
301
			</p>
302
			<div class="note-submit">
303
				<input class="button" type="submit" value="<?php _e( 'Add Note', 'wp-e-commerce' ); ?>" />
304
				<div class="spinner"></div>
305
			</div>
306
		</form>
307
		<?php
308
	}
309
310
	private function edit_contact_details_form() {
311
		$args = wpsc_get_customer_settings_form_args( $this->log->form_data() );
312
		$args['form_actions'][0]['class'] = 'button';
313
		$args['form_actions'][0]['title'] = __( 'Update', 'wp-e-commerce' );
314
		echo wpsc_get_form_output( $args );
315
	}
316
317
	private function purchase_log_cart_items() {
318
		while( wpsc_have_purchaselog_details() ) : wpsc_the_purchaselog_item();
319
			self::purchase_log_cart_item( $this->can_edit );
320
		endwhile;
321
	}
322
323
	public static function purchase_log_cart_item( $can_edit = false ) {
324
		?>
325
		<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(); ?>">
326
			<td><?php echo wpsc_purchaselog_details_name(); ?></td> <!-- NAME! -->
327
			<td><?php echo wpsc_purchaselog_details_SKU(); ?></td> <!-- SKU! -->
328
			<td>
329
				<?php if ( $can_edit ) : ?>
330
					<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">
331
				<?php else: ?>
332
					<?php echo wpsc_purchaselog_details_quantity(); ?>
333
				<?php endif; ?>
334
			</td> <!-- QUANTITY! -->
335
			<td>
336
		 <?php
337
		echo wpsc_currency_display( wpsc_purchaselog_details_price() );
338
		do_action( 'wpsc_additional_sales_amount_info', wpsc_purchaselog_details_id() );
339
		 ?>
340
	 </td> <!-- PRICE! -->
341
			<td><?php echo wpsc_currency_display( wpsc_purchaselog_details_shipping() ); ?></td> <!-- SHIPPING! -->
342
			<?php if( wpec_display_product_tax() ): ?>
343
				<td><?php echo wpsc_currency_display( wpsc_purchaselog_details_tax() ); ?></td> <!-- TAX! -->
344
			<?php endif; ?>
345
			<!-- <td><?php echo wpsc_currency_display( wpsc_purchaselog_details_discount() ); ?></td> --> <!-- DISCOUNT! -->
346
			<td class="amount"><?php echo wpsc_currency_display( wpsc_purchaselog_details_total() ); ?></td> <!-- TOTAL! -->
347
			<?php if ( $can_edit ) : ?>
348
				<td class="remove">
349
					<div class="wpsc-remove-row">
350
						<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>
351
					</div>
352
				</td> <!-- REMOVE! -->
353
			<?php endif; ?>
354
		</tr>
355
		<?php
356
		do_action( 'wpsc_additional_sales_item_info', wpsc_purchaselog_details_id() );
357
	}
358
359
	public function notes_output() {
360
		foreach ( $this->notes as $note_id => $note_args ) : ?>
361
			<?php self::note_output( $this->notes, $note_id, $note_args ); ?>
362
		<?php endforeach;
363
	}
364
365
	public static function note_output( WPSC_Purchase_Log_Notes $notes, $note_id, array $note_args ) {
366
		?>
367
		<div class="wpsc-note" id="wpsc-note-<?php echo absint( $note_id ); ?>" data-id="<?php echo absint( $note_id ); ?>">
368
			<p>
369
				<strong class="note-date"><?php echo $notes->get_formatted_date( $note_args ); ?></strong>
370
				<a href="#wpsc-note-<?php echo absint( $note_id ); ?>" class="note-number">#<?php echo ( $note_id ); ?></a>
371
				<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>
372
			</p>
373
			<div class="wpsc-note-content">
374
				<?php echo wpautop( $note_args['content'] ); ?>
375
			</div>
376
		</div>
377
		<?php
378
	}
379
380
	public static function shipping_address_output() {
381
		?>
382
		<strong>
383
			<?php echo ( wpsc_display_purchlog_shipping_name() != ""           ) ? wpsc_display_purchlog_shipping_name() . "<br />"               : '<span class="field-blank">' . __( 'Anonymous', 'wp-e-commerce' ) . '</span>' ; ?>
384
		</strong>
385
		<?php echo ( wpsc_display_purchlog_shipping_address() != ""            ) ? wpsc_display_purchlog_shipping_address() . "<br />"            : '' ; ?>
386
		<?php echo ( wpsc_display_purchlog_shipping_city() != ""               ) ? wpsc_display_purchlog_shipping_city() . ", "               : '' ; ?>
387
		<?php echo ( wpsc_display_purchlog_shipping_state_and_postcode() != "" ) ? wpsc_display_purchlog_shipping_state_and_postcode() . "<br />" : '' ; ?>
388
		<?php echo ( wpsc_display_purchlog_shipping_country() != ""            ) ? wpsc_display_purchlog_shipping_country() . "<br />"            : '<span class="field-blank">' . __( 'Country not specified', 'wp-e-commerce' ) . '</span>' ; ?>
389
		<?php
390
	}
391
392
	public static function billing_address_output() {
393
		?>
394
		<strong>
395
			<?php echo ( wpsc_display_purchlog_buyers_name() != ""           ) ? wpsc_display_purchlog_buyers_name() . "<br />"               : '<span class="field-blank">' . __( 'Anonymous', 'wp-e-commerce' ) . '</span>' ; ?>
396
		</strong>
397
		<?php echo ( wpsc_display_purchlog_buyers_address() != ""            ) ? wpsc_display_purchlog_buyers_address() . "<br />"            : '' ; ?>
398
		<?php echo ( wpsc_display_purchlog_buyers_city() != ""               ) ? wpsc_display_purchlog_buyers_city() . ", "               : '' ; ?>
399
		<?php echo ( wpsc_display_purchlog_buyers_state_and_postcode() != "" ) ? wpsc_display_purchlog_buyers_state_and_postcode() . "<br />" : '' ; ?>
400
		<?php echo ( wpsc_display_purchlog_buyers_country() != ""            ) ? wpsc_display_purchlog_buyers_country() . "<br />"            : '<span class="field-blank">' . __( 'Country not specified', 'wp-e-commerce' ) . '</span>' ; ?>
401
		<?php
402
	}
403
404
	public static function payment_details_output() {
405
		?>
406
		<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 />
407
		<strong><?php esc_html_e( 'Email:', 'wp-e-commerce' ); ?> </strong>
408
			<a href="mailto:<?php echo wpsc_display_purchlog_buyers_email(); ?>?subject=<?php echo rawurlencode( sprintf( __( 'Message from %s', 'wp-e-commerce' ), site_url() ) ); ?>">
409
				<?php echo ( wpsc_display_purchlog_buyers_email() != "" ) ? wpsc_display_purchlog_buyers_email() : __( '<em class="field-blank">not provided</em>', 'wp-e-commerce' ); ?>
410
			</a>
411
		<br />
412
		<strong><?php esc_html_e( 'Payment Method:', 'wp-e-commerce' ); ?> </strong><?php echo wpsc_display_purchlog_paymentmethod(); ?><br />
413
		<?php if ( wpsc_display_purchlog_display_howtheyfoundus() ) : ?>
414
			<strong><?php esc_html_e( 'How User Found Us:', 'wp-e-commerce' ); ?> </strong><?php echo wpsc_display_purchlog_howtheyfoundus(); ?><br />
415
		<?php endif; ?>
416
		<?php
417
	}
418
419
	public function controller_item_details() {
420
		if (
421
			! isset( $_REQUEST['id'] )
422
			|| ( isset( $_REQUEST['id'] ) && ! is_numeric( $_REQUEST['id'] ) )
423
			|| ! $this->log->exists()
424
		) {
425
			wp_die( __( 'Invalid sales log ID', 'wp-e-commerce'  ) );
426
		}
427
428
		if ( isset( $_POST['wpsc_checkout_details'], $_POST['_wp_nonce'] ) ) {
429
			self::maybe_update_contact_details_for_log( $this->log, wp_unslash( $_POST['wpsc_checkout_details'] ) );
430
		}
431
432
		if ( isset( $_POST['wpsc_log_add_notes_nonce'], $_POST['purchlog_notes'] ) ) {
433
			self::maybe_add_note_to_log( $this->log, wp_unslash( $_POST['purchlog_notes'] ) );
434
		}
435
436
		if ( isset( $_REQUEST['delete-note'], $_REQUEST['note'] ) ) {
437
			self::maybe_delete_note_from_log( $this->log, absint( $_REQUEST['note'] ) );
438
		}
439
440
		$this->log->init_items();
441
442
		$columns = array(
443
			'title'    => __( 'Name', 'wp-e-commerce' ),
444
			'sku'      => __( 'SKU', 'wp-e-commerce' ),
445
			'quantity' => __( 'Quantity','wp-e-commerce' ),
446
			'price'    => __( 'Price', 'wp-e-commerce' ),
447
			'shipping' => __( 'Item Shipping', 'wp-e-commerce'),
448
		);
449
450
		if ( wpec_display_product_tax() ) {
451
			$columns['tax'] = __( 'Item Tax', 'wp-e-commerce' );
452
		}
453
454
		$columns['total'] = __( 'Item Total','wp-e-commerce' );
455
456
		if ( $this->can_edit ) {
457
			$columns['remove'] = '';
458
459
			$this->include_te_v2_resources();
460
			$this->enqueue_te_v2_resources();
461
		}
462
463
		add_filter( 'admin_title', array( $this, 'doc_title' ), 10, 2 );
464
465
		register_column_headers( 'wpsc_purchase_log_item_details', $columns );
466
467
		add_action( 'wpsc_display_purchase_logs_page', array( $this, 'display_purchase_log' ) );
468
		add_action( 'wpsc_purchlog_before_metaboxes' , array( $this, 'register_metaboxes' ) );
469
	}
470
471
	public function register_metaboxes() {
472
		global $purchlogitem;
473
474
		add_meta_box( 'wpsc_items_ordered', esc_html__( 'Items Ordered' , 'wp-e-commerce' ), array( $this, 'items_ordered_box' ), get_current_screen()->id, 'normal' );
475
476
		add_meta_box( 'purchlogs_notes', esc_html__( 'Order Notes' , 'wp-e-commerce' ), array( $this, 'purch_notes_box' ), get_current_screen()->id, 'low' );
477
478
		if ( wpsc_purchlogs_has_customfields() ) {
479
			add_meta_box( 'purchlogs_customfields', esc_html__( 'Users Custom Fields' , 'wp-e-commerce' ), array( $this, 'purchase_log_custom_fields' ), get_current_screen()->id, 'normal' );
480
		}
481
482
		if ( ! empty( $purchlogitem->additional_fields ) ) {
483
			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' );
484
		}
485
486
		do_action( 'wpsc_purchase_logs_register_metaboxes', get_current_screen(), $this );
487
	}
488
489
	public static function maybe_update_contact_details_for_log( WPSC_Purchase_Log $log, $details ) {
490
		if ( is_array( $details ) ) {
491
492
			check_admin_referer( 'wpsc-customer-settings-form', '_wp_nonce' );
493
494
			return WPSC_Checkout_Form_Data::save_form(
495
				$log,
496
				WPSC_Checkout_Form::get()->get_fields(),
497
				array_map( 'sanitize_text_field', $details ),
498
				false
499
			);
500
		}
501
	}
502
503
	/**
504
	 * Update Purchase Log Notes
505
	 *
506
	 * @param  WPSC_Purchase_Log  $log log object.
507
	 */
508
	public static function maybe_add_note_to_log( WPSC_Purchase_Log $log, $note ) {
509
		if ( $note ) {
510
			check_admin_referer( 'wpsc_log_add_notes_nonce', 'wpsc_log_add_notes_nonce' );
511
512
			wpsc_purchlogs_update_notes( $log, wp_kses_post( $note ) );
513
514
			wp_safe_redirect( esc_url_raw( remove_query_arg( 'wpsc_log_add_notes_nonce' ) ) );
515
			exit;
516
		}
517
	}
518
519
	public static function maybe_delete_note_from_log( WPSC_Purchase_Log $log, $note_id ) {
520
		if ( is_numeric( $note_id ) ) {
521
			check_admin_referer( 'delete-note', 'delete-note' );
522
523
			$notes = new WPSC_Purchase_Log_Notes( $log );
524
525
			$notes->remove( $note_id )->save();
526
527
			wp_safe_redirect( esc_url_raw( remove_query_arg( 'delete-note', remove_query_arg( 'note' ) ) ) . '#purchlogs_notes' );
528
			exit;
529
		}
530
	}
531
532
	public function include_te_v2_resources() {
533
		if ( ! defined( 'WPSC_TE_V2_CLASSES_PATH' ) ) {
534
			require_once WPSC_FILE_PATH . '/wpsc-components/theme-engine-v2/core.php';
535
			_wpsc_te_v2_includes();
536
		}
537
538
		require_once( WPSC_TE_V2_CLASSES_PATH . '/message-collection.php' );
539
		require_once( WPSC_TE_V2_HELPERS_PATH . '/message-collection.php' );
540
		require_once( WPSC_TE_V2_HELPERS_PATH . '/template-tags/form.php' );
541
	}
542
543
	public function enqueue_te_v2_resources() {
544
		_wpsc_te2_register_styles();
545
		wp_enqueue_style( 'wpsc-common' );
546
547
		$engine     = WPSC_Template_Engine::get_instance();
548
		$scripts    = $engine->get_core_scripts_data();
549
		$to_enqueue = array(
550
			'wpsc-select-autocomplete',
551
			'wpsc-country-region',
552
			'wpsc-copy-billing-info'
553
		);
554
555
		foreach ( $to_enqueue as $handle ) {
556
			wp_register_script(
557
				$handle,
558
				WPSC_TE_V2_URL . '/theming/assets/' . $scripts[ $handle ]['path'],
559
				$scripts[ $handle ]['dependencies'],
560
				$scripts[ $handle ]['version'],
561
				true
562
			);
563
			wpsc_enqueue_script( $handle );
564
		}
565
566
		wp_localize_script( 'wpsc-copy-billing-info', 'WPSC', array(
567
			'is_admin' => true,
568
		) );
569
570
		_wpsc_action_enqueue_shipping_billing_scripts();
571
572
		foreach ( $engine->get_queued_scripts() as $handle => $data ) {
573
			_wpsc_enqueue_and_localize_script( $handle, $data );
574
		}
575
	}
576
577
	public function doc_title( $admin_title, $title ) {
578
		/* translators: #%d represents the sales log id. */
579
		$this_title = sprintf( esc_html__( 'Sales Log #%d', 'wp-e-commerce' ), $this->log_id );
580
		$admin_title = str_replace( $title, $this_title, $admin_title );
581
582
		return $admin_title;
583
	}
584
585
	public function controller_packing_slip() {
586
		if ( ! isset( $_REQUEST['id'] ) || ( isset( $_REQUEST['id'] ) && ! is_numeric( $_REQUEST['id'] ) ) ) {
587
			wp_die( __( 'Invalid sales log ID', 'wp-e-commerce'  ) );
588
		}
589
590
		$this->log->init_items();
591
592
		$columns = array(
593
			'title'    => __( 'Item Name', 'wp-e-commerce' ),
594
			'sku'      => __( 'SKU', 'wp-e-commerce' ),
595
			'quantity' => __( 'Quantity', 'wp-e-commerce' ),
596
			'price'    => __( 'Price', 'wp-e-commerce' ),
597
			'shipping' => __( 'Item Shipping','wp-e-commerce' ),
598
		);
599
600
		if ( wpec_display_product_tax() ) {
601
			$columns['tax'] = __( 'Item Tax', 'wp-e-commerce' );
602
		}
603
604
		$columns['total'] = __( 'Item Total','wp-e-commerce' );
605
606
		$this->cols = count( $columns ) - 2;
607
608
		register_column_headers( 'wpsc_purchase_log_item_details', $columns );
609
610
		if ( file_exists( get_stylesheet_directory() . '/wpsc-packing-slip.php' ) ) {
611
			$packing_slip_file = get_stylesheet_directory() . '/wpsc-packing-slip.php';
612
		} else {
613
			$packing_slip_file = 'includes/purchase-logs-page/packing-slip.php';
614
		}
615
616
		$packing_slip_file = apply_filters( 'wpsc_packing_packing_slip_path', $packing_slip_file );
617
618
		include( $packing_slip_file );
619
620
		exit;
621
	}
622
623
	public function controller_default() {
624
		// Create an instance of our package class...
625
		$this->list_table = new WPSC_Purchase_Log_List_Table();
626
		$this->process_bulk_action();
627
		$this->list_table->prepare_items();
628
		add_action( 'wpsc_display_purchase_logs_page', array( $this, 'display_list_table' ) );
629
	}
630
631
	public function display_purchase_log() {
632
		$this->cols = 4;
633
		if ( wpec_display_product_tax() ) {
634
			$this->cols++;
635
		}
636
637
		if ( $this->can_edit ) {
638
			$this->cols++;
639
		}
640
641
		$receipt_sent = ! empty( $_GET['sent'] );
642
		$receipt_not_sent = isset( $_GET['sent'] ) && ! $_GET['sent'];
643
		include( 'includes/purchase-logs-page/item-details.php' );
644
645
		global $wp_scripts;
646
647
		wp_enqueue_script( 'wp-backbone' );
648
649
		if ( isset( $wp_scripts->registered['wp-e-commerce-purchase-logs'] ) ) {
650
			// JS needed for modal
651
			$wp_scripts->registered['wp-e-commerce-purchase-logs']->deps[] = 'wp-backbone';
652
		}
653
654
		add_action( 'admin_footer', 'find_posts_div' );
655
	}
656
657
	public function download_csv() {
658
		_wpsc_download_purchase_log_csv();
659
	}
660
661
	public function process_bulk_action() {
662
		global $wpdb;
663
		$current_action = $this->list_table->current_action();
664
665
		do_action( 'wpsc_sales_log_process_bulk_action', $current_action );
666
667
		if ( ! $current_action || ( 'download_csv' != $current_action && empty( $_REQUEST['post'] ) ) ) {
668
			if ( ! empty( $_REQUEST['_wp_http_referer'] ) ) {
669
				wp_redirect( esc_url_raw( remove_query_arg( array( '_wp_http_referer', '_wpnonce', 'action', 'action2' ), stripslashes( $_SERVER['REQUEST_URI'] ) ) ) );
670
				exit;
671
			}
672
673
			unset( $_REQUEST['post'] );
674
			return;
675
		}
676
677
		if ( 'download_csv' == $current_action ) {
678
			$this->download_csv();
679
		}
680
681
		$sendback = remove_query_arg( array(
682
			'_wpnonce',
683
			'_wp_http_referer',
684
			'action',
685
			'action2',
686
			'confirm',
687
			'post',
688
			'last_paged'
689
		) );
690
691
		if ( 'delete' == $current_action ) {
692
693
			// delete action
694
			if ( empty( $_REQUEST['confirm'] ) ) {
695
				$this->list_table->disable_search_box();
696
				$this->list_table->disable_bulk_actions();
697
				$this->list_table->disable_sortable();
698
				$this->list_table->disable_month_filter();
699
				$this->list_table->disable_views();
700
				$this->list_table->set_per_page(0);
701
				add_action( 'wpsc_purchase_logs_list_table_before', array( $this, 'action_list_table_before' ) );
702
				return;
703
			} else {
704
				if ( empty( $_REQUEST['post'] ) )
705
					return;
706
707
				$ids = array_map( 'intval', $_REQUEST['post'] );
708
709
				foreach ( $ids as $id ) {
710
					$log = new WPSC_Purchase_Log( $id );
711
					$log->delete();
712
				}
713
714
				$sendback = add_query_arg( array(
715
					'paged'   => $_REQUEST['last_paged'],
716
					'deleted' => count( $_REQUEST['post'] ),
717
				), $sendback );
718
719
			}
720
		}
721
722
		// change status actions
723
		if ( is_numeric( $current_action ) && ! empty( $_REQUEST['post'] ) ) {
724
725
			foreach ( $_REQUEST['post'] as $id )
726
				wpsc_purchlog_edit_status( $id, $current_action );
727
728
			$sendback = add_query_arg( array(
729
				'updated' => count( $_REQUEST['post'] ),
730
			), $sendback );
731
		}
732
733
		wp_redirect( esc_url_raw( $sendback ) );
734
		exit;
735
	}
736
737
	public function action_list_table_before() {
738
		include( 'includes/purchase-logs-page/bulk-delete-confirm.php' );
739
	}
740
741
	public function display_list_table() {
742
		if ( ! empty( $this->output ) ) {
743
			echo $this->output;
744
			return;
745
		}
746
747
		include( 'includes/purchase-logs-page/list-table.php' );
748
	}
749
750
	private function get_purchase_log_url( $id ) {
751
		$location = add_query_arg( array(
752
			'page' => 'wpsc-purchase-logs',
753
			'c'    => 'item_details',
754
			'id'   => $id,
755
		), admin_url( 'index.php' ) );
756
757
		return esc_url( $location );
758
	}
759
760
}
761