Advanced Sidebar Menu - Version 8.8.0

Version Description

  • Implement universal 'menu-item' style CSS classes to all menus.
  • Introduced Category::is_current_top_level_term method.
  • Introduced Category::get_current_ancestors method.
  • Introduced Category::is_current_term method.
  • Enabled PHPCS caching.
  • Required WordPress core version 5.4.0+.
  • Tested to WordPress 5.9.3.
Download this release

Release Info

Developer Mat Lipe
Plugin Icon 128x128 Advanced Sidebar Menu
Version 8.8.0
Comparing to
See all releases

Code changes from version 8.7.3 to 8.8.0

advanced-sidebar-menu.php CHANGED
@@ -4,12 +4,12 @@
4
  * Plugin URI: https://onpointplugins.com/advanced-sidebar-menu/
5
  * Description: Creates dynamic menus based on parent/child relationship of your pages or categories.
6
  * Author: OnPoint Plugins
7
- * Version: 8.7.3
8
  * Author URI: https://onpointplugins.com
9
  * Text Domain: advanced-sidebar-menu
10
  * Domain Path: /languages/
11
  * Network: false
12
- * Requires at least: 5.2
13
  * Requires PHP: 5.6.0
14
  *
15
  * @package advanced-sidebar-menu
@@ -19,7 +19,7 @@ if ( defined( 'ADVANCED_SIDEBAR_BASIC_VERSION' ) ) {
19
  return;
20
  }
21
 
22
- define( 'ADVANCED_SIDEBAR_BASIC_VERSION', '8.7.3' );
23
  define( 'ADVANCED_SIDEBAR_MENU_REQUIRED_PRO_VERSION', '8.5.0' );
24
  define( 'ADVANCED_SIDEBAR_DIR', plugin_dir_path( __FILE__ ) );
25
  define( 'ADVANCED_SIDEBAR_MENU_URL', plugin_dir_url( __FILE__ ) );
4
  * Plugin URI: https://onpointplugins.com/advanced-sidebar-menu/
5
  * Description: Creates dynamic menus based on parent/child relationship of your pages or categories.
6
  * Author: OnPoint Plugins
7
+ * Version: 8.8.0
8
  * Author URI: https://onpointplugins.com
9
  * Text Domain: advanced-sidebar-menu
10
  * Domain Path: /languages/
11
  * Network: false
12
+ * Requires at least: 5.4.0
13
  * Requires PHP: 5.6.0
14
  *
15
  * @package advanced-sidebar-menu
19
  return;
20
  }
21
 
22
+ define( 'ADVANCED_SIDEBAR_BASIC_VERSION', '8.8.0' );
23
  define( 'ADVANCED_SIDEBAR_MENU_REQUIRED_PRO_VERSION', '8.5.0' );
24
  define( 'ADVANCED_SIDEBAR_DIR', plugin_dir_path( __FILE__ ) );
25
  define( 'ADVANCED_SIDEBAR_MENU_URL', plugin_dir_url( __FILE__ ) );
readme.txt CHANGED
@@ -3,10 +3,10 @@
3
  Contributors: Mat Lipe, onpointplugins
4
  Donate link: https://www.paypal.com/cgi-bin/webscr?cmd=_donations&business=paypal%40onpointplugins%2ecom&lc=US&item_name=Advanced%20Sidebar%20Menu&no_note=0&currency_code=USD&bn=PP%2dDonationsBF%3abtn_donateCC_LG%2egif%3aNonHostedGuest
5
  Tags: menus, sidebar menu, hierarchy, category menu, pages menu, dynamic
6
- Requires at least: 5.2.0
7
- Tested up to: 5.9.2
8
  Requires PHP: 5.6.0
9
- Stable tag: 8.7.3
10
 
11
  == Description ==
12
 
@@ -161,6 +161,15 @@ Yes. Based on whatever page, post or category you are on, the menu will change a
161
 
162
 
163
  == Changelog ==
 
 
 
 
 
 
 
 
 
164
  = 8.7.3 =
165
  * Include WP core version in debug info.
166
  * Tested to WordPress 5.9.2.
3
  Contributors: Mat Lipe, onpointplugins
4
  Donate link: https://www.paypal.com/cgi-bin/webscr?cmd=_donations&business=paypal%40onpointplugins%2ecom&lc=US&item_name=Advanced%20Sidebar%20Menu&no_note=0&currency_code=USD&bn=PP%2dDonationsBF%3abtn_donateCC_LG%2egif%3aNonHostedGuest
