Skip to content

Commit 5245d69

Browse files
committed
Merge branch 'master' into 68-handle-incomplete-class-serialization
2 parents fef94e8 + b45eeb4 commit 5245d69

File tree

3 files changed

+64
-5
lines changed

3 files changed

+64
-5
lines changed

bin/test.sh

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@ then
88
phpunit
99
fi
1010

11+
if [ $WP_VERSION = "latest" ]; then
12+
export WP_VERSION=$(curl -s https://api.wordpress.org/core/version-check/1.7/ | jq -r ".offers[0].current")
13+
fi
14+
1115
# Run the functional tests
1216
BEHAT_TAGS=$(php utils/behat-tags.php)
1317
behat --format progress $BEHAT_TAGS --strict

features/bootstrap/utils.php

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ function extract_from_phar( $path ) {
2424

2525
$fname = basename( $path );
2626

27-
$tmp_path = get_temp_dir() . "wp-cli-$fname";
27+
$tmp_path = get_temp_dir() . uniqid( 'wp-cli-extract-from-phar-', true ) . "-$fname";
2828

2929
copy( $path, $tmp_path );
3030

@@ -351,19 +351,21 @@ function pick_fields( $item, $fields ) {
351351
* @category Input
352352
*
353353
* @param string $content Some form of text to edit (e.g. post content)
354+
* @param string $title Title to display in the editor.
355+
* @param string $ext Extension to use with the temp file.
354356
* @return string|bool Edited text, if file is saved from editor; false, if no change to file.
355357
*/
356-
function launch_editor_for_input( $input, $filename = 'WP-CLI' ) {
358+
function launch_editor_for_input( $input, $title = 'WP-CLI', $ext = 'tmp' ) {
357359

358360
check_proc_available( 'launch_editor_for_input' );
359361

360362
$tmpdir = get_temp_dir();
361363

362364
do {
363-
$tmpfile = basename( $filename );
365+
$tmpfile = basename( $title );
364366
$tmpfile = preg_replace( '|\.[^.]*$|', '', $tmpfile );
365367
$tmpfile .= '-' . substr( md5( mt_rand() ), 0, 6 );
366-
$tmpfile = $tmpdir . $tmpfile . '.tmp';
368+
$tmpfile = $tmpdir . $tmpfile . '.' . $ext;
367369
$fp = fopen( $tmpfile, 'xb' );
368370
if ( ! $fp && is_writable( $tmpdir ) && file_exists( $tmpfile ) ) {
369371
$tmpfile = '';
@@ -772,6 +774,31 @@ function trailingslashit( $string ) {
772774
return rtrim( $string, '/\\' ) . '/';
773775
}
774776

777+
/**
778+
* Normalize a filesystem path.
779+
*
780+
* On Windows systems, replaces backslashes with forward slashes
781+
* and forces upper-case drive letters.
782+
* Allows for two leading slashes for Windows network shares, but
783+
* ensures that all other duplicate slashes are reduced to a single one.
784+
* Ensures upper-case drive letters on Windows systems.
785+
*
786+
* @access public
787+
* @category System
788+
*
789+
* @param string $path Path to normalize.
790+
* @return string Normalized path.
791+
*/
792+
function normalize_path( $path ) {
793+
$path = str_replace( '\\', '/', $path );
794+
$path = preg_replace( '|(?<=.)/+|', '/', $path );
795+
if ( ':' === substr( $path, 1, 1 ) ) {
796+
$path = ucfirst( $path );
797+
}
798+
return $path;
799+
}
800+
801+
775802
/**
776803
* Convert Windows EOLs to *nix.
777804
*
@@ -1307,6 +1334,7 @@ function get_php_binary() {
13071334

13081335
// Available since PHP 5.4.
13091336
if ( defined( 'PHP_BINARY' ) ) {
1337+
// @codingStandardsIgnoreLine
13101338
return PHP_BINARY;
13111339
}
13121340

src/WP_CLI/SearchReplacer.php

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace WP_CLI;
44

55
use ArrayObject;
6+
use Exception;
67

78
class SearchReplacer {
89

@@ -117,7 +118,17 @@ private function _run( $data, $serialised, $recursion_level = 0, $visited_data =
117118
$search_regex .= $this->from;
118119
$search_regex .= $this->regex_delimiter;
119120
$search_regex .= $this->regex_flags;
120-
$data = preg_replace( $search_regex, $this->to, $data );
121+
122+
$result = preg_replace( $search_regex, $this->to, $data );
123+
if ( null === $result || PREG_NO_ERROR !== preg_last_error() ) {
124+
\WP_CLI::warning(
125+
sprintf(
126+
'The provided regular expression threw a PCRE error - %s',
127+
$this->preg_error_message( $result )
128+
)
129+
);
130+
}
131+
$data = $result;
121132
} else {
122133
$data = str_replace( $this->from, $this->to, $data );
123134
}
@@ -150,5 +161,21 @@ public function get_log_data() {
150161
public function clear_log_data() {
151162
$this->log_data = array();
152163
}
164+
165+
/**
166+
* Get the PCRE error constant name from an error value.
167+
*
168+
* @param integer $error Error code.
169+
* @return string Error constant name.
170+
*/
171+
private function preg_error_message( $error ) {
172+
$constants = get_defined_constants( true );
173+
if ( ! array_key_exists( 'pcre', $constants ) ) {
174+
return '<unknown error>';
175+
}
176+
177+
$names = array_flip( $constants['pcre'] );
178+
return isset( $names[ $error ] ) ? $names[ $error ] : '<unknown error>';
179+
}
153180
}
154181

0 commit comments

Comments
 (0)