Completed
Push — master ( 9a3ee6...bca146 )
by Justin
05:42
created

purchase-log.helpers.php ➔ wpsc_update_order_status_fully_refunded()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 2
nop 1
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * Returns a purchase log.
5
 *
6
 * @since 3.11.5
7
 *
8
 * @param int $order_id Order ID.
9
 *
10
 * @return WPSC_Purchase_Log
11
 */
12
function wpsc_get_order( $order_id, $by = 'id' ) {
13
	$order = wpsc_is_order( $order_id );
14
	return $order ? $order : new WPSC_Purchase_Log( $order_id, $by );
15
}
16
17
/**
18
 * Determines if object is a an order (WPSC_Purchase_Log).
19
 *
20
 * @since 3.12.0
21
 *
22
 * @param mixed $order Object to check
23
 *
24
 * @return WPSC_Purchase_Log|bool
0 ignored issues
show
Documentation introduced by
Consider making the return type a bit more specific; maybe use WPSC_Purchase_Log|false.

This check looks for the generic type array as a return type and suggests a more specific type. This type is inferred from the actual code.

Loading history...
25
 */
26
function wpsc_is_order( $order ) {
27
	return $order instanceof WPSC_Purchase_Log ? $order : false;
28
}
29
30
/**
31
 * Returns a purchase log's notes object.
32
 *
33
 * @since 3.11.5
34
 *
35
 * @param int $order_id Order ID or WPSC_Purchase_Log object.
36
 *
37
 * @return WPSC_Purchase_Log
38
 */
