Permalink
Browse files

Handle admin panels (second approach).

  • Loading branch information...
felixarntz committed Jan 14, 2018
1 parent 2e08d5b commit 6326675a7b408992eb8af8de514dcb0f905a2bd6
@@ -8,6 +8,9 @@
namespace Leaves_And_Love\OOP_Admin_Pages\Admin_Page;
use Leaves_And_Love\OOP_Admin_Pages\Admin_Page\WordPress_Admin_Panels\WordPress_Admin_Panel;
use Leaves_And_Love\OOP_Admin_Pages\Admin_Page\WordPress_Admin_Panels\WordPress_Admin_Panels;
/**
* Class representing a basic WordPress admin page.
*
@@ -28,18 +31,8 @@ class WordPress_Admin_Page extends Abstract_Admin_Page {
public function get_url() : string {
$parent_file = $this->get_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 );
}
$url_callback = $this->get_admin_panel()->get_url_callback();
$base_url = call_user_func( $url_callback, $parent_file );
return add_query_arg( 'page', $this->getConfigKey( self::SLUG ), $base_url );
}
@@ -52,18 +45,7 @@ public function get_url() : string {
public function register() {
$callback = $this->get_register_hook_callback();
$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';
}
$hook_name = $this->get_admin_panel()->get_hook_name();
if ( doing_action( $hook_name ) ) {
call_user_func( $callback );
@@ -72,6 +54,21 @@ public function register() {
}
}
/**
* Gets the admin panel instance this admin page is part of.
*
* @since 1.0.0
*
* @return WordPress_Admin_Panel Admin panel instance.
*/
protected function get_admin_panel() : WordPress_Admin_Panel {
if ( $this->hasConfigKey( self::ADMIN_PANEL ) ) {
return WordPress_Admin_Panels::get_panel( $this->getConfigKey( self::ADMIN_PANEL ) );
}
return WordPress_Admin_Panels::get_current_panel();
}
/**
* Gets the callback to use to register the admin page within WordPress.
*
@@ -0,0 +1,35 @@
<?php
/**
* WordPress_Admin_Panel interface
*
* @package Leaves_And_Love\OOP_Admin_Pages\Admin_Page\WordPress_Admin_Panels
* @since 1.0.0
*/
namespace Leaves_And_Love\OOP_Admin_Pages\Admin_Page\WordPress_Admin_Panels;
/**
* Interface representing a WordPress admin panel.
*
* @since 1.0.0
*/
interface WordPress_Admin_Panel {
/**
* Gets the callback to generate an URL to the admin panel.
*
* @since 1.0.0
*
* @return callable Admin panel URL callback.
*/
public function get_url_callback() : callable;
/**
* Gets the hook name to use to register content for the admin panel.
*
* @since 1.0.0
*
* @return string Admin panel hook name.
*/
public function get_hook_name() : string;
}
@@ -0,0 +1,111 @@
<?php
/**
* WordPress_Admin_Panels class
*
* @package Leaves_And_Love\OOP_Admin_Pages\Admin_Page\WordPress_Admin_Panels
* @since 1.0.0
*/
namespace Leaves_And_Love\OOP_Admin_Pages\Admin_Page\WordPress_Admin_Panels;
use Leaves_And_Love\OOP_Admin_Pages\Exception\Invalid_Admin_Panel_Exception;
/**
* Class holding the available WordPress admin panels.
*
* @since 1.0.0
*/
final class WordPress_Admin_Panels {
const SITE = 'site';
const NETWORK = 'network';
const USER = 'user';
/**
* Available admin panels.
*
* @since 1.0.0
* @static
* @var array
*/
private static $panels = array();
/**
* Whether the available admin panels have been initialized yet.
*
* @since 1.0.0
* @static
* @var bool
*/
private static $initialized = false;
/**
* Gets an instance of an admin panel.
*
* @since 1.0.0
* @static
*
* @param string $slug Admin panel identifier.
* @return WordPress_Admin_Panel Admin panel instance.
*
* @throws Invalid_Admin_Panel_Exception Thrown when admin panel identifier is invalid.
*/
public static function get_panel( string $slug ) : WordPress_Admin_Panel {
self::maybe_initialize();
if ( ! isset( self::$panels[ $slug ] ) ) {
throw new Invalid_Admin_Panel_Exception( sprintf( 'An admin panel with the identifier %s is not available.', $slug ) );
}
return self::$panels[ $slug ];
}
/**
* Gets the current admin panel.
*
* @since 1.0.0
* @static
*
* @return WordPress_Admin_Panel Admin panel instance.
*/
public static function get_current_panel() : WordPress_Admin_Panel {
switch ( true ) {
case is_user_admin():
return self::get_panel( self::USER );
case is_network_admin():
return self::get_panel( self::NETWORK );
default:
return self::get_panel( self::SITE );
}
}
/**
* Instantiates the available admin panels as necessary.
*
* @since 1.0.0
* @static
*/
private static function maybe_initialize() {
if ( self::$initialized ) {
return;
}
self::initialize();
self::$initialized = true;
}
/**
* Instantiates the available admin panels.
*
* @since 1.0.0
* @static
*/
private static function initialize() {
self::$panels = array(
self::SITE => new WordPress_Site_Admin_Panel(),
self::NETWORK => new WordPress_Network_Admin_Panel(),
self::USER => new WordPress_User_Admin_Panel(),
);
}
}
@@ -0,0 +1,39 @@
<?php
/**
* WordPress_Network_Admin_Panel class
*
* @package Leaves_And_Love\OOP_Admin_Pages\Admin_Page\WordPress_Admin_Panels
* @since 1.0.0
*/
namespace Leaves_And_Love\OOP_Admin_Pages\Admin_Page\WordPress_Admin_Panels;
/**
* Class representing the WordPress network admin panel.
*
* @since 1.0.0
*/
class WordPress_Network_Admin_Panel implements WordPress_Admin_Panel {
/**
* Gets the callback to generate an URL to the admin panel.
*
* @since 1.0.0
*
* @return callable Admin panel URL callback.
*/
public function get_url_callback() : callable {
return 'network_admin_url';
}
/**
* Gets the hook name to use to register content for the admin panel.
*
* @since 1.0.0
*
* @return string Admin panel hook name.
*/
public function get_hook_name() : string {
return 'network_admin_menu';
}
}
@@ -0,0 +1,39 @@
<?php
/**
* WordPress_Site_Admin_Panel class
*
* @package Leaves_And_Love\OOP_Admin_Pages\Admin_Page\WordPress_Admin_Panels
* @since 1.0.0
*/
namespace Leaves_And_Love\OOP_Admin_Pages\Admin_Page\WordPress_Admin_Panels;
/**
* Class representing the WordPress site admin panel.
*
* @since 1.0.0
*/
class WordPress_Site_Admin_Panel implements WordPress_Admin_Panel {
/**
* Gets the callback to generate an URL to the admin panel.
*
* @since 1.0.0
*
* @return callable Admin panel URL callback.
*/
public function get_url_callback() : callable {
return 'admin_url';
}
/**
* Gets the hook name to use to register content for the admin panel.
*
* @since 1.0.0
*
* @return string Admin panel hook name.
*/
public function get_hook_name() : string {
return 'admin_menu';
}
}
@@ -0,0 +1,39 @@
<?php
/**
* WordPress_User_Admin_Panel class
*
* @package Leaves_And_Love\OOP_Admin_Pages\Admin_Page\WordPress_Admin_Panels
* @since 1.0.0
*/
namespace Leaves_And_Love\OOP_Admin_Pages\Admin_Page\WordPress_Admin_Panels;
/**
* Class representing the WordPress user admin panel.
*
* @since 1.0.0
*/
class WordPress_User_Admin_Panel implements WordPress_Admin_Panel {
/**
* Gets the callback to generate an URL to the admin panel.
*
* @since 1.0.0
*
* @return callable Admin panel URL callback.
*/
public function get_url_callback() : callable {
return 'user_admin_url';
}
/**
* Gets the hook name to use to register content for the admin panel.
*
* @since 1.0.0
*
* @return string Admin panel hook name.
*/
public function get_hook_name() : string {
return 'user_admin_menu';
}
}
@@ -0,0 +1,20 @@
<?php
/**
* Invalid_Admin_Panel_Exception class
*
* @package Leaves_And_Love\OOP_Admin_Pages\Exception
* @since 1.0.0
*/
namespace Leaves_And_Love\OOP_Admin_Pages\Exception;
use Exception;
/**
* Exception thrown when an admin panel does not exist.
*
* @since 1.0.0
*/
class Invalid_Admin_Panel_Exception extends Exception {
}

0 comments on commit 6326675

Please sign in to comment.