WooCommerce Menu Cart - Version 2.11.0-beta-1

Version Description

Download this release

Release Info

Developer wpovernight
Plugin Icon 128x128 WooCommerce Menu Cart
Version 2.11.0-beta-1
Comparing to
See all releases

Code changes from version 2.10.4 to 2.11.0-beta-1

assets/js/wpmenucart-navigation-block.js ADDED
@@ -0,0 +1,62 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ( function( blocks, element, i18n, serverSideRender ) {
2
+
3
+ let __ = i18n.__;
4
+ let el = element.createElement;
5
+ let registerBlockType = blocks.registerBlockType;
6
+ let createBlock = blocks.createBlock;
7
+
8
+ // svg icon
9
+ let iconCart = el(
10
+ 'svg',
11
+ {
12
+ width: 20,
13
+ height: 20,
14
+ },
15
+ el( 'path',
16
+ {
17
+ d: "M15.55 13c.75 0 1.41-.41 1.75-1.03l3.58-6.49c.37-.66-.11-1.48-.87-1.48H5.21l-.94-2H1v2h2l3.6 7.59-1.35 2.44C4.52 15.37 5.48 17 7 17h12v-2H7l1.1-2h7.45zM6.16 6h12.15l-2.76 5H8.53L6.16 6zM7 18c-1.1 0-1.99.9-1.99 2S5.9 22 7 22s2-.9 2-2-.9-2-2-2zm10 0c-1.1 0-1.99.9-1.99 2s.89 2 1.99 2 2-.9 2-2-.9-2-2-2z"
18
+ }
19
+ )
20
+ );
21
+
22
+ // navigation block
23
+ let navigationBlockSettings = {
24
+ title: __( 'Cart', 'wp-menu-cart' ),
25
+ icon: iconCart,
26
+ parent: [ 'core/navigation' ],
27
+ keywords: [ 'cart' ],
28
+ support: {
29
+ html: true,
30
+ },
31
+ transforms: {
32
+ from: [
33
+ {
34
+ type: 'block',
35
+ blocks: [ 'core/navigation-link' ],
36
+ transform: () => createBlock( 'wpo/wpmenucart-navigation' )
37
+ }
38
+ ]
39
+ },
40
+ edit: function( props ) {
41
+ return el(
42
+ serverSideRender,
43
+ {
44
+ block: 'wpo/wpmenucart-navigation',
45
+ className: 'wpmenucart-navigation-block',
46
+ attributes: props.attributes,
47
+ }
48
+ );
49
+ },
50
+ save: function() {
51
+ // return null to render from php
52
+ return null;
53
+ },
54
+ }
55
+ registerBlockType( 'wpo/wpmenucart-navigation', navigationBlockSettings );
56
+
57
+ } )(
58
+ window.wp.blocks,
59
+ window.wp.element,
60
+ window.wp.i18n,
61
+ window.wp.serverSideRender,
62
+ );
assets/js/wpmenucart-navigation-block.min.js ADDED
@@ -0,0 +1 @@
 
1
+ (function(a,b,c,d){let e=c.__,f=b.createElement,g=a.registerBlockType,h=a.createBlock,i=f("svg",{width:20,height:20},f("path",{d:"M15.55 13c.75 0 1.41-.41 1.75-1.03l3.58-6.49c.37-.66-.11-1.48-.87-1.48H5.21l-.94-2H1v2h2l3.6 7.59-1.35 2.44C4.52 15.37 5.48 17 7 17h12v-2H7l1.1-2h7.45zM6.16 6h12.15l-2.76 5H8.53L6.16 6zM7 18c-1.1 0-1.99.9-1.99 2S5.9 22 7 22s2-.9 2-2-.9-2-2-2zm10 0c-1.1 0-1.99.9-1.99 2s.89 2 1.99 2 2-.9 2-2-.9-2-2-2z"})),j={title:e("Cart","wp-menu-cart"),icon:i,parent:["core/navigation"],keywords:["cart"],support:{html:!0},transforms:{from:[{type:"block",blocks:["core/navigation-link"],transform:()=>h("wpo/wpmenucart-navigation")}]},edit:function(a){return f(d,{block:"wpo/wpmenucart-navigation",className:"wpmenucart-navigation-block",attributes:a.attributes})},save:function(){return null}};g("wpo/wpmenucart-navigation",j)})(window.wp.blocks,window.wp.element,window.wp.i18n,window.wp.serverSideRender);
includes/wpmenucart-woocommerce.php CHANGED
@@ -1,21 +1,20 @@
1
  <?php
