Skip to content
Open
Show file tree
Hide file tree
Changes from 30 commits
Commits
Show all changes
44 commits
Select commit Hold shift + click to select a range
ea90c38
Added --where flag to finely select rows aimed for replacement
drzraf Oct 17, 2025
0071c86
phpcbf + features split
drzraf Oct 19, 2025
82cbe76
phpstan
drzraf Oct 19, 2025
faef60f
PHPStan fix
swissspidy Nov 4, 2025
e847138
Update src/Search_Replace_Command.php
drzraf Nov 11, 2025
76c2296
Update src/Search_Replace_Command.php
drzraf Nov 11, 2025
c4fe56c
Update src/Search_Replace_Command.php
drzraf Nov 11, 2025
735fedf
Update src/Search_Replace_Command.php
drzraf Nov 11, 2025
cf89ef5
Update src/Search_Replace_Command.php
drzraf Nov 11, 2025
8b45d7e
Update src/WP_CLI/SearchReplacer.php
drzraf Nov 11, 2025
eb1cc91
Update src/Search_Replace_Command.php
drzraf Nov 11, 2025
21c0b81
Update src/WP_CLI/SearchReplacer.php
drzraf Nov 11, 2025
05041ab
Add some docblocks
swissspidy Nov 11, 2025
c5e06bd
various fixes
drzraf Nov 12, 2025
363b893
misc fixes
drzraf Nov 12, 2025
a821c3d
Lint fixes
swissspidy Nov 12, 2025
001381a
Update src/WP_CLI/SearchReplacer.php
drzraf Nov 12, 2025
5f8498c
Update src/Search_Replace_Command.php
drzraf Nov 12, 2025
816a51a
Update src/Search_Replace_Command.php
drzraf Nov 12, 2025
7a66e18
Merge branch 'main' into callback-104
swissspidy Jan 22, 2026
6176cb0
Lint fix
swissspidy Jan 22, 2026
b71179a
Merge branch 'main' into callback-104
swissspidy Mar 12, 2026
2a5c428
Fix issue after merge
swissspidy Mar 12, 2026
00d2fc7
Array fix
swissspidy Mar 12, 2026
37c8206
Lint fix
swissspidy Mar 12, 2026
47f9419
Fix variable name after merge
swissspidy Mar 12, 2026
7eb3bb7
Default to null
swissspidy Mar 12, 2026
8d4954a
Pass opts
swissspidy Mar 12, 2026
a3176e6
Merge branch 'main' into callback-104
swissspidy Nov 25, 2026
6003c54
Fix --where, --no-revisions and --callback edge cases
swissspidy Nov 25, 2026
9818c3d
Run --callback even when the search and replacement values are identical
swissspidy Nov 26, 2026
358342a
Reject malformed --where specifications instead of ignoring them
swissspidy Nov 26, 2026
5f3581a
Split --where specifications only on semicolons outside quoted literals
swissspidy Nov 26, 2026
9369699
Keep a --where condition of '0' instead of dropping it
swissspidy Nov 26, 2026
471c42b
Apply --callback to --export
swissspidy Nov 26, 2026
42c9851
Log the value --callback returned instead of an ordinary replacement
swissspidy Nov 26, 2026
894ee48
Apply --callback to a value of '0' in an export
swissspidy Nov 26, 2026
8b1b0bd
Select the --where rows once per table, before any column is replaced
swissspidy Nov 26, 2026
5702dfc
Hold the --where rows in a temporary table instead of in PHP
swissspidy Nov 26, 2026
1918289
Compare string primary keys as strings when updating a row with --pre…
swissspidy Nov 26, 2026
9df13d2
Keep the primary key prefixes in the --where temporary table
swissspidy Nov 26, 2026
6333cb6
Flush the object cache before checking the title in the SQLite --wher…
swissspidy Nov 26, 2026
2cf82b0
Read the primary key prefixes as arrays
swissspidy Nov 26, 2026
51f739f
Read the title from the database in the SQLite --where scenario
swissspidy Nov 26, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
215 changes: 215 additions & 0 deletions features/search-replace-callback.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,215 @@
Feature: Test search-replace --callback option

