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

WPSC_Purchase_Log_Page   D

Complexity

Total Complexity 119

Size/Duplication

Total Lines 746
Duplicated Lines 0 %

Coupling/Cohesion

Components 3
Dependencies 6

Importance

Changes 0
Metric Value
dl 0
loc 746
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_pagination() 0 16 3
A purchase_logs_checkout_fields() 0 18 4
B purchase_log_custom_fields() 0 18 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 15 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
		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-pages'><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
		foreach( (array) $purchlogitem->additional_fields as $value ) {
0 ignored issues
show
Bug introduced by
The variable $purchlogitem does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

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