-
Notifications
You must be signed in to change notification settings - Fork 2
/
wp-cli-cac.php
614 lines (504 loc) · 17.8 KB
/
wp-cli-cac.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
<?php
// Load autoloader if available.
if ( file_exists( __DIR__ . '/vendor/autoload.php' ) ) {
require __DIR__ . '/vendor/autoload.php';
}
// Register our command if WP-CLI is available.
if ( defined( 'WP_CLI' ) ) {
WP_CLI::add_command( 'cac', 'CAC_Command' );
}
class CAC_Command extends WP_CLI_Command {
protected $update_blacklist = array(
'plugin' => array(),
'theme' => array(),
);
/**
* Default blacklist values.
*
* Can be overridden with exclude-plugin and exclude-theme flags.
*/
protected $do_not_update = array(
'plugin' => array(
'accordion-slider-lite', // #16654
'buddypress-group-documents',
'bp-groupblog',
'bp-import-blog-activity',
'cac-group-admin-auto-add',
'dk-pdf', // Not compatible with PHP 7.x
'elementor',
'event-organiser-ical-sync',
'events-manager', // Not compatible with PHP 7.x
'forum-attachments-for-buddypress',
'plugins-list', // not compatible with our PHP.
'post-gallery-widget',
'wordpress-mu-domain-mapping',
'wp-front-end-editor', // we run a fork.
'wp-mailinglist',
'wysija-newsletters',
),
'theme' => array(
'atahualpa',
),
);
/**
* Only perform minor updates on these items.
*/
protected $do_not_update_major = [
'plugin' => [
'events-tickets',
'the-events-calendar',
],
'theme' => [],
];
/**
* Items whose updates should trigger specific notifications.
*/
protected $notify_on_update = [
'plugin' => [
'ml-slider' => 'Apply manual patch to metaslider_plugin_is_installed()',
],
'theme' => [],
];
/**
* Prepare a major update manifest and blog post.
*
* ## OPTIONS
*
* [--version=<version>]
* : The version number of the major release. If not provided, will be
* inferred from CAC_VERSION.
*
* [--date=<date>]
* : The date for the major release. If not provided, will be assumed
* to be the 21st of the current month.
*/
public function prepare_major_update( $args, $assoc_args ) {
$types = array( 'plugin', 'theme' );
$update_data = array(
'header' => '',
'data' => array(),
);
$month = date( 'M' );
$first_release = new DateTime( "second tuesday of $month" );
$second_release = new DateTime( "fourth tuesday of $month" );
// Infer that release date is fourth Tuesday of this month.
if ( ! isset( $assoc_args['date'] ) ) {
$assoc_args['date'] = $second_release->format( 'Y-m-d' );
}
if ( ! isset( $assoc_args['version'] ) ) {
$version = 'x.y.z';
if ( defined( 'CAC_VERSION' ) ) {
if ( preg_match( '/^[0-9]+\.[0-9]+\.([0-9]+)/', CAC_VERSION, $matches ) ) {
$z = $matches[1];
$new_z = $z;
$release_time = strtotime( $assoc_args['date'] );
$dom = date( 'j' );
$year = date( 'Y' );
$month = date( 'm' );
for ( $i = date( 'j' ); $i <= intval( $second_release->format( 'j' ) ); $i++ ) {
$maybe_date = "$year-$month-$i";
if ( $maybe_date === $first_release->format( 'Y-m-d' ) || $maybe_date === $second_release->format( 'Y-m-d' ) ) {
$new_z++;
}
}
$new_z = (string) $new_z;
$cac_v_a = explode( '.', CAC_VERSION );
$cac_v_a[2] = $new_z;
$assoc_args['version'] = implode( '.', $cac_v_a );
}
}
}
WP_CLI::log( sprintf(
'Generating data for CAC %s, scheduled for release on %s. If this is incorrect, please use the --version and --date options to specify a version and date.',
$assoc_args['version'],
$assoc_args['date']
) );
$this->set_up_blacklist( $assoc_args, 'major' );
foreach ( $types as $type ) {
$data = $this->prepare_major_update_for_type( $type );
WP_CLI::log( sprintf( "Identified %s items of type '%s' with major updates available.", count( $data ), $type ) );
$update_data['data'][ $type ] = $data;
}
$json_path = ABSPATH . '.cac-major-update.json';
$update_data['header'] = sprintf( 'CAC major upgrades for %s', $assoc_args['date'] );
file_put_contents( $json_path, json_encode( $update_data, JSON_PRETTY_PRINT ) );
WP_CLI::log( sprintf( 'Saved results to %s.', $json_path ) );
$blog_post = $this->generate_major_update_blog_post( $update_data, $assoc_args );
WP_CLI::log( '' );
WP_CLI::log( "Don't forget a blog post. Title it \"{$blog_post['title']}\". Here's a draft:" );
WP_CLI::log( '' );
WP_CLI::log( '===' );
WP_CLI::log( '' );
WP_CLI::log( $blog_post['text'] );
WP_CLI::log( '' );
WP_CLI::log( '===' );
WP_CLI::log( '' );
WP_CLI::log( 'Don\'t forget to manually check WooThemes for available updates.' );
WP_CLI::log( 'Also, don\'t forget to manually check https://wpcom-themes.svn.automattic.com for updates to "imbalance2" and "manifest".' );
WP_CLI::log( 'Also, don\'t forget to manually check http://themetrust.com for updates to "reveal".' );
WP_CLI::log( 'Also, don\'t forget to manually check https://themify.me/themes/basic for updates to "basic".' );
WP_CLI::log( 'Also, don\'t forget to manually check https://elegantthemes.com for updates to "DailyNotes", "ArtSee", "13Floor", "eNews", "Lucid", "Basic", "Cion", "AskIt".' );
WP_CLI::log( 'Look for Gravity Forms updates' );
WP_CLI::success( 'All done! Be sure to review the release manifest (.cac-major-update.json) before checking into the repo.' );
}
/**
* Perform major updates as previously prepared by prepare_major_release.
*
* ## OPTIONS
*
* [--exclude-plugins=<plugins>]
* : Comma-separated list of plugin slugs to be excluded.
*
* [--exclude-themes=<themes>]
* : Comma-separated list of theme slugs to be excluded.
*/
public function do_major_update( $args, $assoc_args ) {
$json_path = ABSPATH . '.cac-major-update.json';
if ( ! file_exists( $json_path ) ) {
WP_CLI::error( sprintf( 'Could not find a manifest at %s.', $json_path ) );
return;
}
$update_data = json_decode( file_get_contents( $json_path ) );
foreach ( $update_data->data as $type => $items ) {
$this->do_major_update_for_type( $type, $items );
}
WP_CLI::success( 'Major updates completed.' );
}
/**
* Perform minor updates.
*
* ## OPTIONS
*
* [--exclude-plugins=<plugins>]
* : Comma-separated list of plugin slugs to be excluded.
*
* [--exclude-themes=<themes>]
* : Comma-separated list of theme slugs to be excluded.
*/
public function do_minor_update( $args, $assoc_args ) {
$this->maybe_register_gh_command();
$types = array( 'plugin', 'theme' );
$update_data = array(
'header' => '',
'data' => array(),
);
$this->set_up_blacklist( $assoc_args, 'minor' );
foreach ( $types as $type ) {
$items = $this->get_available_updates_for_type( $type );
if ( empty( $items ) ) {
continue;
}
foreach ( $items as $item_data ) {
// Ignore items from blacklist.
if ( in_array( $item_data['name'], $this->update_blacklist[ $type ] ) ) {
continue;
}
$new_version = $item_data['update_version'];
$old_version = $item_data['version'];
$version_compare = $this->version_compare( $new_version, $old_version );
// Skip major updates.
if ( $version_compare['is_major_update'] ) {
continue;
}
$args = array( 'gh', $type, 'update', $item_data['name'] );
// Override locale so we can skip translation updates.
add_filter( 'locale', array( $this, 'set_locale' ) );
WP_CLI::run_command( $args, array() );
remove_filter( 'locale', array( $this, 'set_locale' ) );
}
}
}
/**
* Fetch a formatted list of items with available updates.
*
* @param string $type Item type. 'plugin' or 'theme'.
* @return array
*/
protected function get_available_updates_for_type( $type ) {
$command = "$type list";
$assoc_args = '--update=available --format=csv --fields=name,title,update_version,version';
$results = WP_CLI::runcommand( $command . ' ' . $assoc_args, array(
'return' => true
) );
/*
* No results, so bail!
*
* Here, we're checking if there is a line return. If there isn't, then we
* only have the title row, which means no results.
*/
if ( false === strpos( $results, "\n" ) ) {
return false;
}
$raw_items = explode( "\n", trim( $results ) );
$items = array();
foreach ( $raw_items as $i => $raw_item ) {
// Discard title row.
if ( 0 === $i ) {
continue;
}
$item_data = explode( ',', $raw_item );
$items[ $item_data[0] ] = array(
'name' => $item_data[0],
// Titles have been csv-encoded, so strip the quotes.
'title' => preg_replace( '/^"?([^"]+)"?$/', '\1', $item_data[1] ),
'update_version' => $item_data[2],
'version' => $item_data[3],
);
}
return $items;
}
/**
* Compare version numbers and determine whether it's a major update + the whitelisted update series.
*
* @param string $new_version
* @param string $old_version
* @return array
*/
protected function version_compare( $new_version, $old_version ) {
// "Major" means that either x or y is different. Blargh.
$new_version_a = explode( '.', $new_version );
$old_version_a = explode( '.', $old_version );
$is_major_update = false;
$update_series = array();
for ( $i = 0; $i <= 1; $i++ ) {
$new_version_place = isset( $new_version_a[ $i ] ) ? intval( $new_version_a[ $i ] ) : 0;
$old_version_place = isset( $old_version_a[ $i ] ) ? intval( $old_version_a[ $i ] ) : 0;
$update_series[] = $new_version_place;
if ( $new_version_place != $old_version_place ) {
$is_major_update = true;
}
}
return array(
'is_major_update' => $is_major_update,
'update_series' => implode( '.', $update_series ),
);
}
/**
* Prepare major update data for an item type.
*
* @param string $type Item type. 'plugin' or 'theme'.
* @return array
*/
protected function prepare_major_update_for_type( $type ) {
if ( 'theme' !== $type ) {
$type = 'plugin';
}
$items = $this->get_available_updates_for_type( $type );
if ( false === $items ) {
WP_CLI::error( $results->stderr );
return;
}
$updates = array();
foreach ( $items as $item_data ) {
// Ignore items from blacklist.
if ( in_array( $item_data['name'], $this->update_blacklist[ $type ] ) ) {
continue;
}
$new_version = $item_data['update_version'];
$old_version = $item_data['version'];
$version_compare = $this->version_compare( $new_version, $old_version );
// Not a major update.
if ( ! $version_compare['is_major_update'] ) {
continue;
}
$item_update = array(
'name' => $item_data['name'],
'title' => $item_data['title'],
'update_series' => $version_compare['update_series'],
);
$updates[ $item_data['name'] ] = $item_update;
}
return $updates;
}
protected function generate_major_update_blog_post( $update_data, $assoc_args ) {
$update_strings = array();
foreach ( $update_data['data'] as $type => $type_data ) {
$update_strings[ $type ] = array();
foreach ( $type_data as $item ) {
$update_strings[ $type ][] = sprintf(
'<li>%s (%s)</li>',
$item['title'],
$item['update_series']
);
sort( $update_strings[ $type ] );
}
}
$text = '';
$pretty_date = date( 'F j, Y', strtotime( $assoc_args['date'] ) );
if ( ! empty( $update_strings['plugin'] ) ) {
$text .= sprintf(
"<p>The following plugins will receive major updates as part of the %s release of the CUNY Academic Commons, scheduled for %s:</p>\n<ul>%s</ul>",
$assoc_args['version'],
$pretty_date,
implode( "\n", $update_strings['plugin'] )
);
}
if ( ! empty( $update_strings['theme'] ) ) {
$text .= sprintf(
"\n\n<p>The following theme will receive major updates as part of the %s release of the CUNY Academic Commons, scheduled for %s:</p>\n<ul>%s</ul>",
$assoc_args['version'],
$pretty_date,
implode( "\n", $update_strings['theme'] )
);
}
$text .= "\n\n" . '<p>For more details on major update releases, please visit <a href="http://dev.commons.gc.cuny.edu/release-schedule-and-procedures/">our release schedule and procedures page</a>.</p>.';
$title = sprintf( 'Major plugin and theme updates for %s', $pretty_date );
return array(
'title' => $title,
'text' => $text,
);
}
protected function do_major_update_for_type( $type, $items ) {
$this->maybe_register_gh_command();
// Get a list of available updates. If whitelisted series matches, no need to check svn.
$available_updates = $this->get_available_updates_for_type( $type );
$updates = [];
$notify = [];
foreach ( $items as $item ) {
if ( ! isset( $available_updates[ $item->name ] ) ) {
continue;
}
$available_version = $available_updates[ $item->name ]['update_version'];
$version_compare = $this->version_compare( $available_version, $item->update_series );
if ( ! $version_compare['is_major_update'] ) {
$updates[ $item->name ] = 'latest';
continue;
}
// There's a mismatch, so we have to scrape wordpress.org for versions. Whee!
// @todo Get someone to implement this in the API.
// Used to use `svn_ls()` for this, but PECL broke for me. Let the fun begin.
$url = "http://{$type}s.svn.wordpress.org/{$item->name}/tags/";
$f = wp_remote_get( $url );
$body = wp_remote_retrieve_body( $f );
$dom = new DomDocument();
$dom->loadHTML( $body );
$tags = $dom->getElementsByTagName( 'li' );
$versions = array();
foreach ( $tags as $tag ) {
$versions[] = rtrim( $tag->nodeValue, '/' );
}
// If a plugin has been closed or whatever.
if ( ! $versions ) {
continue;
}
rsort( $versions );
foreach ( $versions as $v ) {
$v_version_compare = $this->version_compare( $v, $item->update_series );
if ( ! $v_version_compare['is_major_update'] ) {
$updates[ $item->name ] = $v;
break;
}
}
// If this needs a notification.
if ( isset( $this->notify_on_update[ $type ][ $item->name ] ) ) {
$notify[] = $item->name;
}
}
foreach ( $updates as $plugin_name => $update_version ) {
$args = array( 'gh', $type, 'update', $plugin_name );
$assoc_args = array();
if ( 'latest' !== $update_version ) {
$assoc_args['version'] = $update_version;
}
// Override locale so we can skip translation updates.
add_filter( 'locale', array( $this, 'set_locale' ) );
WP_CLI::run_command( $args, $assoc_args );
remove_filter( 'locale', array( $this, 'set_locale' ) );
}
foreach ( $notify as $notify_item => $notify_action ) {
WP_CLI::warning( sprintf( 'The following %s has been updated and needs attention: %s. Action: %s', $type, $notify_item, $notify_action ) );
}
}
public function set_locale( $locale ) {
return 'en_US';
}
/**
* Change a site's domain.
*
* ## OPTIONS
*
* --from=<from>
* : The current domain of the site being changed.
*
* --to=<date>
* : The domain that the site is being changed to.
*
* [--dry-run]
* : Whether this should be a dry run.
*/
public function change_domain( $args, $assoc_args ) {
global $wpdb;
if ( empty( $assoc_args['from'] ) || empty( $assoc_args['to'] ) ) {
WP_CLI::error( "The 'from' and 'to' parameters are required." );
return;
}
$from_domain = $assoc_args['from'];
$to_domain = $assoc_args['to'];
$from_site = get_site_by_path( $from_domain, '/' );
if ( ! $from_site ) {
WP_CLI::error( sprintf( 'No site with the domain %s was found. Aborting.', $from_domain ) );
return;
}
$to_site = get_site_by_path( $to_domain, '/' );
if ( $to_site ) {
WP_CLI::error( sprintf( 'An existing site was found with the domain %s. Aborting.', $to_domain ) );
}
// Blog-specific tables first.
$base_args = array( 'search-replace', $from_domain, $to_domain );
$base_assoc_args = array( 'skip-columns' => 'guid', 'precise' => 1, 'all-tables' => 1 );
if ( isset( $assoc_args['dry-run'] ) ) {
$base_assoc_args['dry-run'] = 1;
}
$blog_tables = $wpdb->get_col( "SHOW TABLES LIKE '" . like_escape( $wpdb->get_blog_prefix( $from_site->blog_id ) ) . "%'" );
$_args = array_merge( $base_args, $blog_tables );
$_assoc_args = $base_assoc_args;
WP_CLI::run_command( $_args, $_assoc_args );
// Global tables next.
$global_tables = array_merge( $wpdb->global_tables, $wpdb->ms_global_tables );
foreach ( $global_tables as &$global_table ) {
$global_table = $wpdb->base_prefix . $global_table;
}
if ( function_exists( 'buddypress' ) ) {
$bp_prefix = bp_core_get_table_prefix() . 'bp_';
$bp_prefix = esc_sql( $bp_prefix ); // just in case....
$bp_tables = $wpdb->get_col( $wpdb->prepare( 'SHOW TABLES LIKE %s%', $bp_prefix ) );
if ( $bp_tables ) {
$global_tables = array_merge( $global_tables, $bp_tables );
}
}
$_args = array_merge( $base_args, $global_tables );
$_assoc_args = $base_assoc_args;
WP_CLI::run_command( $_args, $_assoc_args );
WP_CLI::success( 'Domains switched!' );
WP_CLI::error( 'wp-cli cannot flush site caches, so make sure to do it yourself!' );
}
/**
* Set up the update blacklist, based on arguments passed to the command.
*
* @param array $assoc_args Associative argument array.
* @param string $type Type of update. 'major' or 'minor'.
*/
protected function set_up_blacklist( $assoc_args, $type ) {
if ( isset( $assoc_args['exclude-plugins'] ) ) {
$this->update_blacklist['plugin'] = array_filter( explode( ',', $assoc_args['exclude-plugins'] ) );
} else {
$this->update_blacklist['plugin'] = $this->do_not_update['plugin'];
}
if ( isset( $assoc_args['exclude-themes'] ) ) {
$this->update_blacklist['theme'] = array_filter( explode( ',', $assoc_args['exclude-themes'] ) );
} else {
$this->update_blacklist['theme'] = $this->do_not_update['theme'];
}
if ( 'major' === $type ) {
$this->update_blacklist['plugin'] = array_merge( $this->update_blacklist['plugin'], $this->do_not_update_major['plugin'] );
$this->update_blacklist['theme'] = array_merge( $this->update_blacklist['theme'], $this->do_not_update_major['theme'] );
}
}
protected function maybe_register_gh_command() {
$root = WP_CLI::get_root_command();
$commands = $root->get_subcommands();
if ( ! isset( $commands['gh'] ) ) {
WP_CLI::add_command( 'gh', '\boonebgorges\WPCLIGitHelper\Command' );
}
}
}