Add new page controller class, replace page registration function.

This commit is contained in:
Jeff Stieler 2019-05-07 12:42:42 -06:00
parent d226b80cd4
commit 19bfd01599
3 changed files with 140 additions and 28 deletions

View File

@ -6,34 +6,6 @@
* @package Woocommerce Admin
*/
if ( ! function_exists( 'wc_admin_register_page' ) ) {
/**
* Add a single page to a given parent top-level-item.
*
* @param array $options {
* Array describing the menu item.
*
* @type string $title Menu title
* @type string $parent Parent path or menu ID
* @type string $path Path for this page, full path in app context; ex /analytics/report
* }
*/
function wc_admin_register_page( $options ) {
$defaults = array(
'parent' => '/analytics',
);
$options = wp_parse_args( $options, $defaults );
add_submenu_page(
'/' === $options['parent'][0] ? "wc-admin#{$options['parent']}" : $options['parent'],
$options['title'],
$options['title'],
'manage_options',
"wc-admin#{$options['path']}",
array( 'WC_Admin_Loader', 'page_wrapper' )
);
}
}
/**
* WC_Admin_Loader Class.
*/
@ -145,6 +117,9 @@ class WC_Admin_Loader {
* Class loader for enabled WooCommerce Admin features/sections.
*/
public static function load_features() {
require_once WC_ADMIN_ABSPATH . 'includes/page-controller/class-wc-admin-page-controller.php';
require_once WC_ADMIN_ABSPATH . 'includes/page-controller/page-controller-functions.php';
$features = self::get_features();
foreach ( $features as $feature ) {
$feature = strtolower( $feature );

View File

@ -0,0 +1,120 @@
<?php
/**
* WC_Admin_Page_Controller
*
* @package Woocommerce Admin
*/
/**
* WC_Admin_Page_Controller
*/
class WC_Admin_Page_Controller {
// JS-powered page root.
const PAGE_ROOT = 'wc-admin#';
/**
* Singleton instance of self.
*
* @var WC_Admin_Page_Controller
*/
private static $instance = false;
/**
* Registered pages
* Contains information (breadcrumbs, menu info) about JS powered pages and classic WooCommerce pages.
*
* @var array
*/
private $pages = array();
/**
* We want a single instance of this class so we can accurately track registered menus and pages.
*/
public static function get_instance() {
if ( ! self::$instance ) {
self::$instance = new self();
}
return self::$instance;
}
/**
* Returns the path from an ID.
*
* @param string $id ID to get path for.
* @return string|null Path for the given ID.
*/
public function get_path_from_id( $id ) {
if ( isset( $this->pages[ $id ] ) && isset( $this->pages[ $id ]['path'] ) ) {
return $this->pages[ $id ]['path'];
}
return null;
}
/**
* Adds a JS powered page to wc-admin.
*
* @param array $options {
* Array describing the page.
*
* @type string id Id to reference the page.
* @type string title Page title. Used in menus and breadcrumbs.
* @type string|null parent Parent ID. Null for new top level page.
* @type string path Path for this page, full path in app context; ex /analytics/report
* @type string capability Capability needed to access the page.
* @type string icon Icon. Dashicons helper class, base64-encoded SVG, or 'none'.
* @type int position Menu item position.
* }
*/
public function register_page( $options ) {
$defaults = array(
'id' => null,
'parent' => null,
'title' => '',
'capability' => 'manage_options',
'path' => '',
'icon' => '',
'position' => null,
);
$options = wp_parse_args( $options, $defaults );
$options['path'] = self::PAGE_ROOT . $options['path'];
// TODO: check for null ID, or collision.
$this->pages[ $options['id'] ] = $options;
if ( is_null( $options['parent'] ) ) {
add_menu_page(
$options['title'],
$options['title'],
$options['capability'],
$options['path'],
array( __CLASS__, 'page_wrapper' ),
$options['icon'],
$options['position']
);
} else {
$parent_path = $this->get_path_from_id( $options['parent'] );
// TODO: check for null path.
add_submenu_page(
$parent_path,
$options['title'],
$options['title'],
$options['capability'],
$options['path'],
array( __CLASS__, 'page_wrapper' )
);
}
}
/**
* Set up a div for the app to render into.
*/
public static function page_wrapper() {
?>
<div class="wrap">
<div id="root"></div>
</div>
<?php
}
}

View File

@ -0,0 +1,17 @@
<?php
/**
* Convenience functions for WC_Admin_Page_Controller.
*
* @package Woocommerce Admin
*/
/**
* Register JS-powered WooCommerce Admin Page.
* Passthrough to WC_Admin_Page_Controller::register_page().
*
* @param array $options Options for WC_Admin_Page_Controller::register_page().
*/
function wc_admin_register_page( $options ) {
$controller = WC_Admin_Page_Controller::get_instance();
$controller->register_page( $options );
}