@require-mysql
Scenario: Search replace with callback function
Given a WP install
And a callback-function.php file:
"""
<?php
function test_callback( $data, $replacement, $search_regex ) {
// Assert that search_regex is null when not using --regex
if ( $search_regex !== null ) {
throw new Exception( 'Expected $search_regex to be null without --regex, got: ' . var_export( $search_regex, true ) );
}
return str_replace( 'foo', strtoupper( $replacement ), $data );
}
"""
And I run `wp post create --post_title='foo bar' --post_content='foo content' --porcelain`
And save STDOUT as {POST_ID}

When I run `wp search-replace 'foo' 'baz' wp_posts --callback='test_callback' --precise --require=callback-function.php`
Then STDOUT should contain:
"""
Success: Made 2 replacements.
"""
And STDOUT should be a table containing rows:
| Table | Column | Replacements | Type |
| wp_posts | post_title | 1 | PHP |
| wp_posts | post_content | 1 | PHP |

When I run `wp post get {POST_ID} --field=title`
Then STDOUT should be:
"""
BAZ bar
"""

When I run `wp post get {POST_ID} --field=content`
Then STDOUT should be:
"""
BAZ content
"""

@require-mysql
Scenario: Search replace with callback function and regex
Given a WP install
And a callback-regex.php file:
"""
<?php
function regex_callback( $data, $replacement, $search_regex ) {
// Assert that search_regex is provided when using --regex
if ( $search_regex === null ) {
throw new Exception( 'Expected $search_regex to be set with --regex, got null' );
}
if ( !is_string( $search_regex ) ) {
throw new Exception( 'Expected $search_regex to be a string, got: ' . gettype( $search_regex ) );
}
// Verify it's a valid regex pattern
if ( @preg_match( $search_regex, '' ) === false ) {
throw new Exception( '$search_regex is not a valid regex pattern: ' . $search_regex );
}
// Replace matched digits with their square
return preg_replace_callback( $search_regex, function( $matches ) {
$num = (int)$matches[1];
return $num * $num;
}, $data );
}
"""
And I run `wp post create --post_title='Number 5 test' --porcelain`
And save STDOUT as {POST_ID}

When I run `wp search-replace 'Number ([0-9]+)' 'ignored' --regex --callback='regex_callback' --precise --require=callback-regex.php`
Then STDOUT should contain:
"""
Success: Made 1 replacement.
"""

When I run `wp post get {POST_ID} --field=title`
Then STDOUT should be:
"""
25 test
"""

@require-mysql
Scenario: Search replace with callback function that doesn't exist
Given a WP install

When I try `wp search-replace 'foo' 'bar' --callback='nonexistent_function' --precise`
Then STDERR should be:
"""
Error: The callback function does not exist. Skipping operation.
"""
And the return code should be 1

@require-mysql
Scenario: Search replace with callback requires precise mode
Given a WP install
And a callback-function.php file:
"""
<?php
function test_callback( $data, $replacement ) {
return str_replace( 'foo', strtoupper( $replacement ), $data );
}
"""

When I try `wp search-replace 'foo' 'bar' --callback='test_callback' --no-precise --require=callback-function.php`
Then STDERR should be:
"""
Error: PHP is required to execute a callback function. --no-precise cannot be set.
"""
And the return code should be 1

@require-mysql
Scenario: Callback function with regex search parameter
Given a WP install
And a regex-aware-callback.php file:
"""
<?php
function regex_aware_callback( $data, $replacement, $search_regex ) {
// If regex is provided, use preg_replace, otherwise use str_replace
if ( !empty( $search_regex ) ) {
return preg_replace( $search_regex, $replacement . '_regex', $data );
}
return str_replace( 'foo', $replacement, $data );
}
"""
And I run `wp post create --post_title='foo title' --post_content='foo content' --porcelain`
And save STDOUT as {POST_ID}

When I run `wp search-replace 'foo' 'bar' wp_posts --callback='regex_aware_callback' --precise --require=regex-aware-callback.php`
Then STDOUT should contain:
"""
Success: Made 2 replacements.
"""

