diff --git a/includes/functions.php b/includes/functions.php index 10676452238733eb2002cdc49b15743b9c046a93..da0ed9542f7c30b01d1f77c0ff5544fc12600b50 100644 --- a/includes/functions.php +++ b/includes/functions.php @@ -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; } /** diff --git a/includes/run.php b/includes/run.php index 90666587f1df5b6b3a291fbbce545c8ee94d7a6e..8e6c834bfa9db802b25f63c4b85cea85d02f8356 100644 --- a/includes/run.php +++ b/includes/run.php @@ -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; }