5
  Tags: menus, sidebar menu, hierarchy, category menu, pages menu, dynamic
6
+ Requires at least: 5.4.0
7
+ Tested up to: 6.0.0
8
  Requires PHP: 5.6.0
9
+ Stable tag: 8.8.0
10
 
11
  == Description ==
12
 
161
 
162
 
163
  == Changelog ==
164
+ = 8.8.0 =
165
+ * Implement universal 'menu-item' style CSS classes to all menus.
166
+ * Introduced `Category::is_current_top_level_term` method.
167
+ * Introduced `Category::get_current_ancestors` method.
168
+ * Introduced `Category::is_current_term` method.
169
+ * Enabled PHPCS caching.
170
+ * Required WordPress core version 5.4.0+.
171
+ * Tested to WordPress 5.9.3.
172
+
173
  = 8.7.3 =
174
  * Include WP core version in debug info.
175
  * Tested to WordPress 5.9.2.
src/List_Pages.php CHANGED
@@ -103,7 +103,7 @@ class List_Pages {
103
  *
104
  * Follow existing WP core pattern for `wp_list_pages`.
105
  *
106
- * WP core handles pages, but it doesn't handle custom custom post types.
107
  * We cover pages as well to handle edge cases where a site's theme
108
  * changes/removes default classes.
109
  *
@@ -113,6 +113,7 @@ class List_Pages {
113
  * @return array
114
  */
115
  public function add_list_item_classes( $classes, \WP_Post $post ) {
 
116
  $children = $this->get_child_pages( $post->ID, $post->ID === $this->top_parent_id );
117
  if ( ! empty( $children ) ) {
118
  $classes[] = 'has_children';
@@ -120,9 +121,11 @@ class List_Pages {
120
  if ( ! empty( $this->get_current_page_id() ) ) {
121
  if ( $this->get_current_page_id() === $post->ID ) {
122
  $classes[] = 'current_page_item';
 
123
  } elseif ( $this->current_page->post_parent === $post->ID ) {
124
  $classes[] = 'current_page_parent';
125
  $classes[] = 'current_page_ancestor';
 
126
  } else {
127
  $ancestors = get_post_ancestors( $this->get_current_page_id() );
128
  if ( ! empty( $ancestors ) && \in_array( $post->ID, $ancestors, true ) ) {
103
  *
104
  * Follow existing WP core pattern for `wp_list_pages`.
105
  *
106
+ * WP core handles pages, but it doesn't handle custom post types.
107
  * We cover pages as well to handle edge cases where a site's theme
108
  * changes/removes default classes.
109
  *
113
  * @return array
114
  */
115
  public function add_list_item_classes( $classes, \WP_Post $post ) {
116
+ $classes[] = 'menu-item';
117
  $children = $this->get_child_pages( $post->ID, $post->ID === $this->top_parent_id );
118
  if ( ! empty( $children ) ) {
119
  $classes[] = 'has_children';
121
  if ( ! empty( $this->get_current_page_id() ) ) {
122
  if ( $this->get_current_page_id() === $post->ID ) {
123
  $classes[] = 'current_page_item';
124
+ $classes[] = 'current-menu-item';
125
  } elseif ( $this->current_page->post_parent === $post->ID ) {
126
  $classes[] = 'current_page_parent';
127
  $classes[] = 'current_page_ancestor';
128
+ $classes[] = 'current-menu-parent';
129
  } else {
130
  $ancestors = get_post_ancestors( $this->get_current_page_id() );
131
  if ( ! empty( $ancestors ) && \in_array( $post->ID, $ancestors, true ) ) {
src/Menus/Category.php CHANGED
@@ -3,6 +3,7 @@
3
  namespace Advanced_Sidebar_Menu\Menus;
4
 
5
  use Advanced_Sidebar_Menu\Core;
 
6
  use Advanced_Sidebar_Menu\Walkers\Category_Walker;
7
 
8
  /**
@@ -12,18 +13,13 @@ use Advanced_Sidebar_Menu\Walkers\Category_Walker;
12
  * @since 7.0.0
13
  */
14
  class Category extends Menu_Abstract {
 
 
15
  const WIDGET = 'category';
16
 
17
  const DISPLAY_ON_SINGLE = 'single';
18
  const EACH_CATEGORY_DISPLAY = 'new_widget';
19
 
20
- /**
21
- * Parents and grandparents of current term.
22
- *
23
- * @var array
24
- */
25
- public $ancestors = [];
26
-
27
  /**
28
  * Top_level_term.
29
  *
@@ -93,12 +89,48 @@ class Category extends Menu_Abstract {
93
 
94
 
95
  /**
96
- * Get the first level child terms.
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
97
  *
98
- * $this->set_current_top_level_term() most likely should be called
99
- * before this.
100
  *
101
- * @see \Advanced_Sidebar_Menu\Menus\Category::set_current_top_level_term()
 
 
 
 
 
 
 
 
 
 
 
102
  *
103
  * @return \WP_Term[]
104
  */
@@ -281,14 +313,27 @@ class Category extends Menu_Abstract {
281
  public function is_tax() {
282
  $taxonomy = $this->get_taxonomy();
283
  if ( 'category' === $taxonomy ) {
284
- if ( is_category() ) {
285
- return true;
286
- }
287
- } elseif ( is_tax( $taxonomy ) ) {
288
- return true;
289
  }
290
 
291
- return false;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
292
  }
293
 
294
 
@@ -330,31 +375,41 @@ class Category extends Menu_Abstract {
330
  * @return int
331
  */
332
  public function get_highest_parent( $term_id ) {
333
- $this->ancestors = \array_reverse( get_ancestors( $term_id, $this->get_taxonomy(), 'taxonomy' ) );
334
- // Store current term at the end ancestors for backward compatibility.
335
- $this->ancestors[] = $term_id;
336
 
337
- return reset( $this->ancestors );
338
  }
339
 
340
 
341
  /**
342
- * If a category has children add the 'has_children' class
343
- * to the list item.
344
  *
345
- * @param array $classes - List of classes added to category list item.
346
  * @param \WP_Term $category - Current category.
347
  *
348
  * @filter category_css_class 11 2
349
  *
350
  * @return array
351
  */
352
- public function add_has_children_category_class( $classes, $category ) {
 
353
  if ( $this->has_children( $category ) ) {
354
  $classes[] = 'has_children';
355
  }
356
 
357
- return array_unique( $classes );
 
 
 
 
 
 
 
 
 
358
  }
359
 
360
 
@@ -380,6 +435,23 @@ class Category extends Menu_Abstract {
380
  }
381
 
382
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
383
  /**
384
  * Is this term an ancestor of the current term?
385
  * Does this term have children?
@@ -390,7 +462,8 @@ class Category extends Menu_Abstract {
390
  */
391
  public function is_current_term_ancestor( \WP_Term $term ) {
392
  $return = false;
393
- if ( (int) $term->term_id === (int) $this->top_level_term->term_id || in_array( $term->term_id, $this->ancestors, false ) ) { //phpcs:ignore WordPress.PHP.StrictInArray.FoundNonStrictFalse
 
394
  $children = get_term_children( $term->term_id, $this->get_taxonomy() );
395
  if ( ! empty( $children ) ) {
396
  $return = true;
@@ -429,7 +502,7 @@ class Category extends Menu_Abstract {
429
  return;
430
  }
431
 
432
- add_filter( 'category_css_class', [ $this, 'add_has_children_category_class' ], 11, 2 );
433
 
434
  $menu_open = false;
435
  $close_menu = false;
3
  namespace Advanced_Sidebar_Menu\Menus;
4
 
5
  use Advanced_Sidebar_Menu\Core;
6
+ use Advanced_Sidebar_Menu\Traits\Memoize;
7
  use Advanced_Sidebar_Menu\Walkers\Category_Walker;
8
 
9
  /**
13
  * @since 7.0.0
14
  */
15
  class Category extends Menu_Abstract {
16
+ use Memoize;
17
+
18
  const WIDGET = 'category';
19
 
20
  const DISPLAY_ON_SINGLE = 'single';
21
  const EACH_CATEGORY_DISPLAY = 'new_widget';
22
 
 
 
 
 
 
 
 
23
  /**
24
  * Top_level_term.
25
  *
89
 
90
 
91
  /**
92
+ * Get all ancestor of the current term or terms assigned
93
+ * to the current post.
94
+ *
95
+ * @since 8.8.0
96
+ *
97
+ * @return array
98
+ */
99
+ public function get_current_ancestors() {
100
+ return $this->once( function() {
101
+ $included = $this->get_included_term_ids();
102
+ $ancestors = [];
103
+ foreach ( $included as $term_id ) {
104
+ $term_ancestors = \array_reverse( get_ancestors( $term_id, $this->get_taxonomy(), 'taxonomy' ) );
105
+ // All the post's assigned categories are considered ancestors.
106
+ if ( ! $this->is_tax() ) {
107
+ $term_ancestors[] = $term_id;
108
+ }
109
+ $ancestors[] = $term_ancestors;
110
+ }
111
+
112
+ return \array_merge( ...$ancestors );
113
+ }, __METHOD__, [] );
114
+ }
115
+
116
+
117
+ /**
118
+ * Get the current item if available.
119
  *
120
+ * @since 8.8.0
 
121
  *
122
+ * @return ?\WP_Term
123
+ */
124
+ public function get_current_term() {
125
+ if ( $this->is_tax() ) {
126
+ return get_queried_object();
127
+ }
128
+ return null;
129
+ }
130
+
131
+
132
+ /**
133
+ * Get the first level child terms.
134
  *
135
  * @return \WP_Term[]
136
  */
313
  public function is_tax() {
314
  $taxonomy = $this->get_taxonomy();
315
  if ( 'category' === $taxonomy ) {
316
+ return is_category();
 
 
 
 
317
  }
318
 
319
+ return is_tax( $taxonomy );
320
+ }
321
+
322
+
323
+ /**
324
+ * Is a term our current top level term?
325
+ *
326
+ * @param \WP_Term $term - Term to check against.
327
+ *
328
+ * @since 8.8.0
329
+ *
330
+ * @return bool
331
+ */
332
+ public function is_current_top_level_term( \WP_Term $term ) {
333
+ if ( null === $this->top_level_term ) {
334
+ return false;
335
+ }
336
+ return ( (int) $term->term_id ) === ( (int) $this->top_level_term->term_id );
337
  }
338
 
339
 
375
  * @return int
376
  */
377
  public function get_highest_parent( $term_id ) {
378
+ $ancestors = \array_reverse( get_ancestors( $term_id, $this->get_taxonomy(), 'taxonomy' ) );
379
+ // Use current term if no ancestors available.
380
+ $ancestors[] = $term_id;
381
 
382
+ return reset( $ancestors );
383
  }
384
 
385
 
386
  /**
387
+ * Add various classes to category item to define it among levels
388
+ * as well as current item state.
389
  *
390
+ * @param array $classes - List of classes added to category list item.
391
  * @param \WP_Term $category - Current category.
392
  *
393
  * @filter category_css_class 11 2
394
  *
395
  * @return array
396
  */
397
+ public function add_list_item_classes( $classes, $category ) {
398
+ $classes[] = 'menu-item';
399
  if ( $this->has_children( $category ) ) {
400
  $classes[] = 'has_children';
401
  }
402
 
403
+ if ( $this->is_current_term( $category ) ) {
404
+ $classes[] = 'current-menu-item';
405
+ } else {
406
+ $current = $this->get_current_term();
407
+ if ( null !== $current && $current->parent === $category->term_id ) {
408
+ $classes[] = 'current-menu-parent';
409
+ }
410
+ }
411
+
412
+ return \array_unique( $classes );
413
  }
414
 
415
 
435
  }
436
 
437
 
438
+ /**
439
+ * Is a term the currently viewed term?
440
+ *
441
+ * @param \WP_Term $term - Term to check against.
442
+ *
443
+ * @since 8.8.0
444
+ *
445
+ * @return bool
446
+ */
447
+ public function is_current_term( \WP_Term $term ) {
448
+ if ( ! $this->is_tax() ) {
449
+ return false;
450
+ }
451
+ return get_queried_object_id() === $term->term_id;
452
+ }
453
+
454
+
455
  /**
456
  * Is this term an ancestor of the current term?
457
  * Does this term have children?
462
  */
463
  public function is_current_term_ancestor( \WP_Term $term ) {
464
  $return = false;
465
+
466
+ if ( $this->is_current_top_level_term( $term ) || \in_array( $term->term_id, $this->get_current_ancestors(), true ) ) {
467
  $children = get_term_children( $term->term_id, $this->get_taxonomy() );
468
  if ( ! empty( $children ) ) {
469
  $return = true;
502
  return;
503
  }
504
 
505
+ add_filter( 'category_css_class', [ $this, 'add_list_item_classes' ], 11, 2 );
506
 
507
  $menu_open = false;
508
  $close_menu = false;
src/Notice.php CHANGED
@@ -84,7 +84,7 @@ class Notice {
84
  ?>
85
  <div class="advanced-sidebar-menu-column-box advanced-sidebar-info-panel">
86
  <h3>
87
- <a href="https://onpointplugins.com/product/advanced-sidebar-menu-pro/">
88
  <?php esc_html_e( 'Advanced Sidebar Menu PRO', 'advanced-sidebar-menu' ); ?>
89
  </a>
90
  </h3>
84
  ?>
85
  <div class="advanced-sidebar-menu-column-box advanced-sidebar-info-panel">
86
  <h3>
87
+ <a href="https://onpointplugins.com/product/advanced-sidebar-menu-pro/?utm_source=widget-title&utm_campaign=gopro&utm_medium=wp-dash">
88
  <?php esc_html_e( 'Advanced Sidebar Menu PRO', 'advanced-sidebar-menu' ); ?>
89
  </a>
90
  </h3>
src/Widget/Category.php CHANGED
@@ -77,13 +77,16 @@ class Category extends Widget_Abstract {
77
  $hooked = true;
78
 
79
  add_action( 'advanced-sidebar-menu/widget/category/left-column', [ $this, 'box_display' ], 5, 2 );
80
- add_action( 'advanced-sidebar-menu/widget/category/left-column', [ $this, 'box_display_on_single_posts' ], 15, 2 );
 
 
 
81
  add_action( 'advanced-sidebar-menu/widget/category/left-column', [ $this, 'box_exclude' ], 20, 2 );
82
  }
83
 
84
 
85
  /**
86
- * Get the label for use taxonomy.
87
  *
88
  * For adjusting widget option labels.
89
  *
77
  $hooked = true;
78
 
79
  add_action( 'advanced-sidebar-menu/widget/category/left-column', [ $this, 'box_display' ], 5, 2 );
80
+ add_action( 'advanced-sidebar-menu/widget/category/left-column', [
81
+ $this,
82
+ 'box_display_on_single_posts',
83
+ ], 15, 2 );
84
  add_action( 'advanced-sidebar-menu/widget/category/left-column', [ $this, 'box_exclude' ], 20, 2 );
85
  }
86
 
87
 
88
  /**
89
+ * Get the label for set taxonomy.
90
  *
91
  * For adjusting widget option labels.
92
  *
views/category_list.php CHANGED
@@ -1,12 +1,12 @@
1
  <?php
2
  /**
3
- * The output of the "Advanced Sidebar Categories Menu" widget
4
  *
5
- * @since 8.4.0
6
  *
7
  * To edit copy this file to a folder in your theme called 'advanced-sidebar-menu' and edit at will.
8
  *
9
- * @notice Do NOT edit this file in this location, or it will break on update.
10
  *
11
  * @package advanced-sidebar-menu
12
  */
@@ -40,7 +40,7 @@ if ( ! empty( $child_terms ) ) {
40
  $content .= $current_menu->openListItem( wp_list_categories( $list_args ) );
41
 
42
  // Grandchild terms.
43
- if ( $current_menu->is_current_term_ancestor( $_term ) && $current_menu->has_children( $_term ) ) {
44
  $content .= '<ul class="grandchild-sidebar-menu children" data-level="2">';
45
 
46
  $list_args = $current_menu->get_list_categories_args( Category::LEVEL_GRANDCHILD, $_term );
1
  <?php
2
  /**
3
+ * The output of the "Advanced Sidebar - Categories" widget.
4
  *
5
+ * @since 8.8.0
6
  *
7
  * To edit copy this file to a folder in your theme called 'advanced-sidebar-menu' and edit at will.
8
  *
9
+ * @notice Do NOT edit this file in this location, or it will break on plugin update.
10
  *
11
  * @package advanced-sidebar-menu
12
  */
40
  $content .= $current_menu->openListItem( wp_list_categories( $list_args ) );
41
 
42
  // Grandchild terms.
43
+ if ( $current_menu->is_current_term( $_term ) || $current_menu->is_current_term_ancestor( $_term ) ) {
44
  $content .= '<ul class="grandchild-sidebar-menu children" data-level="2">';
45
 
46
  $list_args = $current_menu->get_list_categories_args( Category::LEVEL_GRANDCHILD, $_term );