114 $this->
id = sanitize_title(
$id );
115 $this->menuTitle = $menu_title;
116 $this->pageTitle = $menu_title;
120 $this->subMenus = array();
136 if ( is_string( $view_controller ) ) {
137 $result = $view_controller;
139 elseif ( is_array( $view_controller ) ) {
140 $result = get_class( $view_controller[0] ) .
'-' . $view_controller[1];
152 public static function menu( $view_controller )
154 $global_key = self::sanitizeViewController( $view_controller );
156 if ( isset( $GLOBALS[self::GLOBAL_MENU] ) ) {
157 if ( !empty( $global_key ) && !empty( $GLOBALS[self::GLOBAL_MENU][$global_key] ) ) {
158 return $GLOBALS[self::GLOBAL_MENU][$global_key];
162 $GLOBALS[self::GLOBAL_MENU] = array();
174 public static function url( $view_controller )
176 $info = self::menu( $view_controller );
179 if ( !empty( $info ) && isset( $info[
'parent'] ) ) {
181 if (
false === strpos( $info[
'parent'],
'.php' ) ) {
182 $url = add_query_arg( array(
'page' => $info[
'page'] ), admin_url(
'admin.php' ) );
185 $url = add_query_arg( array(
'page' => $info[
'page'] ), admin_url( $info[
'parent'] ) );
201 public static function hook( $view_controller )
203 $info = self::menu( $view_controller );
205 if ( !empty( $info ) ) {
206 return $info[
'hook'];
221 public static function page( $view_controller )
223 $info = self::menu( $view_controller );
225 if ( !empty( $info ) ) {
226 return $info[
'page'];
272 foreach ( $menus as $key => $value ) {
273 if ( is_array( $value ) ) {
275 $menu =
new WPDKMenu( $key, $value[
'menuTitle'], $value[
'capability'], $value[
'icon'] );
277 foreach ( $value[
'subMenus'] as $skey => $svalue ) {
282 elseif ( is_array( $svalue ) ) {
284 $menu->addDivider( $svalue[WPDKSubMenuDivider::DIVIDER] );
287 $sub_menu = $menu->addSubMenu( $svalue[
'menuTitle'], $svalue[
'viewController'] );
289 foreach ( $svalue as $property => $pvalue ) {
290 $sub_menu->$property = $pvalue;
297 foreach( $value as $property => $pvalue ) {
298 if ( !in_array( $property, array(
'subMenus' ) ) ) {
299 $menu->$property = $pvalue;
305 $result[$key] = $menu;
322 if ( count( $this->subMenus ) > 0 ) {
324 $this->subMenus[$divider->id] = $divider;
342 if ( empty( $this->subMenus ) ) {
346 $id = sprintf(
'%s-submenu-%s', $this->
id, count( $this->subMenus ) );
350 $this->subMenus[
$id] = $sub_menu;
360 $this->hookName = add_menu_page( $this->pageTitle, $this->menuTitle, $this->capability, $this->
id,
'', $this->icon, $this->position );
362 while ( ( $sub_menu = current( $this->subMenus ) ) ) {
363 $next = next( $this->subMenus );
364 if ( is_a( $sub_menu,
'WPDKSubMenuDivider' ) ) {
365 if ( isset( $next ) && isset( $next->capability ) ) {
366 $sub_menu->capability = $next->capability;
428 $key = key( $menus );
429 if ( isset( $menus[$key][
'subMenus'] ) ) {
430 $index = ( $index < 0 ) ? count( $menus[$key][
'subMenus'] ) + 1 : $index;
431 $menus[$key][
'subMenus'] =
WPDKArray::insert( $menus[$key][
'subMenus'], array( $menu_item ), $index );
469 $key = key( $menus );
470 if ( isset( $menus[$key][
'subMenus'] ) ) {
471 $pos = ( $index < 0 ) ? count( $menus[$key][
'subMenus'] ) + 1 : $index;
472 foreach ( $submenus as $menu ) {
473 $menus[$key][
'subMenus'] =
WPDKArray::insert( $menus[$key][
'subMenus'], array( $menu ), $pos++ );
477 $pos = ( $index < 0 ) ? count( $menus[$key] ) + 1 : $index;
478 foreach ( $submenus as $menu ) {
498 if (
$id === $plugin_page ) {
567 $this->
id = sanitize_title(
$id );
568 $this->menuTitle = $menu_title;
569 $this->pageTitle = $menu_title;
571 $this->viewController = $view_controller;
614 foreach ( $sub_menus as
$parent => $sub_menu ) {
615 foreach ( $sub_menu as $sub_item ) {
620 elseif ( is_array( $sub_item ) ) {
625 if( is_array( $sub_item[
'viewController'] ) ) {
626 $id = sprintf(
'%s-submenu-%s', sanitize_title( $sub_item[
'viewController'][1] ), $index++ );
628 $id = sprintf(
'%s-submenu-%s', sanitize_title( $sub_item[
'viewController'] ), $index++ );
632 foreach ( $sub_item as $property => $svalue ) {
633 $item->$property = $svalue;
637 if( !empty( $item ) ) {
641 if( !empty( $item ) ) {
661 if ( !empty( $this->viewController ) && is_string( $this->viewController ) && !function_exists( $this->viewController ) && class_exists( $this->viewController ) ) {
664 if ( method_exists( $this->viewController,
'init' ) ) {
665 $hook = create_function(
'', sprintf(
'%s::init()->display();', $this->viewController ) );
668 $hook = create_function(
'', sprintf(
'$view_controller = new %s; $view_controller->display();', $this->viewController ) );
673 elseif ( is_callable( $this->viewController ) ) {
677 if( !empty( $global_key ) ) {
681 'parent' => $this->parent,
689 $menu_title = apply_filters(
'wpdk_submenu_title', $this->menuTitle, $this->
id, $this->parent );
692 $this->hookName = add_submenu_page( $this->parent, $this->pageTitle, $menu_title, $this->capability, $this->
id, $hook );
695 if ( isset( $this->query_args ) && !empty( $this->query_args ) ) {
697 foreach ( $this->query_args as $var => $value ) {
698 $stack[] = sprintf(
'$_GET["%s"] = $_REQUEST["%s"] = "%s";', $var, $var, $value );
700 $func = create_function(
'', implode(
'', $stack ) );
701 add_action(
'load-' . $this->hookName, $func );
705 if( !empty( $plugin_page ) ) {
706 if( $this->
id === $plugin_page ) {
707 do_action(
'wpdk_submenu_page', $this, $plugin_page );
711 if ( !empty( $this->viewController ) && is_string( $this->viewController ) && !function_exists( $this->viewController && class_exists( $this->viewController ) ) ) {
714 if ( method_exists( $this->viewController,
'init' ) ) {
715 $load = create_function(
'', sprintf(
'%s::init()->load();', $this->viewController ) );
716 add_action(
'load-' . $this->hookName, $load );
718 $admin_head = create_function(
'', sprintf(
'$v=%s::init();$v->admin_head();$v->_admin_head();', $this->viewController ) );
719 add_action(
'admin_head-' . $this->hookName, $admin_head );
722 elseif ( !is_callable( $this->viewController ) && class_exists( $this->viewController ) ) {
723 $load = create_function(
'', sprintf(
'%s::willLoad();', $this->viewController ) );
724 add_action(
'load-' . $this->hookName, $load );
726 $admin_head = create_function(
'', sprintf(
'%s::didHeadLoad();', $this->viewController ) );
727 add_action(
'admin_head-' . $this->hookName, $admin_head );
731 if ( !empty( $global_key ) ) {
776 $id = sprintf(
'%s-%s', self::DIVIDER, $index++ );
778 $this->capability = self::DEFAULT_CAPABILITY;