PHP code example of rumur / wp-scheduling

1. Go to this page and download the library: Download rumur/wp-scheduling library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

rumur / wp-scheduling example snippets

 console vendor:publish --provider='Rumur\WordPress\Scheduling\WordPressScheduleServiceProvider'


// functions.php

// With this method the Scheduler will add intervals it uses.
\Rumur\WordPress\Scheduling\Schedule::registerIntoWordPress();



namespace App\Scheduling;

class HelloDolly
{
    protected $lyrics;

    public function __construct($lyrics)
    {
        $this->lyrics = $lyrics;
    }
    
    // It can be an either `handle` or `__invoke` method
    public function handle($args)
    {
        $id = $args['id'];
    }
}



// You can add a class as a Job.
\Rumur\WordPress\Scheduling\Schedule::job(
    new App\Scheduling\HelloDolly('Hello Rudy, well, hello Harry')
)
// You can add args for the task, all these args will be injected to the `handle` method 
->with([ 
    'id' => 2020, 
    //...
])
    
// You can add callbacks that will be executed when the task successfully performed.
->onSuccess(static function() {
    // Do something when the task is run successfully.
})

// You can add callbacks that will be executed when the task encounters an error.
->onFailure(static function() {
    // Do something when the task is failed.
})

// To ping a url when the task is failed.
->pingOnFailure('https://domain.com/?ping=true&id=3790a0e1-3f51-4703-8962-8ed889e2cc7c&action=failed')

// To ping a url when the task is successfully performed.
->pingOnSuccess('https://domain.com/?ping=true&id=3790a0e1-3f51-4703-8962-8ed889e2cc7c&action=success')

// Register the recurrence for a task.
->runOnceInFiveMinutes();


// You can add a Closure as a Job.
\Rumur\WordPress\Scheduling\Schedule::call(static function () {
    // Do something when the task is running.
})->onSuccess(static function ($task, $args) {
    // Do something when the task is run successfully.
})->onFailure(static function ($task, $args, $reason) {
    // Do something when the task is failed.
})

// You can add args for the task
->with([ 'id' => 2020, /*...*/ ])

// You can ping a url when the task is failed.
->pingOnFailure('https://domain.com/?ping=true&id=3790a0e1-3f51-4703-8962-8ed889e2cc7c&action=failed')

// You can ping a url when the task is successfully performed.
->pingOnSuccess('https://domain.com/?ping=true&id=3790a0e1-3f51-4703-8962-8ed889e2cc7c&action=success')

// Register the recurrence for a task and returns the configured task. 
->runEveryThirtyMinutes();


// functions.php

function my_own_task(array $args) {
    // Do what you need to do.
}


// *.php

// You can add a function as a Job.
\Rumur\WordPress\Scheduling\Schedule::call('my_own_task')->onSuccess(static function ($task, $args) {
    // Do something when the task is run successfully.
})->onFailure(static function ($task, $args, $reason) {
    // Do something when the task is failed.
})

// You can add args for the task
->with([ 'id' => 2020, /*...*/ ])

// You can ping a url when the task is failed.
->pingOnFailure('https://domain.com/?ping=true&id=3790a0e1-3f51-4703-8962-8ed889e2cc7c&action=failed')

// You can ping a url when the task is successfully performed.
->pingOnSuccess('https://domain.com/?ping=true&id=3790a0e1-3f51-4703-8962-8ed889e2cc7c&action=success')

// Register the recurrence for a task and returns the configured task. 
->runEveryThirtyMinutes();


// functions.php

// Register the mandatory staff to WordPress.
add_action('init', static function() {
    // In Order to add your own intervals
    
    // The simple an straight way to do that it's just use a `addInterval` method.
    // Please Note that if you added an interval that already exists in a package,
    // it will be replaced by a new one during the register time,
    // however the system's ones that were added by a WordPress won't be touched and replaced. 
    \Rumur\WordPress\Scheduling\Schedule::addInterval('every-25-minutes', 25 * MINUTE_IN_SECONDS);
    
    // Or you can add in WordPress way as well 
    \Rumur\WordPress\Scheduling\Schedule::addInterval('every-45-minutes', [
        'interval' => 45 * MINUTE_IN_SECONDS,
        'display' => esc_html__('Every 45 Minutes'),
    ]);

    // With this method the Scheduler will add intervals it uses.
    \Rumur\WordPress\Scheduling\Schedule::registerIntoWordPress();
});


$pendingTask = \Rumur\WordPress\Scheduling\Schedule::job(
    new App\Scheduling\HelloDolly('Hello Rudy, well, hello Harry')
);

$pendingTask->runOnceInWeek();
$pendingTask->runEveryMinute();
$pendingTask->runOnceInDays(33);
$pendingTask->runOnceInWeeks(2);



use Rumur\WordPress\Scheduling;

$scheduledTask = Scheduling\Schedule::job(
    new App\Scheduling\HelloDolly('Hello Rudy, well, hello Harry')
)->runOnceInWeek();

Scheduling\Schedule::resign($scheduledTask);