-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunctions.php
More file actions
1400 lines (1156 loc) · 44 KB
/
functions.php
File metadata and controls
1400 lines (1156 loc) · 44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
//////////////////////////////////////////////////
//////////// Dev & Build Environment /////////////
//////////////////////////////////////////////////
function csv_is_vite_dev() {
return defined('CUSTOM_WP_VITE_DEV') && CUSTOM_WP_VITE_DEV;
}
// Enqueue either Vite dev assets or the built /dist assets.
function csv_assets() {
$theme_uri = get_template_directory_uri();
$dist = $theme_uri . '/dist';
// Toggle with: define('CUSTOM_WP_VITE_DEV', true); in wp-config.php
$use_vite = csv_is_vite_dev();
if ( $use_vite ) {
// Vite dev server (no SSL, local container).
$vite = 'http://localhost:5175';
// Ensure dev scripts are output as ES modules.
add_filter( 'script_loader_tag', function ( $tag, $handle, $src ) {
$module_handles = array( 'vite-client', 'csv-main' );
if ( in_array( $handle, $module_handles, true ) ) {
return '<script type="module" src="' . esc_url( $src ) . '"></script>';
}
return $tag;
}, 10, 3 );
// Vite HMR client.
wp_enqueue_script( 'vite-client', $vite . '/@vite/client', array(), null, false );
wp_script_add_data( 'vite-client', 'type', 'module' );
// Main JS entry served by Vite (imports SCSS in dev).
wp_enqueue_script( 'csv-main', $vite . '/main.js', array(), null, false );
wp_script_add_data( 'csv-main', 'type', 'module' );
// Static stylesheet served from Vite public/ during dev.
wp_enqueue_style( 'csv-tailwind', $vite . '/tailwind.css', array(), null );
return;
}
$main_css_path = get_template_directory() . '/dist/assets/main.css';
$tailwind_css_path = get_template_directory() . '/dist/tailwind.css';
$main_js_path = get_template_directory() . '/dist/main.js';
$main_css_version = file_exists( $main_css_path ) ? filemtime( $main_css_path ) : null;
$tailwind_css_version = file_exists( $tailwind_css_path ) ? filemtime( $tailwind_css_path ) : null;
$main_js_version = file_exists( $main_js_path ) ? filemtime( $main_js_path ) : null;
// Static stylesheet (not processed by Vite).
wp_enqueue_style( 'csv-tailwind', $dist . '/tailwind.css', array(), $tailwind_css_version );
// Main compiled CSS bundle.
wp_enqueue_style( 'csv-main', $dist . '/assets/main.css', array( 'csv-tailwind' ), $main_css_version );
// Main JS bundle (ES module).
wp_enqueue_script( 'csv-main', $dist . '/main.js', array(), $main_js_version, true );
wp_script_add_data( 'csv-main', 'type', 'module' );
}
add_action( 'wp_enqueue_scripts', 'csv_assets', 999 );
// Force the theme main bundle to render as an ES module to avoid global scope collisions.
function csv_force_csv_main_module_tag( $tag, $handle, $src ) {
if ( 'csv-main' !== $handle ) {
return $tag;
}
return '<script type="module" src="' . esc_url( $src ) . '"></script>';
}
add_filter( 'script_loader_tag', 'csv_force_csv_main_module_tag', 20, 3 );
// Ensure Vite-built block scripts load as ES modules.
function csv_block_module_scripts( $tag, $handle, $src ) {
if ( false !== strpos( $src, '/dist/blocks/' ) ) {
return '<script type="module" src="' . esc_url( $src ) . '"></script>';
}
return $tag;
}
add_filter( 'script_loader_tag', 'csv_block_module_scripts', 10, 3 );
// Output GA only outside local Vite dev mode.
function csv_output_google_analytics_tag() {
if ( csv_is_vite_dev() || is_user_logged_in() ) {
return;
}
if ( ! defined( 'CSV_GA_MEASUREMENT_ID' ) ) {
return;
}
$ga_measurement_id = trim( (string) CSV_GA_MEASUREMENT_ID );
if ( '' === $ga_measurement_id ) {
return;
}
$ga_script_url = add_query_arg( 'id', $ga_measurement_id, 'https://www.googletagmanager.com/gtag/js' );
?>
<!-- Google tag (gtag.js) -->
<script async src="<?php echo esc_url( $ga_script_url ); ?>"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', <?php echo wp_json_encode( $ga_measurement_id ); ?>);
</script>
<?php
}
add_action( 'wp_head', 'csv_output_google_analytics_tag', 5 );
//////////////////////////////////////////////////
///////////////// WordPress Theme ////////////////
//////////////////////////////////////////////////
// Add page template column to Pages list.
function csv_add_page_template_column( $columns ) {
$columns['csv_page_template'] = 'Template';
return $columns;
}
add_filter( 'manage_pages_columns', 'csv_add_page_template_column' );
// Render page template column content.
function csv_render_page_template_column( $column, $post_id ) {
if ( 'csv_page_template' !== $column ) {
return;
}
$template = get_page_template_slug( $post_id );
if ( ! $template ) {
echo 'Default';
return;
}
echo esc_html( basename( $template ) );
}
add_action( 'manage_pages_custom_column', 'csv_render_page_template_column', 10, 2 );
// Make page template column sortable.
function csv_page_template_sortable_column( $columns ) {
$columns['csv_page_template'] = 'csv_page_template';
return $columns;
}
add_filter( 'manage_edit-page_sortable_columns', 'csv_page_template_sortable_column' );
// Apply sorting by page template when requested.
function csv_page_template_orderby( $query ) {
if ( ! is_admin() || ! $query->is_main_query() ) {
return;
}
if ( 'page' !== $query->get( 'post_type' ) ) {
return;
}
if ( 'csv_page_template' !== $query->get( 'orderby' ) ) {
return;
}
$query->set( 'meta_key', '_wp_page_template' );
$query->set( 'orderby', 'meta_value' );
}
add_action( 'pre_get_posts', 'csv_page_template_orderby' );
// Register navigation menus
function csv_setup() {
add_theme_support( 'title-tag' );
add_theme_support( 'post-thumbnails' );
add_theme_support( 'html5', array( 'search-form', 'comment-form', 'comment-list', 'gallery', 'caption', 'style', 'script' ) );
register_nav_menus( array(
//'primary' => 'Primary Menu',
//'footer' => 'Footer Menu',
'mobile' => 'Mobile Menu - Top',
'mobile-about' => 'Mobile Menu - Bottom',
) );
}
add_action( 'after_setup_theme', 'csv_setup' );
// Set the global excerpt word count for get_the_excerpt().
function csv_excerpt_length( $length ) {
return 45;
}
add_filter( 'excerpt_length', 'csv_excerpt_length', 999 );
// Conveinent way to keep copyright updated
function shortcode_current_year() {
return date('Y');
}
add_shortcode('current_year', 'shortcode_current_year');
// Register Gutenburg custom blocks
function theme_register_blocks() {
register_block_type( __DIR__ . '/blocks/hero-header');
register_block_type( __DIR__ . '/blocks/next-step');
}
add_action( 'init', 'theme_register_blocks' );
//////////////////////////////////////////////////
////////////////// Woocommerce ///////////////////
//////////////////////////////////////////////////
// Add WooCommerce theme support.
function csv_add_woocommerce_support() {
add_theme_support( 'woocommerce' );
//add_theme_support( 'wc-product-gallery-zoom' );
//add_theme_support( 'wc-product-gallery-lightbox' );
//add_theme_support( 'wc-product-gallery-slider' );
}
add_action( 'after_setup_theme', 'csv_add_woocommerce_support' );
// Remove related products on single product pages.
function csv_remove_single_product_related_products() {
remove_action( 'woocommerce_after_single_product_summary', 'woocommerce_output_related_products', 20 );
}
add_action( 'init', 'csv_remove_single_product_related_products' );
// Remove the WooCommerce sidebar on product pages, product archives, and blog categories.
function csv_remove_woocommerce_sidebar_on_product() {
$is_single_product = function_exists( 'is_product' ) && is_product();
$is_blog_category = is_category();
$is_product_archive = ( function_exists( 'is_shop' ) && is_shop() )
|| ( function_exists( 'is_product_taxonomy' ) && is_product_taxonomy() )
|| is_post_type_archive( 'product' );
if ( $is_single_product || $is_blog_category || $is_product_archive ) {
remove_action( 'woocommerce_sidebar', 'woocommerce_get_sidebar', 10 );
}
}
add_action( 'wp', 'csv_remove_woocommerce_sidebar_on_product' );
// Remove "Additional information" tab on single product pages.
function csv_remove_additional_information_tab( $tabs ) {
unset( $tabs['additional_information'] );
return $tabs;
}
add_filter( 'woocommerce_product_tabs', 'csv_remove_additional_information_tab', 99 );
// Remove product meta (SKU, category, tags) on single product pages.
function csv_remove_single_product_meta() {
remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_meta', 40 );
}
add_action( 'init', 'csv_remove_single_product_meta' );
// Hide the reset variations link sitewide.
function csv_remove_reset_variations_link( $link ) {
return '';
}
add_filter( 'woocommerce_reset_variations_link', 'csv_remove_reset_variations_link' );
// Remove WooCommerce breadcrumbs.
function csv_remove_woocommerce_breadcrumbs() {
remove_action( 'woocommerce_before_main_content', 'woocommerce_breadcrumb', 20 );
}
add_action( 'init', 'csv_remove_woocommerce_breadcrumbs' );
// Apply coupon codes passed via URL (?coupon_code=XXXX).
function csv_apply_coupon_code_from_url() {
if ( is_admin() || ! function_exists( 'WC' ) ) {
return;
}
if ( empty( $_GET['coupon_code'] ) ) {
return;
}
$coupon_code = sanitize_text_field( wp_unslash( $_GET['coupon_code'] ) );
if ( '' === $coupon_code ) {
return;
}
$cart = WC()->cart;
if ( ! $cart ) {
return;
}
if ( ! $cart->has_discount( $coupon_code ) ) {
$cart->apply_coupon( $coupon_code );
}
}
add_action( 'wp_loaded', 'csv_apply_coupon_code_from_url' );
// Remove links from single product gallery images.
function csv_strip_product_image_links( $html ) {
if ( false === strpos( $html, '<a' ) ) {
return $html;
}
$html = preg_replace( '/<a\\b[^>]*>\\s*/i', '', $html );
$html = preg_replace( '/\\s*<\\/a>\\s*/i', '', $html );
return $html;
}
add_filter( 'woocommerce_single_product_image_thumbnail_html', 'csv_strip_product_image_links', 10 );
add_filter( 'woocommerce_single_product_image_html', 'csv_strip_product_image_links', 10 );
// Disable the Stripe sandbox assistant overlay.
function csv_disable_stripe_sandbox_assistant() {
if ( is_admin() ) {
return;
}
if ( ! wp_script_is( 'stripe', 'registered' ) && ! wp_script_is( 'stripe', 'enqueued' ) ) {
return;
}
$inline_script = <<<'JS'
(function () {
if (typeof window.Stripe !== 'function') {
return;
}
var originalStripe = window.Stripe;
function wrapStripe(key, options) {
var opts = options || {};
var developerTools = opts.developerTools || {};
var assistant = developerTools.assistant || {};
assistant.enabled = false;
developerTools.assistant = assistant;
opts.developerTools = developerTools;
return originalStripe(key, opts);
}
for (var prop in originalStripe) {
if (Object.prototype.hasOwnProperty.call(originalStripe, prop)) {
wrapStripe[prop] = originalStripe[prop];
}
}
window.Stripe = wrapStripe;
})();
JS;
wp_add_inline_script( 'stripe', $inline_script, 'after' );
}
add_action( 'wp_enqueue_scripts', 'csv_disable_stripe_sandbox_assistant', 20 );
// Normalize mixed field/meta values (ID, WP_Post, array) into a valid post ID.
function csv_resolve_linked_post_id( $value, $expected_post_type = '' ) {
$post_id = 0;
if ( $value instanceof WP_Post ) {
$post_id = (int) $value->ID;
} elseif ( is_numeric( $value ) ) {
$post_id = absint( $value );
} elseif ( is_array( $value ) ) {
if ( isset( $value['ID'] ) && is_numeric( $value['ID'] ) ) {
$post_id = absint( $value['ID'] );
} elseif ( isset( $value['id'] ) && is_numeric( $value['id'] ) ) {
$post_id = absint( $value['id'] );
} else {
foreach ( $value as $candidate ) {
$candidate_id = csv_resolve_linked_post_id( $candidate, $expected_post_type );
if ( $candidate_id > 0 ) {
return $candidate_id;
}
}
}
} elseif ( is_string( $value ) && '' !== trim( $value ) ) {
$parts = array_filter( array_map( 'trim', explode( ',', $value ) ) );
foreach ( $parts as $part ) {
if ( is_numeric( $part ) ) {
$post_id = absint( $part );
break;
}
}
}
if ( $post_id <= 0 ) {
return 0;
}
if ( '' !== $expected_post_type && get_post_type( $post_id ) !== $expected_post_type ) {
return 0;
}
return $post_id;
}
// Resolve the linked LearnDash course from product data (ACF + LearnDash Woo meta fallbacks).
function csv_get_product_linked_course_id( $product_id ) {
if ( $product_id <= 0 ) {
return 0;
}
if ( function_exists( 'get_field' ) ) {
$acf_course = get_field( 'courses_post', $product_id );
$course_id = csv_resolve_linked_post_id( $acf_course, 'sfwd-courses' );
if ( $course_id > 0 ) {
return $course_id;
}
}
// LearnDash WooCommerce integration commonly stores linked courses in one of these keys.
$course_meta_keys = array(
'_related_course',
'related_course',
'_learndash_woocommerce_related_course',
'learndash_woocommerce_related_course',
'learndash_woocommerce_courses',
);
foreach ( $course_meta_keys as $meta_key ) {
$meta_value = get_post_meta( $product_id, $meta_key, true );
$course_id = csv_resolve_linked_post_id( $meta_value, 'sfwd-courses' );
if ( $course_id > 0 ) {
return $course_id;
}
$meta_values = get_post_meta( $product_id, $meta_key, false );
if ( ! empty( $meta_values ) ) {
$course_id = csv_resolve_linked_post_id( $meta_values, 'sfwd-courses' );
if ( $course_id > 0 ) {
return $course_id;
}
}
}
return 0;
}
// Resolve the preferred frontend URL for a Woo product in cart contexts.
function csv_get_product_linked_frontend_url( $product_id ) {
if ( $product_id <= 0 ) {
return '';
}
// Prefer linked course pages over product pages.
$course_id = csv_get_product_linked_course_id( $product_id );
if ( $course_id > 0 ) {
$course_url = get_permalink( $course_id );
if ( $course_url ) {
return $course_url;
}
}
// Legacy fallback: linked event page.
if ( function_exists( 'get_field' ) ) {
$event_id = csv_resolve_linked_post_id( get_field( 'event_post', $product_id ) );
if ( $event_id > 0 ) {
$event_url = get_permalink( $event_id );
if ( $event_url ) {
return $event_url;
}
}
}
return '';
}
// Cart/Checkout links: point products to linked course/event pages instead of product pages.
function csv_cart_item_permalink_linked_content( $permalink, $cart_item, $cart_item_key ) {
if ( empty( $cart_item['data'] ) || ! is_a( $cart_item['data'], 'WC_Product' ) ) {
return $permalink;
}
$product = $cart_item['data'];
$product_id = $product->get_id();
if ( $product->is_type( 'variation' ) ) {
$product_id = $product->get_parent_id();
}
$linked_url = csv_get_product_linked_frontend_url( $product_id );
if ( '' !== $linked_url ) {
return $linked_url;
}
return $permalink;
}
add_filter( 'woocommerce_cart_item_permalink', 'csv_cart_item_permalink_linked_content', 10, 3 );
// Safety net: if cart name markup already includes an anchor, force its href to linked content URL.
function csv_cart_item_name_linked_content( $product_name, $cart_item, $cart_item_key ) {
if ( empty( $cart_item['data'] ) || ! is_a( $cart_item['data'], 'WC_Product' ) ) {
return $product_name;
}
$product = $cart_item['data'];
$product_id = $product->get_id();
if ( $product->is_type( 'variation' ) ) {
$product_id = $product->get_parent_id();
}
$linked_url = csv_get_product_linked_frontend_url( $product_id );
if ( '' === $linked_url ) {
return $product_name;
}
if ( false !== stripos( $product_name, '<a ' ) ) {
$product_name = preg_replace( '/href=(["\']).*?\1/i', 'href="' . esc_url( $linked_url ) . '"', $product_name, 1 );
}
return $product_name;
}
add_filter( 'woocommerce_cart_item_name', 'csv_cart_item_name_linked_content', 10, 3 );
// Remove Downloads from My Account navigation.
function csv_remove_account_downloads_menu_item( $items ) {
unset( $items['downloads'] );
return $items;
}
add_filter( 'woocommerce_account_menu_items', 'csv_remove_account_downloads_menu_item' );
// Rename Dashboard to My Courses in My Account navigation.
function csv_rename_account_dashboard_menu_item( $items ) {
if ( isset( $items['dashboard'] ) ) {
$items['dashboard'] = 'My Courses';
}
return $items;
}
add_filter( 'woocommerce_account_menu_items', 'csv_rename_account_dashboard_menu_item', 20 );
// Rename "Browse products" to "View Retreats" on My Account notices.
// function csv_rename_browse_products_text( $translated, $text, $domain ) {
// if ( 'woocommerce' !== $domain ) {
// return $translated;
// }
// if ( 'Browse products' === $text && is_page( 'my-account' ) ) {
// return 'View Retreats';
// }
// return $translated;
// }
// add_filter( 'gettext', 'csv_rename_browse_products_text', 10, 3 );
// oint "return to shop" links to courses.
function csv_return_to_shop_redirect( $url ) {
return home_url( '/courses/' );
}
add_filter( 'woocommerce_return_to_shop_redirect', 'csv_return_to_shop_redirect' );
// Move orders from processing to complete after webhook triggers for successful payment
function auto_complete_paid_orders($order_id) {
$order = wc_get_order($order_id);
if (!$order) {
return;
}
// Only act if order is currently processing
if ($order->has_status('processing')) {
$order->update_status('completed', 'Auto-completed after successful payment.');
}
}
add_action('woocommerce_payment_complete', 'auto_complete_paid_orders');
// Disable messages about the mobile apps in WooCommerce emails
function csv_disable_woocommerce_email_mobile_messages( $mailer ) {
foreach ( $mailer->emails as $email ) {
remove_action( 'woocommerce_email_footer', array( $email, 'mobile_messaging' ), 9 );
}
}
add_action( 'woocommerce_email', 'csv_disable_woocommerce_email_mobile_messages' );
// Add body class to logged out my account pages
function ms_add_logged_out_account_body_class($classes) {
if ( function_exists( 'is_account_page' ) && is_account_page() && ! is_user_logged_in() ) {
$classes[] = 'logged-out';
}
return $classes;
}
add_filter('body_class', 'ms_add_logged_out_account_body_class');
// Add custom page-level body classes from ACF.
function csv_add_acf_page_body_class( $classes ) {
if ( ! is_page() ) {
return $classes;
}
$page_id = get_queried_object_id();
if ( ! $page_id ) {
return $classes;
}
$body_class = function_exists( 'get_field' )
? get_field( 'body_class', $page_id )
: get_post_meta( $page_id, 'body_class', true );
$body_class = trim( (string) $body_class );
if ( '' === $body_class ) {
return $classes;
}
$custom_classes = preg_split( '/\s+/', $body_class );
foreach ( $custom_classes as $custom_class ) {
$custom_class = sanitize_html_class( $custom_class );
if ( '' !== $custom_class ) {
$classes[] = $custom_class;
}
}
return $classes;
}
add_filter( 'body_class', 'csv_add_acf_page_body_class' );
// Redirect to homepage after logout
function csv_logout_redirect_home( $redirect_to, $requested_redirect_to, $user ) {
return home_url( '/' );
}
add_filter( 'logout_redirect', 'csv_logout_redirect_home', 10, 3 );
// WooCommerce handles logout redirects separately.
function csv_woocommerce_logout_redirect_home( $redirect_url ) {
return home_url( '/' );
}
add_filter( 'woocommerce_logout_default_redirect_url', 'csv_woocommerce_logout_redirect_home' );
// Handle free-class checkout links (empty cart, add product, apply coupon, redirect)
// Example URL: https://featherjones.com/checkout/?add-to-cart=2155&coupon_code=freeclass
// Example URL: http://localhost:8080/checkout/?add-to-cart=2155&coupon_code=freeclass
add_action('template_redirect', function () {
// Frontend-only: skip admin and AJAX requests.
if (is_admin() || wp_doing_ajax()) {
return;
}
// Bail if WooCommerce or checkout helpers are unavailable.
if (!function_exists('WC') || !WC()->cart || !function_exists('is_checkout')) {
return;
}
// Only run on the checkout page.
if (!is_checkout()) {
return;
}
// Require both add-to-cart and coupon_code in the query string.
if (empty($_GET['add-to-cart']) || empty($_GET['coupon_code'])) {
return;
}
// Sanitize incoming query values.
$product_id = absint(wp_unslash($_GET['add-to-cart']));
$coupon_code = wc_format_coupon_code(wp_unslash($_GET['coupon_code']));
// Ensure both values are valid.
if (!$product_id || !$coupon_code) {
return;
}
$product = wc_get_product($product_id);
// Only proceed if the product is purchasable.
if (!$product || !$product->is_purchasable()) {
return;
}
// Reset cart to ensure only the free-class product is present.
WC()->cart->empty_cart();
$added = WC()->cart->add_to_cart($product_id);
// Stop if the product could not be added.
if (!$added) {
return;
}
// Apply coupon if it isn't already on the cart.
if (!WC()->cart->has_discount($coupon_code)) {
WC()->cart->apply_coupon($coupon_code);
}
// Redirect to clean checkout URL to avoid reprocessing.
wp_safe_redirect(wc_get_checkout_url());
exit;
}, 20);
// Suppress WooCommerce "added to cart" notices sitewide.
function csv_suppress_add_to_cart_message_html( $message, $products = array(), $show_qty = false ) {
return '';
}
add_filter( 'wc_add_to_cart_message_html', 'csv_suppress_add_to_cart_message_html', 10, 3 );
// Suppress WooCommerce notices on My Account when returning to checkout.
function csv_suppress_checkout_redirect_notices() {
if ( is_admin() || wp_doing_ajax() || ! function_exists( 'is_account_page' ) ) {
return;
}
if ( ! is_account_page() || empty( $_GET['redirect_to'] ) ) {
return;
}
if ( ! function_exists( 'WC' ) || ! WC()->session ) {
return;
}
$redirect_to = esc_url_raw( wp_unslash( $_GET['redirect_to'] ) );
if ( false === strpos( $redirect_to, '/checkout' ) ) {
return;
}
wc_clear_notices();
}
add_action( 'template_redirect', 'csv_suppress_checkout_redirect_notices', 9 );
// WooCommerce asset loading:
// Keep official Woo resources on real commerce screens, but remove them from
// normal marketing/content pages where they add weight without powering UI.
function csv_is_woocommerce_asset_context() {
if ( is_admin() || wp_doing_ajax() || ! function_exists( 'WC' ) ) {
return true;
}
$is_woocommerce = function_exists( 'is_woocommerce' ) && is_woocommerce();
$is_cart = function_exists( 'is_cart' ) && is_cart();
$is_checkout = function_exists( 'is_checkout' ) && is_checkout();
$is_account = function_exists( 'is_account_page' ) && is_account_page();
return $is_woocommerce || $is_cart || $is_checkout || $is_account;
}
function csv_should_load_woocommerce_block_assets( $should_load ) {
if ( csv_is_woocommerce_asset_context() ) {
return $should_load;
}
return false;
}
add_filter( 'woocommerce_should_load_block_assets', 'csv_should_load_woocommerce_block_assets' );
function csv_should_load_woocommerce_block_styles( $should_load ) {
if ( csv_is_woocommerce_asset_context() ) {
return $should_load;
}
return false;
}
add_filter( 'woocommerce_should_load_block_styles', 'csv_should_load_woocommerce_block_styles' );
function csv_filter_woocommerce_enqueue_styles( $styles ) {
if ( csv_is_woocommerce_asset_context() ) {
return $styles;
}
return array();
}
add_filter( 'woocommerce_enqueue_styles', 'csv_filter_woocommerce_enqueue_styles' );
function csv_dequeue_woocommerce_offscreen_assets() {
if ( is_admin() ) {
return;
}
// The header does not need live cart-count refreshes, so cart fragments can
// be disabled globally while cart/checkout still work through normal loads.
wp_dequeue_script( 'wc-cart-fragments' );
if ( csv_is_woocommerce_asset_context() ) {
return;
}
$style_handles = array(
'woocommerce-general',
'woocommerce-layout',
'woocommerce-smallscreen',
'woocommerce-block-style',
'wc-block-style',
'wc-blocks-style',
'wc-blocks-vendors-style',
);
foreach ( $style_handles as $handle ) {
wp_dequeue_style( $handle );
}
$script_handles = array(
'woocommerce',
'wc-add-to-cart',
'wc-add-to-cart-variation',
'wc-single-product',
'wc-cart',
'wc-checkout',
'wc-jquery-blockui',
'wc-js-cookie',
);
foreach ( $script_handles as $handle ) {
wp_dequeue_script( $handle );
}
}
add_action( 'wp_enqueue_scripts', 'csv_dequeue_woocommerce_offscreen_assets', 100 );
function csv_disable_woocommerce_cart_fragments_data( $params, $handle ) {
if ( 'wc-cart-fragments' === $handle ) {
return null;
}
return $params;
}
add_filter( 'woocommerce_get_script_data', 'csv_disable_woocommerce_cart_fragments_data', 10, 2 );
//////////////////////////////////////////////////
////////////////// LearnDash /////////////////////
//////////////////////////////////////////////////
// LearnDash asset loading:
// LearnDash enqueues a broad frontend bundle on pages that do not need course
// UI. Keep those official assets only where students browse or consume courses.
function csv_is_learndash_asset_context() {
if ( is_admin() || wp_doing_ajax() ) {
return true;
}
// The WooCommerce account dashboard includes course overview content.
if ( is_page( 'account' ) ) {
return true;
}
// Keep assets on the course catalog and all course detail URLs.
$request_path = trim( (string) parse_url( $_SERVER['REQUEST_URI'] ?? '', PHP_URL_PATH ), '/' );
if ( 'courses' === $request_path || 0 === strpos( $request_path, 'courses/' ) ) {
return true;
}
// Direct lesson/topic/quiz URLs still need LearnDash behavior even if their
// public permalink does not live under /courses/.
$learndash_post_types = array(
'sfwd-courses',
'sfwd-lessons',
'sfwd-topic',
'sfwd-quiz',
'sfwd-assignment',
'sfwd-essays',
);
if ( is_singular( $learndash_post_types ) ) {
return true;
}
return false;
}
function csv_dequeue_learndash_offscreen_assets() {
if ( csv_is_learndash_asset_context() ) {
return;
}
$style_handles = array(
'learndash_quiz_front_css',
'dashicons',
'learndash',
'jquery-dropdown-css',
'learndash_lesson_video',
'learndash-admin-bar',
'learndash-course-grid-skin-grid',
'learndash-course-grid-pagination',
'learndash-course-grid-filter',
'learndash-course-grid-card-grid-1',
'learndash-front',
);
foreach ( $style_handles as $handle ) {
wp_dequeue_style( $handle );
}
$script_handles = array(
'learndash-course-grid-skin-grid',
'learndash',
'learndash-main',
'learndash-breakpoints',
'learndash-front',
);
foreach ( $script_handles as $handle ) {
wp_dequeue_script( $handle );
}
}
add_action( 'wp_enqueue_scripts', 'csv_dequeue_learndash_offscreen_assets', 100 );
// LearnDash may enqueue some assets late, so catch both enqueue and print time.
add_action( 'wp_print_styles', 'csv_dequeue_learndash_offscreen_assets', 100 );
add_action( 'wp_print_scripts', 'csv_dequeue_learndash_offscreen_assets', 100 );
// Remove LearnDash meta box from pages and posts
add_action('do_meta_boxes', function () {
foreach (['post', 'page', 'product', 'events'] as $screen) {
remove_meta_box('learndash-course-grid-meta-box', $screen, 'advanced');
remove_meta_box('learndash-course-grid-meta-box', $screen, 'normal');
remove_meta_box('learndash-course-grid-meta-box', $screen, 'side');
}
}, 999);
// Point LearnDash login links to the My Account page.
function csv_learndash_login_url( $login_url, $context, $args ) {
if ( 'login' !== $context ) {
return $login_url;
}
return home_url( '/account/' );
}
add_filter( 'learndash_login_url', 'csv_learndash_login_url', 10, 3 );
// Ensure LearnDash prices use currency symbols even when intl is missing.
function csv_learndash_currency_symbol_fallback( $symbol ) {
if ( ! function_exists( 'learndash_get_currency_code' ) ) {
return $symbol;
}
$currency_code = strtoupper( trim( learndash_get_currency_code() ) );
if ( '' === $currency_code ) {
return $symbol;
}
if ( 'USD' === $currency_code ) {
return '$';
}
return $symbol;
}
add_filter( 'learndash_currency_symbol', 'csv_learndash_currency_symbol_fallback' );
// Add priority field to LearnDash course tags (ld_course_tag).
function csv_ld_course_tag_add_priority_field() {
?>
<div class="form-field term-priority-wrap">
<label for="ld_course_tag_priority">Tag Priority</label>
<input type="number" name="ld_course_tag_priority" id="ld_course_tag_priority" value="" min="0" step="1">
<p class="description">Higher numbers will be treated as higher priority.</p>
</div>
<?php
}
add_action( 'ld_course_tag_add_form_fields', 'csv_ld_course_tag_add_priority_field' );
function csv_ld_course_tag_edit_priority_field( $term ) {
$priority = get_term_meta( $term->term_id, 'ld_course_tag_priority', true );
?>
<tr class="form-field term-priority-wrap">
<th scope="row"><label for="ld_course_tag_priority">Tag Priority</label></th>
<td>
<input type="number" name="ld_course_tag_priority" id="ld_course_tag_priority" value="<?php echo esc_attr( $priority ); ?>" min="0" step="1">
<p class="description">Higher numbers will be treated as higher priority.</p>
</td>
</tr>
<?php
}
add_action( 'ld_course_tag_edit_form_fields', 'csv_ld_course_tag_edit_priority_field' );
function csv_ld_course_tag_save_priority( $term_id ) {
if ( ! current_user_can( 'manage_categories' ) ) {
return;
}
if ( ! isset( $_POST['ld_course_tag_priority'] ) ) {
return;
}
$priority = intval( $_POST['ld_course_tag_priority'] );
update_term_meta( $term_id, 'ld_course_tag_priority', $priority );
}
add_action( 'created_ld_course_tag', 'csv_ld_course_tag_save_priority' );
add_action( 'edited_ld_course_tag', 'csv_ld_course_tag_save_priority' );
// Show Tag Priority column on Course Tags list table.
function csv_ld_course_tag_add_priority_column( $columns ) {
$ordered = array();
foreach ( $columns as $key => $label ) {
$ordered[ $key ] = $label;
if ( 'name' === $key ) {
$ordered['ld_course_tag_priority'] = 'Priority';
}
}
if ( ! isset( $ordered['ld_course_tag_priority'] ) ) {
$ordered['ld_course_tag_priority'] = 'Priority';
}
return $ordered;
}
add_filter( 'manage_edit-ld_course_tag_columns', 'csv_ld_course_tag_add_priority_column' );
function csv_ld_course_tag_render_priority_column( $content, $column_name, $term_id ) {
if ( 'ld_course_tag_priority' !== $column_name ) {
return $content;
}
$priority = get_term_meta( $term_id, 'ld_course_tag_priority', true );
if ( '' === $priority ) {
return '—';
}
return esc_html( $priority );
}
add_filter( 'manage_ld_course_tag_custom_column', 'csv_ld_course_tag_render_priority_column', 10, 3 );
// Add Tag Priority to Quick Edit for course tags.
function csv_ld_course_tag_quick_edit_field( $column_name, $screen, $taxonomy ) {
if ( 'ld_course_tag_priority' !== $column_name || 'ld_course_tag' !== $taxonomy ) {
return;