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

Fix variable naming to reflect what it actually is

parent 27b2abe4
No related branches found
No related tags found
No related merge requests found
......@@ -5,22 +5,24 @@ namespace Automattic\WP\WP_CLI_Cron_Control_Offload;
/**
* Create cron event for a given WP-CLI command
*
* @param string $args
* @param string $command
* @param int $timestamp Optional.
* @return bool|\WP_Error
*/
function schedule_cli_command( $args, $timestamp = null ) {
$event_args = validate_args( $args );
function schedule_cli_command( $command, $timestamp = null ) {
$command = validate_command( $command );
if ( is_wp_error( $event_args ) ) {
return $event_args;
if ( is_wp_error( $command ) ) {
return $command;
}
if ( ! is_int( $timestamp ) ) {
$timestamp = strtotime( '+30 seconds' );
}
$scheduled = wp_schedule_single_event( $timestamp, ACTION, array( 'command' => $event_args ) );
$event_args = array( 'command' => $command, );
$scheduled = wp_schedule_single_event( $timestamp, ACTION, $event_args );
return false !== $scheduled;
}
......@@ -31,32 +33,32 @@ function schedule_cli_command( $args, $timestamp = null ) {
* @param string $args
* @return array|\WP_Error
*/
function validate_args( $args ) {
function validate_command( $command ) {
// Strip `wp` if included
if ( 0 === stripos( $args, 'wp' ) ) {
$args = trim( substr( $args, 2 ) );
if ( 0 === stripos( $command, 'wp' ) ) {
$command = trim( substr( $command, 2 ) );
}
// Block disallowed commands
$command = explode( ' ', $args );
$command = array_shift( $command );
if ( ! is_command_allowed( $command ) ) {
return new \WP_Error( "$command not allowed" );
$first_command = explode( ' ', $command );
$first_command = array_shift( $first_command );
if ( ! is_command_allowed( $first_command ) ) {
return new \WP_Error( sprintf( __( '%1$s: `%2$s` not allowed', 'wp-cli-cron-control-offload' ), MESSAGE_PREFIX, $first_command ) );
}
// Don't worry about the user WP-CLI runs as
if ( false === stripos( $args, '--allow-root' ) ) {
if ( false === stripos( $command, '--allow-root' ) ) {
$args .= ' --allow-root';
}
// TODO: validate further
// Nothing to run
if ( empty( $args ) ) {
if ( empty( $command ) ) {
return new \WP_Error( 'Invalid command provided' );
}
return $args;
return $command;
}
/**
......
......@@ -15,7 +15,7 @@ function run_event( $command ) {
return;
}
if ( ! validate_args( $command ) ) {
if ( ! validate_command( $command ) ) {
trigger_error( sprintf( __( '%1$s: Attempted to run blocked WP-CLI command. (%2$s)', 'wp-cli-cron-control-offload' ), MESSAGE_PREFIX, var_export( $command, true ) ), E_USER_WARNING );
return;
}
......
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