j10o/whenthen

A drop-in package for wrapping WordPress' add_action and do_action(soon)

dev-main 2022-11-23 10:29 UTC

This package is auto-updated.

Last update: 2025-06-23 19:15:43 UTC


README

WhenThen

A drop-in package for wrapping WordPress' add_action and do_action(soon) :octocat:

A Glimpse

$wp_event = new \WhenThen\MiddleWare\Event();
$wp_event->when( $hook = array( 'name' => 'wp' ) )->then( function(){
    echo 'π™·πšŽπš•πš•πš˜, πš†πš˜πš›πšπ™Ώπš›πšŽπšœπšœ';
});

Basic usage

Via composer

Do composer require j10o/whenthen

Or locate your project's composer.json then add j10o/whenthen": "dev-main" in require property.

For example:

{
    "require": {
        "j10o/whenthen": "dev-main"
    }
}

In your plugin's main file include the vendor autoload.

// Require Composer's autoload file.
require __DIR__ . '/vendor/autoload.php';

// Create new event object.
$wp_event = new \WhenThen\MiddleWare\Event();

// Similar to add_action stuff with WordPress.
$wp_loaded = array(
    'name' => 'wp',
    'priority' => 10,
    'num_args' => 1
);

// Same here.
$footer_loaded = array(
    'name' => 'wp_footer',
    'priority' => 10,
);

// Closure function to call when something happen.
$do_this_thing_1 = function($query_vars) {
    error_log( var_export( $query_vars, true ) );
};

// Another closure function to call when something happen.
$do_this_thing_2 = function() {
    echo 'I am in footer.';
};

// Listen to the event.
$wp_event->when( $wp_loaded )->then( $do_this_thing_1 );

// Another one.
$wp_event->when( $footer_loaded )->then( $do_this_thing_2 );