Newer
Older
/**
* Common plugin functions
*
* @package WP_CLI_Cron_Control_Offload
*/
namespace Automattic\WP\WP_CLI_Cron_Control_Offload;
/**
* Create cron event for a given WP-CLI command
*
* @param string $command WP-CLI command to schedule.
* @param int $timestamp Optional. Unix timestamp to schedule command to run at.
function schedule_cli_command( $command, $timestamp = null ) {
$command = validate_command( $command );
if ( is_wp_error( $command ) ) {
return $command;
if ( ! is_int( $timestamp ) ) {
$timestamp = strtotime( '+30 seconds' );
}
if ( $timestamp <= time() ) {
return new WP_Error( 'invalid-timestamp', __( 'Timestamp is in the past.', 'wp-cli-cron-control-offload' ) );
}
$scheduled = wp_schedule_single_event( $timestamp, ACTION, $event_args );
if ( false === $scheduled ) {
return new WP_Error( 'not-scheduled', __( 'Command may already be scheduled, or it was blocked via the `schedule_event` filter.', 'wp-cli-cron-control-offload' ) );
}
return $timestamp;
}
/**
* Validate WP-CLI command to be scheduled
*
function validate_command( $command ) {
$command = parse_command( $command );
// Failed to parse command.
if ( is_wp_error( $command ) ) {
return $command;
if ( ! is_command_allowed( $command['positional_args'][0] ) ) {
return new WP_Error( 'blocked-command', sprintf( __( '`%1$s` not allowed', 'wp-cli-cron-control-offload' ), $command['positional_args'][0] ) );
$command = implode_parsed_command( $command );
* Check if command is allowed
* @param string $command Top-level WP-CLI command to check against blacklist and whitelist.
function is_command_allowed( $command ) {
// Command explicitly disallowed.
if ( in_array( $command, get_command_blacklist(), true ) ) {
return false;
}
Erick Hitter
committed
// If there's a whitelist, default to it.
if ( ! empty( get_command_whitelist() ) ) {
add_filter( 'wp_cli_cron_control_offload_is_command_allowed', __NAMESPACE__ . '\command_is_whitelisted', 9, 2 );
}
return apply_filters( 'wp_cli_cron_control_offload_is_command_allowed', true, $command );
}
/**
* Filter callback to check a command against a whitelist
*
* @param bool $whitelisted Command is allowed.
* @param string $command Command to check.
* @return bool
*/
function command_is_whitelisted( $whitelisted, $command ) {
return in_array( $command, get_command_whitelist(), true );
Erick Hitter
committed
* Support a whitelist of commands
*
* @return array
*/
function get_command_whitelist() {
Erick Hitter
committed
if ( defined( 'WP_CLI_CRON_CONTROL_OFFLOAD_COMMAND_WHITELIST' ) && is_array( \WP_CLI_CRON_CONTROL_OFFLOAD_COMMAND_WHITELIST ) ) {
return \WP_CLI_CRON_CONTROL_OFFLOAD_COMMAND_WHITELIST;
}
Erick Hitter
committed
return apply_filters( 'wp_cli_cron_control_offload_command_whitelist', array() );
Erick Hitter
committed
* Allow commands to be blocked
*
* @return array
*/
function get_command_blacklist() {
Erick Hitter
committed
if ( defined( 'WP_CLI_CRON_CONTROL_OFFLOAD_COMMAND_BLACKLIST' ) && is_array( \WP_CLI_CRON_CONTROL_OFFLOAD_COMMAND_BLACKLIST ) ) {
return \WP_CLI_CRON_CONTROL_OFFLOAD_COMMAND_BLACKLIST;
}
return apply_filters( 'wp_cli_cron_control_offload_command_blacklist', array() );
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
/**
* Splits positional args from associative args.
*
* @param array|string $command String or array of command to parse.
* @return array|WP_Error
*/
function parse_command( $command ) {
// Borrowed code expects an array, but a string is easier for our needs.
if ( is_string( $command ) ) {
$command = explode( ' ', $command );
}
// Bad request.
if ( ! is_array( $command ) || empty( $command ) ) {
return new WP_Error( 'command-parse-failed', __( 'Failed to parse requested command', 'wp-cli-cron-control-offload' ) );
}
// WP_CLI::runcommand doesn't need the `wp`.
$wp = array_search( 'wp', $command );
if ( false !== $wp ) {
unset( $command[ $wp ] );
}
// Match naming in what's borrowed from WP-CLI.
$arguments = $command;
// Start what's borrowed from WP-CLI. @codingStandardsIgnoreStart
$positional_args = $assoc_args = $global_assoc = $local_assoc = array();
foreach ( $arguments as $arg ) {
$positional_arg = $assoc_arg = null;
if ( preg_match( '|^--no-([^=]+)$|', $arg, $matches ) ) {
$assoc_arg = array( $matches[1], false );
} elseif ( preg_match( '|^--([^=]+)$|', $arg, $matches ) ) {
$assoc_arg = array( $matches[1], true );
} elseif ( preg_match( '|^--([^=]+)=(.*)|s', $arg, $matches ) ) {
$assoc_arg = array( $matches[1], $matches[2] );
} else {
$positional = $arg;
}
if ( ! is_null( $assoc_arg ) ) {
// Start addition.
// Skip, allowing WP-CLI to inherit
if ( 'allow-root' === $assoc_arg[0] ) {
continue;
}
// End addition.
$assoc_args[] = $assoc_arg;
if ( count( $positional_args ) ) {
$local_assoc[] = $assoc_arg;
} else {
$global_assoc[] = $assoc_arg;
}
} else if ( ! is_null( $positional ) ) {
$positional_args[] = $positional;
}
}
// End what's borrowed from WP-CLI. @codingStandardsIgnoreEnd
// Nothing to do.
if ( ! isset( $positional_args[0] ) || empty( $positional_args[0] ) ) {
return new WP_Error( 'no-command-specified', __( 'No command was provided.', 'wp-cli-cron-control-offload' ) );
}
return compact( 'positional_args', 'assoc_args', 'global_assoc', 'local_assoc' );
}
/**
* Restores a parsed command to a string WP-CLI can run
*
* @param array $command Parsed command to convert to string.
* @return string|WP_Error
*/
function implode_parsed_command( $command ) {
if ( ! is_array( $command ) || empty( $command['positional_args'] ) ) {
return new WP_Error( 'no-command-specified', __( 'No command was provided.', 'wp-cli-cron-control-offload' ) );
}
$to_implode = $command['positional_args'];
if ( ! empty( $command['assoc_args'] ) ) {
foreach ( $command['assoc_args'] as $assoc_arg ) {
if ( true === $assoc_arg[1] ) {
$to_implode[] = '--' . $assoc_arg[0];
} else {
$to_implode[] = sprintf( '--%1$s=%2$s', $assoc_arg[0], $assoc_arg[1] );
}
}
}
$imploded = trim( implode( ' ', $to_implode ) );
if ( empty( $imploded ) ) {
return new WP_Error( 'command-implode-failed', __( 'Failed to convert command array to string.', 'wp-cli-cron-control-offload' ) );
}
return $imploded;
}