Permalink
Browse files

Handle admin panels (first approach).

  • Loading branch information...
felixarntz committed Jan 14, 2018
1 parent 76d6511 commit 2e08d5bb50734f594c011eb43727f8bbb5761d42
Showing with 31 additions and 4 deletions.
  1. +31 −4 src/Admin_Page/WordPress_Admin_Page.php
@@ -15,7 +15,8 @@
*/
class WordPress_Admin_Page extends Abstract_Admin_Page {
const CAPABILITY = 'capability';
const CAPABILITY = 'capability';
const ADMIN_PANEL = 'admin_panel';
/**
* Gets the URL of the admin page.
@@ -27,7 +28,20 @@ class WordPress_Admin_Page extends Abstract_Admin_Page {
public function get_url() : string {
$parent_file = $this->get_parent_file();
return add_query_arg( 'page', $this->getConfigKey( self::SLUG ), admin_url( $parent_file ) );
$admin_panel = $this->hasConfigKey( self::ADMIN_PANEL ) ? $this->getConfigKey( self::ADMIN_PANEL ) : 'site';
switch ( $admin_panel ) {
case 'user':
$base_url = user_admin_url( $parent_file );
break;
case 'network':
$base_url = network_admin_url( $parent_file );
break;
default:
$base_url = admin_url( $parent_file );
}
return add_query_arg( 'page', $this->getConfigKey( self::SLUG ), $base_url );
}
/**
@@ -38,10 +52,23 @@ public function get_url() : string {
public function register() {
$callback = $this->get_register_hook_callback();
if ( doing_action( 'admin_menu' ) ) {
$admin_panel = $this->hasConfigKey( self::ADMIN_PANEL ) ? $this->getConfigKey( self::ADMIN_PANEL ) : 'site';
switch ( $admin_panel ) {
case 'user':
$hook_name = 'user_admin_menu';
break;
case 'network':
$hook_name = 'network_admin_menu';
break;
default:
$hook_name = 'admin_menu';
}
if ( doing_action( $hook_name ) ) {
call_user_func( $callback );
} else {
add_action( 'admin_menu', $callback );
add_action( $hook_name, $callback );
}
}

0 comments on commit 2e08d5b

Please sign in to comment.