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
Branches
No related tags found
No related merge requests found
...@@ -5,22 +5,24 @@ namespace Automattic\WP\WP_CLI_Cron_Control_Offload; ...@@ -5,22 +5,24 @@ namespace Automattic\WP\WP_CLI_Cron_Control_Offload;
/** /**
* Create cron event for a given WP-CLI command * Create cron event for a given WP-CLI command
* *
* @param string $args * @param string $command
* @param int $timestamp Optional. * @param int $timestamp Optional.
* @return bool|\WP_Error * @return bool|\WP_Error
*/ */
function schedule_cli_command( $args, $timestamp = null ) { function schedule_cli_command( $command, $timestamp = null ) {
$event_args = validate_args( $args ); $command = validate_command( $command );
if ( is_wp_error( $event_args ) ) { if ( is_wp_error( $command ) ) {
return $event_args; return $command;
} }
if ( ! is_int( $timestamp ) ) { if ( ! is_int( $timestamp ) ) {
$timestamp = strtotime( '+30 seconds' ); $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; return false !== $scheduled;
} }
...@@ -31,32 +33,32 @@ function schedule_cli_command( $args, $timestamp = null ) { ...@@ -31,32 +33,32 @@ function schedule_cli_command( $args, $timestamp = null ) {
* @param string $args * @param string $args
* @return array|\WP_Error * @return array|\WP_Error
*/ */
function validate_args( $args ) { function validate_command( $command ) {
// Strip `wp` if included // Strip `wp` if included
if ( 0 === stripos( $args, 'wp' ) ) { if ( 0 === stripos( $command, 'wp' ) ) {
$args = trim( substr( $args, 2 ) ); $command = trim( substr( $command, 2 ) );
} }
// Block disallowed commands // Block disallowed commands
$command = explode( ' ', $args ); $first_command = explode( ' ', $command );
$command = array_shift( $command ); $first_command = array_shift( $first_command );
if ( ! is_command_allowed( $command ) ) { if ( ! is_command_allowed( $first_command ) ) {
return new \WP_Error( "$command not allowed" ); 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 // 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'; $args .= ' --allow-root';
} }
// TODO: validate further // TODO: validate further
// Nothing to run // Nothing to run
if ( empty( $args ) ) { if ( empty( $command ) ) {
return new \WP_Error( 'Invalid command provided' ); return new \WP_Error( 'Invalid command provided' );
} }
return $args; return $command;
} }
/** /**
......
...@@ -15,7 +15,7 @@ function run_event( $command ) { ...@@ -15,7 +15,7 @@ function run_event( $command ) {
return; 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 ); 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; return;
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment