-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunctions.php
More file actions
667 lines (544 loc) · 21.1 KB
/
functions.php
File metadata and controls
667 lines (544 loc) · 21.1 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
<?php
use Themosis\Core\Application;
/*
|--------------------------------------------------------------------------
| Bootstrap Theme
|--------------------------------------------------------------------------
|
| We bootstrap the theme. The following code is loading your theme
| configuration files and register theme images sizes, menus, sidebars,
| theme support features and templates.
|
*/
$theme = (Application::getInstance())->loadTheme(__DIR__, 'config');
/*
|--------------------------------------------------------------------------
| Theme i18n | l10n
|--------------------------------------------------------------------------
|
| Registers the "languages" directory for storing the theme translations.
|
| The "THEME_TD" constant is defined during bootstrap and its value is
| set based on the "style.css" [Text Domain] property located into
| the file header.
|
*/
load_theme_textdomain(
THEME_TD,
$theme->getPath($theme->getHeader('domain_path'))
);
/*
|--------------------------------------------------------------------------
| Theme assets locations
|--------------------------------------------------------------------------
|
| You can define your theme assets paths and URLs. You can add as many
| locations as you want. The key is your asset directory path and
| the value is its public URL.
|
*/
$theme->assets([
$theme->getPath('dist') => $theme->getUrl('dist')
]);
/*
|--------------------------------------------------------------------------
| Theme Views
|--------------------------------------------------------------------------
|
| Register theme view paths. By default, the theme is registering
| the "views" directory but you can add as many directories as you want
| from the theme.php configuration file.
|
*/
$theme->views($theme->config('theme.views', []));
/*
|--------------------------------------------------------------------------
| Theme Service Providers
|--------------------------------------------------------------------------
|
| Register theme service providers. You can manage the list of
| services providers through the theme.php configuration file.
|
*/
$theme->providers($theme->config('theme.providers', []));
/*
|--------------------------------------------------------------------------
| Theme includes
|--------------------------------------------------------------------------
|
| Auto includes files by providing one or more paths. By default, we setup
| an "inc" directory within the theme. Use that "inc" directory to extend
| your theme features. Nested files are also included.
|
*/
$theme->includes([
$theme->getPath('inc')
]);
/*
|--------------------------------------------------------------------------
| Theme Image Sizes
|--------------------------------------------------------------------------
|
| Register theme image sizes. Image sizes are configured in your theme
| images.php configuration file.
|
*/
$theme->images($theme->config('images'));
/*
|--------------------------------------------------------------------------
| Theme Menu Locations
|--------------------------------------------------------------------------
|
| Register theme menu locations. Menu locations are configured in your theme
| menus.php configuration file.
|
*/
$theme->menus($theme->config('menus'));
/*
|--------------------------------------------------------------------------
| Theme Sidebars
|--------------------------------------------------------------------------
|
| Register theme sidebars. Sidebars are configured in your theme
| sidebars.php configuration file.
|
*/
$theme->sidebars($theme->config('sidebars'));
/*
|--------------------------------------------------------------------------
| Theme Support
|--------------------------------------------------------------------------
|
| Register theme support. Support features are configured in your theme
| support.php configuration file.
|
*/
$theme->support($theme->config('support', []));
/*
|--------------------------------------------------------------------------
| Theme Templates
|--------------------------------------------------------------------------
|
| Register theme templates. Templates are configured in your theme
| templates.php configuration file.
|
*/
$theme->templates($theme->config('templates', []));
//Woocommerce actions
add_theme_support( 'post-thumbnails' );
//add_action( 'woocommerce_before_shop_loop', function () {
// $terms = get_terms( 'product_cat', [
// 'parent' => get_queried_object_id(),
// 'exclude' => [
// '24', //term id of 'uncategorized'
// ],
// ]);
//
// if ($terms) {
// echo view('components.slider.grid', [
// 'items' => $terms,
// 'card_type' => 'category',
// 'grid_class' => 'product-cats w-full',
// ])->render();
//
// echo '<h2 id="products-start" class="text-36 font-head mt-4 lg:mt-0 lg:pt-20 lg:pb-12">Alle producten</h2>';
// }
//}, 50);
add_filter( 'loop_shop_per_page', function ($number_of_posts) {
return 12; //products per page
}, 20);
add_filter('loop_shop_columns', function () {
return 3; //product-columns per page
}, 1); //DOESNT work, see the _product_archive.scss styling for .products as temp fix.
remove_action('woocommerce_before_shop_loop_item_title', 'woocommerce_template_loop_product_thumbnail');
add_filter('woocommerce_product_loop_title_classes', function ($class) {
return $class .' h5 text-black pb-6';
});
// See content-single-product.php # 49
add_action('woocommerce_single_product_summary', function() {
global $product;
echo '<span class="h5 pb-2">'. $product->get_attribute('brand') .'</span>';
}, 3);
add_action('woocommerce_single_product_summary', function() {
global $product;
echo view('woocommerce.single-product.additional-summary', ['product' => $product])->render();
}, 8);
//add_action('woocommerce_single_product_summary', function() {
// global $product;
// echo '<div class="text-xs font-bold pt-2 pb-8">'. wc_price(wc_get_price_excluding_tax($product)) .' excl. BTW</div>';
//}, 11);
remove_action('woocommerce_single_product_summary','woocommerce_template_single_meta', 40);
add_action('woocommerce_single_product_summary', function() {
global $product;
echo view('woocommerce.single-product.technical-sheet', ['product' => $product])->render();
}, 20);
add_action('woocommerce_single_product_summary', function() {
global $product;
echo view('woocommerce.single-product.quote', ['product' => $product])->render();
}, 60);
add_filter('woocommerce_product_single_add_to_cart_text', function () {
return 'In winkelwagen';
});
add_filter( 'woocommerce_page_title', function ( $page_title ) {
$page_title = str_replace('“', '"', $page_title);
$page_title = str_replace('”', '"', $page_title);
return $page_title;
}, 10, 2 ); //Fix quotes on search page title. Would otherwise show the literal strings.
//Move the description tabs from <after the summary> to <after the thumbnails>
remove_action('woocommerce_after_single_product_summary', 'woocommerce_output_product_data_tabs');
add_action( 'woocommerce_before_single_product_summary', 'woocommerce_output_product_data_tabs', 99 );
// Align the 'add to cart' button on the right
add_action('woocommerce_before_add_to_cart_form', function() {
echo '<div class="flex justify-end">';
});
add_action('woocommerce_after_add_to_cart_form', function() {
echo '</div>';
});
add_action('woocommerce_before_cart_table', function() {
echo '<h2 class="cart-title">'. __('Cart', 'woocommerce') .'</h2>';
});
add_action('woocommerce_cart_is_empty', function() {
echo '<h2 class="cart-title">'. __('Cart', 'woocommerce') .'</h2>';
}, 1);
add_action('woocommerce_before_add_to_cart_button', function() {
echo '<input type="hidden" name="_token" value="'. csrf_token() .'">';
});
add_filter('woocommerce_dropdown_variation_attribute_options_args',function ( $args)
{
if(count($args['options']) === 1) { //Check the count of available options in dropdown
$args['selected'] = $args['options'][0];
}
return $args;
},10,1);
add_action('init', function () {
remove_action('woocommerce_before_main_content', 'woocommerce_breadcrumb', 20, 0);
});
add_filter( 'woocommerce_breadcrumb_defaults', function () {
return array(
'delimiter' => '<span class="breadcrumb-separator">/</span>',
'wrap_before' => '<nav class="woocommerce-breadcrumb" itemprop="breadcrumb">',
'wrap_after' => '</nav>',
'before' => '',
'after' => '',
'home' => _x( 'Home', 'breadcrumb', 'woocommerce' ),
);
});
add_action('woocommerce_archive_description', function() {
woocommerce_breadcrumb();
}, 1);
add_action('woocommerce_before_single_product', function() {
echo view('woocommerce.single-product.breadcrumbs')->render();
}, 1);
add_action('wp', function () {
remove_theme_support( 'wc-product-gallery-zoom' ); //also removes zoom icon on main image.
}, 99 );
add_filter( 'woocommerce_checkout_fields' , function ( $fields ) {
unset($fields['billing']['billing_address_2']);
unset($fields['shipping']['shipping_address_2']);
return $fields;
// unset($fields['billing']['billing_first_name']);
// unset($fields['billing']['billing_last_name']);
// unset($fields['billing']['billing_company']);
// unset($fields['billing']['billing_address_1']);
//
// unset($fields['billing']['billing_city']);
// unset($fields['billing']['billing_postcode']);
// unset($fields['billing']['billing_country']);
// unset($fields['billing']['billing_state']);
// unset($fields['billing']['billing_phone']);
// unset($fields['order']['order_comments']);
// unset($fields['billing']['billing_email']);
// unset($fields['account']['account_username']);
// unset($fields['account']['account_password']);
// unset($fields['account']['account_password-2']);
} );
add_filter( 'woocommerce_reset_variations_link', function ($html){
return '<a class="reset_variations" href="#">' . esc_html__( 'Selectie wissen', 'woocommerce' ) . '</a>';
});
add_filter( 'woocommerce_ajax_variation_threshold', function() { return 200; } );
add_action('woocommerce_before_cart', function (){
echo sprintf('<a href="%s" class="no-underline text-sm hover:text-primary mb-4 inline-block"><i class="fa-solid fa-chevron-left pr-2 text-xs"></i> Verder winkelen</a>', get_permalink( wc_get_page_id( 'shop' ) ));
});
// Fix for redirection adding two slashes in front of redirect.
add_action('redirection_url_target', function ($url){
if(str_starts_with($url, '//')) {
return str_replace(substr($url, 0, 2), '/', $url);
}
return $url;
}, 999);
// Always show sku of product in mail
add_filter( 'woocommerce_email_order_items_args', function ($args){
$args['show_sku'] = true;
return $args;
}, 10, 2 );
/**
* @snippet Show SKU @ WooCommerce Cart
* @how-to Get CustomizeWoo.com FREE
* @author Rodolfo Melogli
* @testedwith WooCommerce 5
* @donate $9 https://businessbloomer.com/bloomer-armada/
*/
// First, let's write the function that returns a given product SKU
function renderSku( $product ) {
$sku = $product->get_sku();
if ( ! empty( $sku ) ) {
return '<span class="inline-block pl-2"> - ' . $sku . '</span>';
} else {
return '';
}
}
// This adds the SKU under cart/checkout item name
add_filter( 'woocommerce_cart_item_name', 'sku_cart_checkout_pages', 9999, 3 );
function sku_cart_checkout_pages( $item_name, $cart_item, $cart_item_key ) {
$product = $cart_item['data'];
$item_name .= renderSku( $product );
return $item_name;
}
add_filter('woocommerce_product_variation_get_sku', function ($sku){
preg_match_all('/\.(.*)/m', $sku, $matches, PREG_SET_ORDER, 0);
if(isset($matches[0], $matches[0][1])) {
return $matches[0][1];
}
return $sku;
});
function hideFlatRateWhenFreeIsAvailable( $rates ) : array
{
$shipmentRateKey = '';
$removeOtherShipmentMethod = false;
foreach ( $rates as $rateId => $rate ) {
if ( 'free_shipping' === $rate->get_method_id() ) {
$removeOtherShipmentMethod = true;
continue;
}
if ( 'flat_rate' === $rate->get_method_id() ) {
$shipmentRateKey = $rateId;
}
}
if(true === $removeOtherShipmentMethod && $shipmentRateKey) {
unset($rates[$shipmentRateKey]);
}
return $rates;
}
add_filter( 'woocommerce_package_rates', 'hideFlatRateWhenFreeIsAvailable', 100 );
//if ( !is_plugin_active( 'woocommerce/woocommerce.php' ) ) {
// function wc_get_page_id() {
// return '';
// }
//}
// Enable the login form by default for unlogged users
add_action( 'woocommerce_before_checkout_form', 'force_checkout_login_for_unlogged_customers', 4 );
function force_checkout_login_for_unlogged_customers() {
if( ! is_user_logged_in() ) {
remove_action( 'woocommerce_before_checkout_form', 'woocommerce_checkout_login_form', 10 );
}
}
remove_action('woocommerce_checkout_order_review', 'woocommerce_checkout_payment', 20 );
add_filter('woocommerce_cart_shipping_method_full_label', function ($label, WC_Shipping_Rate $method){
if($method->get_method_id() === 'local_pickup' || $method->get_method_id() === 'free_shipping') {
$label .= '<span class="woocommerce-Price-amount amount"><bdi>Gratis</bdi></span>';
}
return $label;
}, 5, 2);
/**
* Accepts a zone name and returns its threshold for free shipping.
*
* @param $zone_name The name of the zone to get the threshold of. Case-sensitive.
* @return int The threshold corresponding to the zone, if there is any. If there is no such zone, or no free shipping method, null will be returned.
*/
function get_free_shipping_minimum($zone_name = 'Netherlands') {
if ( ! isset( $zone_name ) ) return null;
$result = null;
$zone = null;
$zones = WC_Shipping_Zones::get_zones();
foreach ( $zones as $z ) {
if ( $z['zone_name'] == $zone_name ) {
$zone = $z;
}
}
if ( $zone ) {
$shipping_methods_nl = $zone['shipping_methods'];
$free_shipping_method = null;
foreach ( $shipping_methods_nl as $method ) {
if ( $method->id == 'free_shipping' ) {
$free_shipping_method = $method;
break;
}
}
if ( $free_shipping_method ) {
$result = $free_shipping_method->min_amount;
}
}
return $result;
}
// Disable WP rocket comments in files
define ('WP_ROCKET_WHITE_LABEL_FOOTPRINT', false);
/**
* Set 'with_front' to false for the 'experts' post type.
*/
add_filter( 'register_post_type_args', function( $args, $post_type )
{
if( 'experts' === $post_type && is_array( $args ) ) {
$args['rewrite'] = [
'with_front' => false,
];
}
return $args;
}, 99, 2 );
add_filter('rank_math/frontend/breadcrumb/html', function ($html){
return str_replace(['Home', 'home', 'HOME'], '', $html);
});
/** *
* Allow editing the robots.txt & htaccess data. *
* @param bool Can edit the robots & htaccess data.
*/
add_filter( 'rank_math/can_edit_file', '__return_true' );
// Hide Wordpress version
remove_action('wp_head', 'wp_generator');
function add_excerpt_to_pages() {
add_post_type_support('page', 'excerpt');
}
add_action('init', 'add_excerpt_to_pages');
// Add custom class to the body_class
function add_sitename_to_body_class( $classes ) {
$sitename = sanitize_title( get_bloginfo( 'name' ) );
$classes[] = $sitename;
return $classes;
}
add_filter( 'body_class', 'add_sitename_to_body_class' );
// Remove WordPress Meta Data
// Remove WordPress version from head
remove_action('wp_head', 'wp_generator');
// Remove WordPress version from RSS feeds
add_filter('the_generator', '__return_empty_string');
function remove_wp_meta_tags() {
remove_action('wp_head', 'rsd_link'); // Really Simple Discovery link
remove_action('wp_head', 'wlwmanifest_link'); // Windows Live Writer manifest link
remove_action('wp_head', 'wp_shortlink_wp_head'); // Shortlink
remove_action('wp_head', 'rest_output_link_wp_head'); // REST API link
remove_action('wp_head', 'wp_oembed_add_discovery_links'); // oEmbed links
remove_action('wp_head', 'wp_oembed_add_host_js'); // oEmbed JavaScript
remove_action('wp_head', 'adjacent_posts_rel_link_wp_head', 10, 0); // Previous/Next post links
}
add_action('init', 'remove_wp_meta_tags');
function remove_version_from_assets($src) {
if (strpos($src, 'ver=')) {
$src = remove_query_arg('ver', $src);
}
return $src;
}
add_filter('style_loader_src', 'remove_version_from_assets', 9999);
add_filter('script_loader_src', 'remove_version_from_assets', 9999);
function disable_wp_emojis() {
remove_action('wp_head', 'print_emoji_detection_script', 7);
remove_action('wp_print_styles', 'print_emoji_styles');
remove_action('admin_print_scripts', 'print_emoji_detection_script');
remove_action('admin_print_styles', 'print_emoji_styles');
remove_filter('the_content_feed', 'wp_staticize_emoji');
remove_filter('comment_text_rss', 'wp_staticize_emoji');
remove_filter('wp_mail', 'wp_staticize_emoji_for_email');
}
add_action('init', 'disable_wp_emojis');
/**
* Disable RSS Feeds based on ACF setting
*/
function maybe_disable_rss_feeds() {
$rss_feeds_settings = get_field('rss_feeds', 'option');
if (empty($rss_feeds_settings) || empty($rss_feeds_settings['disable_rss'])) {
return;
}
$disable_rss_callback = function() {
wp_die( __( 'No feed available, please visit the <a href="'. esc_url( home_url( '/' ) ) .'">homepage</a>!' ) );
};
add_action('do_feed', $disable_rss_callback, 1);
add_action('do_feed_rdf', $disable_rss_callback, 1);
add_action('do_feed_rss', $disable_rss_callback, 1);
add_action('do_feed_rss2', $disable_rss_callback, 1);
add_action('do_feed_atom', $disable_rss_callback, 1);
add_action('do_feed_rss2_comments', $disable_rss_callback, 1);
add_action('do_feed_atom_comments', $disable_rss_callback, 1);
// Remove RSS feed links from header
remove_action('wp_head', 'feed_links', 2);
remove_action('wp_head', 'feed_links_extra', 3);
}
add_action('init', 'maybe_disable_rss_feeds');
/**
* Calculate the estimated reading time of a post by counting words in Gutenberg blocks.
*
* @param WP_Post|int $post The post object or post ID.
* @param int $wordsPerMinute The average reading speed. Default is 238 words per minute.
*
* @return int Estimated reading time in minutes.
*/
function getReadingTime(int|WP_Post $post, int $wordsPerMinute = 238): int
{
// Do not render in WP Json because the content filter has memory leaks in WP admin
if(str_contains(request()->url(), '/wp-json')) {
return 0;
}
if (is_numeric($post)) {
$post = get_post($post);
}
if (!$post instanceof WP_Post) {
return 0;
}
if(!$post->post_content) {
return 0;
}
$content = apply_filters('the_content', $post->post_content);
if (empty($content)) {
return 0;
}
$textContent = trim(preg_replace('/\s+/', ' ', wp_strip_all_tags($content)));
return max(1, ceil(str_word_count($textContent) / $wordsPerMinute));
}
add_filter('acf/load_field/name=cardblock_post_type', 'acf_load_cardblock_post_types');
function acf_load_cardblock_post_types($field) {
// Haal alle publieke custom post types op (geen ingebouwde zoals post/page)
$args = [
'public' => true,
'_builtin' => false
];
$post_types = get_post_types($args, 'objects');
// Voeg optioneel 'post' en 'page' toe als je die ook wil tonen:
$post_types['post'] = get_post_type_object('post');
$post_types['page'] = get_post_type_object('page');
// Reset keuzes
$field['choices'] = [];
// Voeg CPT’s toe aan het selectveld
foreach ($post_types as $post_type) {
$field['choices'][$post_type->name] = $post_type->labels->singular_name;
}
return $field;
}
add_filter('acf/load_field/name=category', 'acf_load_cardblock_categories');
function acf_load_cardblock_categories($field) {
$field['choices'] = [];
$post_types = get_post_types(['public' => true], 'objects');
foreach ($post_types as $post_type_name => $post_type_object) {
// Sla post types over die we niet willen (optioneel)
if (in_array($post_type_name, ['attachment', 'revision', 'nav_menu_item', 'custom_css', 'customize_changeset', 'oembed_cache', 'user_request', 'wp_block', 'wp_template', 'wp_template_part', 'wp_navigation'])) {
continue;
}
$taxonomies = get_object_taxonomies($post_type_name, 'objects');
foreach ($taxonomies as $taxonomy_name => $taxonomy_object) {
if (!$taxonomy_object->public || !$taxonomy_object->show_ui) {
continue;
}
$terms = get_terms([
'taxonomy' => $taxonomy_name,
'hide_empty' => false,
]);
if (!is_wp_error($terms) && !empty($terms)) {
$group_label = $post_type_object->labels->singular_name . ' - ' . $taxonomy_object->labels->singular_name;
foreach ($terms as $term) {
$field['choices'][$group_label][$term->term_id] = $term->name;
}
}
}
}
return $field;
}
/**
* Custom translation file located in htdocs/content/languages
*/
load_theme_textdomain('themosis', WP_CONTENT_DIR . '/languages');