Skip to content

Commit 9818c3d

Browse files
swissspidyclaude
andcommitted
Run --callback even when the search and replacement values are identical
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01UWTd5pwpi2xQfDA6mUYkkx
1 parent 6003c54 commit 9818c3d

3 files changed

Lines changed: 46 additions & 3 deletions

File tree

‎features/search-replace-callback.feature‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,3 +213,29 @@ Feature: Test search-replace --callback option
213213
"""
214214
BAZ bar
215215
"""
216+
217+
@require-mysql
218+
Scenario: Callback runs even when the search and replacement values are identical
219+
Given a WP install
220+
And a callback-upper.php file:
221+
"""
222+
<?php
223+
function upper_callback( $data, $replacement ) {
224+
return str_replace( 'foo', strtoupper( $replacement ), $data );
225+
}
226+
"""
227+
And I run `wp post create --post_title='foo bar' --porcelain`
228+
And save STDOUT as {POST_ID}
229+
230+
When I run `wp search-replace 'foo' 'foo' wp_posts --include-columns=post_title --callback='upper_callback' --require=callback-upper.php`
231+
Then STDOUT should contain:
232+
"""
233+
Success: Made 1 replacement.
234+
"""
235+
And STDERR should be empty
236+
237+
When I run `wp post get {POST_ID} --field=title`
238+
Then STDOUT should be:
239+
"""
240+
FOO bar
241+
"""

‎src/Search_Replace_Command.php‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -439,7 +439,8 @@ public function __invoke( $args, $assoc_args ) {
439439
$this->where = $this->develop_where_specs( Utils\get_flag_value( $assoc_args, 'where' ) );
440440
$this->exclude_revisions = ! Utils\get_flag_value( $assoc_args, 'revisions', true );
441441

442-
if ( $old === $new && ! $this->regex ) {
442+
// A callback may still change the matching strings, so only skip without one.
443+
if ( $old === $new && ! $this->regex && ! $this->callback ) {
443444
WP_CLI::warning( "Replacement value '{$old}' is identical to search value '{$new}'. Skipping operation." );
444445
exit;
445446
}

‎src/WP_CLI/SearchReplacer.php‎

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,11 @@ class SearchReplacer {
6262
*/
6363
private $log_data = [];
6464

65+
/**
66+
* @var string[]
67+
*/
68+
private $log_new_data = [];
69+
6570
/**
6671
* @var int
6772
*/
@@ -287,7 +292,8 @@ private function run_recursively( $data, $serialised, $recursion_level = 0, $vis
287292

288293
$data = $result;
289294
if ( $this->logging && $old_data !== $data ) {
290-
$this->log_data[] = $old_data;
295+
$this->log_data[] = $old_data;
296+
$this->log_new_data[] = $data;
291297
}
292298
}
293299

@@ -309,11 +315,21 @@ public function get_log_data() {
309315
return $this->log_data;
310316
}
311317

318+
/**
319+
* Gets the data strings after replacements saved for this run when logging,
320+
* in the same order as get_log_data().
321+
* @return string[] Array of data strings, after replacements.
322+
*/
323+
public function get_log_new_data() {
324+
return $this->log_new_data;
325+
}
326+
312327
/**
313328
* Clears data stored for logging.
314329
*/
315330
public function clear_log_data() {
316-
$this->log_data = array();
331+
$this->log_data = array();
332+
$this->log_new_data = array();
317333
}
318334

319335
/**

0 commit comments

Comments
 (0)