When I run `wp post get {POST_ID} --field=title`
Then STDOUT should be:
"""
bar title
"""

When I run `wp post get {POST_ID} --field=content`
Then STDOUT should be:
"""
bar content
"""

@require-mysql
Scenario: Callback function receives opts parameter with table and column context
Given a WP install
And a callback-opts.php file:
"""
<?php
function opts_callback( $data, $replacement, $search_regex, $opts ) {
if ( empty( $opts['table'] ) || empty( $opts['col'] ) || empty( $opts['key']['ID'] ) ) {
throw new Exception( 'Expected $opts to have the table, the column and the primary key values, got: ' . var_export( $opts, true ) );
}
// Only verify opts when we have a match for content columns
if ( strpos( $data, 'foo' ) !== false && isset( $opts['col'] ) && $opts['col'] === 'post_content' ) {
// Verify opts parameter exists and has expected structure
if ( !is_array( $opts ) ) {
throw new Exception( 'Expected $opts to be an array, got: ' . gettype( $opts ) );
}
if ( !isset( $opts['table'] ) ) {
throw new Exception( 'Expected $opts["table"] to be set' );
}
// Verify we're processing wp_posts table
if ( strpos( $opts['table'], 'posts' ) === false ) {
throw new Exception( 'Expected table to contain "posts", got: ' . $opts['table'] );
}
return str_replace( 'foo', $replacement, $data );
} elseif ( strpos( $data, 'foo' ) !== false ) {
// For other columns, just do the replacement without assertions
return str_replace( 'foo', $replacement, $data );
}
return $data;
}
"""
And I run `wp post create --post_title='foo title' --post_content='foo content here' --porcelain`
And save STDOUT as {POST_ID}

When I run `wp search-replace 'foo' 'verified' wp_posts --include-columns=post_content --callback='opts_callback' --precise --require=callback-opts.php`
Then STDOUT should contain:
"""
Success: Made 1 replacement.
"""

When I run `wp post get {POST_ID} --field=content`
Then STDOUT should be:
"""
verified content here
"""

@require-mysql
Scenario: Callback infers --precise when it is not passed
Given a WP install
And a callback-function.php file:
"""
<?php
function test_callback( $data, $replacement ) {
return str_replace( 'foo', strtoupper( $replacement ), $data );
}
"""
And I run `wp post create --post_title='foo bar' --porcelain`
And save STDOUT as {POST_ID}

When I run `wp search-replace 'foo' 'baz' wp_posts --include-columns=post_title --callback='test_callback' --require=callback-function.php`
Then STDOUT should contain:
"""
Success: Made 1 replacement.
"""

When I run `wp post get {POST_ID} --field=title`
Then STDOUT should be:
"""
BAZ bar
"""
165 changes: 165 additions & 0 deletions features/search-replace-revisions.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,165 @@
Feature: Test search-replace --revisions option

@require-mysql
Scenario: Search replace without revisions (--no-revisions)
Given a WP install

When I run `wp post create --post_title='Published foo' --post_name='1' --post_status='publish' --porcelain`
Then save STDOUT as {PUBLISHED_ID}

When I run `wp post create --post_title='Draft foo' --post_status='draft' --porcelain`
Then save STDOUT as {DRAFT_ID}

When I run `wp post create --post_title='Revision foo' --post_type='revision' --post_status='inherit' --post_parent={PUBLISHED_ID} --post_name='{PUBLISHED_ID}-revision-v1' --porcelain`
Then save STDOUT as {REVISION_ID}

When I run `wp post meta add {PUBLISHED_ID} test_key 'published_foo_meta'`
Then STDOUT should not be empty

When I run `wp post meta add {DRAFT_ID} test_key 'draft_foo_meta'`
Then STDOUT should not be empty

# add_post_meta() would store the meta on the parent post, not the revision.
When I run `wp eval 'add_metadata( "post", {REVISION_ID}, "test_key", "revision_foo_meta" );'`
And I run `wp option add test_option 'option_foo'`
And I run `wp search-replace 'foo' 'bar' --no-revisions`
Then STDOUT should contain:
"""
Success: Made
"""