39
function wpsc_get_order_notes( $order_id ) {
40
	return new WPSC_Purchase_Log_Notes( $order_id );
41
}
42
43
function wpsc_get_plaintext_table( $headings, $rows ) {
44
	$colwidths = array();
45
	$output    = array();
46
	$alignment = array_values( $headings );
47
	$headings  = array_keys( $headings );
48
	foreach ( $headings as $heading ) {
49
		$colwidths[] = strlen( $heading );
50
	}
51
52
	foreach ( $rows as $row ) {
53
		$i = 0;
54
		foreach ( $row as $col ) {
55
			$colwidths[$i] = max( strlen( $col ), $colwidths[$i] );
56
			$i ++;
57
		}
58
	}
59
60
	foreach ( $rows as &$row ) {
61
		$i = 0;
62
		foreach ( $row as &$col ) {
63
			$align = ( $alignment[$i] == 'left' ) ? STR_PAD_RIGHT : STR_PAD_LEFT;
64
			$col = str_pad( $col, $colwidths[$i], ' ', $align );
65
			$i ++;
66
		}
67
		$output[] = implode( '  ', $row );
68
	}
69
70
	$line = array();
71
	$i = 0;
72
73
	foreach ( $colwidths as $width ) {
74
		$line[] = str_repeat( '-', $width );
75
		$headings[$i] = str_pad( $headings[$i], $width );
76
		$i ++;
77
	}
78
79
	$line = implode( '--', $line );
80
	array_unshift( $output, $line );
81
	if ( ! empty( $headings ) ) {
82
		array_unshift( $output, implode( '  ', $headings ) );
83
		array_unshift( $output, $line );
84
	}
85
	$output[] = $line;
86
87
	return implode( "\r\n", $output ) . "\r\n";
88
}
89
90
function wpsc_update_purchase_log_status( $log_id, $new_status, $by = 'id' ) {
91
	$log = wpsc_get_order( $log_id, $by );
92
93
	$old_status = $log->get( 'processed' );
94
	$log->set( 'processed', $new_status );
95
96
	return $log->save();
97
}
98
99
function wpsc_update_purchase_log_details( $log_id, $details, $by = 'id' ) {
100
	$log = wpsc_get_order( $log_id, $by );
101
	$log->set( $details );
102
103
	return $log->save();
104
}
105
106
function wpsc_get_downloadable_links( $purchase_log ) {
107
	if ( ! $purchase_log->is_transaction_completed() ) {
108
		return array();
109
	}
110
111
	$cart_contents = $purchase_log->get_items();
112
	$links = array();
113
	foreach ( $cart_contents as $item ) {
114
		$item_links = _wpsc_get_cart_item_downloadable_links( $item, $purchase_log );
115
		if ( empty( $item_links ) ) {
116
			continue;
117
		}
118
		$links[$item->name] = $item_links;
119
	}
120
121
	return apply_filters( 'wpsc_get_downloadable_links', $links, $purchase_log );
122
}
123
124
function _wpsc_get_cart_item_downloadable_links( $item, $purchase_log ) {
125
	global $wpdb;
126
	$sql = $wpdb->prepare("
127
			SELECT *
128
			FROM `" . WPSC_TABLE_DOWNLOAD_STATUS . "`
129
			WHERE `active` = '1'
130
			AND `purchid` = %d
131
			AND `cartid` = %d
132
			", $purchase_log->get( 'id' ), $item->id
133
	);
134
135
	$results = $wpdb->get_results( $sql );
136
	$links = array();
137
138
	foreach ( $results as $single_download ) {
139
		$file_data = get_post( $single_download->product_id );
140
		$args = array(
141
			'post_type'   => 'wpsc-product-file',
142
			'post_parent' => $single_download->product_id,
143
			'numberposts' => -1,
0 ignored issues
show
introduced by
Disabling pagination is prohibited in VIP context, do not set numberposts to -1 ever.
Loading history...
144
			'post_status' => 'all',
145
		);
146
		$download_file_posts = (array) get_posts( $args );
147
		foreach( $download_file_posts as $single_file_post ) {
148
			if( $single_file_post->ID == $single_download->fileid ) {
149
				$current_Dl_product_file_post = $single_file_post;
150
				break;
151
			}
152
		}
153
154
		$file_name = $current_Dl_product_file_post->post_title;
0 ignored issues
show
Bug introduced by
The variable $current_Dl_product_file_post does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
155
		$downloadid = is_null( $single_download->uniqueid ) ? $single_download->id : $single_download->uniqueid;
156
		$links[] = array(
157
			'url' => add_query_arg( 'downloadid', $downloadid, home_url( '/' ) ),
158
			'name' => $file_name
159
		);
160
	}
161
162
	return $links;
163
}
164
165
function wpsc_get_purchase_log_html_table( $headings, $rows ) {
166
	ob_start();
167
168
	?>
169
	<table class="wpsc-purchase-log-transaction-results">
170
		<?php if ( ! empty( $headings ) ): ?>
171
			<thead>
172
				<?php foreach ( $headings as $heading => $align ): ?>
173
					<th><?php echo esc_html( $heading ); ?></th>
174
				<?php endforeach; ?>
175
			</thead>
176
		<?php endif; ?>
177
		<tbody>
178
			<?php foreach ( $rows as $row ): ?>
179
				<tr>
180
					<?php foreach ( $row as $col ): ?>
181
						<td><?php echo esc_html( $col ); ?></td>
182
					<?php endforeach; ?>
183
				</tr>
184
			<?php endforeach; ?>
185
		</tbody>
186
	</table>
187
	<?php
188
	$output = ob_get_clean();
189
	$output = apply_filters( 'wpsc_get_purchase_log_html_table', $output, $headings, $rows );
190
	return $output;
191
}
192
193
function _wpsc_process_transaction_coupon( $purchase_log ) {
194
	global $wpdb;
195
196
	if ( ! is_object( $purchase_log ) )
197
		$purchase_log = wpsc_get_order( $purchase_log );
198
199
	$discount_data = $purchase_log->get( 'discount_data' );
200
	if ( ! empty( $discount_data ) ) {
201
202
		$coupon_data = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM `" . WPSC_TABLE_COUPON_CODES . "` WHERE coupon_code = %s LIMIT 1", $discount_data ), ARRAY_A );
203
204
		$coupon = new WPSC_Coupon( $coupon_data['id'] );
205
		$coupon->used();
206
207
	}
208
}
209
210
/**
211
 * Routine that runs when updating a purchase log's status.
212
 * Currently, only used to send customer and admin emails upon successful purchase.
213
 *
214
 * @since  3.8.9
215
 * @since  3.11.5 Removed coupons and stocks from email sending.  Much easier now to remove_action() on either
216
 *                of those functions when desiring to override.
217
 *
218
 * @param  int               $id             Purchase Log ID.
219
 * @param  int               $status         Current status.
220
 * @param  int               $old_status     Previous status.
221
 * @param  WPSC_Purchase_Log $purchase_log   Purchase Log Object.
222
 *
223
 * @return void
224
 */
