This repository has been archived by the owner on Mar 1, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
spreadplugin.php
2820 lines (2375 loc) · 123 KB
/
spreadplugin.php
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
/**
* Plugin Name: WP-Spreadplugin
* Plugin URI: http://wordpress.org/extend/plugins/wp-spreadplugin/
* Description: This plugin uses the Spreadshirt API to list articles and let your customers order articles of your Spreadshirt shop using Spreadshirt order process.
* Version: 3.9.7.7
* Author: Thimo Grauerholz
* Author URI: http://www.spreadplugin.de
*/
@set_time_limit(0);
/**
* WP_Spreadplugin class
*/
if (!class_exists('WP_Spreadplugin')) {
class WP_Spreadplugin {
private $stringTextdomain = 'spreadplugin';
public static $shopOptions;
private static $worksWithLocale = true;
public static $shopArticleSortOptions = array (
'name',
'price',
'recent',
'weight'
);
public $defaultOptions = array (
'shop_id' => '',
'shop_locale' => '',
'shop_api' => '',
'shop_source' => '',
'shop_secret' => '',
'shop_limit' => '',
'shop_category' => '',
'shop_social' => '',
'shop_enablelink' => '',
'shop_productcategory' => '',
'shop_productsubcategory' => '',
'shop_sortby' => '',
'shop_linktarget' => '',
'shop_checkoutiframe' => '',
'shop_designershop' => '',
'shop_display' => '',
'shop_designsbackground' => '',
'shop_showdescription' => '',
'shop_showproductdescription' => '',
'shop_imagesize' => '',
'shop_showextendprice' => '',
'shop_zoomimagebackground' => '',
'shop_infinitescroll' => '',
'shop_customcss' => '',
'shop_design' => '',
'shop_article' => '',
'shop_view' => '',
'shop_zoomtype' => '',
'shop_lazyload' => '',
'shop_language' => '',
'shop_basket_text_icon' => '',
'shop_debug' => '',
'shop_sleep' => '',
'shop_designer' => '',
'shop_max_quantity_articles' => '',
'shop_url_anchor' => '',
'shop_url_productdetail_slug' => ''
);
private static $shopCache = 0; // Shop article cache - never expires
/**
* Returns an instance of this class.
*/
public static function get_instance() {
if (null == self::$instance) {
self::$instance = new WP_Spreadplugin();
}
return self::$instance;
}
public function __construct(){
add_action('init', array (
&$this,
'startSession'
), 1);
add_action('wp_logout', array (
&$this,
'endSession'
));
add_action('wp_login', array (
&$this,
'endSession'
));
add_shortcode('spreadplugin', array (
$this,
'Spreadplugin'
));
// Ajax actions
/*
* add_action('wp_ajax_nopriv_mergeBasket', array( &$this,'mergeBaskets' )); add_action('wp_ajax_mergeBasket', array( &$this,'mergeBaskets' ));
*/
add_action('wp_ajax_nopriv_myAjax', array (
&$this,
'doAjax'
));
add_action('wp_ajax_myAjax', array (
&$this,
'doAjax'
));
add_action('wp_ajax_nopriv_myCart', array (
&$this,
'doCart'
));
add_action('wp_ajax_myCart', array (
&$this,
'doCart'
));
add_action('wp_ajax_nopriv_myDelete', array (
&$this,
'doCartItemDelete'
));
add_action('wp_ajax_myDelete', array (
&$this,
'doCartItemDelete'
));
add_action('wp_ajax_rebuildCache', array (
&$this,
'doRebuildCache'
));
add_action('wp_enqueue_scripts', array (
&$this,
'enqueueSomes'
));
add_action('wp_head', array (
&$this,
'loadHead'
));
add_action('wp_footer', array (
&$this,
'loadFoot'
));
add_action('init', array (
&$this,
'addQueryVars'
));
// admin check
if (is_admin()) {
// Regenerate cache after activation of the plugin
// register_activation_hook(__FILE__, array(&$this,'helperClearCacheQuery'));
register_activation_hook( __FILE__, array(&$this,'registerRewriteRules'));
register_deactivation_hook( __FILE__, array(&$this,'flushRewriteRules'));
// add Admin menu
add_action('admin_menu', array (
&$this,
'addPluginPage'
));
// add Plugin settings link
add_filter('plugin_action_links', array (
&$this,
'addPluginSettingsLink'
), 10, 2);
add_action('admin_enqueue_scripts', array (
&$this,
'enqueueAdminJs'
));
}
}
/**
* PHP 4 Compatible Constructor
*/
function WP_Spreadplugin(){
$this->__construct();
}
/**
* Function Spreadplugin
*
* @return string article display
*
*/
public function Spreadplugin($atts){
$articleCleanData = array (); // Array with article informations for sorting and filtering
$articleCleanDataComplete = array (); // Array with article informations for sorting and filtering
$articleData = array ();
$designsData = array ();
// get admin options (default option set on admin page)
$conOp = $this->getAdminOptions();
// shortcode overwrites admin options (default option set on admin page) if available
$arrSc = shortcode_atts($this->defaultOptions, $atts);
// replace options by shortcode if set
if (!empty($arrSc)) {
foreach ($arrSc as $key => $option) {
if ($option != '') {
$conOp[$key] = $option;
}
}
}
// setting defaults if needed
self::$shopOptions = $conOp;
self::$shopOptions['shop_source'] = (empty($conOp['shop_source']) ? 'net' : $conOp['shop_source']);
self::$shopOptions['shop_limit'] = (empty($conOp['shop_limit']) ? 10 : intval($conOp['shop_limit']));
self::$shopOptions['shop_locale'] = ""; // Workaround for older versions of this plugin
self::$shopOptions['shop_imagesize'] = (intval($conOp['shop_imagesize']) == 0 ? 190 : intval($conOp['shop_imagesize']));
self::$shopOptions['shop_zoomimagebackground'] = (empty($conOp['shop_zoomimagebackground']) ? 'FFFFFF' : str_replace("#", "", $conOp['shop_zoomimagebackground']));
self::$shopOptions['shop_infinitescroll'] = ($conOp['shop_infinitescroll'] == '' ? 1 : $conOp['shop_infinitescroll']);
self::$shopOptions['shop_zoomtype'] = ($conOp['shop_zoomtype'] == '' ? 0 : $conOp['shop_zoomtype']);
self::$shopOptions['shop_lazyload'] = ($conOp['shop_lazyload'] == '' ? 1 : $conOp['shop_lazyload']);
self::$shopOptions['shop_debug'] = ($conOp['shop_debug'] == '' ? 0 : $conOp['shop_debug']);
self::$shopOptions['shop_max_quantity_articles'] = ($conOp['shop_max_quantity_articles'] == '' ? 1000 : $conOp['shop_max_quantity_articles']);
// Overwrite defaults if set (old vals)
self::$shopOptions['shop_designer'] = (self::$shopOptions['shop_designer'] == 2 ? self::$shopOptions['shop_designer'] = 1 : self::$shopOptions['shop_designer']);
// Disable Zoom on min view, because of the new view - not on details page
if (self::$shopOptions['shop_view'] == 2 && !get_query_var(self::$shopOptions['shop_url_productdetail_slug'])) {
self::$shopOptions['shop_zoomtype'] = 2;
}
// overwrite translation if language available and set
if (!empty(self::$shopOptions['shop_language'])) {
$_ol = dirname(__FILE__) . '/translation/' . $this->stringTextdomain . '-' . self::$shopOptions['shop_language'] . '.mo';
if (file_exists($_ol)) {
load_textdomain($this->stringTextdomain, $_ol);
}
} else {
load_plugin_textdomain($this->stringTextdomain, false, dirname(plugin_basename(__FILE__)) . '/translation');
}
if (get_query_var('productCategory')) {
$c = get_query_var('productCategory');
self::$shopOptions['shop_productcategory'] = $c;
self::$shopOptions['shop_productsubcategory'] = 'all';
if (get_query_var('productSubCategory')) {
$c = get_query_var('productSubCategory');
self::$shopOptions['shop_productsubcategory'] = $c;
}
}
if (!empty(self::$shopOptions['shop_productcategory'])) {
self::$shopOptions['shop_productcategory'] = htmlspecialchars_decode(self::$shopOptions['shop_productcategory']);
}
if (!empty(self::$shopOptions['shop_productsubcategory'])) {
self::$shopOptions['shop_productsubcategory'] = htmlspecialchars_decode(self::$shopOptions['shop_productsubcategory']);
}
if (!empty(self::$shopOptions['shop_productcategory']) && empty(self::$shopOptions['shop_productsubcategory'])) {
self::$shopOptions['shop_productsubcategory'] = "all";
}
if (get_query_var('articleSortBy')) {
$c = urldecode(get_query_var('articleSortBy'));
self::$shopOptions['shop_sortby'] = $c;
}
// At filtering articles don't use designs view
if (self::$shopOptions['shop_display'] == 1 && self::$shopOptions['shop_productcategory'] == '' && self::$shopOptions['shop_design'] == 0) {
} else {
self::$shopOptions['shop_display'] = 0;
}
// check
if (!empty(self::$shopOptions['shop_id']) && !empty(self::$shopOptions['shop_api']) && !empty(self::$shopOptions['shop_secret'])) {
$paged = (get_query_var('pagesp') ? get_query_var('pagesp') : 1);
$offset = ($paged - 1) * self::$shopOptions['shop_limit'];
// get article data
$articleData = self::getCacheArticleData();
// get rid of types in array
$typesData = $articleData['types'];
unset($articleData['types']);
// get shipment data and delete
$shipmentData = $articleData['shipment'];
unset($articleData['shipment']);
// get designs data
$designsData = self::getCacheDesignsData();
if (self::$shopOptions['shop_debug'] == 1) {
echo "Stored Article Data RAW (0):<br>";
print_r($articleData);
}
if (self::$shopOptions['shop_debug'] == 1) {
echo "Stored Design Data RAW (0):<br>";
print_r($designsData);
}
// built array with articles for sorting and filtering
if (is_array($designsData)) {
foreach ($designsData as $designId => $arrDesigns) {
if (!empty($articleData[$designId])) {
foreach ($articleData[$designId] as $articleId => $arrArticle) {
$articleCleanData[$articleId] = $arrArticle;
$articleCleanDataComplete[$articleId] = $arrArticle;
}
}
}
if (self::$shopOptions['shop_debug'] == 1) {
echo "With Design (1):<br>";
print_r($articleCleanData);
}
}
// Add all those articles with no own designs and other cases - maybe overwrite them
if (!empty($articleData)) {
foreach ($articleData as $arrDesigns) {
if (!empty($arrDesigns)) {
foreach ($arrDesigns as $articleId => $arrArticle) {
$articleCleanData[$articleId] = $arrArticle;
$articleCleanDataComplete[$articleId] = $arrArticle;
}
}
}
if (self::$shopOptions['shop_debug'] == 1) {
echo "With some cases (2):<br>";
print_r($articleCleanData);
}
}
// filter
if (is_array($articleCleanData)) {
// Single product
if (isset(self::$shopOptions['shop_article']) && self::$shopOptions['shop_article'] > 0 && array_key_exists(self::$shopOptions['shop_article'], $articleCleanData)) {
$articleCleanData = array (
self::$shopOptions['shop_article'] => $articleCleanData[self::$shopOptions['shop_article']]
);
} else {
// All products
foreach ($articleCleanData as $id => $article) {
// designs
if (self::$shopOptions['shop_design'] > 0 && self::$shopOptions['shop_design'] != $articleCleanData[$id]['designid']) {
unset($articleCleanData[$id]);
}
// product categories
if (!empty(self::$shopOptions['shop_productcategory']) && isset($typesData[self::$shopOptions['shop_productcategory']][self::$shopOptions['shop_productsubcategory']])) {
if (!isset($typesData[self::$shopOptions['shop_productcategory']][self::$shopOptions['shop_productsubcategory']][$article['type']])) {
unset($articleCleanData[$id]);
}
}
}
}
}
// default sort
@uasort($designsData, create_function('$a,$b', "return (\$a[place] > \$b[place])?-1:1;"));
/*
* 2014-06-22 Changed from place to id, place is not set anymore (and sort direction to desc) 2014-07-20 Changed back to place and sort direction asc, because place added again
*/
@uasort($articleCleanData, create_function('$a,$b', "return (\$a[place] < \$b[place])?-1:1;"));
// sorting
if (self::$shopOptions['shop_display'] == 1) {
if (!empty(self::$shopOptions['shop_sortby']) && is_array($designsData) && in_array(self::$shopOptions['shop_sortby'], self::$shopArticleSortOptions)) {
if (self::$shopOptions['shop_sortby'] == "recent") {
krsort($designsData);
} elseif (self::$shopOptions['shop_sortby'] == "price") {
uasort($designsData, create_function('$a,$b', "return (\$a[pricenet] < \$b[pricenet])?-1:1;"));
} elseif (self::$shopOptions['shop_sortby'] == "weight") {
uasort($designsData, create_function('$a,$b', "return (\$a[weight] > \$b[weight])?-1:1;"));
} else {
uasort($designsData, create_function('$a,$b', "return strnatcmp(\$a[" . self::$shopOptions['shop_sortby'] . "],\$b[" . self::$shopOptions['shop_sortby'] . "]);"));
}
}
} else {
if (!empty(self::$shopOptions['shop_sortby']) && is_array($articleCleanData) && in_array(self::$shopOptions['shop_sortby'], self::$shopArticleSortOptions)) {
if (self::$shopOptions['shop_sortby'] == "recent") {
krsort($articleCleanData);
} elseif (self::$shopOptions['shop_sortby'] == "price") {
uasort($articleCleanData, create_function('$a,$b', "return (\$a[pricenet] < \$b[pricenet])?-1:1;"));
} elseif (self::$shopOptions['shop_sortby'] == "weight") {
uasort($articleCleanData, create_function('$a,$b', "return (\$a[weight] > \$b[weight])?-1:1;"));
} else {
uasort($articleCleanData, create_function('$a,$b', "return strnatcmp(\$a[" . self::$shopOptions['shop_sortby'] . "],\$b[" . self::$shopOptions['shop_sortby'] . "]);"));
}
}
}
// pagination
if (self::$shopOptions['shop_display'] == 1) {
if (!empty(self::$shopOptions['shop_limit']) && is_array($designsData)) {
$cArticleNext = count(array_slice($designsData, $offset + self::$shopOptions['shop_limit'], self::$shopOptions['shop_limit'], true));
$designsData = array_slice($designsData, $offset, self::$shopOptions['shop_limit'], true);
}
} else {
if (!empty(self::$shopOptions['shop_limit']) && is_array($articleCleanData)) {
$cArticleNext = count(array_slice($articleCleanData, $offset + self::$shopOptions['shop_limit'], self::$shopOptions['shop_limit'], true));
$articleCleanData = array_slice($articleCleanData, $offset, self::$shopOptions['shop_limit'], true);
}
}
// Start output
$output = (!empty($conOp['shop_url_anchor'])?'<a name="' . $conOp['shop_url_anchor'] . '"></a>':"");
// check if curl is enabled
$output .= (function_exists('curl_version') ? '' : '<span class="error">Curl seems to be disabled. In order to use Shop functionality, it should be enabled</span>');
// wrapper for integrated designer
if (self::$shopOptions['shop_designer'] == 1) {
$output .= '
<div id="spreadplugin-designer-wrapper"><div id="spreadplugin-designer" class="spreadplugin-designer spreadplugin-clearfix"></div></div>
';
}
// Start div
$output .= '
<div id="spreadplugin-items" class="spreadplugin-items spreadplugin-clearfix">
';
// display
if (count($articleData) == 0 || $articleData == false) {
$output .= '<br>No articles in Shop. Please rebuild cache.';
} else {
// Listing product
if (!get_query_var(self::$shopOptions['shop_url_productdetail_slug'])) {
// add spreadplugin-menu
$output .= '<div id="spreadplugin-menu" class="spreadplugin-menu">';
/*
// add product categories
$output .= '<select name="productCategory" id="productCategory">';
$output .= '<option value="">' . __('Product category', $this->stringTextdomain) . '</option>';
if (isset($typesData)) {
foreach ($typesData as $t => $v) {
$output .= '<option value="' . str_replace('+','%20',urlencode($t)) . '"' . ($t == self::$shopOptions['shop_productcategory'] ? ' selected' : '') . '>' . $t . '</option>';
}
}
$output .= '</select> ';
// simple sub categories
// @TODO Javascript
if (get_query_var('productCategory')) {
$output .= '<select name="productSubCategory" id="productSubCategory">';
$output .= '<option value="all"></option>';
if (isset($typesData[self::$shopOptions['shop_productcategory']])) {
@ksort($typesData[self::$shopOptions['shop_productcategory']]);
unset($typesData[self::$shopOptions['shop_productcategory']]['all']);
foreach ($typesData[self::$shopOptions['shop_productcategory']] as $t => $v) {
$output .= '<option value="' . str_replace('+','%20',urlencode($t)) . '"' . ($t == self::$shopOptions['shop_productsubcategory'] ? ' selected' : '') . '>' . $t . '</option>';
}
}
$output .= '</select> ';
}
*/
// add sorting
/*$output .= '<select name="articleSortBy" id="articleSortBy">';
$output .= '<option value="">' . __('Sort by', $this->stringTextdomain) . '</option>';
$output .= '<option value="name"' . ('name' == self::$shopOptions['shop_sortby'] ? ' selected' : '') . '>' . __('name', $this->stringTextdomain) . '</option>';
$output .= '<option value="price"' . ('price' == self::$shopOptions['shop_sortby'] ? ' selected' : '') . '>' . __('price', $this->stringTextdomain) . '</option>';
$output .= '<option value="recent"' . ('recent' == self::$shopOptions['shop_sortby'] ? ' selected' : '') . '>' . __('recent', $this->stringTextdomain) . '</option>';
$output .= '<option value="weight"' . ('weight' == self::$shopOptions['shop_sortby'] ? ' selected' : '') . '>' . __('weight', $this->stringTextdomain) . '</option>';
$output .= '</select>';
*/
// url not needed here, but just in case if js won't work for some reason
$output .= '<div id="checkout" class="spreadplugin-checkout"><span></span> <a href="' . (!empty($_SESSION['checkoutUrl'][self::$shopOptions['shop_source'] . self::$shopOptions['shop_language']]) ? $_SESSION['checkoutUrl'][self::$shopOptions['shop_source'] . self::$shopOptions['shop_language']] : '') . '" target="' . self::$shopOptions['shop_linktarget'] . '" id="basketLink" class="spreadplugin-checkout-link' . (self::$shopOptions['shop_basket_text_icon'] == 1 ? ' button' : '') . '">' . (self::$shopOptions['shop_basket_text_icon'] == 0 ? __('Basket', $this->stringTextdomain) : '') . '</a></div>';
$output .= '<div id="spreadplugin-cart" class="spreadplugin-cart"></div>';
$output .= '</div>';
$output .= '<div id="spreadplugin-list">';
// Designs view
if (self::$shopOptions['shop_display'] == 1) {
if (!empty($designsData)) {
foreach ($designsData as $designId => $arrDesigns) {
$bgc = false;
$addStyle = '';
// Display just Designs with products
if (!empty($articleData[$designId])) {
// check if designs background is enabled
if (self::$shopOptions['shop_designsbackground'] == 1) {
// fetch first article background color
@reset($articleData[$designId]);
$bgcV = $articleData[$designId][key($articleData[$designId])]['default_bgc'];
$bgcV = str_replace("#", "", $bgcV);
// calc to hex
$bgc = $this->hex2rgb($bgcV);
$addStyle = "style=\"background-color:rgba(" . $bgc[0] . "," . $bgc[1] . "," . $bgc[2] . ",0.4);\"";
}
$output .= "<div class=\"spreadplugin-designs\">";
$output .= $this->displayDesigns($designId, $arrDesigns, $articleData[$designId], $bgc);
$output .= "<div id=\"designContainer_" . $designId . "\" class=\"design-container spreadplugin-clearfix\" " . $addStyle . ">";
if (!empty($articleData[$designId])) {
// default sort
@uasort($articleData[$designId], create_function('$a,$b', "return (\$a[id] > \$b[id])?-1:1;")); // 2014-06-22 Changed from place to id, place is not set anymore (and sort direction to desc
switch (self::$shopOptions['shop_view']) {
case 1:
foreach ($articleData[$designId] as $articleId => $arrArticle) {
$output .= $this->displayListArticles($articleId, $arrArticle, self::$shopOptions['shop_zoomimagebackground']);
}
break;
case 2:
foreach ($articleData[$designId] as $articleId => $arrArticle) {
$output .= $this->displayMinArticles($articleId, $arrArticle, self::$shopOptions['shop_zoomimagebackground']);
}
break;
default:
foreach ($articleData[$designId] as $articleId => $arrArticle) {
$output .= $this->displayArticles($articleId, $arrArticle, self::$shopOptions['shop_zoomimagebackground']);
}
break;
}
}
$output .= "</div>";
$output .= "</div>";
}
}
} else {
$output .= "No designs available?";
}
} else {
// Article view
if (!empty($articleCleanData)) {
switch (self::$shopOptions['shop_view']) {
case 1:
foreach ($articleCleanData as $articleId => $arrArticle) {
$output .= $this->displayListArticles($articleId, $arrArticle, self::$shopOptions['shop_zoomimagebackground']);
}
break;
case 2:
foreach ($articleCleanData as $articleId => $arrArticle) {
$output .= $this->displayMinArticles($articleId, $arrArticle, self::$shopOptions['shop_zoomimagebackground']);
}
break;
default:
foreach ($articleCleanData as $articleId => $arrArticle) {
$output .= $this->displayArticles($articleId, $arrArticle, self::$shopOptions['shop_zoomimagebackground']);
}
break;
}
}
}
$output .= '</div>';
$output .= "<div id=\"pagination\">";
if ($cArticleNext > 0) {
$output .= "<a href=\"" . $this->prettyPagesUrl() . "\">" . __('next', $this->stringTextdomain) . "</a>";
}
$output .= "</div>";
} else {
// display product page
$output .= '<div id="spreadplugin-list">';
// checkout
// add simple spreadplugin-menu
$output .= '<div id="spreadplugin-menu" class="spreadplugin-menu">';
$output .= '<a href="javascript:history.back();">' . __('Back', $this->stringTextdomain);
$output .= '<div id="checkout" class="spreadplugin-checkout"><span></span> <a href="' . (!empty($_SESSION['checkoutUrl'][self::$shopOptions['shop_source'] . self::$shopOptions['shop_language']]) ? $_SESSION['checkoutUrl'][self::$shopOptions['shop_source'] . self::$shopOptions['shop_language']] : '') . '" target="' . self::$shopOptions['shop_linktarget'] . '" id="basketLink" class="spreadplugin-checkout-link' . (self::$shopOptions['shop_basket_text_icon'] == 1 ? ' button' : '') . '">' . (self::$shopOptions['shop_basket_text_icon'] == 0 ? __('Basket', $this->stringTextdomain) : '') . '</a></div>';
$output .= '<div id="cart" class="spreadplugin-cart"></div>';
$output .= '</div>';
// product
if (!empty($articleCleanDataComplete[intval(get_query_var(self::$shopOptions['shop_url_productdetail_slug']))])) {
$output .= $this->displayDetailPage(intval(get_query_var(self::$shopOptions['shop_url_productdetail_slug'])), $articleCleanDataComplete[intval(get_query_var(self::$shopOptions['shop_url_productdetail_slug']))], self::$shopOptions['shop_zoomimagebackground']);
}
$output .= '</div>';
}
}
// End div
$output .= '</div>';
// Shipment Table
if (!empty($shipmentData)) {
$output .= '<div id="spreadplugin-shipment-wrapper">
<table class="shipment-table">';
foreach ($shipmentData as $c => $v) {
$output .= '<tr>';
$output .= '<th colspan="2">' . $c . '</th>';
$output .= '</tr>';
foreach ($v as $m => $d) {
$output .= '<tr>';
$output .= '<td>' . __('Order Value', $this->stringTextdomain) . '<br>';
if ($d['value-to'] == 0) {
$output .= __('over', $this->stringTextdomain) . ' ';
}
if ($d['value-from'] > 0) {
$output .= self::formatPrice($d['value-from'], '') . ' ';
}
if ($d['value-to'] > 0) {
$output .= __('up to', $this->stringTextdomain) . ' ' . self::formatPrice($d['value-to'], '');
}
$output .= ' </td>';
$output .= '<td>' . self::formatPrice($d['price'], '') . '</td>';
$output .= '</tr>';
}
}
$output .= '</table>
</div>';
}
return $output;
}
}
/**
* Function getCacheArticleData
*
* @return array Article data
*/
private static function getCacheArticleData(){
return get_transient('spreadplugin2-article-cache-' . get_the_ID());
}
/**
* function parseArticleData
* Retrieves article data and collect
*/
private function getRawArticleData($pageId){
$articleData = array ();
$articleDataObj = array ();
$apiUrlBase = 'http://api.spreadshirt.' . self::$shopOptions['shop_source'] . '/api/v1/shops/' . self::$shopOptions['shop_id'];
$apiUrlBase .= (!empty(self::$shopOptions['shop_category']) ? '/articleCategories/' . self::$shopOptions['shop_category'] : '');
$apiUrlBase .= '/articles';
$apiUrlBase .= (!empty(self::$shopOptions['shop_article']) ? '/' . intval(self::$shopOptions['shop_article']) : '');
$apiUrlBase .= '?' . 'fullData=true&noCache=true';
// call first to get count of articles
$apiUrl = $apiUrlBase . '&limit=' . self::$shopOptions['shop_max_quantity_articles'];
$objArticlesBase = $this->runTestApiUrlWithLocaleReturnObject($apiUrl,$pageId);
return $objArticlesBase;
}
/**
* function getTypesData
* Retrieves types data
*/
private function getTypesData(){
$arrTypes = array ();
// Get ProductTypeDepartments
$stringTypeApiUrl = 'http://api.spreadshirt.' . self::$shopOptions['shop_source'] . '/api/v1/shops/' . self::$shopOptions['shop_id'] . '/productTypeDepartments?fullData=true&noCache=true';
$objTypes = $this->runTestApiUrlWithLocaleReturnObject($stringTypeApiUrl);
if (is_object($objTypes)) {
foreach ($objTypes->productTypeDepartment as $row) {
foreach ($row->categories->category as $subrow) {
foreach ($subrow->productTypes as $subrow2) {
foreach ($subrow2->productType as $subrow3) {
$arrTypes[(string)$row->name][(string)$subrow->name][(int)$subrow3['id']] = 1;
$arrTypes[(string)$row->name]['all'][(int)$subrow3['id']] = 1;
}
}
}
}
}
return $arrTypes;
}
private function runTestApiUrlWithLocaleReturnObject($url,$pageId = 0) {
$objTypes = "";
$this->reparseShortcodeData(($pageId>0?$pageId:(get_query_var('pageid') ? intval(get_query_var('pageid')) : null)));
/*
* Run test with locale if previous test was successfull
*
* 2015-11-24 always run test with locale - state not changed anymore. See below
*/
if (self::$worksWithLocale == true) {
$testUrl = $url.(strpos($url,'&') === false?'?':'&').'locale=' . (empty(self::$shopOptions['shop_language'])?get_locale():self::$shopOptions['shop_language']);
if (self::$shopOptions['shop_debug'] == 1) {
echo "try url $testUrl <br>";
}
$stringTypeXml = wp_remote_get($testUrl, array('timeout' => 120));
$stringTypeXml = wp_remote_retrieve_body($stringTypeXml);
// Quickfix for Namespace changes of Spreadshirt API
$stringTypeXml = str_replace('<ns3:', '<', $stringTypeXml);
if (substr($stringTypeXml, 0, 5) != "<?xml") return 'Error fetching URL: ' . $testUrl;
// Quick (dirty) Workaround for Single Article using shop_article
if (!empty(self::$shopOptions['shop_article']) && strpos($stringTypeXml,'<articles>') === false) {
$stringTypeXml = str_replace('<article ', '<articles><article ', str_replace('</article>', '</article></articles>', $stringTypeXml));
}
$objTypes = new SimpleXmlElement($stringTypeXml);
}
// Run test without locale / fallback
if (empty($objTypes) || @$objTypes->count == 0) {
if (self::$shopOptions['shop_debug'] == 1) {
echo "failed url, try $url <br>";
}
$stringTypeXml = wp_remote_get($url, array('timeout' => 120));
$stringTypeXml = wp_remote_retrieve_body($stringTypeXml);
// Quickfix for Namespace changes of Spreadshirt API
$stringTypeXml = str_replace('<ns3:', '<', $stringTypeXml);
if (substr($stringTypeXml, 0, 5) != "<?xml") return 'Error fetching URL: ' . $testUrl;
// Quick (dirty) Workaround for Single Article using shop_article
if (!empty(self::$shopOptions['shop_article']) && strpos($stringTypeXml,'<articles>') === false) {
$stringTypeXml = str_replace('<article ', '<articles><article ', str_replace('</article>', '</article></articles>', $stringTypeXml));
}
$objTypes = new SimpleXmlElement($stringTypeXml);
/*
* Save test state
* 2015-11-24 disabled, always run test with locale
self::$worksWithLocale = false;
*/
}
return $objTypes;
}
/**
* function getShipmentData
* Retrieves types data
*/
private function getShipmentData(){
$arrTypes = array ();
$name = '';
$region = '';
// Get ProductTypeDepartments
$stringTypeApiUrl = 'http://api.spreadshirt.' . self::$shopOptions['shop_source'] . '/api/v1/shops/' . self::$shopOptions['shop_id'] . '/shippingTypes?fullData=true&noCache=true';
$objTypes = $this->runTestApiUrlWithLocaleReturnObject($stringTypeApiUrl);
$countryCode = explode("_", (empty(self::$shopOptions['shop_language'])?get_locale():self::$shopOptions['shop_language']));
if (is_object($objTypes) && !empty($countryCode[1])) {
foreach ($objTypes->shippingType as $row) {
foreach ($row->shippingCountries as $subrow) {
foreach ($subrow->shippingCountry as $subrow2) {
if ((string)$subrow2->isoCode == $countryCode[1]) {
// $name = (string)$subrow2->name;
$region = (int)$subrow2->shippingRegion['id'];
break;
}
}
}
}
}
if ($region !== '') {
foreach ($objTypes->shippingType as $row) {
foreach ($row->shippingRegions as $subrow) {
foreach ($subrow->shippingRegion as $subrow2) {
if ((int)$subrow2['id'] == $region) {
foreach ($subrow2->shippingCosts as $subrow3) {
foreach ($subrow3->shippingCost as $subrow4) {
// [$name] Landname
$arrTypes[(string)$row->name][] = array (
'value-from' => (float)$subrow4->orderValueRange->from,
'value-to' => (float)$subrow4->orderValueRange->to,
'price' => (float)$subrow4->cost->vatIncluded
);
}
}
break;
}
}
}
}
}
return $arrTypes;
}
/**
* function getSingleArticleData
* Retrieves article data and save into cache
*/
private function getSingleArticleData($pageId, $articleId, $place){
$articleData = array ();
$stockstates_size = array ();
$stockstates_appearance = array ();
$objProductData = array ();
$objPrintData = array ();
$objArticleData = array ();
$objCurrencyData = array ();
$objProductData = array ();
$apiUrlBase = 'http://api.spreadshirt.' . self::$shopOptions['shop_source'] . '/api/v1/shops/' . self::$shopOptions['shop_id'];
$apiUrlBase .= '/articles/' . $articleId . '?' . 'fullData=true&noCache=true';
$article = $this->runTestApiUrlWithLocaleReturnObject($apiUrlBase,$pageId);
// 2015-11-27 Workaround with single article
if (!empty($article->article)) $article = $article->article;
if (!is_object($article)) return 'Article empty (object)';
if ((int)$article['id'] > 0) {
$url = (string)$article->product->productType->attributes('http://www.w3.org/1999/xlink') . '?noCache=true';
$objArticleData = $this->runTestApiUrlWithLocaleReturnObject($url,$pageId);
$url = (string)$article->price->currency->attributes('http://www.w3.org/1999/xlink');
$objCurrencyData = $this->runTestApiUrlWithLocaleReturnObject($url,$pageId);
$url = (string)$article->product->attributes('http://www.w3.org/1999/xlink') . '?noCache=true';
$objProductData = $this->runTestApiUrlWithLocaleReturnObject($url,$pageId);
if (is_object($objProductData)) {
if (!empty($objProductData->configurations->configuration->printType)) {
$url = (string)$objProductData->configurations->configuration->printType->attributes('http://www.w3.org/1999/xlink') . '?noCache=true';
$objPrintData = $this->runTestApiUrlWithLocaleReturnObject($url,$pageId);
}
}
$articleData['name'] = (string)$article->name;
$articleData['description'] = (string)$article->description;
$articleData['appearance'] = (int)$article->product->appearance['id'];
$articleData['view'] = (int)$article->product->defaultValues->defaultView['id'];
$articleData['type'] = (int)$article->product->productType['id'];
$articleData['productId'] = (int)$article->product['id'];
$articleData['pricenet'] = (float)$article->price->vatExcluded;
$articleData['pricebrut'] = (float)$article->price->vatIncluded;
$articleData['currencycode'] = (string)$objCurrencyData->isoCode;
$articleData['productname'] = (string)$objArticleData->name;
$articleData['productshortdescription'] = (string)$objArticleData->shortDescription;
$articleData['productdescription'] = (string)$objArticleData->description;
$articleData['weight'] = (float)$article['weight'];
$articleData['id'] = (int)$article['id'];
$articleData['place'] = $place;
$articleData['designid'] = (int)$article->product->defaultValues->defaultDesign['id'];
$articleData['printtypename'] = '';
$articleData['printtypedescription'] = '';
if (is_object($objPrintData)) {
$articleData['printtypename'] = (string)$objPrintData->name;
$articleData['printtypedescription'] = (string)$objPrintData->description;
}
/**
* Stock States disabled at the moment - the informations provided by spreadshirt aren't such reliable as needed
* *
*
* // Assignment of stock availability and matching to articles
* // echo (string)$article->name."<br>";
* foreach($objArticleData->stockStates->stockState as $val) {
* $stockstates_size[(int)$val->size['id']]=(string)$val->available;
* $stockstates_appearance[(int)$val->appearance['id']]=(string)$val->available;
* }
*
* foreach($objArticleData->sizes->size as $val) {
* // echo (int)$val['id']." ".$stockstates_size[(int)$val['id']]." ". (string)$val->name."<br>";
* if ($stockstates_size[(int)$val['id']] == "true") {
* $articleData['sizes'][(int)$val['id']]=(string)$val->name;
* }
* }
*
* foreach($objArticleData->appearances->appearance as $appearance) {
* if ((int)$article->product->appearance['id'] == (int)$appearance['id']) {
* $articleData['default_bgc'] = (string)$appearance->colors->color;
* }
*
* // echo (int)$val['id']." ".$stockstates_appearance[(int)$val['id']]." ". (string)$appearance->resources->resource->attributes('xlink', true)."<br>";
* if (($article->product->restrictions->freeColorSelection == 'true' && $stockstates_appearance[(int)$appearance['id']] == "true") || (int)$article->product->appearance['id'] == (int)$appearance['id']) {
* $articleData['appearances'][(int)$appearance['id']]=(string)$appearance->resources->resource->attributes('xlink', true);
* }
* }
*/
// replace to use stock states || weiter unten ist neuer
// sizes
if (!empty($objArticleData->sizes->size)) {
foreach ($objArticleData->sizes->size as $val) {
$articleData['sizes'][(int)$val['id']]['name'] = (string)$val->name;
if (!empty($val->measures->measure[0]->name)) {
$articleData['sizes'][(int)$val['id']]['measures'][0]['name'] = (string)$val->measures->measure[0]->name;
$articleData['sizes'][(int)$val['id']]['measures'][0]['value'] = (string)$val->measures->measure[0]->value;
}
if (!empty($val->measures->measure[1]->name)) {
$articleData['sizes'][(int)$val['id']]['measures'][1]['name'] = (string)$val->measures->measure[1]->name;
$articleData['sizes'][(int)$val['id']]['measures'][1]['value'] = (string)$val->measures->measure[1]->value;
}
}
}
if (!empty($objArticleData->resources)) {
foreach ($objArticleData->resources as $val) {
foreach ($val->resource as $vr) {
if ($vr['type'] == 'size') {
$articleData['product-resource-size'] = self::getRidOfHttp((string)$vr->attributes('http://www.w3.org/1999/xlink'));
}
if ($vr['type'] == 'detail') {
$articleData['product-resource-detail'] = self::getRidOfHttp((string)$vr->attributes('http://www.w3.org/1999/xlink'));
}
}
}
}
if (!empty($objArticleData->appearances->appearance)) {
foreach ($objArticleData->appearances->appearance as $appearance) {
if ((int)$article->product->appearance['id'] == (int)$appearance['id']) {
$articleData['default_bgc'] = (string)$appearance->colors->color;
}
if ($article->product->restrictions->freeColorSelection == 'true' || (int)$article->product->appearance['id'] == (int)$appearance['id']) {
$articleData['appearances'][(int)$appearance['id']] = self::getRidOfHttp((string)$appearance->resources->resource->attributes('http://www.w3.org/1999/xlink'));
}
}
}
// replace end
if (!empty($objArticleData->views->view)) {
foreach ($objArticleData->views->view as $view) {
$articleData['views'][(int)$view['id']] = self::getRidOfHttp((string)$article->resources->resource->attributes('http://www.w3.org/1999/xlink'));
}
}
return $articleData;
}
return 'Article empty';
}
/**
* Function getCacheDesignsData
*
* @return array designs data
*/
private static function getCacheDesignsData(){
return get_transient('spreadplugin2-designs-cache-' . get_the_ID());
}
/**
* Function getDesignsData
*
* Retrieves design data and saves directly into cache
* Has a quick load time, so possible to save directly to cache
*/
private function getDesignsData($pageId = 0){
// get page Id if not set in args
$pageId = ($pageId == 0 ? get_the_ID() : $pageId);
$arrTypes = array ();
$apiUrlBase = 'http://api.spreadshirt.' . self::$shopOptions['shop_source'] . '/api/v1/shops/' . self::$shopOptions['shop_id'];
// $apiUrlBase .= (!empty(self::$shopOptions['shop_category'])?'/articleCategories/'.self::$shopOptions['shop_category']:'');
$apiUrlBase .= '/designs?fullData=true&noCache=true';
// call first to get count of articles
$apiUrl = $apiUrlBase . '&limit=' . rand(2, 999); // randomize to avoid spreadshirt caching issues
$stringXmlShop = wp_remote_get($apiUrl, array (
'timeout' => 120
));
if (isset($stringXmlShop->errors) && count($stringXmlShop->errors) > 0)
die('Error getting articles. Please check Shop-ID, API and secret.');
if ($stringXmlShop['body'][0] != '<')
die($stringXmlShop['body']);
$stringXmlShop = wp_remote_retrieve_body($stringXmlShop);
// Quickfix for Namespace changes of Spreadshirt API