-
Notifications
You must be signed in to change notification settings - Fork 94
Expand file tree
/
Copy pathSite_Command.php
More file actions
688 lines (612 loc) · 18.6 KB
/
Site_Command.php
File metadata and controls
688 lines (612 loc) · 18.6 KB
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
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
<?php
/**
* Perform site-wide operations.
*
* ## EXAMPLES
*
* # Create site
* $ wp site create --slug=example
* Success: Site 3 created: www.example.com/example/
*
* # Output a simple list of site URLs
* $ wp site list --field=url
* http://www.example.com/
* http://www.example.com/subdir/
*
* # Delete site
* $ wp site delete 123
* Are you sure you want to delete the 'http://www.example.com/example' site? [y/n] y
* Success: The site at 'http://www.example.com/example' was deleted.
*
* @package wp-cli
*/
class Site_Command extends \WP_CLI\CommandWithDBObject {
protected $obj_type = 'site';
protected $obj_id_key = 'blog_id';
public function __construct() {
$this->fetcher = new \WP_CLI\Fetchers\Site;
}
/**
* Delete comments.
*/
private function _empty_comments() {
global $wpdb;
// Empty comments and comment cache
$comment_ids = $wpdb->get_col( "SELECT comment_ID FROM $wpdb->comments" );
foreach ( $comment_ids as $comment_id ) {
wp_cache_delete( $comment_id, 'comment' );
wp_cache_delete( $comment_id, 'comment_meta' );
}
$wpdb->query( "TRUNCATE $wpdb->comments" );
$wpdb->query( "TRUNCATE $wpdb->commentmeta" );
}
/**
* Delete all posts.
*/
private function _empty_posts() {
global $wpdb;
// Empty posts and post cache
$posts_query = "SELECT ID FROM $wpdb->posts";
$posts = new WP_CLI\Iterators\Query( $posts_query, 10000 );
$taxonomies = get_taxonomies();
while ( $posts->valid() ) {
$post_id = $posts->current()->ID;
wp_cache_delete( $post_id, 'posts' );
wp_cache_delete( $post_id, 'post_meta' );
foreach ( $taxonomies as $taxonomy )
wp_cache_delete( $post_id, "{$taxonomy}_relationships" );
wp_cache_delete( $wpdb->blogid . '-' . $post_id, 'global-posts' );
$posts->next();
}
$wpdb->query( "TRUNCATE $wpdb->posts" );
$wpdb->query( "TRUNCATE $wpdb->postmeta" );
}
/**
* Delete terms, taxonomies, and tax relationships.
*/
private function _empty_taxonomies() {
global $wpdb;
// Empty taxonomies and terms
$terms = $wpdb->get_results( "SELECT term_id, taxonomy FROM $wpdb->term_taxonomy" );
$ids = array();
$taxonomies = array();
foreach ( (array) $terms as $term ) {
$taxonomies[] = $term->taxonomy;
$ids[] = $term->term_id;
wp_cache_delete( $term->term_id, $term->taxonomy );
}
$taxonomies = array_unique( $taxonomies );
$cleaned = array();
foreach ( $taxonomies as $taxonomy ) {
if ( isset( $cleaned[$taxonomy] ) )
continue;
$cleaned[$taxonomy] = true;
wp_cache_delete( 'all_ids', $taxonomy );
wp_cache_delete( 'get', $taxonomy );
delete_option( "{$taxonomy}_children" );
}
$wpdb->query( "TRUNCATE $wpdb->terms" );
$wpdb->query( "TRUNCATE $wpdb->term_taxonomy" );
$wpdb->query( "TRUNCATE $wpdb->term_relationships" );
if ( ! empty( $wpdb->termmeta ) ) {
$wpdb->query( "TRUNCATE $wpdb->termmeta" );
}
}
/**
* Insert default terms.
*/
private function _insert_default_terms() {
global $wpdb;
// Default category
$cat_name = __( 'Uncategorized' );
/* translators: Default category slug */
$cat_slug = sanitize_title( _x( 'Uncategorized', 'Default category slug' ) );
if ( global_terms_enabled() ) {
$cat_id = $wpdb->get_var( $wpdb->prepare( "SELECT cat_ID FROM {$wpdb->sitecategories} WHERE category_nicename = %s", $cat_slug ) );
if ( $cat_id == null ) {
$wpdb->insert( $wpdb->sitecategories, array('cat_ID' => 0, 'cat_name' => $cat_name, 'category_nicename' => $cat_slug, 'last_updated' => current_time('mysql', true)) );
$cat_id = $wpdb->insert_id;
}
update_option('default_category', $cat_id);
} else {
$cat_id = 1;
}
$wpdb->insert( $wpdb->terms, array('term_id' => $cat_id, 'name' => $cat_name, 'slug' => $cat_slug, 'term_group' => 0) );
$wpdb->insert( $wpdb->term_taxonomy, array('term_id' => $cat_id, 'taxonomy' => 'category', 'description' => '', 'parent' => 0, 'count' => 1));
}
/**
* Empty a site of its content (posts, comments, terms, and meta).
*
* Truncates posts, comments, and terms tables to empty a site of its
* content. Doesn't affect site configuration (options) or users.
*
* If running a persistent object cache, make sure to flush the cache
* after emptying the site, as the cache values will be invalid otherwise.
*
* To also empty custom database tables, you'll need to hook into command
* execution:
*
* ```
* WP_CLI::add_hook( 'after_invoke:site empty', function(){
* global $wpdb;
* foreach( array( 'p2p', 'p2pmeta' ) as $table ) {
* $table = $wpdb->$table;
* $wpdb->query( "TRUNCATE $table" );
* }
* });
* ```
*
* ## OPTIONS
*
* [--uploads]
* : Also delete *all* files in the site's in the uploads directory.
*
* [--yes]
* : Proceed to empty the site without a confirmation prompt.
*
* ## EXAMPLES
*
* $ wp site empty
* Are you sure you want to empty the site at http://www.example.com of all posts, comments, and terms? [y/n] y
* Success: The site at 'http://www.example.com' was emptied.
*
* @subcommand empty
*/
public function _empty( $args, $assoc_args ) {
$upload_message = '';
if ( \WP_CLI\Utils\get_flag_value( $assoc_args, 'uploads' ) ) {
$upload_message = ', and delete its uploads directory';
}
WP_CLI::confirm( "Are you sure you want to empty the site at '" . site_url() . "' of all posts, comments, and terms" . $upload_message . "?", $assoc_args );
$this->_empty_posts();
$this->_empty_comments();
$this->_empty_taxonomies();
$this->_insert_default_terms();
if ( ! empty( $upload_message ) ) {
$upload_dir = wp_upload_dir();
$files = new RecursiveIteratorIterator(
new RecursiveDirectoryIterator( $upload_dir['basedir'], RecursiveDirectoryIterator::SKIP_DOTS ),
RecursiveIteratorIterator::CHILD_FIRST
);
$files_to_unlink = $directories_to_delete = array();
foreach ( $files as $fileinfo ) {
$realpath = $fileinfo->getRealPath();
// Don't clobber subsites when operating on the main site
if ( is_main_site() && false !== stripos( $realpath, '/sites/' ) ) {
continue;
}
if ( $fileinfo->isDir() ) {
$directories_to_delete[] = $realpath;
} else {
$files_to_unlink[] = $realpath;
}
}
foreach( $files_to_unlink as $file ) {
unlink( $file );
}
foreach( $directories_to_delete as $directory ) {
rmdir( $directory );
}
rmdir( $upload_dir['basedir'] );
}
WP_CLI::success( "The site at '" . site_url() . "' was emptied." );
}
/**
* Delete a site in a multisite install.
*
* ## OPTIONS
*
* [<site-id>]
* : The id of the site to delete. If not provided, you must set the --slug parameter.
*
* [--slug=<slug>]
* : Path of the blog to be deleted. Subdomain on subdomain installs, directory on subdirectory installs.
*
* [--yes]
* : Answer yes to the confirmation message.
*
* [--keep-tables]
* : Delete the blog from the list, but don't drop it's tables.
*
* ## EXAMPLES
*
* $ wp site delete 123
* Are you sure you want to delete the http://www.example.com/example site? [y/n] y
* Success: The site at 'http://www.example.com/example' was deleted.
*/
function delete( $args, $assoc_args ) {
if ( !is_multisite() ) {
WP_CLI::error( 'This is not a multisite install.' );
}
if ( isset( $assoc_args['slug'] ) ) {
$blog = get_blog_details( trim( $assoc_args['slug'], '/' ) );
} else {
if ( empty( $args ) ) {
WP_CLI::error( "Need to specify a blog id." );
}
$blog_id = $args[0];
$blog = get_blog_details( $blog_id );
}
if ( !$blog ) {
WP_CLI::error( "Site not found." );
}
$site_url = trailingslashit( $blog->siteurl );
WP_CLI::confirm( "Are you sure you want to delete the '$site_url' site?", $assoc_args );
wpmu_delete_blog( $blog->blog_id, ! \WP_CLI\Utils\get_flag_value( $assoc_args, 'keep-tables' ) );
WP_CLI::success( "The site at '$site_url' was deleted." );
}
/**
* Create a site in a multisite install.
*
* ## OPTIONS
*
* --slug=<slug>
* : Path for the new site. Subdomain on subdomain installs, directory on subdirectory installs.
*
* [--title=<title>]
* : Title of the new site. Default: prettified slug.
*
* [--email=<email>]
* : Email for Admin user. User will be created if none exists. Assignement to Super Admin if not included.
*
* [--network_id=<network-id>]
* : Network to associate new site with. Defaults to current network (typically 1).
*
* [--private]
* : If set, the new site will be non-public (not indexed)
*
* [--porcelain]
* : If set, only the site id will be output on success.
*
* ## EXAMPLES
*
* $ wp site create --slug=example
* Success: Site 3 created: http://www.example.com/example/
*/
public function create( $_, $assoc_args ) {
if ( !is_multisite() ) {
WP_CLI::error( 'This is not a multisite install.' );
}
global $wpdb, $current_site;
$base = $assoc_args['slug'];
$title = \WP_CLI\Utils\get_flag_value( $assoc_args, 'title', ucfirst( $base ) );
$email = empty( $assoc_args['email'] ) ? '' : $assoc_args['email'];
// Network
if ( !empty( $assoc_args['network_id'] ) ) {
$network = $this->_get_network( $assoc_args['network_id'] );
if ( $network === false ) {
WP_CLI::error( sprintf( 'Network with id %d does not exist.', $assoc_args['network_id'] ) );
}
}
else {
$network = $current_site;
}
$public = ! \WP_CLI\Utils\get_flag_value( $assoc_args, 'private' );
// Sanitize
if ( preg_match( '|^([a-zA-Z0-9-])+$|', $base ) ) {
$base = strtolower( $base );
}
// If not a subdomain install, make sure the domain isn't a reserved word
if ( !is_subdomain_install() ) {
$subdirectory_reserved_names = apply_filters( 'subdirectory_reserved_names', array( 'page', 'comments', 'blog', 'files', 'feed' ) );
if ( in_array( $base, $subdirectory_reserved_names ) ) {
WP_CLI::error( 'The following words are reserved and cannot be used as blog names: ' . implode( ', ', $subdirectory_reserved_names ) );
}
}
// Check for valid email, if not, use the first Super Admin found
// Probably a more efficient way to do this so we dont query for the
// User twice if super admin
$email = sanitize_email( $email );
if ( empty( $email ) || !is_email( $email ) ) {
$super_admins = get_super_admins();
$email = '';
if ( !empty( $super_admins ) && is_array( $super_admins ) ) {
// Just get the first one
$super_login = $super_admins[0];
$super_user = get_user_by( 'login', $super_login );
if ( $super_user ) {
$email = $super_user->user_email;
}
}
}
if ( is_subdomain_install() ) {
$newdomain = $base . '.' . preg_replace( '|^www\.|', '', $current_site->domain );
$path = $current_site->path;
$url = $newdomain;
} else {
$newdomain = $current_site->domain;
$path = $current_site->path . $base . '/';
$url = $newdomain . $path;
}
$user_id = email_exists( $email );
if ( !$user_id ) { // Create a new user with a random password
$password = wp_generate_password( 12, false );
$user_id = wpmu_create_user( $base, $password, $email );
if ( false == $user_id ) {
WP_CLI::error( "Can't create user." );
}
else {
wp_new_user_notification( $user_id, $password );
}
}
$wpdb->hide_errors();
$title = wp_slash( $title );
$id = wpmu_create_blog( $newdomain, $path, $title, $user_id, array( 'public' => $public ), $network->id );
$wpdb->show_errors();
if ( !is_wp_error( $id ) ) {
if ( !is_super_admin( $user_id ) && !get_user_option( 'primary_blog', $user_id ) ) {
update_user_option( $user_id, 'primary_blog', $id, true );
}
// Prevent mailing admins of new sites
// @TODO argument to pass in?
// $content_mail = sprintf(__( "New site created by WP Command Line Interface\n\nAddress: %2s\nName: %3s"), get_site_url($id), stripslashes($title));
// wp_mail(get_site_option('admin_email'), sprintf(__('[%s] New Site Created'), $current_site->site_name), $content_mail, 'From: "Site Admin" <'.get_site_option( 'admin_email').'>');
}
else {
WP_CLI::error( $id->get_error_message() );
}
if ( \WP_CLI\Utils\get_flag_value( $assoc_args, 'porcelain' ) ) {
WP_CLI::line( $id );
} else {
$site_url = trailingslashit( get_site_url( $id ) );
WP_CLI::success( "Site $id created: $site_url" );
}
}
/**
* Get network data for a given id.
*
* @param int $network_id
* @return bool|array False if no network found with given id, array otherwise
*/
private function _get_network( $network_id ) {
global $wpdb;
// Load network data
$networks = $wpdb->get_results( $wpdb->prepare(
"SELECT * FROM $wpdb->site WHERE id = %d", $network_id ) );
if ( !empty( $networks ) ) {
// Only care about domain and path which are set here
return $networks[0];
}
return false;
}
/**
* List all sites in a multisite install.
*
* ## OPTIONS
*
* [--network=<id>]
* : The network to which the sites belong.
*
* [--<field>=<value>]
* : Filter by one or more fields (see "Available Fields" section). However,
* 'url' isn't an available filter, because it's created from domain + path.
*
* [--site__in=<value>]
* : Only list the sites with these blog_id values (comma-separated).
*
* [--field=<field>]
* : Prints the value of a single field for each site.
*
* [--fields=<fields>]
* : Comma-separated list of fields to show.
*
* [--format=<format>]
* : Render output in a particular format.
* ---
* default: table
* options:
* - table
* - csv
* - count
* - ids
* - json
* - yaml
* ---
*
* ## AVAILABLE FIELDS
*
* These fields will be displayed by default for each site:
*
* * blog_id
* * url
* * last_updated
* * registered
*
* These fields are optionally available:
*
* * site_id
* * domain
* * path
* * public
* * archived
* * mature
* * spam
* * deleted
* * lang_id
*
* ## EXAMPLES
*
* # Output a simple list of site URLs
* $ wp site list --field=url
* http://www.example.com/
* http://www.example.com/subdir/
*
* @subcommand list
*/
public function list_( $_, $assoc_args ) {
if ( !is_multisite() ) {
WP_CLI::error( 'This is not a multisite install.' );
}
global $wpdb;
if ( isset( $assoc_args['fields'] ) ) {
$assoc_args['fields'] = preg_split( '/,[ \t]*/', $assoc_args['fields'] );
}
$defaults = array(
'format' => 'table',
'fields' => array( 'blog_id', 'url', 'last_updated', 'registered' ),
);
$assoc_args = array_merge( $defaults, $assoc_args );
$where = array();
$append = '';
$site_cols = array( 'blog_id', 'last_updated', 'registered', 'site_id', 'domain', 'path', 'public', 'archived', 'mature', 'spam', 'deleted', 'lang_id' );
foreach( $site_cols as $col ) {
if ( isset( $assoc_args[ $col ] ) ) {
$where[ $col ] = $assoc_args[ $col ];
}
}
if ( isset( $assoc_args['site__in'] ) ) {
$where['blog_id'] = explode( ',', $assoc_args['site__in'] );
$append = "ORDER BY FIELD( blog_id, " . implode( ',', array_map( 'intval', $where['blog_id'] ) ) . " )";
}
if ( isset( $assoc_args['network'] ) ) {
$where['site_id'] = $assoc_args['network'];
}
$iterator_args = array(
'table' => $wpdb->blogs,
'where' => $where,
'append' => $append,
);
$it = new \WP_CLI\Iterators\Table( $iterator_args );
$it = \WP_CLI\Utils\iterator_map( $it, function( $blog ) {
$blog->url = trailingslashit( get_site_url( $blog->blog_id ) );
return $blog;
} );
if ( ! empty( $assoc_args['format'] ) && 'ids' === $assoc_args['format'] ) {
$sites = iterator_to_array( $it );
$ids = wp_list_pluck( $sites, 'blog_id' );
$formatter = new \WP_CLI\Formatter( $assoc_args, null, 'site' );
$formatter->display_items( $ids );
}
else {
$formatter = new \WP_CLI\Formatter( $assoc_args, null, 'site' );
$formatter->display_items( $it );
}
}
/**
* Archive one or more sites.
*
* ## OPTIONS
*
* <id>...
* : One or more IDs of sites to archive.
*
* ## EXAMPLES
*
* $ wp site archive 123
* Success: Site 123 archived.
*/
public function archive( $args ) {
$this->update_site_status( $args, 'archived', 1 );
}
/**
* Unarchive one or more sites.
*
* ## OPTIONS
*
* <id>...
* : One or more IDs of sites to unarchive.
*
* ## EXAMPLES
*
* $ wp site unarchive 123
* Success: Site 123 unarchived.
*/
public function unarchive( $args ) {
$this->update_site_status( $args, 'archived', 0 );
}
/**
* Activate one or more sites.
*
* ## OPTIONS
*
* <id>...
* : One or more IDs of sites to activate.
*
* ## EXAMPLES
*
* $ wp site activate 123
* Success: Site 123 activated.
*/
public function activate( $args ) {
$this->update_site_status( $args, 'deleted', 0 );
}
/**
* Deactivate one or more sites.
*
* ## OPTIONS
*
* <id>...
* : One or more IDs of sites to deactivate.
*
* ## EXAMPLES
*
* $ wp site deactivate 123
* Success: Site 123 deactivated.
*/
public function deactivate( $args ) {
$this->update_site_status( $args, 'deleted', 1 );
}
/**
* Mark one or more sites as spam.
*
* ## OPTIONS
*
* <id>...
* : One or more IDs of sites to be marked as spam.
*
* ## EXAMPLES
*
* $ wp site spam 123
* Success: Site 123 marked as spam.
*/
public function spam( $args ) {
$this->update_site_status( $args, 'spam', 1 );
}
/**
* Remove one or more sites from spam.
*
* ## OPTIONS
*
* <id>...
* : One or more IDs of sites to remove from spam.
*
* ## EXAMPLES
*
* $ wp site unspam 123
* Success: Site 123 removed from spam.
*
* @subcommand unspam
*/
public function unspam( $args ) {
$this->update_site_status( $args, 'spam', 0 );
}
private function update_site_status( $ids, $pref, $value ) {
if ( $pref == 'archived' && $value == 1 ) {
$action = 'archived';
} else if ( $pref == 'archived' && $value == 0) {
$action = 'unarchived';
} else if ( $pref == 'deleted' && $value == 1 ) {
$action = 'deactivated';
} else if ( $pref == 'deleted' && $value == 0 ) {
$action = 'activated';
} else if ( $pref == 'spam' && $value == 1 ) {
$action = 'marked as spam';
} else if ( $pref == 'spam' && $value == 0 ) {
$action = 'removed from spam';
}
foreach ( $ids as $site_id ) {
$site = $this->fetcher->get_check( $site_id );
if ( is_main_site( $site->blog_id ) ) {
WP_CLI::warning( "You are not allowed to change the main site." );
continue;
}
$old_value = get_blog_status( $site->blog_id, $pref );
if ( $value == $old_value ) {
WP_CLI::warning( "Site {$site->blog_id} already {$action}." );
continue;
}
update_blog_status( $site->blog_id, $pref, $value );
WP_CLI::success( "Site {$site->blog_id} {$action}." );
}
}
}