diff --git a/.distignore b/.distignore index b964b40c7..95b52fb02 100644 --- a/.distignore +++ b/.distignore @@ -6,6 +6,8 @@ .travis.yml behat.yml circle.yml +phpcs.xml.dist +phpunit.xml.dist bin/ features/ utils/ diff --git a/.gitignore b/.gitignore index ff4941991..bcf211b32 100644 --- a/.gitignore +++ b/.gitignore @@ -6,3 +6,6 @@ vendor/ *.tar.gz composer.lock *.log +phpunit.xml +phpcs.xml +.phpcs.xml diff --git a/composer.json b/composer.json index 09f2dff18..fd2519c7c 100644 --- a/composer.json +++ b/composer.json @@ -16,7 +16,7 @@ }, "require-dev": { "wp-cli/entity-command": "^1.3 || ^2", - "wp-cli/wp-cli-tests": "^2.0.11" + "wp-cli/wp-cli-tests": "^2.1" }, "config": { "process-timeout": 7200, diff --git a/cron-command.php b/cron-command.php index eedb81eaa..96022abec 100644 --- a/cron-command.php +++ b/cron-command.php @@ -4,9 +4,9 @@ return; } -$autoload = dirname( __FILE__ ) . '/vendor/autoload.php'; -if ( file_exists( $autoload ) ) { - require_once $autoload; +$wpcli_cron_autoloader = dirname( __FILE__ ) . '/vendor/autoload.php'; +if ( file_exists( $wpcli_cron_autoloader ) ) { + require_once $wpcli_cron_autoloader; } WP_CLI::add_command( 'cron', 'Cron_Command' ); diff --git a/phpcs.xml.dist b/phpcs.xml.dist new file mode 100644 index 000000000..4c5ea7966 --- /dev/null +++ b/phpcs.xml.dist @@ -0,0 +1,59 @@ + + + Custom ruleset for WP-CLI cron-command + + + + + . + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + */src/Cron_(Event_|Schedule_)?Command\.php$ + + + diff --git a/src/Cron_Command.php b/src/Cron_Command.php index b93c86fa6..127186e71 100644 --- a/src/Cron_Command.php +++ b/src/Cron_Command.php @@ -69,15 +69,19 @@ protected static function get_cron_spawn() { $sslverify = \WP_CLI\Utils\wp_version_compare( 4.0, '<' ); $doing_wp_cron = sprintf( '%.22F', microtime( true ) ); - $cron_request = apply_filters( 'cron_request', array( + $cron_request_array = array( 'url' => site_url( 'wp-cron.php?doing_wp_cron=' . $doing_wp_cron ), 'key' => $doing_wp_cron, 'args' => array( 'timeout' => 3, 'blocking' => true, - 'sslverify' => apply_filters( 'https_local_ssl_verify', $sslverify ) - ) - ) ); + // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound -- Calling native WordPress hook. + 'sslverify' => apply_filters( 'https_local_ssl_verify', $sslverify ), + ), + ); + + // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound -- Calling native WordPress hook. + $cron_request = apply_filters( 'cron_request', $cron_request_array ); # Enforce a blocking request in case something that's hooked onto the 'cron_request' filter sets it to false $cron_request['args']['blocking'] = true; diff --git a/src/Cron_Event_Command.php b/src/Cron_Event_Command.php index fde3a77f0..3b76b300c 100644 --- a/src/Cron_Event_Command.php +++ b/src/Cron_Event_Command.php @@ -1,5 +1,7 @@ format ) { + if ( 'ids' === $formatter->format ) { echo implode( ' ', wp_list_pluck( $events, 'hook' ) ); } else { $formatter->display_items( $events ); @@ -153,13 +156,13 @@ public function list_( $args, $assoc_args ) { */ public function schedule( $args, $assoc_args ) { - $hook = $args[0]; - $next_run = \WP_CLI\Utils\get_flag_value( $args, 1, 'now' ); - $recurrence = \WP_CLI\Utils\get_flag_value( $args, 2, false ); + $hook = $args[0]; + $next_run = Utils\get_flag_value( $args, 1, 'now' ); + $recurrence = Utils\get_flag_value( $args, 2, false ); if ( empty( $next_run ) ) { $timestamp = time(); - } else if ( is_numeric( $next_run ) ) { + } elseif ( is_numeric( $next_run ) ) { $timestamp = absint( $next_run ); } else { $timestamp = strtotime( $next_run ); @@ -173,7 +176,7 @@ public function schedule( $args, $assoc_args ) { $schedules = wp_get_schedules(); - if ( ! isset( $schedules[$recurrence] ) ) { + if ( ! isset( $schedules[ $recurrence ] ) ) { WP_CLI::error( sprintf( "'%s' is not a valid schedule name for recurrence.", $recurrence ) ); } @@ -215,7 +218,7 @@ public function schedule( $args, $assoc_args ) { */ public function run( $args, $assoc_args ) { - if ( empty( $args ) && ! \WP_CLI\Utils\get_flag_value( $assoc_args, 'due-now' ) && ! \WP_CLI\Utils\get_flag_value( $assoc_args, 'all' ) ) { + if ( empty( $args ) && ! Utils\get_flag_value( $assoc_args, 'due-now' ) && ! Utils\get_flag_value( $assoc_args, 'all' ) ) { WP_CLI::error( 'Please specify one or more cron events, or use --due-now/--all.' ); } @@ -226,15 +229,15 @@ public function run( $args, $assoc_args ) { } $hooks = wp_list_pluck( $events, 'hook' ); - foreach( $args as $hook ) { + foreach ( $args as $hook ) { if ( ! in_array( $hook, $hooks, true ) ) { WP_CLI::error( sprintf( "Invalid cron event '%s'", $hook ) ); } } - if ( \WP_CLI\Utils\get_flag_value( $assoc_args, 'due-now' ) ) { + if ( Utils\get_flag_value( $assoc_args, 'due-now' ) ) { $due_events = array(); - foreach( $events as $event ) { + foreach ( $events as $event ) { if ( ! empty( $args ) && ! in_array( $event->hook, $args, true ) ) { continue; } @@ -243,10 +246,10 @@ public function run( $args, $assoc_args ) { } } $events = $due_events; - } else if ( ! \WP_CLI\Utils\get_flag_value( $assoc_args, 'all' ) ) { + } elseif ( ! Utils\get_flag_value( $assoc_args, 'all' ) ) { $due_events = array(); - foreach( $events as $event ) { - if ( in_array( $event->hook, $args ) ) { + foreach ( $events as $event ) { + if ( in_array( $event->hook, $args, true ) ) { $due_events[] = $event; } } @@ -255,9 +258,9 @@ public function run( $args, $assoc_args ) { $executed = 0; foreach ( $events as $event ) { - $start = microtime( true ); + $start = microtime( true ); $result = self::run_event( $event ); - $total = round( microtime( true ) - $start, 3 ); + $total = round( microtime( true ) - $start, 3 ); $executed++; WP_CLI::log( sprintf( "Executed the cron event '%s' in %ss.", $event->hook, $total ) ); } @@ -275,16 +278,18 @@ public function run( $args, $assoc_args ) { protected static function run_event( stdClass $event ) { if ( ! defined( 'DOING_CRON' ) ) { + // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedConstantFound -- Using native WordPress constant. define( 'DOING_CRON', true ); } - if ( $event->schedule != false ) { + if ( false !== $event->schedule ) { $new_args = array( $event->time, $event->schedule, $event->hook, $event->args ); call_user_func_array( 'wp_reschedule_event', $new_args ); } wp_unschedule_event( $event->time, $event->hook, $event->args ); + // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.DynamicHooknameFound -- Can't prefix dynamic hooks here, calling registered hooks. do_action_ref_array( $event->hook, $event->args ); return true; @@ -316,7 +321,7 @@ public function delete( $args, $assoc_args ) { $deleted = 0; foreach ( $events as $event ) { - if ( $event->hook == $hook ) { + if ( $event->hook === $hook ) { $result = self::delete_event( $event ); if ( $result ) { $deleted++; @@ -327,7 +332,7 @@ public function delete( $args, $assoc_args ) { } if ( $deleted ) { - $message = ( 1 == $deleted ) ? "Deleted the cron event '%2\$s'." : "Deleted %1\$d instances of the cron event '%2\$s'."; + $message = ( 1 === $deleted ) ? "Deleted the cron event '%2\$s'." : "Deleted %1\$d instances of the cron event '%2\$s'."; WP_CLI::success( sprintf( $message, $deleted, $hook ) ); } else { WP_CLI::error( sprintf( "Invalid cron event '%s'.", $hook ) ); @@ -344,7 +349,7 @@ public function delete( $args, $assoc_args ) { protected static function delete_event( stdClass $event ) { $crons = _get_cron_array(); - if ( ! isset( $crons[$event->time][$event->hook][$event->sig] ) ) { + if ( ! isset( $crons[ $event->time ][ $event->hook ][ $event->sig ] ) ) { return false; } @@ -395,7 +400,7 @@ protected static function get_cron_events() { 'sig' => $sig, 'args' => $data['args'], 'schedule' => $data['schedule'], - 'interval' => \WP_CLI\Utils\get_flag_value( $data, 'interval' ), + 'interval' => Utils\get_flag_value( $data, 'interval' ), ); } @@ -425,13 +430,13 @@ private static function interval( $since ) { // array of time period chunks $chunks = array( - array( 60 * 60 * 24 * 365 , \_n_noop( '%s year', '%s years' ) ), - array( 60 * 60 * 24 * 30 , \_n_noop( '%s month', '%s months' ) ), - array( 60 * 60 * 24 * 7, \_n_noop( '%s week', '%s weeks' ) ), - array( 60 * 60 * 24 , \_n_noop( '%s day', '%s days' ) ), - array( 60 * 60 , \_n_noop( '%s hour', '%s hours' ) ), - array( 60 , \_n_noop( '%s minute', '%s minutes' ) ), - array( 1 , \_n_noop( '%s second', '%s seconds' ) ), + array( 60 * 60 * 24 * 365, 'year' ), + array( 60 * 60 * 24 * 30, 'month' ), + array( 60 * 60 * 24 * 7, 'week' ), + array( 60 * 60 * 24, 'day' ), + array( 60 * 60, 'hour' ), + array( 60, 'minute' ), + array( 1, 'second' ), ); // we only want to output two chunks of time here, eg: @@ -441,26 +446,28 @@ private static function interval( $since ) { // step one: the first chunk for ( $i = 0, $j = count( $chunks ); $i < $j; $i++ ) { - $seconds = $chunks[$i][0]; - $name = $chunks[$i][1]; + $seconds = $chunks[ $i ][0]; + $name = $chunks[ $i ][1]; // finding the biggest chunk (if the chunk fits, break) - if ( ( $count = floor( $since / $seconds ) ) != 0 ){ + $count = floor( $since / $seconds ); + if ( floatval( 0 ) !== $count ) { break; } } // set output var - $output = sprintf( \_n( $name[0], $name[1], $count ), $count ); + $output = sprintf( '%d %s', $count, Utils\pluralize( $name, absint( $count ) ) ); // step two: the second chunk if ( $i + 1 < $j ) { - $seconds2 = $chunks[$i + 1][0]; - $name2 = $chunks[$i + 1][1]; + $seconds2 = $chunks[ $i + 1 ][0]; + $name2 = $chunks[ $i + 1 ][1]; - if ( ( $count2 = floor( ( $since - ( $seconds * $count ) ) / $seconds2 ) ) != 0 ) { + $count2 = floor( ( $since - ( $seconds * $count ) ) / $seconds2 ); + if ( floatval( 0 ) !== $count2 ) { // add to output var - $output .= ' ' . sprintf( \_n( $name2[0], $name2[1], $count2 ), $count2 ); + $output .= ' ' . sprintf( '%d %s', $count2, Utils\pluralize( $name2, absint( $count2 ) ) ); } } diff --git a/src/Cron_Schedule_Command.php b/src/Cron_Schedule_Command.php index 08e4eae2c..46d850ec4 100644 --- a/src/Cron_Schedule_Command.php +++ b/src/Cron_Schedule_Command.php @@ -79,7 +79,7 @@ public function list_( $args, $assoc_args ) { $schedules = self::get_schedules(); - if ( 'ids' == $formatter->format ) { + if ( 'ids' === $formatter->format ) { echo implode( ' ', wp_list_pluck( $schedules, 'name' ) ); } else { $formatter->display_items( $schedules ); @@ -106,7 +106,7 @@ protected static function format_schedule( array $schedule, $name ) { */ protected static function get_schedules() { $schedules = wp_get_schedules(); - if ( !empty( $schedules ) ) { + if ( ! empty( $schedules ) ) { uasort( $schedules, 'Cron_Schedule_Command::sort' ); $schedules = array_map( 'Cron_Schedule_Command::format_schedule', $schedules, array_keys( $schedules ) ); }