225
function _wpsc_action_update_purchase_log_status( $id, $status, $old_status, $purchase_log ) {
0 ignored issues
show
Unused Code introduced by
The parameter $id is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $status is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $old_status is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
226
	if ( $purchase_log->is_order_received() || $purchase_log->is_accepted_payment() ) {
227
		wpsc_send_customer_email( $purchase_log );
228
		wpsc_send_admin_email( $purchase_log );
229
	}
230
}
231
232
add_action( 'wpsc_update_purchase_log_status', '_wpsc_action_update_purchase_log_status', 10, 4 );
233
234
/**
235
 * Routine that runs when updating a purchase log's status, used to update status of coupon's used.
236
 *
237
 * @since  3.11.5
238
 *
239
 * @param  int               $id             Purchase Log ID.
240
 * @param  int               $status         Current status.
241
 * @param  int               $old_status     Previous status.
242
 * @param  WPSC_Purchase_Log $purchase_log   Purchase Log Object.
243
 *
244
 * @return void
245
 */
246
function _wpsc_update_purchase_log_coupon_status( $id, $status, $old_status, $purchase_log ) {
0 ignored issues
show
Unused Code introduced by
The parameter $id is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $status is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
247
248
	if ( ! $purchase_log->is_transaction_completed() ) {
249
		return;
250
	}
251
252
	$already_processed = in_array(
253
		$old_status,
254
		array(
255
			WPSC_Purchase_Log::ACCEPTED_PAYMENT,
256
			WPSC_Purchase_Log::JOB_DISPATCHED,
257
			WPSC_Purchase_Log::CLOSED_ORDER,
258
		)
259
	);
260
261
	if ( $already_processed ) {
262
		return;
263
	}
264
265
	_wpsc_process_transaction_coupon( $purchase_log );
266
}
267
268
add_action( 'wpsc_update_purchase_log_status', '_wpsc_update_purchase_log_coupon_status', 11, 4 );
269
270
/**
271
 * Routine that runs when updating a purchase log's status, used to update status of inventory.
272
 *
273
 * @since  3.11.5
274
 *
275
 * @param  int               $id             Purchase Log ID.
276
 * @param  int               $status         Current status.
277
 * @param  int               $old_status     Previous status.
278
 * @param  WPSC_Purchase_Log $purchase_log   Purchase Log Object.
279
 *
280
 * @return void
281
 */
