Skip to content

Commit

Permalink
First pass at "fields". Fields are a just a wrapper method for regist…
Browse files Browse the repository at this point in the history
…ering both a control and setting. This is a little easier to use than registering both a control and setting separately. This will only work when you have a single setting tied to a control. Controls with multiple settings would need to use the normal system.

This commit introduces the `register_field()`, `unregister_field()`, `get_field()`, and `field_exists()` methods for the `ButterBean_Manager` class.
  • Loading branch information
Justin Tadlock committed Jun 20, 2016
1 parent aaf09b7 commit 525b21d
Showing 1 changed file with 61 additions and 0 deletions.
61 changes: 61 additions & 0 deletions inc/class-manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,22 @@ public function register_setting( $setting, $args = array() ) {
$this->settings[ $setting->name ] = $setting;
}

/**
* Register a control and setting object.
*
* @since 1.0.0
* @access public
* @param string $name
* @param object|array $control Control object or array of control arguments.
* @param object|array $setting Setting object or array of setting arguments.
* @return void
*/
public function register_field( $name, $control, $setting ) {

is_object( $control ) ? $this->register_control( $control ) : $this->register_control( $name, $control );
is_object( $setting ) ? $this->register_setting( $setting ) : $this->register_setting( $name, $setting );
}

/**
* Unregisters a section object.
*
Expand Down Expand Up @@ -279,6 +295,20 @@ public function unregister_setting( $name ) {
unset( $this->settings[ $name ] );
}

/**
* Unregisters a control and setting object.
*
* @since 1.0.0
* @access public
* @param string $name
* @return void
*/
public function unregister_field( $name ) {

$this->unregister_control( $name );
$this->unregister_setting( $name );
}

/**
* Returns a section object.
*
Expand Down Expand Up @@ -318,6 +348,24 @@ public function get_setting( $name ) {
return $this->setting_exists( $name ) ? $this->settings[ $name ] : false;
}

/**
* Returns an object that contains both the control and setting objects.
*
* @since 1.0.0
* @access public
* @param string $name
* @return object|bool
*/
public function get_field( $name ) {

$control = $this->get_control( $name );
$setting = $this->get_setting( $name );

$field = array( 'name' => $name, 'control' => $control, 'setting' => $setting );

return $control && $setting ? (object) $field : false;
}

/**
* Checks if a section exists.
*
Expand Down Expand Up @@ -357,6 +405,19 @@ public function setting_exists( $name ) {
return isset( $this->settings[ $name ] );
}

/**
* Checks if a both a control and setting exist.
*
* @since 1.0.0
* @access public
* @param string $name
* @return bool
*/
public function field_exists( $name ) {

return $this->control_exists( $name ) && $this->setting_exists( $name );
}

/**
* Returns the json array.
*
Expand Down

0 comments on commit 525b21d

Please sign in to comment.