| 1 |
<?php |
| 2 |
namespace Whols\Admin; |
| 3 |
|
| 4 |
class Menu_Manager { |
| 5 |
public function __construct() { |
| 6 |
add_action( 'admin_menu', [ $this, 'register_main_menu' ] ); |
| 7 |
|
| 8 |
// Set settings page as submenu |
| 9 |
add_action( 'admin_menu', array( $this, 'add_submenu_pages' ), 30 ); |
| 10 |
|
| 11 |
// custom_menu_order action hook |
| 12 |
// add_action( 'custom_menu_order', array( $this, 'custom_menu_order') ); |
| 13 |
|
| 14 |
// Highlight the submenu when active this page. |
| 15 |
add_action( 'parent_file', array( $this, 'fix_submenu_hilight') ); |
| 16 |
|
| 17 |
// Set active class using js |
| 18 |
add_action('admin_footer', [ $this, 'menu_item_active_js' ]); |
| 19 |
} |
| 20 |
|
| 21 |
/** |
| 22 |
* Register admin menu |
| 23 |
* |
| 24 |
* @return void |
| 25 |
*/ |
| 26 |
public function register_main_menu() { |
| 27 |
add_menu_page( |
| 28 |
__( 'Whols', 'whols' ), |
| 29 |
__( 'Whols', 'whols' ), |
| 30 |
'manage_options', |
| 31 |
'whols-admin', |
| 32 |
[ $this, 'plugin_page' ], |
| 33 |
'dashicons-money-alt', |
| 34 |
'58.5' |
| 35 |
); |
| 36 |
} |
| 37 |
|
| 38 |
/** |
| 39 |
* Set settings page as submenu |
| 40 |
* |
| 41 |
* @since 1.0.0 |
| 42 |
*/ |
| 43 |
function add_submenu_pages(){ |
| 44 |
global $menu, $submenu; |
| 45 |
$capabilities = whols_get_capabilities(); |
| 46 |
|
| 47 |
$query = new \WP_Query(array( |
| 48 |
'post_type' => 'whols_user_request', |
| 49 |
'meta_query' => array( |
| 50 |
'relation' => 'AND', |
| 51 |
array( |
| 52 |
'key' => 'whols_user_request_meta', |
| 53 |
'value' => serialize('approve'), |
| 54 |
'compare' => 'NOT LIKE', |
| 55 |
), |
| 56 |
array( |
| 57 |
'key' => 'whols_user_request_meta', |
| 58 |
'value' => serialize('reject'), |
| 59 |
'compare' => 'NOT LIKE', |
| 60 |
), |
| 61 |
), |
| 62 |
)); |
| 63 |
|
| 64 |
$pending_request_count = $query->post_count; |
| 65 |
wp_reset_postdata(); |
| 66 |
|
| 67 |
// Add pending request count to menu |
| 68 |
if($pending_request_count > 0){ |
| 69 |
$menu['58.5'][0] = 'Whols <span class="update-plugins whols_request_count"><span>'. $pending_request_count .'</span></span>'; |
| 70 |
} |
| 71 |
|
| 72 |
// Dashboard |
| 73 |
add_submenu_page( 'whols-admin', esc_html__('Dashboard', 'whols'), esc_html__('Dashboard', 'whols'), $capabilities['manage_settings'], 'admin.php?page=whols-admin#/dashboard', '', 0); |
| 74 |
|
| 75 |
// Settings |
| 76 |
add_submenu_page( 'whols-admin', 'Whols Admin', esc_html__( 'Settings', 'whols' ), $capabilities['manage_settings'], 'admin.php?page=whols-admin#/settings/general', '', 1); |
| 77 |
|
| 78 |
// Wholesaler requestes (reststered from post type) |
| 79 |
|
| 80 |
// Wholesaler Roles |
| 81 |
add_submenu_page( 'whols-admin', esc_html__('Wholesaler Roles', 'whols'), esc_html__('Wholesaler Roles', 'whols'), $capabilities['manage_roles'], 'edit-tags.php?taxonomy=whols_role_cat', '', 3); |
| 82 |
} |
| 83 |
|
| 84 |
/** |
| 85 |
* Plugin page |
| 86 |
* |
| 87 |
* @return void |
| 88 |
*/ |
| 89 |
public function plugin_page() { |
| 90 |
?> |
| 91 |
<div class="wrap"> |
| 92 |
<div id="whols-vue-settings-app"></div> |
| 93 |
</div> |
| 94 |
<?php |
| 95 |
} |
| 96 |
|
| 97 |
/** |
| 98 |
* Custom menu order. |
| 99 |
*/ |
| 100 |
public function custom_menu_order( $menu_order ) { |
| 101 |
$enable_conversation = whols_get_option('enable_conversation'); |
| 102 |
|
| 103 |
// No need change menu order. |
| 104 |
if( !$enable_conversation ){ |
| 105 |
return; |
| 106 |
} |
| 107 |
|
| 108 |
global $submenu; |
| 109 |
|
| 110 |
$conversation_menu = $submenu['whols-admin'][2]; |
| 111 |
$wholesaler_roles_menu = $submenu['whols-admin'][3]; |
| 112 |
|
| 113 |
// Add counter badge to conversation menu. |
| 114 |
$conversation_menu[0] = $conversation_menu[0] . ' <span class="awaiting-mod">' . whols_get_conversation_count( 'unread' ) . '</span>'; |
| 115 |
|
| 116 |
// Swap order of conversation and wholesaler roles menu. |
| 117 |
$submenu['whols-admin'][2] = $wholesaler_roles_menu; |
| 118 |
$submenu['whols-admin'][3] = $conversation_menu; |
| 119 |
} |
| 120 |
|
| 121 |
/** |
| 122 |
* Highlight the submenu when active this page. |
| 123 |
*/ |
| 124 |
public function fix_submenu_hilight( $parent_file ) { |
| 125 |
global $current_screen, $parent_file, $submenu_file; // Defined in wp-admin/menu-header.php _wp_menu_output function. |
| 126 |
|
| 127 |
// Fix Settings submenu does not highlight. |
| 128 |
// if( $current_screen->base === 'toplevel_page_whols-admin' ){ |
| 129 |
// $submenu_file = 'admin.php?page=whols-admin'; |
| 130 |
// } |
| 131 |
|
| 132 |
// Fix Wholesaler Roles submenu does not highlight. |
| 133 |
$taxonomy = $current_screen->taxonomy; |
| 134 |
if ( $taxonomy == 'whols_role_cat' ) { |
| 135 |
$parent_file = 'whols-admin'; |
| 136 |
} |
| 137 |
|
| 138 |
return $parent_file; |
| 139 |
} |
| 140 |
|
| 141 |
public function menu_item_active_js(){ |
| 142 |
$submenu_items = 'li.toplevel_page_whols-admin ul.wp-submenu li:nth-child(2), li.toplevel_page_whols-admin ul.wp-submenu li:nth-child(3)'; |
| 143 |
?> |
| 144 |
<script> |
| 145 |
jQuery(document).ready(function($) { |
| 146 |
const $subMenuItems = $('<?php echo wp_kses_post($submenu_items); ?>'); |
| 147 |
|
| 148 |
// Function to handle menu activation |
| 149 |
const activateMenuItem = (hash) => { |
| 150 |
// Remove active class from all menu items first |
| 151 |
$subMenuItems.removeClass('current active'); |
| 152 |
|
| 153 |
// Find and activate the matching menu item |
| 154 |
$subMenuItems.each(function() { |
| 155 |
const subMenuLink = $(this).find('a').attr('href'); |
| 156 |
if (hash && subMenuLink.indexOf(hash) > -1) { |
| 157 |
$(this).addClass('current active'); |
| 158 |
} |
| 159 |
}); |
| 160 |
}; |
| 161 |
|
| 162 |
// Initialize for page load |
| 163 |
activateMenuItem(window.location.hash); |
| 164 |
|
| 165 |
// Add click event handler to menu items |
| 166 |
$subMenuItems.on('click', function() { |
| 167 |
const clickedItemHref = $(this).find('a').attr('href'); |
| 168 |
const hashPart = clickedItemHref.split('#')[1]; |
| 169 |
|
| 170 |
if (hashPart) { |
| 171 |
// Use timeout to ensure this runs after the default navigation |
| 172 |
setTimeout(() => { |
| 173 |
activateMenuItem('#' + hashPart); |
| 174 |
}, 50); |
| 175 |
} |
| 176 |
}); |
| 177 |
|
| 178 |
// Listen for hash changes to handle browser navigation |
| 179 |
$(window).on('hashchange', function() { |
| 180 |
activateMenuItem(window.location.hash); |
| 181 |
}); |
| 182 |
}); |
| 183 |
</script> |
| 184 |
<?php |
| 185 |
} |
| 186 |
} |