-
Notifications
You must be signed in to change notification settings - Fork 212
Expand file tree
/
Copy pathpurchase-log.class.php
More file actions
1693 lines (1377 loc) · 46.1 KB
/
purchase-log.class.php
File metadata and controls
1693 lines (1377 loc) · 46.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
// by default, expire stats cache after 48 hours
// this doesn't have any effect if you're not using APC or memcached
if ( ! defined( 'WPSC_PURCHASE_LOG_STATS_CACHE_EXPIRE' ) ) {
define( 'WPSC_PURCHASE_LOG_STATS_CACHE_EXPIRE', DAY_IN_SECONDS * 2 );
}
class WPSC_Purchase_Log extends WPSC_Query_Registry {
const INCOMPLETE_SALE = 1;
const ORDER_RECEIVED = 2;
const ACCEPTED_PAYMENT = 3;
const JOB_DISPATCHED = 4;
const CLOSED_ORDER = 5;
const PAYMENT_DECLINED = 6;
const REFUNDED = 7;
const REFUND_PENDING = 8;
const PARTIALLY_REFUNDED = 9;
/**
* Names of column that requires escaping values as strings before being inserted
* into the database
*
* @access private
* @static
* @since 3.8.9
*
* @var array
*/
private static $string_cols = array(
'sessionid',
'transactid',
'authcode',
'date',
'gateway',
'billing_country',
'shipping_country',
'email_sent',
'stock_adjusted',
'discount_data',
'track_id',
'billing_region',
'shipping_region',
'find_us',
'engrave_text',
'shipping_method',
'shipping_option',
'affiliate_id',
'plugin_version',
'notes',
);
/**
* Names of column that requires escaping values as integers before being inserted
* into the database
*
* @static
* @since 3.8.9
* @var array
*/
private static $int_cols = array(
'id',
'statusno',
'processed',
'user_ID',
);
/**
* Names of column that requires escaping values as float before being inserted
* into the database
*
* @static
* @since 3.11.5
* @var array
*/
private static $float_cols = array(
'totalprice',
'base_shipping',
'discount_value',
'wpec_taxes_total',
'wpec_taxes_rate',
);
/**
* Array of metadata
*
* @static
* @since 3.11.5
* @var array
*/
private static $metadata = array(
'totalprice',
'base_shipping',
'discount_value',
'wpec_taxes_total',
'wpec_taxes_rate',
);
private $gateway_data = array();
private $form_data_obj = null;
private $is_status_changed = false;
private $previous_status = false;
private $log_items = array();
private $log_item_ids = array();
private $can_edit = null;
private static $multiple_meta = array(
'notes' => 1,
);
/**
* Contains the constructor arguments. This array is necessary because we will
* lazy load the DB row into $this->data whenever necessary. Lazy loading is,
* in turn, necessary because sometimes right after saving a new record, we need
* to fetch a property with the same object.
*
* @access private
* @since 3.8.9
*
* @var array
*/
private $args = array(
'col' => '',
'value' => '',
);
protected $buyers_name = null;
protected $buyers_city = null;
protected $buyers_email = null;
protected $buyers_address = null;
protected $buyers_state_and_postcode = null;
protected $buyers_country = null;
protected $buyers_phone = null;
protected $shipping_name = null;
protected $shipping_address = null;
protected $shipping_city = null;
protected $shipping_state_and_postcode = null;
protected $shipping_country = null;
protected $payment_method = null;
protected $shipping_method = null;
protected static $flag = false;
protected static $alt_ids = false;
/**
* Get the SQL query format for a column
*
* @since 3.8.9
* @param string $col Name of the column
* @return string Placeholder
*/
private static function get_column_format( $col ) {
if ( in_array( $col, self::$string_cols ) ) {
return '%s';
}
if ( in_array( $col, self::$int_cols ) ) {
return '%d';
}
return '%f';
}
/**
* Query the purchase log table to get sales and earning stats
*
* Accepts an array of arguments:
* - 'ids': IDs of products for which you want to get stats
* - 'products': array of WPSC_Product objects for which you want to get stats
* - 'start' and 'end': the timestamp range (integers) during which you want
* to collect the stats.
* You can use none, either, or both of them.
* Note that the [start, end) interval is a left-closed,
* right-open.
* E.g.: to get stats from Jan 1st, 2013 to
* Dec 31st, 2013 (23:59:59),
* set "start" to the timestamp for Jan 1st, 2013, set
* "end" to the timestamp for Jan 1st, 2014
* - 'order': what to sort the results by, defaults to 'id'.
* Can be 'ids', 'sales', 'earnings' or empty string to disable sorting
* - 'orderby': how to sort the results, defaults to 'ASC'.
* Can be 'ASC', 'DESC' (lowercase is fine too)
* - 'per_page': How many items to fetch, defaults to 0, which fetches all results
* - 'page': Which page of the results to fetch, defaults to 1.
* Has no effect if per_page is set to 0.
*
* @since 3.8.14
* @param array|string $args Arguments
* @return array Array containing 'sales' and 'earnings' stats
*/
public static function fetch_stats( $args ) {
global $wpdb;
$defaults = array(
'ids' => array(), // IDs of the products to be queried
'products' => array(), // Array of WPSC_Products objects
'start' => 0, // Int. timestamp, has to be UTC
'end' => 0, // Int. timestamp, has to be UTC
'order' => 'ASC',
'orderby' => 'id',
'per_page' => 0,
'page' => 1,
);
$args = wp_parse_args( $args, $defaults );
// convert more advanced date / time args into "start" and "end"
$args = self::convert_date_args( $args );
// build an array of WPSC_Product objects based on 'ids' and 'products' args
$products = array_merge(
$args['products'],
array_map( array( 'WPSC_Product', 'get_instance' ), $args['ids'] )
);
// eliminate duplicates (Note: usage of this requires PHP 5.2.9)
$products = array_unique( $products, SORT_REGULAR );
if ( empty( $products ) ) {
return null;
}
$needs_fetching = array();
$stats = array(
'sales' => 0,
'earnings' => 0,
);
// if we have date restriction, that means we won't be able to rely upon
// individual stats cache inside WPSC_Product objects
$has_date_restriction = ! ( empty( $args['start'] ) && empty( $args['end'] ) );
// if we do NOT have date restriction, find out which WPSC_Product object
// has stats cache, and which don't
if ( ! $has_date_restriction ) {
foreach ( $products as $product ) {
// store the ID if this product doesn't have a stats cache yet
if ( $product->post->_wpsc_stats === '' ) {
$needs_fetching[] = $product->post->ID;
} else {
// tally up the sales and earnings if this one has cache already
$prod_meta = get_post_meta( $product->post->ID, '_wpsc_stats', true );
if ( isset( $prod_meta['sales'] ) && isset( $prod_meta['earnings'] ) ) {
$stats['sales'] += $prod_meta['sales'];
$stats['earnings'] += $prod_meta['earnings'];
}
$needs_fetching[] = $product->post->ID;
}
}
}
// never hurts to make sure
$needs_fetching = array_map( 'absint', $needs_fetching );
// pagination arguments
$limit = '';
if ( ! empty( $args['per_page'] ) ) {
$offset = ( $args['page'] - 1 ) * $args['per_page'];
$limit = "LIMIT " . absint( $args['per_page'] ) . " OFFSET " . absint( $offset );
}
// sorting
$order = '';
if ( ! empty( $args['orderby'] ) )
$order = "ORDER BY " . esc_sql( $args['orderby'] ) . " " . esc_sql( $args['order'] );
// date
$where = "WHERE p.processed IN (3, 4, 5)";
if ( $has_date_restriction ) {
// start date equal or larger than $args['start']
if ( ! empty( $args['start'] ) )
$where .= " AND CAST(p.date AS UNSIGNED) >= " . absint( $args['start'] );
// end date less than $args['end'].
// the "<" sign is not a typo, such upper limit makes it easier for
// people to specify range.
// E.g.: [1/1/2013 - 1/1/2014) rather than:
// [1/1/2013 - 31/12/2013 23:59:59]
if ( ! empty( $args['end'] ) )
$where .= " AND CAST(p.date AS UNSIGNED) < " . absint( $args['end'] );
}
// assemble the SQL query
$sql = "
SELECT cc.prodid AS id, SUM(cc.quantity) AS sales, SUM(cc.quantity * cc.price) AS earnings
FROM $wpdb->wpsc_purchase_logs AS p
INNER JOIN
$wpdb->wpsc_cart_contents AS cc
ON p.id = cc.purchaseid AND cc.prodid IN (" . implode( ', ', $needs_fetching ) . ")
{$where}
GROUP BY cc.prodid
{$order}
{$limit}
";
// if the result is cached, don't bother querying the database
$cache_key = md5( $sql );
$results = wp_cache_get( $cache_key, 'wpsc_purchase_logs_stats' );
if ( false === $results ) {
$results = $wpdb->get_results( $sql );
wp_cache_set( $cache_key, $results, 'wpsc_purchase_logs_stats', WPSC_PURCHASE_LOG_STATS_CACHE_EXPIRE );
}
// tally up the sales and earnings from the query results
foreach ( $results as $row ) {
if ( ! $has_date_restriction ) {
$product = WPSC_Product::get_instance( $row->id );
$product->sales = $row->sales;
$product->earnings = $row->earnings;
}
$stats['sales'] += $row->sales;
$stats['earnings'] += $row->earnings;
}
return $stats;
}
/**
* Convert advanced date/time arguments like year, month, day, 'ago' etc.
* into basic arguments which are "start" and "end".
*
* @since 3.8.14
* @param array $args Arguments
* @return array Arguments after converting
*/
private static function convert_date_args( $args ) {
// TODO: Implement this
return $args;
}
/**
* Get overall sales and earning stats for just one product
*
* @since 3.8.14
* @param int $id ID of the product
* @return array Array containing 'sales' and 'earnings' stats
*/
public static function get_stats_for_product( $id, $args = '' ) {
$product = WPSC_Product::get_instance( $id );
// if this product has variations
if ( $product->has_variations ) {
// get total stats of variations
$args['products'] = $product->variations;
} else {
// otherwise, get stats of only this product
$args['products'] = array( $product );
}
return self::fetch_stats( $args );
}
/**
* Check whether the status code indicates a completed status
*
* @since 3.8.13
* @param int $status Status code
* @return boolean
*/
public static function is_order_status_completed( $status ) {
$completed_status = apply_filters( 'wpsc_order_status_completed', array(
self::ACCEPTED_PAYMENT,
self::JOB_DISPATCHED,
self::CLOSED_ORDER,
) );
return in_array( $status, $completed_status );
}
/**
* Update cache of the passed log object
*
* @access public
* @static
* @since 3.8.9
*
* @param WPSC_Purchase_Log $log The log object that you want to store into cache
* @return void
*/
public static function update_cache( &$log ) {
return $log->update_caches();
}
/**
* Update caches.
*
* @access public
* @static
* @since 3.11.5
*
* @return void
*/
public function update_caches() {
// wpsc_purchase_logs stores the data array, while wpsc_purchase_logs_sessionid stores the
// log id that's associated with the sessionid
$id = $this->get( 'id' );
wp_cache_set( $id, $this->data, 'wpsc_purchase_logs' );
if ( $sessionid = $this->get( 'sessionid' ) ) {
wp_cache_set( $sessionid, $id, 'wpsc_purchase_logs_sessionid' );
}
wp_cache_set( $id, $this->log_items, 'wpsc_purchase_log_items' );
do_action( 'wpsc_purchase_log_update_cache', $this );
}
/**
* Deletes cache of a log (either by using the log ID or sessionid)
*
* @access public
* @static
* @since 3.8.9
*
* @param string $value The value to query
* @param string $col Optional. Defaults to 'id'. Whether to delete cache by using
* a purchase log ID or sessionid
* @return void
*/
public static function delete_cache( $value, $col = 'id' ) {
// this will pull from the old cache, so no worries there
$log = self::get_instance( $value, $col );
$log->delete_caches( $value, $col );
}
/**
* Deletes caches.
*
* @access public
* @static
* @since 3.11.5
*
* @param string|null $value Optional (left for back-compatibility). The value which was queried.
* @param string|null $col Optional (left for back-compatibility). The column used as the identifier.
*
* @return void
*/
public function delete_caches( $value = null, $col = null ) {
wp_cache_delete( $this->get( 'id' ), 'wpsc_purchase_logs' );
wp_cache_delete( $this->get( 'sessionid' ), 'wpsc_purchase_logs_sessionid' );
wp_cache_delete( $this->get( 'id' ), 'wpsc_purchase_log_items' );
wp_cache_delete( $this->get( 'id' ), 'wpsc_purchase_meta' );
if ( null === $value ) {
$value = $this->args['value'];
}
if ( null === $col ) {
$col = $this->args['col'];
}
do_action( 'wpsc_purchase_log_delete_cache', $this, $value, $col );
}
/**
* Deletes a log from the database.
*
* @access public
* @since 3.8.9
*
* @uses $wpdb Global database instance.
* @uses wpsc_is_store_admin() Check user has admin capabilities.
* @uses WPSC_Purchase_Log::delete_cache() Delete purchaselog cache.
* @uses WPSC_Claimed_Stock Claimed Stock class.
*
* @param string $log_id ID of the log.
* @return boolean Deleted successfully.
*/
public function delete( $log_id = false ) {
global $wpdb;
if ( ! ( isset( $this ) && get_class( $this ) == __CLASS__ ) ) {
_wpsc_doing_it_wrong( 'WPSC_Purchase_Log::delete', __( 'WPSC_Purchase_Log::delete() is no longer a static method and should not be called statically.', 'wp-e-commerce' ), '3.9.0' );
}
if ( false !== $log_id ) {
_wpsc_deprecated_argument( __FUNCTION__, '3.9.0', 'The $log_id param is not used. You must first create an instance of WPSC_Purchase_Log before calling this method.' );
}
if ( ! wpsc_is_store_admin() ) {
return false;
}
$log_id = $this->get( 'id' );
if ( $log_id > 0 ) {
do_action( 'wpsc_purchase_log_before_delete', $log_id, $this );
$this->delete_caches();
// Delete claimed stock
$purchlog_status = $wpdb->get_var( $wpdb->prepare( "SELECT `processed` FROM `" . WPSC_TABLE_PURCHASE_LOGS . "` WHERE `id`= %d", $log_id ) );
if ( $purchlog_status == WPSC_Purchase_Log::CLOSED_ORDER || $purchlog_status == WPSC_Purchase_Log::INCOMPLETE_SALE ) {
$claimed_query = new WPSC_Claimed_Stock( array(
'cart_id' => $log_id,
'cart_submitted' => 1
) );
$claimed_query->clear_claimed_stock( 0 );
}
// Delete cart content, submitted data, then purchase log
$wpdb->query( $wpdb->prepare( "DELETE FROM `" . WPSC_TABLE_CART_CONTENTS . "` WHERE `purchaseid` = %d", $log_id ) );
$wpdb->query( $wpdb->prepare( "DELETE FROM `" . WPSC_TABLE_SUBMITTED_FORM_DATA . "` WHERE `log_id` IN (%d)", $log_id ) );
$wpdb->query( $wpdb->prepare( "DELETE FROM `" . WPSC_TABLE_PURCHASE_LOGS . "` WHERE `id` = %d LIMIT 1", $log_id ) );
$wpdb->query( $wpdb->prepare( "DELETE FROM `" . WPSC_TABLE_PURCHASE_META . "` WHERE `wpsc_purchase_id` = %d", $log_id ) );
$wpdb->query( $wpdb->prepare( "DELETE FROM `" . WPSC_TABLE_DOWNLOAD_STATUS . "` WHERE `purchid` = %d ", $log_id ) );
do_action( 'wpsc_purchase_log_delete', $log_id, $this );
return true;
}
return false;
}
/**
* Constructor of the purchase log object. If no argument is passed, this simply
* create a new empty object. Otherwise, this will get the purchase log from the
* DB either by using purchase log id or sessionid (specified by the 2nd argument).
*
* It is preferred to use WPSC_Purchase_Log::get_instance() or wpsc_get_order().
*
* Eg:
*
* // get purchase log with ID number 23
* $log = WPSC_Purchase_Log::get_instance( 23 );
*
* // get purchase log with sessionid "asdf"
* $log = WPSC_Purchase_Log::get_instance( 'asdf', 'sessionid' )
*
* @access public
* @since 3.8.9
*
* @param string $value Optional. Defaults to false.
* @param string $col Optional. Defaults to 'id'.
*/
public function __construct( $value = false, $col = 'id' ) {
if ( ! self::$flag ) {
_wpsc_doing_it_wrong( 'wpsc_purchlog_notes_class_error', __( 'Please use `WPSC_Purchase_Log::get_instance( $log_id )` instead of `new WPSC_Purchase_Log( $log_id ).', 'wp-e-commerce' ), '3.12.0' );
}
parent::add_instance( $this );
if ( false === $value ) {
return;
}
if ( is_array( $value ) ) {
$this->set( $value );
return;
}
global $wpdb;
if ( ! in_array( $col, array( 'id', 'sessionid' ) ) ) {
return;
}
// store the constructor args into an array so that later we can lazy load the data
$this->args = array(
'col' => $col,
'value' => $value,
);
// if the sessionid is in cache, pull out the id
if ( $col == 'sessionid' && $id = wp_cache_get( $value, 'wpsc_purchase_logs_sessionid' ) ) {
$col = 'id';
$value = $id;
}
// if the id is specified, try to get from cache
if ( $col == 'id' ) {
$this->data = wp_cache_get( $value, 'wpsc_purchase_logs' );
$this->log_items = wp_cache_get( $value, 'wpsc_purchase_log_items' );
}
// cache exists
if ( $this->data ) {
$this->set_meta_props();
$this->fetched = true;
$this->exists = true;
return;
}
}
/**
* Retrieve a WPSC_Purchase_Log instance.
* Get the log either by using purchase log id or sessionid (specified by the 2nd argument).
*
* Eg:
*
* // get purchase log with ID number 23
* $log = WPSC_Purchase_Log::get_instance( 23 );
*
* // get purchase log with sessionid "asdf"
* $log = WPSC_Purchase_Log::get_instance( 'asdf', 'sessionid' )
*
* @since 3.12.0
*
* @param string $value Optional. Defaults to false.
* @param string $col Optional. Defaults to 'id'.
*
* @return WPSC_Purchase_Log object instance.
*/
public static function get_instance( $value = false, $col = 'id' ) {
$instance = false;
if ( $value && isset( self::$alt_ids[ $col ][ $value ] ) ) {
$instance = parent::_get_instance( __CLASS__, self::$alt_ids[ $col ][ $value ] );
}
if ( ! $instance ) {
self::$flag = true;
$instance = new self( $value, $col );
self::$flag = false;
}
$id = $instance->get( 'id' );
$instance_id = $instance->instance_id();
// If fetched object matches one we already have (based on the ID), then use original instance.
if ( $id && 'id' !== $col && isset( self::$alt_ids['id'][ $id ] ) ) {
$existing = parent::_get_instance( __CLASS__, self::$alt_ids['id'][ $id ] );
if ( $existing ) {
$instance = $existing;
$instance_id = $instance->instance_id();
}
}
if ( $value && 'id' !== $col ) {
self::$alt_ids[ $col ][ $value ] = $instance_id;
}
if ( $id ) {
self::$alt_ids['id'][ $id ] = $instance_id;
}
return $instance;
}
/**
* Retrieves the unique identifier for a WPSC_Query_Base instance.
*
* @since 3.12.0
*
* @return mixed
*/
public function instance_id() {
return spl_object_hash( $this );
}
private function set_total_shipping() {
$base_shipping = $this->get( 'base_shipping' );
$item_shipping = wp_list_pluck( $this->get_items(), 'pnp' );
$this->meta_data['total_shipping'] = $base_shipping + array_sum( $item_shipping );
return $this->meta_data['total_shipping'];
}
private function set_gateway_name() {
global $wpsc_gateways;
$gateway = $this->get( 'gateway' );
$gateway_name = $gateway;
if( 'wpsc_merchant_testmode' == $gateway )
$gateway_name = __( 'Manual Payment', 'wp-e-commerce' );
elseif ( isset( $wpsc_gateways[$gateway] ) )
$gateway_name = $wpsc_gateways[$gateway]['name'];
$this->meta_data['gateway_name'] = $gateway_name;
return $this->meta_data['gateway_name'];
}
private function set_shipping_method_names() {
global $wpsc_shipping_modules;
$shipping_method = $this->get( 'shipping_method' );
$shipping_option = $this->get( 'shipping_option' );
$shipping_method_name = $shipping_method;
$shipping_option_name = $shipping_option;
if ( ! empty ( $wpsc_shipping_modules[$shipping_method] ) ) {
$shipping_class = $wpsc_shipping_modules[$shipping_method];
$shipping_method_name = $shipping_class->name;
}
$this->meta_data['shipping_method_name'] = $shipping_method_name;
$this->meta_data['shipping_option_name'] = $shipping_option_name;
}
private function set_meta_props() {
foreach ( wpsc_get_purchase_custom( $this->get( 'id' ) ) as $key => $value ) {
$is_multiple_meta = isset( self::$multiple_meta[ $key ] );
$this->meta_data[ $key ] = wpsc_get_purchase_meta( $this->get( 'id' ), $key, ! $is_multiple_meta );
}
$this->set_total_shipping();
$this->set_gateway_name();
$this->set_shipping_method_names();
}
public function get_meta() {
if ( empty( $this->data ) || empty( $this->meta_data ) ) {
$this->fetch();
}
return (array) apply_filters( 'wpsc_purchase_log_meta_data', $this->meta_data );
}
/**
* Fetches the actual record from the database
*
* @access protected
* @since 3.8.9
*
* @return WPSC_Purchase_Log
*/
protected function fetch() {
global $wpdb;
if ( $this->fetched ) {
return;
}
// If $this->args is not set yet, it means the object contains a new unsaved
// row so we don't need to fetch from DB
if ( ! $this->args['col'] || ! $this->args['value'] ) {
return;
}
$col = $this->args['col'];
$format = self::get_column_format( $col );
$sql = $wpdb->prepare( "SELECT * FROM " . WPSC_TABLE_PURCHASE_LOGS . " WHERE {$col} = {$format}", $this->args['value'] );
$this->exists = false;
if ( $data = $wpdb->get_row( $sql, ARRAY_A ) ) {
$this->exists = true;
$this->data = apply_filters( 'wpsc_purchase_log_data', $data );
$this->log_items = $this->get_items();
$this->set_meta_props();
$this->update_caches();
}
do_action( 'wpsc_purchase_log_fetched', $this );
$this->fetched = true;
return $this;
}
/**
* Returns the value of the specified property of the $data array if it exists.
*
* @access public
* @since 3.11.5
*
* @param string $key Name of the property (column)
* @return mixed
*/
public function get( $key ) {
if ( 'notes' === $key ) {
_wpsc_doing_it_wrong( __FUNCTION__, __( 'Getting notes from the Log object has been deprecated in favor of the wpsc_get_order_notes() function.', 'wp-e-commerce' ), '3.11.5' );
}
return parent::get( $key );
}
/**
* Prepares the return value for get() (apply_filters, etc).
*
* @access protected
* @since 3.11.5
*
* @param mixed $value Value fetched
* @param string $key Key for $data.
*
* @return mixed
*/
protected function prepare_get( $value, $key ) {
return apply_filters( 'wpsc_purchase_log_get_property', $value, $key, $this );
}
/**
* Prepares the return value for get_data() (apply_filters, etc).
*
* @access protected
* @since 3.11.5
*
* @return mixed
*/
protected function prepare_get_data() {
return apply_filters( 'wpsc_purchase_log_get_data', $this->data, $this );
}
/**
* Prepares the return value for get_meta() (apply_filters, etc).
*
* @access protected
* @since 3.11.5
*
* @return mixed
*/
protected function prepare_get_meta() {
return (array) apply_filters( 'wpsc_purchase_log_meta_data', $this->meta_data );
}
public function get_cart_contents() {
_wpsc_doing_it_wrong( __FUNCTION__, __( 'This function has been deprecated in favor of the get_items() method.', 'wp-e-commerce' ), '3.11.5' );
return $this->get_items();
}
public function get_items() {
global $wpdb;
if ( ! empty( $this->log_items ) && $this->fetched ) {
return $this->log_items;
}
$id = $this->get( 'id' );
// Bail if we don't have a log object yet (no id).
if ( empty( $id ) ) {
return $this->log_items;
}
$sql = $wpdb->prepare( "SELECT * FROM " . WPSC_TABLE_CART_CONTENTS . " WHERE purchaseid = %d", $id );
$this->log_items = $wpdb->get_results( $sql );
if ( is_array( $this->log_items ) ) {
foreach ( $this->log_items as $index => $item ) {
$this->log_item_ids[ absint( $item->id ) ] = $index;
}
}
return $this->log_items;
}
public function get_item( $item_id ) {
$item_id = absint( $item_id );
$items = $this->get_items();
if ( isset( $this->log_item_ids[ $item_id ] ) ) {
return $items[ $this->log_item_ids[ $item_id ] ];
}
return false;
}
public function get_item_from_product_id( $product_id ) {
$product_id = absint( $product_id );
$items = $this->get_items();
foreach ( $items as $item ) {
if ( $product_id === absint( $item->prodid ) ) {
return $item;
}
}
return false;
}
public function update_item( $item_id, $data ) {
global $wpdb;
$item_id = absint( $item_id );
$item = $this->get_item( $item_id );
if ( $item ) {
do_action( 'wpsc_purchase_log_before_update_item', $item_id, $this );
$data = wp_unslash( $data );
$result = $wpdb->update( WPSC_TABLE_CART_CONTENTS, $data, array( 'id' => $item_id ) );
if ( $result ) {
$this->log_items = array();
$this->get_item( $item_id );
do_action( 'wpsc_purchase_log_update_item', $item_id, $this );
}
return $result;
}
return false;
}
public function remove_item( $item_id ) {
global $wpdb;
$item_id = absint( $item_id );
$item = $this->get_item( $item_id );
if ( $item ) {
do_action( 'wpsc_purchase_log_before_remove_item', $item_id, $this );
$result = $wpdb->delete( WPSC_TABLE_CART_CONTENTS, array( 'id' => $item_id ) );
if ( $result ) {
unset( $this->log_items[ $this->log_item_ids[ $item_id ] ] );
unset( $this->log_item_ids[ $item_id ] );
do_action( 'wpsc_purchase_log_remove_item', $item_id, $this );
}
return $result;
}
return false;
}
public function form_data() {
if ( null === $this->form_data_obj ) {
$this->form_data_obj = new WPSC_Checkout_Form_Data( $this->get( 'id' ), false );
}
return $this->form_data_obj;
}
public function get_gateway_data( $from_currency = false, $to_currency = false ) {
if ( ! $this->exists() ) {
return array();
}
$subtotal = 0;
$shipping = wpsc_convert_currency( (float) $this->get( 'base_shipping' ), $from_currency, $to_currency );
$items = array();
$this->gateway_data = array(
'amount' => wpsc_convert_currency( $this->get( 'totalprice' ), $from_currency, $to_currency ),
'invoice' => $this->get( 'sessionid' ),
'tax' => wpsc_convert_currency( $this->get( 'wpec_taxes_total' ), $from_currency, $to_currency ),
);
foreach ( $this->log_items as $item ) {
$item_price = wpsc_convert_currency( $item->price, $from_currency, $to_currency );
$items[] = array(
'name' => $item->name,
'amount' => $item_price,
'tax' => wpsc_convert_currency( $item->tax_charged, $from_currency, $to_currency ),
'quantity' => $item->quantity,
);
$subtotal += $item_price * $item->quantity;
$shipping += wpsc_convert_currency( $item->pnp, $from_currency, $to_currency );
}
$this->gateway_data['discount'] = wpsc_convert_currency( (float) $this->get( 'discount_value' ), $from_currency, $to_currency );
$this->gateway_data['items'] = $items;
$this->gateway_data['shipping'] = $shipping;
$this->gateway_data['subtotal'] = $subtotal;
if ( $from_currency ) {
// adjust total amount in case there's slight decimal error
$total = $subtotal + $shipping + $this->gateway_data['tax'] - $this->gateway_data['discount'];
if ( $this->gateway_data['amount'] != $total ) {
$this->gateway_data['amount'] = $total;
}
}
$this->gateway_data = apply_filters( 'wpsc_purchase_log_gateway_data', $this->gateway_data, $this->get_data() );
return $this->gateway_data;
}
/**
* Sets a property to a certain value. This function accepts a key and a value
* as arguments, or an associative array containing key value pairs.
*
* Loops through data, comparing against database, and saves as meta if not found in purchase log table.
*
* @access public
* @since 3.8.9
*
* @param mixed $key Name of the property (column), or an array containing key
* value pairs
* @param string|int $value Optional. Defaults to false. In case $key is a string,
* this should be specified.
* @return WPSC_Purchase_Log The current object (for method chaining)
*/