Skip to content

Commit

Permalink
Update 'cmb2_override_meta_value' and "cmb2_override_{$field_id}_meta…
Browse files Browse the repository at this point in the history
…_value" filters and their location.

These filters are moved to the get_data method so that the filters are applied every time the data is requested. Also changes the number and type of arguments passed to them.
Add unit tests to test setting/getting filters.
  • Loading branch information
jtsternberg committed Apr 7, 2015
1 parent 09d6c56 commit 3efe270
Show file tree
Hide file tree
Showing 3 changed files with 114 additions and 73 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,13 @@ All notable changes to this project will be documented in this file.

* Fix issue with oembed fields in repeatable groups where changing video changed it for all fields in a group.
* Fix empty arrays (like in the group field) saving as a value.
* Move `'cmb2_override_meta_value'` and `"cmb2_override_{$field_id}_meta_value"` filters to the `CMB2_Field::get_data()` method so that the filters are applied every time the data is requested. **THIS IS A BREAKING CHANGE:** The parameters for those filters have changed a bit. Previously, the filters accepted 5 arguments, `$value`, `$object_id`, `$field_args`, `$object_type`, `$field`. They have changed to accept 4 arguments instead, `$value`, `$object_id`, `$args`, `$field`, where `$args` is an array that contains the following:
* @type string $type The current object type
* @type int $id The current object ID
* @type string $field_id The ID of the field being requested
* @type bool $repeat Whether current field is repeatable
* @type bool $single Whether current field is a single database row


## 2.0.5 - 2015-03-17

Expand Down
139 changes: 66 additions & 73 deletions includes/CMB2_Field.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ public function __construct( $args ) {
$this->args = $this->_set_field_defaults( $args['field_args'] );

if ( $this->object_id ) {
$this->set_value();
$this->value = $this->get_data();
}
}

Expand Down Expand Up @@ -158,53 +158,6 @@ public function _data( $var, $key = '' ) {
return $vars;
}

/**
* Fetches this field's value and sets as an object property
* @since 2.0.3
*/
public function set_value() {
/**
* Filter whether to override getting of meta value.
* Returning a non 'cmb2_field_no_override_val' value
* will effectively short-circuit the value retrieval.
*
* @since 2.0.0
*
* @param null|array|string $value The value get_metadata() should
* return - a single metadata value,
* or an array of values.
* @param int $object_id Object ID.
* @param array $field_args All field arguments
* @param string $object_type Object Type
* @param CMB2_Field object $field_obj This field object
*/
$this->value = apply_filters( 'cmb2_override_meta_value', 'cmb2_field_no_override_val', $this->object_id, $this->args(), $this->object_type, $this );

/**
* Filter whether to override getting of meta value.
*
* The dynamic portion of the hook, $field_id, refers to the current
* field id paramater. Returning a non 'cmb2_field_no_override_val' value
* will effectively short-circuit the value retrieval.
*
* @since 2.0.0
*
* @param null|array|string $value The value get_metadata() should
* return - a single metadata value,
* or an array of values.
* @param int $object_id Object ID.
* @param array $field_args All field arguments
* @param string $object_type Object Type
* @param CMB2_Field object $field_obj This field object
*/
$this->value = apply_filters( "cmb2_override_{$this->id( true )}_meta_value", $this->value, $this->object_id, $this->args(), $this->object_type, $this );

// If no override, get our data
$this->value = 'cmb2_field_no_override_val' === $this->value
? $this->get_data()
: $this->value;
}

/**
* Get Field's value
* @since 1.1.0
Expand All @@ -230,9 +183,51 @@ public function get_data( $field_id = '', $args = array() ) {

$a = $this->data_args( $args );

$data = 'options-page' === $a['type']
? cmb2_options( $a['id'] )->get( $a['field_id'] )
: get_metadata( $a['type'], $a['id'], $a['field_id'], ( $a['single'] || $a['repeat'] ) );
/**
* Filter whether to override getting of meta value.
* Returning a non 'cmb2_field_no_override_val' value
* will effectively short-circuit the value retrieval.
*
* @since 2.0.0
*
* @param mixed $value The value get_metadata() should
* return - a single metadata value,
* or an array of values.
*
* @param int $object_id Object ID.
*
* @param array $args {
* An array of arguments for retrieving data
*
* @type string $type The current object type
* @type int $id The current object ID
* @type string $field_id The ID of the field being requested
* @type bool $repeat Whether current field is repeatable
* @type bool $single Whether current field is a single database row
* }
*
* @param CMB2_Field object $field This field object
*/
$data = apply_filters( 'cmb2_override_meta_value', 'cmb2_field_no_override_val', $this->object_id, $a, $this );

/**
* Filter and parameters are documented for 'cmb2_override_meta_value' filter (above).
*
* The dynamic portion of the hook, $field_id, refers to the current
* field id paramater. Returning a non 'cmb2_field_no_override_val' value
* will effectively short-circuit the value retrieval.
*
* @since 2.0.0
*/
$data = apply_filters( "cmb2_override_{$a['field_id']}_meta_value", $data, $this->object_id, $a, $this );

// If no override, get value normally
if ( 'cmb2_field_no_override_val' === $data ) {
$data = 'options-page' === $a['type']
? cmb2_options( $a['id'] )->get( $a['field_id'] )
: get_metadata( $a['type'], $a['id'], $a['field_id'], ( $a['single'] || $a['repeat'] ) );
}


