search-replace-command icon indicating copy to clipboard operation
search-replace-command copied to clipboard

Feature: Call user function with --callback flag

Open brandonkal opened this issue 7 years ago • 10 comments

This PR adds the ability to run a user function on search results if a match is found. In my case, I needed this to change image src urls based on attachment id and size where a hash is a required part of the url.

brandonkal avatar Dec 15 '18 04:12 brandonkal

I am hoping someone is willing and able to test this out and release it in or let me know what needs to change. I am glad the CI tests are passing now. I am not able to test this locally because it appears to have conflicts with the search-replace that is installed by default.

So I am seeing this even though I am passing 9 args in the new version:

Uncaught ArgumentCountError: Too few arguments to function WP_CLI\SearchReplacer::__construct(),
8 passed in phar:///usr/local/bin/wp/vendor/wp-cli/search-replace-command/src/Search_Replace_Command.php on line 489
and exactly 9 expected in
/home/brandonkal/.wp-cli/packages/vendor/wp-cli/search-replace-command/src/WP_CLI/SearchReplacer.php:31

I installed my fork as explained in the README:

wp package install [email protected]:brandonkal/search-replace-command.git

Thanks

brandonkal avatar Dec 15 '18 09:12 brandonkal

Try updating to WP-CLI nightly

swissspidy avatar Dec 15 '18 10:12 swissspidy

Installing the nightly version fixed the issue. Everything looks good so far but I will report back after some more thorough testing.

brandonkal avatar Dec 15 '18 10:12 brandonkal

Tested thoroughly and everything looks good to go!

brandonkal avatar Dec 15 '18 14:12 brandonkal

@brandonkal The PR looks good at first glance. We still need to add functional tests through Behat. Are you able to add these?

schlessera avatar Feb 21 '19 08:02 schlessera

@brandonkal Are you interested in providing the required Behat tests to move this over the finish line?

schlessera avatar Apr 01 '19 12:04 schlessera

@brandonkal Gentle ping to see if you are up for adding the needed tests...?

schlessera avatar Jul 24 '19 21:07 schlessera

Thank you for the reminder @schlessera. I forgot about this. I am not very familiar with Behat and my WordPress projects are currently on hold so I expect it will be a few months before I can look into this again. If anyone is willing to add the tests in the meantime, I've allowed edits on this PR.

brandonkal avatar Jul 25 '19 00:07 brandonkal

@wp-cli/committers There might be a much easier approach here: call apply_filters() if it exists. What do you think?

danielbachhuber avatar Jul 07 '22 15:07 danielbachhuber

I implemented callback support within #128 It relies upon callback's name being passed as an option, rely upon call_user_func and should easily made a filter.

From a quick look, a filter sounds better because:

  • Multiple callbacks can be run
  • It allows plugin's own code to rely upon this very filter to implement "built-in / handy / predefined callbacks"
  • It avoids passing callback's name down the function stack
  • It does not require the user to write more boilerplate code to implement such a callback than to define a top-level function.

drzraf avatar Jul 11 '22 02:07 drzraf

Proceeding with https://github.com/wp-cli/wp-cli/issues/5594 for this repository. I've captured this PR to https://gist.github.com/danielbachhuber/f4bb0f6caaebc2c8a6390adc19cd1f50 in case this PR is auto-closed or broken in some way.

danielbachhuber avatar Nov 18 '22 17:11 danielbachhuber