@@ -32,6 +32,11 @@ class SearchReplacer {
3232 */
3333 private $ recurse_objects ;
3434
35+ /**
36+ * @var bool
37+ */
38+ private $ recurse_keys ;
39+
3540 /**
3641 * @var bool
3742 */
@@ -81,11 +86,13 @@ class SearchReplacer {
8186 * @param string $regex_delimiter Delimiter for regular expression.
8287 * @param bool $logging Whether logging.
8388 * @param integer $regex_limit The maximum possible replacements for each pattern in each subject string.
89+ * @param bool $recurse_keys Should array keys and object property names be recursively replaced?
8490 */
85- public function __construct ( $ from , $ to , $ recurse_objects = false , $ regex = false , $ regex_flags = '' , $ regex_delimiter = '/ ' , $ logging = false , $ regex_limit = -1 ) {
91+ public function __construct ( $ from , $ to , $ recurse_objects = false , $ regex = false , $ regex_flags = '' , $ regex_delimiter = '/ ' , $ logging = false , $ regex_limit = -1 , $ recurse_keys = false ) {
8692 $ this ->from = $ from ;
8793 $ this ->to = $ to ;
8894 $ this ->recurse_objects = $ recurse_objects ;
95+ $ this ->recurse_keys = $ recurse_keys ;
8996 $ this ->regex = $ regex ;
9097 $ this ->regex_flags = $ regex_flags ;
9198 $ this ->regex_delimiter = $ regex_delimiter ;
@@ -180,10 +187,17 @@ private function run_recursively( $data, $serialised, $recursion_level = 0, $vis
180187 if ( false !== $ unserialized ) {
181188 $ data = $ this ->run_recursively ( $ unserialized , true , $ recursion_level + 1 );
182189 } elseif ( is_array ( $ data ) ) {
183- $ keys = array_keys ( $ data );
190+ $ keys = array_keys ( $ data );
191+ $ replaced = array ();
184192 foreach ( $ keys as $ key ) {
185- $ data [ $ key ] = $ this ->run_recursively ( $ data [ $ key ], false , $ recursion_level + 1 , $ visited_data );
193+ $ value = $ this ->run_recursively ( $ data [ $ key ], false , $ recursion_level + 1 , $ visited_data );
194+ $ new_key = $ key ;
195+ if ( $ this ->recurse_keys && is_string ( $ key ) ) {
196+ $ new_key = $ this ->replace_string ( $ key );
197+ }
198+ $ replaced [ $ new_key ] = $ value ;
186199 }
200+ $ data = $ replaced ;
187201 } elseif ( $ this ->recurse_objects && ( is_object ( $ data ) || $ data instanceof \__PHP_Incomplete_Class ) ) {
188202 if ( $ data instanceof \__PHP_Incomplete_Class ) {
189203 $ array = new ArrayObject ( $ data );
@@ -196,12 +210,26 @@ private function run_recursively( $data, $serialised, $recursion_level = 0, $vis
196210 } else {
197211 try {
198212 $ is_array_access = $ data instanceof \ArrayAccess;
213+ $ properties = array ();
199214 foreach ( $ data as $ key => $ value ) {
200- $ value = $ this ->run_recursively ( $ value , false , $ recursion_level + 1 , $ visited_data );
215+ $ properties [ $ key ] = $ value ;
216+ }
217+ foreach ( $ properties as $ key => $ value ) {
218+ $ value = $ this ->run_recursively ( $ value , false , $ recursion_level + 1 , $ visited_data );
219+ $ new_key = $ key ;
220+ if ( $ this ->recurse_keys && is_string ( $ key ) ) {
221+ $ new_key = $ this ->replace_string ( $ key );
222+ }
201223 if ( $ is_array_access ) {
202- $ data [ $ key ] = $ value ;
224+ if ( $ new_key !== $ key ) {
225+ unset( $ data [ $ key ] );
226+ }
227+ $ data [ $ new_key ] = $ value ;
203228 } else {
204- $ data ->$ key = $ value ;
229+ if ( $ new_key !== $ key ) {
230+ unset( $ data ->$ key );
231+ }
232+ $ data ->$ new_key = $ value ;
205233 }
206234 }
207235 } catch ( \Error $ exception ) { // phpcs:ignore PHPCompatibility.Classes.NewClasses.errorFound
@@ -220,34 +248,7 @@ private function run_recursively( $data, $serialised, $recursion_level = 0, $vis
220248 }
221249 }
222250 } elseif ( is_string ( $ data ) ) {
223- if ( $ this ->logging ) {
224- $ old_data = $ data ;
225- }
226- if ( $ this ->regex ) {
227- $ search_regex = $ this ->regex_delimiter ;
228- $ search_regex .= $ this ->from ;
229- $ search_regex .= $ this ->regex_delimiter ;
230- $ search_regex .= $ this ->regex_flags ;
231-
232- $ result = preg_replace ( $ search_regex , $ this ->to , $ data , $ this ->regex_limit );
233- if ( null === $ result || PREG_NO_ERROR !== preg_last_error () ) {
234- \WP_CLI ::warning (
235- sprintf (
236- 'The provided regular expression threw a PCRE error - %s ' ,
237- $ this ->preg_error_message ( $ result )
238- )
239- );
240- }
241- $ data = $ result ;
242- } else {
243- $ data = str_replace ( $ this ->from , $ this ->to , $ data );
244- if ( $ this ->from_json !== $ this ->from ) {
245- $ data = str_replace ( $ this ->from_json , $ this ->to_json , $ data );
246- }
247- }
248- if ( $ this ->logging && $ old_data !== $ data ) {
249- $ this ->log_data [] = $ old_data ;
250- }
251+ $ data = $ this ->replace_string ( $ data );
251252 }
252253
253254 if ( $ serialised ) {
@@ -260,6 +261,48 @@ private function run_recursively( $data, $serialised, $recursion_level = 0, $vis
260261 return $ data ;
261262 }
262263
264+ /**
265+ * Replace occurrences of the search string within a single string.
266+ *
267+ * Used for values as well as array keys and object property names when
268+ * key recursion is enabled.
269+ *
270+ * @param string $data The string to operate on.
271+ * @return string The string with all replacements made as needed.
272+ */
273+ private function replace_string ( $ data ) {
274+ if ( $ this ->logging ) {
275+ $ old_data = $ data ;
276+ }
277+ if ( $ this ->regex ) {
278+ $ search_regex = $ this ->regex_delimiter ;
279+ $ search_regex .= $ this ->from ;
280+ $ search_regex .= $ this ->regex_delimiter ;
281+ $ search_regex .= $ this ->regex_flags ;
282+
283+ $ result = preg_replace ( $ search_regex , $ this ->to , $ data , $ this ->regex_limit );
284+ if ( null === $ result || PREG_NO_ERROR !== preg_last_error () ) {
285+ \WP_CLI ::warning (
286+ sprintf (
287+ 'The provided regular expression threw a PCRE error - %s ' ,
288+ $ this ->preg_error_message ( $ result )
289+ )
290+ );
291+ }
292+ $ data = $ result ;
293+ } else {
294+ $ data = str_replace ( $ this ->from , $ this ->to , $ data );
295+ if ( $ this ->from_json !== $ this ->from ) {
296+ $ data = str_replace ( $ this ->from_json , $ this ->to_json , $ data );
297+ }
298+ }
299+ if ( $ this ->logging && $ old_data !== $ data ) {
300+ $ this ->log_data [] = $ old_data ;
301+ }
302+
303+ return $ data ;
304+ }
305+
263306 /**
264307 * Gets existing data saved for this run when logging.
265308 * @return string[] Array of data strings, prior to replacements.
0 commit comments