if ( $this->group && $data ) {
$data = isset( $data[ $this->group->index ][ $this->args( '_id' ) ] )
Expand Down Expand Up @@ -261,44 +256,40 @@ public function update_data( $new_value, $single = true ) {
* @since 2.0.0
*
* @param null|bool $check Whether to allow updating metadata for the given type.
* @param array $args Array of data about current field including:
* 'type' : Current object type
* 'id' : Current object ID
* 'field_id' : Current Field ID
* 'repeat' : Whether current field is repeatable
* 'single' : Whether to save as a
* single meta value
*
* @param array $args {
* Array of data about current field including:
*
* @type string $value The value to set
* @type string $type The current object type
* @type int $id The current object ID
* @type string $field_id The ID of the field being updated
* @type bool $repeat Whether current field is repeatable
* @type bool $single Whether current field is a single database row
* }
*
* @param array $field_args All field arguments
* @param CMB2_Field object $field_obj This field object
*
* @param CMB2_Field object $field This field object
*/
$override = apply_filters( 'cmb2_override_meta_save', null, $a, $this->args(), $this );

/**
* Filter whether to override saving of meta value.
* Filter and parameters are documented for 'cmb2_override_meta_save' filter (above).
*
* The dynamic portion of the hook, $a['field_id'], refers to the current
* field id paramater. Returning a non-null value
* will effectively short-circuit the function.
*
* @since 2.0.0
*
* @param null|bool $check Whether to allow updating metadata for the given type.
* @param array $args Array of data about current field including:
* 'type' : Current object type
* 'id' : Current object ID
* 'field_id' : Current Field ID
* 'repeat' : Whether current field is repeatable
* 'single' : Whether to save as a
* single meta value
* @param array $field_args All field arguments
* @param CMB2_Field object $field_obj This field object
*/
$override = apply_filters( "cmb2_override_{$a['field_id']}_meta_save", $override, $a, $this->args(), $this );

// If override, return that
if ( null !== $override ) {
return $override;
}

// Options page handling (or temp data store)
if ( 'options-page' === $a['type'] || empty( $a['id'] ) ) {
return cmb2_options( $a['id'] )->update( $a['field_id'], $a[ 'value' ], false, $a['single'] );
Expand All @@ -308,6 +299,7 @@ public function update_data( $new_value, $single = true ) {
if ( ! $a['single'] ) {
return add_metadata( $a['type'], $a['id'], $a['field_id'], $a[ 'value' ], false );
}

// Delete meta if we have an empty array
if ( is_array( $a[ 'value' ] ) && empty( $a[ 'value' ] ) ) {
return delete_metadata( $a['type'], $a['id'], $a['field_id'], $this->value );
Expand Down Expand Up @@ -340,7 +332,7 @@ public function remove_data( $old = '' ) {
* 'single' : Whether to save as a
* single meta value
* @param array $field_args All field arguments
* @param CMB2_Field object $field_obj This field object
* @param CMB2_Field object $field This field object
*/
$override = apply_filters( 'cmb2_override_meta_remove', null, $a, $this->args(), $this );

Expand All @@ -362,7 +354,7 @@ public function remove_data( $old = '' ) {
* 'single' : Whether to save as a
* single meta value
* @param array $field_args All field arguments
* @param CMB2_Field object $field_obj This field object
* @param CMB2_Field object $field This field object
*/
$override = apply_filters( "cmb2_override_{$a['field_id']}_meta_remove", $override, $a, $this->args(), $this );

Expand Down Expand Up @@ -466,6 +458,7 @@ public function save_field( $meta_value ) {

$updated = $count ? $count : false;


} elseif ( ! cmb2_utils()->isempty( $new_value ) && $new_value !== $old ) {
$updated = $this->update_data( $new_value );
} elseif ( cmb2_utils()->isempty( $new_value ) ) {
Expand Down
41 changes: 41 additions & 0 deletions tests/test-cmb-field.php
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,36 @@ public function test_multiple_meta_rows() {

}

public function test_set_get_filters() {
$array_val = array( 'check1', 'check2' );

$this->field->save_field( $array_val );
$this->assertEquals( $array_val, get_post_meta( $this->field->object_id, $this->field->_id(), 1 ) );

delete_post_meta( $this->field->object_id, $this->field->_id() );

$this->assertFalse( !! get_post_meta( $this->field->object_id, $this->field->_id(), 1 ) );

add_filter( "cmb2_override_{$this->field->_id()}_meta_save", array( $this, 'override_set' ), 10, 4 );
$this->field->save_field( $array_val );

$this->assertFalse( !! get_post_meta( $this->field->object_id, $this->field->_id(), 1 ) );

$opt_key = 'test-'. $this->field->object_id . '-' . $this->field->_id();
error_log( '$opt_key: '. print_r( $opt_key, true ) );
$opt = get_option( $opt_key );
$this->assertEquals( $array_val, $opt );

$value = $this->field->get_data();

$this->assertFalse( !! $value );

add_filter( "cmb2_override_{$this->field->_id()}_meta_value", array( $this, 'override_get' ), 10, 4 );
$value = $this->field->get_data();

$this->assertEquals( $array_val, $value );
}

public function before_field_cb( $args ) {
echo 'before_field_cb_' . $args['id'];
}
Expand All @@ -254,4 +284,15 @@ public function cb_to_set_default( $args ) {
return implode( ', ', array_keys( $args ) );
}

public function override_set( $override, $args, $field_args, $field ) {
$opt_key = 'test-'. $args['id'] . '-' . $args['field_id'];
$updated = update_option( $opt_key, $args['value'] );
return true;
}

public function override_get( $override_val, $object_id, $args, $field ) {
$opt_key = 'test-'. $args['id'] . '-' . $args['field_id'];
return get_option( $opt_key );
}

}

0 comments on commit 3efe270

Please sign in to comment.