Source: includes/core/um-filters-navmenu.php

  1. <?php
  2. if ( ! defined( 'ABSPATH' ) ) {
  3. exit;
  4. }
  5. if ( ! is_admin() ) {
  6. /**
  7. * Add dynamic profile headers
  8. *
  9. * @param array $sorted_menu_items
  10. * @param \stdClass $args
  11. *
  12. * @return array
  13. */
  14. function um_add_custom_message_to_menu( $sorted_menu_items, $args ) {
  15. if ( empty( $sorted_menu_items ) ) {
  16. return $sorted_menu_items;
  17. }
  18. if ( is_user_logged_in() ) {
  19. um_fetch_user( get_current_user_id() );
  20. }
  21. foreach ( $sorted_menu_items as &$menu_item ) {
  22. if ( ! empty( $menu_item->title ) ) {
  23. $menu_item->title = UM()->shortcodes()->convert_user_tags( $menu_item->title );
  24. }
  25. if ( ! empty( $menu_item->attr_title ) ) {
  26. $menu_item->attr_title = UM()->shortcodes()->convert_user_tags( $menu_item->attr_title );
  27. }
  28. if ( ! empty( $menu_item->description ) ) {
  29. $menu_item->description = UM()->shortcodes()->convert_user_tags( $menu_item->description );
  30. }
  31. }
  32. if ( is_user_logged_in() ) {
  33. um_reset_user();
  34. }
  35. return $sorted_menu_items;
  36. }
  37. add_filter( 'wp_nav_menu_objects', 'um_add_custom_message_to_menu', 9999, 2 );
  38. /**
  39. * Conditional menu items
  40. *
  41. * @param $menu_items
  42. * @param $args
  43. *
  44. * @return mixed
  45. */
  46. function um_conditional_nav_menu( $menu_items, $args ) {
  47. //if empty
  48. if ( empty( $menu_items ) ) {
  49. return $menu_items;
  50. }
  51. um_fetch_user( get_current_user_id() );
  52. $filtered_items = array();
  53. $hide_children_of = array();
  54. //other filter
  55. foreach ( $menu_items as $item ) {
  56. if ( empty( $item->ID ) ) {
  57. // Left item with empty ID for the cases like in MegaMenu when generated submenu doesn't have the menu item ID.
  58. if ( ! empty( $item->is_mega_menu ) ) {
  59. if ( isset( $item->menu_item_parent ) && in_array( absint( $item->menu_item_parent ), $hide_children_of, true ) ) {
  60. continue;
  61. }
  62. $filtered_items[] = $item;
  63. }
  64. continue;
  65. }
  66. $mode = get_post_meta( $item->ID, 'menu-item-um_nav_public', true );
  67. $roles = get_post_meta( $item->ID, 'menu-item-um_nav_roles', true );
  68. $visible = true;
  69. // Hide any item that is the child of a hidden item.
  70. if ( isset( $item->menu_item_parent ) && in_array( absint( $item->menu_item_parent ), $hide_children_of, true ) ) {
  71. $visible = false;
  72. $hide_children_of[] = absint( $item->ID ); // for nested menus
  73. }
  74. if ( isset( $mode ) && $visible ) {
  75. switch ( $mode ) {
  76. case 2:
  77. if ( ! empty( $roles ) && is_user_logged_in() ) {
  78. $current_user_roles = um_user( 'roles' );
  79. if ( empty( $current_user_roles ) ) {
  80. $visible = false;
  81. } else {
  82. $visible = ( count( array_intersect( $current_user_roles, (array) $roles ) ) > 0 ) ? true : false;
  83. }
  84. } else {
  85. $visible = is_user_logged_in();
  86. }
  87. break;
  88. case 1:
  89. $visible = ! is_user_logged_in();
  90. break;
  91. }
  92. }
  93. /**
  94. * Filters menu item visibility base on Ultimate Member visibility settings.
  95. *
  96. * @since 1.3.x
  97. * @hook um_nav_menu_roles_item_visibility
  98. *
  99. * @param {bool} $visible Menu item visibility.
  100. * @param {object} $item Menu item instance.
  101. *
  102. * @return {bool} Menu item visibility.
  103. *
  104. * @example <caption>Make the menu item visible for some custom reason.</caption>
  105. * function my_nav_menu_roles_item_visibility( $visible, $item ) {
  106. * $visible = true;
  107. * return $visible;
  108. * }
  109. * add_filter( 'um_nav_menu_roles_item_visibility', 'my_nav_menu_roles_item_visibility', 10, 2 );
  110. */
  111. $visible = apply_filters( 'um_nav_menu_roles_item_visibility', $visible, $item );
  112. // unset non-visible item
  113. if ( ! $visible ) {
  114. $hide_children_of[] = absint( $item->ID ); // store ID of item
  115. } else {
  116. $filtered_items[] = $item;
  117. }
  118. }
  119. um_reset_user();
  120. return $filtered_items;
  121. }
  122. add_filter( 'wp_nav_menu_objects', 'um_conditional_nav_menu', 9999, 2 );
  123. /**
  124. * Conditional menu items
  125. *
  126. * @param $items
  127. * @param $menu
  128. * @param $args
  129. *
  130. * @return mixed
  131. */
  132. function um_get_nav_menu_items( $items, $menu, $args ) {
  133. return um_conditional_nav_menu( $items, $args );
  134. }
  135. add_filter( 'wp_get_nav_menu_items', 'um_get_nav_menu_items', 9999, 3 );
  136. }