Add custom post type support and add an action helper
Diff
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(-)
@@ -0,0 +1,19 @@
<?php
return [
'supplier' => [
'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',
]
]
];
@@ -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')
]);
}
}
@@ -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);
}
}
}
@@ -0,0 +1,47 @@
<?php
namespace Koselig\Support;
/**
* Action helper class, nice interface over Wordpress' filter/action functionality.
*
* @author Jordan Doyle <jordan@doyle.wf>
*/
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 <pre>$tag</pre> on the given <pre>$value</pre>
*
* @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);
}
}