From 3515c6afaf9ca354f28a4b07143e9a7eaf78ef9b Mon Sep 17 00:00:00 2001 From: Jordan Doyle Date: Mon, 3 Oct 2016 12:48:46 +0100 Subject: [PATCH] Add custom post type support and add an action helper --- config/posttypes.php | 19 +++++++++++++++++++ src/Providers/ConfigServiceProvider.php | 3 ++- src/Providers/WordpressServiceProvider.php | 20 +++++++++++++++++--- src/Support/Action.php | 47 +++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 85 insertions(+), 4 deletions(-) create mode 100644 config/posttypes.php create mode 100644 src/Support/Action.php diff --git a/config/posttypes.php b/config/posttypes.php new file mode 100644 index 0000000..fe2b144 --- /dev/null +++ b/config/posttypes.php @@ -0,0 +1,19 @@ + [ + 'public' => true, + 'capability_type' => 'page', + 'label' => 'Supplier', + 'map_meta_cap' => true, + 'menu_position' => 5, + 'hierarchical' => false, + 'rewrite' => false, + 'query_var' => false, + 'delete_with_user' => true, + 'supports' => [ + 'title', + 'revisions', + ] + ] +]; diff --git a/src/Providers/ConfigServiceProvider.php b/src/Providers/ConfigServiceProvider.php index be9ee0d..336f587 100644 --- a/src/Providers/ConfigServiceProvider.php +++ b/src/Providers/ConfigServiceProvider.php @@ -14,7 +14,8 @@ class ConfigServiceProvider extends ServiceProvider { $this->publishes([ realpath(__DIR__ . '/../../config/templates.php') => config_path('templates.php'), - realpath(__DIR__ . '/../../config/wordpress.php') => config_path('wordpress.php') + realpath(__DIR__ . '/../../config/wordpress.php') => config_path('wordpress.php'), + realpath(__DIR__ . '/../../config/posttypes.php') => config_path('posttypes.php') ]); } } diff --git a/src/Providers/WordpressServiceProvider.php b/src/Providers/WordpressServiceProvider.php index 7ad2b7a..845d103 100644 --- a/src/Providers/WordpressServiceProvider.php +++ b/src/Providers/WordpressServiceProvider.php @@ -5,6 +5,7 @@ namespace Koselig\Providers; use Illuminate\Contracts\Routing\UrlGenerator; use Illuminate\Support\Facades\DB; use Illuminate\Support\ServiceProvider; +use Koselig\Support\Action; /** * Service provider for everything Wordpress, configures @@ -71,8 +72,6 @@ class WordpressServiceProvider extends ServiceProvider define('WP_DEBUG_DISPLAY', WP_DEBUG); define('SCRIPT_DEBUG', WP_DEBUG); - define('DISALLOW_FILE_EDIT', true); - if (!defined('ABSPATH')) { define('ABSPATH', $this->app->basePath() . DIRECTORY_SEPARATOR . WP_PATH); } @@ -98,8 +97,23 @@ class WordpressServiceProvider extends ServiceProvider */ protected function triggerHooks() { - add_filter('theme_page_templates', function ($page_templates) { + // register the user's templates + Action::hook('theme_page_templates', function ($page_templates) { return array_merge($page_templates, config('templates')); }); + + $this->registerPostTypes(); + } + + /** + * Register all the user's custom post types with Wordpress. + * + * @return void + */ + protected function registerPostTypes() + { + foreach (config('posttypes') as $key => $value) { + register_post_type($key, $value); + } } } diff --git a/src/Support/Action.php b/src/Support/Action.php new file mode 100644 index 0000000..cd188eb --- /dev/null +++ b/src/Support/Action.php @@ -0,0 +1,47 @@ + + */ +class Action +{ + /** + * Hook a function or method to a specific filter action. + * + * @param $tag + * @param callable $function + * @param int $priority + * @param int $accepted_args + */ + public static function hook($tag, callable $function, $priority = 10, $accepted_args = 1) + { + add_filter($tag, $function, $priority, $accepted_args); + } + + /** + * Run all filters hooked to
$tag
on the given
$value
+ * + * @param string $tag tag to run + * @param mixed $value value to run filters on + * @param array ...$params extra params to pass to filters + * @return mixed|void + */ + public static function filter($tag, $value, ...$params) + { + return apply_filters($tag, $value, ...$params); + } + + /** + * Execute functions hooked on a specific action hook. + * + * @param string $tag name of the action to be executed + * @param array $params parameters to pass to the hooked functions + */ + public static function trigger($tag, ...$params) + { + return do_action($tag, $params); + } +} -- libgit2 1.7.2