Actions and filters in Laravel. WordPress-style. Actions are pieces of code you want to execute at certain points in your code. Actions never return anything but merely serve as the option to hook in to your existing code without having to mess things up. Filters are made to modify entities. They always return some kind of value. By default, they return their first parameter, and you should too.
Do action hook helper function
function do_action($tag, ...$args)
Add action to hook
function add_action($tag, $callback, $priority = 20, $arguments = 1)
do_action('my.hook', $user);
The first parameter is the name of the hook; you will use this at a later point when you'll be listening to your hook. All subsequent parameters are sent to the action as parameters. These can be anything you'd like. For example, you might want to tell the listeners that this is attached to a certain model. Then you would pass this as one of the arguments.
To listen to your hooks, you attach listeners. These are best added to file in actions folder in your plugin
For example if you wanted to hook in to the above hook, you could do:
add_action('my.hook', function($user) {
if ($user->is_awesome) {
$this->doSomethingAwesome($user);
}
}, 20, 1);
HookAction::addAction($tag, $callback, $priority = 20, $arguments = 1): void;
function apply_filters($tag, $value, ...$args) {}
function add_filters($tag, $callback, $priority = 20, $arguments = 1) {}
$value = apply_filters('my.hook', 'awesome');
'awesome'
.add_filters('my.hook', function($what) {
$what = 'not '. $what;
return $what;
}, 20, 1);
'not awesome'
. Neat!add_action('my.hook', function($what) {
$what = add_filters('my.hook', 'awesome');
echo 'You are '. $what;
});
Adding the same action as the one in the action example above:
@do_action('my.hook', $user)
Adding the same filter as the one in the filter example above:
You are @apply_filters('my.hook', 'awesome')
HookAction::addFilter($tag, $callback, $priority = 20, $arguments = 1): void;
HookAction::applyFilters(string $tag, mixed $value, ...$args): mixed;
To make the management easy, we divide the action handlers into separate classes. Instead of calling add_action or apply_filter everywhere your project, Let's create an Action class, handle your add_action or apply_filter, making them easy to manage.
Make your custom actions
php artisan plugin:make-action ActionName vender/plugin-name
A new file is created, with the content as below
namespace Vendor\Plugin\Actions;
use Juzaweb\CMS\Abstracts\Action;
class CustomAction extends Action
{
public function handle()
{
// Add action or acc filter function
}
// Handle add action and filter
}
namespace Vendor\Plugin;
use Juzaweb\CMS\Abstracts\Action;
use Juzaweb\AdsManager\Models\Ads;
use Juzaweb\CMS\Facades\HookAction;
class CustomAction extends Action
{
public function handle()
{
$this->addAction(Action::BACKEND_INIT, [$this, 'addAdminMenus']);
$this->addAction(Action::POSTS_FORM_LEFT_ACTION, [$this, 'addActionWithParameter']);
}
public function addAdminMenus()
{
HookAction::registerAdminPage(
'banner-ads',
[
'title' => trans('plugin_domain::content.banner_ads'),
'menu' => [
'icon' => 'fa fa-file',
'position' => 30,
]
]
);
// Or an easier way
$this->hookAction->registerAdminPage(
'banner-ads',
[
'title' => trans('plugin_domain::content.banner_ads'),
'menu' => [
'icon' => 'fa fa-file',
'position' => 30,
]
]
);
}
public function addActionWithParameter($model)
{
echo 'Model Title: ' . $model->title;
}
}
For your actions to work, you need to register them In boot
function in your ServiceProvider, Register your action.
namespace Juzaweb\Example\Providers;
use Juzaweb\CMS\Facades\ActionRegister;
use Juzaweb\CMS\Support\ServiceProvider;
use Juzaweb\Example\Actions\ExampleAction;
class ExampleServiceProvider extends ServiceProvider
{
public function boot()
{
// Register Plugin Action
ActionRegister::register([ExampleAction::class]);
}
// Your code
}
HookAction::registerAdminPage(string $key, array $args);
@param string $key: Key as page
@param array $args
Register Permalink
HookAction::registerPermalink($key, $args);