@@ -106,7 +106,7 @@ function check_update( $_, $assoc_args ) {
106
106
* : Select which version you want to download. Accepts a version number, 'latest' or 'nightly'
107
107
*
108
108
* [--skip-content]
109
- * : Download the latest version of WP without the default themes and plugins (en_US locale only)
109
+ * : Download WP without the default themes and plugins.
110
110
*
111
111
* [--force]
112
112
* : Overwrites existing files, if present.
@@ -145,38 +145,26 @@ public function download( $args, $assoc_args ) {
145
145
}
146
146
147
147
$ locale = \WP_CLI \Utils \get_flag_value ( $ assoc_args , 'locale ' , 'en_US ' );
148
+ $ skip_content = \WP_CLI \Utils \get_flag_value ( $ assoc_args , 'skip-content ' );
148
149
149
- if ( true === \WP_CLI \Utils \get_flag_value ( $ assoc_args , 'skip-content ' ) && 'en_US ' !== $ locale ) {
150
- WP_CLI ::error ( 'Skip content build is only available for the en_US locale. ' );
151
- }
152
-
153
- if ( true === \WP_CLI \Utils \get_flag_value ( $ assoc_args , 'skip-content ' ) && isset ( $ assoc_args ['version ' ] ) ) {
154
- WP_CLI ::error ( 'Skip content build is only available for the latest version. ' );
155
- }
156
-
157
- $ no_content = '' ;
158
- if ( true === \WP_CLI \Utils \get_flag_value ( $ assoc_args , 'skip-content ' ) ) {
159
- $ response = \WP_CLI \Utils \http_request ( 'GET ' , 'https://api.wordpress.org/core/version-check/1.7/ ' );
160
- if ( 200 === $ response ->status_code && ( $ body = json_decode ( $ response ->body ) ) && is_object ( $ body ) && isset ( $ body ->offers [0 ]->packages ->no_content ) && is_array ( $ body ->offers ) ) {
161
- $ download_url = $ body ->offers [0 ]->packages ->no_content ;
162
- $ version = $ body ->offers [0 ]->version ;
163
- $ no_content = 'no-content- ' ;
164
- } else {
165
- WP_CLI ::error ( 'Skip content build is not available. ' );
166
- }
167
- } elseif ( isset ( $ assoc_args ['version ' ] ) && 'latest ' !== $ assoc_args ['version ' ] ) {
150
+ if ( isset ( $ assoc_args ['version ' ] ) && 'latest ' !== $ assoc_args ['version ' ] ) {
168
151
$ version = $ assoc_args ['version ' ];
169
152
$ version = ( in_array ( strtolower ( $ version ), array ( 'trunk ' , 'nightly ' ) ) ? 'nightly ' : $ version );
170
- //nightly builds are only available in .zip format
153
+ // nightly builds are only available in .zip format
171
154
$ ext = ( 'nightly ' === $ version ? 'zip ' : 'tar.gz ' );
155
+ // Skip content requires ZIP.
156
+ $ ext = ( $ skip_content ) ? 'zip ' : $ ext ;
172
157
$ download_url = $ this ->get_download_url ( $ version , $ locale , $ ext );
173
158
} else {
174
159
$ offer = $ this ->get_download_offer ( $ locale );
175
160
if ( !$ offer ) {
176
161
WP_CLI ::error ( "The requested locale ( $ locale) was not found. " );
177
162
}
178
163
$ version = $ offer ['current ' ];
179
- $ download_url = str_replace ( '.zip ' , '.tar.gz ' , $ offer ['download ' ] );
164
+ $ download_url = $ offer ['download ' ];
165
+ if ( ! $ skip_content ) {
166
+ $ download_url = str_replace ( '.zip ' , '.tar.gz ' , $ download_url );
167
+ }
180
168
}
181
169
182
170
if ( 'nightly ' === $ version && 'en_US ' !== $ locale ) {
@@ -200,15 +188,20 @@ public function download( $args, $assoc_args ) {
200
188
}
201
189
}
202
190
191
+ if ( $ skip_content && 'zip ' !== $ extension ) {
192
+ WP_CLI ::error ( 'Skip content is only available for ZIP files. ' );
193
+ }
194
+
203
195
$ cache = WP_CLI ::get_cache ();
204
- $ cache_key = "core/wordpress- {$ version }- {$ no_content }{ $ locale }. {$ extension }" ;
196
+ $ cache_key = "core/wordpress- {$ version }- {$ locale }. {$ extension }" ;
205
197
$ cache_file = $ cache ->has ($ cache_key );
206
198
207
199
$ bad_cache = false ;
208
200
if ( $ cache_file ) {
209
201
WP_CLI ::log ( "Using cached file ' $ cache_file'... " );
202
+ $ skip_content_cache_file = $ skip_content ? self ::strip_content_dir ( $ cache_file ) : null ;
210
203
try {
211
- Extractor::extract ( $ cache_file , $ download_dir );
204
+ Extractor::extract ( $ skip_content_cache_file ? $ skip_content_cache_file : $ cache_file , $ download_dir );
212
205
} catch ( Exception $ e ) {
213
206
WP_CLI ::warning ( "Extraction failed, downloading a new copy... " );
214
207
$ bad_cache = true ;
@@ -219,6 +212,11 @@ public function download( $args, $assoc_args ) {
219
212
// We need to use a temporary file because piping from cURL to tar is flaky
220
213
// on MinGW (and probably in other environments too).
221
214
$ temp = \WP_CLI \Utils \get_temp_dir () . uniqid ('wp_ ' ) . ". {$ extension }" ;
215
+ register_shutdown_function ( function () use ( $ temp ) {
216
+ if ( file_exists ( $ temp ) ) {
217
+ unlink ( $ temp );
218
+ }
219
+ } );
222
220
223
221
$ headers = array ('Accept ' => 'application/json ' );
224
222
$ options = array (
@@ -250,16 +248,17 @@ public function download( $args, $assoc_args ) {
250
248
WP_CLI ::warning ( 'md5 hash checks are not available for nightly downloads. ' );
251
249
}
252
250
251
+ $ skip_content_temp = $ skip_content ? self ::strip_content_dir ( $ temp ): null ;
252
+
253
253
try {
254
- Extractor::extract ( $ temp , $ download_dir );
254
+ Extractor::extract ( $ skip_content_temp ? $ skip_content_temp : $ temp , $ download_dir );
255
255
} catch ( Exception $ e ) {
256
256
WP_CLI ::error ( "Couldn't extract WordPress archive. " . $ e ->getMessage () );
257
257
}
258
258
259
259
if ( 'nightly ' !== $ version ) {
260
260
$ cache ->import ( $ cache_key , $ temp );
261
261
}
262
- unlink ( $ temp );
263
262
}
264
263
265
264
if ( $ wordpress_present ) {
@@ -1312,4 +1311,31 @@ private function cleanup_extra_files( $version_from, $version_to, $locale ) {
1312
1311
}
1313
1312
}
1314
1313
1314
+ private static function strip_content_dir ( $ zip_file ) {
1315
+ $ new_zip_file = \WP_CLI \Utils \get_temp_dir () . uniqid ( 'wp_ ' ) . '.zip ' ;
1316
+ register_shutdown_function ( function () use ( $ new_zip_file ) {
1317
+ if ( file_exists ( $ new_zip_file ) ) {
1318
+ unlink ( $ new_zip_file );
1319
+ }
1320
+ } );
1321
+ // Duplicate file to avoid modifying the original, which could be cache.
1322
+ if ( ! copy ( $ zip_file , $ new_zip_file ) ) {
1323
+ WP_CLI ::error ( 'Failed to copy ZIP file. ' );
1324
+ }
1325
+ $ zip = new ZipArchive ();
1326
+ $ res = $ zip ->open ( $ new_zip_file );
1327
+ if ( true === $ res ) {
1328
+ for ( $ i = 0 ; $ i < $ zip ->numFiles ; $ i ++ ) {
1329
+ $ info = $ zip ->statIndex ( $ i );
1330
+ if ( false !== stripos ( $ info ['name ' ], 'wp-content/ ' ) ) {
1331
+ $ zip ->deleteIndex ( $ i );
1332
+ }
1333
+ }
1334
+ $ zip ->close ();
1335
+ return $ new_zip_file ;
1336
+ } else {
1337
+ WP_CLI ::error ( 'ZipArchive failed to open ZIP file. ' );
1338
+ }
1339
+ }
1340
+
1315
1341
}
0 commit comments