When I run `wp post get {PUBLISHED_ID} --field=title`
Then STDOUT should be:
"""
Published bar
"""

# Drafts are not revisions
When I run `wp post get {DRAFT_ID} --field=title`
Then STDOUT should be:
"""
Draft bar
"""

When I run `wp post get {REVISION_ID} --field=title`
Then STDOUT should be:
"""
Revision foo
"""

When I run `wp post meta get {PUBLISHED_ID} test_key`
Then STDOUT should be:
"""
published_bar_meta
"""

When I run `wp post meta get {DRAFT_ID} test_key`
Then STDOUT should be:
"""
draft_bar_meta
"""

When I run `wp eval 'echo get_metadata( "post", {REVISION_ID}, "test_key", true );'`
Then STDOUT should be:
"""
revision_foo_meta
"""

# Tables other than posts and postmeta are still processed
When I run `wp option get test_option`
Then STDOUT should be:
"""
option_bar
"""

@require-mysql
Scenario: Search replace with default revisions behavior
Given a WP install

When I run `wp post create --post_title='Published fooooo' --post_name=1 --post_status='publish' --porcelain`
Then save STDOUT as {PUBLISHED_ID}

When I run `wp post create --post_title='Revision fooooo' --post_type='revision' --post_status='inherit' --post_parent={PUBLISHED_ID} --post_name='{PUBLISHED_ID}-revision-v1' --porcelain`
Then save STDOUT as {REVISION_ID}

# With default behavior (--revisions=true), both should be changed
When I run `wp search-replace 'fooooo' 'baz'`
Then STDOUT should contain:
"""
Success: Made 2 replacements.
"""

When I run `wp post get {PUBLISHED_ID} --field=title`
Then STDOUT should be:
"""
Published baz
"""

When I run `wp post get {REVISION_ID} --field=title`
Then STDOUT should be:
"""
Revision baz
"""

@require-mysql
Scenario: Combining no-revisions with regex
Given a WP install
And I run `wp post create --post_title='Test foo123' --post_name='pubslug' --post_status='publish' --porcelain`
And save STDOUT as {PUB_ID}
And I run `wp post create --post_title='Test foo456' --post_type='revision' --post_status='inherit' --post_parent={PUB_ID} --post_name='{PUB_ID}-revision-v1' --porcelain`
And save STDOUT as {REVISION_ID}

When I run `wp search-replace 'foo[0-9]+' 'bar999' --regex --no-revisions`
Then STDOUT should contain:
"""
Success: Made 1 replacement.
"""

When I run `wp post get {PUB_ID} --field=title`
Then STDOUT should be:
"""
Test bar999
"""

When I run `wp post get {REVISION_ID} --field=title`
Then STDOUT should be:
"""
Test foo456
"""

@require-mysql
Scenario: Excluding revisions on all sites of a network
Given a WP multisite install
And I run `wp site create --slug=second --porcelain`
And I run `wp post create --url=example.com/second --post_title='Second foo' --post_name='secondslug' --post_status='publish' --porcelain`
And save STDOUT as {POST_ID}
And I run `wp post create --url=example.com/second --post_title='Second revision foo' --post_type='revision' --post_status='inherit' --post_parent={POST_ID} --post_name='{POST_ID}-revision-v1' --porcelain`
And save STDOUT as {REVISION_ID}

When I run `wp search-replace 'foo' 'bar' --network --no-revisions`
Then STDOUT should contain:
"""
Success:
"""

When I run `wp post get {POST_ID} --url=example.com/second --field=title`
Then STDOUT should be:
"""
Second bar
"""

When I run `wp post get {REVISION_ID} --url=example.com/second --field=title`
Then STDOUT should be:
"""
Second revision foo
"""

Scenario: Excluding revisions cannot be combined with an export
Given a WP install

When I try `wp search-replace 'foo' 'bar' --no-revisions --export=out.sql`
Then STDERR should be:
"""
Error: You cannot supply --where or --no-revisions together with --export.
"""
And the return code should be 1
Loading