| 1 |
<?php |
| 2 |
namespace Whols\Admin; |
| 3 |
|
| 4 |
class Menu_Manager { |
| 5 |
public function __construct() { |
| 6 |
// Hook run orders: init -> admin_menu -> custom_menu_order -> parent_file |
| 7 |
add_action( 'admin_menu', [ $this, 'admin_menu' ] ); |
| 8 |
|
| 9 |
// Set the taxonomy as submenu. |
| 10 |
add_action( 'admin_menu', array( $this, 'customer_roles_submenu') ); |
| 11 |
|
| 12 |
// custom_menu_order action hook |
| 13 |
add_action( 'custom_menu_order', array( $this, 'custom_menu_order') ); |
| 14 |
|
| 15 |
// Highlight the submenu when active this page. |
| 16 |
add_action( 'parent_file', array( $this, 'fix_submenu_hilight') ); |
| 17 |
} |
| 18 |
|
| 19 |
/** |
| 20 |
* Register admin menu |
| 21 |
* |
| 22 |
* @return void |
| 23 |
*/ |
| 24 |
public function admin_menu() { |
| 25 |
add_menu_page( |
| 26 |
__( 'Whols', 'whols' ), |
| 27 |
__( 'Whols', 'whols' ), |
| 28 |
'manage_options', |
| 29 |
'whols-admin', |
| 30 |
[ $this, 'plugin_page' ], |
| 31 |
'dashicons-money-alt', |
| 32 |
'55.8' |
| 33 |
); |
| 34 |
|
| 35 |
// After initialization of the post type, the post type menu added in 0 index. |
| 36 |
// Fixed that issue by registering the submenu page. |
| 37 |
add_submenu_page( 'whols-admin', esc_html__('Whols Admin', 'whols'), esc_html__( 'Settings', 'whols' ), 'manage_options','admin.php?page=whols-admin', '', 0); |
| 38 |
|
| 39 |
} |
| 40 |
|
| 41 |
/** |
| 42 |
* Plugin page |
| 43 |
* |
| 44 |
* @return void |
| 45 |
*/ |
| 46 |
public function plugin_page() { |
| 47 |
?> |
| 48 |
<div class="wrap"> |
| 49 |
<div id="whols-vue-settings-app"></div> |
| 50 |
</div> |
| 51 |
<?php |
| 52 |
} |
| 53 |
|
| 54 |
/** |
| 55 |
* Create submenu for customer roles taxonomy. |
| 56 |
*/ |
| 57 |
public function customer_roles_submenu() { |
| 58 |
$capabilities = whols_get_capabilities(); |
| 59 |
|
| 60 |
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', '', null); |
| 61 |
} |
| 62 |
|
| 63 |
/** |
| 64 |
* Custom menu order. |
| 65 |
*/ |
| 66 |
public function custom_menu_order( $menu_order ) { |
| 67 |
global $menu, $submenu; |
| 68 |
|
| 69 |
$enable_conversation = whols_get_option('enable_conversation'); |
| 70 |
|
| 71 |
// No need change menu order. |
| 72 |
if( !$enable_conversation ){ |
| 73 |
return; |
| 74 |
} |
| 75 |
|
| 76 |
global $submenu; |
| 77 |
|
| 78 |
$conversation_menu = $submenu['whols-admin'][2]; |
| 79 |
$wholesaler_roles_menu = $submenu['whols-admin'][3]; |
| 80 |
|
| 81 |
// Add counter badge to conversation menu. |
| 82 |
$conversation_menu[0] = $conversation_menu[0] . ' <span class="awaiting-mod">' . whols_get_conversation_count( 'unread' ) . '</span>'; |
| 83 |
|
| 84 |
// Swap order of conversation and wholesaler roles menu. |
| 85 |
$submenu['whols-admin'][2] = $wholesaler_roles_menu; |
| 86 |
$submenu['whols-admin'][3] = $conversation_menu; |
| 87 |
} |
| 88 |
|
| 89 |
public function get_pending_request_count(){ |
| 90 |
$query = new \WP_Query(array( |
| 91 |
'post_type' => 'whols_user_request', |
| 92 |
'meta_query' => array( |
| 93 |
'relation' => 'AND', |
| 94 |
array( |
| 95 |
'key' => 'whols_user_request_meta', |
| 96 |
'value' => serialize('approve'), |
| 97 |
'compare' => 'NOT LIKE', |
| 98 |
), |
| 99 |
array( |
| 100 |
'key' => 'whols_user_request_meta', |
| 101 |
'value' => serialize('reject'), |
| 102 |
'compare' => 'NOT LIKE', |
| 103 |
), |
| 104 |
), |
| 105 |
)); |
| 106 |
|
| 107 |
$pending_request_count = $query->post_count; |
| 108 |
wp_reset_postdata(); |
| 109 |
|
| 110 |
return $pending_request_count; |
| 111 |
} |
| 112 |
|
| 113 |
/** |
| 114 |
* Highlight the submenu when active this page. |
| 115 |
*/ |
| 116 |
public function fix_submenu_hilight( $parent_file ) { |
| 117 |
global $current_screen, $parent_file, $submenu_file, $submenu; // Defined in wp-admin/menu-header.php _wp_menu_output function. |
| 118 |
|
| 119 |
// Fix Wholesaler Roles submenu does not highlight. |
| 120 |
$taxonomy = $current_screen->taxonomy; |
| 121 |
if ( $taxonomy == 'whols_role_cat' ) { |
| 122 |
$parent_file = 'whols-admin'; |
| 123 |
} |
| 124 |
|
| 125 |
return $parent_file; |
| 126 |
} |
| 127 |
} |