From 1842c21116632ccb5148938fce80dcfc7793b224 Mon Sep 17 00:00:00 2001 From: Jordan Doyle Date: Sat, 22 Oct 2016 16:15:58 +0100 Subject: [PATCH] Change action to use laravel-style callables and add new Page class for Admin pages --- src/Admin/Page.php | 62 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ src/Support/Action.php | 8 +++++--- 2 files changed, 67 insertions(+), 3 deletions(-) create mode 100644 src/Admin/Page.php diff --git a/src/Admin/Page.php b/src/Admin/Page.php new file mode 100644 index 0000000..28c75f5 --- /dev/null +++ b/src/Admin/Page.php @@ -0,0 +1,62 @@ + + */ +class Page +{ + /** + * Add a top-level menu page. Action takes the same parameters as any Route method, for example a string in the format + * method@class or a callback. Dependencies are automatically injected by this function. + * + * @param string $pageTitle The text to be displayed in the title tags of the page when the menu is selected. + * @param string $menuTitle The text to be used for the menu. + * @param string $capability The capability required for this menu to be displayed to the user. + * @param string $slug The slug name to refer to this menu by (should be unique for this menu). + * @param mixed $action The function to be called to output the content for this page. + * @param string $iconUrl The URL to the icon to be used for this menu. + * @param int $position The position in the menu order this one should appear. + * @return string The resulting page's hook_suffix. + */ + public static function addPage($pageTitle, $menuTitle, $capability, $slug, $action, $iconUrl = '', $position = null) + { + return add_menu_page($pageTitle, $menuTitle, $capability, $slug, self::wrap($action), $iconUrl, $position); + } + + /** + * Add a submenu page. Action takes the same parameters as any Route method, for example a string in the format + * method@class or a callback. Dependencies are automatically injected by this function. + * + * @param string $parent The slug name for the parent menu (or the file name of a standard WordPress admin page). + * @param string $pageTitle The text to be displayed in the title tags of the page when the menu is selected. + * @param string $menuTitle The text to be used for the menu. + * @param string $capabilities The capability required for this menu to be displayed to the user. + * @param string $slug The slug name to refer to this menu by (should be unique for this menu). + * @param mixed $action The function to be called to output the content for this page. + * @return false|string The resulting page's hook_suffix, or false if the user does not have the capability required + */ + public static function addSubpage($parent, $pageTitle, $menuTitle, $capabilities, $slug, $action) + { + return add_submenu_page($parent, $pageTitle, $menuTitle, $capabilities, $slug, self::wrap($action)); + } + + /** + * Wrap the action given to us by the user to allow for dependency injection and nicer callable + * syntax. + * + * @param $callback + * @return Closure + */ + protected static function wrap($callback) + { + return function () use ($callback) { + Route::prepareResponse(app('request'), app()->call($callback))->sendContent(); + }; + } +} diff --git a/src/Support/Action.php b/src/Support/Action.php index 9aa9e3e..1af69aa 100644 --- a/src/Support/Action.php +++ b/src/Support/Action.php @@ -12,13 +12,15 @@ class Action * Hook a function or method to a specific filter action. * * @param $tag - * @param callable $function + * @param string $function * @param int $priority * @param int $acceptedArgs */ - public static function hook($tag, callable $function, $priority = 10, $acceptedArgs = 1) + public static function hook($tag, $function, $priority = 10, $acceptedArgs = 1) { - add_filter($tag, $function, $priority, $acceptedArgs); + add_filter($tag, function (...$args) use ($function) { + return app()->call($function, $args); + }, $priority, $acceptedArgs); } /** -- libgit2 1.7.2