Skip to content
Snippets Groups Projects
Commit f2808dd0 authored by Erick Hitter's avatar Erick Hitter
Browse files

Primative scaffolding to schedule a CLI command

parent 9d25b5df
No related branches found
No related tags found
No related merge requests found
......@@ -9,7 +9,7 @@ namespace Automattic\WP\WP_CLI_Cron_Control_Offload;
* @return bool
*/
function is_subcommand_allowed( $subcommand ) {
return in_array( $subcommand, get_subcommand_whitelist(), true ) && ! in_array( $subcommand, get_subcommand_blacklist(), true );
return in_array( $subcommand, get_command_whitelist(), true ) && ! in_array( $subcommand, get_command_blacklist(), true );
}
/**
......@@ -17,13 +17,12 @@ function is_subcommand_allowed( $subcommand ) {
*
* @return array
*/
function get_subcommand_whitelist() {
function get_command_whitelist() {
// Supported built-in commands
$whitelist = array(
'cache',
'cap',
'comment',
'import',
'media',
'menu',
'network',
......@@ -52,7 +51,7 @@ function get_subcommand_whitelist() {
*
* @return array
*/
function get_subcommand_blacklist() {
function get_command_blacklist() {
return array(
'cli',
'config',
......@@ -60,9 +59,50 @@ function get_subcommand_blacklist() {
'cron',
'db',
'eval',
'export',
'eval-file',
'import',
'package',
'scaffold',
'server',
);
}
/**
* Create cron event for a given WP-CLI command
*
* return bool|\WP_Error
*/
function schedule_cli_command( $args ) {
$event_args = validate_args( $args );
if ( is_wp_error( $event_args ) ) {
return $event_args;
}
$scheduled = wp_schedule_single_event( strtotime( '+30 seconds' ), ACTION, $event_args );
return false !== $scheduled;
}
/**
* Validate WP-CLI command to be scheduled
*
* @param array $args
* @return array|\WP_Error
*/
function validate_args( $args ) {
$validated_args = array();
// TODO: validate
// TODO: strip leading "wp"
// TODO: check first positional argument against `is_command_allowed()`
$validated_args['command'] = $args;
if ( empty( $validated_args ) ) {
return new \WP_Error( 'Arguments could not be parsed for validation.' );
}
return $validated_args;
}
......@@ -3,14 +3,19 @@
namespace Automattic\WP\WP_CLI_Cron_Control_Offload;
/**
* Intended for non-interactive use, so all output ends up in the error log
*
* @param array $args
* @return null
*/
function run_event( $args ) {
if ( ! defined( 'WP_CLI' ) || ! \WP_CLI ) {
trigger_error( 'Attempted to run event without WP-CLI loaded. ' . compact( $args ), E_USER_WARNING );
// TODO: reschedule at least once or twice
trigger_error( 'Attempted to run event without WP-CLI loaded. ' . var_export( $args, true ), E_USER_WARNING );
return false;
}
// TODO: run event, sending output to error log
trigger_error( var_export( $args, true ), E_USER_NOTICE );
}
add_action( ACTION, __NAMESPACE__ . '\run_event' );
......@@ -2,3 +2,8 @@
namespace Automattic\WP\WP_CLI_Cron_Control_Offload;
if ( ! defined( 'WP_CLI' ) || ! \WP_CLI ) {
return false;
}
// TODO: WP-CLI command to schedule an event
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment