Download the PHP package a5hleyrich/wp-queue without Composer
On this page you can find all versions of the php package a5hleyrich/wp-queue. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download a5hleyrich/wp-queue
More information about a5hleyrich/wp-queue
Files in a5hleyrich/wp-queue
Informations about the package wp-queue
WP Queue
Job queues for WordPress.
Install
The recommended way to install this library in your project is by loading it through Composer:
It is highly recommended to prefix wrap the library class files using PHP-Scoper, to prevent collisions with other projects using this same library.
Prerequisites
WP_Queue requires PHP 7.3+.
The following database tables need to be created:
Alternatively, you can call the wp_queue_install_tables()
helper function to install the tables. If using WP_Queue in a plugin you may opt to call the helper from within your register_activation_hook
.
Jobs
Job classes should extend the WP_Queue\Job
class and normally only contain a handle
method which is called when the job is processed by the queue worker. Any data required by the job should be passed to the constructor and assigned to a public property. This data will remain available once the job is retrieved from the queue. Let's look at an example job class:
Dispatching Jobs
Jobs can be pushed to the queue like so:
You can create delayed jobs by passing an optional second parameter to the push
method. This job will be delayed by 60 minutes:
Cron Worker
Jobs need to be processed by a queue worker. You can start a cron worker like so, which piggy backs onto WP cron:
You can also specify the number of times a job should be attempted before being marked as a failure.
Restricting Allowed Job Classes
The queue will handle any subclass of WP_Queue\Job
. For better security,
it is strongly recommended that the DatabaseConnection
be instantiated with a list
of allowed Job
subclasses that are expected to be handled.
You can do that by passing in an array of Job
subclasses when the Queue
sets
up its database connection, or by having a database connection that only handles
certain Job
subclasses.
Local Development
When developing locally you may want jobs processed instantly, instead of them being pushed to the queue. This can be useful for debugging jobs via Xdebug. Add the following filter to use the sync
connection.
Contributing
Contributions are welcome via Pull Requests, but please do raise an issue before working on anything to discuss the change if there isn't already an issue. If there is an approved issue you'd like to tackle, please post a comment on it to let people know you're going to have a go at it so that effort isn't wasted through duplicated work.
Unit & Style Tests
When working on the library, please add unit tests to the appropriate file in the
tests
directory that cover your changes.
Setting Up
We use the standard WordPress test libraries for running unit tests.
Please run the following command to set up the libraries:
Substitute db_name
, db_user
and db_pass
as appropriate.
Please be aware that running the unit tests is a destructive operation, database
tables will be cleared, so please use a database name dedicated to running unit tests.
The standard database name usually used by the WordPress community is wordpress_test
, e.g.
Please refer to the Initialize the testing environment locally section of the WordPress Handbook's Plugin Integration Tests entry should you run into any issues.
Running Unit Tests
To run the unit tests, simply run:
If the composer
dependencies aren't in place, they'll be automatically installed first.
Running Style Tests
It's important that the code in the library use a consistent style to aid in quickly
understanding it, and to avoid some common issues. PHP_Code_Sniffer
is used with
mostly standard WordPress rules to help check for consistency.
To run the style tests, simply run:
If the composer
dependencies aren't in place, they'll be automatically installed first.
Running All Tests
To make things super simple, just run the following to run all tests:
If the composer
dependencies aren't in place, they'll be automatically installed first.
Creating a PR
When creating a PR, please make sure to mention which GitHub issue is being resolved at the top of the description, e.g.:
Resolves #123
The unit and style tests will be run automatically, the PR will not be eligible for
merge unless they pass, and the branch is up-to-date with master
.
License
WP Queue is open-sourced software licensed under the MIT license.