-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathwp-menu-cart.php
executable file
·900 lines (769 loc) · 29.1 KB
/
wp-menu-cart.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
<?php
/**
* Plugin Name: WP Menu Cart
* Plugin URI: https://wpovernight.com/downloads/menu-cart-pro/
* Description: Extension for your e-commerce plugin (WooCommerce or Easy Digital Downloads) that places a cart icon with number of items and total cost in the menu bar. Activate the plugin, set your options and you're ready to go! Will automatically conform to your theme styles.
* Version: 2.14.10
* Author: WP Overnight
* Author URI: https://wpovernight.com/
* License: GPLv2 or later
* License URI: https://opensource.org/licenses/gpl-license.php
* Text Domain: wp-menu-cart
* WC requires at least: 3.0
* WC tested up to: 9.4
*/
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly
}
if ( ! class_exists( 'WpMenuCart' ) && ! class_exists( 'WPO_Menu_Cart_Pro' ) ) :
class WpMenuCart {
/**
* @var string
*/
protected $plugin_version = '2.14.10';
/**
* @var string
*/
public $plugin_slug;
/**
* @var string
*/
public $plugin_basename;
/**
* @var array
*/
public $options;
/**
* @var string
*/
public $asset_suffix;
/**
* @var WpMenuCart_Settings
*/
public $settings;
/**
* @var WPMenuCart_WooCommerce|WPMenuCart_EDD
*/
public $shop;
/**
* @var array
*/
public $menu_items;
/**
* @var WpMenuCart
*/
protected static $_instance = null;
/**
* Main Plugin Instance
*
* Ensures only one instance of plugin is loaded or can be loaded.
*/
public static function instance() {
if ( is_null( self::$_instance ) ) {
self::$_instance = new self();
}
return self::$_instance;
}
/**
* Construct.
*/
public function __construct() {
$this->plugin_slug = basename( dirname( __FILE__ ) );
$this->plugin_basename = plugin_basename( __FILE__ );
$this->options = get_option( 'wpmenucart', array() );
$this->asset_suffix = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ? '' : '.min';
$this->define( 'WPMENUCART_VERSION', $this->plugin_version );
// load the localisation & classes
add_action( 'init', array( &$this, 'wpml' ), 0 );
add_action( 'init', array( &$this, 'languages' ), 8 );
add_action( 'init', array( $this, 'load_classes' ), 9 );
add_filter( 'load_textdomain_mofile', array( $this, 'textdomain_fallback' ), 10, 2 );
// enqueue scripts & styles
add_action( 'admin_enqueue_scripts', array( &$this, 'load_admin_assets' ) );
add_action( 'wp_enqueue_scripts', array( &$this, 'load_frontend_assets' ) );
add_action( 'init', array( &$this, 'register_cart_navigation_block' ) ); // register cart navigation block
add_action( 'wp_default_styles', array( &$this, 'load_block_editor_styles' ), 99 ); // load block editor styles
// AJAX
add_action( 'wp_ajax_wpmenucart_ajax', array( &$this, 'wpmenucart_ajax' ), 0 );
add_action( 'wp_ajax_nopriv_wpmenucart_ajax', array( &$this, 'wpmenucart_ajax' ), 0 );
// add filters to selected menus to add cart item <li>
add_action( 'init', array( $this, 'filter_nav_menus' ) );
// $this->filter_nav_menus();
// HPOS compatibility
add_action( 'before_woocommerce_init', array( $this, 'woocommerce_hpos_compatible' ) );
add_action( 'woocommerce_blocks_enqueue_cart_block_scripts_after', array( $this, 'wc_block_support_script' ) );
add_action( 'woocommerce_blocks_enqueue_checkout_block_scripts_after', array( $this, 'wc_block_support_script' ) );
}
/**
* Define constant if not already set
* @param string $name
* @param string|bool $value
*/
private function define( $name, $value ) {
if ( ! defined( $name ) ) {
define( $name, $value );
}
}
/**
* Load classes
* @return void
*/
public function load_classes() {
include_once( 'includes/wpmenucart-settings.php' );
$this->settings = new WpMenuCart_Settings();
if ( $this->good_to_go() ) {
if ( isset( $this->options['shop_plugin'] ) ) {
if ( false === $this->is_shop_active( $this->options['shop_plugin'] ) ) {
return;
}
switch ( $this->options['shop_plugin'] ) {
case 'woocommerce':
include_once( 'includes/wpmenucart-woocommerce.php' );
$this->shop = new WPMenuCart_WooCommerce();
if ( ! isset( $this->options['builtin_ajax'] ) ) {
add_filter( 'woocommerce_add_to_cart_fragments', array( $this, 'woocommerce_ajax_fragments' ) );
}
break;
case 'easy-digital-downloads':
case 'easy-digital-downloads-pro':
include_once( 'includes/wpmenucart-edd.php' );
$this->shop = new WPMenuCart_EDD();
if ( ! isset( $this->options['builtin_ajax'] ) ) {
add_action("wp_enqueue_scripts", array( &$this, 'load_edd_ajax' ), 0 );
}
break;
}
if ( isset( $this->options['builtin_ajax'] ) ) {
add_action( 'wp_enqueue_scripts', array( &$this, 'load_custom_ajax' ), 0 );
}
}
}
}
/**
* Check if a shop is active or if conflicting old versions of the plugin are active
* @return boolean
*/
public function good_to_go() {
$wpmenucart_shop_check = get_option( 'wpmenucart_shop_check' );
// check for shop plugins
if ( ! $this->is_shop_active() && 'hide' !== $wpmenucart_shop_check ) {
add_action( 'admin_notices', array ( $this, 'need_shop' ) );
return FALSE;
}
// check for old versions
if ( count( $this->get_active_old_versions() ) > 0 ) {
add_action( 'admin_notices', array ( $this, 'woocommerce_version_active' ) );
return FALSE;
}
// we made it! good to go :o)
return TRUE;
}
/**
* Return true if one ore more shops are activated.
* @return boolean
*/
public function is_shop_active( $shop = '' ) {
if ( empty($shop) ) {
if ( count( $this->get_active_shops() ) > 0 ) {
return TRUE;
} else {
return FALSE;
}
} else {
switch ( $shop ) {
case 'woocommerce':
return function_exists('WC');
break;
case 'easy-digital-downloads':
case 'easy-digital-downloads-pro':
return function_exists('EDD');
break;
default:
return false;
break;
}
}
}
/**
* Get an array of all active plugins, including multisite
* @return array active plugin paths
*/
public static function get_active_plugins() {
$active_plugins = (array) apply_filters( 'active_plugins', get_option( 'active_plugins' ) );
if ( is_multisite() ) {
// get_site_option( 'active_sitewide_plugins', array() ) returns a 'reversed list'
// like [hello-dolly/hello.php] => 1369572703 so we do array_keys to make the array
// compatible with $active_plugins
$active_sitewide_plugins = (array) array_keys( get_site_option( 'active_sitewide_plugins', array() ) );
// merge arrays and remove doubles
$active_plugins = (array) array_unique( array_merge( $active_plugins, $active_sitewide_plugins ) );
}
return $active_plugins;
}
/**
* Get array of active shop plugins
*
* @return array plugin name => plugin path
*/
public static function get_active_shops() {
$active_plugins = self::get_active_plugins();
$shop_plugins = array(
'WooCommerce' => 'woocommerce/woocommerce.php',
'Easy Digital Downloads' => 'easy-digital-downloads/easy-digital-downloads.php',
'Easy Digital Downloads Pro' => 'easy-digital-downloads-pro/easy-digital-downloads.php',
);
// filter shop plugins & add shop names as keys
$active_shop_plugins = array_intersect( $shop_plugins, $active_plugins );
return $active_shop_plugins;
}
/**
* Get array of active old WooCommerce Menu Cart plugins
*
* @return array plugin paths
*/
public function get_active_old_versions() {
$active_plugins = $this->get_active_plugins();
$old_versions = array(
'woocommerce-menu-bar-cart/wc_cart_nav.php', //first version
'woocommerce-menu-bar-cart/woocommerce-menu-cart.php', //last free version
'woocommerce-menu-cart/woocommerce-menu-cart.php', //never actually released? just in case...
'woocommerce-menu-cart-pro/woocommerce-menu-cart-pro.php', //old pro version
);
$active_old_plugins = array_intersect( $old_versions, $active_plugins );
return $active_old_plugins;
}
/**
* Fallback admin notices
*
* @return void
*/
public function need_shop() {
$error = __( 'WP Menu Cart could not detect an active shop plugin. Make sure you have activated at least one of the supported plugins.', 'wp-menu-cart' );
printf(
'<div class="notice notice-error"><p>%1$s <a href="%2$s">%3$s</a></p></div>',
esc_html( $error ),
esc_url( wp_nonce_url( add_query_arg( 'hide_wpmenucart_shop_check', 'true' ), 'need_shop_notice_nonce' ) ),
esc_html__( 'Hide this notice', 'wp-menu-cart' )
);
// Hide notice.
if ( isset( $_GET['hide_wpmenucart_shop_check'] ) && isset( $_REQUEST['_wpnonce'] ) ) {
$nonce = sanitize_text_field( wp_unslash( $_REQUEST['_wpnonce'] ) );
if ( ! wp_verify_nonce( $nonce, 'need_shop_notice_nonce' ) ) {
wcpdf_log_error( 'You do not have sufficient permissions to perform this action: need_shop_notice_nonce' );
} else {
update_option( 'wpmenucart_shop_check', 'hide' );
}
wp_redirect( remove_query_arg( array( 'hide_wpmenucart_shop_check', '_wpnonce' ) ) );
exit;
}
}
public function woocommerce_version_active() {
$error = __( 'An old version of Menu Cart for WooCommerce is currently activated, you need to disable or uninstall it for WP Menu Cart to function properly' , 'wp-menu-cart' );
printf( '<div class="notice notice-error"><p>%s</p></div>', esc_html( $error ) );
}
/**
* Declares WooCommerce HPOS compatibility.
*
* @return void
*/
public function woocommerce_hpos_compatible() {
if ( class_exists( '\Automattic\WooCommerce\Utilities\FeaturesUtil' ) ) {
\Automattic\WooCommerce\Utilities\FeaturesUtil::declare_compatibility( 'custom_order_tables', __FILE__, true );
}
}
/**
* Load translations.
*/
public function languages() {
if ( function_exists( 'determine_locale' ) ) { // WP5.0+
$locale = determine_locale();
} else {
$locale = is_admin() && function_exists( 'get_user_locale' ) ? get_user_locale() : get_locale();
}
$locale = apply_filters( 'plugin_locale', $locale, 'wp-menu-cart' );
/**
* Frontend/global Locale. Looks in:
*
* - WP_LANG_DIR/wp-menu-cart/wp-menu-cart-LOCALE.mo
* - wp-menu-cart/languages/wp-menu-cart-LOCALE.mo (which if not found falls back to:)
* - WP_LANG_DIR/plugins/wp-menu-cart-LOCALE.mo
*/
unload_textdomain( 'wp-menu-cart');
load_textdomain( 'wp-menu-cart', WP_LANG_DIR . '/wp-menu-cart/wp-menu-cart-' . $locale . '.mo' );
load_plugin_textdomain( 'wp-menu-cart', false, dirname( plugin_basename( __FILE__ ) ) . '/languages/' );
}
/**
* Maintain textdomain compatibility between main plugin (wp-menu-cart) and WooCommerce version (woocommerce-menu-bar-cart)
* so that wordpress.org language packs can be used for both
*/
public function textdomain_fallback( $mofile, $textdomain ) {
$main_domain = 'wp-menu-cart';
$wc_domain = 'woocommerce-menu-bar-cart';
// check if this is filtering the mofile for this plugin
if ( $textdomain === $main_domain ) {
$wc_mofile = str_replace( "{$textdomain}-", "{$wc_domain}-", $mofile ); // with trailing dash to target file and not folder
if ( file_exists( $wc_mofile ) ) {
if ( ! is_callable( 'copy' ) ) {
$copy = false;
} elseif ( ! file_exists( $mofile ) ) {
$copy = true;
} else { // can copy but file already exists
$wc_file_date = filemtime( $wc_mofile );
$main_file_date = filemtime( $mofile );
// check if wc file is newer
if ( $wc_file_date && $main_file_date && ( $wc_file_date > $main_file_date ) ) {
$copy = true;
} else {
$copy = false;
}
}
// we have a wc override - copy and use it
if ( $copy && $success = copy( $wc_mofile, $mofile ) ) {
// copy .po too if available
$wc_pofile = substr_replace( $wc_mofile, ".po", -3 );
if ( file_exists( $wc_pofile ) ) {
copy( $wc_pofile, substr_replace( $mofile, ".po", -3 ) );
}
return $mofile;
}
return $wc_mofile;
}
}
return $mofile;
}
/**
* Register strings for WPML String Translation
*/
public function wpml() {
if ( isset( $this->options['wpml_string_translation'] ) && function_exists( 'icl_register_string' ) ) {
icl_register_string('WP Menu Cart', 'item text', 'item');
icl_register_string('WP Menu Cart', 'items text', 'items');
icl_register_string('WP Menu Cart', 'empty cart text', 'your cart is currently empty');
icl_register_string('WP Menu Cart', 'hover text', 'View your shopping cart');
icl_register_string('WP Menu Cart', 'empty hover text', 'Start shopping');
}
}
/**
* Load custom ajax
*/
public function load_custom_ajax() {
wp_enqueue_script(
'wpmenucart',
$this->plugin_url() . '/assets/js/wpmenucart' . $this->asset_suffix . '.js',
array( 'jquery' ),
WPMENUCART_VERSION,
true
);
// get URL to WordPress ajax handling page
if ( in_array( $this->options['shop_plugin'], [ 'easy-digital-downloads', 'easy-digital-downloads-pro' ] ) && function_exists( 'edd_get_ajax_url' ) ) {
// use EDD function to prevent SSL issues http://git.io/V7w76A
$ajax_url = edd_get_ajax_url();
} else {
$ajax_url = admin_url( 'admin-ajax.php' );
}
wp_localize_script(
'wpmenucart',
'wpmenucart_ajax',
array(
'ajaxurl' => $ajax_url,
'nonce' => wp_create_nonce('wpmenucart')
)
);
}
/**
* Load EDD ajax helper
*/
public function load_edd_ajax() {
wp_enqueue_script(
'wpmenucart-edd-ajax',
$this->plugin_url() . '/assets/js/wpmenucart-edd-ajax' . $this->asset_suffix . '.js',
array( 'jquery' ),
WPMENUCART_VERSION,
true
);
wp_localize_script(
'wpmenucart-edd-ajax',
'wpmenucart_ajax',
array(
'ajaxurl' => function_exists( 'edd_get_ajax_url' ) ? edd_get_ajax_url() : admin_url( 'admin-ajax.php' ),
'nonce' => wp_create_nonce('wpmenucart'),
'always_display' => isset($this->options['always_display']) ? $this->options['always_display'] : '',
)
);
}
/*
* In order to avoid issues with relative font paths, we parse the CSS file to print it inline
*/
public function get_parsed_font_css() {
ob_start();
if ( file_exists( $this->plugin_path() . '/assets/css/wpmenucart-font' . $this->asset_suffix . '.css' ) ) {
include( $this->plugin_path() . '/assets/css/wpmenucart-font' . $this->asset_suffix . '.css' ) ;
}
$font_css = str_replace( '../fonts', $this->plugin_url() . '/assets/fonts', ob_get_clean() );
return $font_css;
}
/*
* Allow wpmenucart-main.css to be overriden via the theme
*/
public function get_main_css_url() {
return file_exists( get_stylesheet_directory() . '/wpmenucart-main.css' ) ? get_stylesheet_directory_uri() . '/wpmenucart-main.css' : $this->plugin_url() . '/assets/css/wpmenucart-main' . $this->asset_suffix . '.css';
}
/**
* Load admin assets
*/
public function load_admin_assets() {
if ( is_admin() && in_array( get_current_screen()->id, [ 'woocommerce_page_wpmenucart_options_page', 'settings_page_wpmenucart_options_page' ] ) ) {
wp_enqueue_style( 'wpmenucart-settings-styles', $this->plugin_url() . '/assets/css/wpmenucart-settings' . $this->asset_suffix . '.css', array(), WPMENUCART_VERSION );
wp_enqueue_script(
'wpmenucart-settings-scripts',
$this->plugin_url() . '/assets/js/wpmenucart-settings' . $this->asset_suffix . '.js',
array( 'jquery' ),
WPMENUCART_VERSION,
true
);
}
}
/**
* Load frontend assets
*/
public function load_frontend_assets() {
if ( isset( $this->options['icon_display'] ) ) {
wp_enqueue_style( 'wpmenucart-icons', $this->plugin_url() . '/assets/css/wpmenucart-icons' . $this->asset_suffix . '.css', array(), WPMENUCART_VERSION, 'all' );
wp_add_inline_style( 'wpmenucart-icons', $this->get_parsed_font_css() );
}
wp_enqueue_style( 'wpmenucart', $this->get_main_css_url(), array(), WPMENUCART_VERSION, 'all' );
// Hide built-in theme carts
if ( isset( $this->options['hide_theme_cart'] ) ) {
wp_add_inline_style( 'wpmenucart', '.et-cart-info { display:none !important; } .site-header-cart { display:none !important; }' );
}
// Load Stylesheet if twentytwelve is active
if ( 'Twenty Twelve' === wp_get_theme() ) {
wp_enqueue_style( 'wpmenucart-twentytwelve', $this->plugin_url() . '/assets/css/wpmenucart-twentytwelve' . $this->asset_suffix . '.css', array(), WPMENUCART_VERSION, 'all' );
}
// Load Stylesheet if twentyfourteen is active
if ( 'Twenty Fourteen' === wp_get_theme() ) {
wp_enqueue_style( 'wpmenucart-twentyfourteen', $this->plugin_url() . '/assets/css/wpmenucart-twentyfourteen' . $this->asset_suffix . '.css', array(), WPMENUCART_VERSION, 'all' );
}
// extra script that improves AJAX behavior when 'Always display cart' is disabled
if ( ! isset( $this->options['always_display'] ) ) {
wp_enqueue_script(
'wpmenucart-ajax-assist',
$this->plugin_url() . '/assets/js/wpmenucart-ajax-assist' . $this->asset_suffix . '.js',
array( 'jquery' ),
WPMENUCART_VERSION,
true
);
wp_localize_script(
'wpmenucart-ajax-assist',
'wpmenucart_ajax_assist',
array(
'shop_plugin' => isset( $this->options['shop_plugin'] ) ? $this->options['shop_plugin'] : '',
'always_display' => isset( $this->options['always_display'] ) ? $this->options['always_display'] : '',
)
);
}
if ( isset( $this->options['show_on_cart_checkout_page'] ) && function_exists( 'is_checkout' ) && function_exists( 'is_cart' ) && ( is_checkout() || is_cart() ) && version_compare( WC_VERSION, '7.7', '>' ) ) {
wp_enqueue_script( 'wc-cart-fragments' );
}
}
/**
* Load Block Editor CSS
*/
public function load_block_editor_styles( $wp_styles ) {
$wp_edit_blocks = $wp_styles->query( 'wp-edit-blocks', 'registered' );
$handles = array(
'wpmenucart-icons',
'wpmenucart',
);
if ( ! $wp_edit_blocks ) {
return;
}
// add handle css as 'wp-edit-blocks' dependency
foreach ( $handles as $handle ) {
$style = $wp_styles->query( $handle, 'registered' );
if ( ! $style ) {
$wp_styles->add( 'wpmenucart-icons', $this->plugin_url() . '/assets/css/wpmenucart-icons' . $this->asset_suffix . '.css', array(), WPMENUCART_VERSION, 'all' );
$wp_styles->add( 'wpmenucart', $this->get_main_css_url(), array(), WPMENUCART_VERSION, 'all' );
}
if ( $wp_styles->query( $handle, 'registered' ) && ! in_array( $handle, $wp_edit_blocks->deps, true ) ) {
$wp_edit_blocks->deps[] = $handle;
}
}
// add inline font css
$wp_styles->add_inline_style( 'wp-edit-blocks', $this->get_parsed_font_css() );
}
public function register_cart_navigation_block() {
if ( ! function_exists( 'register_block_type' ) ) {
return;
}
wp_register_script(
'wpmenucart-navigation-block',
$this->plugin_url() . '/assets/js/wpmenucart-navigation-block' . $this->asset_suffix . '.js',
array( 'wp-blocks', 'wp-element', 'wp-i18n', 'wp-server-side-render' ),
WPMENUCART_VERSION,
true
);
register_block_type( 'wpo/wpmenucart-navigation', array(
'editor_script' => 'wpmenucart-navigation-block',
'render_callback' => array( $this, 'cart_navigation_block_output' ),
) );
}
public function cart_navigation_block_output( $atts ) {
$menu = sprintf( '<ul>%s</ul>', $this->generate_menu_item_li( '', 'block' ) );
if ( $this->is_block_editor() ) {
// deactivate links when using the full site or block editor to prevent navigating away from the editor
$menu = preg_replace( '/(<[^>]+) href=".*?"/i', '$1', $menu );
}
return $menu;
}
public function is_rest_request() {
return defined( 'REST_REQUEST' ) && REST_REQUEST;
}
public function is_block_editor() {
if ( $this->is_rest_request() ) {
$route = untrailingslashit( $GLOBALS['wp']->query_vars['rest_route'] );
if ( strpos( $route, 'wpo/wpmenucart-navigation' ) !== false || strpos( $route, '/navigation' ) !== false ) {
return true;
}
}
return false;
}
public function is_block_theme() {
$theme = wp_get_theme();
if ( ! empty( $theme ) && is_callable( array( $theme, 'is_block_theme' ) ) ) {
return $theme->is_block_theme();
}
return false;
}
public function get_current_theme_name() {
$theme = wp_get_theme();
if ( ! empty( $theme ) && is_callable( array( $theme, 'display' ) ) ) {
return $theme->display( 'Name' );
}
return false;
}
/**
* Add filters to selected menus to add cart item <li>
*/
public function filter_nav_menus() {
// exit if no shop class is active
if ( ! isset( $this->shop ) )
return;
// exit if no menus set
if ( ! isset( $this->options['menu_slugs'] ) || empty( $this->options['menu_slugs'] ) )
return;
if ( '0' !== $this->options['menu_slugs'][1] ) {
add_filter( 'wp_nav_menu_' . $this->options['menu_slugs'][1] . '_items', array( &$this, 'add_itemcart_to_menu' ) , 10, 2 );
}
}
/**
* Gets the menu item <li>
*
* @param string $classes
* @param string $context can be 'classic' or 'block'
*
* @return string
*/
public function generate_menu_item_li( $classes, $context = 'classic' ) {
$alignment = $this->options['items_alignment'] ?? 'standard';
$classes .= ' wpmenucartli wpmenucart-display-' . $alignment;
if ( function_exists( 'is_checkout' ) && function_exists( 'is_cart' ) && ( is_checkout() || is_cart() ) && empty( $this->options['show_on_cart_checkout_page'] ) ) {
$classes .= ' hidden-wpmenucart';
}
if ( 'classic' === $context ) {
$classes .= ' menu-item';
} elseif ( 'block' === $context ) {
$classes .= ' wp-block-navigation-item wp-block-navigation-link';
}
$item_data = $this->shop->menu_item();
if ( 0 === $item_data['cart_contents_count'] && ! isset( $this->options['always_display'] ) && ! $this->is_block_editor() ) {
$classes .= ' empty-wpmenucart';
}
$classes = apply_filters( 'wpmenucart_menu_item_classes', $classes );
$this->menu_items['menu']['menu_item_li_classes'] = $classes;
// DEPRECATED: These filters are now deprecated in favour of the more precise filters in the functions!
$menu_item_li = apply_filters_deprecated( 'wpmenucart_menu_item_filter', array( $this->wpmenucart_menu_item() ), '2.5.3', '' );
return '<li class="' . $classes . '" id="wpmenucartli">' . $menu_item_li . '</li>';
}
/**
* Add Menu Cart to menu
*
* @return menu items + Menu Cart item
*/
public function add_itemcart_to_menu( $items ) {
$common_classes = $this->get_common_li_classes( $items );
$menu_item_li = $this->generate_menu_item_li( $common_classes, 'classic' );
if ( apply_filters( 'wpmenucart_prepend_menu_item', false ) ) {
$items = apply_filters( 'wpmenucart_menu_item_wrapper', $menu_item_li ) . $items;
} else {
$items .= apply_filters( 'wpmenucart_menu_item_wrapper', $menu_item_li );
}
return $items;
}
/**
* Get a flat list of common classes from all menu items in a menu
* @param string $items nav_menu HTML containing all <li> menu items
* @return string flat (imploded) list of common classes
*/
public function get_common_li_classes( $items ) {
if ( empty( $items ) || ! class_exists( 'DOMDocument' ) ) {
return '';
}
$libxml_previous_state = libxml_use_internal_errors( true ); // enable user error handling
$dom_items = new DOMDocument;
$dom_items->loadHTML( $items );
$lis = $dom_items->getElementsByTagName( 'li' );
if ( empty( $lis ) ) {
libxml_clear_errors();
libxml_use_internal_errors( $libxml_previous_state );
return;
}
foreach ( $lis as $li ) {
if ( 'ul' !== $li->parentNode->tagName )
$li_classes[] = explode( ' ', $li->getAttribute( 'class' ) );
}
// Uncomment to dump DOM errors / warnings
//$errors = libxml_get_errors();
//print_r ($errors);
// clear errors and reset to previous error handling state
libxml_clear_errors();
libxml_use_internal_errors( $libxml_previous_state );
if ( ! empty( $li_classes ) ) {
$common_li_classes = array_shift( $li_classes );
foreach ( $li_classes as $li_class ) {
$common_li_classes = array_intersect( $li_class, $common_li_classes );
}
$common_li_classes_flat = implode( ' ', $common_li_classes );
} else {
$common_li_classes_flat = '';
}
return $common_li_classes_flat;
}
/**
* Ajaxify Menu Cart
*/
public function woocommerce_ajax_fragments( $fragments ) {
if ( ! defined('WOOCOMMERCE_CART') ) {
define( 'WOOCOMMERCE_CART', true );
}
$fragments['a.wpmenucart-contents'] = $this->wpmenucart_menu_item();
return $fragments;
}
/**
* Create HTML for Menu Cart item
*/
public function wpmenucart_menu_item() {
$item_data = $this->shop->menu_item();
// Check empty cart settings
if ( 0 === $item_data['cart_contents_count'] && ! isset( $this->options['always_display'] ) && ! $this->is_block_editor() ) {
$empty_menu_item = '<a class="wpmenucart-contents empty-wpmenucart" style="display:none"> </a>';
return $empty_menu_item;
}
if ( isset( $this->options['wpml_string_translation'] ) && function_exists( 'icl_t' ) ) {
//use WPML
$viewing_cart = icl_t('WP Menu Cart', 'hover text', 'View your shopping cart');
$start_shopping = icl_t('WP Menu Cart', 'empty hover text', 'Start shopping');
$cart_contents = $item_data['cart_contents_count'] .' '. ( 1 === $item_data['cart_contents_count'] ? icl_t( 'WP Menu Cart', 'item text', 'item' ) : icl_t( 'WP Menu Cart', 'items text', 'items' ) );
} else {
//use regular WP i18n
$viewing_cart = __( 'View your shopping cart', 'wp-menu-cart' );
$start_shopping = __( 'Start shopping', 'wp-menu-cart' );
/* translators: item count */
$cart_contents = sprintf( _n( '%d item', '%d items', $item_data['cart_contents_count'], 'wp-menu-cart' ), $item_data['cart_contents_count'] );
}
$this->menu_items['menu']['cart_contents'] = $cart_contents;
if ( 0 === $item_data['cart_contents_count'] ) {
$menu_item_href = apply_filters( 'wpmenucart_emptyurl', $item_data['shop_page_url'] );
$menu_item_title = apply_filters( 'wpmenucart_emptytitle', $start_shopping );
$menu_item_classes = 'wpmenucart-contents empty-wpmenucart-visible';
} else {
$menu_item_href = apply_filters( 'wpmenucart_fullurl', $item_data['cart_url'] );
$menu_item_title = apply_filters( 'wpmenucart_fulltitle', $viewing_cart );
$menu_item_classes = 'wpmenucart-contents';
}
$this->menu_items['menu']['menu_item_href'] = $menu_item_href;
$this->menu_items['menu']['menu_item_title'] = $menu_item_title;
if ( defined( 'UBERMENU_VERSION' ) && ( version_compare( UBERMENU_VERSION, '3.0.0' ) >= 0 ) ) {
$menu_item_classes .= ' ubermenu-target';
}
$menu_item = '<a class="' . $menu_item_classes . '" href="' . $menu_item_href . '" title="' . $menu_item_title . '">';
$menu_item_a_content = '';
if ( isset( $this->options['icon_display'] ) ) {
$icon = isset( $this->options['cart_icon'] ) ? $this->options['cart_icon'] : '0';
$menu_item_icon = '<i class="wpmenucart-icon-shopping-cart-' . $icon . '" role="img" aria-label="' . __( 'Cart','wp-menu-cart' ) . '"></i>';
$menu_item_a_content .= $menu_item_icon;
} else {
$menu_item_icon = '';
}
$items_display = $this->options['items_display'] ?? 3;
switch ( $items_display ) {
case 1: //items only
$menu_item_a_content .= '<span class="cartcontents">' . $cart_contents . '</span>';
break;
case 2: //price only
$menu_item_a_content .= '<span class="amount">' . $item_data['cart_total'] . '</span>';
break;
case 3: //items & price
$menu_item_a_content .= '<span class="cartcontents">' . $cart_contents . '</span><span class="amount">' . $item_data['cart_total'] . '</span>';
break;
}
$menu_item_a_content = apply_filters( 'wpmenucart_menu_item_a_content', $menu_item_a_content, $menu_item_icon, $cart_contents, $item_data );
$this->menu_items['menu']['menu_item_a_content'] = $menu_item_a_content;
$menu_item .= $menu_item_a_content . '</a>';
$menu_item = apply_filters ('wpmenucart_menu_item_a', $menu_item, $item_data, $this->options, $menu_item_a_content, $viewing_cart, $start_shopping, $cart_contents);
if ( ! empty( $menu_item ) ) {
return $menu_item;
}
}
public function wpmenucart_ajax() {
check_ajax_referer( 'wpmenucart', 'security' );
$variable = $this->wpmenucart_menu_item();
echo wp_kses_post( $variable );
die();
}
/**
* Get the plugin url.
* @return string
*/
public function plugin_url() {
return untrailingslashit( plugin_dir_url( __FILE__ ) );
}
/**
* Get the plugin path.
* @return string
*/
public function plugin_path() {
return untrailingslashit( plugin_dir_path( __FILE__ ) );
}
/**
* Enqueue script after WooCommerce Cart and Checkout block
* @return null
*/
public function wc_block_support_script() {
wp_enqueue_script(
'wpmenucart-cart-checkout-js',
$this->plugin_url() . '/assets/js/wpmenucart-wc-block-support'. $this->asset_suffix . '.js',
array( 'jquery' ),
WPMENUCART_VERSION,
true
);
wp_localize_script(
'wpmenucart-cart-checkout-js',
'wpmenucart_cart_ajax',
array(
'ajaxurl' => admin_url( 'admin-ajax.php' ),
'nonce' => wp_create_nonce( 'wpmenucart' ),
)
);
}
} // end class
endif; // class_exists
/**
* Returns the main instance of WP Menu Cart to prevent the need to use globals.
*
* @return WPO_Menu_Cart
*/
function WPO_Menu_Cart() {
return WpMenuCart::instance();
}
WPO_Menu_Cart(); // load plugin