-
Notifications
You must be signed in to change notification settings - Fork 212
Expand file tree
/
Copy pathproduct-functions.php
More file actions
1255 lines (1042 loc) · 42.6 KB
/
product-functions.php
File metadata and controls
1255 lines (1042 loc) · 42.6 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
/**
* WPSC Product modifying functions
*
* @package wp-e-commerce
* @since 3.7
*/
function wpsc_get_max_upload_size(){
return size_format( wp_max_upload_size() );
}
/**
* wpsc_admin_submit_product function
* @internal Was going to completely refactor sanitise forms and wpsc_insert_product, but they are also used by the import system
* which I'm not really familiar with...so I'm not touching them :) Erring on the side of redundancy and caution I'll just
* refactor this to do the job.
* @return nothing
*/
function wpsc_admin_submit_product( $post_ID, $post ) {
if ( ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) || $post->post_type != 'wpsc-product' ) {
return;
}
//Type-casting ( not so much sanitization, which would be good to do )
$post_data = stripslashes_deep( $_POST );
$product_id = $post_ID;
$post_data['additional_description'] = isset( $post_data['additional_description'] ) ? $post_data['additional_description'] : '';
if ( ! isset( $post_data['meta'] ) && isset( $_POST['meta'] ) ) {
$post_data['meta'] = (array) $_POST['meta'];
}
if ( isset( $post_data['meta']['_wpsc_price'] ) )
$post_data['meta']['_wpsc_price'] = wpsc_string_to_float( $post_data['meta']['_wpsc_price'] );
if ( isset( $post_data['meta']['_wpsc_special_price'] ) )
$post_data['meta']['_wpsc_special_price'] = wpsc_string_to_float( $post_data['meta']['_wpsc_special_price'] );
if ( isset( $post_data['meta']['_wpsc_sku'] ) && $post_data['meta']['_wpsc_sku'] == __('N/A', 'wpsc') ) {
$post_data['meta']['_wpsc_sku'] = '';
}
// Update donation setting
if ( isset( $post_data['wpsc_product_pricing_nonce'] ) && wp_verify_nonce( $post_data['wpsc_product_pricing_nonce'], 'update' ) ) {
$post_data['meta']['_wpsc_is_donation'] = isset( $post_data['meta']['_wpsc_is_donation'] ) ? 1 : 0;
}
if ( ! isset( $post_data['meta']['_wpsc_limited_stock'] ) ){
$post_data['meta']['_wpsc_stock'] = false;
} else {
$post_data['meta']['_wpsc_stock'] = isset( $post_data['meta']['_wpsc_stock'] ) ? (int) $post_data['meta']['_wpsc_stock'] : 0;
}
unset($post_data['meta']['_wpsc_limited_stock']);
if(!isset($post_data['quantity_limited'])) $post_data['quantity_limited'] = '';
if(!isset($post_data['special'])) $post_data['special'] = '';
$post_data['meta']['_wpsc_product_metadata']['quantity_limited'] = (int)(bool)$post_data['quantity_limited'];
$post_data['meta']['_wpsc_product_metadata']['special'] = (int)(bool)$post_data['special'];
// Update Stock Options
if ( isset( $_POST['wpsc_product_stock_nonce'] ) && wp_verify_nonce( $_POST['wpsc_product_stock_nonce'], 'update' ) ) {
$post_data['meta']['_wpsc_product_metadata'] = wp_parse_args( $post_data['meta']['_wpsc_product_metadata'], array(
'notify_when_none_left' => 0,
'unpublish_when_none_left' => 0
) );
$post_data['meta']['_wpsc_product_metadata']['notify_when_none_left'] = absint( (bool) $post_data['meta']['_wpsc_product_metadata']['notify_when_none_left'] );
$post_data['meta']['_wpsc_product_metadata']['unpublish_when_none_left'] = absint( (bool) $post_data['meta']['_wpsc_product_metadata']['unpublish_when_none_left'] );
}
// Update shipping setting
if ( isset( $_POST['wpsc_product_shipping_nonce'] ) && wp_verify_nonce( $_POST['wpsc_product_shipping_nonce'], 'update' ) ) {
$post_data['meta']['_wpsc_product_metadata'] = wp_parse_args( $post_data['meta']['_wpsc_product_metadata'], array(
'no_shipping' => 0
) );
$post_data['meta']['_wpsc_product_metadata']['no_shipping'] = absint( (bool) $post_data['meta']['_wpsc_product_metadata']['no_shipping'] );
}
// Product Weight
if(!isset($post_data['meta']['_wpsc_product_metadata']['display_weight_as'])) $post_data['meta']['_wpsc_product_metadata']['display_weight_as'] = '';
if ( isset( $post_data['meta']['_wpsc_product_metadata']['weight'] ) ) {
$weight = wpsc_string_to_float( $post_data['meta']['_wpsc_product_metadata']['weight'] );
$weight = wpsc_convert_weight( $weight, $post_data['meta']['_wpsc_product_metadata']['weight_unit'], "pound", true);
$post_data['meta']['_wpsc_product_metadata']['weight'] = $weight;
$post_data['meta']['_wpsc_product_metadata']['display_weight_as'] = $post_data['meta']['_wpsc_product_metadata']['weight_unit'];
}
if ( isset( $post_data['meta']['_wpsc_product_metadata']['dimensions'] ) ) {
$dimensions =& $post_data['meta']['_wpsc_product_metadata']['dimensions'];
foreach ( $dimensions as $key => $value ) {
if ( ! in_array( $key, array( 'height', 'width', 'length' ) ) )
continue;
$dimensions[$key] = wpsc_string_to_float( $value );
}
}
// Update the table rate prices (quantity discounts)
if ( isset( $post_data['wpsc-update-quantity-discounts'] ) && wp_verify_nonce( $post_data['wpsc-update-quantity-discounts'], 'update-options' ) ) {
$post_data['meta']['_wpsc_product_metadata']['table_rate_price'] = isset( $post_data['table_rate_price'] ) ? $post_data['table_rate_price'] : array();
// If table_rate_price is empty, set empty table rate price arrays
if ( empty( $post_data['meta']['_wpsc_product_metadata']['table_rate_price'] ) ) {
$post_data['meta']['_wpsc_product_metadata']['table_rate_price']['table_price'] = array();
$post_data['meta']['_wpsc_product_metadata']['table_rate_price']['quantity'] = array();
}
// Remove any rates with no quantity or price
if ( ! empty( $post_data['meta']['_wpsc_product_metadata']['table_rate_price']['table_price'] ) ) {
foreach ( (array) $post_data['meta']['_wpsc_product_metadata']['table_rate_price']['quantity'] as $key => $value ) {
if ( empty( $value ) ) {
unset( $post_data['meta']['_wpsc_product_metadata']['table_rate_price']['table_price'][ $key ] );
unset( $post_data['meta']['_wpsc_product_metadata']['table_rate_price']['quantity'][ $key ] );
}
}
foreach ( (array) $post_data['meta']['_wpsc_product_metadata']['table_rate_price']['table_price'] as $key => $value ) {
if ( empty( $value ) ) {
unset( $post_data['meta']['_wpsc_product_metadata']['table_rate_price']['table_price'][ $key ] );
unset( $post_data['meta']['_wpsc_product_metadata']['table_rate_price']['quantity'][ $key ] );
}
}
}
}
if ( isset( $post_data['meta']['_wpsc_product_metadata']['shipping'] ) ) {
$post_data['meta']['_wpsc_product_metadata']['shipping']['local'] = wpsc_string_to_float( $post_data['meta']['_wpsc_product_metadata']['shipping']['local'] );
$post_data['meta']['_wpsc_product_metadata']['shipping']['international'] = wpsc_string_to_float( $post_data['meta']['_wpsc_product_metadata']['shipping']['international'] );
}
// Update product taxes
if ( isset( $_POST['wpsc_product_tax_nonce'] ) && wp_verify_nonce( $_POST['wpsc_product_tax_nonce'], 'update' ) ) {
$post_data['meta']['_wpsc_product_metadata'] = wp_parse_args( $post_data['meta']['_wpsc_product_metadata'], array(
'wpec_taxes_taxable_amount' => '',
'wpec_taxes_taxable' => ''
) );
if ( ! empty( $post_data['meta']['_wpsc_product_metadata']['wpec_taxes_taxable_amount'] ) ) {
$post_data['meta']['_wpsc_product_metadata']['wpec_taxes_taxable_amount'] = wpsc_string_to_float($post_data['meta']['_wpsc_product_metadata']['wpec_taxes_taxable_amount'] );
}
$post_data['meta']['_wpsc_product_metadata']['wpec_taxes_taxable'] = $post_data['meta']['_wpsc_product_metadata']['wpec_taxes_taxable'];
}
// External Link Options
if ( isset( $_POST['wpsc_product_external_link_nonce'] ) && wp_verify_nonce( $_POST['wpsc_product_external_link_nonce'], 'update' ) ) {
// Parse post meta to ensure default values
$post_data['meta']['_wpsc_product_metadata'] = wp_parse_args( $post_data['meta']['_wpsc_product_metadata'], array(
'external_link' => '',
'external_link_text' => '',
'external_link_target' => ''
) );
}
// Advanced Options
if ( isset( $_POST['wpsc_product_personalization_nonce'] ) && wp_verify_nonce( $_POST['wpsc_product_personalization_nonce'], 'update' ) ) {
// Parse post meta to ensure default values (especially checkboxes)
$post_data['meta']['_wpsc_product_metadata'] = wp_parse_args( $post_data['meta']['_wpsc_product_metadata'], array(
'engraved' => 0,
'can_have_uploaded_image' => 0
) );
$post_data['meta']['_wpsc_product_metadata']['engraved'] = absint( (bool) $post_data['meta']['_wpsc_product_metadata']['engraved'] );
$post_data['meta']['_wpsc_product_metadata']['can_have_uploaded_image'] = absint( (bool) $post_data['meta']['_wpsc_product_metadata']['can_have_uploaded_image'] );
}
if ( ! isset($post_data['meta']['_wpsc_product_metadata']['google_prohibited'])) $post_data['meta']['_wpsc_product_metadata']['google_prohibited'] = '';
$post_data['meta']['_wpsc_product_metadata']['google_prohibited'] = (int)(bool)$post_data['meta']['_wpsc_product_metadata']['google_prohibited'];
// Fill in any missing meta values with existing values.
$post_data['meta'] = wp_parse_args( $post_data['meta'], array(
'_wpsc_is_donation' => get_product_meta( $product_id, 'is_donation', true )
) );
// Fill in any missing product meta values with existing values.
$default_meta_values = wp_parse_args( get_product_meta( $product_id, 'product_metadata', true ), array(
'notify_when_none_left' => 0,
'unpublish_when_none_left' => 0,
'no_shipping' => 0,
'external_link' => '',
'external_link_text' => '',
'external_link_target' => '',
'engraved' => 0,
'can_have_uploaded_image' => 0
) );
$post_data['meta']['_wpsc_product_metadata'] = wp_parse_args( $post_data['meta']['_wpsc_product_metadata'], $default_meta_values );
$post_data['files'] = $_FILES;
if(isset($post_data['post_title']) && $post_data['post_title'] != '') {
$product_columns = array(
'name' => '',
'description' => '',
'additional_description' => '',
'price' => null,
'weight' => null,
'weight_unit' => '',
'pnp' => null,
'international_pnp' => null,
'file' => null,
'image' => '0',
'quantity_limited' => '',
'quantity' => null,
'special' => null,
'special_price' => null,
'display_frontpage' => null,
'notax' => null,
'publish' => null,
'active' => null,
'donation' => null,
'no_shipping' => null,
'thumbnail_image' => null,
'thumbnail_state' => null
);
foreach ( $product_columns as $column => $default ) {
if ( ! isset( $post_data[ $column ] ) ) {
$post_data[ $column ] = '';
}
}
// if we succeed, we can do further editing (todo - if_wp_error)
// if we have no categories selected, assign one.
if ( isset( $post_data['tax_input']['wpsc_product_category'] ) && count( $post_data['tax_input']['wpsc_product_category'] ) == 1 && $post_data['tax_input']['wpsc_product_category'][0] == 0){
$post_data['tax_input']['wpsc_product_category'][1] = wpsc_add_product_category_default($product_id);
}
// and the meta
wpsc_update_product_meta($product_id, $post_data['meta']);
// and the custom meta
wpsc_update_custom_meta($product_id, $post_data);
// Update the alternative currencies
if ( isset( $post_data['wpsc-update-currency-layers'] ) && wp_verify_nonce( $post_data['wpsc-update-currency-layers'], 'update-options' ) ) {
// Clear currencies before re-saving to make sure deleted currencies are removed
update_product_meta( $product_id, 'currency', array() );
if ( ! empty( $post_data['newCurrency'] ) ) {
foreach( (array) $post_data['newCurrency'] as $key =>$value ) {
wpsc_update_alt_product_currency( $product_id, $value, $post_data['newCurrPrice'][ $key ] );
}
}
}
if ( isset( $post_data['files']['file'] ) && $post_data['files']['file']['tmp_name'] != '' ) {
wpsc_item_process_file($product_id, $post_data['files']['file']);
} else {
if (!isset($post_data['select_product_file'])) $post_data['select_product_file'] = null;
wpsc_item_reassign_file($product_id, $post_data['select_product_file']);
}
if(isset($post_data['files']['preview_file']['tmp_name']) && ($post_data['files']['preview_file']['tmp_name'] != '')) {
wpsc_item_add_preview_file($product_id, $post_data['files']['preview_file']);
}
do_action('wpsc_edit_product', $product_id);
}
return $product_id;
}
function wpsc_pre_update( $data , $postarr ) {
if ( (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) || $postarr["post_type"] != 'wpsc-product' )
return $data;
if( isset( $postarr["additional_description"] ) )
$data["post_excerpt"] = $postarr["additional_description"];
if( isset( $postarr["parent_post"] ) && !empty( $postarr["parent_post"] ) )
$data["post_parent"] = $postarr["parent_post"];
// Sanitize status for variations (see #324)
if ( $data['post_parent'] && ( ! isset( $data['ID'] ) || $data['post_parent'] != $data['ID'] ) && $data['post_status'] == 'publish' ) {
$data['post_status'] = 'inherit';
}
if ( ! empty( $postarr['meta'] ) && ( ! isset( $postarr['meta']['_wpsc_product_metadata']['enable_comments'] ) || $postarr['meta']['_wpsc_product_metadata']['enable_comments'] == 0 || empty( $postarr['meta']['_wpsc_product_metadata']['enable_comments'] ) ) ) {
$data["comment_status"] = "closed";
} else {
$data["comment_status"] = "open";
}
//Can anyone explain to me why this is here?
if ( isset( $sku ) && ( $sku != '' ) )
$data['guid'] = $sku;
return $data;
}
add_filter( 'wp_insert_post_data','wpsc_pre_update', 99, 2 );
add_action( 'save_post', 'wpsc_admin_submit_product', 5, 2 );
add_action( 'admin_notices', 'wpsc_admin_submit_notices' );
/**
* Remove category meta box from variation editor. This would disassociate variations
* with the default category. See #431 (http://code.google.com/p/wp-e-commerce/issues/detail?id=431)
*
*/
function wpsc_variation_remove_metaboxes() {
global $post;
if ( ! $post->post_parent )
return;
remove_meta_box( 'wpsc_product_categorydiv', 'wpsc-product', 'side' );
}
add_action( 'add_meta_boxes_wpsc-product', 'wpsc_variation_remove_metaboxes', 99 );
function wpsc_admin_submit_notices() {
global $current_screen, $post;
if( $current_screen->id != 'wpsc-product' || !isset( $_SESSION['product_error_messages'] ) )
return;
foreach ( $_SESSION['product_error_messages'] as $error )
echo "<div id=\"message\" class=\"updated below-h2\"><p>".$error."</p></div>";
unset( $_SESSION['product_error_messages'] );
}
/**
* wpsc_add_product_category_default, if there is no category assigned assign first product category as default
*
* @since 3.8
* @param $product_id (int) the Post ID
* @return null
*/
function wpsc_add_product_category_default( $product_id ){
$terms = get_terms( 'wpsc_product_category', array( 'orderby' => 'id', 'hide_empty' => 0 ) );
if ( ! empty( $terms ) ) {
$default = array_shift( $terms );
wp_set_object_terms( $product_id , array( $default->slug ) , 'wpsc_product_category' );
}
}
/**
* wpsc_sanitise_product_forms function
*
* @return array - Sanitised product details
*/
function wpsc_sanitise_product_forms($post_data = null) {
if ( empty($post_data) ) {
$post_data = &$_POST;
}
$post_data = stripslashes_deep( $post_data );
$post_data['name'] = isset($post_data['post_title']) ? $post_data['post_title'] : '';
$post_data['title'] = $post_data['name'];
$post_data['description'] = isset($post_data['content']) ? $post_data['content'] : '';
$post_data['additional_description'] = isset($post_data['additional_description']) ? $post_data['additional_description'] : '';
$post_data['post_status'] = 'draft';
if(isset($post_data['publish'])) {
$post_data['post_status'] = 'publish';
} else if(isset($post_data['unpublish'])) {
$post_data['post_status'] = 'draft';
}
$post_data['meta']['_wpsc_price'] = wpsc_string_to_float( $post_data['meta']['_wpsc_price'] );
$post_data['meta']['_wpsc_special_price'] = wpsc_string_to_float( $post_data['meta']['_wpsc_special_price'] );
if (!isset($post_data['meta']['_wpsc_is_donation'])) $post_data['meta']['_wpsc_is_donation'] = '';
$post_data['meta']['_wpsc_is_donation'] = (int)(bool)$post_data['meta']['_wpsc_is_donation'];
$post_data['meta']['_wpsc_stock'] = (int)$post_data['meta']['_wpsc_stock'];
if (!isset($post_data['meta']['_wpsc_limited_stock'])) $post_data['meta']['_wpsc_limited_stock'] = '';
if((bool)$post_data['meta']['_wpsc_limited_stock'] != true) {
$post_data['meta']['_wpsc_stock'] = false;
}
unset($post_data['meta']['_wpsc_limited_stock']);
if(!isset($post_data['meta']['_wpsc_product_metadata']['notify_when_none_left'])) $post_data['meta']['_wpsc_product_metadata']['notify_when_none_left'] = 0;
if(!isset($post_data['meta']['_wpsc_product_metadata']['unpublish_when_none_left'])) $post_data['meta']['_wpsc_product_metadata']['unpublish_when_none_left'] = '';
if(!isset($post_data['quantity_limited'])) $post_data['quantity_limited'] = '';
if(!isset($post_data['special'])) $post_data['special'] = '';
if(!isset($post_data['meta']['_wpsc_product_metadata']['no_shipping'])) $post_data['meta']['_wpsc_product_metadata']['no_shipping'] = '';
$post_data['meta']['_wpsc_product_metadata']['notify_when_none_left'] = (int)(bool)$post_data['meta']['_wpsc_product_metadata']['notify_when_none_left'];
$post_data['meta']['_wpsc_product_metadata']['unpublish_when_none_left'] = (int)(bool)$post_data['meta']['_wpsc_product_metadata']['unpublish_when_none_left'];
$post_data['meta']['_wpsc_product_metadata']['quantity_limited'] = (int)(bool)$post_data['quantity_limited'];
$post_data['meta']['_wpsc_product_metadata']['special'] = (int)(bool)$post_data['special'];
$post_data['meta']['_wpsc_product_metadata']['no_shipping'] = (int)(bool)$post_data['meta']['_wpsc_product_metadata']['no_shipping'];
// Product Weight
if(!isset($post_data['meta']['_wpsc_product_metadata']['display_weight_as'])) $post_data['meta']['_wpsc_product_metadata']['display_weight_as'] = '';
if(!isset($post_data['meta']['_wpsc_product_metadata']['display_weight_as'])) $post_data['meta']['_wpsc_product_metadata']['display_weight_as'] = '';
$weight = wpsc_string_to_float( $post_data['meta']['_wpsc_product_metadata']['weight'] );
$weight = wpsc_convert_weight( $weight, $post_data['meta']['_wpsc_product_metadata']['weight_unit'], "pound", true);
$post_data['meta']['_wpsc_product_metadata']['weight'] = $weight;
$post_data['meta']['_wpsc_product_metadata']['display_weight_as'] = $post_data['meta']['_wpsc_product_metadata']['weight_unit'];
$post_data['files'] = $_FILES;
return $post_data;
}
/**
* wpsc_insert_product function
*
* @param unknown
* @return unknown
*/
function wpsc_insert_product($post_data, $wpsc_error = false) {
global $wpdb, $user_ID;
$adding = false;
$update = false;
$product_columns = array(
'name' => '',
'description' => '',
'additional_description' => '',
'price' => null,
'weight' => null,
'weight_unit' => '',
'pnp' => null,
'international_pnp' => null,
'file' => null,
'image' => '0',
'quantity_limited' => '',
'quantity' => null,
'special' => null,
'special_price' => null,
'display_frontpage' => null,
'notax' => null,
'publish' => null,
'active' => null,
'donation' => null,
'no_shipping' => null,
'thumbnail_image' => null,
'thumbnail_state' => null
);
foreach ( $product_columns as $column => $default ) {
if ( ! isset( $post_data[ $column ] ) ) {
$post_data[ $column ] = '';
}
}
$product_post_values = array(
'post_author' => $user_ID,
'post_content' => $post_data['description'],
'post_excerpt' => $post_data['additional_description'],
'post_title' => $post_data['name'],
'post_status' => $post_data['post_status'],
'post_type' => "wpsc-product",
'post_name' => sanitize_title($post_data['name'])
);
$product_post_values["comment_status"] = "open";
if(isset($sku) && ($sku != '')) {
$product_post_array['guid'] = $sku;
}
$product_id = wp_insert_post($product_post_values);
if ( isset ( $post_data["sticky"] ) ) {
stick_post($product_id);
}else {
unstick_post($product_id);
}
$adding = true;
// if we succeed, we can do further editing
// and the meta
wpsc_update_product_meta($product_id, $post_data['meta']);
do_action('wpsc_edit_product', $product_id);
return $product_id;
}
/**
* term_id_price function
* Retreives associated price, if any, with term_id
* @param integer term ID
* @param integer parent product price
* @return integer modified price for child product, based on term ID price and parent price
*/
function term_id_price($term_id, $parent_price) {
$term_price_arr = get_option( 'term_prices' );
if ( isset($term_price_arr[$term_id]) ) {
$price = $term_price_arr[$term_id]["price"];
} else {
$price = 0;
}
//Check for flat, percentile or differential
$var_price_type = '';
if (flat_price($price)) {
$var_price_type = 'flat';
$price = floatval($price);
} elseif ( differential_price($price) ) {
$var_price_type = 'differential';
} elseif (percentile_price($price)) {
$var_price_type = 'percentile';
}
if (strchr($price, '-') ) {
$positive = false;
} else {
$positive = true;
}
if ($positive) {
if ( $var_price_type == 'differential' ) {
$differential = (floatval($price));
$price = $parent_price + $differential;
} elseif ( $var_price_type == 'percentile' ) {
$percentage = (floatval($price) / 100);
$price = $parent_price + ($parent_price * $percentage);
}
} else {
if ( $var_price_type == 'differential' ) {
$differential = (floatval($price));
$price = $parent_price - $differential;
} elseif ( $var_price_type == 'percentile' ) {
$percentage = (floatval($price) / 100);
$price = $parent_price - ($parent_price * $percentage);
}
}
return $price;
}
/**
* Determine the price of a variation product based on the variation it's assigned
* to. Because each variation term can have its own price (eg. 10, +10, -5%), this
* function also takes those into account.
*
* @since 3.8.6
* @param int $variation_id ID of the variation product
* @param string $terms Optional. Defaults to false. Variation terms assigned to
* the variation product. Pass this argument to save one SQL query.
* @return float Calculated price of the variation
*/
function wpsc_determine_variation_price( $variation_id, $term_ids = false ) {
$flat = array();
$diff = 0;
$variation = get_post( $variation_id );
$price = (float) get_product_meta( $variation->post_parent, 'price', true );
if ( ! $term_ids )
$term_ids = wpsc_get_product_terms( $variation_id, 'wpsc-variation', 'term_id' );
$term_price_arr = get_option( 'term_prices' );
foreach ( $term_ids as $term_id ) {
if ( isset( $term_price_arr[$term_id] ) )
$term_price = trim( $term_price_arr[$term_id]['price'] );
else
continue;
if ( flat_price( $term_price ) ) {
$flat[] = $term_price;
} elseif ( differential_price( $term_price ) ) {
$diff += (float) $term_price;
} elseif ( percentile_price( $term_price ) ) {
$diff += (float) $term_price / 100 * $price;
}
}
// Variation price should at least be the maximum of all flat prices
if ( ! empty( $flat ) )
$price = max( $flat );
$price += $diff;
return $price;
}
/**
* wpsc_edit_product_variations function.
* this is the function to make child products using variations
*
* @access public
* @param mixed $product_id
* @param mixed $post_data
* @return void
*/
function wpsc_edit_product_variations($product_id, $post_data) {
global $user_ID;
$parent = get_post_field( 'post_parent', $product_id );
if( ! empty( $parent ) )
return;
$variations = array();
$product_children = array();
if (!isset($post_data['edit_var_val']))
$post_data['edit_var_val'] = '';
$variations = (array) $post_data['edit_var_val'];
// Generate the arrays for variation sets, values and combinations
$wpsc_combinator = new wpsc_variation_combinator($variations);
// Retrieve the array containing the variation set IDs
$variation_sets = $wpsc_combinator->return_variation_sets();
// Retrieve the array containing the combinations of each variation set to be associated with this product.
$variation_values = $wpsc_combinator->return_variation_values();
// Retrieve the array containing the combinations of each variation set to be associated with this product.
$combinations = $wpsc_combinator->return_combinations();
$product_terms = wpsc_get_product_terms( $product_id, 'wpsc-variation' );
$variation_sets_and_values = array_merge($variation_sets, $variation_values);
$variation_sets_and_values = apply_filters('wpsc_edit_product_variation_sets_and_values', $variation_sets_and_values, $product_id);
wp_set_object_terms($product_id, $variation_sets_and_values, 'wpsc-variation');
$parent_id = $_REQUEST['product_id'];
$child_product_template = array(
'post_author' => $user_ID,
'post_content' => get_post_field( 'post_content', $parent_id, 'raw' ),
'post_excerpt' => get_post_field( 'post_excerpt', $parent_id, 'raw' ),
'post_title' => get_post_field( 'post_title', $parent_id, 'raw' ),
'post_status' => 'inherit',
'post_type' => "wpsc-product",
'post_parent' => $product_id
);
$child_product_meta = get_post_custom($product_id);
// here we loop through the combinations, get the term data and generate custom product names
foreach($combinations as $combination) {
$term_names = array();
$term_ids = array();
$term_slugs = array();
$product_values = $child_product_template;
$combination_terms = get_terms('wpsc-variation', array(
'hide_empty' => 0,
'include' => implode(",", $combination),
'orderby' => 'parent',
));
foreach($combination_terms as $term) {
$term_ids[] = $term->term_id;
$term_slugs[] = $term->slug;
$term_names[] = $term->name;
}
$product_values['post_title'] .= " (".implode(", ", $term_names).")";
$product_values['post_name'] = sanitize_title($product_values['post_title']);
$selected_post = get_posts(array(
'name' => $product_values['post_name'],
'post_parent' => $product_id,
'post_type' => "wpsc-product",
'post_status' => 'all',
'suppress_filters' => true
));
$selected_post = array_shift($selected_post);
$child_product_id = wpsc_get_child_object_in_terms($product_id, $term_ids, 'wpsc-variation');
$already_a_variation = true;
if($child_product_id == false) {
$already_a_variation = false;
if($selected_post != null) {
$child_product_id = $selected_post->ID;
} else {
$child_product_id = wp_insert_post($product_values);
}
} else {
// sometimes there have been problems saving the variations, this gets the correct product ID
if(($selected_post != null) && ($selected_post->ID != $child_product_id)) {
$child_product_id = $selected_post->ID;
}
}
$product_children[] = $child_product_id;
if($child_product_id > 0) {
wp_set_object_terms($child_product_id, $term_slugs, 'wpsc-variation');
}
//JS - 7.9 - Adding loop to include meta data in child product.
if(!$already_a_variation){
$this_child_product_meta = apply_filters( 'insert_child_product_meta', $child_product_meta, $product_id, $combination_terms );
foreach ($this_child_product_meta as $meta_key => $meta_value ) :
if ($meta_key == "_wpsc_product_metadata") {
update_post_meta($child_product_id, $meta_key, unserialize($meta_value[0]));
} else {
update_post_meta($child_product_id, $meta_key, $meta_value[0]);
}
endforeach;
if ( is_array( $term_ids ) && $price = wpsc_determine_variation_price( $child_product_id, $term_ids ) )
update_product_meta( $child_product_id, 'price', $price );
}
}
//For reasons unknown, this code did not previously deal with variation deletions.
//Basically, we'll just check if any existing term associations are missing from the posted variables, delete if they are.
//Get posted terms (multi-dimensional array, first level = parent var, second level = child var)
$posted_term = $variations;
//Get currently associated terms
$currently_associated_var = $product_terms;
foreach ($currently_associated_var as $current) {
$currently_associated_vars[] = $current->term_id;
}
$posted_terms = array();
foreach ($posted_term as $term=>$val) {
$posted_terms[] = $term;
if(is_array($val)) {
foreach($val as $term2=>$val2) {
$posted_terms[] = $term2;
}
}
}
if(!empty($currently_associated_vars)){
$term_ids_to_delete = array();
$term_ids_to_delete = array_diff($currently_associated_vars, $posted_terms);
}
if(isset($_REQUEST["post_ID"])) {
$post_id = $_REQUEST["post_ID"];
} elseif(isset($_REQUEST["product_id"])) {
$post_id = $_REQUEST["product_id"];
} else {
return;
}
if(!empty($term_ids_to_delete) && (isset($_REQUEST["product_id"]) || isset($post_id))) {
$post_ids_to_delete = array();
// Whatever remains, find child products of current product with that term, in the variation taxonomy, and delete
$post_ids_to_delete = wpsc_get_child_object_in_terms_var($_REQUEST["product_id"], $term_ids_to_delete, 'wpsc-variation');
if(is_array($post_ids_to_delete) && !empty($post_ids_to_delete)) {
foreach($post_ids_to_delete as $object_ids) {
foreach($object_ids as $object_id) {
wp_delete_post($object_id);
}
}
}
}
$current_children = get_posts(array(
'post_parent' => $post_id,
'post_type' => 'wpsc-product',
'post_status' => 'all',
'numberposts' => -1
));
foreach((array)$current_children as $child_prod){
$childs[] = $child_prod->ID;
}
if(!empty($childs)){
$old_ids_to_delete = array_diff($childs, $product_children);
$old_ids_to_delete = apply_filters('wpsc_edit_product_variations_deletion', $old_ids_to_delete);
if(is_array($old_ids_to_delete) && !empty($old_ids_to_delete)) {
foreach($old_ids_to_delete as $object_ids) {
wp_delete_post($object_ids);
}
}
}
_wpsc_refresh_parent_product_terms( $parent_id );
}
/**
* @param int $product_id
* @param int $new_currency_country_id
* @param float $new_price
*/
function wpsc_update_alt_product_currency($product_id, $new_currency_country_id, $new_price ){
global $wpdb;
$old_currency_original_meta_value = get_product_meta( $product_id, 'currency', true );
if ( !is_array( $old_currency_original_meta_value ) ) {
$new_meta_value = array();
} else {
$new_meta_value = $old_currency_original_meta_value;
}
if ( ! empty( $new_currency_country_id ) ) {
$wpsc_country_new_currency = wpsc_get_country_object( $new_currency_country_id );
if ( $wpsc_country_new_currency ) {
$new_meta_value[ $wpsc_country_new_currency->get_isocode() ] = $new_price;
if ( ! empty( $new_price ) ) {
update_product_meta( $product_id, 'currency', $new_meta_value );
} else {
if ( ( empty( $new_meta_value[ $wpsc_country_new_currency->get_isocode() ] ) || 0.00 == $new_meta_value[ $wpsc_country_new_currency->get_isocode() ] ) && is_array( $new_meta_value ) ) {
unset( $new_meta_value[ $wpsc_country_new_currency->get_isocode() ] );
}
update_product_meta( $product_id, 'currency', $new_meta_value );
}
}
}
}
/**
* wpsc_update_product_meta function
*
* @param integer product ID
* @param string comma separated tags
*/
function wpsc_update_product_meta($product_id, $product_meta) {
if($product_meta != null) {
foreach((array)$product_meta as $key => $value) {
update_post_meta($product_id, $key, $value);
}
}
}
/**
* Called from javascript within product page to toggle publish status - AJAX
* @return bool publish status
*/
function wpsc_ajax_toggle_publish() {
/**
* @todo - Check Admin Referer
* @todo - Check Permissions
*/
$status = (wpsc_toggle_publish_status($_REQUEST['productid'])) ? ('true') : ('false');
exit( $status );
}
/*
/* END - Publish /No Publish functions
*/
function wpsc_update_custom_meta($product_id, $post_data) {
if ( isset( $post_data['new_custom_meta'] ) && $post_data['new_custom_meta'] != null ) {
foreach((array)$post_data['new_custom_meta']['name'] as $key => $name) {
$value = $post_data['new_custom_meta']['value'][(int)$key];
if(($name != '') && ($value != '')) {
add_post_meta($product_id, $name, $value);
}
}
}
if (!isset($post_data['custom_meta'])) $post_data['custom_meta'] = '';
if($post_data['custom_meta'] != null) {
foreach((array)$post_data['custom_meta'] as $key => $values) {
if(($values['name'] != '') && ($values['value'] != '')) {
update_post_meta($product_id, $values['name'], $values['value']);
}
}
}
}
/**
* wpsc_item_process_file function
*
* @param integer product ID
* @param array the file array from $_FILES
* @param array the preview file array from $_FILES
*/
function wpsc_item_process_file( $product_id, $submitted_file, $preview_file = null ) {
add_filter( 'upload_dir', 'wpsc_modify_upload_directory' );
$time = current_time( 'mysql' );
if ( $post = get_post( $product_id ) ) {
if ( substr( $post->post_date, 0, 4 ) > 0 )
$time = $post->post_date;
}
$file = wp_handle_upload( $submitted_file, array( 'test_form' => false ), $time );
if ( isset( $file['error'] ) ) {
return new WP_Error( 'upload_error', $file['error'] );
}
$name_parts = pathinfo( $file['file'] );
// Construct the attachment array
$attachment = array(
'post_mime_type' => $file['type'],
'guid' => $file['url'],
'post_parent' => $product_id,
'post_title' => $name_parts['basename'],
'post_content' => '',
'post_type' => 'wpsc-product-file',
'post_status' => 'inherit'
);
// Save the data
wp_insert_post( $attachment );
remove_filter( 'upload_dir', 'wpsc_modify_upload_directory' );
}
function wpsc_modify_upload_directory($input) {
$previous_subdir = $input['subdir'];
$download_subdir = str_replace($input['basedir'], '', WPSC_FILE_DIR);
$input['path'] = substr_replace(str_replace($previous_subdir, $download_subdir, $input['path']),'',-1);
$input['url'] = substr_replace(str_replace($previous_subdir, $download_subdir, $input['url']),'',-1);
$input['subdir'] = substr_replace(str_replace($previous_subdir, $download_subdir, $input['subdir']),'',-1);
return $input;
}
function wpsc_modify_preview_directory($input) {
$previous_subdir = $input['subdir'];
$download_subdir = str_replace($input['basedir'], '', WPSC_PREVIEW_DIR);
$input['path'] = substr_replace(str_replace($previous_subdir, $download_subdir, $input['path']),'',-1);
$input['url'] = substr_replace(str_replace($previous_subdir, $download_subdir, $input['url']),'',-1);
$input['subdir'] = substr_replace(str_replace($previous_subdir, $download_subdir, $input['subdir']),'',-1);
return $input;
}
/**
* wpsc_item_reassign_file function
*
* @param integer product ID
* @param string the selected file name;
*/
function wpsc_item_reassign_file($product_id, $selected_files) {
global $wpdb;
$product_file_list = array();
// initialise $idhash to null to prevent issues with undefined variables and error logs
$idhash = null;
$args = array(
'post_type' => 'wpsc-product-file',
'post_parent' => $product_id,
'numberposts' => -1,
'post_status' => 'any'
);
$attached_files = (array) get_posts( $args );
$attached_files_by_file = array();
foreach($attached_files as $key => $attached_file) {
$attached_files_by_file[$attached_file->post_title] = $attached_files[$key];
}
/* if we are editing, grab the current file and ID hash */
if(!$selected_files) {
// unlikely that anyone will ever upload a file called .none., so its the value used to signify clearing the product association
return null;
}
foreach($selected_files as $selected_file) {
// if we already use this file, there is no point doing anything more.
$file_is_attached = false;
$selected_file_path = WPSC_FILE_DIR.basename($selected_file);
if(isset($attached_files_by_file[$selected_file])) {
$file_is_attached = true;
}
if($file_is_attached == false ) {
$type = wpsc_get_mimetype($selected_file_path);
$attachment = array(
'post_mime_type' => $type,
'post_parent' => $product_id,
'post_title' => $selected_file,
'post_content' => '',
'post_type' => "wpsc-product-file",
'post_status' => 'inherit'
);
wp_insert_post($attachment);
} else {
$product_post_values = array(
'ID' => $attached_files_by_file[$selected_file]->ID,
'post_status' => 'inherit'
);
wp_update_post($product_post_values);
}
}
foreach($attached_files as $attached_file) {
if(!in_array($attached_file->post_title, $selected_files)) {
$product_post_values = array(
'ID' => $attached_file->ID,
'post_status' => 'draft'
);
wp_update_post($product_post_values);
}
}
return true;