diff --git a/features/post-meta.feature b/features/post-meta.feature index a7eacd135f..e8dd92da3c 100644 --- a/features/post-meta.feature +++ b/features/post-meta.feature @@ -109,3 +109,33 @@ Feature: Manage post custom fields Then STDOUT should be a table containing rows: | post_id | meta_key | meta_value | | 1 | foo | | + + Scenario: Make sure WordPress receives the slashed data it expects in meta fields + Given a WP install + + When I run `wp post-meta add 1 foo 'My\Meta'` + Then STDOUT should not be empty + + When I run `wp post-meta get 1 foo` + Then STDOUT should be: + """ + My\Meta + """ + + When I run `wp post-meta update 1 foo 'My\New\Meta'` + Then STDOUT should be: + """ + Success: Updated custom field 'foo'. + """ + + When I run the previous command again + Then STDOUT should be: + """ + Success: Value passed for custom field 'foo' is unchanged. + """ + + When I run `wp post-meta get 1 foo` + Then STDOUT should be: + """ + My\New\Meta + """ diff --git a/php/WP_CLI/CommandWithMeta.php b/php/WP_CLI/CommandWithMeta.php index 0e8a07a39a..26c717c205 100644 --- a/php/WP_CLI/CommandWithMeta.php +++ b/php/WP_CLI/CommandWithMeta.php @@ -179,6 +179,7 @@ public function add( $args, $assoc_args ) { $object_id = $this->check_object_id( $object_id ); + $meta_value = wp_slash( $meta_value ); $success = add_metadata( $this->meta_type, $object_id, $meta_key, $meta_value ); if ( $success ) { @@ -221,6 +222,7 @@ public function update( $args, $assoc_args ) { if ( $meta_value === $old_value ) { WP_CLI::success( "Value passed for custom field '$meta_key' is unchanged." ); } else { + $meta_value = wp_slash( $meta_value ); $success = update_metadata( $this->meta_type, $object_id, $meta_key, $meta_value ); if ( $success ) {