Skip to content

Conversation

@drzraf
Copy link

@drzraf drzraf commented Oct 17, 2025

Replacement callback & row-based filtering (include/exclude posts revisions)

  • Replacing revisions is confusing (total replacements count) and annoying
  • Specifying rows/columns/custom --where expression has been a long requested feature (for more than 8 years)
  • Extra flexibility regarding replacements (custom PHP callback) has also been requested multiple times

@drzraf drzraf requested a review from a team as a code owner October 17, 2025 03:17
@codecov
Copy link

codecov bot commented Oct 17, 2025

Codecov Report

❌ Patch coverage is 94.59459% with 6 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
src/WP_CLI/SearchReplacer.php 77.27% 5 Missing ⚠️
src/Search_Replace_Command.php 98.87% 1 Missing ⚠️

📢 Thoughts on this report? Let us know!

@swissspidy
Copy link
Member

swissspidy commented Oct 18, 2025

Thanks for your PR!

Would it be possible to split it up into 3 individual ones, one per new argument? That would make reviews much easier.

There could also be one .feature file per new argument for example. search-replace-new-options.feature doesn't really make sense (define new... what if we add even newer arguments two months from now 😄)

PS. Notice the lint failure

@drzraf
Copy link
Author

drzraf commented Oct 19, 2025

  • Splitting the *.features: sure. I just did in this later commit (+ phpcs)

  • But splitting logically the actual code changes seems over my forces.

  1. It would have been humanly impossible for me develop them separately and keep them in sync one with the other and all against upstream branch across these many years and actually use them (maintaining a combined branch).
  2. I think this was my last burst of motivation : if anything goes out of sync again, I'm too likely to just definitely give up

I understand it's a significant diff on core code, but: 175 insertions(+), 43 deletions(-) doesn't seem that much considering the amount of capabilities these new options brings to search-replace

  • I don't understand the phpstan error:
    php -r '$r["post"]["*"][] = "c"; print_r($r);' is valid.

There is also at least one imperfection I'm aware of:

--where <table>:<col>:<cond> is actually adding <col> restrictions globally (not actually scoped to said table (reusing the logic of --include-columns). Moving the restriction more precisely, within the search and replace inner loops is probably doable, but would be even more complex (to write/assess and review).
It's better left to a future improvement (and the current logic is not breaking anything anyway)

@drzraf
Copy link
Author

drzraf commented Nov 4, 2025

ping

@swissspidy
Copy link
Member

Heya, apologies for missing your previous reply. Thanks a lot for your effort, it's truly appreciated!

@swissspidy swissspidy requested a review from Copilot November 11, 2025 13:58
Copilot finished reviewing on behalf of swissspidy November 11, 2025 14:06
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR adds three major features to the WP-CLI search-replace command: the ability to filter rows using WHERE clauses (--where), skip revisions (--no-revisions), and execute custom callback functions during replacements (--callback). These long-requested features provide users with more precise control over search-replace operations.

  • Added --where flag to filter rows by SQL conditions, supporting multiple table/column specifications
  • Added --revisions/--no-revisions flag to exclude draft/revision posts from replacements
  • Added --callback flag to execute user-defined PHP functions for custom replacement logic

Reviewed Changes

Copilot reviewed 5 out of 5 changed files in this pull request and generated 21 comments.

Show a summary per file
File Description
src/WP_CLI/SearchReplacer.php Added callback parameter to constructor and run methods; implemented callback invocation logic with error handling for both regex and non-regex replacements
src/Search_Replace_Command.php Added --where, --revisions, and --callback command-line options; implemented WHERE clause parsing, revision filtering, and callback validation; integrated custom clauses into SQL queries
features/search-replace-where.feature Test scenarios for the new --where flag covering single/multiple tables, subqueries, dry-run mode, and column restrictions
features/search-replace-revisions.feature Test scenarios for the new --revisions flag verifying published vs draft post filtering with and without regex
features/search-replace-callback.feature Test scenarios for the new --callback flag including custom functions, regex callbacks, error handling, and parameter validation

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

drzraf and others added 15 commits November 11, 2025 14:57
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Filter by post types and post status in post table Pass current $col and $primary_keys down to the SearchReplacer

2 participants