-
Notifications
You must be signed in to change notification settings - Fork 86
Expand file tree
/
Copy pathTheme_Command.php
More file actions
1087 lines (988 loc) · 30.7 KB
/
Theme_Command.php
File metadata and controls
1087 lines (988 loc) · 30.7 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
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
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
use WP_CLI\CommandWithUpgrade;
use WP_CLI\ParseThemeNameInput;
use WP_CLI\Utils;
use WP_CLI\Path;
/**
* 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.
* Success: Installed 1 of 1 themes.
*
* # 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
*
* @phpstan-type ThemeInformation object{name: string, slug: non-empty-string, version: string, new_version: string, download_link: string, requires_php?: string, requires?: string}&\stdClass
* @extends CommandWithUpgrade<\WP_Theme>
*/
class Theme_Command extends CommandWithUpgrade {
/**
* @use ParseThemeNameInput<\WP_Theme>
*/
use ParseThemeNameInput;
protected $item_type = 'theme';
protected $upgrade_refresh = 'wp_update_themes';
protected $upgrade_transient = 'update_themes';
protected $obj_fields = [
'name',
'status',
'update',
'version',
'update_version',
'auto_update',
'type',
];
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 );
}
/**
* Checks for theme updates without performing them.
*
* Lists the available theme updates. Similar to `wp core check-update`.
*
* ## OPTIONS
*
* [<theme>...]
* : One or more themes to check for updates.
*
* [--all]
* : If set, all themes will be checked for updates.
*
* [--field=<field>]
* : Prints the value of a single field for each update.
*
* [--fields=<fields>]
* : Limit the output to specific object fields. Defaults to name,status,version,update_version.
*
* [--format=<format>]
* : Render output in a particular format.
* ---
* default: table
* options:
* - table
* - csv
* - json
* - yaml
* ---
*
* ## EXAMPLES
*
* # Check for theme updates
* $ wp theme check-update
* +------------+----------+---------+----------------+
* | name | status | version | update_version |
* +------------+----------+---------+----------------+
* | twentytwelve | inactive | 2.0 | 2.1 |
* +------------+----------+---------+----------------+
*
* # List themes with available updates in JSON format
* $ wp theme check-update --format=json
* [{"name":"twentytwelve","status":"inactive","version":"2.0","update_version":"2.1"}]
*
* @subcommand check-update
*/
public function check_update( $args, $assoc_args ) {
$all = Utils\get_flag_value( $assoc_args, 'all', false );
$args = $this->check_optional_args_and_all( $args, $all );
if ( ! $args ) {
return;
}
// Force WordPress to check for updates.
call_user_func( $this->upgrade_refresh );
if ( $all ) {
// Get all themes
$items = $this->get_item_list();
} else {
// Get specific themes and their update info
$themes = $this->fetcher->get_many( $args );
$all_items = $this->get_item_list();
$items = [];
foreach ( $themes as $theme ) {
$stylesheet = $theme->get_stylesheet();
if ( isset( $all_items[ $stylesheet ] ) ) {
$items[ $stylesheet ] = $all_items[ $stylesheet ];
}
}
}
// Filter to only themes with available updates
$items_with_updates = array_filter(
$items,
function ( $item ) {
return 'available' === $item['update'];
}
);
if ( empty( $items_with_updates ) ) {
WP_CLI::success( 'All themes are up to date.' );
return;
}
// Set default fields for check-update output
if ( ! isset( $assoc_args['fields'] ) ) {
$assoc_args['fields'] = 'name,status,version,update_version';
}
$formatter = $this->get_formatter( $assoc_args );
$formatter->display_items( array_values( $items_with_updates ) );
}
/**
* 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.
*
* [--page=<page>]
* : Optional page to display.
* ---
* default: 1
* ---
*
* [--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(
Utils\mustache_render(
self::get_template_path( 'theme-status.mustache' ),
[
'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();
}
/**
* Activates a theme.
*
* ## OPTIONS
*
* <theme>
* : The theme to activate.
*
* ## EXAMPLES
*
* $ wp theme activate twentysixteen
* Success: Switched to 'Twenty Sixteen' theme.
*
* @param string[] $args Positional arguments.
* @param array $assoc_args Associative arguments. Unused.
*/
public function activate( $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 );
}
$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_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 installation.' );
}
/**
* @var \WP_Theme $theme
*/
$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();
}
/**
* @var array<string, bool> $allowed_themes
*/
$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 installation.' );
}
$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' );
/**
* @var array<string, bool> $allowed_themes
*/
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." );
}
}
/**
* 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 ( ! Utils\get_flag_value( $assoc_args, 'dir' ) ) {
$path .= '/style.css';
}
}
WP_CLI::line( $path );
}
protected function install_from_repo( $slug, $assoc_args ) {
global $wp_version;
// Extract the major WordPress version (e.g., "6.3") from the full version string
list($wp_core_version) = explode( '-', $wp_version );
$wp_core_version = implode( '.', array_slice( explode( '.', $wp_core_version ), 0, 2 ) );
/**
* @var \WP_Error|ThemeInformation $api
*/
$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'] );
} elseif ( ! Utils\get_flag_value( $assoc_args, 'ignore-requirements', false ) ) {
$requires_php = isset( $api->requires_php ) ? $api->requires_php : null;
$requires_wp = isset( $api->requires ) ? $api->requires : null;
$compatible_php = empty( $requires_php ) || version_compare( PHP_VERSION, $requires_php, '>=' );
$compatible_wp = empty( $requires_wp ) || version_compare( $wp_core_version, $requires_wp, '>=' );
if ( ! $compatible_wp ) {
return new WP_Error( 'requirements_not_met', "This theme does not work with your version of WordPress. Minimum WordPress requirement is $requires_wp" );
}
if ( ! $compatible_php ) {
return new WP_Error( 'requirements_not_met', "This theme does not work with your version of PHP. Minimum PHP required is $requires_php" );
}
}
if ( ! 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 ( [ '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 ( 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() {
return $this->get_all_themes();
}
protected function filter_item_list( $items, $args ) {
$theme_files = array();
foreach ( $args as $arg ) {
$theme_files[] = $this->fetcher->get_check( $arg )->get_stylesheet();
}
return 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, a URL to a remote zip file, or a URL to a WordPress.org theme directory.
*
* [--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.
*
* [--ignore-requirements]
* : If set, the command will install the theme while ignoring any WordPress or PHP version requirements
* specified by the theme authors.
*
* [--activate]
* : If set, the theme will be activated immediately after install.
*
* [--insecure]
* : Retry downloads without certificate validation if TLS handshake fails. Note: This makes the request vulnerable to a MITM attack.
*
* [--slug=<slug>]
* : Use this as the target directory name when installing from a zip file. Cannot be used when installing multiple themes.
*
* ## 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.
* Success: Installed 1 of 1 themes.
*
* # 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
*
* # Install from a WordPress.org theme directory URL
* $ wp theme install https://wordpress.org/themes/twentysixteen/
* Detected WordPress.org themes directory URL, using slug: twentysixteen
* 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.
* Success: Installed 1 of 1 themes.
*/
public function install( $args, $assoc_args ) {
if ( count( $args ) > 1 && Utils\get_flag_value( $assoc_args, 'activate', false ) ) {
WP_CLI::warning( sprintf( 'Only this single theme will be activated: %s', end( $args ) ) );
reset( $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 ) {
/**
* @var \WP_Theme $theme
*/
$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 = [
'name',
'title',
'version',
'status',
'parent_theme',
'template_dir',
'stylesheet_dir',
'template',
'stylesheet',
'screenshot',
'description',
'author',
'tags',
'theme_root',
'theme_root_uri',
'type',
];
$theme_obj = new stdClass();
foreach ( $theme_vars as $var ) {
// @phpstan-ignore-next-line
$theme_obj->$var = $theme->$var;
}
$theme_obj->status = $this->get_status( $theme );
$theme_obj->description = wordwrap( $theme_obj->description );
// Determine theme type (block or classic). is_block_theme() was added in WP 5.9.
$theme_obj->type = 'classic';
if ( method_exists( $theme, 'is_block_theme' ) && $theme->is_block_theme() ) {
$theme_obj->type = 'block';
}
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.
*
* [--minor]
* : Only perform updates for minor releases (e.g. from 1.3 to 1.4 instead of 2.0)
*
* [--patch]
* : Only perform updates for patch releases (e.g. from 1.3 to 1.3.3 instead of 1.4)
*
* [--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.
*
* [--insecure]
* : Retry downloads without certificate validation if TLS handshake fails. Note: This makes the request vulnerable to a MITM attack.
*
* [--auto-update-indicated]
* : Only update themes where the server response indicates an automatic update. Updates to the version indicated by the server, not necessarily the latest version. Cannot be used with `--version`, `--minor`, or `--patch`.
*
* [--include-vcs]
* : Include themes that are version-controlled with a VCS (e.g. git, svn, hg). Skipped by default.
*
* ## 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 );
// Handle --auto-update-indicated flag if present.
if ( $this->handle_auto_update_indicated( $args, $assoc_args ) ) {
return;
}
$args = $this->check_optional_args_and_all( $args, $all );
if ( ! $args ) {
return;
}
if ( isset( $assoc_args['version'] ) && isset( $assoc_args['dry-run'] ) ) {
WP_CLI::error( '--dry-run cannot be used together with --version.' );
}
if ( isset( $assoc_args['version'] ) ) {
foreach ( $this->fetcher->get_many( $args ) as $theme ) {
$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 ) {
$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 theme 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 ) {
$theme = wp_get_theme( $args[0] );
if ( ! $theme->exists() ) {
WP_CLI::halt( 1 );
}
$this->is_active_theme( $theme ) || $this->is_active_parent_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.
*
* [--all]
* : If set, all themes will be deleted except active theme.
*
* [--force]
* : To delete active theme use this.
*
* ## EXAMPLES
*
* $ wp theme delete twentytwelve
* Deleted 'twentytwelve' theme.
* Success: Deleted 1 of 1 themes.
*
* @alias uninstall
*/
public function delete( $args, $assoc_args ) {
$all = Utils\get_flag_value( $assoc_args, 'all', false );
$args = $this->check_optional_args_and_all( $args, $all, 'delete' );
if ( ! $args ) {
return;
}
$force = Utils\get_flag_value( $assoc_args, 'force', false );
$successes = 0;
$errors = 0;
foreach ( $this->fetcher->get_many( $args ) as $theme ) {
$theme_slug = $theme->get_stylesheet();
if ( $this->is_active_theme( $theme ) && ! $force ) {
if ( ! $all ) {
WP_CLI::warning( "Can't delete the currently active theme: $theme_slug" );
++$errors;
}
continue;
}
if ( $this->is_active_parent_theme( $theme ) && ! $force ) {
if ( ! $all ) {
WP_CLI::warning( "Can't delete the parent of 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 );
}
}