2
  if ( ! class_exists( 'WPMenuCart_WooCommerce' ) ) {
3
  class WPMenuCart_WooCommerce {
 
4
  /**
5
  * Construct.
6
  */
7
- public function __construct() {
8
- }
9
 
10
  public function menu_item() {
11
- // make sure cart is loaded! https://wordpress.org/support/topic/activation-breaks-customise?replies=10#post-7908988
12
- $this->maybe_load_cart();
13
 
14
  $menu_item = array(
15
- 'cart_url' => $this->cart_url(),
16
- 'shop_page_url' => $this->shop_url(),
17
- 'cart_total' => strip_tags( $this->get_cart_total() ),
18
- 'cart_contents_count' => $this->get_cart_contents_count(),
19
  );
20
 
21
  return apply_filters( 'wpmenucart_menu_item_data', $menu_item );
@@ -23,9 +22,14 @@ if ( ! class_exists( 'WPMenuCart_WooCommerce' ) ) {
23
 
24
  public function maybe_load_cart() {
25
  if ( function_exists( 'WC' ) ) {
26
- if ( empty( WC()->cart ) ) {
27
- WC()->cart = new WC_Cart();
 
 
 
 
28
  }
 
29
  } else {
30
  global $woocommerce;
31
  if ( empty( $woocommerce->cart ) ) {
1
  <?php
2
  if ( ! class_exists( 'WPMenuCart_WooCommerce' ) ) {
3
  class WPMenuCart_WooCommerce {
4
+
5
  /**
6
  * Construct.
7
  */
8
+ public function __construct() {}
 
9
 
10
  public function menu_item() {
11
+ $this->maybe_load_cart(); // make sure cart is loaded! https://wordpress.org/support/topic/activation-breaks-customise?replies=10#post-7908988
 
12
 
13
  $menu_item = array(
14
+ 'cart_url' => $this->cart_url(),
15
+ 'shop_page_url' => $this->shop_url(),
16
+ 'cart_total' => strip_tags( $this->get_cart_total() ),
17
+ 'cart_contents_count' => $this->get_cart_contents_count(),
18
  );
19
 
20
  return apply_filters( 'wpmenucart_menu_item_data', $menu_item );
22
 
23
  public function maybe_load_cart() {
24
  if ( function_exists( 'WC' ) ) {
25
+ if ( function_exists( 'wc_load_cart' ) && did_action( 'before_woocommerce_init' ) ) {
26
+ wc_load_cart(); // loads session, customer & cart - WC 3.6.4+
27
+ } else {
28
+ if ( empty( WC()->cart ) ) {
29
+ WC()->cart = new WC_Cart();
30
+ }
31
  }
32
+ WC()->cart->get_cart(); // force cart contents refresh
33
  } else {
34
  global $woocommerce;
35
  if ( empty( $woocommerce->cart ) ) {
wp-menu-cart.php CHANGED
@@ -3,7 +3,7 @@
3
  * Plugin Name: WooCommerce Menu Cart
4
  * Plugin URI: https://wpovernight.com/downloads/menu-cart-pro/
5
  * Description: Extension for your e-commerce plugin (WooCommerce, WP-Ecommerce, Easy Digital Downloads, Eshop or Jigoshop) 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.
6
- * Version: 2.10.4
7
  * Author: WP Overnight
8
  * Author URI: https://wpovernight.com/
9
  * License: GPLv2 or later
@@ -21,9 +21,10 @@ if ( ! class_exists( 'WpMenuCart' ) && ! class_exists( 'WPO_Menu_Cart_Pro' ) ) :
21
 
22
  class WpMenuCart {
23
 
24
- protected $plugin_version = '2.10.4';
25
  public static $plugin_slug;
26
  public static $plugin_basename;
 
27
 
28
  /**
29
  * Construct.
@@ -36,14 +37,20 @@ class WpMenuCart {
36
 
37
  $this->define( 'WPMENUCART_VERSION', $this->plugin_version );
38
 
 
 
39
  // load the localisation & classes
40
  add_action( 'plugins_loaded', array( &$this, 'languages' ), 0 ); // or use init?
41
  add_filter( 'load_textdomain_mofile', array( $this, 'textdomain_fallback' ), 10, 2 );
42
  add_action( 'init', array( &$this, 'wpml' ), 0 );
43
  add_action( 'init', array( $this, 'load_classes' ) );
44
 
45
- // enqueue scripts & ajax
46
- add_action( 'wp_enqueue_scripts', array( &$this, 'load_scripts_styles' ) ); // Load scripts
 
 
 
 
47
  add_action( 'wp_ajax_wpmenucart_ajax', array( &$this, 'wpmenucart_ajax' ), 0 );
48
  add_action( 'wp_ajax_nopriv_wpmenucart_ajax', array( &$this, 'wpmenucart_ajax' ), 0 );
49
 
@@ -112,7 +119,7 @@ class WpMenuCart {
112
  break;
113
  }
114
  if ( isset( $this->options['builtin_ajax'] ) || in_array( $this->options['shop_plugin'], array( 'WP e-Commerce', 'wp-e-commerce', 'eShop', 'eshop' ) ) ) {
115
- add_action("wp_enqueue_scripts", array( &$this, 'load_custom_ajax' ), 0 );
116
  }
117
 
118
  }
@@ -337,10 +344,9 @@ class WpMenuCart {
337
  * Load custom ajax
338
  */
339
  public function load_custom_ajax() {
340
- $suffix = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ? '' : '.min';
341
  wp_enqueue_script(
342
  'wpmenucart',
343
- plugins_url( '/assets/js/wpmenucart'.$suffix.'.js' , __FILE__ ),
344
  array( 'jquery' ),
345
  WPMENUCART_VERSION,
346
  true
@@ -368,10 +374,9 @@ class WpMenuCart {
368
  * Load EDD ajax helper
369
  */
370
  public function load_edd_ajax() {
371
- $suffix = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ? '' : '.min';
372
  wp_enqueue_script(
373
  'wpmenucart-edd-ajax',
374
- plugins_url( '/assets/js/wpmenucart-edd-ajax'.$suffix.'.js', __FILE__ ),
375
  array( 'jquery' ),
376
  WPMENUCART_VERSION
377
  );
@@ -387,31 +392,36 @@ class WpMenuCart {
387
  );
388
  }
389
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
390
 
391
  /**
392
  * Load CSS
393
  */
394
  public function load_scripts_styles() {
395
- $suffix = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ? '' : '.min';
396
-
397
- if (isset($this->options['icon_display'])) {
398
- wp_enqueue_style( 'wpmenucart-icons', plugins_url( '/assets/css/wpmenucart-icons'.$suffix.'.css', __FILE__ ), array(), WPMENUCART_VERSION, 'all' );
399
-
400
- // in order to avoid issues with relative font paths, we parse the CSS file and print it inline
401
- // wp_enqueue_style( 'wpmenucart-font', plugins_url( '/assets/css/wpmenucart-font'.$suffix.'.css', __FILE__ ), array(), WPMENUCART_VERSION, 'all' );
402
- ob_start();
403
- if ( file_exists( plugin_dir_path( __FILE__ ) . 'assets/css/wpmenucart-font'.$suffix.'.css' ) ) {
404
- include( plugin_dir_path( __FILE__ ) . 'assets/css/wpmenucart-font'.$suffix.'.css' ) ;
405
- }
406
- $font_css = str_replace( '../fonts', plugins_url( '/assets/fonts', __FILE__ ), ob_get_clean() );
407
- wp_add_inline_style( 'wpmenucart-icons', $font_css );
408
  }
409
 
410
- // Allow wpmenucart-main.css to be overriden via the theme
411
- $css = file_exists( get_stylesheet_directory() . '/wpmenucart-main.css' )
412
- ? get_stylesheet_directory_uri() . '/wpmenucart-main.css'
413
- : plugins_url( '/assets/css/wpmenucart-main'.$suffix.'.css', __FILE__ );
414
- wp_enqueue_style( 'wpmenucart', $css, array(), WPMENUCART_VERSION, 'all' );
415
 
416
  // Hide built-in theme carts
417
  if ( isset($this->options['hide_theme_cart']) ) {
@@ -420,18 +430,23 @@ class WpMenuCart {
420
 
421
  //Load Stylesheet if twentytwelve is active
422
  if ( wp_get_theme() == 'Twenty Twelve' ) {
423
- wp_enqueue_style( 'wpmenucart-twentytwelve', plugins_url( '/assets/css/wpmenucart-twentytwelve'.$suffix.'.css', __FILE__ ), array(), WPMENUCART_VERSION, 'all' );
424
  }
425
 
426
  //Load Stylesheet if twentyfourteen is active
427
  if ( wp_get_theme() == 'Twenty Fourteen' ) {
428
- wp_enqueue_style( 'wpmenucart-twentyfourteen', plugins_url( '/assets/css/wpmenucart-twentyfourteen'.$suffix.'.css', __FILE__ ), array(), WPMENUCART_VERSION, 'all' );
 
 
 
 
 
429
  }
430
 
431
  // extra script that improves AJAX behavior when 'Always display cart' is disabled
432
  wp_enqueue_script(
433
  'wpmenucart-ajax-assist',
434
- plugins_url( '/assets/js/wpmenucart-ajax-assist'.$suffix.'.js', __FILE__ ),
435
  array( 'jquery' ),
436
  WPMENUCART_VERSION
437
  );
@@ -439,11 +454,77 @@ class WpMenuCart {
439
  'wpmenucart-ajax-assist',
440
  'wpmenucart_ajax_assist',
441
  array(
442
- 'shop_plugin' => isset($this->options['shop_plugin']) ? $this->options['shop_plugin'] : '',
443
- 'always_display' => isset($this->options['always_display']) ? $this->options['always_display'] : '',
444
  )
445
  );
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
446
 
 
 
 
 
 
 
 
 
447
  }
448
 
449
  /**
@@ -462,36 +543,25 @@ class WpMenuCart {
462
  add_filter( 'wp_nav_menu_' . $this->options['menu_slugs'][1] . '_items', array( &$this, 'add_itemcart_to_menu' ) , 10, 2 );
463
  }
464
  }
465
-
466
  /**
467
- * Add Menu Cart to menu
468
  *
469
- * @return menu items + Menu Cart item
 
 
 
470
  */
471
- public function add_itemcart_to_menu( $items ) {
472
- // WooCommerce specific: check if woocommerce cart object is actually loaded
473
- if ( isset($this->options['shop_plugin']) && $this->options['shop_plugin'] == 'woocommerce' ) {
474
- if ( function_exists( 'WC' ) ) {
475
- if ( empty( WC()->cart ) ) {
476
- return $items; // nothing to load data from, return menu without cart item
477
- }
478
- } else {
479
- global $woocommerce;
480
- if ( empty($woocommerce) || !is_object($woocommerce) || !isset($woocommerce->cart) || !is_object($woocommerce->cart) ) {
481
- return $items; // nothing to load data from, return menu without cart item
482
- }
483
- }
484
- }
485
-
486
- $classes = 'menu-item wpmenucartli wpmenucart-display-'.$this->options['items_alignment'];
487
-
488
- if ($this->get_common_li_classes($items) != '') {
489
- $classes .= ' ' . $this->get_common_li_classes($items);
490
- }
491
-
492
- if ( function_exists( 'is_checkout' ) && function_exists( 'is_cart' ) && ( is_checkout() || is_cart() ) && empty($this->options['show_on_cart_checkout_page']) ) {
493
  $classes .= ' hidden-wpmenucart';
494
  }
 
 
 
 
 
495
 
496
  // Filter for <li> item classes
497
  /* Usage (in the themes functions.php):
@@ -501,17 +571,26 @@ class WpMenuCart {
501
  return $classes;
502
  }
503
  */
 
504
  $classes = apply_filters( 'wpmenucart_menu_item_classes', $classes );
505
  $this->menu_items['menu']['menu_item_li_classes'] = $classes;
506
 
507
  // DEPRECATED: These filters are now deprecated in favour of the more precise filters in the functions!
508
- $wpmenucart_menu_item = apply_filters( 'wpmenucart_menu_item_filter', $this->wpmenucart_menu_item() );
509
-
510
- $item_data = $this->shop->menu_item();
511
-
512
- $menu_item_li = '<li class="'.$classes.'" id="wpmenucartli">' . $wpmenucart_menu_item . '</li>';
513
 
514
- if ( apply_filters('wpmenucart_prepend_menu_item', false) ) {
 
 
 
 
 
 
 
 
 
 
 
 
515
  $items = apply_filters( 'wpmenucart_menu_item_wrapper', $menu_item_li ) . $items;
516
  } else {
517
  $items .= apply_filters( 'wpmenucart_menu_item_wrapper', $menu_item_li );
@@ -585,7 +664,7 @@ class WpMenuCart {
585
  $item_data = $this->shop->menu_item();
586
 
587
  // Check empty cart settings
588
- if ($item_data['cart_contents_count'] == 0 && ( !isset($this->options['always_display']) ) ) {
589
  $empty_menu_item = '<a class="wpmenucart-contents empty-wpmenucart" style="display:none">&nbsp;</a>';
590
  return $empty_menu_item;
591
  }
3
  * Plugin Name: WooCommerce Menu Cart
4
  * Plugin URI: https://wpovernight.com/downloads/menu-cart-pro/
5
  * Description: Extension for your e-commerce plugin (WooCommerce, WP-Ecommerce, Easy Digital Downloads, Eshop or Jigoshop) 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.
6
+ * Version: 2.11.0-beta-1
7
  * Author: WP Overnight
8
  * Author URI: https://wpovernight.com/
9
  * License: GPLv2 or later
21
 
22
  class WpMenuCart {
23
 
24
+ protected $plugin_version = '2.11.0-beta-1';
25
  public static $plugin_slug;
26
  public static $plugin_basename;
27
+ public $asset_suffix;
28
 
29
  /**
30
  * Construct.
37
 
38
  $this->define( 'WPMENUCART_VERSION', $this->plugin_version );
39
 
40
+ $this->asset_suffix = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ? '' : '.min';
41
+
42
  // load the localisation & classes
43
  add_action( 'plugins_loaded', array( &$this, 'languages' ), 0 ); // or use init?
44
  add_filter( 'load_textdomain_mofile', array( $this, 'textdomain_fallback' ), 10, 2 );
45
  add_action( 'init', array( &$this, 'wpml' ), 0 );
46
  add_action( 'init', array( $this, 'load_classes' ) );
47
 
48
+ // enqueue scripts & styles
49
+ add_action( 'wp_enqueue_scripts', array( &$this, 'load_scripts_styles' ) ); // load frontend scripts
50
+ add_action( 'init', array( &$this, 'register_cart_navigation_block' ) ); // register cart navigation block
51
+ add_action( 'wp_default_styles', array( &$this, 'load_block_editor_styles' ), 99 ); // load block editor styles
52
+
53
+ // AJAX
54
  add_action( 'wp_ajax_wpmenucart_ajax', array( &$this, 'wpmenucart_ajax' ), 0 );
55
  add_action( 'wp_ajax_nopriv_wpmenucart_ajax', array( &$this, 'wpmenucart_ajax' ), 0 );
56
 
119
  break;
120
  }
121
  if ( isset( $this->options['builtin_ajax'] ) || in_array( $this->options['shop_plugin'], array( 'WP e-Commerce', 'wp-e-commerce', 'eShop', 'eshop' ) ) ) {
122
+ add_action( 'wp_enqueue_scripts', array( &$this, 'load_custom_ajax' ), 0 );
123
  }
124
 
125
  }
344
  * Load custom ajax
345
  */
346
  public function load_custom_ajax() {
 
347
  wp_enqueue_script(
348
  'wpmenucart',
349
+ plugins_url( '/assets/js/wpmenucart'.$this->asset_suffix.'.js' , __FILE__ ),
350
  array( 'jquery' ),
351
  WPMENUCART_VERSION,
352
  true
374
  * Load EDD ajax helper
375
  */
376
  public function load_edd_ajax() {
 
377
  wp_enqueue_script(
378
  'wpmenucart-edd-ajax',
379
+ plugins_url( '/assets/js/wpmenucart-edd-ajax'.$this->asset_suffix.'.js', __FILE__ ),
380
  array( 'jquery' ),
381
  WPMENUCART_VERSION
382
  );
392
  );
393
  }
394
 
395
+ /*
396
+ * In order to avoid issues with relative font paths, we parse the CSS file to print it inline
397
+ */
398
+ public function get_parsed_font_css() {
399
+ ob_start();
400
+ if ( file_exists( plugin_dir_path( __FILE__ ) . 'assets/css/wpmenucart-font'.$this->asset_suffix.'.css' ) ) {
401
+ include( plugin_dir_path( __FILE__ ) . 'assets/css/wpmenucart-font'.$this->asset_suffix.'.css' ) ;
402
+ }
403
+ $font_css = str_replace( '../fonts', plugins_url( '/assets/fonts', __FILE__ ), ob_get_clean() );
404
+
405
+ return $font_css;
406
+ }
407
+
408
+ /*
409
+ * Allow wpmenucart-main.css to be overriden via the theme
410
+ */
411
+ public function get_main_css_url() {
412
+ return file_exists( get_stylesheet_directory() . '/wpmenucart-main.css' ) ? get_stylesheet_directory_uri() . '/wpmenucart-main.css' : plugins_url( '/assets/css/wpmenucart-main'.$this->asset_suffix.'.css', __FILE__ );
413
+ }
414
 
415
  /**
416
  * Load CSS
417
  */
418
  public function load_scripts_styles() {
419
+ if ( isset( $this->options['icon_display'] ) ) {
420
+ wp_enqueue_style( 'wpmenucart-icons', plugins_url( '/assets/css/wpmenucart-icons'.$this->asset_suffix.'.css', __FILE__ ), array(), WPMENUCART_VERSION, 'all' );
421
+ wp_add_inline_style( 'wpmenucart-icons', $this->get_parsed_font_css() );
 
 
 
 
 
 
 
 
 
 
422
  }
423
 
424
+ wp_enqueue_style( 'wpmenucart', $this->get_main_css_url(), array(), WPMENUCART_VERSION, 'all' );
 
 
 
 
425
 
426
  // Hide built-in theme carts
427
  if ( isset($this->options['hide_theme_cart']) ) {
430
 
431
  //Load Stylesheet if twentytwelve is active
432
  if ( wp_get_theme() == 'Twenty Twelve' ) {
433
+ wp_enqueue_style( 'wpmenucart-twentytwelve', plugins_url( '/assets/css/wpmenucart-twentytwelve'.$this->asset_suffix.'.css', __FILE__ ), array(), WPMENUCART_VERSION, 'all' );
434
  }
435
 
436
  //Load Stylesheet if twentyfourteen is active
437
  if ( wp_get_theme() == 'Twenty Fourteen' ) {
438
+ wp_enqueue_style( 'wpmenucart-twentyfourteen', plugins_url( '/assets/css/wpmenucart-twentyfourteen'.$this->asset_suffix.'.css', __FILE__ ), array(), WPMENUCART_VERSION, 'all' );
439
+ }
440
+
441
+ //Load Stylesheet if twentyfourteen is active
442
+ if ( wp_get_theme() == 'Twenty Fourteen' ) {
443
+ wp_enqueue_style( 'wpmenucart-twentyfourteen', plugins_url( '/assets/css/wpmenucart-twentyfourteen'.$this->asset_suffix.'.css', __FILE__ ), array(), WPMENUCART_VERSION, 'all' );
444
  }
445
 
446
  // extra script that improves AJAX behavior when 'Always display cart' is disabled
447
  wp_enqueue_script(
448
  'wpmenucart-ajax-assist',
449
+ plugins_url( '/assets/js/wpmenucart-ajax-assist'.$this->asset_suffix.'.js', __FILE__ ),
450
  array( 'jquery' ),
451
  WPMENUCART_VERSION
452
  );
454
  'wpmenucart-ajax-assist',
455
  'wpmenucart_ajax_assist',
456
  array(
457
+ 'shop_plugin' => isset( $this->options['shop_plugin'] ) ? $this->options['shop_plugin'] : '',
458
+ 'always_display' => isset( $this->options['always_display'] ) ? $this->options['always_display'] : '',
459
  )
460
  );
461
+ }
462
+
463
+ /**
464
+ * Load Block Editor CSS
465
+ */
466
+ public function load_block_editor_styles( $wp_styles ) {
467
+ $wp_edit_blocks = $wp_styles->query( 'wp-edit-blocks', 'registered' );
468
+ $handles = array(
469
+ 'wpmenucart-icons',
470
+ 'wpmenucart',
471
+ );
472
+
473
+ if ( ! $wp_edit_blocks ) {
474
+ return;
475
+ }
476
+
477
+ // add handle css as 'wp-edit-blocks' dependency
478
+ foreach ( $handles as $handle ) {
479
+ $style = $wp_styles->query( $handle, 'registered' );
480
+ if ( ! $style ) {
481
+ $wp_styles->add( 'wpmenucart-icons', plugins_url( '/assets/css/wpmenucart-icons'.$this->asset_suffix.'.css', __FILE__ ), array(), WPMENUCART_VERSION, 'all' );
482
+ $wp_styles->add( 'wpmenucart', $this->get_main_css_url(), array(), WPMENUCART_VERSION, 'all' );
483
+ }
484
+ if ( $wp_styles->query( $handle, 'registered' ) && ! in_array( $handle, $wp_edit_blocks->deps, true ) ) {
485
+ $wp_edit_blocks->deps[] = $handle;
486
+ }
487
+ }
488
+
489
+ // add inline font css
490
+ $wp_styles->add_inline_style( 'wp-edit-blocks', $this->get_parsed_font_css() );
491
+ }
492
+
493
+ public function register_cart_navigation_block() {
494
+ wp_register_script(
495
+ 'wpmenucart-navigation-block',
496
+ plugins_url( '/assets/js/wpmenucart-navigation-block'.$this->asset_suffix.'.js', __FILE__ ),
497
+ array( 'wp-blocks', 'wp-element', 'wp-i18n', 'wp-server-side-render' ),
498
+ WPMENUCART_VERSION
499
+ );
500
+
501
+ register_block_type( 'wpo/wpmenucart-navigation', array(
502
+ 'editor_script' => 'wpmenucart-navigation-block',
503
+ 'render_callback' => array( $this, 'cart_navigation_block_output' ),
504
+ ) );
505
+ }
506
+
507
+ public function cart_navigation_block_output( $atts ) {
508
+ $menu = sprintf( '<ul>%s</ul>', $this->generate_menu_item_li( '', 'block' ) );
509
+ if ( $this->is_block_editor() ) {
510
+ // deactivate links when using the full site or block editor to prevent navigating away from the editor
511
+ $menu = preg_replace( '/(<[^>]+) href=".*?"/i', '$1', $menu );
512
+ }
513
+ return $menu;
514
+ }
515
+
516
+ public function is_rest_request() {
517
+ return defined( 'REST_REQUEST' ) && REST_REQUEST;
518
+ }
519
 
520
+ public function is_block_editor() {
521
+ if ( $this->is_rest_request() ) {
522
+ $route = untrailingslashit( $GLOBALS['wp']->query_vars['rest_route'] );
523
+ if ( strpos( $route, 'wpo/wpmenucart-navigation' ) !== false || strpos( $route, '/navigation' ) !== false ) {
524
+ return true;
525
+ }
526
+ }
527
+ return false;
528
  }
529
 
530
  /**
543
  add_filter( 'wp_nav_menu_' . $this->options['menu_slugs'][1] . '_items', array( &$this, 'add_itemcart_to_menu' ) , 10, 2 );
544
  }
545
  }
546
+
547
  /**
548
+ * Gets the menu item <li>
549
  *
550
+ * @param string $classes
551
+ * @param string $context can be 'classic' or 'block'
552
+ *
553
+ * @return string
554
  */
555
+ public function generate_menu_item_li( $classes, $context = 'classic' ) {
556
+ $classes .= ' wpmenucartli wpmenucart-display-'.$this->options['items_alignment'];
557
+ if ( function_exists( 'is_checkout' ) && function_exists( 'is_cart' ) && ( is_checkout() || is_cart() ) && empty( $this->options['show_on_cart_checkout_page'] ) ) {
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
558
  $classes .= ' hidden-wpmenucart';
559
  }
560
+ if ( $context == 'classic' ) {
561
+ $classes .= ' menu-item';
562
+ } elseif ( $context == 'block' ) {
563
+ $classes .= ' wp-block-navigation-item wp-block-navigation-link';
564
+ }
565
 
566
  // Filter for <li> item classes
567
  /* Usage (in the themes functions.php):
571
  return $classes;
572
  }
573
  */
574
+
575
  $classes = apply_filters( 'wpmenucart_menu_item_classes', $classes );
576
  $this->menu_items['menu']['menu_item_li_classes'] = $classes;
577
 
578
  // DEPRECATED: These filters are now deprecated in favour of the more precise filters in the functions!
579
+ $menu_item_li = apply_filters_deprecated( 'wpmenucart_menu_item_filter', array( $this->wpmenucart_menu_item() ), '2.5.3', '' );
 
 
 
 
580
 
581
+ return '<li class="'.$classes.'" id="wpmenucartli">' . $menu_item_li . '</li>';
582
+ }
583
+
584
+ /**
585
+ * Add Menu Cart to menu
586
+ *
587
+ * @return menu items + Menu Cart item
588
+ */
589
+ public function add_itemcart_to_menu( $items ) {
590
+ $common_classes = $this->get_common_li_classes( $items );
591
+ $menu_item_li = $this->generate_menu_item_li( $common_classes, 'classic' );
592
+
593
+ if ( apply_filters( 'wpmenucart_prepend_menu_item', false ) ) {
594
  $items = apply_filters( 'wpmenucart_menu_item_wrapper', $menu_item_li ) . $items;
595
  } else {
596
  $items .= apply_filters( 'wpmenucart_menu_item_wrapper', $menu_item_li );
664
  $item_data = $this->shop->menu_item();
665
 
666
  // Check empty cart settings
667
+ if ( $item_data['cart_contents_count'] == 0 && ! isset( $this->options['always_display'] ) && ! $this->is_block_editor() ) {
668
  $empty_menu_item = '<a class="wpmenucart-contents empty-wpmenucart" style="display:none">&nbsp;</a>';
669
  return $empty_menu_item;
670
  }