Wrapping WordPress for more beautiful code.
Use composer to add WP Wrapper to our code.
composer require awsm/wp-wrapper
The plugin wrapper helps you to initialize your plugin. This is a simple example for a plugin.
<?php
/**
* Plugin Name: Example plugin.
*/
// Path to composer autoload file.
require dirname( __FILE__ ) .'/vendor/autoload.php';
(new \Awsm\WP_Wrapper\Plugin\Plugin() )
->add_translation( 'example-plugin', dirname(__DIR__) . '/languages' )
->add_task(My_Task::class)
->boot();
A task is a class with your program code and contains the task interface.
<?php
/**
* Example service.
**/
class My_Task implements \Awsm\WP_Wrapper\Interfaces\Task {
public function run() {
// Your code here
}
}
The task runner is a trait which can be used in classes which have to start tasks.
<?php
/**
* Example task runner class.
**/
class My_Task_Runner {
use \Awsm\WP_Wrapper\Tasks\Task_Runner;
public function __construct() {
$this->run_tasks();
}
}
(new My_Task_Runner())->add_task( My_Task::class );
But better load the scripts where they have to be loaded. Use the action interface to do your actions.
<?php
/**
* Example task runner class.
**/
class My_Task_Runner implements Awsm\WP_Wrapper\Interfaces\Actions {
use \Awsm\WP_Wrapper\Tasks\Task_Runner;
public function __construct() {
$this->run_tasks();
}
public function add_actions(){
add_action( 'select_a_hook_here', [ $this, 'run_tasks'] );
}
}
(new My_Task_Runner())->add_task( My_Task::class );
It is possible to use depency injection in the constructor by passing objects on task addition.