Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions features/option-pluck-patch.feature
Original file line number Diff line number Diff line change
Expand Up @@ -269,3 +269,33 @@ Feature: Option commands have pluck and patch.
"""
[ "new", "bar" ]
"""

@patch @pluck
Scenario: An object value can be updated
Given a WP install
And a setup.php file:
"""
<?php
$option = new stdClass;
$option->test_mode = 0;
$ret = update_option( 'wp_cli_test', $option );
"""
And I run `wp eval-file setup.php`

When I run `wp option pluck wp_cli_test test_mode`
Then STDOUT should be:
"""
0
"""

When I run `wp option patch update wp_cli_test test_mode 1`
Then STDOUT should be:
"""
Success: Updated 'wp_cli_test' option.
"""

When I run `wp option pluck wp_cli_test test_mode`
Then STDOUT should be:
"""
1
"""
3 changes: 3 additions & 0 deletions src/Option_Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -543,6 +543,9 @@ public function patch( $args, $assoc_args ) {

/* Need to make a copy of $current_value here as it is modified by reference */
$old_value = $current_value = sanitize_option( $key, get_option( $key ) );
if ( is_object( $current_value ) ) {
$old_value = clone $current_value;
}

$traverser = new RecursiveDataStructureTraverser( $current_value );

Expand Down
3 changes: 3 additions & 0 deletions src/Site_Option_Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -372,6 +372,9 @@ public function patch( $args, $assoc_args ) {

/* Need to make a copy of $current_value here as it is modified by reference */
$old_value = $current_value = sanitize_option( $key, get_site_option( $key ) );
if ( is_object( $current_value ) ) {
$old_value = clone $current_value;
}

$traverser = new RecursiveDataStructureTraverser( $current_value );

Expand Down
3 changes: 3 additions & 0 deletions src/WP_CLI/CommandWithMeta.php
Original file line number Diff line number Diff line change
Expand Up @@ -390,6 +390,9 @@ public function patch( $args, $assoc_args ) {

/* Need to make a copy of $current_meta_value here as it is modified by reference */
$current_meta_value = $old_meta_value = sanitize_meta( $meta_key, get_metadata( $this->meta_type, $object_id, $meta_key, true ), $this->meta_type );
if ( is_object( $current_meta_value ) ) {
$old_meta_value = clone $current_meta_value;
}

$traverser = new RecursiveDataStructureTraverser( $current_meta_value );

Expand Down
13 changes: 13 additions & 0 deletions tests/RecursiveDataStructureTraverserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,19 @@ function it_can_set_a_nested_object_value() {
$this->assertEquals( 'new', $object->foo->bar );
}

/** @test */
function it_can_update_an_integer_object_value() {
$object = (object) array(
'test_mode' => 0,
);
$this->assertEquals( 0, $object->test_mode );

$traverser = new RecursiveDataStructureTraverser( $object );
$traverser->update( array( 'test_mode' ), 1 );

$this->assertEquals( 1, $object->test_mode );
}

/** @test */
function it_can_delete_a_nested_array_value() {
$array = array(
Expand Down