Skip to content
This repository was archived by the owner on Sep 26, 2019. It is now read-only.

Commit 2e08d5b

Browse files
author
Felix Arntz
committed
Handle admin panels (first approach).
1 parent 76d6511 commit 2e08d5b

1 file changed

Lines changed: 31 additions & 4 deletions

File tree

src/Admin_Page/WordPress_Admin_Page.php

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@
1515
*/
1616
class WordPress_Admin_Page extends Abstract_Admin_Page {
1717

18-
const CAPABILITY = 'capability';
18+
const CAPABILITY = 'capability';
19+
const ADMIN_PANEL = 'admin_panel';
1920

2021
/**
2122
* Gets the URL of the admin page.
@@ -27,7 +28,20 @@ class WordPress_Admin_Page extends Abstract_Admin_Page {
2728
public function get_url() : string {
2829
$parent_file = $this->get_parent_file();
2930

30-
return add_query_arg( 'page', $this->getConfigKey( self::SLUG ), admin_url( $parent_file ) );
31+
$admin_panel = $this->hasConfigKey( self::ADMIN_PANEL ) ? $this->getConfigKey( self::ADMIN_PANEL ) : 'site';
32+
33+
switch ( $admin_panel ) {
34+
case 'user':
35+
$base_url = user_admin_url( $parent_file );
36+
break;
37+
case 'network':
38+
$base_url = network_admin_url( $parent_file );
39+
break;
40+
default:
41+
$base_url = admin_url( $parent_file );
42+
}
43+
44+
return add_query_arg( 'page', $this->getConfigKey( self::SLUG ), $base_url );
3145
}
3246

3347
/**
@@ -38,10 +52,23 @@ public function get_url() : string {
3852
public function register() {
3953
$callback = $this->get_register_hook_callback();
4054

41-
if ( doing_action( 'admin_menu' ) ) {
55+
$admin_panel = $this->hasConfigKey( self::ADMIN_PANEL ) ? $this->getConfigKey( self::ADMIN_PANEL ) : 'site';
56+
57+
switch ( $admin_panel ) {
58+
case 'user':
59+
$hook_name = 'user_admin_menu';
60+
break;
61+
case 'network':
62+
$hook_name = 'network_admin_menu';
63+
break;
64+
default:
65+
$hook_name = 'admin_menu';
66+
}
67+
68+
if ( doing_action( $hook_name ) ) {
4269
call_user_func( $callback );
4370
} else {
44-
add_action( 'admin_menu', $callback );
71+
add_action( $hook_name, $callback );
4572
}
4673
}
4774

0 commit comments

Comments
 (0)