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
1 change: 1 addition & 0 deletions php/context/class-fieldmanager-context-post.php
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,7 @@ protected function add_data( $post_id, $meta_key, $meta_value, $unique = false )
* Default empty.
*/
protected function update_data( $post_id, $meta_key, $meta_value, $data_prev_value = '' ) {
$meta_value = $this->sanitize_scalar_value( $meta_value );
return update_post_meta( $post_id, $meta_key, $meta_value, $data_prev_value );
}

Expand Down
1 change: 1 addition & 0 deletions php/context/class-fieldmanager-context-quickedit.php
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,7 @@ protected function add_data( $post_id, $meta_key, $meta_value, $unique = false )
* Default empty.
*/
protected function update_data( $post_id, $meta_key, $meta_value, $data_prev_value = '' ) {
$meta_value = $this->sanitize_scalar_value( $meta_value );
return update_post_meta( $post_id, $meta_key, $meta_value, $data_prev_value );
}

Expand Down
17 changes: 17 additions & 0 deletions php/context/class-fieldmanager-context-storable.php
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,23 @@ protected function load_walk_children( $field ) {
}
}

/**
* Meta and options are always stored as strings, so it's best to ensure
* that scalar values get cast as strings to ensure that `update_metadata()`
* and `update_option()` are able to correctly compare the current value
* against the previous value.
*
* @param mixed $value Value being stored.
* @return string|array If $value is scalar, a string is returned. Otherwise,
* $value returns untouched.
*/
public static function sanitize_scalar_value( $value ) {
if ( is_scalar( $value ) && ! is_string( $value ) ) {
return strval( $value );
}
return $value;
}

/**
* Method to get data from the context's storage engine.
*
Expand Down
1 change: 1 addition & 0 deletions php/context/class-fieldmanager-context-submenu.php
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,7 @@ protected function add_data( $data_id, $option_name, $option_value, $unique = fa
* @return bool Option updated successfully.
*/
protected function update_data( $data_id, $option_name, $option_value, $option_prev_value = '' ) {
$option_value = $this->sanitize_scalar_value( $option_value );
return update_option( $option_name, $option_value );
}

Expand Down
1 change: 1 addition & 0 deletions php/context/class-fieldmanager-context-term.php
Original file line number Diff line number Diff line change
Expand Up @@ -384,6 +384,7 @@ protected function add_data( $term_id, $meta_key, $meta_value, $unique = false )
* @param bool $meta_prev_value The previous meta value.
*/
protected function update_data( $term_id, $meta_key, $meta_value, $meta_prev_value = '' ) {
$meta_value = $this->sanitize_scalar_value( $meta_value );
if ( $this->use_fm_meta ) {
return fm_update_term_meta( $term_id, $this->current_taxonomy, $meta_key, $meta_value, $meta_prev_value );
} else {
Expand Down
1 change: 1 addition & 0 deletions php/context/class-fieldmanager-context-user.php
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,7 @@ protected function add_data( $user_id, $meta_key, $meta_value, $unique = false )
* @param mixed $data_prev_value The previous meta data.
*/
protected function update_data( $user_id, $meta_key, $meta_value, $data_prev_value = '' ) {
$meta_value = $this->sanitize_scalar_value( $meta_value );
return call_user_func(
/**
* Filters function used to update user meta. This improves compatibility with
Expand Down
32 changes: 32 additions & 0 deletions tests/php/test-fieldmanager-context-storable.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

/**
* Tests the Storable Context base.
*
* @group context
*/
class Test_Fieldmanager_Context_Storable extends WP_UnitTestCase {
public function scalar_sanitize_data() {
return array(
array( 1, '1' ),
array( 0, '0' ),
array( true, '1' ),
array( false, '' ),
array( 'abc', 'abc' ),
array( array(), array() ),
array( array( 1, 2, 3 ), array( 1, 2, 3 ) ),
array( 1.234, '1.234' ),
array( null, null ),
array( '', '' ),
);
}

/**
* @dataProvider scalar_sanitize_data
* @param mixed $test Test cases.
* @param mixed $expected Expected values.
*/
public function test_sanitize_scalar_values( $test, $expected ) {
$this->assertSame( $expected, \Fieldmanager_Context_Post::sanitize_scalar_value( $test ) );
}
}