Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support arrays/JSON in pluck and patch #460

Closed
wants to merge 7 commits into from
Closed

Support arrays/JSON in pluck and patch #460

wants to merge 7 commits into from

Conversation

BlairCooper
Copy link

@BlairCooper BlairCooper commented Feb 3, 2024

Add support to for serialized arrays and JSON to both pluck and patch. Add additional examples and a note about the stdin blocking issue with patch.

Fixes #459

Add support to for serialized arrays and JSON to both plick and patch.
Add additional examples and a note about the stdin blocking issue with
patch.
@BlairCooper BlairCooper requested a review from a team as a code owner February 3, 2024 06:05
A serialized version of an array as a response is likely more useful
than a var_dump quivilent.
@swissspidy swissspidy changed the title Fixes #459 Support arrays/JSON in pluck and patch Support arrays/JSON in pluck and patch Feb 4, 2024
Copy link
Member

@danielbachhuber danielbachhuber left a comment

Choose a reason for hiding this comment

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

Thanks for the pull request, @BlairCooper !

I don't think the maybe_unserialize() is necessary. get_option() is already unserializing for us.

I wasn't able to reproduce your original issue:

$ wp shell
wp> update_option( 'my_option', array( 'myKey' => 'myValue' ) );
=> bool(true)
$ wp option pluck my_option myKey
myValue

Notably, in your original report, the string was incorrectly serialized:

The documentation for the "option pluck" and "option patch" commands suggest that they work with arrays and JSON. Specifically the reference to 'keys within the value'. However they do not.

Assuming my_option has a value of a:1:{s:5:"myKey";s:6:"aValue"}

The serialized value should be a:1:{s:5:"myKey";s:7:"myValue";}.

Additionally, adding support for JSON-encoded options (and meta, etc.) would be an enhancement. WordPress doesn't automatically work natively with JSON, so I'm not sure it makes sense to add that to WP-CLI.

Thoughts?

@BlairCooper
Copy link
Author

Concerning JSON, while WordPress doesn't support storing JSON natively having this functionality would be useful. WordPress does store the health-check-site-status-result (transient) option as a JSON serialized string.

I may revisit this pull request and break it into smaller pieces. Can you have a look at the feature tests that were added and let me know if they're valid scenario, or if there are others that should be added?

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.

Option Pluck & Patch don't work with arrays or JSON
2 participants