Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
66 commits
Select commit Hold shift + click to select a range
f0fa8b1
More scaffolding
ethitter Sep 7, 2017
5aa1177
Match filename to namespace
ethitter Sep 7, 2017
f7c9097
Further scaffolding
ethitter Sep 7, 2017
5287b0e
Provide way to limit commands that can be scheduled
ethitter Sep 7, 2017
2bbbfba
More renaming
ethitter Sep 7, 2017
c6c452a
Better naming, most of the plugin will live here to support later UI
ethitter Sep 7, 2017
9d25b5d
Whitelist WP-CLI's native commands
ethitter Sep 7, 2017
f2808dd
Primative scaffolding to schedule a CLI command
ethitter Sep 7, 2017
b7a1650
Rough approach to run events and log results
ethitter Sep 8, 2017
eca8ecc
Time event execution
ethitter Sep 8, 2017
058a267
When the spawned command fails, return its error rather than killing …
ethitter Sep 8, 2017
a15a433
Note more TODOs
ethitter Sep 8, 2017
fbc6427
Last of package renaming
ethitter Sep 8, 2017
d87e7c1
Grunt build
ethitter Sep 8, 2017
c28c37b
Improve message format and support translation
ethitter Sep 8, 2017
009d038
Reogranize to match use and priority
ethitter Sep 8, 2017
4cdfcde
Blacklist takes priority
ethitter Sep 8, 2017
27b2abe
Allow future-dated execution
ethitter Sep 8, 2017
3658a3c
Fix variable naming to reflect what it actually is
ethitter Sep 8, 2017
83d9a3b
Bug fixes for 3658a3c
ethitter Sep 8, 2017
97baa5b
CLI command to offload a CLI command
ethitter Sep 8, 2017
3c7644a
Oops
ethitter Sep 8, 2017
ace4421
Use timestamp after bothering to return it in 97baa5b
ethitter Sep 8, 2017
20ccbcb
Fix filter, since we're whitelisting by top-level WP-CLI command
ethitter Sep 8, 2017
5366706
Remove placeholder text
ethitter Sep 8, 2017
35052c9
Grunt build
ethitter Sep 8, 2017
2a5dedb
Travis dropped 5.3
ethitter Sep 8, 2017
c808127
PHPCS fixes
ethitter Sep 8, 2017
cfe5ad3
PHPCS fixes
ethitter Sep 8, 2017
4bd67d5
PHPCS fixes
ethitter Sep 8, 2017
2052d88
PHPCS fixes
ethitter Sep 8, 2017
9da1d8c
PHPCS fixes
ethitter Sep 8, 2017
4fa216b
Test all branches, and disable xdebug during tests
ethitter Sep 8, 2017
5fc62fd
Grunt build
ethitter Sep 8, 2017
f3511ee
Prevent inception
ethitter Sep 9, 2017
3338de9
Introduce first tests, and fix test structure to appease PHPCS
ethitter Sep 11, 2017
5f8ba34
Fix logic error that blocked all commands
ethitter Sep 11, 2017
8dd650f
Incorrect PHPDoc
ethitter Sep 11, 2017
f16eee4
Fix *list tests to respect underlying functions
ethitter Sep 11, 2017
5a3878e
Add scheduling tests
ethitter Sep 11, 2017
1eebe29
Tests for scheduling blocked events too
ethitter Sep 11, 2017
8e936d5
Better use of `use`, and shorter comments to allow for more of the me…
ethitter Sep 11, 2017
d9ff51a
Don't bother retrying. Will update readme to explain the errors the p…
ethitter Sep 11, 2017
aad32db
Introduce constants and filters for command whitelists/blacklists, wi…
ethitter Sep 11, 2017
a533b45
PHP 5.6, to support arrays in constants
ethitter Sep 11, 2017
ff3aeef
Require PHP 7 to support whitelists/blacklists
ethitter Sep 11, 2017
4d69f8d
Restore tests against trunk accidentally removed in ff3aeef
ethitter Sep 11, 2017
a2e17fb
`nightly` is already 7.3
ethitter Sep 11, 2017
98642db
Grunt build
ethitter Sep 11, 2017
5b60aee
Improve command parsing by borrowing WP-CLI's approach
ethitter Sep 12, 2017
8631cab
Don't just note source in commit message
ethitter Sep 12, 2017
5e01506
Respect global versus local assoc args
ethitter Sep 12, 2017
ba5d9d6
Assuaging my neurosis
ethitter Sep 12, 2017
523bcd7
grunt build
ethitter Sep 12, 2017
a5ad987
Update readme for latest changes
ethitter Sep 12, 2017
626c3f7
Use actions so users can decide how to handle "output"
ethitter Sep 14, 2017
1c14761
First round of review feedback
ethitter Sep 25, 2017
eafe83e
Review feedback: extend filters to allow additions (and only addition…
ethitter Sep 25, 2017
919f8e3
PHPCS fix
ethitter Sep 25, 2017
94d630e
Check for basic bash operators that would permit wandering around the…
ethitter Sep 26, 2017
966d0f3
Tests for 94d630e
ethitter Sep 26, 2017
35c2831
Translation updates for 94d630e
ethitter Sep 26, 2017
7e7fe4c
Better tests
ethitter Sep 29, 2017
797e802
Review feedback
ethitter Sep 29, 2017
7fc1f2c
Block recursive scheduling, no one really likes infinite loops
ethitter Sep 29, 2017
43883c8
Simplify how the whitelist is applied
ethitter Sep 29, 2017
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 11 additions & 12 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,33 +7,32 @@ notifications:
on_success: never
on_failure: change

branches:
only:
- master

cache:
directories:
- vendor
- $HOME/.composer/cache

matrix:
include:
# PHPUnit
- php: 7.2
env: WP_VERSION=latest
- php: 7.2
env: WP_VERSION=trunk
- php: 7.1
env: WP_VERSION=latest
- php: 7.1
env: WP_VERSION=trunk
- php: 7.0
env: WP_VERSION=latest
- php: 5.6
env: WP_VERSION=4.4
- php: 5.6
env: WP_VERSION=latest
- php: 5.6
- php: 7.0
env: WP_VERSION=trunk
- php: 5.6
# PHPCS
- php: 7.1
env: WP_TRAVISCI=phpcs
- php: 5.3
env: WP_VERSION=latest

before_script:
- phpenv config-rm xdebug.ini
- export PATH="$HOME/.composer/vendor/bin:$PATH"
- |
if [[ ! -z "$WP_VERSION" ]] ; then
Expand Down
6 changes: 3 additions & 3 deletions Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ module.exports = function( grunt ) {

addtextdomain: {
options: {
textdomain: 'wp-cli-via-cron-control',
textdomain: 'wp-cli-cron-control-offload',
},
update_all_domains: {
options: {
Expand All @@ -31,8 +31,8 @@ module.exports = function( grunt ) {
target: {
options: {
domainPath: '/languages',
mainFile: 'wp-cli-via-cron-control.php',
potFilename: 'wp-cli-via-cron-control.pot',
mainFile: 'wp-cli-cron-control-offload.php',
potFilename: 'wp-cli-cron-control-offload.pot',
potHeaders: {
poedit: true,
'x-poedit-keywordslist': true
Expand Down
77 changes: 77 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
# WP-CLI Cron Control Offload #
**Contributors:** ethitter, automattic
**Tags:** wp-cli, cli, cron, cron control
**Requires at least:** 4.8.1
**Tested up to:** 4.9
**Requires PHP:** 7.0
**Stable tag:** 0.1.0
**License:** GPLv2 or later
**License URI:** http://www.gnu.org/licenses/gpl-2.0.html

Schedule WP-CLI tasks to run via Cron Control

## Description ##

Offload WP-CLI requests to cron, to be executed via (Cron Control)[https://github.com/Automattic/Cron-Control] and its CLI runner.

Provides a WP-CLI command to schedule these events. A UI is under consideration.

## Installation ##

1. Upload the `wp-cli-cron-control-offload` directry to the `/wp-content/plugins/` directory
1. Activate the plugin through the 'Plugins' menu in WordPress

## Frequently Asked Questions ##

### Where is command output captured? ###

The actions `wp_cli_cron_control_offload_run_success` and `wp_cli_cron_control_offload_run_error` are fired, allowing you to capture command output as your environment allows.

### Can this be used with commands that manipulate files? ###

This depends on how the commands are written, and the environment that hosts Cron Control's runner. The plugin makes no special accommodations to handle files required by or produced by commands.

### Why is PHP 7 required? ###

To support arrays in constants set using `define()`, PHP 7 is required. These are used to set whitelists and blacklists, though filters are also provided; see below.

### Does this support custom WP-CLI commands? ###

Yes. By default, no restrictions are placed on what commands are supported, as those restrictions depend on the environment where this plugin is used. That said, see the following sections regarding support for whitelists and blacklists.

### Can I dynamically block commands? ###

Yes, using the `wp_cli_cron_control_offload_is_command_allowed` filter. Note that the blacklist described below takes precedence over this filter (the filter is ignored). Additionally, if a whitelist is provided, the filter uses it as the default.

### Can commands be blocked or blacklisted? ###

Yes, using either the `WP_CLI_CRON_CONTROL_OFFLOAD_COMMAND_BLACKLIST` constant or the `wp_cli_cron_control_offload_command_blacklist` filter. If defined, the constant takes precedence and the filter is only able to supplement the constant's blacklist.

Regardless of whether the constant or filter is used, either should provide an array of top-level commands to permit:

```
array(
'cli',
'core',
'eval',
'eval-file',
)
```

### Can commands be restricted or whitelisted? ###

Yes, using either the `WP_CLI_CRON_CONTROL_OFFLOAD_COMMAND_WHITELIST` constant or the `wp_cli_cron_control_offload_command_whitelist` filter. If defined, the constant takes precedence and the filter is only able to supplement the constant's whitelist.

Regardless of whether the constant or filter is used, either should provide an array of top-level commands to block:

```
array(
'post',
'site',
)
```

## Changelog ##

### 0.1.0 ###
* Initial release
48 changes: 48 additions & 0 deletions includes/class-command.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php
/**
* Class Command
*
* @package WP_CLI_Cron_Control_Offload
*/

namespace Automattic\WP\WP_CLI_Cron_Control_Offload;

if ( ! defined( 'WP_CLI' ) || ! \WP_CLI ) {
return;
}

use WP_CLI;
use WP_CLI_Command;

/**
* Offload WP-CLI commands to cron
*/
class Command extends WP_CLI_Command {
/**
* Create an event to run a given WP-CLI command
*
* @subcommand create
* @synopsis --command=<command> [--timestamp=<timestamp>]
* @param array $args Array of positional arguments.
* @param array $assoc_args Array of flags.
*/
public function create( $args, $assoc_args ) {
$command = WP_CLI\Utils\get_flag_value( $assoc_args, 'command', '' );

$timestamp = WP_CLI\Utils\get_flag_value( $assoc_args, 'timestamp', null );
if ( is_numeric( $timestamp ) ) {
$timestamp = absint( $timestamp );
}

$scheduled = schedule_cli_command( $command, $timestamp );

if ( is_wp_error( $scheduled ) ) {
WP_CLI::error( $scheduled->get_error_message() );
}

/* translators: 1: Human time difference, 2. Timestamp in UTC */
WP_CLI::success( sprintf( __( 'Command scheduled for %1$s from now (%2$s)', 'wp-cli-cron-control-offload' ), human_time_diff( $scheduled ), date( 'Y-m-d H:i:s T', $scheduled ) ) );
}
}

WP_CLI::add_command( CLI_NAMESPACE, __NAMESPACE__ . '\Command' );
Loading