-
Notifications
You must be signed in to change notification settings - Fork 84
Expand file tree
/
Copy pathParseThemeNameInput.php
More file actions
258 lines (220 loc) · 8.76 KB
/
ParseThemeNameInput.php
File metadata and controls
258 lines (220 loc) · 8.76 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
<?php
namespace WP_CLI;
use WP_CLI;
use Theme_AutoUpdates_Command;
/**
* @template T of \WP_Theme
*/
trait ParseThemeNameInput {
/**
* 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.
* @param string $verb Optional. Verb to use. Defaults to 'install'.
* @return array Same as $args if not all, otherwise all slugs.
* @throws ExitException If neither plugin name nor --all were provided.
*/
protected function check_optional_args_and_all( $args, $all, $verb = 'install' ) {
if ( $all ) {
$args = array_map(
'\WP_CLI\Utils\get_theme_name',
array_keys( $this->get_all_themes() )
);
}
if ( empty( $args ) ) {
if ( ! $all ) {
WP_CLI::error( 'Please specify one or more themes, or use --all.' );
}
$past_tense_verb = Utils\past_tense_verb( $verb );
WP_CLI::success( "No themes {$past_tense_verb}." ); // Don't error if --all given for BC.
}
return $args;
}
/**
* Gets all available themes.
*
* Uses the same filter core uses in themes.php to determine which themes
* should be available to manage through the WP_Themes_List_Table class.
*
* @return array
*/
private function get_all_themes() {
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 ) );
$items = array();
$theme_version_info = array();
if ( is_multisite() ) {
/**
* @var array<string, array{enabled: string}> $site_enabled
*/
$site_enabled = get_option( 'allowedthemes' );
if ( empty( $site_enabled ) ) {
$site_enabled = array();
}
/**
* @var array<string, array{enabled: string}> $network_enabled
*/
$network_enabled = get_site_option( 'allowedthemes' );
if ( empty( $network_enabled ) ) {
$network_enabled = array();
}
}
$all_update_info = $this->get_update_info();
$checked_themes = isset( $all_update_info->checked ) ? $all_update_info->checked : array();
if ( ! empty( $checked_themes ) ) {
foreach ( $checked_themes as $slug => $version ) {
$theme_version_info[ $slug ] = $this->is_theme_version_valid( $slug, $version );
}
}
$auto_updates = get_site_option( Theme_AutoUpdates_Command::SITE_OPTION );
if ( ! is_array( $auto_updates ) ) {
$auto_updates = [];
}
foreach ( wp_get_themes( [ 'errors' => null ] ) as $key => $theme ) {
$stylesheet = $theme->get_stylesheet();
$update_info = ( isset( $all_update_info->response[ $stylesheet ] ) && null !== $all_update_info->response[ $theme->get_stylesheet() ] ) ? (array) $all_update_info->response[ $theme->get_stylesheet() ] : null;
$auto_update_indicated = isset( $update_info ) && isset( $update_info['autoupdate'] ) ? (bool) $update_info['autoupdate'] : false;
// Unlike plugin update responses, the wordpress.org API does not seem to check and filter themes that don't meet
// WordPress version requirements into a separate no_updates array
// Also unlike plugin update responses, the wordpress.org API seems to always include requires AND requires_php
$requires = isset( $update_info ) && isset( $update_info['requires'] ) ? $update_info['requires'] : null;
$requires_php = isset( $update_info ) && isset( $update_info['requires_php'] ) ? $update_info['requires_php'] : null;
$compatible_php = empty( $requires_php ) || version_compare( PHP_VERSION, $requires_php, '>=' );
$compatible_wp = empty( $requires ) || version_compare( $wp_version, $requires, '>=' );
if ( ! $compatible_php ) {
$update = 'unavailable';
$update_unavailable_reason = sprintf(
'This update requires PHP version %s, but the version installed is %s.',
$requires_php,
PHP_VERSION
);
} elseif ( ! $compatible_wp ) {
$update = 'unavailable';
$update_unavailable_reason = sprintf(
'This update requires WordPress version %s, but the version installed is %s.',
$requires,
$wp_version
);
} else {
$update = $update_info ? 'available' : 'none';
}
// For display consistency, get these values from the current plugin file if they aren't in this response
if ( null === $requires ) {
$requires = ! empty( $theme->get( 'RequiresWP' ) ) ? $theme->get( 'RequiresWP' ) : '';
}
if ( null === $requires_php ) {
$requires_php = ! empty( $theme->get( 'RequiresPHP' ) ) ? $theme->get( 'RequiresPHP' ) : '';
}
// Determine theme type (block or classic). is_block_theme() was added in WP 5.9.
$theme_type = 'classic';
if ( method_exists( $theme, 'is_block_theme' ) && $theme->is_block_theme() ) {
$theme_type = 'block';
}
$items[ $stylesheet ] = [
'name' => $key,
'status' => $this->get_status( $theme ),
'update' => $update,
'update_version' => isset( $update_info['new_version'] ) ? $update_info['new_version'] : null,
'update_package' => isset( $update_info['package'] ) ? $update_info['package'] : null,
'version' => $theme->get( 'Version' ),
'update_id' => $stylesheet,
'title' => $theme->get( 'Name' ),
'description' => wordwrap( $theme->get( 'Description' ) ),
'author' => $theme->get( 'Author' ),
'auto_update' => in_array( $stylesheet, $auto_updates, true ),
'auto_update_indicated' => $auto_update_indicated,
'requires' => $requires,
'requires_php' => $requires_php,
'update_unavailable_reason' => isset( $update_unavailable_reason ) ? $update_unavailable_reason : '',
'type' => $theme_type,
];
// Compare version and update information in theme list.
if ( isset( $theme_version_info[ $key ] ) && false === $theme_version_info[ $key ] ) {
$items[ $stylesheet ]['update'] = 'version higher than expected';
}
if ( is_multisite() ) {
if ( ! empty( $site_enabled[ $key ] ) && ! empty( $network_enabled[ $key ] ) ) {
$items[ $stylesheet ]['enabled'] = 'network,site';
} elseif ( ! empty( $network_enabled[ $key ] ) ) {
$items[ $stylesheet ]['enabled'] = 'network';
} elseif ( ! empty( $site_enabled[ $key ] ) ) {
$items[ $stylesheet ]['enabled'] = 'site';
} else {
$items[ $stylesheet ]['enabled'] = 'no';
}
}
}
return $items;
}
/**
* Check if current version of the theme is higher than the one available at WP.org.
*
* @param string $slug Theme slug.
* @param string $version Theme current version.
*
* @return bool|string
*/
protected function is_theme_version_valid( $slug, $version ) {
/**
* @var \WP_Error|object{name: string, slug: string, version: string, download_link: string} $theme_info
*/
$theme_info = themes_api( 'theme_information', array( 'slug' => $slug ) );
// Return empty string for themes not on WP.org.
if ( is_wp_error( $theme_info ) ) {
return '';
}
// Compare theme version info.
return ! version_compare( $version, $theme_info->version, '>' );
}
/**
* Get the status for a given theme.
*
* @param T $theme Theme to get the status for.
*
* @return string Status of the theme.
*/
protected function get_status( $theme ) {
if ( $this->is_active_theme( $theme ) ) {
return 'active';
}
if ( $theme->get_stylesheet_directory() === get_template_directory() ) {
return 'parent';
}
return 'inactive';
}
/**
* Check whether a given theme is the active theme.
*
* @param \WP_Theme $theme Theme to check.
*
* @return bool Whether the provided theme is the active theme.
*/
protected function is_active_theme( $theme ) {
return $theme->get_stylesheet_directory() === get_stylesheet_directory();
}
/**
* Check whether a given theme is the active theme parent.
*
* @param \WP_Theme $theme Theme to check.
*
* @return bool Whether the provided theme is the active theme.
*/
protected function is_active_parent_theme( $theme ) {
return $theme->get_stylesheet_directory() === get_template_directory();
}
/**
* Get the available update info.
*
* @return object{checked: array<string, string>, response: array<string, array<string, string|null>>, no_update: array<string, object{new_version: string, package: string, requires: string}&\stdClass>} Available update info.
*/
protected function get_update_info() {
/**
* @var object{checked: array<string, string>, response: array<string, array<string, string|null>>, no_update: array<string, object{new_version: string, package: string, requires: string}&\stdClass>} $result
*/
$result = get_site_transient( 'update_themes' );
return $result;
}
}