|
1 | 1 | <?php |
2 | 2 |
|
3 | | -use WP_CLI\Utils; |
4 | 3 | use WP_CLI\Formatter; |
| 4 | +use WP_CLI\Traverser\RecursiveDataStructureTraverser; |
| 5 | +use WP_CLI\Utils; |
5 | 6 |
|
6 | 7 | /** |
7 | 8 | * Manages widgets, including adding and moving them within sidebars. |
@@ -206,6 +207,110 @@ public function update( $args, $assoc_args ) { |
206 | 207 | WP_CLI::success( 'Widget updated.' ); |
207 | 208 | } |
208 | 209 |
|
| 210 | + /** |
| 211 | + * Updates a nested value in a widget's options. |
| 212 | + * |
| 213 | + * ## OPTIONS |
| 214 | + * |
| 215 | + * <action> |
| 216 | + * : Patch action to perform. |
| 217 | + * --- |
| 218 | + * options: |
| 219 | + * - insert |
| 220 | + * - update |
| 221 | + * - delete |
| 222 | + * --- |
| 223 | + * |
| 224 | + * <widget-id> |
| 225 | + * : Unique ID for the widget. |
| 226 | + * |
| 227 | + * <key-path>... |
| 228 | + * : The name(s) of the keys within the value to locate the value to patch. |
| 229 | + * |
| 230 | + * [<value>] |
| 231 | + * : The new value. If omitted, the value is read from STDIN. |
| 232 | + * |
| 233 | + * [--format=<format>] |
| 234 | + * : The serialization format for the value. |
| 235 | + * --- |
| 236 | + * default: plaintext |
| 237 | + * options: |
| 238 | + * - plaintext |
| 239 | + * - json |
| 240 | + * --- |
| 241 | + * |
| 242 | + * ## EXAMPLES |
| 243 | + * |
| 244 | + * # Update a nested value in the options of the archives-1 widget |
| 245 | + * $ wp widget patch update archives-1 title "My Archives" |
| 246 | + * Success: Widget updated. |
| 247 | + * |
| 248 | + * # Insert a new nested value into the options of the archives-1 widget |
| 249 | + * $ wp widget patch insert archives-1 new_key "New Value" |
| 250 | + * Success: Widget updated. |
| 251 | + * |
| 252 | + * # Delete a nested value from the options of the archives-1 widget |
| 253 | + * $ wp widget patch delete archives-1 title |
| 254 | + * Success: Widget updated. |
| 255 | + * |
| 256 | + * @subcommand patch |
| 257 | + */ |
| 258 | + public function patch( $args, $assoc_args ) { |
| 259 | + list( $action, $widget_id ) = $args; |
| 260 | + |
| 261 | + if ( ! $this->validate_sidebar_widget( $widget_id ) ) { |
| 262 | + WP_CLI::error( "Widget doesn't exist." ); |
| 263 | + } |
| 264 | + |
| 265 | + $key_path = array_map( |
| 266 | + function ( $key ) { |
| 267 | + if ( is_numeric( $key ) && ( (string) intval( $key ) === $key ) ) { |
| 268 | + return (int) $key; |
| 269 | + } |
| 270 | + return $key; |
| 271 | + }, |
| 272 | + array_slice( $args, 2 ) |
| 273 | + ); |
| 274 | + |
| 275 | + if ( 'delete' === $action ) { |
| 276 | + $patch_value = null; |
| 277 | + } else { |
| 278 | + $stdin_value = Utils\has_stdin() |
| 279 | + ? trim( WP_CLI::get_value_from_arg_or_stdin( $args, -1 ) ) |
| 280 | + : null; |
| 281 | + |
| 282 | + if ( ! empty( $stdin_value ) ) { |
| 283 | + $patch_value = WP_CLI::read_value( $stdin_value, $assoc_args ); |
| 284 | + } elseif ( count( $key_path ) > 1 ) { |
| 285 | + $patch_value = WP_CLI::read_value( array_pop( $key_path ), $assoc_args ); |
| 286 | + } else { |
| 287 | + $patch_value = null; |
| 288 | + } |
| 289 | + |
| 290 | + if ( null === $patch_value ) { |
| 291 | + WP_CLI::error( 'Please provide value to update.' ); |
| 292 | + } |
| 293 | + } |
| 294 | + |
| 295 | + list( $name, $option_index ) = $this->get_widget_data( $widget_id ); |
| 296 | + |
| 297 | + $widget_options = $this->get_widget_options( $name ); |
| 298 | + $instance_options = isset( $widget_options[ $option_index ] ) ? $widget_options[ $option_index ] : array(); |
| 299 | + |
| 300 | + $traverser = new RecursiveDataStructureTraverser( $instance_options ); |
| 301 | + |
| 302 | + try { |
| 303 | + $traverser->$action( $key_path, $patch_value ); |
| 304 | + } catch ( Exception $exception ) { |
| 305 | + WP_CLI::error( $exception->getMessage() ); |
| 306 | + } |
| 307 | + |
| 308 | + $widget_options[ $option_index ] = $traverser->value(); |
| 309 | + $this->update_widget_options( $name, $widget_options ); |
| 310 | + |
| 311 | + WP_CLI::success( 'Widget updated.' ); |
| 312 | + } |
| 313 | + |
209 | 314 | /** |
210 | 315 | * Moves the position of a widget. |
211 | 316 | * |
|
0 commit comments