From 3f73ca49e15d6c11338f3812afb55d014b092b9d Mon Sep 17 00:00:00 2001 From: Felix Arntz Date: Sun, 14 Jan 2018 17:09:17 +0100 Subject: [PATCH] Introduce WordPress_Admin_Page class. --- src/Admin_Page/WordPress_Admin_Page.php | 93 +++++++++++++++++++++++++ 1 file changed, 93 insertions(+) create mode 100644 src/Admin_Page/WordPress_Admin_Page.php diff --git a/src/Admin_Page/WordPress_Admin_Page.php b/src/Admin_Page/WordPress_Admin_Page.php new file mode 100644 index 0000000..0eee5cd --- /dev/null +++ b/src/Admin_Page/WordPress_Admin_Page.php @@ -0,0 +1,93 @@ +get_parent_file(); + + return add_query_arg( 'page', $this->getConfigKey( self::SLUG ), admin_url( $parent_file ) ); + } + + /** + * Registers the admin page within the environment. + * + * @since 1.0.0 + */ + public function register() { + $callback = $this->get_register_hook_callback(); + + if ( doing_action( 'admin_menu' ) ) { + call_user_func( $callback ); + } else { + add_action( 'admin_menu', $callback ); + } + } + + /** + * Gets the callback to use to register the admin page within WordPress. + * + * @since 1.0.0 + * + * @return callable Callback to hook into the respective menu action. + */ + protected function get_register_hook_callback() { + return function() { + $slug = $this->getConfigKey( self::SLUG ); + $title = $this->getConfigKey( self::TITLE ); + $capability = $this->getConfigKey( self::CAPABILITY ); + + $hook_suffix = add_submenu_page( null, $title, $title, $capability, $slug, array( $this, 'render' ) ); + + add_action( "load-{$hook_suffix}", array( $this, 'initialize' ) ); + }; + } + + /** + * Gets the parent file for the admin page URL in WordPress. + * + * May contain query parameters. + * + * @since 1.0.0 + * + * @return string Parent file for the URL. + */ + protected function get_parent_file() { + return 'admin.php'; + } + + /** + * Gets the configuration keys that are required for an admin page. + * + * @since 1.0.0 + * + * @return array Array of configuration keys. + */ + protected function get_required_config_keys() { + $required_keys = parent::get_required_config_keys(); + $required_keys[] = self::CAPABILITY; + + return $required_keys; + } +}