From 19bfd01599e9409f6f2907a4dc941955ec8663c1 Mon Sep 17 00:00:00 2001 From: Jeff Stieler Date: Tue, 7 May 2019 12:42:42 -0600 Subject: [PATCH] Add new page controller class, replace page registration function. --- .../includes/class-wc-admin-loader.php | 31 +---- .../class-wc-admin-page-controller.php | 120 ++++++++++++++++++ .../page-controller-functions.php | 17 +++ 3 files changed, 140 insertions(+), 28 deletions(-) create mode 100644 plugins/woocommerce-admin/includes/page-controller/class-wc-admin-page-controller.php create mode 100644 plugins/woocommerce-admin/includes/page-controller/page-controller-functions.php diff --git a/plugins/woocommerce-admin/includes/class-wc-admin-loader.php b/plugins/woocommerce-admin/includes/class-wc-admin-loader.php index d06f2e83641..beffdab10de 100644 --- a/plugins/woocommerce-admin/includes/class-wc-admin-loader.php +++ b/plugins/woocommerce-admin/includes/class-wc-admin-loader.php @@ -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 ); diff --git a/plugins/woocommerce-admin/includes/page-controller/class-wc-admin-page-controller.php b/plugins/woocommerce-admin/includes/page-controller/class-wc-admin-page-controller.php new file mode 100644 index 00000000000..c8d18f172aa --- /dev/null +++ b/plugins/woocommerce-admin/includes/page-controller/class-wc-admin-page-controller.php @@ -0,0 +1,120 @@ +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() { + ?> +
+
+
+ register_page( $options ); +}