class-one-time-fixers.php 5.82 KB
Newer Older
1
<?php
Erick Hitter's avatar
Erick Hitter committed
2
3
4
5
6
/**
 * Clean up plugin data via WP-CLI
 *
 * @package a8c_Cron_Control
 */
7
8
9

namespace Automattic\WP\Cron_Control\CLI;

10
11
12
/**
 * Run one-time fixers for Cron Control
 */
13
class One_Time_Fixers extends \WP_CLI_Command {
14
15
16
17
18
	/**
	 * Remove all data stored in the plugin's table
	 *
	 * @subcommand remove-all-plugin-data
	 * @synopsis [--dry-run=<dry-run>]
Erick Hitter's avatar
Erick Hitter committed
19
20
	 * @param array $args Array of positional arguments.
	 * @param array $assoc_args Array of flags.
21
22
23
24
25
26
27
28
29
30
31
	 */
	public function purge( $args, $assoc_args ) {
		global $wpdb;

		// Are we actually destroying any data?
		$dry_run = true;

		if ( isset( $assoc_args['dry-run'] ) && 'false' === $assoc_args['dry-run'] ) {
			$dry_run = false;
		}

Erick Hitter's avatar
Erick Hitter committed
32
		// Provide some idea of what's going on.
33
		\WP_CLI::log( __( 'CRON CONTROL', 'automattic-cron-control' ) . "\n" );
34
35

		$table_name = \Automattic\WP\Cron_Control\Events_Store::instance()->get_table_name();
Erick Hitter's avatar
Erick Hitter committed
36
		$count = (int) $wpdb->get_var( "SELECT COUNT(ID) FROM {$table_name}" ); // Cannot prepare table name. @codingStandardsIgnoreLine
37
38

		if ( $count > 1 ) {
Erick Hitter's avatar
Erick Hitter committed
39
			/* translators: 1: Event count */
40
			\WP_CLI::log( sprintf( __( 'Found %s total items', 'automattic-cron-control' ), number_format_i18n( $count ) ) . "\n\n" );
41
42
43
44
45
46
47
48
49
50

			if ( $dry_run ) {
				\WP_CLI::error( __( 'Stopping as this is a dry run!', 'automattic-cron-control' ) );
			}
		} else {
			\WP_CLI::error( __( 'No entries found...aborting!', 'automattic-cron-control' ) );
		}

		// Should we really destroy all this data?
		if ( ! $dry_run ) {
51
			\WP_CLI::log( __( 'This process will remove all data for the Cron Control plugin', 'automattic-cron-control' ) );
52
			\WP_CLI::confirm( __( 'Proceed?', 'automattic-cron-control' ) );
53
			\WP_CLI::log( "\n" . __( 'Starting...', 'automattic-cron-control' ) . "\n" );
54
55
		}

Erick Hitter's avatar
Erick Hitter committed
56
		// Don't create new events while deleting events.
57
58
		\Automattic\WP\Cron_Control\_suspend_event_creation();

Erick Hitter's avatar
Erick Hitter committed
59
		// Don't truncate as it requires DROP and resets auto-increment value.
60
		if ( ! $dry_run ) {
Erick Hitter's avatar
Erick Hitter committed
61
			$wpdb->query( "DELETE FROM {$table_name}" ); // Cannot prepare table name. @codingStandardsIgnoreLine
62
63
		}

Erick Hitter's avatar
Erick Hitter committed
64
		// Remove the now-stale cache when actively run.
65
66
		if ( ! $dry_run ) {
			\Automattic\WP\Cron_Control\_flush_internal_caches();
Erick Hitter's avatar
Erick Hitter committed
67
			/* translators: 1: Plugin name */
68
			\WP_CLI::log( "\n" . sprintf( __( 'Cleared the %s cache', 'automattic-cron-control' ), 'Cron Control' ) );
69
70
		}

Erick Hitter's avatar
Erick Hitter committed
71
		// Let event creation resume.
72
73
		\Automattic\WP\Cron_Control\_resume_event_creation();

Erick Hitter's avatar
Erick Hitter committed
74
		// Fin.
75
76
77
		\WP_CLI::success( __( 'All done.', 'automattic-cron-control' ) );
	}

78
	/**
79
	 * Remove Cron Control data previously stored in a CPT
80
	 *
81
	 * @subcommand purge-legacy-cpt-entries
82
	 * @synopsis [--batch-size=<batch-size>] [--dry-run=<dry-run>]
Erick Hitter's avatar
Erick Hitter committed
83
84
	 * @param array $args Array of positional arguments.
	 * @param array $assoc_args Array of flags.
85
	 */
86
	public function purge_cpt( $args, $assoc_args ) {
87
88
		global $wpdb;

89
90
91
92
93
94
95
		// Are we actually destroying any data?
		$dry_run = true;

		if ( isset( $assoc_args['dry-run'] ) && 'false' === $assoc_args['dry-run'] ) {
			$dry_run = false;
		}

Erick Hitter's avatar
Erick Hitter committed
96
		// Provide some idea of what's going on.
97
		\WP_CLI::log( __( 'CRON CONTROL', 'automattic-cron-control' ) . "\n" );
98

99
		$count = (int) $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(ID) FROM {$wpdb->posts} WHERE post_type = %s;", 'a8c_cron_ctrl_event' ) );
100

Erick Hitter's avatar
Erick Hitter committed
101
		if ( $count > 0 ) {
Erick Hitter's avatar
Erick Hitter committed
102
			/* translators: 1: Event count */
Erick Hitter's avatar
Erick Hitter committed
103
			\WP_CLI::log( sprintf( _n( 'Found %s item', 'Found %s total items', $count, 'automattic-cron-control' ), number_format_i18n( $count ) ) . "\n\n" );
104
			\WP_CLI::confirm( __( 'Proceed?', 'automattic-cron-control' ) );
105
		} else {
106
			\WP_CLI::error( __( 'No entries found...aborting!', 'automattic-cron-control' ) );
107
108
109
110
		}

		// Should we really destroy all this data?
		if ( ! $dry_run ) {
111
			\WP_CLI::log( __( 'This process will remove all CPT data for the Cron Control plugin', 'automattic-cron-control' ) );
112
			\WP_CLI::confirm( __( 'Proceed?', 'automattic-cron-control' ) );
113
			\WP_CLI::log( "\n" . __( 'Starting...', 'automattic-cron-control' ) . "\n" );
114
115
		}

Erick Hitter's avatar
Erick Hitter committed
116
		// Determine how many batches this will take.
117
118
119
120
121
		if ( isset( $assoc_args['batch-size'] ) ) {
			$page_size = max( 1, min( absint( $assoc_args['batch-size'] ), 500 ) );
		} else {
			$page_size = 250;
		}
Erick Hitter's avatar
Erick Hitter committed
122
123

		/* translators: 1: Batch size */
124
		\WP_CLI::log( sprintf( __( 'Processing in batches of %s', 'automattic-cron-control' ), number_format_i18n( $page_size ) ) . "\n\n" );
125
126
127
128
129
130
131
132

		$pages     = 1;
		$page      = 1;

		if ( $count > $page_size ) {
			$pages = ceil( $count / $page_size );
		}

Erick Hitter's avatar
Erick Hitter committed
133
		// Let's get on with it.
134
		do {
Erick Hitter's avatar
Erick Hitter committed
135
			/* translators: 1: Current page, 2: total pages */
136
			\WP_CLI::log( "\n\n" . sprintf( __( 'Processing page %1$s of %2$s', 'automattic-cron-control' ), number_format_i18n( $page ), number_format_i18n( $pages ) ) . "\n" );
137

138
			$items = $wpdb->get_results( $wpdb->prepare( "SELECT ID, post_title FROM {$wpdb->posts} WHERE post_type = %s LIMIT %d,%d", 'a8c_cron_ctrl_event', absint( ( $page - 1 ) * $page_size ),$page_size ) );
139

Erick Hitter's avatar
Erick Hitter committed
140
			// Nothing more to do.
141
			if ( ! is_array( $items ) || empty( $items ) ) {
142
				\WP_CLI::log( __( 'No more items found!', 'automattic-cron-control' ) );
143
144
				break;
			}
145

Erick Hitter's avatar
Erick Hitter committed
146
			/* translators: 1: Event count for this batch */
147
			\WP_CLI::log( sprintf( __( 'Found %s items in this batch' ), number_format_i18n( count( $items ) ) ) );
148

149
			foreach ( $items as $item ) {
150
				\WP_CLI::log( "{$item->ID}, `{$item->post_title}`" );
151
152

				if ( ! $dry_run ) {
153
154
155
156
					wp_delete_post( $item->ID, true );
				}
			}

Erick Hitter's avatar
Erick Hitter committed
157
			// Some cleanup.
158
			unset( $items );
159
			stop_the_insanity();
160

Erick Hitter's avatar
Erick Hitter committed
161
			// Prepare for the next batch.
162
163
164
165
166
167
			$page++;

			if ( $page > $pages ) {
				break;
			}

Erick Hitter's avatar
Erick Hitter committed
168
			// Don't rush into the next batch, unless we haven't done anything.
169
170
171
			if ( ! $dry_run ) {
				sleep( 5 );
			}
Erick Hitter's avatar
Erick Hitter committed
172
		} while ( true );
173

Erick Hitter's avatar
Erick Hitter committed
174
		// Fin.
175
		\WP_CLI::success( __( 'All done.', 'automattic-cron-control' ) );
176
177
178
	}
}

179
\WP_CLI::add_command( 'cron-control-fixers', 'Automattic\WP\Cron_Control\CLI\One_Time_Fixers' );