forked from wp-cli/extension-command
/
Theme_Command.php
873 lines (791 loc) · 24.5 KB
/
Theme_Command.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
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
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
<?php
use WP_CLI\Utils;
/**
* Manages themes, including installs, activations, and updates.
*
* See the WordPress [Theme Handbook](https://developer.wordpress.org/themes/) developer resource for more information on themes.
*
* ## EXAMPLES
*
* # Install the latest version of a theme from wordpress.org and activate
* $ wp theme install twentysixteen --activate
* Installing Twenty Sixteen (1.2)
* Downloading install package from http://downloads.wordpress.org/theme/twentysixteen.1.2.zip...
* Unpacking the package...
* Installing the theme...
* Theme installed successfully.
* Activating 'twentysixteen'...
* Success: Switched to 'Twenty Sixteen' theme.
*
* # Get details of an installed theme
* $ wp theme get twentysixteen --fields=name,title,version
* +---------+----------------+
* | Field | Value |
* +---------+----------------+
* | name | Twenty Sixteen |
* | title | Twenty Sixteen |
* | version | 1.2 |
* +---------+----------------+
*
* # Get status of theme
* $ wp theme status twentysixteen
* Theme twentysixteen details:
* Name: Twenty Sixteen
* Status: Active
* Version: 1.2
* Author: the WordPress team
*
* @package wp-cli
*/
class Theme_Command extends \WP_CLI\CommandWithUpgrade {
protected $item_type = 'theme';
protected $upgrade_refresh = 'wp_update_themes';
protected $upgrade_transient = 'update_themes';
protected $obj_fields = array(
'name',
'status',
'update',
'version'
);
public function __construct() {
if ( is_multisite() ) {
$this->obj_fields[] = 'enabled';
}
parent::__construct();
$this->fetcher = new \WP_CLI\Fetchers\Theme;
}
protected function get_upgrader_class( $force ) {
return $force ? '\\WP_CLI\\DestructiveThemeUpgrader' : 'Theme_Upgrader';
}
/**
* Reveals the status of one or all themes.
*
* ## OPTIONS
*
* [<theme>]
* : A particular theme to show the status for.
*
* ## EXAMPLES
*
* $ wp theme status twentysixteen
* Theme twentysixteen details:
* Name: Twenty Sixteen
* Status: Inactive
* Version: 1.2
* Author: the WordPress team
*/
public function status( $args ) {
if ( isset( $args[0] ) ) {
$theme = $this->fetcher->get_check( $args[0] );
$errors = $theme->errors();
if ( is_wp_error( $errors ) ) {
$message = $errors->get_error_message();
WP_CLI::error( $message );
}
}
parent::status( $args );
}
/**
* Searches the WordPress.org theme directory.
*
* Displays themes in the WordPress.org theme directory matching a given
* search query.
*
* ## OPTIONS
*
* <search>
* : The string to search for.
*
* [--per-page=<per-page>]
* : Optional number of results to display. Defaults to 10.
*
* [--field=<field>]
* : Prints the value of a single field for each theme.
*
* [--fields=<fields>]
* : Ask for specific fields from the API. Defaults to name,slug,author,rating. Acceptable values:
*
* **name**: Theme Name
* **slug**: Theme Slug
* **version**: Current Version Number
* **author**: Theme Author
* **preview_url**: Theme Preview URL
* **screenshot_url**: Theme Screenshot URL
* **rating**: Theme Rating
* **num_ratings**: Number of Theme Ratings
* **homepage**: Theme Author's Homepage
* **description**: Theme Description
* **url**: Theme's URL on wordpress.org
*
* [--format=<format>]
* : Render output in a particular format.
* ---
* default: table
* options:
* - table
* - csv
* - json
* - count
* - yaml
* ---
*
* ## EXAMPLES
*
* $ wp theme search photo --per-page=6
* Success: Showing 6 of 203 themes.
* +----------------------+----------------------+--------+
* | name | slug | rating |
* +----------------------+----------------------+--------+
* | Photos | photos | 100 |
* | Infinite Photography | infinite-photography | 100 |
* | PhotoBook | photobook | 100 |
* | BG Photo Frame | bg-photo-frame | 0 |
* | fPhotography | fphotography | 0 |
* | Photo Perfect | photo-perfect | 98 |
* +----------------------+----------------------+--------+
*/
public function search( $args, $assoc_args ) {
parent::_search( $args, $assoc_args );
}
protected function status_single( $args ) {
$theme = $this->fetcher->get_check( $args[0] );
$status = $this->format_status( $this->get_status( $theme ), 'long' );
$version = $theme->get('Version');
if ( $this->has_update( $theme->get_stylesheet() ) )
$version .= ' (%gUpdate available%n)';
echo WP_CLI::colorize( \WP_CLI\Utils\mustache_render( 'theme-status.mustache', array(
'slug' => $theme->get_stylesheet(),
'status' => $status,
'version' => $version,
'name' => $theme->get('Name'),
'author' => $theme->get('Author'),
) ) );
}
protected function get_all_items() {
return $this->get_item_list();
}
protected function get_status( $theme ) {
if ( $this->is_active_theme( $theme ) ) {
return 'active';
} else if ( $theme->get_stylesheet_directory() === get_template_directory() ) {
return 'parent';
} else {
return 'inactive';
}
}
/**
* Activates a theme.
*
* ## OPTIONS
*
* <theme>
* : The theme to activate.
*
* ## EXAMPLES
*
* $ wp theme activate twentysixteen
* Success: Switched to 'Twenty Sixteen' theme.
*/
public function activate( $args = array() ) {
$theme = $this->fetcher->get_check( $args[0] );
$errors = $theme->errors();
if ( is_wp_error( $errors ) ) {
$message = $errors->get_error_message();
WP_CLI::error( $message );
}
$name = $theme->get('Name');
if ( 'active' === $this->get_status( $theme ) ) {
WP_CLI::warning( "The '$name' theme is already active." );
return;
}
if ( $theme->get_stylesheet() != $theme->get_template() && ! $this->fetcher->get( $theme->get_template() ) ) {
WP_CLI::error( "The '{$theme->get_stylesheet()}' theme cannot be activated without its parent, '{$theme->get_template()}'." );
}
switch_theme( $theme->get_template(), $theme->get_stylesheet() );
if ( $this->is_active_theme( $theme ) ) {
WP_CLI::success( "Switched to '$name' theme." );
} else {
WP_CLI::error( "Could not switch to '$name' theme." );
}
}
/**
* Enables a theme on a WordPress multisite install.
*
* Permits theme to be activated from the dashboard of a site on a WordPress
* multisite install.
*
* ## OPTIONS
*
* <theme>
* : The theme to enable.
*
* [--network]
* : If set, the theme is enabled for the entire network
*
* [--activate]
* : If set, the theme is activated for the current site. Note that
* the "network" flag has no influence on this.
*
* ## EXAMPLES
*
* # Enable theme
* $ wp theme enable twentysixteen
* Success: Enabled the 'Twenty Sixteen' theme.
*
* # Network enable theme
* $ wp theme enable twentysixteen --network
* Success: Network enabled the 'Twenty Sixteen' theme.
*
* # Network enable and activate theme for current site
* $ wp theme enable twentysixteen --activate
* Success: Enabled the 'Twenty Sixteen' theme.
* Success: Switched to 'Twenty Sixteen' theme.
*/
public function enable( $args, $assoc_args ) {
if ( ! is_multisite() ) {
WP_CLI::error( 'This is not a multisite install.' );
}
$theme = $this->fetcher->get_check( $args[0] );
$name = $theme->get( 'Name' );
# If the --network flag is set, we'll be calling the (get|update)_site_option functions
$_site = ! empty( $assoc_args['network'] ) ? '_site' : '';
# Add the current theme to the allowed themes option or site option
$allowed_themes = call_user_func( "get{$_site}_option", 'allowedthemes' );
if ( empty( $allowed_themes ) )
$allowed_themes = array();
$allowed_themes[ $theme->get_stylesheet() ] = true;
call_user_func( "update{$_site}_option", 'allowedthemes', $allowed_themes );
if ( ! empty( $assoc_args['network'] ) )
WP_CLI::success( "Network enabled the '$name' theme." );
else
WP_CLI::success( "Enabled the '$name' theme." );
# If the --activate flag is set, activate the theme for the current site
if ( ! empty( $assoc_args['activate'] ) ) {
$this->activate( $args );
}
}
/**
* Disables a theme on a WordPress multisite install.
*
* Removes ability for a theme to be activated from the dashboard of a site
* on a WordPress multisite install.
*
* ## OPTIONS
*
* <theme>
* : The theme to disable.
*
* [--network]
* : If set, the theme is disabled on the network level. Note that
* individual sites may still have this theme enabled if it was
* enabled for them independently.
*
* ## EXAMPLES
*
* # Disable theme
* $ wp theme disable twentysixteen
* Success: Disabled the 'Twenty Sixteen' theme.
*
* # Disable theme in network level
* $ wp theme disable twentysixteen --network
* Success: Network disabled the 'Twenty Sixteen' theme.
*/
public function disable( $args, $assoc_args ) {
if ( ! is_multisite() ) {
WP_CLI::error( 'This is not a multisite install.' );
}
$theme = $this->fetcher->get_check( $args[0] );
$name = $theme->get( 'Name' );
# If the --network flag is set, we'll be calling the (get|update)_site_option functions
$_site = ! empty( $assoc_args['network'] ) ? '_site' : '';
# Add the current theme to the allowed themes option or site option
$allowed_themes = call_user_func( "get{$_site}_option", 'allowedthemes' );
if ( ! empty( $allowed_themes[ $theme->get_stylesheet() ] ) )
unset( $allowed_themes[ $theme->get_stylesheet() ] );
call_user_func( "update{$_site}_option", 'allowedthemes', $allowed_themes );
if ( ! empty( $assoc_args['network'] ) )
WP_CLI::success( "Network disabled the '$name' theme." );
else
WP_CLI::success( "Disabled the '$name' theme." );
}
private function is_active_theme( $theme ) {
return $theme->get_stylesheet_directory() == get_stylesheet_directory();
}
/**
* Gets the path to a theme or to the theme directory.
*
* ## OPTIONS
*
* [<theme>]
* : The theme to get the path to. Path includes "style.css" file.
* If not set, will return the path to the themes directory.
*
* [--dir]
* : If set, get the path to the closest parent directory, instead of the
* theme's "style.css" file.
*
* ## EXAMPLES
*
* # Get theme path
* $ wp theme path
* /var/www/example.com/public_html/wp-content/themes
*
* # Change directory to theme path
* $ cd $(wp theme path)
*/
public function path( $args, $assoc_args ) {
if ( empty( $args ) ) {
$path = WP_CONTENT_DIR . '/themes';
} else {
$theme = $this->fetcher->get_check( $args[0] );
$path = $theme->get_stylesheet_directory();
if ( ! \WP_CLI\Utils\get_flag_value( $assoc_args, 'dir' ) )
$path .= '/style.css';
}
WP_CLI::line( $path );
}
protected function install_from_repo( $slug, $assoc_args ) {
$api = themes_api( 'theme_information', array( 'slug' => $slug ) );
if ( is_wp_error( $api ) ) {
return $api;
}
if ( isset( $assoc_args['version'] ) ) {
self::alter_api_response( $api, $assoc_args['version'] );
}
if ( ! \WP_CLI\Utils\get_flag_value( $assoc_args, 'force' ) ) {
$theme = wp_get_theme( $slug );
if ( $theme->exists() ) {
// We know this will fail, so avoid a needless download of the package.
return new WP_Error( 'already_installed', 'Theme already installed.' );
}
// Clear cache so WP_Theme doesn't create a "missing theme" object.
$cache_hash = md5( $theme->theme_root . '/' . $theme->stylesheet );
foreach( array( 'theme', 'screenshot', 'headers', 'page_templates' ) as $key ) {
wp_cache_delete( $key . '-' . $cache_hash, 'themes' );
}
}
WP_CLI::log( sprintf( 'Installing %s (%s)', html_entity_decode( $api->name, ENT_QUOTES ), $api->version ) );
if ( \WP_CLI\Utils\get_flag_value( $assoc_args, 'version' ) != 'dev' ) {
WP_CLI::get_http_cache_manager()->whitelist_package( $api->download_link, $this->item_type, $api->slug, $api->version );
}
// Ignore failures on accessing SSL "https://api.wordpress.org/themes/update-check/1.1/" in `wp_update_themes()` which seem to occur intermittently.
set_error_handler( array( __CLASS__, 'error_handler' ), E_USER_WARNING | E_USER_NOTICE );
$result = $this->get_upgrader( $assoc_args )->install( $api->download_link );
restore_error_handler();
return $result;
}
protected function get_item_list() {
$items = array();
if ( is_multisite() ) {
$site_enabled = get_option( 'allowedthemes' );
if ( empty( $site_enabled ) )
$site_enabled = array();
$network_enabled = get_site_option( 'allowedthemes' );
if ( empty( $network_enabled ) )
$network_enabled = array();
}
foreach ( wp_get_themes() as $key => $theme ) {
$file = $theme->get_stylesheet_directory();
$update_info = $this->get_update_info( $theme->get_stylesheet() );
$items[ $file ] = array(
'name' => $key,
'status' => $this->get_status( $theme ),
'update' => (bool) $update_info,
'update_version' => $update_info['new_version'],
'update_package' => $update_info['package'],
'version' => $theme->get('Version'),
'update_id' => $theme->get_stylesheet(),
'title' => $theme->get('Name'),
'description' => wordwrap( $theme->get('Description') ),
'author' => $theme->get('Author'),
);
if ( is_multisite() ) {
if ( ! empty( $site_enabled[ $key ] ) && ! empty( $network_enabled[ $key ] ) )
$items[ $file ]['enabled'] = 'network,site';
elseif ( ! empty( $network_enabled[ $key ] ) )
$items[ $file ]['enabled'] = 'network';
elseif ( ! empty( $site_enabled[ $key ] ) )
$items[ $file ]['enabled'] = 'site';
else
$items[ $file ]['enabled'] = 'no';
}
}
return $items;
}
protected function filter_item_list( $items, $args ) {
$theme_files = array();
foreach ( $args as $arg ) {
$theme_files[] = $this->fetcher->get_check( $arg )->get_stylesheet_directory();
}
return \WP_CLI\Utils\pick_fields( $items, $theme_files );
}
/**
* Installs one or more themes.
*
* ## OPTIONS
*
* <theme|zip|url>...
* : One or more themes to install. Accepts a theme slug, the path to a local zip file, or a URL to a remote zip file.
*
* [--version=<version>]
* : If set, get that particular version from wordpress.org, instead of the
* stable version.
*
* [--force]
* : If set, the command will overwrite any installed version of the theme, without prompting
* for confirmation.
*
* [--activate]
* : If set, the theme will be activated immediately after install.
*
* ## EXAMPLES
*
* # Install the latest version from wordpress.org and activate
* $ wp theme install twentysixteen --activate
* Installing Twenty Sixteen (1.2)
* Downloading install package from http://downloads.wordpress.org/theme/twentysixteen.1.2.zip...
* Unpacking the package...
* Installing the theme...
* Theme installed successfully.
* Activating 'twentysixteen'...
* Success: Switched to 'Twenty Sixteen' theme.
*
* # Install from a local zip file
* $ wp theme install ../my-theme.zip
*
* # Install from a remote zip file
* $ wp theme install http://s3.amazonaws.com/bucketname/my-theme.zip?AWSAccessKeyId=123&Expires=456&Signature=abcdef
*/
public function install( $args, $assoc_args ) {
$theme_root = get_theme_root();
if ( $theme_root && ! is_dir( $theme_root ) ) {
wp_mkdir_p( $theme_root );
register_theme_directory( $theme_root );
}
parent::install( $args, $assoc_args );
}
/**
* Gets details about a theme.
*
* ## OPTIONS
*
* <theme>
* : The theme to get.
*
* [--field=<field>]
* : Instead of returning the whole theme, returns the value of a single field.
*
* [--fields=<fields>]
* : Limit the output to specific fields. Defaults to all fields.
*
* [--format=<format>]
* : Render output in a particular format.
* ---
* default: table
* options:
* - table
* - csv
* - json
* - yaml
* ---
*
* ## EXAMPLES
*
* $ wp theme get twentysixteen --fields=name,title,version
* +---------+----------------+
* | Field | Value |
* +---------+----------------+
* | name | Twenty Sixteen |
* | title | Twenty Sixteen |
* | version | 1.2 |
* +---------+----------------+
*/
public function get( $args, $assoc_args ) {
$theme = $this->fetcher->get_check( $args[0] );
$errors = $theme->errors();
if ( is_wp_error( $errors ) ) {
$message = $errors->get_error_message();
WP_CLI::error( $message );
}
// WP_Theme object employs magic getter, unfortunately
$theme_vars = array( 'name', 'title', 'version', 'parent_theme', 'template_dir', 'stylesheet_dir', 'template', 'stylesheet', 'screenshot', 'description', 'author', 'tags', 'theme_root', 'theme_root_uri',
);
$theme_obj = new stdClass;
foreach ( $theme_vars as $var ) {
$theme_obj->$var = $theme->$var;
}
$theme_obj->description = wordwrap( $theme_obj->description );
if ( empty( $assoc_args['fields'] ) ) {
$assoc_args['fields'] = $theme_vars;
}
$formatter = $this->get_formatter( $assoc_args );
$formatter->display_item( $theme_obj );
}
/**
* Updates one or more themes.
*
* ## OPTIONS
*
* [<theme>...]
* : One or more themes to update.
*
* [--all]
* : If set, all themes that have updates will be updated.
*
* [--exclude=<theme-names>]
* : Comma separated list of theme names that should be excluded from updating.
*
* [--format=<format>]
* : Render output in a particular format.
* ---
* default: table
* options:
* - table
* - csv
* - json
* - summary
* ---
*
* [--version=<version>]
* : If set, the theme will be updated to the specified version.
*
* [--dry-run]
* : Preview which themes would be updated.
*
* ## EXAMPLES
*
* # Update multiple themes
* $ wp theme update twentyfifteen twentysixteen
* Downloading update from https://downloads.wordpress.org/theme/twentyfifteen.1.5.zip...
* Unpacking the update...
* Installing the latest version...
* Removing the old version of the theme...
* Theme updated successfully.
* Downloading update from https://downloads.wordpress.org/theme/twentysixteen.1.2.zip...
* Unpacking the update...
* Installing the latest version...
* Removing the old version of the theme...
* Theme updated successfully.
* +---------------+-------------+-------------+---------+
* | name | old_version | new_version | status |
* +---------------+-------------+-------------+---------+
* | twentyfifteen | 1.4 | 1.5 | Updated |
* | twentysixteen | 1.1 | 1.2 | Updated |
* +---------------+-------------+-------------+---------+
* Success: Updated 2 of 2 themes.
*
* # Exclude themes updates when bulk updating the themes
* $ wp theme update --all --exclude=twentyfifteen
* Downloading update from https://downloads.wordpress.org/theme/astra.1.0.5.1.zip...
* Unpacking the update...
* Installing the latest version...
* Removing the old version of the theme...
* Theme updated successfully.
* Downloading update from https://downloads.wordpress.org/theme/twentyseventeen.1.2.zip...
* Unpacking the update...
* Installing the latest version...
* Removing the old version of the theme...
* Theme updated successfully.
* +-----------------+----------+---------+----------------+
* | name | status | version | update_version |
* +-----------------+----------+---------+----------------+
* | astra | inactive | 1.0.1 | 1.0.5.1 |
* | twentyseventeen | inactive | 1.1 | 1.2 |
* +-----------------+----------+---------+----------------+
* Success: Updated 2 of 2 themes.
*
* # Update all themes
* $ wp theme update --all
*
* @alias upgrade
*/
public function update( $args, $assoc_args ) {
$all = Utils\get_flag_value( $assoc_args, 'all', false );
if ( ! ( $args = $this->check_optional_args_and_all( $args, $all ) ) ) {
return;
}
if ( isset( $assoc_args['version'] ) ) {
foreach ( $this->fetcher->get_many( $args ) as $theme ) {
$r = delete_theme( $theme->stylesheet );
if ( is_wp_error( $r ) ) {
WP_CLI::warning( $r );
} else {
$assoc_args['force'] = true;
$this->install( array( $theme->stylesheet ), $assoc_args );
}
}
} else {
parent::update_many( $args, $assoc_args );
}
}
/**
* Checks if a given theme is installed.
*
* Returns exit code 0 when installed, 1 when uninstalled.
*
* ## OPTIONS
*
* <theme>
* : The theme to check.
*
* ## EXAMPLES
*
* # Check whether theme is installed; exit status 0 if installed, otherwise 1
* $ wp theme is-installed hello
* $ echo $?
* 1
*
* @subcommand is-installed
*/
public function is_installed( $args, $assoc_args = array() ) {
$theme = wp_get_theme( $args[0] );
if ( $theme->exists() ) {
WP_CLI::halt( 0 );
} else {
WP_CLI::halt( 1 );
}
}
/**
* Checks if a given theme is active.
*
* Returns exit code 0 when active, 1 when not active.
*
* ## OPTIONS
*
* <theme>
* : The plugin to check.
*
* ## EXAMPLES
*
* # Check whether theme is Active; exit status 0 if active, otherwise 1
* $ wp theme is-active twentyfifteen
* $ echo $?
* 1
*
* @subcommand is-active
*/
public function is_active( $args, $assoc_args = array() ) {
$theme = wp_get_theme( $args[0] );
if ( ! $theme->exists() ) {
WP_CLI::halt( 1 );
}
$this->is_active_theme( $theme ) ? WP_CLI::halt( 0 ) : WP_CLI::halt( 1 );
}
/**
* Deletes one or more themes.
*
* Removes the theme or themes from the filesystem.
*
* ## OPTIONS
*
* <theme>...
* : One or more themes to delete.
*
* ## EXAMPLES
*
* $ wp theme delete twentytwelve
* Deleted 'twentytwelve' theme.
* Success: Deleted 1 of 1 themes.
*
* @alias uninstall
*/
public function delete( $args ) {
$successes = $errors = 0;
foreach ( $this->fetcher->get_many( $args ) as $theme ) {
$theme_slug = $theme->get_stylesheet();
if ( $this->is_active_theme( $theme ) ) {
WP_CLI::warning( "Can't delete the currently active theme: $theme_slug" );
$errors++;
continue;
}
$r = delete_theme( $theme_slug );
if ( is_wp_error( $r ) ) {
WP_CLI::warning( $r );
$errors++;
} else {
WP_CLI::log( "Deleted '$theme_slug' theme." );
$successes++;
}
}
if ( ! $this->chained_command ) {
Utils\report_batch_operation_results( 'theme', 'delete', count( $args ), $successes, $errors );
}
}
/**
* Gets a list of themes.
*
* ## OPTIONS
*
* [--<field>=<value>]
* : Filter results based on the value of a field.
*
* [--field=<field>]
* : Prints the value of a single field for each theme.
*
* [--fields=<fields>]
* : Limit the output to specific object fields.
*
* [--format=<format>]
* : Render output in a particular format.
* ---
* default: table
* options:
* - table
* - csv
* - json
* - count
* - yaml
* ---
*
* ## AVAILABLE FIELDS
*
* These fields will be displayed by default for each theme:
*
* * name
* * status
* * update
* * version
*
* These fields are optionally available:
*
* * update_version
* * update_package
* * update_id
* * title
* * description
*
* ## EXAMPLES
*
* # List themes
* $ wp theme list --status=inactive --format=csv
* name,status,update,version
* twentyfourteen,inactive,none,1.7
* twentysixteen,inactive,available,1.1
*
* @subcommand list
*/
public function list_( $_, $assoc_args ) {
parent::_list( $_, $assoc_args );
}
/**
* If have optional args ([<theme>...]) and an all option, then check have something to do.
*
* @param array $args Passed-in arguments.
* @param bool $all All flag.
* @return array Same as $args if not all, otherwise all slugs.
*/
private function check_optional_args_and_all( $args, $all ) {
if ( $all ) {
$args = array_map( function( $item ){
return Utils\get_theme_name( $item );
}, array_keys( $this->get_all_items() ) );
}
if ( empty( $args ) ) {
if ( ! $all ) {
WP_CLI::error( 'Please specify one or more themes, or use --all.' );
}
WP_CLI::success( 'No themes installed.' ); // Don't error if --all given for BC.
}
return $args;
}
}