wp-cli / entity-command

Manage WordPress comments, menus, options, posts, sites, terms, and users.
MIT License
100 stars 90 forks source link

Option Pluck & Patch don't work with arrays or JSON #459

Closed BlairCooper closed 9 months ago

BlairCooper commented 9 months ago

Bug Report

Describe the current, buggy behavior

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 command wp option pluck my_option myKey returns nothing.

The command wp option patch update my_option myKey newValue returns

Error: No data exists for key "myKey"

Describe how other contributors can replicate this bug

Pluck:

Patch

Describe what you would expect as the correct outcome

It should be possible to read a value from an option stored as a serialized array or JSON. It should be possible to write a new value into an option stored as a serialized array or JSON.

Let us know what environment you are running this on

Windows 10, PHP8.2.15

OS:     Windows NT 10.0 build 19045 (Windows 10) AMD64
Shell:  C:\WINDOWS\system32\cmd.exe
PHP binary:     C:\PHP\php-8.2.15\php.exe
PHP version:    8.2.15
php.ini used:   C:\PHP\php-8.2.15\php.ini
MySQL binary:
MySQL version:
SQL modes:
WP-CLI root dir:        phar://wp-cli.phar/vendor/wp-cli/wp-cli
WP-CLI vendor dir:      phar://wp-cli.phar/vendor
WP_CLI phar path:       C:\Projects\CPRN\Website
WP-CLI packages dir:    C:\Users\rainc/.wp-cli/packages/
WP-CLI cache dir:       C:\Users\rainc/.wp-cli/cache
WP-CLI global config:   C:\Users\rainc/.wp-cli/config.yml
WP-CLI project config:
WP-CLI version: 2.9.0

Provide a possible solution

Add detection for and handling of serialized values in options. I have a pull request incoming.

Provide additional context/Screenshots

n/a

BlairCooper commented 9 months ago

I miss understood the option I was dealing with in WordPress in reporting this issue. Turns out the option is an array that was serialized before being passed to update_option, making it a string. I'll submit a new enhancement request for supporting pluck and patch on JSON serialized options.