-
Notifications
You must be signed in to change notification settings - Fork 13
/
tinv-wishlists-function.php
1148 lines (984 loc) · 32.5 KB
/
tinv-wishlists-function.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
/**
* Basic function for plugin
*
* @since 1.0.0
* @package TInvWishlist
*/
// If this file is called directly, abort.
if ( ! defined( 'ABSPATH' ) ) {
die;
}
if ( ! function_exists( 'tinv_get_option' ) ) {
/**
* Extract options from database or default array settings.
*
* @param string $category Name category settings.
* @param string $option Name paremetr. If is empty string, then function return array category settings.
*
* @return mixed
*/
function tinv_get_option( $category, $option = '' ) {
$prefix = TINVWL_PREFIX . '-';
$values = get_option( $prefix . $category, array() );
if ( empty( $values ) ) {
$values = tinv_get_option_defaults( $category );
}
if ( empty( $option ) ) {
return $values;
} else {
if ( array_key_exists( $option, (array) $values ) ) {
return $values[ $option ];
} else {
$values = tinv_get_option_defaults( $category );
if ( array_key_exists( $option, (array) $values ) ) {
return $values[ $option ];
}
}
}
return null;
}
}
if ( ! function_exists( 'tinv_get_option_admin' ) ) {
/**
* Extract options from database or default array settings.
*
* @param string $category Name category settings.
* @param string $option Name paremetr. If is empty string, then function return array category settings.
*
* @return mixed
*/
function tinv_get_option_admin( $category, $option = '' ) {
$prefix = TINVWL_PREFIX . '-';
$values = get_option( $prefix . $category, array() );
if ( empty( $values ) ) {
$values = array();
}
if ( empty( $option ) ) {
return $values;
} elseif ( array_key_exists( $option, $values ) ) {
return $values[ $option ];
}
return null;
}
}
if ( ! function_exists( 'tinv_style' ) ) {
/**
* Get style for custom style
*
* @param string $selector Selector style.
* @param string $element Attribute name.
*
* @return string
*/
function tinv_style( $selector = '', $element = '' ) {
$key = md5( $selector . '||' . $element );
$values = get_option( TINVWL_PREFIX . '-style_options', array() );
if ( empty( $values ) ) {
return '';
}
if ( array_key_exists( $key, $values ) ) {
return $values[ $key ];
}
return '';
}
}
if ( ! function_exists( 'tinv_update_option' ) ) {
/**
* Update options in database.
*
* @param string $category Name category settings.
* @param string $option Name paremetr. If is empty string, then function update array category settings.
* @param mixed $value Value option.
*
* @return boolean
*/
function tinv_update_option( $category, $option = '', $value = false ) {
$prefix = TINVWL_PREFIX . '-';
if ( empty( $option ) ) {
if ( is_array( $value ) ) {
update_option( $prefix . $category, $value );
return true;
}
} else {
$values = get_option( $prefix . $category, array() );
$values[ $option ] = $value;
update_option( $prefix . $category, $values );
return true;
}
return false;
}
}
/**
* Handles the dynamic renaming of "wishlist" terminology across the plugin.
*
* @since 1.x.x
* @package TInvWishlist
*/
/**
* Manages dynamic renaming of "wishlist" based on settings.
*/
class TInvWLRename {
/**
* Whether renaming is enabled.
*
* @var bool
*/
private $rename;
/**
* The singular form of the custom name for "wishlist".
*
* @var string
*/
private $rename_single;
/**
* The plural form of the custom name for "wishlist".
*
* @var string
*/
private $rename_plural;
/**
* Sets up the renaming functionality by fetching options and adding filters.
*/
public function __construct() {
$this->rename = tinv_get_option( 'rename', 'rename' );
$this->rename_single = tinv_get_option( 'rename', 'rename_single' );
$this->rename_plural = tinv_get_option( 'rename', 'rename_plural' );
if ( $this->rename && $this->rename_single ) {
add_filter( 'gettext', [ $this, 'translations' ], 999, 3 );
add_filter( 'ngettext', [ $this, 'translations_n' ], 999, 5 );
}
}
/**
* Handles plural translations.
*
* @param string $translation The translated text.
* @param string $single The singular form of the text.
* @param string $plural The plural form of the text.
* @param int $number The number to compare to decide if singular or plural.
* @param string $domain The text-domain.
*
* @return string The potentially modified translation.
*/
public function translations_n( $translation, $single, $plural, $number, $domain ) {
return $this->translation_update( $translation, $domain );
}
/**
* Handles singular translations.
*
* @param string $translation The translated text.
* @param string $text The text to translate.
* @param string $domain The text-domain.
*
* @return string The potentially modified translation.
*/
public function translations( $translation, $text, $domain ) {
return $this->translation_update( $translation, $domain );
}
/**
* Updates translations based on renaming settings.
*
* @param string $text The text to potentially modify.
* @param string $domain The text-domain.
*
* @return string The potentially modified text.
*/
private function translation_update( $text, $domain ) {
if ( 'ti-woocommerce-wishlist' === $domain ) {
if ( strpos( $text, '{wishlist_title}' ) !== false ) {
return $text; // Skip replacement for special placeholders.
}
$translations = [
'wishlist' => [
$this->rename_single,
$this->rename_plural ?: $this->rename_single . 's'
]
];
$text = preg_replace_callback( '~\b[a-z]+(?:(?<=(s)))?~i', function ( $matches ) use ( $translations ) {
return $this->replaceText( $matches, $translations );
}, $text );
}
return $text;
}
/**
* Replaces text based on matched patterns and translations.
*
* @param array $matches Matches from the regular expression.
* @param array $translations Translations array.
*
* @return string Replaced text.
*/
private function replaceText( $matches, $translations ) {
$lower = strtolower( $matches[0] );
$rep = $matches[0];
if ( isset( $translations[ $lower ] ) ) {
$rep = is_array( $translations[ $lower ] ) ? $translations[ $lower ][0] : $translations[ $lower ];
} elseif ( isset( $matches[1] ) ) {
$sing = substr( $lower, 0, - 1 );
if ( isset( $translations[ $sing ] ) ) {
$rep = is_array( $translations[ $sing ] ) ? $translations[ $sing ][1] : $translations[ $sing ] . 's';
}
} else {
return $rep;
}
if ( $matches[0] == $lower ) {
return $rep;
} elseif ( $matches[0] == strtoupper( $lower ) ) {
return strtoupper( $rep );
} elseif ( $matches[0] == ucfirst( $lower ) ) {
return ucfirst( $rep );
}
return $rep;
}
}
$tinvwl_rename = new TInvWLRename();
if ( ! function_exists( 'tinv_wishlist_template' ) ) {
/**
* The function overwrites the method output templates woocommerce
*
* @param string $template_name Name file template.
* @param array $args Array variable in template.
* @param string $template_path Customization path.
*/
function tinv_wishlist_template( $template_name, $args = array(), $template_path = '' ) {
if ( function_exists( 'wc_get_template' ) ) {
wc_get_template( $template_name, $args, $template_path );
} else {
woocommerce_get_template( $template_name, $args, $template_path );
}
}
}
if ( ! function_exists( 'tinv_wishlist_locate_template' ) ) {
/**
* Overwrites path for email and other template
*
* @param string $template_name Requered Template file.
* @param string $template_path Template path.
* @param string $default_path Template default path.
*
* @return mixed
*/
function tinv_wishlist_locate_template( $template_name, $template_path = '', $default_path = '' ) {
$prefix = 'ti-';
if ( substr( basename( $template_name ), 0, strlen( $prefix ) ) !== $prefix ) {
return;
}
if ( ! $template_path ) {
$template_path = WC()->template_path();
}
if ( ! $default_path ) {
$default_path = TINVWL_PATH . DIRECTORY_SEPARATOR . 'templates' . DIRECTORY_SEPARATOR;
}
// Look within passed path within the theme - this is priority.
$template = locate_template( array(
trailingslashit( $template_path ) . $template_name,
$template_name,
) );
// Get default template.
if ( ! $template && file_exists( $default_path . $template_name ) ) {
$template = $default_path . $template_name;
}
// Return what we found.
return apply_filters( 'tinvwl_locate_template', $template, $template_name, $template_path );
}
} // End if().
if ( ! function_exists( 'tinv_wishlist_template_html' ) ) {
/**
* The function overwrites the method return templates woocommerce
*
* @param string $template_name Name file template.
* @param array $args Array variable in template.
* @param string $template_path Customization path.
*
* @return string
*/
function tinv_wishlist_template_html( $template_name, $args = array(), $template_path = '' ) {
ob_start();
tinv_wishlist_template( $template_name, $args, $template_path );
return ob_get_clean();
}
}
if ( ! function_exists( 'tinv_wishlist_get_item_data' ) ) {
/**
* Extract meta attributes for product
*
* @param object $product Object selected product.
* @param array $wl_product Wishlist selected product.
* @param boolean $flat Return text or template.
*
* @return string
*/
function tinv_wishlist_get_item_data( $product, $wl_product = array(), $flat = false ) {
$item_data = array();
$variation_id = $product->is_type( 'variation' ) ? $product->get_id() : 0;
$variation_data = $product->is_type( 'variation' ) ? wc_get_product_variation_attributes( $product->get_id() ) : array();
if ( ! empty( $variation_id ) && is_array( $variation_data ) && is_array( $wl_product ) ) {
foreach ( $variation_data as $name => $value ) {
if ( '' === $value ) {
// Could be any value that saved to a custom meta.
if ( array_key_exists( 'meta', $wl_product ) && array_key_exists( $name, $wl_product['meta'] ) ) {
$value = $wl_product['meta'][ $name ];
} else {
continue;
}
}
$taxonomy = wc_attribute_taxonomy_name( str_replace( 'attribute_pa_', '', urldecode( $name ) ) );
// If this is a term slug, get the term's nice name.
if ( taxonomy_exists( $taxonomy ) ) {
$term = get_term_by( 'slug', $value, $taxonomy ); // @codingStandardsIgnoreLine WordPress.VIP.RestrictedFunctions.get_term_by
if ( ! is_wp_error( $term ) && $term && $term->name ) {
$value = $term->name;
}
$label = wc_attribute_label( $taxonomy );
// If this is a custom option slug, get the options name.
} else {
$value = apply_filters( 'woocommerce_variation_option_name', $value );
$product_attributes = $product->get_attributes();
$_name = str_replace( 'attribute_', '', $name );
if ( isset( $product_attributes[ $_name ] ) ) {
$label = wc_attribute_label( $_name, $product );
} else {
$label = $name;
}
}
if ( '' === $value || wc_is_attribute_in_product_name( $value, is_callable( array(
$product,
'get_name'
) ) ? $product->get_name() : $product->get_title() ) ) {
continue;
}
$item_data[] = array(
'key' => $label,
'value' => $value,
);
} // End foreach().
} // End if().
// Filter item data to allow 3rd parties to add more to the array.
$item_data = apply_filters( 'tinvwl_wishlist_get_item_data', $item_data, $product );
// Format item data ready to display.
foreach ( $item_data as $key => $data ) {
// Set hidden to true to not display meta on cart.
if ( ! empty( $data['hidden'] ) ) {
unset( $item_data[ $key ] );
continue;
}
$item_data[ $key ]['key'] = ! empty( $data['key'] ) ? $data['key'] : $data['name'];
$item_data[ $key ]['display'] = ! empty( $data['display'] ) ? $data['display'] : $data['value'];
}
// Output flat or in list format.
if ( 0 < count( $item_data ) ) {
ob_start();
if ( $flat ) {
foreach ( $item_data as $data ) {
echo esc_html( $data['key'] ) . ': ' . wp_kses_post( $data['display'] ) . '<br>';
}
} else {
tinv_wishlist_template( 'ti-wishlist-item-data.php', array( 'item_data' => $item_data ) );
}
return ob_get_clean();
}
return '';
}
} // End if().
if ( ! function_exists( 'tinv_wishlist_get' ) ) {
/**
* Return Wishlist by id or share key
*
* @param mixed $id Integer wishlist ID, or Share Key wishlist.
* @param boolean $toend Switches to the extract the default or guest wishlist.
*
* @return array
*/
function tinv_wishlist_get( $id = '', $toend = true ) {
$wl = new TInvWL_Wishlist();
$wishlist = null;
if ( empty( $id ) ) {
$id = get_query_var( 'tinvwlID', null );
}
if ( ! empty( $id ) ) {
if ( is_integer( $id ) ) {
$wishlist = $wl->get_by_id( $id );
}
if ( empty( $wishlist ) ) {
$wishlist = $wl->get_by_share_key( $id );
}
if ( is_array( $wishlist ) ) {
$wishlist['is_owner'] = false;
if ( is_user_logged_in() ) {
$wishlist['is_owner'] = get_current_user_id() == $wishlist['author']; // WPCS: loose comparison ok.
} else {
$wishlist['is_owner'] = $wl->get_sharekey() === $wishlist['share_key']; // WPCS: loose comparison ok.
}
}
} elseif ( is_user_logged_in() && $toend ) {
$wishlist = $wl->add_user_default();
$wishlist['is_owner'] = true;
} elseif ( $toend ) {
$wishlist = $wl->get_by_sharekey_default();
if ( ! empty( $wishlist ) ) {
$wishlist = array_shift( $wishlist );
$wishlist['is_owner'] = $wl->get_sharekey() === $wishlist['share_key'];
}
}
return $wishlist;
}
} // End if().
if ( ! function_exists( 'tinv_url_wishlist_default' ) ) {
/**
* Return the default wishlist url
*
* @return string
*/
function tinv_url_wishlist_default() {
if ( tinv_get_option( 'general', 'my_account_endpoint' ) ) {
return esc_url( wc_get_endpoint_url( tinv_get_option( 'general', 'my_account_endpoint_slug' ), '', wc_get_page_permalink( 'myaccount' ) ) );
}
$page = apply_filters( 'wpml_object_id', tinv_get_option( 'page', 'wishlist' ), 'page', true ); // @codingStandardsIgnoreLine WordPress.Variables.GlobalVariables.OverrideProhibited
if ( empty( $page ) ) {
return '';
}
$link = get_permalink( $page );
return $link;
}
}
if ( ! function_exists( 'tinv_url_wishlist_by_key' ) ) {
/**
* Return the wishlist url by share key
*
* @param string $share_key Share Key wishlist.
* @param integer $paged Page.
*
* @return string
*/
function tinv_url_wishlist_by_key( $share_key, $paged = 1 ) {
$paged = absint( $paged );
$paged = 1 < $paged ? $paged : 1;
$link = tinv_url_wishlist_default();
if ( empty( $link ) ) {
return $link;
}
if ( 1 < $paged ) {
$link = add_query_arg( 'wl_paged', $paged, $link );
}
if ( $share_key ) {
if ( get_option( 'permalink_structure' ) ) {
$suffix = '';
if ( preg_match( '/([^\?]+)\?*?(.*)/i', $link, $_link ) ) {
$link = $_link[1];
$suffix = $_link[2];
}
if ( ! preg_match( '/\/$/', $link ) ) {
$link .= '/';
}
$link .= $share_key . '/' . $suffix;
} else {
$link = add_query_arg( 'tinvwlID', $share_key, $link );
}
}
return $link;
}
} // End if().
if ( ! function_exists( 'tinv_url_wishlist' ) ) {
/**
* Return the wishlist url by id or share key
*
* @param mixed $id Integer wishlist ID, or Share Key wishlist.
* @param integer $paged Page.
* @param boolean $full Return full url or shroted url for logged in user.
*
* @return string
*/
function tinv_url_wishlist( $id = '', $paged = 1, $full = true ) {
$share_key = $id;
if ( ! ( is_string( $id ) && preg_match( '/^[A-Fa-f0-9]{6}$/', $id ) ) ) {
$wishlist = tinv_wishlist_get( $id, false );
$share_key = isset( $wishlist['share_key'] ) ? $wishlist['share_key'] : '';
}
return tinv_url_wishlist_by_key( $share_key, $paged );
}
}
if ( ! function_exists( 'tinv_wishlist_status' ) ) {
/**
* Check status free or premium plugin and disable free
*
* @param string $transient Plugin transient name.
*
* @return string
* @global string $s
*
* @global string $status
* @global string $page
*/
function tinv_wishlist_status( $transient ) {
if ( TINVWL_LOAD_FREE === $transient ) {
TInvWL_PluginExtend::deactivate_self( TINVWL_LOAD_FREE );
return 'plugins.php';
}
if ( defined( 'TINVWL_LOAD_PREMIUM' ) && TINVWL_LOAD_PREMIUM === $transient ) {
if ( is_plugin_active( TINVWL_LOAD_FREE ) ) {
TInvWL_PluginExtend::deactivate_self( TINVWL_LOAD_FREE );
if ( ! function_exists( 'wp_create_nonce' ) ) {
return 'plugins.php';
}
global $status, $page, $s;
$redirect = 'plugins.php?';
$redirect .= http_build_query( array(
'action' => 'activate',
'plugin' => $transient,
'plugin_status' => $status,
'paged' => $page,
's' => $s,
) );
$redirect = esc_url_raw( add_query_arg( '_wpnonce', wp_create_nonce( 'activate-plugin_' . $transient ), $redirect ) );
return $redirect;
}
}
return false;
}
} // End if().
if ( ! function_exists( 'tinvwl_body_classes' ) ) {
/**
* Add custom class
*
* @param array $classes Current classes.
*
* @return array
*/
function tinvwl_body_classes( $classes ) {
if ( tinv_get_option( 'style', 'customstyle' ) ) {
$classes[] = 'tinvwl-theme-style';
} else {
$classes[] = 'tinvwl-custom-style';
}
return $classes;
}
add_filter( 'body_class', 'tinvwl_body_classes' );
}
if ( ! function_exists( 'tinvwl_shortcode_addtowishlist' ) ) {
/**
* Shortcode Add To Wishlist
*
* @param array $atts Array parameter from shortcode.
*
* @return string
*/
function tinvwl_shortcode_addtowishlist( $atts = array() ) {
$class = TInvWL_Public_AddToWishlist::instance();
return $class->shortcode( $atts );
}
add_shortcode( 'ti_wishlists_addtowishlist', 'tinvwl_shortcode_addtowishlist' );
}
if ( ! function_exists( 'tinvwl_shortcode_view' ) ) {
/**
* Shortcode view Wishlist
*
* @param array $atts Array parameter from shortcode.
*
* @return string
*/
function tinvwl_shortcode_view( $atts = array() ) {
$class = TInvWL_Public_Wishlist_View::instance();
return $class->shortcode( $atts );
}
add_shortcode( 'ti_wishlistsview', 'tinvwl_shortcode_view' );
}
if ( ! function_exists( 'tinvwl_shortcode_products_counter' ) ) {
/**
* Shortcode view Wishlist
*
* @param array $atts Array parameter from shortcode.
*
* @return string
*/
function tinvwl_shortcode_products_counter( $atts = array() ) {
$class = TInvWL_Public_WishlistCounter::instance();
return $class->shortcode( $atts );
}
add_shortcode( 'ti_wishlist_products_counter', 'tinvwl_shortcode_products_counter' );
}
if ( ! function_exists( 'tinvwl_view_addto_html' ) ) {
/**
* Show button Add to Wishlsit
*/
function tinvwl_view_addto_html() {
$class = TInvWL_Public_AddToWishlist::instance();
$class->htmloutput();
}
}
if ( ! function_exists( 'tinvwl_view_addto_htmlout' ) ) {
/**
* Show button Add to Wishlsit, if product is not purchasable
*/
function tinvwl_view_addto_htmlout() {
$class = TInvWL_Public_AddToWishlist::instance();
$class->htmloutput_out();
}
}
if ( ! function_exists( 'tinvwl_view_addto_htmlloop' ) ) {
/**
* Show button Add to Wishlsit, in loop
*/
function tinvwl_view_addto_htmlloop() {
$class = TInvWL_Public_AddToWishlist::instance();
$class->htmloutput_loop();
}
}
if ( ! function_exists( 'tinvwl_clean_url' ) ) {
/**
* Clear esc_url to original
*
* @param string $good_protocol_url Cleared URL.
* @param string $original_url Original URL.
*
* @return string
*/
function tinvwl_clean_url( $good_protocol_url, $original_url ) {
return $original_url;
}
}
if ( ! function_exists( 'tinvwl_add_to_cart_need_redirect' ) ) {
/**
* Check if the product is third-party, or has another link added to the cart then redirect to the product page.
*
* @param boolean $redirect Default value to redirect.
* @param \WC_Product $_product Product data.
* @param string $redirect_url Current url for redirect.
*
* @return boolean
*/
function tinvwl_add_to_cart_need_redirect( $redirect, $_product, $redirect_url ) {
if ( $redirect ) {
return true;
}
if ( 'external' === $_product->get_type() ) {
return true;
}
$need_url_data = array_merge( array(
'variation_id' => $_product->is_type( 'variation' ) ? $_product->get_id() : 0,
'add-to-cart' => $_product->is_type( 'variation' ) ? $_product->get_parent_id() : $_product->get_id(),
), array_map( 'urlencode', array() ) );
$need_url_data = array_filter( $need_url_data );
$need_url = apply_filters( 'tinvwl_product_add_to_cart_redirect_slug_original', remove_query_arg( 'added-to-cart', ( version_compare( WC_VERSION, '3.8.0', '<' ) ? add_query_arg( $need_url_data ) : add_query_arg( $need_url_data, '' ) ) ), $_product );
$need_url_full = apply_filters( 'tinvwl_product_add_to_cart_redirect_url_original', remove_query_arg( 'added-to-cart', add_query_arg( $need_url_data, $_product->get_permalink() ) ), $_product );
global $product;
// store global product data.
$_product_tmp = $product;
// override global product data.
$product = $_product;
add_filter( 'clean_url', 'tinvwl_clean_url', 10, 2 );
do_action( 'before_get_redirect_url' );
$_redirect_url = apply_filters( 'tinvwl_product_add_to_cart_redirect_url', $_product->add_to_cart_url(), $_product );
do_action( 'after_get_redirect_url' );
remove_filter( 'clean_url', 'tinvwl_clean_url', 10 );
// restore global product data.
$product = $_product_tmp;
if ( $_redirect_url !== $need_url && $_redirect_url !== $need_url_full ) {
return true;
}
return $redirect;
}
add_filter( 'tinvwl_product_add_to_cart_need_redirect', 'tinvwl_add_to_cart_need_redirect', 10, 3 );
} // End if().
if ( ! function_exists( 'tinvwl_meta_validate_cart_add' ) ) {
/**
* Checks the ability to add a product
*
* @param boolean $redirect Default value to redirect.
* @param \WC_Product $product Product data.
* @param string $redirect_url Current url for redirect.
* @param array $wl_product Wishlist Product.
*
* @return boolean
*/
function tinvwl_meta_validate_cart_add( $redirect, $product, $redirect_url, $wl_product ) {
if ( $redirect && array_key_exists( 'meta', $wl_product ) && ! empty( $wl_product['meta'] ) ) {
$wl_product = apply_filters( 'tinvwl_addproduct_tocart', $wl_product );
TInvWL_Public_Cart::prepare_post( $wl_product );
$product_id = apply_filters( 'woocommerce_add_to_cart_product_id', absint( $wl_product['product_id'] ) );
$quantity = empty( $wl_product['quantity'] ) ? 1 : wc_stock_amount( $wl_product['quantity'] );
$variation_id = $wl_product['variation_id'];
$variations = $product->is_type( 'variation' ) ? wc_get_product_variation_attributes( $product->get_id() ) : array();
$passed_validation = $product->is_purchasable() && ( $product->is_in_stock() || $product->backorders_allowed() ) && 'external' !== $product->get_type();
ob_start();
if ( function_exists( 'wc_clear_notices' ) ) {
wc_clear_notices();
}
$passed_validation = apply_filters( 'woocommerce_add_to_cart_validation', $passed_validation, $product_id, $quantity, $variation_id, $variations );
if ( function_exists( 'wc_get_notices' ) ) {
$wc_errors = wc_get_notices( 'error' );
}
$wc_output = ob_get_clean();
if ( $passed_validation && empty( $wc_errors ) && empty( $wc_output ) ) {
$redirect = false;
}
TInvWL_Public_Cart::unprepare_post( $wl_product );
}
return $redirect;
}
add_filter( 'tinvwl_product_add_to_cart_need_redirect', 'tinvwl_meta_validate_cart_add', 90, 4 );
} // End if().
if ( ! function_exists( 'tinv_wishlist_print_meta' ) ) {
/**
* Print meta data for wishlist form
*
* @param array $meta Meta Array.
* @param boolean $flat Return text or template.
*
* @return string
*/
function tinv_wishlist_print_meta( $meta = array(), $flat = false ) {
if ( ! is_array( $meta ) ) {
$meta = array();
}
$product_id = $variation_id = 0;
if ( array_key_exists( 'product_id', $meta ) ) {
$product_id = $meta['product_id'];
}
if ( array_key_exists( 'variation_id', $meta ) ) {
$variation_id = $meta['variation_id'];
}
foreach ( array( 'add-to-cart', 'product_id', 'variation_id', 'quantity', 'action', 'variation' ) as $field ) {
if ( array_key_exists( $field, $meta ) ) {
unset( $meta[ $field ] );
}
}
if ( array_key_exists( 'tinvwl-hidden-fields', $meta ) ) {
$hiddenFields = json_decode( $meta['tinvwl-hidden-fields'], true );
if ( $hiddenFields !== null ) {
foreach ( $hiddenFields as $hiddenKey ) {
if ( isset( $meta[ $hiddenKey ] ) ) {
unset( $meta[ $hiddenKey ] );
}
}
}
unset( $meta['tinvwl-hidden-fields'] );
}
$meta = array_filter( $meta );
if ( empty( $meta ) ) {
return '';
}
$item_data = array();
foreach ( $meta as $key => $value ) {
if ( ! preg_match( '/^\_/', $key ) ) {
$item_data[ $key ] = array(
'key' => $key,
'display' => $value,
);
}
}
foreach ( array_keys( $item_data ) as $key ) {
if ( strpos( $key, 'attribute_' ) === 0 ) {
unset( $item_data[ $key ] );
}
}
$item_data = apply_filters( 'tinvwl_wishlist_item_meta_post', $item_data, $product_id, $variation_id );
foreach ( $item_data as $key => $data ) {
if ( is_object( $data['display'] ) || is_array( $data['display'] ) ) {
$item_data[ $key ]['display'] = json_encode( $data['display'] );
}
}
ob_start();
if ( $flat ) {
foreach ( $item_data as $data ) {
echo esc_html( $data['key'] ) . ': ' . wp_kses_post( $data['display'] ) . '<br>';
}
} else {
if ( $item_data ) {
tinv_wishlist_template( 'ti-wishlist-item-data.php', array( 'item_data' => $item_data ) );
}
}
return apply_filters( 'tinvwl_wishlist_item_meta_wishlist', ob_get_clean() );
}
} // End if().
if ( ! function_exists( 'tinv_wishlistmeta' ) ) {
/**
* Show new meta data
*
* @param string $meta Print meta.
* @param array $wl_product Wishlist product.
* @param \WC_Product $product Woocommerce product.
*
* @return string
*/
function tinv_wishlistmeta( $meta, $wl_product, $product ) {
if ( array_key_exists( 'meta', $wl_product ) ) {
$wlmeta = apply_filters( 'tinvwl_wishlist_item_meta_wishlist_output', tinv_wishlist_print_meta( $wl_product['meta'] ), $wl_product, $product );
$meta .= $wlmeta;
}
return $meta;
}
add_filter( 'tinvwl_wishlist_item_meta_data', 'tinv_wishlistmeta', 10, 3 );
}
if ( ! function_exists( 'tinvwl_add_to_cart_item_meta_post' ) ) {
/**
* Save post data to cart item
*
* @param array $cart_item_data Array with cart item information.
* @param string $cart_item_key Cart item key.
*
* @return array
*/
function tinvwl_add_to_cart_item_meta_post( $cart_item_data, $cart_item_key ) {
$postdata = $_POST; // @codingStandardsIgnoreLine WordPress.VIP.SuperGlobalInputUsage.AccessDetected
$postdata = apply_filters( 'tinvwl_product_prepare_meta', $postdata, $cart_item_data['product_id'], $cart_item_data['variation_id'] );
if ( array_key_exists( 'variation_id', $postdata ) && ! empty( $postdata['variation_id'] ) ) {
foreach ( $postdata as $key => $field ) {
if ( preg_match( '/^attribute\_/', $key ) ) {
unset( $postdata[ $key ] );
}
}
}
foreach ( array( 'add-to-cart', 'product_id', 'variation_id', 'quantity' ) as $field ) {
if ( array_key_exists( $field, $postdata ) ) {
unset( $postdata[ $field ] );
}
}
$postdata = array_filter( $postdata );
if ( empty( $postdata ) ) {
return $cart_item_data;
}
ksort( $postdata );
$cart_item_data['tinvwl_formdata'] = $postdata;
return $cart_item_data;
}
add_action( 'woocommerce_add_cart_item', 'tinvwl_add_to_cart_item_meta_post', 10, 2 );
} // End if().
if ( ! function_exists( 'tinvwl_set_utm' ) ) {
/**
* Set UTM sources.
*/
function tinvwl_set_utm() {
// Set a source.
define( 'TINVWL_UTM_SOURCE', 'wordpress_org' );
// Set a medium.
define( 'TINVWL_UTM_MEDIUM', 'organic' );
// Set a campaign.