| 1 |
<?php /** |
| 2 |
* @version 1.1 |
| 3 |
* @package Any |
| 4 |
* @category Menu in Admin Panel |
| 5 |
* @author wpdevelop |
| 6 |
* |
| 7 |
* @web-site https://wpbookingcalendar.com/ |
| 8 |
* @email info@wpbookingcalendar.com |
| 9 |
* |
| 10 |
* @modified 2015-11-02 |
| 11 |
*/ |
| 12 |
|
| 13 |
if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly |
| 14 |
|
| 15 |
class WPBC_Admin_Menus { |
| 16 |
|
| 17 |
/* Static Variables */ |
| 18 |
static $capability = array( 'administrator' => 'activate_plugins', |
| 19 |
'editor' => 'publish_pages', |
| 20 |
'author' => 'publish_posts', |
| 21 |
'contributor' => 'edit_posts', |
| 22 |
'subscriber' => 'read' |
| 23 |
); |
| 24 |
protected $menu_tag; |
| 25 |
protected $menu_title; |
| 26 |
protected $menu_title_second; // For root menu chnage second title in submenu - little hack |
| 27 |
protected $page_header; // Header - H2, in page content |
| 28 |
protected $browser_header; // Browser Header Title |
| 29 |
protected $in_menu; |
| 30 |
protected $user_role; // Acceess Role for current menu item |
| 31 |
protected $mune_icon_url; // Icon - if used 'none', then you can define it in the CSS backgound: .toplevel_page_wpbc .wp-menu-image { background-image: url("../img/icon-16x16.png"); background-position: 7px 7px; } But its mean that you are need to load this CSS in every admin page. |
| 32 |
|
| 33 |
public $root_position; /* Positions for Core Menu Items |
| 34 |
2 Dashboard |
| 35 |
4 Separator |
| 36 |
5 Posts |
| 37 |
10 Media |
| 38 |
15 Links |
| 39 |
20 Pages |
| 40 |
25 Comments |
| 41 |
59 Separator |
| 42 |
60 Appearance |
| 43 |
65 Plugins |
| 44 |
70 Users |
| 45 |
75 Tools |
| 46 |
80 Settings |
| 47 |
99 Separator |
| 48 |
*/ |
| 49 |
|
| 50 |
function __construct( $slug, $param = array() ) { |
| 51 |
|
| 52 |
$this->in_menu = false; |
| 53 |
$this->menu_tag = $slug; // For exmaple: 'wpbc-settings' - its 'page' parameter in URL |
| 54 |
|
| 55 |
if ( isset( $param['menu_title'] ) ) $this->menu_title = $param['menu_title']; |
| 56 |
if ( isset( $param['menu_title_second'] ) ) $this->menu_title_second = $param['menu_title_second']; |
| 57 |
if ( isset( $param['page_header'] ) ) $this->page_header = $param['page_header']; |
| 58 |
if ( isset( $param['browser_header'] ) ) $this->browser_header = $param['browser_header']; |
| 59 |
if ( isset( $param['in_menu'] ) ) $this->in_menu = $param['in_menu']; |
| 60 |
|
| 61 |
/** |
| 62 |
* Filter the parent slug used to register a Booking Calendar admin page. |
| 63 |
* |
| 64 |
* Returning false registers the controller as a hidden WordPress plugin |
| 65 |
* page. Its direct URL and capability check remain active, while WordPress |
| 66 |
* does not add the page to a native submenu. |
| 67 |
* |
| 68 |
* @param string|false $parent_menu_slug Parent menu slug, or false for a hidden page. |
| 69 |
* @param string $menu_slug Booking Calendar admin page slug. |
| 70 |
*/ |
| 71 |
$this->in_menu = apply_filters( 'wpbc_admin_menu_parent_slug', $this->in_menu, $this->menu_tag ); |
| 72 |
if ( isset( $param['position'] ) ) $this->root_position = $param['position']; |
| 73 |
else $this->root_position = null; |
| 74 |
|
| 75 |
if ( isset( $param['mune_icon_url'] ) ) $this->mune_icon_url = $param['mune_icon_url']; |
| 76 |
else $this->mune_icon_url = 'none'; |
| 77 |
|
| 78 |
if ( isset( $param['user_role'] ) ) $this->user_role = $param['user_role']; |
| 79 |
else $this->user_role = 'subscriber'; |
| 80 |
|
| 81 |
add_action( 'admin_menu', array($this, 'new_admin_page'), 10 ); |
| 82 |
|
| 83 |
add_action( 'admin_menu', array($this, 'change_second_root_menu_title'), 11 ); // Change Title of Submenu title for root menu item |
| 84 |
} |
| 85 |
|
| 86 |
|
| 87 |
public function load_js() { |
| 88 |
do_action( 'wpbc_load_js_on_admin_page' ); |
| 89 |
} |
| 90 |
|
| 91 |
|
| 92 |
public function load_css() { |
| 93 |
do_action( 'wpbc_load_css_on_admin_page' ); |
| 94 |
} |
| 95 |
|
| 96 |
|
| 97 |
/** |
| 98 |
* Restore the browser title for a registered hidden plugin page. |
| 99 |
* |
| 100 |
* WordPress cannot always resolve a title for submenu pages registered with |
| 101 |
* a false parent slug. The load-hook runs before admin-header.php and keeps |
| 102 |
* PHP 8.1+ from passing null into strip_tags() while retaining the hidden |
| 103 |
* page's normal screen hook, assets, controller, and capability check. |
| 104 |
* |
| 105 |
* @return void |
| 106 |
*/ |
| 107 |
public function set_hidden_page_title() { |
| 108 |
global $title; |
| 109 |
|
| 110 |
$page_title = ! empty( $this->browser_header ) ? $this->browser_header : $this->page_header; |
| 111 |
if ( empty( $page_title ) ) { |
| 112 |
$page_title = $this->menu_title; |
| 113 |
} |
| 114 |
|
| 115 |
$title = wp_strip_all_tags( (string) $page_title ); |
| 116 |
} |
| 117 |
|
| 118 |
|
| 119 |
/** |
| 120 |
* Define Plugin Menu Page |
| 121 |
* |
| 122 |
*/ |
| 123 |
public function new_admin_page(){ |
| 124 |
|
| 125 |
if ( $this->in_menu == 'root' ) { // Main Menu |
| 126 |
|
| 127 |
$page = $this->create_plugin_menu( |
| 128 |
$this->browser_header // Browser Page Header Title |
| 129 |
, $this->menu_title // Menu Title |
| 130 |
, ( ( isset( self::$capability[ $this->user_role ] ) ) ? self::$capability[ $this->user_role ] : self::$capability[ 'subscriber' ] ) // Capabilities |
| 131 |
, $this->menu_tag // Slug // I was used early -> plugin_basename(WPBC_FILE).'wpbc' |
| 132 |
, $this->mune_icon_url // Icon // - if used 'none', then you can define it in the CSS backgound: .toplevel_page_wpbc .wp-menu-image { background-image: url("../img/icon-16x16.png"); background-position: 7px 7px; } But its mean that you are need to load this CSS in every admin page. |
| 133 |
, array( $this, 'content' ) // Function for output content of page |
| 134 |
); |
| 135 |
|
| 136 |
} else { // Sub Menu |
| 137 |
|
| 138 |
$page = $this->create_plugin_submenu( |
| 139 |
$this->in_menu // The slug name for parent menu (or file name of standard WordPress admin page) |
| 140 |
, $this->browser_header // Page Title |
| 141 |
, $this->menu_title // Menu Title |
| 142 |
, ( ( isset( self::$capability[ $this->user_role ] ) ) ? self::$capability[ $this->user_role ] : self::$capability[ 'subscriber' ] ) // Capabilities |
| 143 |
, $this->menu_tag // Slug |
| 144 |
, array( $this, 'content' ) // Function for output content of page |
| 145 |
); |
| 146 |
} |
| 147 |
|
| 148 |
if ( false === $this->in_menu && false !== $page ) { |
| 149 |
add_action( 'load-' . $page, array( $this, 'set_hidden_page_title' ) ); |
| 150 |
} |
| 151 |
|
| 152 |
//do_action('wpbc_define_settings_pages', $this->menu_tag ); // Define sub classes, like: page-settings-general.php $this->menu_tag - 'wpbc-settings' - its 'page' parameter in URL |
| 153 |
|
| 154 |
do_action('wpbc_menu_created', $this->menu_tag ); // Define sub classes, like: page-settings-general.php $this->menu_tag - 'wpbc-settings' - its 'page' parameter in URL |
| 155 |
do_action('wpbc_define_nav_tabs', $this->menu_tag ); // Define Nav tabs. |
| 156 |
} |
| 157 |
|
| 158 |
|
| 159 |
/** |
| 160 |
* Content of the Menu Page |
| 161 |
* |
| 162 |
Define page S t r u c t u r e, nav T A B s , set N O N C E: wpbc_ajax_admin_nonce field |
| 163 |
|
| 164 |
in ..\any\class\class-admin-page-structure.php |
| 165 |
|
| 166 |
then show page C O N T E N T in files, like page-structure.php |
| 167 |
|
| 168 |
$this->menu_tag - 'wpbc-settings' - its 'page' parameter in URL |
| 169 |
*/ |
| 170 |
public function content() { |
| 171 |
|
| 172 |
do_action('wpbc_page_structure_show', $this->menu_tag ); |
| 173 |
} |
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
/** |
| 178 |
* Hack for changing Root 2nd Submenu Title |
| 179 |
* |
| 180 |
* @global type $submenu |
| 181 |
*/ |
| 182 |
public function change_second_root_menu_title() { |
| 183 |
|
| 184 |
// Change Title of the Main menu inside of submenu |
| 185 |
global $submenu; |
| 186 |
|
| 187 |
if ( ( $this->in_menu == 'root' ) |
| 188 |
&& ( isset( $submenu[ $this->menu_tag ] ) ) |
| 189 |
&& ( isset( $this->menu_title_second ) ) |
| 190 |
) { |
| 191 |
$submenu[ $this->menu_tag ][0][0] = $this->menu_title_second; |
| 192 |
} |
| 193 |
} |
| 194 |
|
| 195 |
|
| 196 |
/** |
| 197 |
* Add Menu |
| 198 |
* |
| 199 |
* @param type $menu_page_title |
| 200 |
* @param type $menu_title |
| 201 |
* @param type $capability |
| 202 |
* @param type $menu_slug |
| 203 |
* @param type $mune_icon_url |
| 204 |
* @param type $page_content |
| 205 |
* @param type $page_css |
| 206 |
* @param type $page_js |
| 207 |
* @return type |
| 208 |
*/ |
| 209 |
protected function create_plugin_menu($menu_page_title, $menu_title, $capability, $menu_slug, $mune_icon_url, $page_content ) { |
| 210 |
/** |
| 211 |
* add_menu_page( $page_title, $menu_title, $capability, $menu_slug, $function, $icon_url, $position ); |
| 212 |
* @return string The resulting page's hook_suffix |
| 213 |
*/ |
| 214 |
|
| 215 |
|
| 216 |
// FixIn: 9.0.1.7. |
| 217 |
|
| 218 |
// The url to the icon to be used for this menu. Using 'none' would leave div.wp-menu-image empty so an icon can be added as background with CSS. |
| 219 |
|
| 220 |
if ( '<svg' == substr( $mune_icon_url, 0, 4 ) ) { |
| 221 |
/* |
| 222 |
$mune_icon_url_svg = '<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" class="" viewBox="-2 -1 20 20"> |
| 223 |
<path d="M3.5 0a.5.5 0 0 1 .5.5V1h8V.5a.5.5 0 0 1 1 0V1h1a2 2 0 0 1 2 2v11a2 2 0 0 1-2 2H2a2 2 0 0 1-2-2V3a2 2 0 0 1 2-2h1V.5a.5.5 0 0 1 .5-.5zM2 2a1 1 0 0 0-1 1v1h14V3a1 1 0 0 0-1-1H2zm13 3H1v9a1 1 0 0 0 1 1h12a1 1 0 0 0 1-1V5z"/> |
| 224 |
<path d="M9 7.5a.5.5 0 0 1 .5-.5H15v2H9.5a.5.5 0 0 1-.5-.5v-1zm-2 3v1a.5.5 0 0 1-.5.5H1v-2h5.5a.5.5 0 0 1 .5.5z"/> |
| 225 |
</svg>'; |
| 226 |
*/ |
| 227 |
$mune_icon_url = sprintf( 'data:image/svg+xml;base64,%s', base64_encode( $mune_icon_url ) ); |
| 228 |
|
| 229 |
} elseif ( $mune_icon_url == 'none' ) { |
| 230 |
$mune_icon_url = 'none'; |
| 231 |
} elseif ( empty( $mune_icon_url ) ) { |
| 232 |
$mune_icon_url = ''; |
| 233 |
} else { |
| 234 |
$mune_icon_url = plugins_url( $mune_icon_url, WPBC_FILE ); |
| 235 |
} |
| 236 |
|
| 237 |
$page = add_menu_page( $menu_page_title, // The text to be displayed in the title tags of the page when the menu is selected |
| 238 |
$menu_title, // The text to be used for the menu |
| 239 |
$capability, // The capability required for this menu to be displayed to the user. |
| 240 |
$menu_slug, // The slug name to refer to this menu by (should be unique for this menu) |
| 241 |
$page_content, // The function to be called to output the content for this page. |
| 242 |
$mune_icon_url//(($mune_icon_url=='none')?'none':((empty($mune_icon_url))?'':plugins_url($mune_icon_url, WPBC_FILE)) ) // The url to the icon to be used for this menu. Using 'none' would leave div.wp-menu-image empty so an icon can be added as background with CSS. |
| 243 |
, $this->root_position /* @param int $position The position in the menu order this one should appear |
| 244 |
* Positions for Core Menu Items |
| 245 |
2 Dashboard |
| 246 |
4 Separator |
| 247 |
5 Posts |
| 248 |
10 Media |
| 249 |
15 Links |
| 250 |
20 Pages |
| 251 |
25 Comments |
| 252 |
59 Separator |
| 253 |
60 Appearance |
| 254 |
65 Plugins |
| 255 |
70 Users |
| 256 |
75 Tools |
| 257 |
80 Settings |
| 258 |
99 Separator |
| 259 |
*/ |
| 260 |
); |
| 261 |
|
| 262 |
add_action( 'admin_print_styles-' . $page, array( $this, 'load_css' ) ); |
| 263 |
|
| 264 |
add_action( 'admin_print_scripts-' . $page, array( $this, 'load_js' ) ); |
| 265 |
|
| 266 |
return $page; |
| 267 |
} |
| 268 |
|
| 269 |
|
| 270 |
/** |
| 271 |
* Add Sub Menu |
| 272 |
* |
| 273 |
* @param type $parent_menu_slug - The slug name for the parent menu (or the file name of a standard WordPress admin page) |
| 274 |
* @param type $menu_page_title |
| 275 |
* @param type $menu_title |
| 276 |
* @param type $capability |
| 277 |
* @param type $menu_slug |
| 278 |
* @param type $page_content |
| 279 |
* @param type $page_css |
| 280 |
* @param type $page_js |
| 281 |
* @return type |
| 282 |
*/ |
| 283 |
protected function create_plugin_submenu( $parent_menu_slug, $menu_page_title, $menu_title, $capability, $menu_slug, $page_content ) { |
| 284 |
|
| 285 |
/** |
| 286 |
* function add_submenu_page( $parent_slug, $page_title, $menu_title, $capability, $menu_slug, $function = '' ); |
| 287 |
* @return string|bool The resulting page's hook_suffix, or false if the user does not have the capability required. |
| 288 |
*/ |
| 289 |
|
| 290 |
$page = add_submenu_page( |
| 291 |
$parent_menu_slug, // The slug name for the parent menu (or the file name of a standard WordPress admin page) |
| 292 |
$menu_page_title, // The text to be displayed in the title tags of the page when the menu is selected |
| 293 |
$menu_title, // The text to be used for the menu |
| 294 |
$capability, // The capability required for this menu to be displayed to the user. |
| 295 |
$menu_slug, // The slug name to refer to this menu by (should be unique for this menu) |
| 296 |
$page_content // The function to be called to output the content for this page. |
| 297 |
); |
| 298 |
|
| 299 |
add_action( 'admin_print_styles-' . $page, array( $this, 'load_css' ) ); |
| 300 |
|
| 301 |
add_action( 'admin_print_scripts-' . $page, array( $this, 'load_js' ) ); |
| 302 |
|
| 303 |
return $page; |
| 304 |
} |
| 305 |
} |
| 306 |
|