Skip to content

Commit 2ef05ac

Browse files
Merge pull request #59 from wp-cli/57-strip-wp-content
Strip `wp-content/` using `ZipArchive` to allow `--skip-content` always
2 parents 661a58c + 9ef518a commit 2ef05ac

File tree

4 files changed

+69
-36
lines changed

4 files changed

+69
-36
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ Subsequent uses of command will use the local cache if it still exists.
111111
Select which version you want to download. Accepts a version number, 'latest' or 'nightly'
112112

113113
[--skip-content]
114-
Download the latest version of WP without the default themes and plugins (en_US locale only)
114+
Download WP without the default themes and plugins.
115115

116116
[--force]
117117
Overwrites existing files, if present.

features/core-download.feature

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -337,22 +337,24 @@ Feature: Download WordPress
337337
And the wp-includes directory should exist
338338
And the wp-content directory should not exist
339339

340-
Scenario: Core download without the wp-content dir should error for non US locale
340+
Scenario: Core download without the wp-content dir should work non US locale
341341
Given an empty directory
342342

343-
When I try `wp core download --skip-content --locale=nl_NL`
344-
Then STDERR should be:
343+
When I run `wp core download --skip-content --locale=nl_NL`
344+
Then STDOUT should contain:
345345
"""
346-
Error: Skip content build is only available for the en_US locale.
346+
Success: WordPress downloaded.
347347
"""
348-
And the return code should be 1
348+
And the wp-includes directory should exist
349+
And the wp-content directory should not exist
349350

350-
Scenario: Core download without the wp-content dir should error if a version is set
351+
Scenario: Core download without the wp-content dir should work if a version is set
351352
Given an empty directory
352353

353354
When I try `wp core download --skip-content --version=4.7`
354-
Then STDERR should contain:
355+
Then STDOUT should contain:
355356
"""
356-
Skip content build is only available for the latest version.
357+
Success: WordPress downloaded.
357358
"""
358-
And the return code should be 1
359+
And the wp-includes directory should exist
360+
And the wp-content directory should not exist

src/Core_Command.php

Lines changed: 52 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ function check_update( $_, $assoc_args ) {
106106
* : Select which version you want to download. Accepts a version number, 'latest' or 'nightly'
107107
*
108108
* [--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.
110110
*
111111
* [--force]
112112
* : Overwrites existing files, if present.
@@ -145,38 +145,26 @@ public function download( $args, $assoc_args ) {
145145
}
146146

147147
$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' );
148149

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'] ) {
168151
$version = $assoc_args['version'];
169152
$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
171154
$ext = ( 'nightly' === $version ? 'zip' : 'tar.gz' );
155+
// Skip content requires ZIP.
156+
$ext = ( $skip_content ) ? 'zip' : $ext;
172157
$download_url = $this->get_download_url( $version, $locale, $ext );
173158
} else {
174159
$offer = $this->get_download_offer( $locale );
175160
if ( !$offer ) {
176161
WP_CLI::error( "The requested locale ($locale) was not found." );
177162
}
178163
$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+
}
180168
}
181169

182170
if ( 'nightly' === $version && 'en_US' !== $locale ) {
@@ -200,15 +188,20 @@ public function download( $args, $assoc_args ) {
200188
}
201189
}
202190

191+
if ( $skip_content && 'zip' !== $extension ) {
192+
WP_CLI::error( 'Skip content is only available for ZIP files.' );
193+
}
194+
203195
$cache = WP_CLI::get_cache();
204-
$cache_key = "core/wordpress-{$version}-{$no_content}{$locale}.{$extension}";
196+
$cache_key = "core/wordpress-{$version}-{$locale}.{$extension}";
205197
$cache_file = $cache->has($cache_key);
206198

207199
$bad_cache = false;
208200
if ( $cache_file ) {
209201
WP_CLI::log( "Using cached file '$cache_file'..." );
202+
$skip_content_cache_file = $skip_content ? self::strip_content_dir( $cache_file ) : null;
210203
try{
211-
Extractor::extract( $cache_file, $download_dir );
204+
Extractor::extract( $skip_content_cache_file ? $skip_content_cache_file : $cache_file, $download_dir );
212205
} catch ( Exception $e ) {
213206
WP_CLI::warning( "Extraction failed, downloading a new copy..." );
214207
$bad_cache = true;
@@ -219,6 +212,11 @@ public function download( $args, $assoc_args ) {
219212
// We need to use a temporary file because piping from cURL to tar is flaky
220213
// on MinGW (and probably in other environments too).
221214
$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+
} );
222220

223221
$headers = array('Accept' => 'application/json');
224222
$options = array(
@@ -250,16 +248,17 @@ public function download( $args, $assoc_args ) {
250248
WP_CLI::warning( 'md5 hash checks are not available for nightly downloads.' );
251249
}
252250

251+
$skip_content_temp = $skip_content ? self::strip_content_dir( $temp ): null;
252+
253253
try {
254-
Extractor::extract( $temp, $download_dir );
254+
Extractor::extract( $skip_content_temp ? $skip_content_temp : $temp, $download_dir );
255255
} catch ( Exception $e ) {
256256
WP_CLI::error( "Couldn't extract WordPress archive. " . $e->getMessage() );
257257
}
258258

259259
if ( 'nightly' !== $version ) {
260260
$cache->import( $cache_key, $temp );
261261
}
262-
unlink( $temp );
263262
}
264263

265264
if ( $wordpress_present ) {
@@ -1312,4 +1311,31 @@ private function cleanup_extra_files( $version_from, $version_to, $locale ) {
13121311
}
13131312
}
13141313

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+
13151341
}

src/WP_CLI/CoreUpgrader.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,11 @@ public function download_package( $package ) {
4949
$ext = pathinfo( $package, PATHINFO_EXTENSION );
5050

5151
$temp = \WP_CLI\Utils\get_temp_dir() . uniqid( 'wp_' ) . '.' . $ext;
52+
register_shutdown_function( function () use ( $temp ) {
53+
if ( file_exists( $temp ) ) {
54+
unlink( $temp );
55+
}
56+
} );
5257

5358
$cache = WP_CLI::get_cache();
5459
$update = $GLOBALS['wp_cli_update_obj'];

0 commit comments

Comments
 (0)