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

Commit 6326675

Browse files
author
Felix Arntz
committed
Handle admin panels (second approach).
1 parent 2e08d5b commit 6326675

7 files changed

+304
-24
lines changed

src/Admin_Page/WordPress_Admin_Page.php

+21-24
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@
88

99
namespace Leaves_And_Love\OOP_Admin_Pages\Admin_Page;
1010

11+
use Leaves_And_Love\OOP_Admin_Pages\Admin_Page\WordPress_Admin_Panels\WordPress_Admin_Panel;
12+
use Leaves_And_Love\OOP_Admin_Pages\Admin_Page\WordPress_Admin_Panels\WordPress_Admin_Panels;
13+
1114
/**
1215
* Class representing a basic WordPress admin page.
1316
*
@@ -28,18 +31,8 @@ class WordPress_Admin_Page extends Abstract_Admin_Page {
2831
public function get_url() : string {
2932
$parent_file = $this->get_parent_file();
3033

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-
}
34+
$url_callback = $this->get_admin_panel()->get_url_callback();
35+
$base_url = call_user_func( $url_callback, $parent_file );
4336

4437
return add_query_arg( 'page', $this->getConfigKey( self::SLUG ), $base_url );
4538
}
@@ -52,18 +45,7 @@ public function get_url() : string {
5245
public function register() {
5346
$callback = $this->get_register_hook_callback();
5447

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-
}
48+
$hook_name = $this->get_admin_panel()->get_hook_name();
6749

6850
if ( doing_action( $hook_name ) ) {
6951
call_user_func( $callback );
@@ -72,6 +54,21 @@ public function register() {
7254
}
7355
}
7456

57+
/**
58+
* Gets the admin panel instance this admin page is part of.
59+
*
60+
* @since 1.0.0
61+
*
62+
* @return WordPress_Admin_Panel Admin panel instance.
63+
*/
64+
protected function get_admin_panel() : WordPress_Admin_Panel {
65+
if ( $this->hasConfigKey( self::ADMIN_PANEL ) ) {
66+
return WordPress_Admin_Panels::get_panel( $this->getConfigKey( self::ADMIN_PANEL ) );
67+
}
68+
69+
return WordPress_Admin_Panels::get_current_panel();
70+
}
71+
7572
/**
7673
* Gets the callback to use to register the admin page within WordPress.
7774
*
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
/**
3+
* WordPress_Admin_Panel interface
4+
*
5+
* @package Leaves_And_Love\OOP_Admin_Pages\Admin_Page\WordPress_Admin_Panels
6+
* @since 1.0.0
7+
*/
8+
9+
namespace Leaves_And_Love\OOP_Admin_Pages\Admin_Page\WordPress_Admin_Panels;
10+
11+
/**
12+
* Interface representing a WordPress admin panel.
13+
*
14+
* @since 1.0.0
15+
*/
16+
interface WordPress_Admin_Panel {
17+
18+
/**
19+
* Gets the callback to generate an URL to the admin panel.
20+
*
21+
* @since 1.0.0
22+
*
23+
* @return callable Admin panel URL callback.
24+
*/
25+
public function get_url_callback() : callable;
26+
27+
/**
28+
* Gets the hook name to use to register content for the admin panel.
29+
*
30+
* @since 1.0.0
31+
*
32+
* @return string Admin panel hook name.
33+
*/
34+
public function get_hook_name() : string;
35+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
<?php
2+
/**
3+
* WordPress_Admin_Panels class
4+
*
5+
* @package Leaves_And_Love\OOP_Admin_Pages\Admin_Page\WordPress_Admin_Panels
6+
* @since 1.0.0
7+
*/
8+
9+
namespace Leaves_And_Love\OOP_Admin_Pages\Admin_Page\WordPress_Admin_Panels;
10+
11+
use Leaves_And_Love\OOP_Admin_Pages\Exception\Invalid_Admin_Panel_Exception;
12+
13+
/**
14+
* Class holding the available WordPress admin panels.
15+
*
16+
* @since 1.0.0
17+
*/
18+
final class WordPress_Admin_Panels {
19+
20+
const SITE = 'site';
21+
const NETWORK = 'network';
22+
const USER = 'user';
23+
24+
/**
25+
* Available admin panels.
26+
*
27+
* @since 1.0.0
28+
* @static
29+
* @var array
30+
*/
31+
private static $panels = array();
32+
33+
/**
34+
* Whether the available admin panels have been initialized yet.
35+
*
36+
* @since 1.0.0
37+
* @static
38+
* @var bool
39+
*/
40+
private static $initialized = false;
41+
42+
/**
43+
* Gets an instance of an admin panel.
44+
*
45+
* @since 1.0.0
46+
* @static
47+
*
48+
* @param string $slug Admin panel identifier.
49+
* @return WordPress_Admin_Panel Admin panel instance.
50+
*
51+
* @throws Invalid_Admin_Panel_Exception Thrown when admin panel identifier is invalid.
52+
*/
53+
public static function get_panel( string $slug ) : WordPress_Admin_Panel {
54+
self::maybe_initialize();
55+
56+
if ( ! isset( self::$panels[ $slug ] ) ) {
57+
throw new Invalid_Admin_Panel_Exception( sprintf( 'An admin panel with the identifier %s is not available.', $slug ) );
58+
}
59+
60+
return self::$panels[ $slug ];
61+
}
62+
63+
/**
64+
* Gets the current admin panel.
65+
*
66+
* @since 1.0.0
67+
* @static
68+
*
69+
* @return WordPress_Admin_Panel Admin panel instance.
70+
*/
71+
public static function get_current_panel() : WordPress_Admin_Panel {
72+
switch ( true ) {
73+
case is_user_admin():
74+
return self::get_panel( self::USER );
75+
case is_network_admin():
76+
return self::get_panel( self::NETWORK );
77+
default:
78+
return self::get_panel( self::SITE );
79+
}
80+
}
81+
82+
/**
83+
* Instantiates the available admin panels as necessary.
84+
*
85+
* @since 1.0.0
86+
* @static
87+
*/
88+
private static function maybe_initialize() {
89+
if ( self::$initialized ) {
90+
return;
91+
}
92+
93+
self::initialize();
94+
95+
self::$initialized = true;
96+
}
97+
98+
/**
99+
* Instantiates the available admin panels.
100+
*
101+
* @since 1.0.0
102+
* @static
103+
*/
104+
private static function initialize() {
105+
self::$panels = array(
106+
self::SITE => new WordPress_Site_Admin_Panel(),
107+
self::NETWORK => new WordPress_Network_Admin_Panel(),
108+
self::USER => new WordPress_User_Admin_Panel(),
109+
);
110+
}
111+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
/**
3+
* WordPress_Network_Admin_Panel class
4+
*
5+
* @package Leaves_And_Love\OOP_Admin_Pages\Admin_Page\WordPress_Admin_Panels
6+
* @since 1.0.0
7+
*/
8+
9+
namespace Leaves_And_Love\OOP_Admin_Pages\Admin_Page\WordPress_Admin_Panels;
10+
11+
/**
12+
* Class representing the WordPress network admin panel.
13+
*
14+
* @since 1.0.0
15+
*/
16+
class WordPress_Network_Admin_Panel implements WordPress_Admin_Panel {
17+
18+
/**
19+
* Gets the callback to generate an URL to the admin panel.
20+
*
21+
* @since 1.0.0
22+
*
23+
* @return callable Admin panel URL callback.
24+
*/
25+
public function get_url_callback() : callable {
26+
return 'network_admin_url';
27+
}
28+
29+
/**
30+
* Gets the hook name to use to register content for the admin panel.
31+
*
32+
* @since 1.0.0
33+
*
34+
* @return string Admin panel hook name.
35+
*/
36+
public function get_hook_name() : string {
37+
return 'network_admin_menu';
38+
}
39+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
/**
3+
* WordPress_Site_Admin_Panel class
4+
*
5+
* @package Leaves_And_Love\OOP_Admin_Pages\Admin_Page\WordPress_Admin_Panels
6+
* @since 1.0.0
7+
*/
8+
9+
namespace Leaves_And_Love\OOP_Admin_Pages\Admin_Page\WordPress_Admin_Panels;
10+
11+
/**
12+
* Class representing the WordPress site admin panel.
13+
*
14+
* @since 1.0.0
15+
*/
16+
class WordPress_Site_Admin_Panel implements WordPress_Admin_Panel {
17+
18+
/**
19+
* Gets the callback to generate an URL to the admin panel.
20+
*
21+
* @since 1.0.0
22+
*
23+
* @return callable Admin panel URL callback.
24+
*/
25+
public function get_url_callback() : callable {
26+
return 'admin_url';
27+
}
28+
29+
/**
30+
* Gets the hook name to use to register content for the admin panel.
31+
*
32+
* @since 1.0.0
33+
*
34+
* @return string Admin panel hook name.
35+
*/
36+
public function get_hook_name() : string {
37+
return 'admin_menu';
38+
}
39+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
/**
3+
* WordPress_User_Admin_Panel class
4+
*
5+
* @package Leaves_And_Love\OOP_Admin_Pages\Admin_Page\WordPress_Admin_Panels
6+
* @since 1.0.0
7+
*/
8+
9+
namespace Leaves_And_Love\OOP_Admin_Pages\Admin_Page\WordPress_Admin_Panels;
10+
11+
/**
12+
* Class representing the WordPress user admin panel.
13+
*
14+
* @since 1.0.0
15+
*/
16+
class WordPress_User_Admin_Panel implements WordPress_Admin_Panel {
17+
18+
/**
19+
* Gets the callback to generate an URL to the admin panel.
20+
*
21+
* @since 1.0.0
22+
*
23+
* @return callable Admin panel URL callback.
24+
*/
25+
public function get_url_callback() : callable {
26+
return 'user_admin_url';
27+
}
28+
29+
/**
30+
* Gets the hook name to use to register content for the admin panel.
31+
*
32+
* @since 1.0.0
33+
*
34+
* @return string Admin panel hook name.
35+
*/
36+
public function get_hook_name() : string {
37+
return 'user_admin_menu';
38+
}
39+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
/**
3+
* Invalid_Admin_Panel_Exception class
4+
*
5+
* @package Leaves_And_Love\OOP_Admin_Pages\Exception
6+
* @since 1.0.0
7+
*/
8+
9+
namespace Leaves_And_Love\OOP_Admin_Pages\Exception;
10+
11+
use Exception;
12+
13+
/**
14+
* Exception thrown when an admin panel does not exist.
15+
*
16+
* @since 1.0.0
17+
*/
18+
class Invalid_Admin_Panel_Exception extends Exception {
19+
20+
}

0 commit comments

Comments
 (0)