Commit 2372f6de authored by Erick Hitter's avatar Erick Hitter
Browse files

PHPCS fixes

parent fac26d28
<?php
/**
* Manage events via WP-CLI
*
* @package a8c_Cron_Control
*/
namespace Automattic\WP\Cron_Control\CLI;
......@@ -13,43 +18,45 @@ class Events extends \WP_CLI_Command {
*
* @subcommand list
* @synopsis [--status=<pending|completed>] [--page=<page>] [--limit=<limit>] [--format=<format>]
* @param array $args Array of positional arguments.
* @param array $assoc_args Array of flags.
*/
public function list_events( $args, $assoc_args ) {
$events = $this->get_events( $args, $assoc_args );
// Prevent one from requesting a page that doesn't exist
// Shouldn't error when first page is requested, though, as that is handled below and is an odd behaviour otherwise
// Prevent one from requesting a page that doesn't exist.
// Shouldn't error when first page is requested, though, as that is handled below and is an odd behaviour otherwise.
if ( $events['page'] > $events['total_pages'] && $events['page'] > 1 ) {
\WP_CLI::error( __( 'Invalid page requested', 'automattic-cron-control' ) );
}
// Output in the requested format
// Output in the requested format.
if ( isset( $assoc_args['format'] ) && 'ids' === $assoc_args['format'] ) {
echo implode( ' ', wp_list_pluck( $events['items'], 'ID' ) );
} else {
// Lest someone think the `completed` record should be...complete
// Lest someone think the `completed` record should be...complete.
if ( isset( $assoc_args['status'] ) && 'completed' === $assoc_args['status'] ) {
\WP_CLI::warning( __( 'Entries are purged automatically, so this cannot be relied upon as a record of past event execution.', 'automattic-cron-control' ) );
}
// Not much to do
// Not much to do.
if ( 0 === $events['total_items'] || empty( $events['items'] ) ) {
\WP_CLI::warning( __( 'No events to display', 'automattic-cron-control' ) );
return;
}
// Prepare events for display
// Prepare events for display.
$events_for_display = $this->format_events( $events['items'] );
$total_events_to_display = count( $events_for_display );
// Count, noting if showing fewer than all
// Count, noting if showing fewer than all.
if ( $events['total_items'] <= $total_events_to_display ) {
\WP_CLI::log( sprintf( _n( 'Displaying one entry', 'Displaying all %s entries', $total_events_to_display, 'automattic-cron-control' ), number_format_i18n( $total_events_to_display ) ) );
\WP_CLI::log( sprintf( _n( 'Displaying %s entry', 'Displaying all %s entries', $total_events_to_display, 'automattic-cron-control' ), number_format_i18n( $total_events_to_display ) ) );
} else {
\WP_CLI::log( sprintf( __( 'Displaying %1$s of %2$s entries, page %3$s of %4$s', 'automattic-cron-control' ), number_format_i18n( $total_events_to_display ), number_format_i18n( $events['total_items'] ), number_format_i18n( $events['page'] ), number_format_i18n( $events['total_pages'] ) ) );
}
// And reformat
// And reformat!
$format = 'table';
if ( isset( $assoc_args['format'] ) ) {
$format = $assoc_args['format'];
......@@ -75,21 +82,23 @@ class Events extends \WP_CLI_Command {
*
* @subcommand delete
* @synopsis [--event_id=<event_id>] [--action=<action>] [--completed]
* @param array $args Array of positional arguments.
* @param array $assoc_args Array of flags.
*/
public function delete_events( $args, $assoc_args ) {
// Remove a specific event
// Remove a specific event.
if ( isset( $assoc_args['event_id'] ) ) {
$this->delete_event_by_id( $args, $assoc_args );
return;
}
// Remove all events with a given action
// Remove all events with a given action.
if ( isset( $assoc_args['action'] ) ) {
$this->delete_event_by_action( $args, $assoc_args );
return;
}
// Remove all completed events
// Remove all completed events.
if ( isset( $assoc_args['completed'] ) ) {
$this->delete_completed_events( $args, $assoc_args );
return;
......@@ -103,14 +112,16 @@ class Events extends \WP_CLI_Command {
*
* @subcommand run
* @synopsis <event_id>
* @param array $args Array of positional arguments.
* @param array $assoc_args Array of flags.
*/
public function run_event( $args, $assoc_args ) {
// Validate ID
// Validate ID.
if ( ! is_numeric( $args[0] ) ) {
\WP_CLI::error( __( 'Specify the ID of an event to run', 'automattic-cron-control' ) );
}
// Retrieve information needed to execute event
// Retrieve information needed to execute event.
$event = \Automattic\WP\Cron_Control\get_event_by_id( $args[0] );
if ( ! is_object( $event ) ) {
......@@ -127,13 +138,13 @@ class Events extends \WP_CLI_Command {
\WP_CLI::confirm( sprintf( __( 'Run this event?', 'automattic-cron-control' ) ) );
// Environment preparation
// Environment preparation.
\Automattic\WP\Cron_Control\set_doing_cron();
// Run the event
// Run the event!
$run = \Automattic\WP\Cron_Control\run_event( $event->timestamp, $event->action_hashed, $event->instance, true );
// Output based on run attempt
// Output based on run attempt.
if ( is_array( $run ) ) {
\WP_CLI::success( $run['message'] );
} elseif ( is_wp_error( $run ) ) {
......@@ -145,15 +156,19 @@ class Events extends \WP_CLI_Command {
/**
* Retrieve list of events, and related data, for a given request
*
* @param array $args Array of positional arguments.
* @param array $assoc_args Array of flags.
* @return array
*/
private function get_events( $args, $assoc_args ) {
// Accept a status argument, with a default
// Accept a status argument, with a default.
$status = 'pending';
if ( isset( $assoc_args['status'] ) ) {
$status = $assoc_args['status'];
}
// Convert to status used by Event Store
// Convert to status used by Event Store.
$event_status = null;
switch ( $status ) {
case 'pending' :
......@@ -175,13 +190,13 @@ class Events extends \WP_CLI_Command {
unset( $status );
// Total to show
// Total to show.
$limit = 25;
if ( isset( $assoc_args['limit'] ) && is_numeric( $assoc_args['limit'] ) ) {
$limit = max( 1, min( absint( $assoc_args['limit'] ), 500 ) );
}
// Pagination
// Pagination.
$page = 1;
if ( isset( $assoc_args['page'] ) && is_numeric( $assoc_args['page'] ) ) {
$page = absint( $assoc_args['page'] );
......@@ -189,7 +204,7 @@ class Events extends \WP_CLI_Command {
$offset = absint( ( $page - 1 ) * $limit );
// Query
// Query!
$items = \Automattic\WP\Cron_Control\get_events( array(
'status' => $event_status,
'quantity' => $limit,
......@@ -197,12 +212,12 @@ class Events extends \WP_CLI_Command {
'force_sort' => true,
) );
// Bail if we don't get results
// Bail if we don't get results!
if ( ! is_array( $items ) ) {
\WP_CLI::error( __( 'Problem retrieving events', 'automattic-cron-control' ) );
}
// Include totals for pagination etc
// Include totals for pagination etc.
$total_items = \Automattic\WP\Cron_Control\count_events_by_status( $event_status );
$total_pages = ceil( $total_items / $limit );
......@@ -211,11 +226,14 @@ class Events extends \WP_CLI_Command {
/**
* Format event data into something human-readable
*
* @param array $events Array of events to reformat.
* @return array
*/
private function format_events( $events ) {
$formatted_events = array();
// Reformat events
// Reformat events.
foreach ( $events as $event ) {
$row = array(
'ID' => $event->ID,
......@@ -230,7 +248,7 @@ class Events extends \WP_CLI_Command {
'event_args' => '',
);
if ( $event->status === \Automattic\WP\Cron_Control\Events_Store::STATUS_PENDING ) {
if ( \Automattic\WP\Cron_Control\Events_Store::STATUS_PENDING === $event->status ) {
$row['next_run_relative'] = $this->calculate_interval( $event->timestamp - time() );
}
......@@ -254,7 +272,7 @@ class Events extends \WP_CLI_Command {
$formatted_events[] = $row;
}
// Sort results
// Sort results.
if ( ! empty( $formatted_events ) ) {
usort( $formatted_events, array( $this, 'sort_events' ) );
}
......@@ -264,9 +282,13 @@ class Events extends \WP_CLI_Command {
/**
* Sort events by timestamp, then action name
*
* @param array $first First event to compare.
* @param array $second Second event to compare.
* @return int
*/
private function sort_events( $first, $second ) {
// Timestamp is usually sufficient
// Timestamp is usually sufficient.
if ( isset( $first['next_run_gmt'] ) ) {
$first_timestamp = strtotime( $first['next_run_gmt'] );
$second_timestamp = strtotime( $second['next_run_gmt'] );
......@@ -281,7 +303,7 @@ class Events extends \WP_CLI_Command {
return $first_timestamp - $second_timestamp;
}
// If timestamps are equal, consider action
// If timestamps are equal, consider action.
return strnatcasecmp( $first['action'], $second['action'] );
}
......@@ -292,8 +314,8 @@ class Events extends \WP_CLI_Command {
*
* Borrowed from WP-CLI
*
* @param int $since An interval of time in seconds
* @return string The interval in human readable format
* @param int $since An interval of time in seconds.
* @return string
*/
private function calculate_interval( $since ) {
if ( $since <= 0 ) {
......@@ -302,7 +324,7 @@ class Events extends \WP_CLI_Command {
$since = absint( $since );
// array of time period chunks
// 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' ) ),
......@@ -310,36 +332,38 @@ class Events extends \WP_CLI_Command {
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( 01 , \_n_noop( '%s second', '%s seconds' ) ),
);
// we only want to output two chunks of time here, eg:
// x years, xx months
// x days, xx hours
// so there's only two bits of calculation below:
/**
* We only want to output two chunks of time here, eg:
* x years, xx months
* x days, xx hours
* so there's only two bits of calculation below:
*/
// step one: the first chunk
// 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 ){
// finding the biggest chunk (if the chunk fits, break).
if ( ( $count = floor( $since / $seconds ) ) != 0 ) {
break;
}
}
// set output var
$output = sprintf( \_n( $name[0], $name[1], $count ), $count );
// set output var.
$output = sprintf( \_n( $name[0], $name[1], $count ), $count ); // @codingStandardsIgnoreLine
// step two: the second chunk
// 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 ) {
// add to output var
$output .= ' ' . sprintf( \_n( $name2[0], $name2[1], $count2 ), $count2 );
// add to output var.
$output .= ' ' . sprintf( \_n( $name2[0], $name2[1], $count2 ), $count2 ); // @codingStandardsIgnoreLine
}
}
......@@ -348,22 +372,25 @@ class Events extends \WP_CLI_Command {
/**
* Delete an event by ID
*
* @param array $args Array of positional arguments.
* @param array $assoc_args Array of flags.
*/
private function delete_event_by_id( $args, $assoc_args ) {
$jid = absint( $assoc_args['event_id'] );
// Validate ID
// Validate ID.
if ( ! $jid ) {
\WP_CLI::error( __( 'Invalid event ID', 'automattic-cron-control' ) );
}
\WP_CLI::log( __( 'Locating event...', 'automattic-cron-control' ) . "\n" );
// Look up full event object
// Look up full event object.
$event = \Automattic\WP\Cron_Control\get_event_by_id( $jid );
if ( is_object( $event ) ) {
// Warning about Internal Events
// Warning about Internal Events.
if ( \Automattic\WP\Cron_Control\is_internal_event( $event->action ) ) {
\WP_CLI::warning( __( 'This is an event created by the Cron Control plugin. It will recreated automatically.', 'automattic-cron-control' ) );
}
......@@ -374,7 +401,7 @@ class Events extends \WP_CLI_Command {
\WP_CLI::log( '' );
\WP_CLI::confirm( sprintf( __( 'Are you sure you want to delete this event?', 'automattic-cron-control' ) ) );
// Try to delete the item and provide some relevant output
// Try to delete the item and provide some relevant output.
\Automattic\WP\Cron_Control\_suspend_event_creation();
$deleted = \Automattic\WP\Cron_Control\delete_event_by_id( $event->ID, true );
\Automattic\WP\Cron_Control\_resume_event_creation();
......@@ -393,42 +420,45 @@ class Events extends \WP_CLI_Command {
/**
* Delete all events of the same action
*
* @param array $args Array of positional arguments.
* @param array $assoc_args Array of flags.
*/
private function delete_event_by_action( $args, $assoc_args ) {
$action = $assoc_args['action'];
// Validate entry
// Validate entry.
if ( empty( $action ) ) {
\WP_CLI::error( __( 'Invalid action', 'automattic-cron-control' ) );
}
// Warning about Internal Events
// Warning about Internal Events.
if ( \Automattic\WP\Cron_Control\is_internal_event( $action ) ) {
\WP_CLI::warning( __( 'This is an event created by the Cron Control plugin. It will recreated automatically.', 'automattic-cron-control' ) );
}
// Set defaults needed to gather all events
// Set defaults needed to gather all events.
$assoc_args['page'] = 1;
$assoc_args['limit'] = 50;
// Gather events
// Gather events.
\WP_CLI::log( __( 'Gathering events...', 'automattic-cron-control' ) );
$events_to_delete = array();
$events = $this->get_events( $args, $assoc_args );
\WP_CLI::log( sprintf( _n( 'Found one event to check', 'Found %s events to check', $events['total_items'], 'automattic-cron-control' ), number_format_i18n( $events['total_items'] ) ) );
\WP_CLI::log( sprintf( _n( 'Found %s event to check', 'Found %s events to check', $events['total_items'], 'automattic-cron-control' ), number_format_i18n( $events['total_items'] ) ) );
$search_progress = \WP_CLI\Utils\make_progress_bar( sprintf( __( 'Searching events for those with the action `%s`', 'automattic-cron-control' ), $action ), $events['total_items'] );
// Loop and pull out events to be deleted
// Loop and pull out events to be deleted.
do {
if ( ! is_array( $events ) || empty( $events['items'] ) ) {
break;
}
// Check events for those that should be deleted
// Check events for those that should be deleted.
foreach ( $events['items'] as $single_event ) {
if ( $single_event->action === $action ) {
$events_to_delete[] = (array) $single_event;
......@@ -437,7 +467,7 @@ class Events extends \WP_CLI_Command {
$search_progress->tick();
}
// Proceed to next batch
// Proceed to next batch.
$assoc_args['page']++;
if ( $assoc_args['page'] > $events['total_pages'] ) {
......@@ -445,29 +475,28 @@ class Events extends \WP_CLI_Command {
}
$events = $this->get_events( $args, $assoc_args );
} while( $events['page'] <= $events['total_pages'] );
} while ( $events['page'] <= $events['total_pages'] );
$search_progress->finish();
\WP_CLI::log( '' );
// Nothing more to do
// Nothing more to do.
if ( empty( $events_to_delete ) ) {
\WP_CLI::error( sprintf( __( 'No events with action `%s` found', 'automattic-cron-control' ), $action ) );
}
// List the items to remove
// List the items to remove.
$total_to_delete = count( $events_to_delete );
\WP_CLI::log( sprintf( _n( 'Found one event with action `%2$s`:', 'Found %1$s events with action `%2$s`:', $total_to_delete, 'automattic-cron-control' ), number_format_i18n( $total_to_delete ), $action ) );
\WP_CLI::log( sprintf( _n( 'Found %1$s event with action `%2$s`:', 'Found %1$s events with action `%2$s`:', $total_to_delete, 'automattic-cron-control' ), number_format_i18n( $total_to_delete ), $action ) );
if ( $total_to_delete <= $assoc_args['limit'] ) {
// Sort results
// Sort results.
if ( ! empty( $events_to_delete ) ) {
usort( $events_to_delete, array( $this, 'sort_events' ) );
}
\WP_CLI\Utils\format_items( 'table', $events_to_delete, array(
'ID',
'created',
......@@ -482,13 +511,13 @@ class Events extends \WP_CLI_Command {
\WP_CLI::log( '' );
\WP_CLI::confirm( _n( 'Are you sure you want to delete this event?', 'Are you sure you want to delete these events?', $total_to_delete, 'automattic-cron-control' ) );
// Remove the items
// Remove the items.
$delete_progress = \WP_CLI\Utils\make_progress_bar( __( 'Deleting events', 'automattic-cron-control' ), $total_to_delete );
$events_deleted = array();
$events_deleted_count = $events_failed_delete = 0;
// Don't create new events while deleting events
// Don't create new events while deleting events.
\Automattic\WP\Cron_Control\_suspend_event_creation();
foreach ( $events_to_delete as $event_to_delete ) {
......@@ -510,15 +539,15 @@ class Events extends \WP_CLI_Command {
$delete_progress->finish();
// When deletes succeed, sync internal caches
// When deletes succeed, sync internal caches.
if ( $events_deleted_count > 0 ) {
\Automattic\WP\Cron_Control\_flush_internal_caches();
}
// New events can be created now that removal is complete
// New events can be created now that removal is complete.
\Automattic\WP\Cron_Control\_resume_event_creation();
// List the removed items
// List the removed items.
\WP_CLI::log( "\n" . __( 'RESULTS:', 'automattic-cron-control' ) );
if ( 1 === $total_to_delete && 1 === $events_deleted_count ) {
......@@ -530,7 +559,7 @@ class Events extends \WP_CLI_Command {
\WP_CLI::warning( sprintf( __( 'Expected to delete %1$s events, but could only delete %2$s events. It\'s likely that some events were executed while this command ran.', 'automattic-cron-control' ), number_format_i18n( $total_to_delete ), number_format_i18n( $events_deleted_count ) ) );
}
// Limit just to failed deletes when many events are removed
// Limit just to failed deletes when many events are removed.
if ( count( $events_deleted ) > $assoc_args['limit'] ) {
$events_deleted = array_filter( $events_deleted, function( $event ) {
if ( 'no' === $event['deleted'] ) {
......@@ -547,7 +576,7 @@ class Events extends \WP_CLI_Command {
\WP_CLI::log( "\n" . __( 'Events deleted:', 'automattic-cron-control' ) );
}
// Don't display a table if there's nothing to display
// Don't display a table if there's nothing to display.
if ( count( $events_deleted ) > 0 ) {
\WP_CLI\Utils\format_items( 'table', $events_deleted, array(
'ID',
......@@ -561,11 +590,14 @@ class Events extends \WP_CLI_Command {
/**
* Delete all completed events
*
* @param array $args Array of positional arguments.
* @param array $assoc_args Array of flags.
*/
private function delete_completed_events( $args, $assoc_args ) {
$count = \Automattic\WP\Cron_Control\count_events_by_status( \Automattic\WP\Cron_Control\Events_Store::STATUS_COMPLETED );
\WP_CLI::confirm( sprintf( _n( 'Found one completed event to remove. Continue?', 'Found %s completed events to remove. Continue?', $count, 'automattic-cron-control' ), number_format_i18n( $count ) ) );
\WP_CLI::confirm( sprintf( _n( 'Found %s completed event to remove. Continue?', 'Found %s completed events to remove. Continue?', $count, 'automattic-cron-control' ), number_format_i18n( $count ) ) );
\Automattic\WP\Cron_Control\Events_Store::instance()->purge_completed_events( false );
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment