-
Notifications
You must be signed in to change notification settings - Fork 0
/
wp-template-functions.php
3896 lines (3289 loc) · 118 KB
/
wp-template-functions.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
/**
* WooCommerce Template
*
* Functions for the templating system.
*
* @package WooCommerce\Functions
* @version 2.5.0
*/
use Automattic\Jetpack\Constants;
defined( 'ABSPATH' ) || exit;
/**
* Handle redirects before content is output - hooked into template_redirect so is_page works.
*/
function wc_template_redirect() {
global $wp_query, $wp;
// phpcs:disable WordPress.Security.NonceVerification.Recommended
// When default permalinks are enabled, redirect shop page to post type archive url.
if ( ! empty( $_GET['page_id'] ) && '' === get_option( 'permalink_structure' ) && wc_get_page_id( 'shop' ) === absint( $_GET['page_id'] ) && get_post_type_archive_link( 'product' ) ) {
wp_safe_redirect( get_post_type_archive_link( 'product' ) );
exit;
}
// phpcs:enable WordPress.Security.NonceVerification.Recommended
// When on the checkout with an empty cart, redirect to cart page.
if ( is_page( wc_get_page_id( 'checkout' ) ) && wc_get_page_id( 'checkout' ) !== wc_get_page_id( 'cart' ) && WC()->cart->is_empty() && empty( $wp->query_vars['order-pay'] ) && ! isset( $wp->query_vars['order-received'] ) && ! is_customize_preview() && apply_filters( 'woocommerce_checkout_redirect_empty_cart', true ) ) {
wc_add_notice( __( 'Checkout is not available whilst your cart is empty.', 'woocommerce' ), 'notice' );
wp_safe_redirect( wc_get_cart_url() );
exit;
}
// Logout.
if ( isset( $wp->query_vars['customer-logout'] ) && ! empty( $_REQUEST['_wpnonce'] ) && wp_verify_nonce( sanitize_key( $_REQUEST['_wpnonce'] ), 'customer-logout' ) ) {
wp_safe_redirect( str_replace( '&', '&', wp_logout_url( apply_filters( 'woocommerce_logout_default_redirect_url', wc_get_page_permalink( 'myaccount' ) ) ) ) );
exit;
}
// Redirect to the correct logout endpoint.
if ( isset( $wp->query_vars['customer-logout'] ) && 'true' === $wp->query_vars['customer-logout'] ) {
wp_safe_redirect( esc_url_raw( wc_get_account_endpoint_url( 'customer-logout' ) ) );
exit;
}
// Trigger 404 if trying to access an endpoint on wrong page.
if ( is_wc_endpoint_url() && ! is_account_page() && ! is_checkout() && apply_filters( 'woocommerce_account_endpoint_page_not_found', true ) ) {
$wp_query->set_404();
status_header( 404 );
include get_query_template( '404' );
exit;
}
// Redirect to the product page if we have a single product.
if ( is_search() && is_post_type_archive( 'product' ) && apply_filters( 'woocommerce_redirect_single_search_result', true ) && 1 === absint( $wp_query->found_posts ) ) {
$product = wc_get_product( $wp_query->post );
if ( $product && $product->is_visible() ) {
wp_safe_redirect( get_permalink( $product->get_id() ), 302 );
exit;
}
}
// Ensure gateways and shipping methods are loaded early.
if ( is_add_payment_method_page() || is_checkout() ) {
// Buffer the checkout page.
ob_start();
// Ensure gateways and shipping methods are loaded early.
WC()->payment_gateways();
WC()->shipping();
}
}
add_action( 'template_redirect', 'wc_template_redirect' );
/**
* When loading sensitive checkout or account pages, send a HTTP header to limit rendering of pages to same origin iframes for security reasons.
*
* Can be disabled with: remove_action( 'template_redirect', 'wc_send_frame_options_header' );
*
* @since 2.3.10
*/
function wc_send_frame_options_header() {
if ( ( is_checkout() || is_account_page() ) && ! is_customize_preview() ) {
send_frame_options_header();
}
}
add_action( 'template_redirect', 'wc_send_frame_options_header' );
/**
* No index our endpoints.
* Prevent indexing pages like order-received.
*
* @since 2.5.3
*/
function wc_prevent_endpoint_indexing() {
// phpcs:disable WordPress.Security.NonceVerification.Recommended, WordPress.PHP.NoSilencedErrors.Discouraged
if ( is_wc_endpoint_url() || isset( $_GET['download_file'] ) ) {
@header( 'X-Robots-Tag: noindex' );
}
// phpcs:enable WordPress.Security.NonceVerification.Recommended, WordPress.PHP.NoSilencedErrors.Discouraged
}
add_action( 'template_redirect', 'wc_prevent_endpoint_indexing' );
/**
* Remove adjacent_posts_rel_link_wp_head - pointless for products.
*
* @since 3.0.0
*/
function wc_prevent_adjacent_posts_rel_link_wp_head() {
if ( is_singular( 'product' ) ) {
remove_action( 'wp_head', 'adjacent_posts_rel_link_wp_head', 10, 0 );
}
}
add_action( 'template_redirect', 'wc_prevent_adjacent_posts_rel_link_wp_head' );
/**
* Show the gallery if JS is disabled.
*
* @since 3.0.6
*/
function wc_gallery_noscript() {
?>
<noscript><style>.woocommerce-product-gallery{ opacity: 1 !important; }</style></noscript>
<?php
}
add_action( 'wp_head', 'wc_gallery_noscript' );
/**
* When the_post is called, put product data into a global.
*
* @param mixed $post Post Object.
* @return WC_Product
*/
function wc_setup_product_data( $post ) {
unset( $GLOBALS['product'] );
if ( is_int( $post ) ) {
$post = get_post( $post );
}
if ( empty( $post->post_type ) || ! in_array( $post->post_type, array( 'product', 'product_variation' ), true ) ) {
return;
}
$GLOBALS['product'] = wc_get_product( $post );
return $GLOBALS['product'];
}
add_action( 'the_post', 'wc_setup_product_data' );
/**
* Sets up the woocommerce_loop global from the passed args or from the main query.
*
* @since 3.3.0
* @param array $args Args to pass into the global.
*/
function wc_setup_loop( $args = array() ) {
$default_args = array(
'loop' => 0,
'columns' => wc_get_default_products_per_row(),
'name' => '',
'is_shortcode' => false,
'is_paginated' => true,
'is_search' => false,
'is_filtered' => false,
'total' => 0,
'total_pages' => 0,
'per_page' => 0,
'current_page' => 1,
);
// If this is a main WC query, use global args as defaults.
if ( $GLOBALS['wp_query']->get( 'wc_query' ) ) {
$default_args = array_merge(
$default_args,
array(
'is_search' => $GLOBALS['wp_query']->is_search(),
'is_filtered' => is_filtered(),
'total' => $GLOBALS['wp_query']->found_posts,
'total_pages' => $GLOBALS['wp_query']->max_num_pages,
'per_page' => $GLOBALS['wp_query']->get( 'posts_per_page' ),
'current_page' => max( 1, $GLOBALS['wp_query']->get( 'paged', 1 ) ),
)
);
}
// Merge any existing values.
if ( isset( $GLOBALS['woocommerce_loop'] ) ) {
$default_args = array_merge( $default_args, $GLOBALS['woocommerce_loop'] );
}
$GLOBALS['woocommerce_loop'] = wp_parse_args( $args, $default_args );
}
add_action( 'woocommerce_before_shop_loop', 'wc_setup_loop' );
/**
* Resets the woocommerce_loop global.
*
* @since 3.3.0
*/
function wc_reset_loop() {
unset( $GLOBALS['woocommerce_loop'] );
}
add_action( 'woocommerce_after_shop_loop', 'woocommerce_reset_loop', 999 );
/**
* Gets a property from the woocommerce_loop global.
*
* @since 3.3.0
* @param string $prop Prop to get.
* @param string $default Default if the prop does not exist.
* @return mixed
*/
function wc_get_loop_prop( $prop, $default = '' ) {
wc_setup_loop(); // Ensure shop loop is setup.
return isset( $GLOBALS['woocommerce_loop'], $GLOBALS['woocommerce_loop'][ $prop ] ) ? $GLOBALS['woocommerce_loop'][ $prop ] : $default;
}
/**
* Sets a property in the woocommerce_loop global.
*
* @since 3.3.0
* @param string $prop Prop to set.
* @param string $value Value to set.
*/
function wc_set_loop_prop( $prop, $value = '' ) {
if ( ! isset( $GLOBALS['woocommerce_loop'] ) ) {
wc_setup_loop();
}
$GLOBALS['woocommerce_loop'][ $prop ] = $value;
}
/**
* Set the current visbility for a product in the woocommerce_loop global.
*
* @since 4.4.0
* @param int $product_id Product it to cache visbiility for.
* @param bool $value The poduct visibility value to cache.
*/
function wc_set_loop_product_visibility( $product_id, $value ) {
wc_set_loop_prop( "product_visibility_$product_id", $value );
}
/**
* Gets the cached current visibility for a product from the woocommerce_loop global.
*
* @since 4.4.0
* @param int $product_id Product id to get the cached visibility for.
*
* @return bool|null The cached product visibility, or null if on visibility has been cached for that product.
*/
function wc_get_loop_product_visibility( $product_id ) {
return wc_get_loop_prop( "product_visibility_$product_id", null );
}
/**
* Should the WooCommerce loop be displayed?
*
* This will return true if we have posts (products) or if we have subcats to display.
*
* @since 3.4.0
* @return bool
*/
function woocommerce_product_loop() {
return have_posts() || 'products' !== woocommerce_get_loop_display_mode();
}
/**
* Output generator tag to aid debugging.
*
* @param string $gen Generator.
* @param string $type Type.
* @return string
*/
function wc_generator_tag( $gen, $type ) {
$version = Constants::get_constant( 'WC_VERSION' );
switch ( $type ) {
case 'html':
$gen .= "\n" . '<meta name="generator" content="WooCommerce ' . esc_attr( $version ) . '">';
break;
case 'xhtml':
$gen .= "\n" . '<meta name="generator" content="WooCommerce ' . esc_attr( $version ) . '" />';
break;
}
return $gen;
}
/**
* Add body classes for WC pages.
*
* @param array $classes Body Classes.
* @return array
*/
function wc_body_class( $classes ) {
$classes = (array) $classes;
if ( is_shop() ) {
$classes[] = 'woocommerce-shop';
}
if ( is_woocommerce() ) {
$classes[] = 'woocommerce';
$classes[] = 'woocommerce-page';
} elseif ( is_checkout() ) {
$classes[] = 'woocommerce-checkout';
$classes[] = 'woocommerce-page';
} elseif ( is_cart() ) {
$classes[] = 'woocommerce-cart';
$classes[] = 'woocommerce-page';
} elseif ( is_account_page() ) {
$classes[] = 'woocommerce-account';
$classes[] = 'woocommerce-page';
}
if ( is_store_notice_showing() ) {
$classes[] = 'woocommerce-demo-store';
}
foreach ( WC()->query->get_query_vars() as $key => $value ) {
if ( is_wc_endpoint_url( $key ) ) {
$classes[] = 'woocommerce-' . sanitize_html_class( $key );
}
}
if ( wc_block_theme_has_styles_for_element( 'button' ) ) {
$classes[] = 'woocommerce-block-theme-has-button-styles';
}
$classes[] = 'woocommerce-no-js';
add_action( 'wp_footer', 'wc_no_js' );
return array_unique( $classes );
}
/**
* NO JS handling.
*
* @since 3.4.0
*/
function wc_no_js() {
?>
<script type="text/javascript">
(function () {
var c = document.body.className;
c = c.replace(/woocommerce-no-js/, 'woocommerce-js');
document.body.className = c;
})();
</script>
<?php
}
/**
* Display the classes for the product cat div.
*
* @since 2.4.0
* @param string|array $class One or more classes to add to the class list.
* @param object $category object Optional.
*/
function wc_product_cat_class( $class = '', $category = null ) {
// Separates classes with a single space, collates classes for post DIV.
echo 'class="' . esc_attr( join( ' ', wc_get_product_cat_class( $class, $category ) ) ) . '"';
}
/**
* Get the default columns setting - this is how many products will be shown per row in loops.
*
* @since 3.3.0
* @return int
*/
function wc_get_default_products_per_row() {
$columns = get_option( 'woocommerce_catalog_columns', 4 );
$product_grid = wc_get_theme_support( 'product_grid' );
$min_columns = isset( $product_grid['min_columns'] ) ? absint( $product_grid['min_columns'] ) : 0;
$max_columns = isset( $product_grid['max_columns'] ) ? absint( $product_grid['max_columns'] ) : 0;
if ( $min_columns && $columns < $min_columns ) {
$columns = $min_columns;
update_option( 'woocommerce_catalog_columns', $columns );
} elseif ( $max_columns && $columns > $max_columns ) {
$columns = $max_columns;
update_option( 'woocommerce_catalog_columns', $columns );
}
if ( has_filter( 'loop_shop_columns' ) ) { // Legacy filter handling.
$columns = apply_filters( 'loop_shop_columns', $columns );
}
$columns = absint( $columns );
return max( 1, $columns );
}
/**
* Get the default rows setting - this is how many product rows will be shown in loops.
*
* @since 3.3.0
* @return int
*/
function wc_get_default_product_rows_per_page() {
$rows = absint( get_option( 'woocommerce_catalog_rows', 4 ) );
$product_grid = wc_get_theme_support( 'product_grid' );
$min_rows = isset( $product_grid['min_rows'] ) ? absint( $product_grid['min_rows'] ) : 0;
$max_rows = isset( $product_grid['max_rows'] ) ? absint( $product_grid['max_rows'] ) : 0;
if ( $min_rows && $rows < $min_rows ) {
$rows = $min_rows;
update_option( 'woocommerce_catalog_rows', $rows );
} elseif ( $max_rows && $rows > $max_rows ) {
$rows = $max_rows;
update_option( 'woocommerce_catalog_rows', $rows );
}
return $rows;
}
/**
* Reset the product grid settings when a new theme is activated.
*
* @since 3.3.0
*/
function wc_reset_product_grid_settings() {
$product_grid = wc_get_theme_support( 'product_grid' );
if ( ! empty( $product_grid['default_rows'] ) ) {
update_option( 'woocommerce_catalog_rows', absint( $product_grid['default_rows'] ) );
}
if ( ! empty( $product_grid['default_columns'] ) ) {
update_option( 'woocommerce_catalog_columns', absint( $product_grid['default_columns'] ) );
}
wp_cache_flush(); // Flush any caches which could impact settings or templates.
}
add_action( 'after_switch_theme', 'wc_reset_product_grid_settings' );
/**
* Get classname for woocommerce loops.
*
* @since 2.6.0
* @return string
*/
function wc_get_loop_class() {
$loop_index = wc_get_loop_prop( 'loop', 0 );
$columns = absint( max( 1, wc_get_loop_prop( 'columns', wc_get_default_products_per_row() ) ) );
$loop_index ++;
wc_set_loop_prop( 'loop', $loop_index );
if ( 0 === ( $loop_index - 1 ) % $columns || 1 === $columns ) {
return 'first';
}
if ( 0 === $loop_index % $columns ) {
return 'last';
}
return '';
}
/**
* Get the classes for the product cat div.
*
* @since 2.4.0
*
* @param string|array $class One or more classes to add to the class list.
* @param object $category object Optional.
*
* @return array
*/
function wc_get_product_cat_class( $class = '', $category = null ) {
$classes = is_array( $class ) ? $class : array_map( 'trim', explode( ' ', $class ) );
$classes[] = 'product-category';
$classes[] = 'product';
$classes[] = wc_get_loop_class();
$classes = apply_filters( 'product_cat_class', $classes, $class, $category );
return array_unique( array_filter( $classes ) );
}
/**
* Adds extra post classes for products via the WordPress post_class hook, if used.
*
* Note: For performance reasons we instead recommend using wc_product_class/wc_get_product_class instead.
*
* @since 2.1.0
* @param array $classes Current classes.
* @param string|array $class Additional class.
* @param int $post_id Post ID.
* @return array
*/
function wc_product_post_class( $classes, $class = '', $post_id = 0 ) {
if ( ! $post_id || ! in_array( get_post_type( $post_id ), array( 'product', 'product_variation' ), true ) ) {
return $classes;
}
$product = wc_get_product( $post_id );
if ( ! $product ) {
return $classes;
}
$classes[] = 'product';
$classes[] = wc_get_loop_class();
$classes[] = $product->get_stock_status();
if ( $product->is_on_sale() ) {
$classes[] = 'sale';
}
if ( $product->is_featured() ) {
$classes[] = 'featured';
}
if ( $product->is_downloadable() ) {
$classes[] = 'downloadable';
}
if ( $product->is_virtual() ) {
$classes[] = 'virtual';
}
if ( $product->is_sold_individually() ) {
$classes[] = 'sold-individually';
}
if ( $product->is_taxable() ) {
$classes[] = 'taxable';
}
if ( $product->is_shipping_taxable() ) {
$classes[] = 'shipping-taxable';
}
if ( $product->is_purchasable() ) {
$classes[] = 'purchasable';
}
if ( $product->get_type() ) {
$classes[] = 'product-type-' . $product->get_type();
}
if ( $product->is_type( 'variable' ) && $product->get_default_attributes() ) {
$classes[] = 'has-default-attributes';
}
$key = array_search( 'hentry', $classes, true );
if ( false !== $key ) {
unset( $classes[ $key ] );
}
return $classes;
}
/**
* Get product taxonomy HTML classes.
*
* @since 3.4.0
* @param array $term_ids Array of terms IDs or objects.
* @param string $taxonomy Taxonomy.
* @return array
*/
function wc_get_product_taxonomy_class( $term_ids, $taxonomy ) {
$classes = array();
foreach ( $term_ids as $term_id ) {
$term = get_term( $term_id, $taxonomy );
if ( empty( $term->slug ) ) {
continue;
}
$term_class = sanitize_html_class( $term->slug, $term->term_id );
if ( is_numeric( $term_class ) || ! trim( $term_class, '-' ) ) {
$term_class = $term->term_id;
}
// 'post_tag' uses the 'tag' prefix for backward compatibility.
if ( 'post_tag' === $taxonomy ) {
$classes[] = 'tag-' . $term_class;
} else {
$classes[] = sanitize_html_class( $taxonomy . '-' . $term_class, $taxonomy . '-' . $term->term_id );
}
}
return $classes;
}
/**
* Retrieves the classes for the post div as an array.
*
* This method was modified from WordPress's get_post_class() to allow the removal of taxonomies
* (for performance reasons). Previously wc_product_post_class was hooked into post_class. @since 3.6.0
*
* @since 3.4.0
* @param string|array $class One or more classes to add to the class list.
* @param int|WP_Post|WC_Product $product Product ID or product object.
* @return array
*/
function wc_get_product_class( $class = '', $product = null ) {
if ( is_null( $product ) && ! empty( $GLOBALS['product'] ) ) {
// Product was null so pull from global.
$product = $GLOBALS['product'];
}
if ( $product && ! is_a( $product, 'WC_Product' ) ) {
// Make sure we have a valid product, or set to false.
$product = wc_get_product( $product );
}
if ( $class ) {
if ( ! is_array( $class ) ) {
$class = preg_split( '#\s+#', $class );
}
} else {
$class = array();
}
$post_classes = array_map( 'esc_attr', $class );
if ( ! $product ) {
return $post_classes;
}
// Run through the post_class hook so 3rd parties using this previously can still append classes.
// Note, to change classes you will need to use the newer woocommerce_post_class filter.
// @internal This removes the wc_product_post_class filter so classes are not duplicated.
$filtered = has_filter( 'post_class', 'wc_product_post_class' );
if ( $filtered ) {
remove_filter( 'post_class', 'wc_product_post_class', 20 );
}
$post_classes = apply_filters( 'post_class', $post_classes, $class, $product->get_id() );
if ( $filtered ) {
add_filter( 'post_class', 'wc_product_post_class', 20, 3 );
}
$classes = array_merge(
$post_classes,
array(
'product',
'type-product',
'post-' . $product->get_id(),
'status-' . $product->get_status(),
wc_get_loop_class(),
$product->get_stock_status(),
),
wc_get_product_taxonomy_class( $product->get_category_ids(), 'product_cat' ),
wc_get_product_taxonomy_class( $product->get_tag_ids(), 'product_tag' )
);
if ( $product->get_image_id() ) {
$classes[] = 'has-post-thumbnail';
}
if ( $product->get_post_password() ) {
$classes[] = post_password_required( $product->get_id() ) ? 'post-password-required' : 'post-password-protected';
}
if ( $product->is_on_sale() ) {
$classes[] = 'sale';
}
if ( $product->is_featured() ) {
$classes[] = 'featured';
}
if ( $product->is_downloadable() ) {
$classes[] = 'downloadable';
}
if ( $product->is_virtual() ) {
$classes[] = 'virtual';
}
if ( $product->is_sold_individually() ) {
$classes[] = 'sold-individually';
}
if ( $product->is_taxable() ) {
$classes[] = 'taxable';
}
if ( $product->is_shipping_taxable() ) {
$classes[] = 'shipping-taxable';
}
if ( $product->is_purchasable() ) {
$classes[] = 'purchasable';
}
if ( $product->get_type() ) {
$classes[] = 'product-type-' . $product->get_type();
}
if ( $product->is_type( 'variable' ) && $product->get_default_attributes() ) {
$classes[] = 'has-default-attributes';
}
// Include attributes and any extra taxonomies only if enabled via the hook - this is a performance issue.
if ( apply_filters( 'woocommerce_get_product_class_include_taxonomies', false ) ) {
$taxonomies = get_taxonomies( array( 'public' => true ) );
$type = 'variation' === $product->get_type() ? 'product_variation' : 'product';
foreach ( (array) $taxonomies as $taxonomy ) {
if ( is_object_in_taxonomy( $type, $taxonomy ) && ! in_array( $taxonomy, array( 'product_cat', 'product_tag' ), true ) ) {
$classes = array_merge( $classes, wc_get_product_taxonomy_class( (array) get_the_terms( $product->get_id(), $taxonomy ), $taxonomy ) );
}
}
}
/**
* WooCommerce Post Class filter.
*
* @since 3.6.2
* @param array $classes Array of CSS classes.
* @param WC_Product $product Product object.
*/
$classes = apply_filters( 'woocommerce_post_class', $classes, $product );
return array_map( 'esc_attr', array_unique( array_filter( $classes ) ) );
}
/**
* Display the classes for the product div.
*
* @since 3.4.0
* @param string|array $class One or more classes to add to the class list.
* @param int|WP_Post|WC_Product $product_id Product ID or product object.
*/
function wc_product_class( $class = '', $product_id = null ) {
echo 'class="' . esc_attr( implode( ' ', wc_get_product_class( $class, $product_id ) ) ) . '"';
}
/**
* Outputs hidden form inputs for each query string variable.
*
* @since 3.0.0
* @param string|array $values Name value pairs, or a URL to parse.
* @param array $exclude Keys to exclude.
* @param string $current_key Current key we are outputting.
* @param bool $return Whether to return.
* @return string
*/
function wc_query_string_form_fields( $values = null, $exclude = array(), $current_key = '', $return = false ) {
if ( is_null( $values ) ) {
// phpcs:ignore WordPress.Security.NonceVerification.Recommended
$values = $_GET;
} elseif ( is_string( $values ) ) {
$url_parts = wp_parse_url( $values );
$values = array();
if ( ! empty( $url_parts['query'] ) ) {
// This is to preserve full-stops, pluses and spaces in the query string when ran through parse_str.
$replace_chars = array(
'.' => '{dot}',
'+' => '{plus}',
);
$query_string = str_replace( array_keys( $replace_chars ), array_values( $replace_chars ), $url_parts['query'] );
// Parse the string.
parse_str( $query_string, $parsed_query_string );
// Convert the full-stops, pluses and spaces back and add to values array.
foreach ( $parsed_query_string as $key => $value ) {
$new_key = str_replace( array_values( $replace_chars ), array_keys( $replace_chars ), $key );
$new_value = str_replace( array_values( $replace_chars ), array_keys( $replace_chars ), $value );
$values[ $new_key ] = $new_value;
}
}
}
$html = '';
foreach ( $values as $key => $value ) {
if ( in_array( $key, $exclude, true ) ) {
continue;
}
if ( $current_key ) {
$key = $current_key . '[' . $key . ']';
}
if ( is_array( $value ) ) {
$html .= wc_query_string_form_fields( $value, $exclude, $key, true );
} else {
$html .= '<input type="hidden" name="' . esc_attr( $key ) . '" value="' . esc_attr( wp_unslash( $value ) ) . '" />';
}
}
if ( $return ) {
return $html;
}
echo $html; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
}
/**
* Get the terms and conditions page ID.
*
* @since 3.4.0
* @return int
*/
function wc_terms_and_conditions_page_id() {
$page_id = wc_get_page_id( 'terms' );
return apply_filters( 'woocommerce_terms_and_conditions_page_id', 0 < $page_id ? absint( $page_id ) : 0 );
}
/**
* Get the privacy policy page ID.
*
* @since 3.4.0
* @return int
*/
function wc_privacy_policy_page_id() {
$page_id = get_option( 'wp_page_for_privacy_policy', 0 );
return apply_filters( 'woocommerce_privacy_policy_page_id', 0 < $page_id ? absint( $page_id ) : 0 );
}
/**
* See if the checkbox is enabled or not based on the existance of the terms page and checkbox text.
*
* @since 3.4.0
* @return bool
*/
function wc_terms_and_conditions_checkbox_enabled() {
$page_id = wc_terms_and_conditions_page_id();
$page = $page_id ? get_post( $page_id ) : false;
return $page && wc_get_terms_and_conditions_checkbox_text();
}
/**
* Get the terms and conditions checkbox text, if set.
*
* @since 3.4.0
* @return string
*/
function wc_get_terms_and_conditions_checkbox_text() {
/* translators: %s terms and conditions page name and link */
return trim( apply_filters( 'woocommerce_get_terms_and_conditions_checkbox_text', get_option( 'woocommerce_checkout_terms_and_conditions_checkbox_text', sprintf( __( 'I have read and agree to the website %s', 'woocommerce' ), '[terms]' ) ) ) );
}
/**
* Get the privacy policy text, if set.
*
* @since 3.4.0
* @param string $type Type of policy to load. Valid values include registration and checkout.
* @return string
*/
function wc_get_privacy_policy_text( $type = '' ) {
$text = '';
switch ( $type ) {
case 'checkout':
/* translators: %s privacy policy page name and link */
$text = get_option( 'woocommerce_checkout_privacy_policy_text', sprintf( __( 'Your personal data will be used to process your order, support your experience throughout this website, and for other purposes described in our %s.', 'woocommerce' ), '[privacy_policy]' ) );
break;
case 'registration':
/* translators: %s privacy policy page name and link */
$text = get_option( 'woocommerce_registration_privacy_policy_text', sprintf( __( 'Your personal data will be used to support your experience throughout this website, to manage access to your account, and for other purposes described in our %s.', 'woocommerce' ), '[privacy_policy]' ) );
break;
}
return trim( apply_filters( 'woocommerce_get_privacy_policy_text', $text, $type ) );
}
/**
* Output t&c checkbox text.
*
* @since 3.4.0
*/
function wc_terms_and_conditions_checkbox_text() {
$text = wc_get_terms_and_conditions_checkbox_text();
if ( ! $text ) {
return;
}
echo wp_kses_post( wc_replace_policy_page_link_placeholders( $text ) );
}
/**
* Output t&c page's content (if set). The page can be set from checkout settings.
*
* @since 3.4.0
*/
function wc_terms_and_conditions_page_content() {
$terms_page_id = wc_terms_and_conditions_page_id();
if ( ! $terms_page_id ) {
return;
}
$page = get_post( $terms_page_id );
if ( $page && 'publish' === $page->post_status && $page->post_content && ! has_shortcode( $page->post_content, 'woocommerce_checkout' ) ) {
echo '<div class="woocommerce-terms-and-conditions" style="display: none; max-height: 200px; overflow: auto;">' . wc_format_content( wp_kses_post( $page->post_content ) ) . '</div>'; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
}
}
/**
* Render privacy policy text on the checkout.
*
* @since 3.4.0
*/
function wc_checkout_privacy_policy_text() {
echo '<div class="woocommerce-privacy-policy-text">';
wc_privacy_policy_text( 'checkout' );
echo '</div>';
}
/**
* Render privacy policy text on the register forms.
*
* @since 3.4.0
*/
function wc_registration_privacy_policy_text() {
echo '<div class="woocommerce-privacy-policy-text">';
wc_privacy_policy_text( 'registration' );
echo '</div>';
}
/**
* Output privacy policy text. This is custom text which can be added via the customizer/privacy settings section.
*
* Loads the relevant policy for the current page unless a specific policy text is required.
*
* @since 3.4.0
* @param string $type Type of policy to load. Valid values include registration and checkout.
*/
function wc_privacy_policy_text( $type = 'checkout' ) {
if ( ! wc_privacy_policy_page_id() ) {
return;
}
echo wp_kses_post( wpautop( wc_replace_policy_page_link_placeholders( wc_get_privacy_policy_text( $type ) ) ) );
}
/**
* Replaces placeholders with links to WooCommerce policy pages.
*
* @since 3.4.0
* @param string $text Text to find/replace within.
* @return string
*/
function wc_replace_policy_page_link_placeholders( $text ) {
$privacy_page_id = wc_privacy_policy_page_id();
$terms_page_id = wc_terms_and_conditions_page_id();
$privacy_link = $privacy_page_id ? '<a href="' . esc_url( get_permalink( $privacy_page_id ) ) . '" class="woocommerce-privacy-policy-link" target="_blank">' . __( 'privacy policy', 'woocommerce' ) . '</a>' : __( 'privacy policy', 'woocommerce' );
$terms_link = $terms_page_id ? '<a href="' . esc_url( get_permalink( $terms_page_id ) ) . '" class="woocommerce-terms-and-conditions-link" target="_blank">' . __( 'terms and conditions', 'woocommerce' ) . '</a>' : __( 'terms and conditions', 'woocommerce' );
$find_replace = array(
'[terms]' => $terms_link,
'[privacy_policy]' => $privacy_link,
);
return str_replace( array_keys( $find_replace ), array_values( $find_replace ), $text );
}
/**
* Template pages
*/
if ( ! function_exists( 'woocommerce_content' ) ) {
/**
* Output WooCommerce content.
*
* This function is only used in the optional 'woocommerce.php' template.
* which people can add to their themes to add basic woocommerce support.
* without hooks or modifying core templates.
*/
function woocommerce_content() {
if ( is_singular( 'product' ) ) {
while ( have_posts() ) :
the_post();
wc_get_template_part( 'content', 'single-product' );
endwhile;
} else {
?>
<?php if ( apply_filters( 'woocommerce_show_page_title', true ) ) : ?>
<h1 class="page-title"><?php woocommerce_page_title(); ?></h1>
<?php endif; ?>
<?php do_action( 'woocommerce_archive_description' ); ?>
<?php if ( woocommerce_product_loop() ) : ?>
<?php do_action( 'woocommerce_before_shop_loop' ); ?>
<?php woocommerce_product_loop_start(); ?>
<?php if ( wc_get_loop_prop( 'total' ) ) : ?>
<?php while ( have_posts() ) : ?>
<?php the_post(); ?>
<?php wc_get_template_part( 'content', 'product' ); ?>