282
283
function _wpsc_update_purchase_log_stock_status( $id, $status, $old_status, $purchase_log ) {
0 ignored issues
show
Unused Code introduced by
The parameter $status is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
284
285
	if ( ! $purchase_log->is_transaction_completed() ) {
286
		return;
287
	}
288
289
	$already_processed = in_array(
290
		$old_status,
291
		array(
292
			WPSC_Purchase_Log::ACCEPTED_PAYMENT,
293
			WPSC_Purchase_Log::JOB_DISPATCHED,
294
			WPSC_Purchase_Log::CLOSED_ORDER,
295
		)
296
	);
297
298
	if ( $already_processed ) {
299
		return;
300
	}
301
302
	wpsc_decrement_claimed_stock( $id );
303
}
304
305
add_action( 'wpsc_update_purchase_log_status', '_wpsc_update_purchase_log_stock_status', 12, 4 );
306
307
function wpsc_send_customer_email( $purchase_log ) {
308
	if ( ! is_object( $purchase_log ) ) {
309
		$purchase_log = wpsc_get_order( $purchase_log );
310
	}
311
312
	if ( ! $purchase_log->is_transaction_completed() && ! $purchase_log->is_order_received() ) {
313
		return;
314
	}
315
316
	$email = new WPSC_Purchase_Log_Customer_Notification( $purchase_log );
317
	$email_sent = $email->send();
318
319
	do_action( 'wpsc_transaction_send_email_to_customer', $email, $email_sent );
320
	return $email_sent;
321
}
322
323
function wpsc_send_admin_email( $purchase_log, $force = false ) {
324
325
	if ( ! is_object( $purchase_log ) ) {
326
		$purchase_log = wpsc_get_order( $purchase_log );
327
	}
328
329
	if ( $purchase_log->get( 'email_sent' ) && ! $force ) {
330
		return;
331
	}
332
333
	$email = new WPSC_Purchase_Log_Admin_Notification( $purchase_log );
334
	$email_sent = $email->send();
335
336
	if ( $email_sent ) {
337
		$purchase_log->set( 'email_sent', 1 )->save();
338
	}
339
340
	do_action( 'wpsc_transaction_send_email_to_admin', $email, $email_sent );
341
342
	return $email_sent;
343
}
344
345
function wpsc_get_transaction_html_output( $purchase_log ) {
346
347
	if ( ! is_object( $purchase_log ) ) {
348
		$purchase_log = wpsc_get_order( $purchase_log );
349
	}
350
351
	$notification = new WPSC_Purchase_Log_Customer_HTML_Notification( $purchase_log );
352
	$output = $notification->get_html_message();
353
354
	// see if the customer trying to view this transaction output is the person
355
	// who made the purchase.
356
	$checkout_session_id = wpsc_get_customer_meta( 'checkout_session_id' );
357
358
    if ( $checkout_session_id == $purchase_log->get( 'sessionid' ) ) {
359
    	$output = apply_filters( 'wpsc_get_transaction_html_output', $output, $notification );
360
	} else {
361
		$output = apply_filters( 'wpsc_get_transaction_unauthorized_view', __( "You don't have the permission to view this page", 'wp-e-commerce' ), $output, $notification );
362
	}
363
364
	return $output;
365
}
366
367
function _wpsc_update_log_total_with_item_update( $item_id, $purchase_log ) {
0 ignored issues
show
Unused Code introduced by
The parameter $item_id is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
368
	$purchase_log->set( 'totalprice', $purchase_log->get_total() )->save();
369
}
370
add_action( 'wpsc_purchase_log_update_item', '_wpsc_update_log_total_with_item_update', 10, 2 );
371
372
373
function wpsc_update_order_status_partially_refunded( $log ) {
374
	if ( WPSC_Purchase_Log::PARTIALLY_REFUNDED !== $log->get( 'processed' ) ) {
375
		wpsc_update_purchase_log_status( $log, WPSC_Purchase_Log::PARTIALLY_REFUNDED );
376
	}
377
}
378
379
add_action( 'wpsc_order_partially_refunded', 'wpsc_update_order_status_partially_refunded' );
380
381
function wpsc_update_order_status_fully_refunded( $log ) {
382
	if ( WPSC_Purchase_Log::REFUNDED !== $log->get( 'processed' ) ) {
383
		wpsc_update_purchase_log_status( $log, WPSC_Purchase_Log::REFUNDED );
384
	}
385
}
386
387
add_action( 'wpsc_order_fully_refunded', 'wpsc_update_order_status_fully_refunded' );
388