Repository navigation
Expand file tree
/
Copy pathvd-store.php
More file actions
998 lines (868 loc) · 36 KB
/
Copy pathvd-store.php
File metadata and controls
998 lines (868 loc) · 36 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
<?php
/**
* Plugin Name: VD Store
* Description: Plugin ecommerce VD Store berbasis REST API dan Alpine.js dengan pengaturan checkout, ongkir, dan pembayaran fleksibel.
* Version: 1.4.16
* Author: Dev Team Velocitydeveloper.com
* Author URI: https://velocitydeveloper.com
* Text Domain: vd-store
*/
if (!defined('ABSPATH')) {
exit;
}
define('WP_STORE_VERSION', '1.4.16');
define('WP_STORE_PATH', plugin_dir_path(__FILE__));
define('WP_STORE_URL', plugin_dir_url(__FILE__));
function wp_store_checkout_requires_login()
{
return (bool) apply_filters('wp_store_checkout_requires_login', false);
}
function wp_store_checkout_login_url($redirect_url = '')
{
$redirect_url = is_string($redirect_url) ? trim($redirect_url) : '';
if ($redirect_url === '') {
$request_uri = isset($_SERVER['REQUEST_URI']) ? wp_unslash((string) $_SERVER['REQUEST_URI']) : '/';
$redirect_url = home_url('/' . ltrim($request_uri, '/'));
}
$redirect_url = wp_validate_redirect($redirect_url, home_url('/'));
return (string) apply_filters('wp_store_checkout_login_url', wp_login_url($redirect_url), $redirect_url);
}
if (file_exists(WP_STORE_PATH . 'vendor/autoload.php')) {
require_once WP_STORE_PATH . 'vendor/autoload.php';
}
if (file_exists(WP_STORE_PATH . 'vendor/cmb2/cmb2/init.php')) {
require_once WP_STORE_PATH . 'vendor/cmb2/cmb2/init.php';
}
spl_autoload_register(function ($class) {
$prefix = 'WpStore\\';
$base_dir = WP_STORE_PATH . 'src/';
$len = strlen($prefix);
if (strncmp($prefix, $class, $len) !== 0) {
return;
}
$relative_class = substr($class, $len);
$file = $base_dir . str_replace('\\', '/', $relative_class) . '.php';
if (file_exists($file)) {
require $file;
}
});
function wp_store_init()
{
$should_migrate = get_option('wp_store_db_version') !== '1.4.5';
global $wpdb;
$table_name = $wpdb->prefix . 'store_carts';
$exists = $wpdb->get_var("SHOW TABLES LIKE '$table_name'") === $table_name;
if ($should_migrate || !$exists) {
require_once ABSPATH . 'wp-admin/includes/upgrade.php';
$charset_collate = $wpdb->get_charset_collate();
$sql = "CREATE TABLE {$table_name} (
id BIGINT(20) UNSIGNED NOT NULL AUTO_INCREMENT,
user_id BIGINT(20) UNSIGNED NULL DEFAULT NULL,
guest_key VARCHAR(64) NULL DEFAULT NULL,
cart LONGTEXT NOT NULL,
shipping_data LONGTEXT NULL DEFAULT NULL,
marketplace_snapshot LONGTEXT NULL DEFAULT NULL,
total_price DECIMAL(10,2) NULL DEFAULT NULL,
updated_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY (id),
UNIQUE KEY uniq_user (user_id),
UNIQUE KEY uniq_guest (guest_key)
) {$charset_collate};";
dbDelta($sql);
update_option('wp_store_db_version', '1.4.5');
}
// Create wishlist table
$wishlist_table = $wpdb->prefix . 'store_wishlists';
$wishlist_exists = $wpdb->get_var("SHOW TABLES LIKE '$wishlist_table'") === $wishlist_table;
if ($should_migrate || !$wishlist_exists) {
require_once ABSPATH . 'wp-admin/includes/upgrade.php';
$charset_collate = $wpdb->get_charset_collate();
$sql2 = "CREATE TABLE {$wishlist_table} (
id BIGINT(20) UNSIGNED NOT NULL AUTO_INCREMENT,
user_id BIGINT(20) UNSIGNED NULL DEFAULT NULL,
guest_key VARCHAR(64) NULL DEFAULT NULL,
wishlist LONGTEXT NOT NULL,
updated_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY (id),
UNIQUE KEY uniq_user (user_id),
UNIQUE KEY uniq_guest (guest_key)
) {$charset_collate};";
dbDelta($sql2);
}
$review_table = $wpdb->prefix . 'vmp_reviews';
$review_exists = $wpdb->get_var("SHOW TABLES LIKE '$review_table'") === $review_table;
if ($should_migrate || !$review_exists) {
require_once ABSPATH . 'wp-admin/includes/upgrade.php';
$charset_collate = $wpdb->get_charset_collate();
$sql3 = "CREATE TABLE {$review_table} (
id bigint(20) unsigned NOT NULL AUTO_INCREMENT,
product_id bigint(20) unsigned NOT NULL,
order_id bigint(20) unsigned NOT NULL,
user_id bigint(20) unsigned NOT NULL,
seller_id bigint(20) unsigned NOT NULL DEFAULT 0,
rating tinyint(3) unsigned NOT NULL DEFAULT 0,
title varchar(190) NOT NULL DEFAULT '',
content longtext NOT NULL,
image_ids text NULL,
is_approved tinyint(1) unsigned NOT NULL DEFAULT 1,
created_at datetime NOT NULL,
updated_at datetime NOT NULL,
PRIMARY KEY (id),
UNIQUE KEY vmp_review_unique (product_id,order_id,user_id),
KEY vmp_review_product (product_id),
KEY vmp_review_order (order_id),
KEY vmp_review_user (user_id),
KEY vmp_review_seller (seller_id),
KEY vmp_review_approved_created (is_approved,created_at)
) {$charset_collate};";
dbDelta($sql3);
}
$plugin = new \WpStore\Core\Plugin();
$plugin->run();
}
add_action('plugins_loaded', 'wp_store_init');
register_activation_hook(__FILE__, function () {
$post_types = new \WpStore\Core\PostTypes();
$post_types->register_product_type();
$post_types->register_order_type();
// Trigger update logic
delete_option('wp_store_db_version'); // Force update
wp_store_init();
flush_rewrite_rules();
});
function wps_icon_registry()
{
$icons = apply_filters('wp_store_icons', []);
if (!is_array($icons)) {
return [];
}
$registry = [];
foreach ($icons as $name => $definition) {
$key = sanitize_key((string) $name);
if ($key === '') {
continue;
}
if (is_string($definition)) {
$definition = ['html' => $definition];
} elseif (!is_array($definition)) {
continue;
}
$html = isset($definition['html']) ? (string) $definition['html'] : '';
$render = isset($definition['render']) && is_callable($definition['render']) ? $definition['render'] : null;
if ($html === '' && $render === null) {
continue;
}
$registry[$key] = [
'html' => $html,
'render' => $render,
'label' => isset($definition['label']) ? (string) $definition['label'] : '',
];
}
$registry = apply_filters('wp_store_icon_registry', $registry);
return is_array($registry) ? $registry : [];
}
function wps_icon_render_registered($args = [])
{
$args = is_array($args) ? $args : [];
$name = isset($args['name']) ? sanitize_key((string) $args['name']) : '';
if ($name === '') {
return '';
}
$registry = wps_icon_registry();
if (!isset($registry[$name])) {
return '';
}
$definition = $registry[$name];
if (!empty($definition['render']) && is_callable($definition['render'])) {
$html = call_user_func($definition['render'], $args, $definition);
} else {
$html = (string) ($definition['html'] ?? '');
}
if (!is_string($html) || trim($html) === '') {
return '';
}
return (string) apply_filters('wp_store_icon_html', $html, $name, $args, $definition);
}
function wps_icon($args = [])
{
if (is_string($args)) {
$args = ['name' => $args];
}
if (!is_array($args)) {
return '';
}
$data = [];
if (isset($args['name'])) {
$data['name'] = sanitize_key($args['name']);
}
if (isset($args['size'])) {
$data['size'] = (int) $args['size'];
}
if (isset($args['class']) && is_string($args['class'])) {
$data['class'] = $args['class'];
}
if (isset($args['stroke_color']) && is_string($args['stroke_color'])) {
$data['stroke_color'] = $args['stroke_color'];
} elseif (isset($args['stroke-color']) && is_string($args['stroke-color'])) {
$data['stroke_color'] = $args['stroke-color'];
} elseif (isset($args['stroke']) && is_string($args['stroke'])) {
$data['stroke_color'] = $args['stroke'];
} elseif (isset($args['color']) && is_string($args['color'])) {
$data['stroke_color'] = $args['color'];
} elseif (isset($args['border-color']) && is_string($args['border-color'])) {
$data['stroke_color'] = $args['border-color'];
} elseif (isset($args['border_color']) && is_string($args['border_color'])) {
$data['stroke_color'] = $args['border_color'];
}
if (isset($args['stroke_width'])) {
$data['stroke_width'] = (int) $args['stroke_width'];
} elseif (isset($args['stroke-width'])) {
$data['stroke_width'] = (int) $args['stroke-width'];
}
if (isset($args['fill_color']) && is_string($args['fill_color'])) {
$data['fill_color'] = $args['fill_color'];
} elseif (isset($args['fill-color']) && is_string($args['fill-color'])) {
$data['fill_color'] = $args['fill-color'];
} elseif (isset($args['fill']) && is_string($args['fill'])) {
$data['fill_color'] = $args['fill'];
}
$registered = wps_icon_render_registered($data);
if ($registered !== '') {
return $registered;
}
return \WpStore\Frontend\Template::render('components/icons', $data);
}
function wps_product_label_key($label)
{
$key = sanitize_key(str_replace([' ', '_'], '-', (string) $label));
return $key === '' ? '' : $key;
}
function wps_product_label_defaults()
{
return [
'best-seller' => [
'label' => 'Best Seller',
'class' => 'label-best',
'bg' => '#f59e0b',
'color' => '#ffffff',
],
'limited' => [
'label' => 'Limited',
'class' => 'label-limited',
'bg' => '#ef4444',
'color' => '#ffffff',
],
'pre-order' => [
'label' => 'Pre Order',
'class' => 'label-pre-order',
'bg' => '#8b5cf6',
'color' => '#ffffff',
],
'ready-stock' => [
'label' => 'Ready Stock',
'class' => 'label-ready-stock',
'bg' => '#059669',
'color' => '#ffffff',
],
'new' => [
'label' => 'New',
'class' => 'label-new',
'bg' => '#10b981',
'color' => '#ffffff',
],
'recommended' => [
'label' => 'Recommended',
'class' => 'label-recommended',
'bg' => '#2563eb',
'color' => '#ffffff',
],
'sale' => [
'label' => 'Sale',
'class' => 'label-sale',
'bg' => '#ef4444',
'color' => '#ffffff',
],
];
}
function wps_product_label_registry()
{
$labels = apply_filters('wp_store_product_labels', wps_product_label_defaults());
if (!is_array($labels)) {
$labels = [];
}
$registry = [];
foreach ($labels as $key => $definition) {
$key = wps_product_label_key($key);
if ($key === '') {
continue;
}
if (is_string($definition)) {
$definition = ['label' => $definition];
} elseif (!is_array($definition)) {
continue;
}
$label = isset($definition['label']) ? trim((string) $definition['label']) : '';
if ($label === '') {
$label = ucwords(str_replace('-', ' ', $key));
}
$class_tokens = [];
$class_value = isset($definition['class']) ? trim((string) $definition['class']) : '';
if ($class_value !== '') {
foreach (preg_split('/\s+/', $class_value) as $token) {
$token = sanitize_html_class($token);
if ($token !== '') {
$class_tokens[] = $token;
}
}
}
$registry[$key] = [
'key' => $key,
'label' => $label,
'class' => trim(implode(' ', array_unique(array_merge(['wps-label-badge'], $class_tokens)))),
'bg' => isset($definition['bg']) ? trim((string) $definition['bg']) : '',
'color' => isset($definition['color']) ? trim((string) $definition['color']) : '',
];
}
$registry = apply_filters('wp_store_product_labels_registry', $registry);
return is_array($registry) ? $registry : [];
}
function wps_product_label_options()
{
$options = [
'' => __('Tanpa Label', 'wp-store'),
];
foreach (wps_product_label_registry() as $key => $definition) {
$options[$key] = isset($definition['label']) ? (string) $definition['label'] : ucwords(str_replace('-', ' ', $key));
}
return apply_filters('wp_store_product_label_options', $options);
}
function wps_product_label_badge_html($product_id)
{
$product_id = (int) $product_id;
if ($product_id <= 0 || get_post_type($product_id) !== 'store_product') {
return '';
}
$label_key = \WpStore\Domain\Product\ProductMeta::label($product_id);
if ($label_key === '') {
return '';
}
$registry = wps_product_label_registry();
if (!isset($registry[$label_key])) {
return '';
}
$definition = $registry[$label_key];
$classes = isset($definition['class']) && is_string($definition['class']) ? trim($definition['class']) : 'wps-label-badge';
if ($classes === '') {
$classes = 'wps-label-badge';
}
$styles = [
'position:absolute',
'top:8px',
'right:8px',
'display:inline-flex',
'align-items:center',
'gap:4px',
'padding:2px 6px',
'border-radius:9999px',
'font-size:10px',
'line-height:12px',
'white-space:nowrap',
'z-index:2',
];
if (!empty($definition['bg'])) {
$styles[] = 'background:' . trim((string) $definition['bg']);
}
if (!empty($definition['color'])) {
$styles[] = 'color:' . trim((string) $definition['color']);
}
$html = '<span class="' . esc_attr($classes) . '" style="' . esc_attr(implode(';', $styles)) . '">';
$html .= '<span class="txt">' . esc_html((string) ($definition['label'] ?? '')) . '</span>';
$html .= '</span>';
$html = apply_filters('wp_store_product_label_badge_html', $html, $product_id, $label_key, $definition);
return is_string($html) ? $html : '';
}
function wps_label_badge_html($product_id)
{
return wps_product_label_badge_html($product_id);
}
function wps_discount_badge_html($product_id)
{
$settings = get_option('wp_store_settings', []);
if (!empty($settings['members_only_discount']) && !is_user_logged_in()) {
return '';
}
$price = \WpStore\Domain\Product\ProductData::resolve_regular_price((int) $product_id);
$sale = \WpStore\Domain\Product\ProductData::resolve_sale_price((int) $product_id);
if ($price !== null && $sale !== null && $price > 0 && $sale > 0 && $sale < $price) {
$percent = round((($price - $sale) / $price) * 100);
if ($percent <= 0) return '';
return '<span style="position:absolute;bottom:8px;right:8px;display:inline-flex;align-items:center;background:#ef4444;color:#fff;border-radius:9999px;padding:2px 6px;font-size:10px;line-height:12px;z-index:2;">' . esc_html($percent) . '%</span>';
}
return '';
}
function wps_product_price_html($product_id, $args = [])
{
$product_id = (int) $product_id;
if ($product_id <= 0 || get_post_type($product_id) !== 'store_product') {
return '';
}
$args = wp_parse_args(is_array($args) ? $args : [], [
'countdown' => false,
'currency' => '',
'wrapper' => null,
'tag' => '',
'class' => '',
'wrapper_tag' => 'div',
'sale_group_tag' => 'div',
'price_tag' => 'div',
'empty_tag' => 'div',
'wrapper_class' => 'wps-price',
'sale_group_class' => 'wps-flex wps-items-center wps-gap-2 wps-price-group',
'sale_class' => 'wps-text-lg wps-text-gray-900 wps-font-medium wps-price-text',
'regular_class' => 'wps-text-sm wps-text-gray-500',
'price_class' => 'wps-text-lg wps-text-gray-900 wps-font-medium wps-price-text',
'empty_class' => 'wps-text-sm wps-text-gray-500',
'empty_text' => '',
'show_empty' => true,
]);
$countdown_attr = $args['countdown'];
$currency = is_string($args['currency']) && $args['currency'] !== ''
? $args['currency']
: (get_option('wp_store_settings', [])['currency_symbol'] ?? 'Rp');
$product = \WpStore\Domain\Product\ProductData::map_post($product_id);
if ($product === null) {
return '';
}
$price = array_key_exists('regular_price', $product) ? $product['regular_price'] : \WpStore\Domain\Product\ProductData::resolve_regular_price($product_id);
$sale = array_key_exists('sale_price', $product) ? $product['sale_price'] : \WpStore\Domain\Product\ProductData::resolve_sale_price($product_id);
$want_countdown = is_bool($countdown_attr)
? $countdown_attr
: in_array(strtolower((string) $countdown_attr), ['1', 'true', 'yes'], true);
$until_raw = (string) \WpStore\Domain\Product\ProductMeta::get($product_id, 'sale_until', '');
$until_ts = $until_raw ? strtotime($until_raw) : 0;
$now_ts = current_time('timestamp');
$sale_active = $sale !== null
&& $sale > 0
&& (($price !== null && $sale < $price) || $price === null)
&& ($until_ts === 0 || $until_ts > $now_ts);
$settings = get_option('wp_store_settings', []);
if (!empty($settings['members_only_discount']) && !is_user_logged_in()) {
$sale_active = false;
}
if ($args['wrapper'] !== null) {
$args['wrapper_tag'] = in_array((string) $args['wrapper'], ['none', 'false', '0'], true) ? '' : (string) $args['wrapper'];
}
if (is_string($args['tag']) && $args['tag'] !== '') {
$args['price_tag'] = $args['tag'];
$args['empty_tag'] = $args['tag'];
}
if (is_string($args['class']) && $args['class'] !== '') {
$args['price_class'] = $args['class'];
$args['empty_class'] = $args['class'];
}
$allowed_tags = ['div', 'span', 'p'];
$wrapper_tag_raw = (string) $args['wrapper_tag'];
$wrapper_tag = $wrapper_tag_raw === '' ? '' : (in_array($wrapper_tag_raw, $allowed_tags, true) ? $wrapper_tag_raw : 'div');
$sale_group_tag = in_array((string) $args['sale_group_tag'], $allowed_tags, true) ? (string) $args['sale_group_tag'] : 'div';
$price_tag = in_array((string) $args['price_tag'], $allowed_tags, true) ? (string) $args['price_tag'] : 'div';
$empty_tag = in_array((string) $args['empty_tag'], $allowed_tags, true) ? (string) $args['empty_tag'] : 'div';
$wrapper_class = trim((string) $args['wrapper_class']);
$sale_group_class = trim((string) $args['sale_group_class']);
$sale_class = trim((string) $args['sale_class']);
$regular_class = trim((string) $args['regular_class']);
$price_class = trim((string) $args['price_class']);
$empty_class = trim((string) $args['empty_class']);
$empty_text = is_string($args['empty_text']) && $args['empty_text'] !== ''
? $args['empty_text']
: (string) apply_filters('wp_store_empty_price_text', 'Hubungi Admin', $product_id, $args);
$show_empty = (bool) $args['show_empty'];
$html = '';
if ($wrapper_tag !== '') {
$html .= '<' . tag_escape($wrapper_tag) . ($wrapper_class !== '' ? ' class="' . esc_attr($wrapper_class) . '"' : '') . '>';
}
if ($sale_active) {
$html .= '<' . tag_escape($sale_group_tag) . ($sale_group_class !== '' ? ' class="' . esc_attr($sale_group_class) . '"' : '') . '>';
$html .= '<span' . ($sale_class !== '' ? ' class="' . esc_attr($sale_class) . '"' : '') . '>' . esc_html(($currency ?: 'Rp') . ' ' . number_format((float) $sale, 0, ',', '.')) . '</span>';
if ($price !== null && (float) $price > 0) {
$html .= '<span' . ($regular_class !== '' ? ' class="' . esc_attr($regular_class) . '"' : '') . ' style="text-decoration: line-through;">' . esc_html(($currency ?: 'Rp') . ' ' . number_format((float) $price, 0, ',', '.')) . '</span>';
}
$html .= '</' . tag_escape($sale_group_tag) . '>';
} else {
if ($price !== null) {
$html .= '<' . tag_escape($price_tag) . ($price_class !== '' ? ' class="' . esc_attr($price_class) . '"' : '') . '>' . esc_html(($currency ?: 'Rp') . ' ' . number_format((float) $price, 0, ',', '.')) . '</' . tag_escape($price_tag) . '>';
} elseif ($show_empty) {
$html .= '<' . tag_escape($empty_tag) . ($empty_class !== '' ? ' class="' . esc_attr($empty_class) . '"' : '') . '>' . esc_html($empty_text) . '</' . tag_escape($empty_tag) . '>';
}
}
if ($want_countdown && $until_ts > $now_ts) {
wp_enqueue_script('alpinejs');
$end_js = esc_js($until_raw);
$html .= '<div class="wps-text-xs wps-text-gray-700 wps-mt-1" x-data="{ end: new Date(\'' . $end_js . '\'), d:0,h:0,m:0,s:0, tick(){ const diff = Math.max(0, this.end - new Date()); this.d = Math.floor(diff/86400000); this.h = Math.floor((diff%86400000)/3600000); this.m = Math.floor((diff%3600000)/60000); this.s = Math.floor((diff%60000)/1000); }, init(){ this.tick(); setInterval(()=>this.tick(), 1000); } } ?? {}" x-init="init">';
$html .= '<span>Berakhir dalam </span><span x-text="d"></span><span> hari </span><span x-text="h"></span><span> jam </span><span x-text="m"></span><span> menit </span><span x-text="s"></span><span> detik</span>';
$html .= '</div>';
}
if ($wrapper_tag !== '') {
$html .= '</' . tag_escape($wrapper_tag) . '>';
}
return $html;
}
function wp_store_add_to_cart_button($product_id, $args = [])
{
$product_id = (int) $product_id;
if ($product_id <= 0 || get_post_type($product_id) !== 'store_product') {
return '';
}
if (!shortcode_exists('wp_store_add_to_cart')) {
return '';
}
$args = wp_parse_args(is_array($args) ? $args : [], [
'text' => '',
'label' => '+',
'class' => 'wps-btn wps-btn-primary',
'qty' => 0,
]);
$shortcode_atts = [
'id' => $product_id,
'text' => (string) $args['text'],
'label' => (string) $args['label'],
'class' => (string) $args['class'],
'qty' => (string) $args['qty'],
];
$attr_html = '';
foreach ($shortcode_atts as $name => $value) {
$attr_html .= ' ' . $name . '="' . esc_attr((string) $value) . '"';
}
return do_shortcode('[wp_store_add_to_cart' . $attr_html . ']');
}
function wp_store_resolve_digital_file_url($product_id)
{
$product_id = (int) $product_id;
if ($product_id <= 0 || get_post_type($product_id) !== 'store_product') {
return '';
}
$file = get_post_meta($product_id, '_store_digital_file', true);
if (is_numeric($file)) {
$file = wp_get_attachment_url((int) $file);
}
$file = is_string($file) ? trim($file) : '';
if ($file === '') {
return '';
}
return esc_url_raw($file);
}
function wp_store_get_order_digital_downloads($order_id, $items = null)
{
$order_id = (int) $order_id;
if ($order_id <= 0 || get_post_type($order_id) !== 'store_order') {
return [];
}
if (!is_array($items)) {
$items = get_post_meta($order_id, '_store_order_items', true);
}
$items = is_array($items) ? $items : [];
$downloads = [];
$seen = [];
foreach ($items as $index => $item) {
if (!is_array($item)) {
continue;
}
$product_id = isset($item['product_id']) ? (int) $item['product_id'] : 0;
if ($product_id <= 0 || get_post_type($product_id) !== 'store_product') {
continue;
}
$is_digital = array_key_exists('is_digital', $item)
? !empty($item['is_digital'])
: (sanitize_key((string) get_post_meta($product_id, '_store_product_type', true)) === 'digital'
|| !empty(get_post_meta($product_id, '_store_is_digital', true))
|| (class_exists('\WpStore\Domain\Product\ProductData') && \WpStore\Domain\Product\ProductData::is_digital($product_id)));
if (!$is_digital) {
continue;
}
$url = '';
if (!empty($item['digital_file'])) {
$url = is_numeric($item['digital_file'])
? wp_get_attachment_url((int) $item['digital_file'])
: (string) $item['digital_file'];
} elseif (!empty($item['download_url'])) {
$url = (string) $item['download_url'];
}
if ($url === '') {
$url = wp_store_resolve_digital_file_url($product_id);
}
$url = esc_url_raw(trim((string) $url));
if ($url === '') {
continue;
}
$title = isset($item['title']) && $item['title'] !== ''
? sanitize_text_field((string) $item['title'])
: get_the_title($product_id);
$qty = isset($item['qty']) ? max(1, (int) $item['qty']) : 1;
$key = $product_id . '|' . md5($url);
if (isset($seen[$key])) {
continue;
}
$seen[$key] = true;
$downloads[] = [
'product_id' => $product_id,
'title' => $title,
'qty' => $qty,
'url' => $url,
];
}
return $downloads;
}
function wp_store_render_order_digital_downloads($order_id, $items = null, $args = [])
{
$downloads = wp_store_get_order_digital_downloads($order_id, $items);
if (empty($downloads)) {
return '';
}
$args = wp_parse_args(is_array($args) ? $args : [], [
'title' => __('Link File Digital', 'vd-store'),
'intro' => __('Klik tautan di bawah untuk mengunduh file digital milik pesanan Anda.', 'vd-store'),
]);
$html = '<div class="wps-card wps-p-4 wps-mt-4" style="border:1px solid #dbeafe;background:#eff6ff;">';
$html .= '<div class="wps-text-base wps-font-semibold wps-text-blue-900">' . esc_html((string) $args['title']) . '</div>';
if (!empty($args['intro'])) {
$html .= '<div class="wps-text-sm wps-text-blue-800 wps-mt-1">' . esc_html((string) $args['intro']) . '</div>';
}
$html .= '<div class="wps-mt-3" style="display:grid;gap:12px;">';
foreach ($downloads as $download) {
$title = esc_html((string) ($download['title'] ?? ''));
$qty = max(1, (int) ($download['qty'] ?? 1));
$url = esc_url((string) ($download['url'] ?? ''));
if ($url === '') {
continue;
}
$html .= '<div class="wps-flex wps-items-center wps-justify-between wps-gap-3 wps-p-3" style="border:1px solid #bfdbfe;background:#fff;border-radius:10px;">';
$html .= '<div>';
$html .= '<div class="wps-text-sm wps-font-medium wps-text-blue-900">' . $title . '</div>';
if ($qty > 1) {
$html .= '<div class="wps-text-xs wps-text-blue-700">' . esc_html(sprintf(__('Jumlah: %d', 'vd-store'), $qty)) . '</div>';
}
$html .= '</div>';
$html .= '<a class="wps-btn wps-btn-primary" href="' . $url . '" target="_blank" rel="noopener noreferrer">' . esc_html__('Unduh File', 'vd-store') . '</a>';
$html .= '</div>';
}
$html .= '</div></div>';
return $html;
}
function wp_store_render_product_card($product_id, $args = [])
{
return \WpStore\Domain\Product\ProductRenderer::render_card((int) $product_id, is_array($args) ? $args : []);
}
function wp_store_render_product_component($component, $product_id, $args = [])
{
return \WpStore\Domain\Product\ProductRenderer::render_component((string) $component, (int) $product_id, is_array($args) ? $args : []);
}
function wp_store_product_info($product_id, $args = [])
{
return \WpStore\Domain\Product\ProductRenderer::product_info((int) $product_id, is_array($args) ? $args : []);
}
function wp_store_render_single_product($product_id, $args = [])
{
return \WpStore\Domain\Product\ProductRenderer::render_single((int) $product_id, is_array($args) ? $args : []);
}
function wp_store_product_filter_args($base_args = [], $source = null)
{
$request = $source === null
? \WpStore\Domain\Product\ProductFilterRequest::from_globals()
: \WpStore\Domain\Product\ProductFilterRequest::from_source($source);
return \WpStore\Domain\Product\ProductQuery::apply_to_args(is_array($base_args) ? $base_args : [], $request);
}
function wp_store_courier_labels()
{
return [
'jne' => 'JNE',
'pos' => 'POS Indonesia',
'sicepat' => 'SiCepat',
'jnt' => 'J&T',
'ninja' => 'Ninja Xpress',
'wahana' => 'Wahana',
'lion' => 'Lion Parcel',
'sap' => 'SAP Express',
'rex' => 'REX',
'ide' => 'IDExpress',
'sentral' => 'Sentral Cargo',
];
}
// Kurir yang dimatikan: API ongkir TIKI gagal lambat (±10 dtk) dan ikut mengosongkan kurir lain di checkout.
function wp_store_disabled_couriers()
{
return (array) apply_filters('wp_store_disabled_couriers', ['tiki']);
}
function wp_store_active_couriers($default = ['jne', 'sicepat', 'ide'])
{
$settings = get_option('wp_store_settings', []);
$couriers = isset($settings['shipping_couriers']) && is_array($settings['shipping_couriers']) ? $settings['shipping_couriers'] : (array) $default;
return array_values(array_diff(array_map('strval', $couriers), wp_store_disabled_couriers()));
}
function wp_store_admin_whatsapp_url($product_id = 0)
{
$settings = get_option('wp_store_settings', []);
$number = preg_replace('/\D+/', '', (string) ($settings['store_wa'] ?? ''));
if ($number === '') {
return '';
}
if (substr($number, 0, 1) === '0') {
$number = '62' . substr($number, 1);
} elseif (substr($number, 0, 1) === '8') {
$number = '62' . $number;
}
$product_id = (int) $product_id;
$message = 'Halo ' . wp_specialchars_decode(get_bloginfo('name'), ENT_QUOTES);
if ($product_id > 0) {
$message .= ', saya ingin menanyakan produk ' . wp_specialchars_decode(get_the_title($product_id), ENT_QUOTES) . ' ' . get_permalink($product_id);
}
$message = (string) apply_filters('wp_store_admin_whatsapp_message', $message, $product_id);
return (string) apply_filters('wp_store_admin_whatsapp_url', 'https://wa.me/' . $number . '?text=' . rawurlencode($message), $product_id, $number);
}
function wp_store_shipping_disabled()
{
$settings = get_option('wp_store_settings', []);
$mode = isset($settings['shipping_mode']) ? sanitize_key((string) $settings['shipping_mode']) : '';
if ($mode === '') {
$mode = !empty($settings['disable_shipping']) ? 'off' : 'normal';
}
$disabled = $mode === 'off';
return (bool) apply_filters('wp_store_shipping_disabled', $disabled, is_array($settings) ? $settings : []);
}
function wp_store_shipping_mode()
{
$settings = get_option('wp_store_settings', []);
$mode = isset($settings['shipping_mode']) ? sanitize_key((string) $settings['shipping_mode']) : '';
if (!in_array($mode, ['normal', 'free', 'off'], true)) {
$mode = !empty($settings['disable_shipping']) ? 'off' : 'normal';
}
return (string) apply_filters('wp_store_shipping_mode', $mode, is_array($settings) ? $settings : []);
}
function wp_store_shipping_collect_address()
{
$settings = get_option('wp_store_settings', []);
$collect = !isset($settings['collect_address']) || (string) $settings['collect_address'] !== '0';
return (bool) apply_filters('wp_store_shipping_collect_address', $collect, is_array($settings) ? $settings : []);
}
function wp_store_shipping_allow_cod()
{
$settings = get_option('wp_store_settings', []);
$allow = !isset($settings['allow_cod']) || (string) $settings['allow_cod'] !== '0';
if (wp_store_shipping_mode() === 'off') {
$allow = false;
}
return (bool) apply_filters('wp_store_shipping_allow_cod', $allow, is_array($settings) ? $settings : []);
}
function wp_store_bank_labels()
{
return [
'mandiri' => 'Bank Mandiri',
'bri' => 'BRI',
'bca' => 'BCA',
'bca_syariah' => 'BCA Syariah',
'bni' => 'BNI',
'btn' => 'BTN',
'bsi' => 'BSI',
'cimb_niaga' => 'CIMB Niaga',
'ocbc_nisp' => 'OCBC NISP',
'permata' => 'Bank Permata',
'danamon' => 'Bank Danamon',
'panin' => 'Panin Bank',
'maybank' => 'Maybank Indonesia',
'mega' => 'Bank Mega',
'muamalat' => 'Bank Muamalat',
'sinarmas' => 'Bank Sinarmas',
'bsn' => 'BSN',
'mega_syariah' => 'Bank Mega Syariah',
'commonwealth' => 'Bank Commonwealth',
'uob' => 'Bank UOB Indonesia',
'dbs' => 'Bank DBS Indonesia',
'bws' => 'Bank Woori Saudara',
'hana' => 'Bank Hana Indonesia',
'resona' => 'Bank Resona Perdania',
'j_trust' => 'Bank J Trust Indonesia',
'ina' => 'Bank Ina Perdana',
'artha' => 'Bank Artha Graha',
'index' => 'Bank Index Selindo',
'ganesha' => 'Bank Ganesha',
'maspion' => 'Bank Maspion',
'bumi_arta' => 'Bank Bumi Arta',
'victoria' => 'Bank Victoria',
'jago' => 'Bank Jago',
'allo' => 'Allo Bank',
'smbc' => 'Bank SMBC Indonesia',
'seabank' => 'SeaBank',
'neocommerce' => 'Bank Neo Commerce',
'hsbc' => 'HSBC Indonesia',
'bpd_diy' => 'Bank BPD DIY',
'bank_jateng' => 'Bank Jateng',
'bank_jatim' => 'Bank Jatim',
'bjb' => 'Bank BJB',
'lainnya' => 'Lainnya',
];
}
function wp_store_bank_logo($bank_name)
{
$bank_name = (string) $bank_name;
$labels = function_exists('wp_store_bank_labels') ? wp_store_bank_labels() : [];
$key = array_search($bank_name, $labels, true);
if ($key === false || $key === 'lainnya') {
return '';
}
$file = $key . '.gif';
if (!file_exists(WP_STORE_PATH . 'assets/frontend/img/bank/' . $file)) {
return '';
}
return WP_STORE_URL . 'assets/frontend/img/bank/' . $file;
}
function wp_store_bank_account_name($account)
{
if (!is_array($account)) {
return '';
}
$bank_name = isset($account['bank_name']) ? (string) $account['bank_name'] : '';
if ($bank_name === 'Lainnya' && !empty($account['custom_bank_name'])) {
return (string) $account['custom_bank_name'];
}
return $bank_name;
}
function wp_store_bank_account_logo($account)
{
if (!is_array($account)) {
return '';
}
$is_custom_bank = isset($account['bank_name']) && (string) $account['bank_name'] === 'Lainnya';
$custom_logo_id = $is_custom_bank && isset($account['bank_logo_id']) ? absint($account['bank_logo_id']) : 0;
if ($custom_logo_id > 0) {
$custom_logo_url = wp_get_attachment_image_url($custom_logo_id, 'medium');
if ($custom_logo_url) {
return $custom_logo_url;
}
}
return wp_store_bank_logo(wp_store_bank_account_name($account));
}
function wp_store_bank_logo_placeholder($bank_name = '', $centered = false)
{
$margin = $centered ? '0 auto 6px' : '0 0 8px';
return '<span role="img" aria-label="' . esc_attr(sprintf('Logo %s belum tersedia', (string) $bank_name)) . '" style="display:flex; align-items:center; justify-content:center; gap:6px; width:120px; height:40px; margin:' . esc_attr($margin) . '; border:1px solid #e5e7eb; border-radius:6px; box-sizing:border-box; background:#f8fafc; color:#64748b;">'
. '<svg aria-hidden="true" width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.8" stroke-linecap="round" stroke-linejoin="round"><path d="M3 10h18"/><path d="M5 10v8"/><path d="M9 10v8"/><path d="M15 10v8"/><path d="M19 10v8"/><path d="M3 18h18"/><path d="M12 3 3 8h18l-9-5Z"/></svg>'
. '<span style="font-size:11px; font-weight:700; letter-spacing:.08em; line-height:1;">BANK</span>'
. '</span>';
}
function wp_store_order_status_labels()
{
return [
'pending' => 'Pending',
'awaiting_payment' => 'Menunggu Pembayaran',
'paid' => 'Sudah Dibayar',
'processing' => 'Sedang Diproses',
'shipped' => 'Dikirim',
'completed' => 'Selesai',
'cancelled' => 'Dibatalkan',
'refunded' => 'Dikembalikan',
];
}
function wp_store_order_statuses()
{
return function_exists('wp_store_order_status_labels')
? array_keys(wp_store_order_status_labels())
: '';
}