🏡 index : ~doyle/koselig.git

author Jordan Doyle <jordan@doyle.wf> 2016-10-22 15:15:58.0 +00:00:00
committer Jordan Doyle <jordan@doyle.wf> 2016-10-22 15:15:58.0 +00:00:00
commit
1842c21116632ccb5148938fce80dcfc7793b224 [patch]
tree
b0f0e35efae4e919928208450ea1f47b3eda69bb
parent
9e009c015ae7b9b3681a691e810be2df41f122f8
download
1842c21116632ccb5148938fce80dcfc7793b224.tar.gz

Change action to use laravel-style callables and add new Page class for Admin pages



Diff

 src/Admin/Page.php     | 62 +++++++++++++++++++++++++++++++++++++++++++++++++++-
 src/Support/Action.php |  8 ++++---
 2 files changed, 67 insertions(+), 3 deletions(-)

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 @@
<?php
namespace Koselig\Admin;

use Closure;
use Illuminate\Support\Facades\Route;

/**
 * Various helper methods to allow for extension of the Wordpress administration panel.
 *
 * @author Jordan Doyle <jordan@doyle.wf>
 */
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);
    }

    /**