Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add helper function to update an option #110

Merged
merged 2 commits into from Dec 9, 2014
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
18 changes: 18 additions & 0 deletions includes/helper-functions.php
Expand Up @@ -77,6 +77,24 @@ function cmb2_get_option( $option_key, $field_id = '' ) {
return cmb2_options( $option_key )->get( $field_id );
}

/**
* A helper function to update an option in a CMB options array
* @since 2.0.0
* @param string $option_key Option key
* @param string $field_id Option array field key
* @param mixed $value Value to update data with
* @param bool $resave Whether to re-save the data
* @param bool $single Whether data should be an array
* @return array Modified options
*/
function cmb2_update_option( $option_key, $field_id, $value, $resave = false, $single = true ) {
if ( cmb2_options( $option_key )->update( $field_id, $value, $resave, $single ) ) {
return cmb2_options( $option_key )->set();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

since you are using the set method, there is no reason to take in the $resave argument to this helper function (because using set, it will save it to the DB). I would remove it from the cmb2_update_option function and set it to 'false' for cmb2_options()->update.

}

return false;
}

/**
* Get a CMB field object.
* @since 1.1.0
Expand Down
14 changes: 14 additions & 0 deletions tests/test-cmb-core.php
Expand Up @@ -185,14 +185,28 @@ public function cmb_after( $field_args, $field ) {
public function test_cmb2_options() {
$opts = cmb2_options( $this->options_cmb->cmb_id );
$this->assertEquals( $opts->get_options(), $this->opt_set );
}

public function test_cmb2_get_option() {
$get = get_option( $this->options_cmb->cmb_id );
$val = cmb2_get_option( $this->options_cmb->cmb_id, 'my_name' );

$this->assertEquals( $this->opt_set['my_name'], $get['my_name'] );
$this->assertEquals( $val, $get['my_name'] );
$this->assertEquals( $val, $this->opt_set['my_name'] );
}

public function test_cmb2_update_option() {
$new_value = 'James';

cmb2_update_option( $this->options_cmb->cmb_id, 'my_name', $new_value );

$get = get_option( $this->options_cmb->cmb_id );
$val = cmb2_get_option( $this->options_cmb->cmb_id, 'my_name' );

$this->assertEquals( $new_value, $get['my_name'] );
$this->assertEquals( $val, $get['my_name'] );
$this->assertEquals( $val, $new_value );
}

public function test_class_getters() {
Expand Down