-
Notifications
You must be signed in to change notification settings - Fork 1
Installation & Setup
- PHP >= 5.4.0
- WordPress >= 4.7
Option A - Using Composer: (Recommended)
composer require rebelcode/cronarchyOption B - Manually: (Not recommended)
- Download a release version of this repository from the Releases page.
- Extract the
cronarchydirectory into your project. - In your project,
require_oncethe files in thecronarchy/src/directory. - Give up and use Composer π
In the coming sections, we will be assuming the below file structure. You can use whatever file structure you wish, so long as you make sure to use the correct paths when setting things up.
my-project/
βββ cron/
β βββ daemon.php
β βββ config
βββ my-project.php
You will need to set up the entry point for the daemon yourself. This will be the PHP file that Cronarchy will ping directly through an HTTP(S) request.
This script should set up a Daemon instance and run it, as demonstrated below:
use RebelCode\Cronarchy\Daemon;
require __DIR__ . '/../vendor/autoload.php';
$daemon = new Daemon('example', __DIR__ . '/config');
$daemon->run();The arguments passed to the Daemon class are:
- the ID of the Cronarchy instance to use
- the path to the config file
In this example, we're naming our instance "example" and telling the daemon to load its config from a cron-config file from the same directory (see next step).
The term "the client" in this section refers to the code in your plugin or theme that is being run normally within WordPress. The client must do three things:
- create a
Cronarchyinstance - hook into the Cronarchy instance filter and return this instance for the correct ID
- call the Runner, which in turn might ping the daemon script from step 2
The easiest way to achieve all three is by using the Cronarchy::setup() static method.
$daemonUrl = plugin_dir_url(__FILE__) . '/cron/daemon.php';
$configFile = __DIR__ . '/cron/config';
$cronarchy = Cronarchy::setup('example', $daemonUrl, $configFile);setup() method from inside a hook! Call it directly from within your plugin's file-level scope or theme's functions.php file.
The arguments passed to setup() are:
- A unique ID for this instance, which should match the ID given in step 2
- The absolute URL to the daemon script created in step 2
- The path to the daemon config file, which must point to the same file as in step 2
In the above demonstration, we set up our client to use our "example" instance, as set up in step 2. We're also assuming that the above code is running from a plugin's main file since we're using plugin_dir_url(__FILE__) to obtain the URL to the script.
The return value of Cronarchy::setup() will be your plugin or theme's own Cronarchy instance, automatically initialized and hooked into WordPress. Keep this instance stored somewhere handly to be able to schedule and manage your cron jobs.
You're done. Cronarchy will take care of the rest for you, including auto-creating the config file if it does not exist. Your theme/plugin may now start scheduling cron jobs to Cronarchy. Check out the API Documentation to learn how.