Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
ea90c38
Added --where flag to finely select rows aimed for replacement
Oct 17, 2025
0071c86
phpcbf + features split
Oct 19, 2025
82cbe76
phpstan
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
Nov 12, 2025
363b893
misc fixes
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
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
187 changes: 187 additions & 0 deletions features/search-replace-callback.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,187 @@
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 ) {
// 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
"""
106 changes: 106 additions & 0 deletions features/search-replace-revisions.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
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 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

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

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

# Verify draft post was NOT changed
When I run `wp post get {DRAFT_ID} --field=title`
Then STDOUT should be:
"""
Draft foo
"""

# Verify published post meta was changed
When I run `wp post meta get {PUBLISHED_ID} test_key`
Then STDOUT should be:
"""
published_bar_meta
"""

# Verify draft post meta was NOT changed
When I run `wp post meta get {DRAFT_ID} test_key`
Then STDOUT should be:
"""
draft_foo_meta
"""

@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='Draft fooooo' --post_name=2 --post_status='draft' --porcelain`
Then save STDOUT as {DRAFT_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 {DRAFT_ID} --field=title`
Then STDOUT should be:
"""
Draft 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_name='draftslug' --post_status='draft' --porcelain`
And save STDOUT as {DRAFT_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 {DRAFT_ID} --field=title`
Then STDOUT should be:
"""
Test foo456
"""
Loading
Loading