first commit
This commit is contained in:
@@ -0,0 +1,399 @@
|
||||
<?php
|
||||
/**
|
||||
* Base class for handling controls. Controls are the form fields for the manager. Each
|
||||
* control should be tied to a section.
|
||||
*
|
||||
* @package ButterBean
|
||||
* @author Justin Tadlock <justin@justintadlock.com>
|
||||
* @copyright Copyright (c) 2015-2016, Justin Tadlock
|
||||
* @link https://github.com/justintadlock/butterbean
|
||||
* @license http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
|
||||
*/
|
||||
|
||||
/**
|
||||
* Base control class.
|
||||
*
|
||||
* @since 1.0.0
|
||||
* @access public
|
||||
*/
|
||||
class ButterBean_Control {
|
||||
|
||||
/**
|
||||
* Stores the manager object.
|
||||
*
|
||||
* @since 1.0.0
|
||||
* @access public
|
||||
* @var object
|
||||
*/
|
||||
public $manager;
|
||||
|
||||
/**
|
||||
* Name/ID of the control.
|
||||
*
|
||||
* @since 1.0.0
|
||||
* @access public
|
||||
* @var string
|
||||
*/
|
||||
public $name = '';
|
||||
|
||||
/**
|
||||
* Label for the control.
|
||||
*
|
||||
* @since 1.0.0
|
||||
* @access public
|
||||
* @var string
|
||||
*/
|
||||
public $label = '';
|
||||
|
||||
/**
|
||||
* Description for the control.
|
||||
*
|
||||
* @since 1.0.0
|
||||
* @access public
|
||||
* @var string
|
||||
*/
|
||||
public $description = '';
|
||||
|
||||
/**
|
||||
* ID of the section the control is for.
|
||||
*
|
||||
* @since 1.0.0
|
||||
* @access public
|
||||
* @var string
|
||||
*/
|
||||
public $section = '';
|
||||
|
||||
/**
|
||||
* The setting key for the specific setting the control is tied to.
|
||||
* Controls can have multiple settings attached to them. The default
|
||||
* setting is `default`.
|
||||
*
|
||||
* @since 1.0.0
|
||||
* @access public
|
||||
* @var string
|
||||
*/
|
||||
public $setting = 'default';
|
||||
|
||||
/**
|
||||
* Array of settings if the control has multiple settings.
|
||||
*
|
||||
* @since 1.0.0
|
||||
* @access public
|
||||
* @var array
|
||||
*/
|
||||
public $settings = array();
|
||||
|
||||
/**
|
||||
* The type of control.
|
||||
*
|
||||
* @since 1.0.0
|
||||
* @access public
|
||||
* @var string
|
||||
*/
|
||||
public $type = 'text';
|
||||
|
||||
/**
|
||||
* Form field attributes.
|
||||
*
|
||||
* @since 1.0.0
|
||||
* @access public
|
||||
* @var array
|
||||
*/
|
||||
public $attr = '';
|
||||
|
||||
/**
|
||||
* Choices for fields with multiple choices.
|
||||
*
|
||||
* @since 1.0.0
|
||||
* @access public
|
||||
* @var array
|
||||
*/
|
||||
public $choices = array();
|
||||
|
||||
/**
|
||||
* Priority (order) the control should be output.
|
||||
*
|
||||
* @since 1.0.0
|
||||
* @access public
|
||||
* @var int
|
||||
*/
|
||||
public $priority = 10;
|
||||
|
||||
/**
|
||||
* The number of instances created.
|
||||
*
|
||||
* @since 1.0.0
|
||||
* @access protected
|
||||
* @var int
|
||||
*/
|
||||
protected static $instance_count = 0;
|
||||
|
||||
/**
|
||||
* The instance of the current control.
|
||||
*
|
||||
* @since 1.0.0
|
||||
* @access public
|
||||
* @var int
|
||||
*/
|
||||
public $instance_number;
|
||||
|
||||
/**
|
||||
* A callback function for deciding if a control is active.
|
||||
*
|
||||
* @since 1.0.0
|
||||
* @access public
|
||||
* @var callable
|
||||
*/
|
||||
public $active_callback = '';
|
||||
|
||||
/**
|
||||
* A user role capability required to show the control.
|
||||
*
|
||||
* @since 1.0.0
|
||||
* @access public
|
||||
* @var string|array
|
||||
*/
|
||||
public $capability = '';
|
||||
|
||||
/**
|
||||
* A feature that the current post type must support to show the control.
|
||||
*
|
||||
* @since 1.0.0
|
||||
* @access public
|
||||
* @var string
|
||||
*/
|
||||
public $post_type_supports = '';
|
||||
|
||||
/**
|
||||
* A feature that the current theme must support to show the control.
|
||||
*
|
||||
* @since 1.0.0
|
||||
* @access public
|
||||
* @var string|array
|
||||
*/
|
||||
public $theme_supports = '';
|
||||
|
||||
/**
|
||||
* Stores the JSON data for the control.
|
||||
*
|
||||
* @since 1.0.0
|
||||
* @access public
|
||||
* @var array()
|
||||
*/
|
||||
public $json = array();
|
||||
|
||||
/**
|
||||
* Creates a new control object.
|
||||
*
|
||||
* @since 1.0.0
|
||||
* @access public
|
||||
* @param object $manager
|
||||
* @param string $name
|
||||
* @param array $args
|
||||
* @return void
|
||||
*/
|
||||
public function __construct( $manager, $name, $args = array() ) {
|
||||
|
||||
foreach ( array_keys( get_object_vars( $this ) ) as $key ) {
|
||||
|
||||
if ( isset( $args[ $key ] ) )
|
||||
$this->$key = $args[ $key ];
|
||||
}
|
||||
|
||||
$this->manager = $manager;
|
||||
$this->name = $name;
|
||||
|
||||
if ( empty( $args['settings'] ) || ! is_array( $args['settings'] ) )
|
||||
$this->settings['default'] = $name;
|
||||
|
||||
// Increment the instance count and set the instance number.
|
||||
self::$instance_count += 1;
|
||||
$this->instance_number = self::$instance_count;
|
||||
|
||||
// Set the active callback function if not set.
|
||||
if ( ! $this->active_callback )
|
||||
$this->active_callback = array( $this, 'active_callback' );
|
||||
}
|
||||
|
||||
/**
|
||||
* Enqueue scripts/styles for the control.
|
||||
*
|
||||
* @since 1.0.0
|
||||
* @access public
|
||||
* @return void
|
||||
*/
|
||||
public function enqueue() {}
|
||||
|
||||
/**
|
||||
* Get the value for the setting.
|
||||
*
|
||||
* @since 1.0.0
|
||||
* @access public
|
||||
* @param string $setting
|
||||
* @return mixed
|
||||
*/
|
||||
public function get_value( $setting = 'default' ) {
|
||||
|
||||
$setting = $this->get_setting( $setting );
|
||||
|
||||
return $setting ? $setting->get_value() : '';
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the setting object associated with this control. If no setting is
|
||||
* found, `false` is returned.
|
||||
*
|
||||
* @since 1.0.0
|
||||
* @access public
|
||||
* @param string $setting
|
||||
* @return object|bool
|
||||
*/
|
||||
public function get_setting( $setting = 'default' ) {
|
||||
|
||||
return $this->manager->get_setting( $this->settings[ $setting ] );
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the attributes for the control.
|
||||
*
|
||||
* @since 1.0.0
|
||||
* @access public
|
||||
* @return array
|
||||
*/
|
||||
public function get_attr() {
|
||||
|
||||
$defaults = array();
|
||||
|
||||
if ( isset( $this->settings[ $this->setting ] ) )
|
||||
$defaults['name'] = $this->get_field_name( $this->setting );
|
||||
|
||||
return wp_parse_args( $this->attr, $defaults );
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the HTML field name for the control.
|
||||
*
|
||||
* @since 1.0.0
|
||||
* @access public
|
||||
* @param string $setting
|
||||
* @return array
|
||||
*/
|
||||
public function get_field_name( $setting = 'default' ) {
|
||||
|
||||
return "butterbean_{$this->manager->name}_setting_{$this->settings[ $setting ]}";
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the json array.
|
||||
*
|
||||
* @since 1.0.0
|
||||
* @access public
|
||||
* @return array
|
||||
*/
|
||||
public function get_json() {
|
||||
$this->to_json();
|
||||
|
||||
return $this->json;
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds custom data to the json array. This data is passed to the Underscore template.
|
||||
*
|
||||
* @since 1.0.0
|
||||
* @access public
|
||||
* @return void
|
||||
*/
|
||||
public function to_json() {
|
||||
|
||||
$this->json['manager'] = $this->manager->name;
|
||||
$this->json['section'] = $this->section;
|
||||
$this->json['setting'] = $this->setting;
|
||||
$this->json['settings'] = $this->settings;
|
||||
$this->json['name'] = $this->name;
|
||||
$this->json['label'] = $this->label;
|
||||
$this->json['type'] = $this->type;
|
||||
$this->json['description'] = $this->description;
|
||||
$this->json['choices'] = $this->choices;
|
||||
$this->json['active'] = $this->is_active();
|
||||
|
||||
$this->json['value'] = isset( $this->settings[ $this->setting ] ) ? $this->get_value( $this->setting ) : '';
|
||||
$this->json['field_name'] = isset( $this->settings[ $this->setting ] ) ? $this->get_field_name( $this->setting ) : '';
|
||||
|
||||
$this->json['attr'] = '';
|
||||
|
||||
foreach ( $this->get_attr() as $attr => $value ) {
|
||||
$this->json['attr'] .= sprintf( '%s="%s" ', esc_html( $attr ), esc_attr( $value ) );
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns whether the control is active.
|
||||
*
|
||||
* @since 1.0.0
|
||||
* @access public
|
||||
* @return bool
|
||||
*/
|
||||
public function is_active() {
|
||||
|
||||
$is_active = call_user_func( $this->active_callback, $this );
|
||||
|
||||
return apply_filters( 'butterbean_is_control_active', $is_active, $this );
|
||||
}
|
||||
|
||||
/**
|
||||
* Default active callback.
|
||||
*
|
||||
* @since 1.0.0
|
||||
* @access public
|
||||
* @return bool
|
||||
*/
|
||||
public function active_callback() {
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if the control should be allowed at all.
|
||||
*
|
||||
* @since 1.0.0
|
||||
* @access public
|
||||
* @return bool
|
||||
*/
|
||||
public function check_capabilities() {
|
||||
|
||||
if ( $this->capability && ! call_user_func_array( 'current_user_can', (array) $this->capability ) )
|
||||
return false;
|
||||
|
||||
if ( $this->post_type_supports && ! call_user_func_array( 'post_type_supports', array( get_post_type( $this->manager->post_id ), $this->post_type_supports ) ) )
|
||||
return false;
|
||||
|
||||
if ( $this->theme_supports && ! call_user_func_array( 'theme_supports', (array) $this->theme_supports ) )
|
||||
return false;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Prints Underscore.js template.
|
||||
*
|
||||
* @since 1.0.0
|
||||
* @access public
|
||||
* @return void
|
||||
*/
|
||||
public function print_template() { ?>
|
||||
|
||||
<script type="text/html" id="tmpl-butterbean-control-<?php echo esc_attr( $this->type ); ?>">
|
||||
<?php $this->get_template(); ?>
|
||||
</script>
|
||||
<?php }
|
||||
|
||||
/**
|
||||
* Gets the Underscore.js template.
|
||||
*
|
||||
* @since 1.0.0
|
||||
* @access public
|
||||
* @return void
|
||||
*/
|
||||
public function get_template() {
|
||||
butterbean_get_control_template( $this->type );
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,550 @@
|
||||
<?php
|
||||
/**
|
||||
* Base class for handling managers. Managers are groups of sections, which are groups of
|
||||
* controls + settings. Managers are output as a metabox. This essentially allows
|
||||
* developers to output multiple post meta fields within a single metabox.
|
||||
*
|
||||
* @package ButterBean
|
||||
* @author Justin Tadlock <justin@justintadlock.com>
|
||||
* @copyright Copyright (c) 2015-2016, Justin Tadlock
|
||||
* @link https://github.com/justintadlock/butterbean
|
||||
* @license http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
|
||||
*/
|
||||
|
||||
/**
|
||||
* Base manager class.
|
||||
*
|
||||
* @since 1.0.0
|
||||
* @access public
|
||||
*/
|
||||
class ButterBean_Manager {
|
||||
|
||||
/**
|
||||
* The type of manager.
|
||||
*
|
||||
* @since 1.0.0
|
||||
* @access public
|
||||
* @var string
|
||||
*/
|
||||
public $type = 'default';
|
||||
|
||||
/**
|
||||
* Name of this instance of the manager.
|
||||
*
|
||||
* @since 1.0.0
|
||||
* @access public
|
||||
* @var string
|
||||
*/
|
||||
public $name = '';
|
||||
|
||||
/**
|
||||
* Label for the manager.
|
||||
*
|
||||
* @since 1.0.0
|
||||
* @access public
|
||||
* @var string
|
||||
*/
|
||||
public $label = '';
|
||||
|
||||
/**
|
||||
* Post type this manager is used on.
|
||||
*
|
||||
* @since 1.0.0
|
||||
* @access public
|
||||
* @var string|array
|
||||
*/
|
||||
public $post_type = 'post';
|
||||
|
||||
/**
|
||||
* Location of the meta box. Accepted values: 'normal', 'advanced', 'side'.
|
||||
*
|
||||
* @link https://developer.wordpress.org/reference/functions/add_meta_box/
|
||||
* @since 1.0.0
|
||||
* @access public
|
||||
* @var string
|
||||
*/
|
||||
public $context = 'advanced';
|
||||
|
||||
/**
|
||||
* Priority of the meta box. Accepted values: 'high', 'core', 'default', 'low'.
|
||||
*
|
||||
* @link https://developer.wordpress.org/reference/functions/add_meta_box/
|
||||
* @since 1.0.0
|
||||
* @access public
|
||||
* @var string
|
||||
*/
|
||||
public $priority = 'default';
|
||||
|
||||
/**
|
||||
* Array of sections.
|
||||
*
|
||||
* @since 1.0.0
|
||||
* @access public
|
||||
* @var array
|
||||
*/
|
||||
public $sections = array();
|
||||
|
||||
/**
|
||||
* Array of controls.
|
||||
*
|
||||
* @since 1.0.0
|
||||
* @access public
|
||||
* @var array
|
||||
*/
|
||||
public $controls = array();
|
||||
|
||||
/**
|
||||
* Array of settings.
|
||||
*
|
||||
* @since 1.0.0
|
||||
* @access public
|
||||
* @var array
|
||||
*/
|
||||
public $settings = array();
|
||||
|
||||
/**
|
||||
* A user role capability required to show the manager.
|
||||
*
|
||||
* @since 1.0.0
|
||||
* @access public
|
||||
* @var string|array
|
||||
*/
|
||||
public $capability = '';
|
||||
|
||||
/**
|
||||
* A feature that the current post type must support to show the manager.
|
||||
*
|
||||
* @since 1.0.0
|
||||
* @access public
|
||||
* @var string
|
||||
*/
|
||||
public $post_type_supports = '';
|
||||
|
||||
/**
|
||||
* A feature that the current theme must support to show the manager.
|
||||
*
|
||||
* @since 1.0.0
|
||||
* @access public
|
||||
* @var string|array
|
||||
*/
|
||||
public $theme_supports = '';
|
||||
|
||||
/**
|
||||
* Stores the JSON data for the manager.
|
||||
*
|
||||
* @since 1.0.0
|
||||
* @access public
|
||||
* @var array()
|
||||
*/
|
||||
public $json = array();
|
||||
|
||||
/**
|
||||
* ID of the post that's being edited.
|
||||
*
|
||||
* @since 1.0.0
|
||||
* @access public
|
||||
* @var int
|
||||
*/
|
||||
public $post_id = 0;
|
||||
|
||||
/**
|
||||
* Sets up the manager.
|
||||
*
|
||||
* @since 1.0.0
|
||||
* @access public
|
||||
* @param string $name
|
||||
* @param array $args
|
||||
* @return void
|
||||
*/
|
||||
public function __construct( $name, $args = array() ) {
|
||||
|
||||
foreach ( array_keys( get_object_vars( $this ) ) as $key ) {
|
||||
|
||||
if ( isset( $args[ $key ] ) )
|
||||
$this->$key = $args[ $key ];
|
||||
}
|
||||
|
||||
// Make sure the post type is an array.
|
||||
$this->post_type = (array) $this->post_type;
|
||||
|
||||
// Set the manager name.
|
||||
$this->name = sanitize_key( $name );
|
||||
}
|
||||
|
||||
/**
|
||||
* Enqueue scripts/styles for the manager.
|
||||
*
|
||||
* @since 1.0.0
|
||||
* @access public
|
||||
* @return void
|
||||
*/
|
||||
public function enqueue() {}
|
||||
|
||||
/**
|
||||
* Register a section.
|
||||
*
|
||||
* @since 1.0.0
|
||||
* @access public
|
||||
* @param object|string $section
|
||||
* @param array $args
|
||||
* @return void
|
||||
*/
|
||||
public function register_section( $section, $args = array() ) {
|
||||
|
||||
if ( ! is_object( $section ) ) {
|
||||
|
||||
$type = isset( $args['type'] ) ? butterbean()->get_section_type( $args['type'] ) : butterbean()->get_section_type( 'default' );
|
||||
|
||||
$section = new $type( $this, $section, $args );
|
||||
}
|
||||
|
||||
if ( ! $this->section_exists( $section->name ) )
|
||||
$this->sections[ $section->name ] = $section;
|
||||
}
|
||||
|
||||
/**
|
||||
* Register a control.
|
||||
*
|
||||
* @since 1.0.0
|
||||
* @access public
|
||||
* @param object|string $control
|
||||
* @param array $args
|
||||
* @return void
|
||||
*/
|
||||
public function register_control( $control, $args = array() ) {
|
||||
|
||||
if ( ! is_object( $control ) ) {
|
||||
|
||||
$type = isset( $args['type'] ) ? butterbean()->get_control_type( $args['type'] ) : butterbean()->get_control_type( 'default' );
|
||||
|
||||
$control = new $type( $this, $control, $args );
|
||||
}
|
||||
|
||||
if ( ! $this->control_exists( $control->name ) )
|
||||
$this->controls[ $control->name ] = $control;
|
||||
}
|
||||
|
||||
/**
|
||||
* Register a setting.
|
||||
*
|
||||
* @since 1.0.0
|
||||
* @access public
|
||||
* @param object|string $setting
|
||||
* @param array $args
|
||||
* @return void
|
||||
*/
|
||||
public function register_setting( $setting, $args = array() ) {
|
||||
|
||||
if ( ! is_object( $setting ) ) {
|
||||
|
||||
$type = isset( $args['type'] ) ? butterbean()->get_setting_type( $args['type'] ) : butterbean()->get_setting_type( 'default' );
|
||||
|
||||
$setting = new $type( $this, $setting, $args );
|
||||
}
|
||||
|
||||
if ( ! $this->setting_exists( $setting->name ) )
|
||||
$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.
|
||||
*
|
||||
* @since 1.0.0
|
||||
* @access public
|
||||
* @param string $name
|
||||
* @return void
|
||||
*/
|
||||
public function unregister_section( $name ) {
|
||||
|
||||
if ( $this->section_exists( $name ) )
|
||||
unset( $this->sections[ $name ] );
|
||||
}
|
||||
|
||||
/**
|
||||
* Unregisters a control object.
|
||||
*
|
||||
* @since 1.0.0
|
||||
* @access public
|
||||
* @param string $name
|
||||
* @return void
|
||||
*/
|
||||
public function unregister_control( $name ) {
|
||||
|
||||
if ( $this->control_exists( $name ) )
|
||||
unset( $this->controls[ $name ] );
|
||||
}
|
||||
|
||||
/**
|
||||
* Unregisters a setting object.
|
||||
*
|
||||
* @since 1.0.0
|
||||
* @access public
|
||||
* @param string $name
|
||||
* @return void
|
||||
*/
|
||||
public function unregister_setting( $name ) {
|
||||
|
||||
if ( $this->setting_exists( $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.
|
||||
*
|
||||
* @since 1.0.0
|
||||
* @access public
|
||||
* @param string $name
|
||||
* @return object|bool
|
||||
*/
|
||||
public function get_section( $name ) {
|
||||
|
||||
return $this->section_exists( $name ) ? $this->sections[ $name ] : false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a control object.
|
||||
*
|
||||
* @since 1.0.0
|
||||
* @access public
|
||||
* @param string $name
|
||||
* @return object|bool
|
||||
*/
|
||||
public function get_control( $name ) {
|
||||
|
||||
return $this->control_exists( $name ) ? $this->controls[ $name ] : false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a setting object.
|
||||
*
|
||||
* @since 1.0.0
|
||||
* @access public
|
||||
* @param string $name
|
||||
* @return object|bool
|
||||
*/
|
||||
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.
|
||||
*
|
||||
* @since 1.0.0
|
||||
* @access public
|
||||
* @param string $name
|
||||
* @return bool
|
||||
*/
|
||||
public function section_exists( $name ) {
|
||||
|
||||
return isset( $this->sections[ $name ] );
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if a control exists.
|
||||
*
|
||||
* @since 1.0.0
|
||||
* @access public
|
||||
* @param string $name
|
||||
* @return bool
|
||||
*/
|
||||
public function control_exists( $name ) {
|
||||
|
||||
return isset( $this->controls[ $name ] );
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if a setting exists.
|
||||
*
|
||||
* @since 1.0.0
|
||||
* @access public
|
||||
* @param string $name
|
||||
* @return bool
|
||||
*/
|
||||
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.
|
||||
*
|
||||
* @since 1.0.0
|
||||
* @access public
|
||||
* @return array
|
||||
*/
|
||||
public function get_json() {
|
||||
$this->to_json();
|
||||
|
||||
return $this->json;
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds custom data to the JSON array. This data is passed to the Underscore template.
|
||||
*
|
||||
* @since 1.0.0
|
||||
* @access public
|
||||
* @return void
|
||||
*/
|
||||
public function to_json() {
|
||||
|
||||
$sections_with_controls = array();
|
||||
$blocked_sections = array();
|
||||
|
||||
$this->json['name'] = $this->name;
|
||||
$this->json['type'] = $this->type;
|
||||
|
||||
// Get all sections that have controls.
|
||||
foreach ( $this->controls as $control )
|
||||
$sections_with_controls[] = $control->section;
|
||||
|
||||
$sections_with_controls = array_unique( $sections_with_controls );
|
||||
|
||||
// Get the JSON data for each section.
|
||||
foreach ( $this->sections as $section ) {
|
||||
|
||||
$caps = $section->check_capabilities();
|
||||
|
||||
if ( $caps && in_array( $section->name, $sections_with_controls ) )
|
||||
$this->json['sections'][] = $section->get_json();
|
||||
|
||||
if ( ! $caps )
|
||||
$blocked_sections[] = $section->name;
|
||||
}
|
||||
|
||||
// Get the JSON data for each control.
|
||||
foreach ( $this->controls as $control ) {
|
||||
|
||||
if ( $control->check_capabilities() && ! in_array( $control->section, $blocked_sections ) )
|
||||
$this->json['controls'][] = $control->get_json();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Saves each of the settings for the manager.
|
||||
*
|
||||
* @since 1.0.0
|
||||
* @access public
|
||||
* @return void
|
||||
*/
|
||||
public function save( $post_id ) {
|
||||
|
||||
if ( ! $this->post_id )
|
||||
$this->post_id = $post_id;
|
||||
|
||||
// Verify the nonce for this manager.
|
||||
if ( ! isset( $_POST["butterbean_{$this->name}"] ) || ! wp_verify_nonce( $_POST["butterbean_{$this->name}"], "butterbean_{$this->name}_nonce" ) )
|
||||
return;
|
||||
|
||||
// Loop through each setting and save it.
|
||||
foreach ( $this->settings as $setting )
|
||||
$setting->save();
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if the control should be allowed at all.
|
||||
*
|
||||
* @since 1.0.0
|
||||
* @access public
|
||||
* @return bool
|
||||
*/
|
||||
public function check_capabilities() {
|
||||
|
||||
if ( $this->capability && ! call_user_func_array( 'current_user_can', (array) $this->capability ) )
|
||||
return false;
|
||||
|
||||
if ( $this->post_type_supports && ! call_user_func_array( 'post_type_supports', array( get_post_type( $this->manager->post_id ), $this->post_type_supports ) ) )
|
||||
return false;
|
||||
|
||||
if ( $this->theme_supports && ! call_user_func_array( 'theme_supports', (array) $this->theme_supports ) )
|
||||
return false;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Prints Underscore.js template.
|
||||
*
|
||||
* @since 1.0.0
|
||||
* @access public
|
||||
* @return void
|
||||
*/
|
||||
public function print_template() { ?>
|
||||
|
||||
<script type="text/html" id="tmpl-butterbean-manager-<?php echo esc_attr( $this->type ); ?>">
|
||||
<?php $this->get_template(); ?>
|
||||
</script>
|
||||
<?php }
|
||||
|
||||
/**
|
||||
* Gets the Underscore.js template.
|
||||
*
|
||||
* @since 1.0.0
|
||||
* @access public
|
||||
* @return void
|
||||
*/
|
||||
public function get_template() {
|
||||
butterbean_get_manager_template( $this->type );
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,291 @@
|
||||
<?php
|
||||
/**
|
||||
* Base class for handling sections. Sections house groups of controls. Multiple sections can
|
||||
* be added to a manager.
|
||||
*
|
||||
* @package ButterBean
|
||||
* @subpackage Admin
|
||||
* @author Justin Tadlock <justin@justintadlock.com>
|
||||
* @copyright Copyright (c) 2015-2016, Justin Tadlock
|
||||
* @link https://github.com/justintadlock/butterbean
|
||||
* @license http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
|
||||
*/
|
||||
|
||||
/**
|
||||
* Base section class.
|
||||
*
|
||||
* @since 1.0.0
|
||||
* @access public
|
||||
*/
|
||||
class ButterBean_Section {
|
||||
|
||||
/**
|
||||
* Stores the project details manager object.
|
||||
*
|
||||
* @since 1.0.0
|
||||
* @access public
|
||||
* @var object
|
||||
*/
|
||||
public $manager;
|
||||
|
||||
/**
|
||||
* Name/ID of the section.
|
||||
*
|
||||
* @since 1.0.0
|
||||
* @access public
|
||||
* @var string
|
||||
*/
|
||||
public $name = '';
|
||||
|
||||
/**
|
||||
* The type of section.
|
||||
*
|
||||
* @since 1.0.0
|
||||
* @access public
|
||||
* @var string
|
||||
*/
|
||||
public $type = 'default';
|
||||
|
||||
/**
|
||||
* Dashicons icon for the section.
|
||||
*
|
||||
* @since 1.0.0
|
||||
* @access public
|
||||
* @var string
|
||||
*/
|
||||
public $icon = 'dashicons-admin-generic';
|
||||
|
||||
/**
|
||||
* Label for the section.
|
||||
*
|
||||
* @since 1.0.0
|
||||
* @access public
|
||||
* @var string
|
||||
*/
|
||||
public $label = '';
|
||||
|
||||
/**
|
||||
* Description for the section.
|
||||
*
|
||||
* @since 1.0.0
|
||||
* @access public
|
||||
* @var string
|
||||
*/
|
||||
public $description = '';
|
||||
|
||||
/**
|
||||
* Priority (order) the section should be output.
|
||||
*
|
||||
* @since 1.0.0
|
||||
* @access public
|
||||
* @var int
|
||||
*/
|
||||
public $priority = 10;
|
||||
|
||||
/**
|
||||
* The number of instances created.
|
||||
*
|
||||
* @since 1.0.0
|
||||
* @access protected
|
||||
* @var int
|
||||
*/
|
||||
protected static $instance_count = 0;
|
||||
|
||||
/**
|
||||
* The instance of the current section.
|
||||
*
|
||||
* @since 1.0.0
|
||||
* @access public
|
||||
* @var int
|
||||
*/
|
||||
public $instance_number;
|
||||
|
||||
/**
|
||||
* A callback function for deciding if a section is active.
|
||||
*
|
||||
* @since 1.0.0
|
||||
* @access public
|
||||
* @var callable
|
||||
*/
|
||||
public $active_callback = '';
|
||||
|
||||
/**
|
||||
* A user role capability required to show the section.
|
||||
*
|
||||
* @since 1.0.0
|
||||
* @access public
|
||||
* @var string|array
|
||||
*/
|
||||
public $capability = '';
|
||||
|
||||
/**
|
||||
* A feature that the current post type must support to show the section.
|
||||
*
|
||||
* @since 1.0.0
|
||||
* @access public
|
||||
* @var string
|
||||
*/
|
||||
public $post_type_supports = '';
|
||||
|
||||
/**
|
||||
* A feature that the current theme must support to show the section.
|
||||
*
|
||||
* @since 1.0.0
|
||||
* @access public
|
||||
* @var string|array
|
||||
*/
|
||||
public $theme_supports = '';
|
||||
|
||||
/**
|
||||
* Stores the JSON data for the manager.
|
||||
*
|
||||
* @since 1.0.0
|
||||
* @access public
|
||||
* @var array()
|
||||
*/
|
||||
public $json = array();
|
||||
|
||||
/**
|
||||
* Creates a new section object.
|
||||
*
|
||||
* @since 1.0.0
|
||||
* @access public
|
||||
* @param object $manager
|
||||
* @param string $section
|
||||
* @param array $args
|
||||
* @return void
|
||||
*/
|
||||
public function __construct( $manager, $name, $args = array() ) {
|
||||
|
||||
foreach ( array_keys( get_object_vars( $this ) ) as $key ) {
|
||||
|
||||
if ( isset( $args[ $key ] ) )
|
||||
$this->$key = $args[ $key ];
|
||||
}
|
||||
|
||||
$this->manager = $manager;
|
||||
$this->name = $name;
|
||||
|
||||
// Increment the instance count and set the instance number.
|
||||
self::$instance_count += 1;
|
||||
$this->instance_number = self::$instance_count;
|
||||
|
||||
// Set the active callback function if not set.
|
||||
if ( ! $this->active_callback )
|
||||
$this->active_callback = array( $this, 'active_callback' );
|
||||
}
|
||||
|
||||
/**
|
||||
* Enqueue scripts/styles for the section.
|
||||
*
|
||||
* @since 1.0.0
|
||||
* @access public
|
||||
* @return void
|
||||
*/
|
||||
public function enqueue() {}
|
||||
|
||||
/**
|
||||
* Returns the json array.
|
||||
*
|
||||
* @since 1.0.0
|
||||
* @access public
|
||||
* @return array
|
||||
*/
|
||||
public function get_json() {
|
||||
$this->to_json();
|
||||
|
||||
return $this->json;
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds custom data to the json array. This data is passed to the Underscore template.
|
||||
*
|
||||
* @since 1.0.0
|
||||
* @access public
|
||||
* @return void
|
||||
*/
|
||||
public function to_json() {
|
||||
|
||||
$this->json['manager'] = $this->manager->name;
|
||||
$this->json['name'] = $this->name;
|
||||
$this->json['type'] = $this->type;
|
||||
$this->json['icon'] = preg_match( '/dashicons-/', $this->icon ) ? sprintf( 'dashicons %s', sanitize_html_class( $this->icon ) ) : esc_attr( $this->icon );
|
||||
$this->json['label'] = $this->label;
|
||||
$this->json['description'] = $this->description;
|
||||
$this->json['active'] = $this->is_active();
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns whether the section is active.
|
||||
*
|
||||
* @since 1.0.0
|
||||
* @access public
|
||||
* @return bool
|
||||
*/
|
||||
public function is_active() {
|
||||
|
||||
$is_active = call_user_func( $this->active_callback, $this );
|
||||
|
||||
if ( $is_active )
|
||||
$is_active = $this->check_capabilities();
|
||||
|
||||
return apply_filters( 'butterbean_is_section_active', $is_active, $this );
|
||||
}
|
||||
|
||||
/**
|
||||
* Default active callback.
|
||||
*
|
||||
* @since 1.0.0
|
||||
* @access public
|
||||
* @return bool
|
||||
*/
|
||||
public function active_callback() {
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if the section should be allowed at all.
|
||||
*
|
||||
* @since 1.0.0
|
||||
* @access public
|
||||
* @return bool
|
||||
*/
|
||||
public function check_capabilities() {
|
||||
|
||||
if ( $this->capability && ! call_user_func_array( 'current_user_can', (array) $this->capability ) )
|
||||
return false;
|
||||
|
||||
if ( $this->post_type_supports && ! call_user_func_array( 'post_type_supports', array( get_post_type( $this->manager->post_id ), $this->post_type_supports ) ) )
|
||||
return false;
|
||||
|
||||
if ( $this->theme_supports && ! call_user_func_array( 'theme_supports', (array) $this->theme_supports ) )
|
||||
return false;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Prints Underscore.js template.
|
||||
*
|
||||
* @since 1.0.0
|
||||
* @access public
|
||||
* @return void
|
||||
*/
|
||||
public function print_template() { ?>
|
||||
|
||||
<script type="text/html" id="tmpl-butterbean-section-<?php echo esc_attr( $this->type ); ?>">
|
||||
<?php $this->get_template(); ?>
|
||||
</script>
|
||||
<?php }
|
||||
|
||||
/**
|
||||
* Gets the Underscore.js template.
|
||||
*
|
||||
* @since 1.0.0
|
||||
* @access public
|
||||
* @return void
|
||||
*/
|
||||
public function get_template() {
|
||||
butterbean_get_section_template( $this->type );
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,226 @@
|
||||
<?php
|
||||
/**
|
||||
* Base setting class for the fields manager.
|
||||
*
|
||||
* @package ButterBean
|
||||
* @subpackage Admin
|
||||
* @author Justin Tadlock <justin@justintadlock.com>
|
||||
* @copyright Copyright (c) 2015-2016, Justin Tadlock
|
||||
* @link https://github.com/justintadlock/butterbean
|
||||
* @license http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
|
||||
*/
|
||||
|
||||
/**
|
||||
* Base setting class.
|
||||
*
|
||||
* @since 1.0.0
|
||||
* @access public
|
||||
*/
|
||||
class ButterBean_Setting {
|
||||
|
||||
/**
|
||||
* The type of setting.
|
||||
*
|
||||
* @since 1.0.0
|
||||
* @access public
|
||||
* @var string
|
||||
*/
|
||||
public $type = 'default';
|
||||
|
||||
/**
|
||||
* Stores the manager object.
|
||||
*
|
||||
* @since 1.0.0
|
||||
* @access public
|
||||
* @var object
|
||||
*/
|
||||
public $manager;
|
||||
|
||||
/**
|
||||
* Name/ID of the setting.
|
||||
*
|
||||
* @since 1.0.0
|
||||
* @access public
|
||||
* @var string
|
||||
*/
|
||||
public $name = '';
|
||||
|
||||
/**
|
||||
* Value of the setting.
|
||||
*
|
||||
* @since 1.0.0
|
||||
* @access public
|
||||
* @var string
|
||||
*/
|
||||
public $value = '';
|
||||
|
||||
/**
|
||||
* Default value of the setting.
|
||||
*
|
||||
* @since 1.0.0
|
||||
* @access public
|
||||
* @var string
|
||||
*/
|
||||
public $default = '';
|
||||
|
||||
/**
|
||||
* Sanitization/Validation callback function.
|
||||
*
|
||||
* @since 1.0.0
|
||||
* @access public
|
||||
* @var string
|
||||
*/
|
||||
public $sanitize_callback = '';
|
||||
|
||||
/**
|
||||
* A user role capability required to save the setting.
|
||||
*
|
||||
* @since 1.0.0
|
||||
* @access public
|
||||
* @var string|array
|
||||
*/
|
||||
public $capability = '';
|
||||
|
||||
/**
|
||||
* A feature that the current post type must support to save the setting.
|
||||
*
|
||||
* @since 1.0.0
|
||||
* @access public
|
||||
* @var string
|
||||
*/
|
||||
public $post_type_supports = '';
|
||||
|
||||
/**
|
||||
* A feature that the current theme must support to save the setting.
|
||||
*
|
||||
* @since 1.0.0
|
||||
* @access public
|
||||
* @var string|array
|
||||
*/
|
||||
public $theme_supports = '';
|
||||
|
||||
/**
|
||||
* Creates a new setting object.
|
||||
*
|
||||
* @since 1.0.0
|
||||
* @access public
|
||||
* @param object $manager
|
||||
* @param string $cap
|
||||
* @param array $args
|
||||
* @return void
|
||||
*/
|
||||
public function __construct( $manager, $name, $args = array() ) {
|
||||
|
||||
foreach ( array_keys( get_object_vars( $this ) ) as $key ) {
|
||||
|
||||
if ( isset( $args[ $key ] ) )
|
||||
$this->$key = $args[ $key ];
|
||||
}
|
||||
|
||||
$this->manager = $manager;
|
||||
$this->name = $name;
|
||||
|
||||
if ( $this->sanitize_callback )
|
||||
add_filter( "butterbean_{$this->manager->name}_sanitize_{$this->name}", $this->sanitize_callback, 10, 2 );
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the value of the setting.
|
||||
*
|
||||
* @since 1.0.0
|
||||
* @access public
|
||||
* @return mixed
|
||||
*/
|
||||
public function get_value() {
|
||||
|
||||
$value = get_post_meta( $this->manager->post_id, $this->name, true );
|
||||
|
||||
return ! $value && butterbean()->is_new_post ? $this->default : $value;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the posted value of the setting.
|
||||
*
|
||||
* @since 1.0.0
|
||||
* @access public
|
||||
* @return mixed
|
||||
*/
|
||||
public function get_posted_value() {
|
||||
|
||||
$value = '';
|
||||
|
||||
if ( isset( $_POST[ $this->get_field_name() ] ) )
|
||||
$value = $_POST[ $this->get_field_name() ];
|
||||
|
||||
return $this->sanitize( $value );
|
||||
}
|
||||
|
||||
/**
|
||||
* Retuns the correct field name for the setting.
|
||||
*
|
||||
* @since 1.0.0
|
||||
* @access public
|
||||
* @return string
|
||||
*/
|
||||
public function get_field_name() {
|
||||
|
||||
return "butterbean_{$this->manager->name}_setting_{$this->name}";
|
||||
}
|
||||
|
||||
/**
|
||||
* Sanitizes the value of the setting.
|
||||
*
|
||||
* @since 1.0.0
|
||||
* @access public
|
||||
* @return mixed
|
||||
*/
|
||||
public function sanitize( $value ) {
|
||||
|
||||
return apply_filters( "butterbean_{$this->manager->name}_sanitize_{$this->name}", $value, $this );
|
||||
}
|
||||
|
||||
/**
|
||||
* Saves the value of the setting.
|
||||
*
|
||||
* @since 1.0.0
|
||||
* @access public
|
||||
* @return void
|
||||
*/
|
||||
public function save() {
|
||||
|
||||
if ( ! $this->check_capabilities() )
|
||||
return;
|
||||
|
||||
$old_value = $this->get_value();
|
||||
$new_value = $this->get_posted_value();
|
||||
|
||||
// If we have don't have a new value but do have an old one, delete it.
|
||||
if ( ! $new_value && $old_value )
|
||||
delete_post_meta( $this->manager->post_id, $this->name );
|
||||
|
||||
// If the new value doesn't match the old value, set it.
|
||||
else if ( $new_value !== $old_value )
|
||||
update_post_meta( $this->manager->post_id, $this->name, $new_value );
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if the setting should be saved at all.
|
||||
*
|
||||
* @since 1.0.0
|
||||
* @access public
|
||||
* @return bool
|
||||
*/
|
||||
public function check_capabilities() {
|
||||
|
||||
if ( $this->capability && ! call_user_func_array( 'current_user_can', (array) $this->capability ) )
|
||||
return false;
|
||||
|
||||
if ( $this->post_type_supports && ! call_user_func_array( 'post_type_supports', array( get_post_type( $this->manager->post_id ), $this->post_type_supports ) ) )
|
||||
return false;
|
||||
|
||||
if ( $this->theme_supports && ! call_user_func_array( 'theme_supports', (array) $this->theme_supports ) )
|
||||
return false;
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
+44
@@ -0,0 +1,44 @@
|
||||
<?php
|
||||
/**
|
||||
* Buttonset control class.
|
||||
*
|
||||
* @package ButterBean
|
||||
* @author Justin Tadlock <justin@justintadlock.com>
|
||||
* @copyright Copyright (c) 2015-2016, Justin Tadlock
|
||||
* @link https://github.com/justintadlock/butterbean
|
||||
* @license http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
|
||||
*/
|
||||
|
||||
/**
|
||||
* Buttonset control class.
|
||||
*
|
||||
* @since 1.0.0
|
||||
* @access public
|
||||
*/
|
||||
class OceanWP_ButterBean_Control_Buttonset extends OceanWP_ButterBean_Control {
|
||||
|
||||
/**
|
||||
* The type of control.
|
||||
*
|
||||
* @since 1.0.0
|
||||
* @access public
|
||||
* @var string
|
||||
*/
|
||||
public $type = 'buttonset';
|
||||
|
||||
/**
|
||||
* Get the value for the setting.
|
||||
*
|
||||
* @since 1.0.0
|
||||
* @access public
|
||||
* @param string $setting
|
||||
* @return mixed
|
||||
*/
|
||||
public function get_value( $setting = 'default' ) {
|
||||
|
||||
$value = parent::get_value( $setting );
|
||||
$object = $this->get_setting( $setting );
|
||||
|
||||
return ! $value && $object ? $object->default : $value;
|
||||
}
|
||||
}
|
||||
+43
@@ -0,0 +1,43 @@
|
||||
<?php
|
||||
/**
|
||||
* Multiple checkbox control class. This is for array-type settings, so you'll need
|
||||
* to utilize a setting type that handles arrays. Both the `array` and `multiple`
|
||||
* setting types will do this.
|
||||
*
|
||||
* @package ButterBean
|
||||
* @author Justin Tadlock <justin@justintadlock.com>
|
||||
* @copyright Copyright (c) 2015-2016, Justin Tadlock
|
||||
* @link https://github.com/justintadlock/butterbean
|
||||
* @license http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
|
||||
*/
|
||||
|
||||
/**
|
||||
* Multiple checkboxes control class.
|
||||
*
|
||||
* @since 1.0.0
|
||||
* @access public
|
||||
*/
|
||||
class ButterBean_Control_CheckBoxes extends ButterBean_Control {
|
||||
|
||||
/**
|
||||
* The type of control.
|
||||
*
|
||||
* @since 1.0.0
|
||||
* @access public
|
||||
* @var string
|
||||
*/
|
||||
public $type = 'checkboxes';
|
||||
|
||||
/**
|
||||
* Adds custom data to the json array. This data is passed to the Underscore template.
|
||||
*
|
||||
* @since 1.0.0
|
||||
* @access public
|
||||
* @return void
|
||||
*/
|
||||
public function to_json() {
|
||||
parent::to_json();
|
||||
|
||||
$this->json['value'] = (array) $this->get_value();
|
||||
}
|
||||
}
|
||||
+104
@@ -0,0 +1,104 @@
|
||||
<?php
|
||||
/**
|
||||
* Color control class. This class uses the core WordPress color picker. Expected
|
||||
* values are hex colors. This class also attempts to strip `#` from the hex color.
|
||||
* By design, it's recommended to add the `#` on output.
|
||||
*
|
||||
* @package ButterBean
|
||||
* @author Justin Tadlock <justin@justintadlock.com>
|
||||
* @copyright Copyright (c) 2015-2016, Justin Tadlock
|
||||
* @link https://github.com/justintadlock/butterbean
|
||||
* @license http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
|
||||
*/
|
||||
|
||||
/**
|
||||
* Color control class.
|
||||
*
|
||||
* @since 1.0.0
|
||||
* @access public
|
||||
*/
|
||||
class ButterBean_Control_Color extends ButterBean_Control {
|
||||
|
||||
/**
|
||||
* The type of control.
|
||||
*
|
||||
* @since 1.0.0
|
||||
* @access public
|
||||
* @var string
|
||||
*/
|
||||
public $type = 'color';
|
||||
|
||||
/**
|
||||
* Custom options to pass to the color picker. Mostly, this is a wrapper for
|
||||
* `iris()`, which is bundled with core WP. However, if they change pickers
|
||||
* in the future, it may correspond to a different script.
|
||||
*
|
||||
* @link http://automattic.github.io/Iris/#options
|
||||
* @link https://make.wordpress.org/core/2012/11/30/new-color-picker-in-wp-3-5/
|
||||
* @since 1.0.0
|
||||
* @access public
|
||||
* @var array
|
||||
*/
|
||||
public $options = array();
|
||||
|
||||
/**
|
||||
* Enqueue scripts/styles for the control.
|
||||
*
|
||||
* @since 1.0.0
|
||||
* @access public
|
||||
* @return void
|
||||
*/
|
||||
public function enqueue() {
|
||||
|
||||
wp_enqueue_script( 'wp-color-picker' );
|
||||
wp_enqueue_style( 'wp-color-picker' );
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the attributes for the control.
|
||||
*
|
||||
* @since 1.0.0
|
||||
* @access public
|
||||
* @return array
|
||||
*/
|
||||
public function get_attr() {
|
||||
$attr = parent::get_attr();
|
||||
|
||||
$setting = $this->get_setting();
|
||||
|
||||
$attr['class'] = 'butterbean-color-picker';
|
||||
$attr['type'] = 'text';
|
||||
$attr['maxlength'] = 7;
|
||||
$attr['data-default-color'] = $setting ? $setting->default : '';
|
||||
|
||||
return $attr;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the value for the setting.
|
||||
*
|
||||
* @since 1.0.0
|
||||
* @access public
|
||||
* @param string $setting
|
||||
* @return mixed
|
||||
*/
|
||||
public function get_value( $setting = 'default' ) {
|
||||
|
||||
$value = parent::get_value( $setting );
|
||||
|
||||
return ltrim( $value, '#' );
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds custom data to the json array. This data is passed to the Underscore template.
|
||||
*
|
||||
* @since 1.0.0
|
||||
* @access public
|
||||
* @return void
|
||||
*/
|
||||
public function to_json() {
|
||||
parent::to_json();
|
||||
|
||||
$this->json['options'] = $this->options;
|
||||
}
|
||||
}
|
||||
+140
@@ -0,0 +1,140 @@
|
||||
<?php
|
||||
/**
|
||||
* Datetime control class. This class is meant for storing a datetime in the format
|
||||
* of `YYYY-MM-DD HH:MM:SS` or `0000-00-00 00:00:00`. You can set the `$show_time`
|
||||
* property to `false`.
|
||||
*
|
||||
* Note that this control should be used in conjunction with the `datetime` setting
|
||||
* type or another custom class that can handle the datetime.
|
||||
*
|
||||
* @package ButterBean
|
||||
* @subpackage Admin
|
||||
* @author Justin Tadlock <justin@justintadlock.com>
|
||||
* @copyright Copyright (c) 2015-2016, Justin Tadlock
|
||||
* @link https://github.com/justintadlock/butterbean
|
||||
* @license http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
|
||||
*/
|
||||
|
||||
/**
|
||||
* Datetime control class.
|
||||
*
|
||||
* @since 1.0.0
|
||||
* @access public
|
||||
*/
|
||||
class ButterBean_Control_Datetime extends ButterBean_Control {
|
||||
|
||||
/**
|
||||
* The type of control.
|
||||
*
|
||||
* @since 1.0.0
|
||||
* @access public
|
||||
* @var string
|
||||
*/
|
||||
public $type = 'datetime';
|
||||
|
||||
/**
|
||||
* Whether to show the time. Note that settings, particularly the
|
||||
* `ButterBean_Setting_Date` class will store the time as `00:00:00` if
|
||||
* no time is provided.
|
||||
*
|
||||
* @since 1.0.0
|
||||
* @access public
|
||||
* @var bool
|
||||
*/
|
||||
public $show_time = true;
|
||||
|
||||
/**
|
||||
* Adds custom data to the json array. This data is passed to the Underscore template.
|
||||
*
|
||||
* @since 1.0.0
|
||||
* @access public
|
||||
* @globl object $wp_locale
|
||||
* @return void
|
||||
*/
|
||||
public function to_json() {
|
||||
global $wp_locale;
|
||||
|
||||
parent::to_json();
|
||||
|
||||
$this->json['show_time'] = $this->show_time;
|
||||
|
||||
$field_name = $this->get_field_name();
|
||||
|
||||
// Get project start/end dates.
|
||||
$date = $this->get_value();
|
||||
|
||||
// Get the year, month, and day.
|
||||
$year = $date ? mysql2date( 'Y', $date, false ) : '';
|
||||
$month = $date ? mysql2date( 'm', $date, false ) : '';
|
||||
$day = $date ? mysql2date( 'd', $date, false ) : '';
|
||||
|
||||
// Get the hour, minute, and second.
|
||||
$hour = $date ? mysql2date( 'H', $date, false ) : '';
|
||||
$minute = $date ? mysql2date( 'i', $date, false ) : '';
|
||||
$second = $date ? mysql2date( 's', $date, false ) : '';
|
||||
|
||||
// Year
|
||||
$this->json['year'] = array(
|
||||
'value' => esc_attr( $year ),
|
||||
'label' => esc_html__( 'Year', 'butterbean' ),
|
||||
'name' => esc_attr( "{$field_name}_year" ),
|
||||
'attr' => sprintf( 'placeholder="%s" size="4" maxlength="4" autocomplete="off"', esc_attr( date_i18n( 'Y' ) ) )
|
||||
);
|
||||
|
||||
// Month
|
||||
$this->json['month'] = array(
|
||||
'value' => esc_attr( $month ),
|
||||
'name' => esc_attr( "{$field_name}_month" ),
|
||||
'label' => esc_html__( 'Month', 'butterbean' ),
|
||||
'choices' => array(
|
||||
array(
|
||||
'num' => '',
|
||||
'label' => ''
|
||||
)
|
||||
)
|
||||
);
|
||||
|
||||
for ( $i = 1; $i < 13; $i = $i +1 ) {
|
||||
|
||||
$monthnum = zeroise( $i, 2 );
|
||||
$monthtext = $wp_locale->get_month_abbrev( $wp_locale->get_month( $i ) );
|
||||
|
||||
$this->json['month']['choices'][] = array(
|
||||
'num' => $monthnum,
|
||||
'label' => $monthtext
|
||||
);
|
||||
}
|
||||
|
||||
// Day
|
||||
$this->json['day'] = array(
|
||||
'value' => esc_attr( $day ),
|
||||
'name' => esc_attr( "{$field_name}_day" ),
|
||||
'label' => esc_html__( 'Day', 'butterbean' ),
|
||||
'attr' => sprintf( 'placeholder="%s" size="2" maxlength="2" autocomplete="off"', esc_attr( date_i18n( 'd' ) ) )
|
||||
);
|
||||
|
||||
// Hour
|
||||
$this->json['hour'] = array(
|
||||
'value' => esc_attr( $hour ),
|
||||
'name' => esc_attr( "{$field_name}_hour" ),
|
||||
'label' => esc_html__( 'Hour', 'butterbean' ),
|
||||
'attr' => 'placeholder="00" size="2" maxlength="2" autocomplete="off"'
|
||||
);
|
||||
|
||||
// Minute
|
||||
$this->json['minute'] = array(
|
||||
'value' => esc_attr( $minute ),
|
||||
'name' => esc_attr( "{$field_name}_minute" ),
|
||||
'label' => esc_html__( 'Minute', 'butterbean' ),
|
||||
'attr' => 'placeholder="00" size="2" maxlength="2" autocomplete="off"'
|
||||
);
|
||||
|
||||
// Second
|
||||
$this->json['second'] = array(
|
||||
'value' => esc_attr( $second ),
|
||||
'name' => esc_attr( "{$field_name}_second" ),
|
||||
'label' => esc_html__( 'Second', 'butterbean' ),
|
||||
'attr' => 'placeholder="00" size="2" maxlength="2" autocomplete="off"'
|
||||
);
|
||||
}
|
||||
}
|
||||
+54
@@ -0,0 +1,54 @@
|
||||
<?php
|
||||
/**
|
||||
* Editor control class.
|
||||
*
|
||||
* @package ButterBean
|
||||
* @author Justin Tadlock <justin@justintadlock.com>
|
||||
* @copyright Copyright (c) 2015-2016, Justin Tadlock
|
||||
* @link https://github.com/justintadlock/butterbean
|
||||
* @license http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
|
||||
*/
|
||||
|
||||
/**
|
||||
* Editor control class.
|
||||
*
|
||||
* @since 1.0.0
|
||||
* @access public
|
||||
*/
|
||||
class OceanWP_ButterBean_Control_Editor extends OceanWP_ButterBean_Control {
|
||||
|
||||
/**
|
||||
* The type of control.
|
||||
*
|
||||
* @since 1.0.0
|
||||
* @access public
|
||||
* @var string
|
||||
*/
|
||||
public $type = 'editor';
|
||||
|
||||
/**
|
||||
* Adds custom data to the json array. This data is passed to the Underscore template.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function to_json() {
|
||||
parent::to_json();
|
||||
|
||||
$this->json['value'] = $this->get_value();
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the attributes for the control.
|
||||
* Sets the new id attribute, as it's required for TinyMCE to function properly.
|
||||
* Sets new class .tinymce for easier js initialization.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function get_attr() {
|
||||
$this->attr = parent::get_attr();
|
||||
|
||||
$this->attr['id'] = $this->get_field_name();
|
||||
|
||||
return $this->attr;
|
||||
}
|
||||
}
|
||||
+82
@@ -0,0 +1,82 @@
|
||||
<?php
|
||||
/**
|
||||
* Excerpt control class. Note that this control isn't meant to be tied to a setting. Core
|
||||
* WP will save the excerpt. Also, make sure to disable the core excerpt metabox if using
|
||||
* this control.
|
||||
*
|
||||
* @package ButterBean
|
||||
* @subpackage Admin
|
||||
* @author Justin Tadlock <justin@justintadlock.com>
|
||||
* @copyright Copyright (c) 2015-2016, Justin Tadlock
|
||||
* @link https://github.com/justintadlock/butterbean
|
||||
* @license http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
|
||||
*/
|
||||
|
||||
/**
|
||||
* Excerpt control class.
|
||||
*
|
||||
* @since 1.0.0
|
||||
* @access public
|
||||
*/
|
||||
class ButterBean_Control_Excerpt extends ButterBean_Control_Textarea {
|
||||
|
||||
/**
|
||||
* The type of control.
|
||||
*
|
||||
* @since 1.0.0
|
||||
* @access public
|
||||
* @var string
|
||||
*/
|
||||
public $type = 'excerpt';
|
||||
|
||||
/**
|
||||
* Gets the attributes for the control.
|
||||
*
|
||||
* @since 1.0.0
|
||||
* @access public
|
||||
* @return array
|
||||
*/
|
||||
public function get_attr() {
|
||||
$attr = parent::get_attr();
|
||||
|
||||
$attr['id'] = 'post_excerpt';
|
||||
|
||||
return $attr;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the HTML field name for the control.
|
||||
*
|
||||
* @since 1.0.0
|
||||
* @access public
|
||||
* @param string $setting
|
||||
* @return string
|
||||
*/
|
||||
public function get_field_name( $setting = 'default' ) {
|
||||
return 'post_excerpt';
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the value for the setting.
|
||||
*
|
||||
* @since 1.0.0
|
||||
* @access public
|
||||
* @param string $setting
|
||||
* @return mixed
|
||||
*/
|
||||
public function get_value( $setting = 'default' ) {
|
||||
|
||||
return get_post( $this->manager->post_id )->post_excerpt;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the Underscore.js template.
|
||||
*
|
||||
* @since 1.0.0
|
||||
* @access public
|
||||
* @return void
|
||||
*/
|
||||
public function get_template() {
|
||||
butterbean_get_control_template( 'textarea' );
|
||||
}
|
||||
}
|
||||
+112
@@ -0,0 +1,112 @@
|
||||
<?php
|
||||
/**
|
||||
* Image control class. This control allows users to set an image. It passes the attachment
|
||||
* ID the setting, so you'll need a custom control class if you want to store anything else,
|
||||
* such as the URL or other data.
|
||||
*
|
||||
* @package ButterBean
|
||||
* @author Justin Tadlock <justin@justintadlock.com>
|
||||
* @copyright Copyright (c) 2015-2016, Justin Tadlock
|
||||
* @link https://github.com/justintadlock/butterbean
|
||||
* @license http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
|
||||
*/
|
||||
|
||||
/**
|
||||
* Image control class.
|
||||
*
|
||||
* @since 1.0.0
|
||||
* @access public
|
||||
*/
|
||||
class ButterBean_Control_Image extends ButterBean_Control {
|
||||
|
||||
/**
|
||||
* The type of control.
|
||||
*
|
||||
* @since 1.0.0
|
||||
* @access public
|
||||
* @var string
|
||||
*/
|
||||
public $type = 'image';
|
||||
|
||||
/**
|
||||
* Array of text labels to use for the media upload frame.
|
||||
*
|
||||
* @since 1.0.0
|
||||
* @access public
|
||||
* @var string
|
||||
*/
|
||||
public $l10n = array();
|
||||
|
||||
/**
|
||||
* Image size to display. If the size isn't found for the image,
|
||||
* the full size of the image will be output.
|
||||
*
|
||||
* @since 1.0.0
|
||||
* @access public
|
||||
* @var string
|
||||
*/
|
||||
public $size = 'large';
|
||||
|
||||
/**
|
||||
* Creates a new control object.
|
||||
*
|
||||
* @since 1.0.0
|
||||
* @access public
|
||||
* @param object $manager
|
||||
* @param string $name
|
||||
* @param array $args
|
||||
* @return void
|
||||
*/
|
||||
public function __construct( $manager, $name, $args = array() ) {
|
||||
parent::__construct( $manager, $name, $args );
|
||||
|
||||
$this->l10n = wp_parse_args(
|
||||
$this->l10n,
|
||||
array(
|
||||
'upload' => esc_html__( 'Add image', 'butterbean' ),
|
||||
'set' => esc_html__( 'Set as image', 'butterbean' ),
|
||||
'choose' => esc_html__( 'Choose image', 'butterbean' ),
|
||||
'change' => esc_html__( 'Change image', 'butterbean' ),
|
||||
'remove' => esc_html__( 'Remove image', 'butterbean' ),
|
||||
'placeholder' => esc_html__( 'No image selected', 'butterbean' )
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Enqueue scripts/styles for the control.
|
||||
*
|
||||
* @since 1.0.0
|
||||
* @access public
|
||||
* @return void
|
||||
*/
|
||||
public function enqueue() {
|
||||
|
||||
wp_enqueue_script( 'media-views' );
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds custom data to the json array.
|
||||
*
|
||||
* @since 1.0.0
|
||||
* @access public
|
||||
* @return void
|
||||
*/
|
||||
public function to_json() {
|
||||
parent::to_json();
|
||||
|
||||
$this->json['l10n'] = $this->l10n;
|
||||
$this->json['size'] = $this->size;
|
||||
|
||||
$value = $this->get_value();
|
||||
$image = $alt = '';
|
||||
|
||||
if ( $value ) {
|
||||
$image = wp_get_attachment_image_src( absint( $value ), $this->size );
|
||||
$alt = get_post_meta( absint( $value ), '_wp_attachment_image_alt', true );
|
||||
}
|
||||
|
||||
$this->json['src'] = $image ? esc_url( $image[0] ) : '';
|
||||
$this->json['alt'] = $alt ? esc_attr( $alt ) : '';
|
||||
}
|
||||
}
|
||||
+83
@@ -0,0 +1,83 @@
|
||||
<?php
|
||||
/**
|
||||
* Media control class.
|
||||
*
|
||||
* @package ButterBean
|
||||
* @author Justin Tadlock <justin@justintadlock.com>
|
||||
* @copyright Copyright (c) 2015-2016, Justin Tadlock
|
||||
* @link https://github.com/justintadlock/butterbean
|
||||
* @license http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
|
||||
*/
|
||||
|
||||
/**
|
||||
* Media control class.
|
||||
*
|
||||
* @since 1.0.0
|
||||
* @access public
|
||||
*/
|
||||
class OceanWP_ButterBean_Control_Media extends OceanWP_ButterBean_Control {
|
||||
|
||||
/**
|
||||
* The type of control.
|
||||
*
|
||||
* @since 1.0.0
|
||||
* @access public
|
||||
* @var string
|
||||
*/
|
||||
public $type = 'media';
|
||||
|
||||
/**
|
||||
* Array of text labels to use for the media upload frame.
|
||||
*
|
||||
* @since 1.0.0
|
||||
* @access public
|
||||
* @var string
|
||||
*/
|
||||
public $l10n = array();
|
||||
|
||||
/**
|
||||
* Creates a new control object.
|
||||
*
|
||||
* @since 1.0.0
|
||||
* @access public
|
||||
* @param object $manager
|
||||
* @param string $name
|
||||
* @param array $args
|
||||
* @return void
|
||||
*/
|
||||
public function __construct( $manager, $name, $args = array() ) {
|
||||
parent::__construct( $manager, $name, $args );
|
||||
|
||||
$this->l10n = wp_parse_args(
|
||||
$this->l10n,
|
||||
array(
|
||||
'upload' => esc_html__( 'Browse', 'ocean-extra' ),
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Enqueue scripts/styles for the control.
|
||||
*
|
||||
* @since 1.0.0
|
||||
* @access public
|
||||
* @return void
|
||||
*/
|
||||
public function enqueue() {
|
||||
|
||||
wp_enqueue_media();
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds custom data to the json array.
|
||||
*
|
||||
* @since 1.0.0
|
||||
* @access public
|
||||
* @return void
|
||||
*/
|
||||
public function to_json() {
|
||||
parent::to_json();
|
||||
|
||||
$this->json['l10n'] = $this->l10n;
|
||||
}
|
||||
}
|
||||
+90
@@ -0,0 +1,90 @@
|
||||
<?php
|
||||
/**
|
||||
* Multi-avatars control. This control is for outputting multiple users who can create,
|
||||
* edit, or publish posts of the given post type. Multiple users can be selected. The
|
||||
* data is expected to be an array. This control should be used with a setting type that
|
||||
* handles arrays, such as the built-in `array` or `multiple` types.
|
||||
*
|
||||
* @package ButterBean
|
||||
* @author Justin Tadlock <justin@justintadlock.com>
|
||||
* @copyright Copyright (c) 2015-2016, Justin Tadlock
|
||||
* @link https://github.com/justintadlock/butterbean
|
||||
* @license http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
|
||||
*/
|
||||
|
||||
/**
|
||||
* Multi-avatars control class.
|
||||
*
|
||||
* @since 1.0.0
|
||||
* @access public
|
||||
*/
|
||||
class ButterBean_Control_Multi_Avatars extends ButterBean_Control {
|
||||
|
||||
/**
|
||||
* The type of control.
|
||||
*
|
||||
* @since 1.0.0
|
||||
* @access public
|
||||
* @var string
|
||||
*/
|
||||
public $type = 'multi-avatars';
|
||||
|
||||
/**
|
||||
* Adds custom data to the json array. This data is passed to the Underscore template.
|
||||
*
|
||||
* @since 1.0.0
|
||||
* @access public
|
||||
* @return void
|
||||
*/
|
||||
public function to_json() {
|
||||
parent::to_json();
|
||||
|
||||
$this->json['value'] = is_array( $this->get_value() ) ? array_map( 'absint', $this->get_value() ) : array();
|
||||
$this->json['choices'] = array();
|
||||
|
||||
$users = get_users( array( 'role__in' => $this->get_roles() ) );
|
||||
|
||||
foreach ( $users as $user ) {
|
||||
$this->json['choices'][] = array(
|
||||
'id' => $user->ID,
|
||||
'name' => $user->display_name,
|
||||
'avatar' => get_avatar( $user->ID, 70 )
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns an array of user roles that are allowed to edit, publish, or create
|
||||
* posts of the given post type.
|
||||
*
|
||||
* @since 1.0.0
|
||||
* @access public
|
||||
* @global object $wp_roles
|
||||
* @return array
|
||||
*/
|
||||
public function get_roles() {
|
||||
global $wp_roles;
|
||||
|
||||
$roles = array();
|
||||
$type = get_post_type_object( get_post_type( $this->manager->post_id ) );
|
||||
|
||||
// Get the post type object caps.
|
||||
$caps = array( $type->cap->edit_posts, $type->cap->publish_posts, $type->cap->create_posts );
|
||||
$caps = array_unique( $caps );
|
||||
|
||||
// Loop through the available roles.
|
||||
foreach ( $wp_roles->roles as $name => $role ) {
|
||||
|
||||
foreach ( $caps as $cap ) {
|
||||
|
||||
// If the role is granted the cap, add it.
|
||||
if ( isset( $role['capabilities'][ $cap ] ) && true === $role['capabilities'][ $cap ] ) {
|
||||
$roles[] = $name;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return $roles;
|
||||
}
|
||||
}
|
||||
+51
@@ -0,0 +1,51 @@
|
||||
<?php
|
||||
/**
|
||||
* Color palette control class. The purpose of this class is to give users a choice
|
||||
* of color palettes. The actual data that is stored is a key of your choosing.
|
||||
*
|
||||
* @package ButterBean
|
||||
* @author Justin Tadlock <justin@justintadlock.com>
|
||||
* @copyright Copyright (c) 2015-2016, Justin Tadlock
|
||||
* @link https://github.com/justintadlock/butterbean
|
||||
* @license http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
|
||||
*/
|
||||
|
||||
/**
|
||||
* Color palette control class.
|
||||
*
|
||||
* @since 1.0.0
|
||||
* @access public
|
||||
*/
|
||||
class ButterBean_Control_Palette extends ButterBean_Control {
|
||||
|
||||
/**
|
||||
* The type of control.
|
||||
*
|
||||
* @since 1.0.0
|
||||
* @access public
|
||||
* @var string
|
||||
*/
|
||||
public $type = 'palette';
|
||||
|
||||
/**
|
||||
* Adds custom data to the json array. This data is passed to the Underscore template.
|
||||
*
|
||||
* @since 1.0.0
|
||||
* @access public
|
||||
* @return void
|
||||
*/
|
||||
public function to_json() {
|
||||
parent::to_json();
|
||||
|
||||
$value = $this->get_value();
|
||||
|
||||
// Make sure the colors have a hash.
|
||||
foreach ( $this->choices as $choice => $palette ) {
|
||||
$this->choices[ $choice ]['colors'] = array_map( 'butterbean_maybe_hash_hex_color', $palette['colors'] );
|
||||
|
||||
$this->choices[ $choice ]['selected'] = $value && $choice === $value;
|
||||
}
|
||||
|
||||
$this->json['choices'] = $this->choices;
|
||||
}
|
||||
}
|
||||
+97
@@ -0,0 +1,97 @@
|
||||
<?php
|
||||
/**
|
||||
* Post parent control class. This class is a specialty class meant for use in unique
|
||||
* scenarios where you're not using the core post parent drop-down. This is often the
|
||||
* case with flat post types that have a parent post. This control is not meant to be
|
||||
* used with a setting. Core WP will store the data in the `post.post_parent` field.
|
||||
*
|
||||
* @package ButterBean
|
||||
* @author Justin Tadlock <justin@justintadlock.com>
|
||||
* @copyright Copyright (c) 2015-2016, Justin Tadlock
|
||||
* @link https://github.com/justintadlock/butterbean
|
||||
* @license http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
|
||||
*/
|
||||
|
||||
/**
|
||||
* Post parent control class.
|
||||
*
|
||||
* @since 1.0.0
|
||||
* @access public
|
||||
*/
|
||||
class ButterBean_Control_Parent extends ButterBean_Control {
|
||||
|
||||
/**
|
||||
* The type of control.
|
||||
*
|
||||
* @since 1.0.0
|
||||
* @access public
|
||||
* @var string
|
||||
*/
|
||||
public $type = 'parent';
|
||||
|
||||
/**
|
||||
* The post type to select posts from.
|
||||
*
|
||||
* @since 1.0.0
|
||||
* @access public
|
||||
* @var string
|
||||
*/
|
||||
public $post_type = '';
|
||||
|
||||
/**
|
||||
* Returns the HTML field name for the control.
|
||||
*
|
||||
* @since 1.0.0
|
||||
* @access public
|
||||
* @param string $setting
|
||||
* @return array
|
||||
*/
|
||||
public function get_field_name( $setting = 'default' ) {
|
||||
|
||||
return 'post_parent';
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the value for the setting.
|
||||
*
|
||||
* @since 1.0.0
|
||||
* @access public
|
||||
* @param string $setting
|
||||
* @return mixed
|
||||
*/
|
||||
public function get_value( $setting = 'default' ) {
|
||||
|
||||
return get_post( $this->manager->post_id )->post_parent;
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds custom data to the json array. This data is passed to the Underscore template.
|
||||
*
|
||||
* @since 1.0.0
|
||||
* @access public
|
||||
* @return void
|
||||
*/
|
||||
public function to_json() {
|
||||
parent::to_json();
|
||||
|
||||
$_post = get_post( $this->manager->post_id );
|
||||
|
||||
$posts = get_posts(
|
||||
array(
|
||||
'post_type' => $this->post_type ? $this->post_type : get_post_type( $this->manager->post_id ),
|
||||
'post_status' => 'any',
|
||||
'post__not_in' => array( $this->manager->post_id ),
|
||||
'posts_per_page' => -1,
|
||||
'post_parent' => 0,
|
||||
'orderby' => 'title',
|
||||
'order' => 'ASC',
|
||||
'fields' => array( 'ID', 'post_title' )
|
||||
)
|
||||
);
|
||||
|
||||
$this->json['choices'] = array( array( 'value' => 0, 'label' => '' ) );
|
||||
|
||||
foreach ( $posts as $post )
|
||||
$this->json['choices'][] = array( 'value' => $post->ID, 'label' => $post->post_title );
|
||||
}
|
||||
}
|
||||
+46
@@ -0,0 +1,46 @@
|
||||
<?php
|
||||
/**
|
||||
* Radio image control class extends the built-in radio control. This control is
|
||||
* meant for displaying an image instead of the radio fields.
|
||||
*
|
||||
* @package ButterBean
|
||||
* @subpackage Admin
|
||||
* @author Justin Tadlock <justin@justintadlock.com>
|
||||
* @copyright Copyright (c) 2015-2016, Justin Tadlock
|
||||
* @link https://github.com/justintadlock/butterbean
|
||||
* @license http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
|
||||
*/
|
||||
|
||||
/**
|
||||
* Radio image control class.
|
||||
*
|
||||
* @since 1.0.0
|
||||
* @access public
|
||||
*/
|
||||
class ButterBean_Control_Radio_Image extends ButterBean_Control_Radio {
|
||||
|
||||
/**
|
||||
* The type of control.
|
||||
*
|
||||
* @since 1.0.0
|
||||
* @access public
|
||||
* @var string
|
||||
*/
|
||||
public $type = 'radio-image';
|
||||
|
||||
/**
|
||||
* Adds custom data to the json array. This data is passed to the Underscore template.
|
||||
*
|
||||
* @since 1.0.0
|
||||
* @access public
|
||||
* @return void
|
||||
*/
|
||||
public function to_json() {
|
||||
parent::to_json();
|
||||
|
||||
foreach ( $this->choices as $value => $args )
|
||||
$this->choices[ $value ]['url'] = esc_url( sprintf( $args['url'], get_template_directory_uri(), get_stylesheet_directory_uri() ) );
|
||||
|
||||
$this->json['choices'] = $this->choices;
|
||||
}
|
||||
}
|
||||
+46
@@ -0,0 +1,46 @@
|
||||
<?php
|
||||
/**
|
||||
* Radio control class that creates a list of radio inputs to choose from.
|
||||
*
|
||||
* @package ButterBean
|
||||
* @subpackage Admin
|
||||
* @author Justin Tadlock <justin@justintadlock.com>
|
||||
* @copyright Copyright (c) 2015-2016, Justin Tadlock
|
||||
* @link https://github.com/justintadlock/butterbean
|
||||
* @license http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
|
||||
*/
|
||||
|
||||
/**
|
||||
* Radio control class.
|
||||
*
|
||||
* @since 1.0.0
|
||||
* @access public
|
||||
*/
|
||||
class ButterBean_Control_Radio extends ButterBean_Control {
|
||||
|
||||
/**
|
||||
* The type of control.
|
||||
*
|
||||
* @since 1.0.0
|
||||
* @access public
|
||||
* @var string
|
||||
*/
|
||||
public $type = 'radio';
|
||||
|
||||
/**
|
||||
* Radio controls imply that a value should be set. Therefore, we will return
|
||||
* the default if there is no value.
|
||||
*
|
||||
* @since 1.0.0
|
||||
* @access public
|
||||
* @param string $setting
|
||||
* @return mixed
|
||||
*/
|
||||
public function get_value( $setting = 'default' ) {
|
||||
|
||||
$value = parent::get_value( $setting );
|
||||
$object = $this->get_setting( $setting );
|
||||
|
||||
return ! $value && $object ? $object->default : $value;
|
||||
}
|
||||
}
|
||||
+61
@@ -0,0 +1,61 @@
|
||||
<?php
|
||||
/**
|
||||
* Range control class.
|
||||
*
|
||||
* @package ButterBean
|
||||
* @author Justin Tadlock <justin@justintadlock.com>
|
||||
* @copyright Copyright (c) 2015-2016, Justin Tadlock
|
||||
* @link https://github.com/justintadlock/butterbean
|
||||
* @license http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
|
||||
*/
|
||||
|
||||
/**
|
||||
* Range control class.
|
||||
*
|
||||
* @since 1.0.0
|
||||
* @access public
|
||||
*/
|
||||
class OceanWP_ButterBean_Control_Range extends OceanWP_ButterBean_Control {
|
||||
|
||||
/**
|
||||
* The type of control.
|
||||
*
|
||||
* @since 1.0.0
|
||||
* @access public
|
||||
* @var string
|
||||
*/
|
||||
public $type = 'range';
|
||||
|
||||
/**
|
||||
* Gets the attributes for the control.
|
||||
*
|
||||
* @since 1.0.0
|
||||
* @access public
|
||||
* @return array
|
||||
*/
|
||||
public function get_attr() {
|
||||
$attr = parent::get_attr();
|
||||
|
||||
$setting = $this->get_setting();
|
||||
|
||||
$attr['data-reset_value'] = $setting ? $setting->default : '';
|
||||
|
||||
return $attr;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the value for the setting.
|
||||
*
|
||||
* @since 1.0.0
|
||||
* @access public
|
||||
* @param string $setting
|
||||
* @return mixed
|
||||
*/
|
||||
public function get_value( $setting = 'default' ) {
|
||||
|
||||
$value = parent::get_value( $setting );
|
||||
$object = $this->get_setting( $setting );
|
||||
|
||||
return ! $value && $object ? $object->default : $value;
|
||||
}
|
||||
}
|
||||
+53
@@ -0,0 +1,53 @@
|
||||
<?php
|
||||
/**
|
||||
* Select group control class. This works just like a normal select. However, it
|
||||
* allows for `<optgroup>` to be added.
|
||||
*
|
||||
* @package ButterBean
|
||||
* @author Justin Tadlock <justin@justintadlock.com>
|
||||
* @copyright Copyright (c) 2015-2016, Justin Tadlock
|
||||
* @link https://github.com/justintadlock/butterbean
|
||||
* @license http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
|
||||
*/
|
||||
|
||||
/**
|
||||
* Select group control class.
|
||||
*
|
||||
* @since 1.0.0
|
||||
* @access public
|
||||
*/
|
||||
class ButterBean_Control_Select_Group extends ButterBean_Control {
|
||||
|
||||
/**
|
||||
* The type of control.
|
||||
*
|
||||
* @since 1.0.0
|
||||
* @access public
|
||||
* @var string
|
||||
*/
|
||||
public $type = 'select-group';
|
||||
|
||||
/**
|
||||
* Adds custom data to the json array.
|
||||
*
|
||||
* @since 1.0.0
|
||||
* @access public
|
||||
* @return void
|
||||
*/
|
||||
public function to_json() {
|
||||
parent::to_json();
|
||||
|
||||
$choices = $group = array();
|
||||
|
||||
foreach ( $this->choices as $choice => $maybe_group ) {
|
||||
|
||||
if ( is_array( $maybe_group ) )
|
||||
$group[ $choice ] = $maybe_group;
|
||||
else
|
||||
$choices[ $choice ] = $maybe_group;
|
||||
}
|
||||
|
||||
$this->json['choices'] = $choices;
|
||||
$this->json['group'] = $group;
|
||||
}
|
||||
}
|
||||
+42
@@ -0,0 +1,42 @@
|
||||
<?php
|
||||
/**
|
||||
* Textarea control class.
|
||||
*
|
||||
* @package ButterBean
|
||||
* @subpackage Admin
|
||||
* @author Justin Tadlock <justin@justintadlock.com>
|
||||
* @copyright Copyright (c) 2015-2016, Justin Tadlock
|
||||
* @link https://github.com/justintadlock/butterbean
|
||||
* @license http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
|
||||
*/
|
||||
|
||||
/**
|
||||
* Textarea control class.
|
||||
*
|
||||
* @since 1.0.0
|
||||
* @access public
|
||||
*/
|
||||
class ButterBean_Control_Textarea extends ButterBean_Control {
|
||||
|
||||
/**
|
||||
* The type of control.
|
||||
*
|
||||
* @since 1.0.0
|
||||
* @access public
|
||||
* @var string
|
||||
*/
|
||||
public $type = 'textarea';
|
||||
|
||||
/**
|
||||
* Adds custom data to the json array. This data is passed to the Underscore template.
|
||||
*
|
||||
* @since 1.0.0
|
||||
* @access public
|
||||
* @return void
|
||||
*/
|
||||
public function to_json() {
|
||||
parent::to_json();
|
||||
|
||||
$this->json['value'] = esc_textarea( $this->get_value() );
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,169 @@
|
||||
<?php
|
||||
/**
|
||||
* Helper functions.
|
||||
*
|
||||
* @package ButterBean
|
||||
* @subpackage Admin
|
||||
* @author Justin Tadlock <justin@justintadlock.com>
|
||||
* @copyright Copyright (c) 2015-2016, Justin Tadlock
|
||||
* @link https://github.com/justintadlock/butterbean
|
||||
* @license http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
|
||||
*/
|
||||
|
||||
/**
|
||||
* Function for validating booleans before saving them as metadata. If the value is
|
||||
* `true`, we'll return a `1` to be stored as the meta value. Else, we return `false`.
|
||||
*
|
||||
* @since 1.0.0
|
||||
* @access public
|
||||
* @param mixed
|
||||
* @return bool|int
|
||||
*/
|
||||
function butterbean_validate_boolean( $value ) {
|
||||
|
||||
return wp_validate_boolean( $value ) ? 1 : false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Pre-WP 4.6 function for sanitizing hex colors.
|
||||
*
|
||||
* @since 1.0.0
|
||||
* @access public
|
||||
* @param string $color
|
||||
* @return string
|
||||
*/
|
||||
function butterbean_sanitize_hex_color( $color ) {
|
||||
|
||||
if ( function_exists( 'sanitize_hex_color' ) )
|
||||
return sanitize_hex_color( $color );
|
||||
|
||||
return $color && preg_match('|^#([A-Fa-f0-9]{3}){1,2}$|', $color ) ? $color : '';
|
||||
}
|
||||
|
||||
/**
|
||||
* Pre-WP 4.6 function for sanitizing hex colors without a hash.
|
||||
*
|
||||
* @since 1.0.0
|
||||
* @access public
|
||||
* @param string $color
|
||||
* @return string
|
||||
*/
|
||||
function butterbean_sanitize_hex_color_no_hash( $color ) {
|
||||
|
||||
if ( function_exists( 'sanitize_hex_color_no_hash' ) )
|
||||
return sanitize_hex_color_no_hash( $color );
|
||||
|
||||
$color = ltrim( $color, '#' );
|
||||
|
||||
if ( '' === $color )
|
||||
return '';
|
||||
|
||||
return butterbean_sanitize_hex_color( '#' . $color ) ? $color : null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Pre-WP 4.6 function for sanitizing a color and adding a hash.
|
||||
*
|
||||
* @since 1.0.0
|
||||
* @access public
|
||||
* @param string $color
|
||||
* @return string
|
||||
*/
|
||||
function butterbean_maybe_hash_hex_color( $color ) {
|
||||
|
||||
if ( function_exists( 'maybe_hash_hex_color' ) )
|
||||
return maybe_hash_hex_color( $color );
|
||||
|
||||
if ( $unhashed = butterbean_sanitize_hex_color_no_hash( $color ) )
|
||||
return '#' . $unhashed;
|
||||
|
||||
return $color;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets Underscore.js templates for managers.
|
||||
*
|
||||
* @since 1.0.0
|
||||
* @param string $slug
|
||||
* @return void
|
||||
*/
|
||||
function butterbean_get_manager_template( $slug = '' ) {
|
||||
butterbean_get_template( 'manager', $slug );
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets Underscore.js templates for navs.
|
||||
*
|
||||
* @since 1.0.0
|
||||
* @param string $slug
|
||||
* @return void
|
||||
*/
|
||||
function butterbean_get_nav_template( $slug = '' ) {
|
||||
butterbean_get_template( 'nav', $slug );
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets Underscore.js templates for sections.
|
||||
*
|
||||
* @since 1.0.0
|
||||
* @param string $slug
|
||||
* @return void
|
||||
*/
|
||||
function butterbean_get_section_template( $slug = '' ) {
|
||||
butterbean_get_template( 'section', $slug );
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets Underscore.js templates for controls.
|
||||
*
|
||||
* @since 1.0.0
|
||||
* @param string $slug
|
||||
* @return void
|
||||
*/
|
||||
function butterbean_get_control_template( $slug = '' ) {
|
||||
butterbean_get_template( 'control', $slug );
|
||||
}
|
||||
|
||||
/**
|
||||
* Helper function for getting Underscore.js templates.
|
||||
*
|
||||
* @since 1.0.0
|
||||
* @param string $name
|
||||
* @param string $slug
|
||||
* @return void
|
||||
*/
|
||||
function butterbean_get_template( $name, $slug = '' ) {
|
||||
|
||||
// Allow devs to hook in early to bypass template checking.
|
||||
$located = apply_filters( "butterbean_pre_{$name}_template", '', $slug );
|
||||
|
||||
// If there's no template, let's try to find one.
|
||||
if ( ! $located ) {
|
||||
|
||||
$templates = array();
|
||||
|
||||
if ( $slug )
|
||||
$templates[] = "{$name}-{$slug}.php";
|
||||
|
||||
$templates[] = "{$name}.php";
|
||||
|
||||
// Allow devs to filter the template hierarchy.
|
||||
$templates = apply_filters( "butterbean_{$name}_template_hierarchy", $templates, $slug );
|
||||
|
||||
// Loop through the templates and locate one.
|
||||
foreach ( $templates as $template ) {
|
||||
|
||||
if ( file_exists( butterbean()->tmpl_path . $template ) ) {
|
||||
$located = butterbean()->tmpl_path . $template;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Allow devs to filter the final template.
|
||||
$located = apply_filters( "butterbean_{$name}_template", $located, $slug );
|
||||
|
||||
// Load the template.
|
||||
if ( $located )
|
||||
require( $located );
|
||||
}
|
||||
+80
@@ -0,0 +1,80 @@
|
||||
<?php
|
||||
/**
|
||||
* Setting class for storing a single meta value as an array.
|
||||
*
|
||||
* @package ButterBean
|
||||
* @author Justin Tadlock <justin@justintadlock.com>
|
||||
* @copyright Copyright (c) 2015-2016, Justin Tadlock
|
||||
* @link https://github.com/justintadlock/butterbean
|
||||
* @license http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
|
||||
*/
|
||||
|
||||
/**
|
||||
* Array setting class.
|
||||
*
|
||||
* @since 1.0.0
|
||||
* @access public
|
||||
*/
|
||||
class ButterBean_Setting_Array extends ButterBean_Setting {
|
||||
|
||||
/**
|
||||
* The type of setting.
|
||||
*
|
||||
* @since 1.0.0
|
||||
* @access public
|
||||
* @var string
|
||||
*/
|
||||
public $type = 'array';
|
||||
|
||||
/**
|
||||
* Sanitizes the value of the setting.
|
||||
*
|
||||
* @since 1.0.0
|
||||
* @access public
|
||||
* @param array $value
|
||||
* @return array
|
||||
*/
|
||||
public function sanitize( $values ) {
|
||||
|
||||
$multi_values = $values && ! is_array( $values ) ? explode( ',', $values ) : $values;
|
||||
|
||||
return $multi_values ? array_map( array( $this, 'map' ), $multi_values ) : array();
|
||||
}
|
||||
|
||||
/**
|
||||
* Helper function for sanitizing each value of the array.
|
||||
*
|
||||
* @since 1.0.0
|
||||
* @access public
|
||||
* @param mixed $value
|
||||
* @return mixed
|
||||
*/
|
||||
public function map( $value ) {
|
||||
|
||||
return apply_filters( "butterbean_{$this->manager->name}_sanitize_{$this->name}", $value, $this );
|
||||
}
|
||||
|
||||
/**
|
||||
* Saves the value of the setting.
|
||||
*
|
||||
* @since 1.0.0
|
||||
* @access public
|
||||
* @return void
|
||||
*/
|
||||
public function save() {
|
||||
|
||||
if ( ! $this->check_capabilities() )
|
||||
return;
|
||||
|
||||
$old_values = $this->get_value();
|
||||
$new_values = $this->get_posted_value();
|
||||
|
||||
// If there's an array of posted values, set them.
|
||||
if ( $new_values && is_array( $new_values ) && $new_values !== $old_values )
|
||||
return update_post_meta( $this->manager->post_id, $this->name, $new_values );
|
||||
|
||||
// If no array of posted values but we have old values, delete them.
|
||||
else if ( $old_values && ! $new_values )
|
||||
return delete_post_meta( $this->manager->post_id, $this->name );
|
||||
}
|
||||
}
|
||||
+105
@@ -0,0 +1,105 @@
|
||||
<?php
|
||||
/**
|
||||
* Datetime setting class. This is meant to be used in conjunction with the built-in
|
||||
* `ButterBean_Datetime_Control` or a sub-class that passes the appropriate values.
|
||||
*
|
||||
* @package ButterBean
|
||||
* @subpackage Admin
|
||||
* @author Justin Tadlock <justin@justintadlock.com>
|
||||
* @copyright Copyright (c) 2015-2016, Justin Tadlock
|
||||
* @link https://github.com/justintadlock/butterbean
|
||||
* @license http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
|
||||
*/
|
||||
|
||||
/**
|
||||
* Date setting class.
|
||||
*
|
||||
* @since 1.0.0
|
||||
* @access public
|
||||
*/
|
||||
class ButterBean_Setting_Datetime extends ButterBean_Setting {
|
||||
|
||||
/**
|
||||
* The type of setting.
|
||||
*
|
||||
* @since 1.0.0
|
||||
* @access public
|
||||
* @var string
|
||||
*/
|
||||
public $type = 'datetime';
|
||||
|
||||
/**
|
||||
* Gets the posted value of the setting.
|
||||
*
|
||||
* @since 1.0.0
|
||||
* @access public
|
||||
* @return mixed
|
||||
*/
|
||||
public function get_posted_value() {
|
||||
|
||||
$field_name = $this->get_field_name();
|
||||
|
||||
// Get the posted date.
|
||||
$year = ! empty( $_POST[ "{$field_name}_year" ] ) ? zeroise( absint( $_POST[ "{$field_name}_year" ] ), 4 ) : '';
|
||||
$month = ! empty( $_POST[ "{$field_name}_month" ] ) ? zeroise( absint( $_POST[ "{$field_name}_month" ] ), 2 ) : '';
|
||||
$day = ! empty( $_POST[ "{$field_name}_day" ] ) ? zeroise( absint( $_POST[ "{$field_name}_day" ] ), 2 ) : '';
|
||||
|
||||
// Get the posted time.
|
||||
$hour = ! empty( $_POST[ "{$field_name}_hour" ] ) ? $this->validate_hour( $_POST[ "{$field_name}_hour" ] ) : '00';
|
||||
$minute = ! empty( $_POST[ "{$field_name}_minute" ] ) ? $this->validate_minute( $_POST[ "{$field_name}_minute" ] ) : '00';
|
||||
$second = ! empty( $_POST[ "{$field_name}_second" ] ) ? $this->validate_second( $_POST[ "{$field_name}_second" ] ) : '00';
|
||||
|
||||
$date = "{$year}-{$month}-{$day}";
|
||||
$time = "{$hour}:{$minute}:{$second}";
|
||||
|
||||
if ( $year && $month && $day && wp_checkdate( absint( $month ), absint( $day ), absint( $year ), $date ) )
|
||||
return "{$date} {$time}";
|
||||
|
||||
return '';
|
||||
}
|
||||
|
||||
/**
|
||||
* Validates the hour.
|
||||
*
|
||||
* @since 1.0.0
|
||||
* @access public
|
||||
* @param int|string $hour
|
||||
* @return string
|
||||
*/
|
||||
public function validate_hour( $hour ) {
|
||||
|
||||
$hour = absint( $hour );
|
||||
|
||||
return $hour < 0 || $hour > 23 ? zeroise( $hour, 2 ) : '00';
|
||||
}
|
||||
|
||||
/**
|
||||
* Validates the minute.
|
||||
*
|
||||
* @since 1.0.0
|
||||
* @access public
|
||||
* @param int|string $minute
|
||||
* @return string
|
||||
*/
|
||||
public function validate_minute( $minute ) {
|
||||
|
||||
$minute = absint( $minute );
|
||||
|
||||
return $minute < 0 || $minute > 59 ? zeroise( $minute, 2 ) : '00';
|
||||
}
|
||||
|
||||
/**
|
||||
* Validates the second.
|
||||
*
|
||||
* @since 1.0.0
|
||||
* @access public
|
||||
* @param int|string $second
|
||||
* @return string
|
||||
*/
|
||||
public function validate_second( $second ) {
|
||||
|
||||
$second = absint( $second );
|
||||
|
||||
return $second < 0 || $second > 59 ? zeroise( $second, 2 ) : '00';
|
||||
}
|
||||
}
|
||||
+154
@@ -0,0 +1,154 @@
|
||||
<?php
|
||||
/**
|
||||
* Setting class for storing multiple post meta values for a single key.
|
||||
*
|
||||
* @package ButterBean
|
||||
* @author Justin Tadlock <justin@justintadlock.com>
|
||||
* @copyright Copyright (c) 2015-2016, Justin Tadlock
|
||||
* @link https://github.com/justintadlock/butterbean
|
||||
* @license http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
|
||||
*/
|
||||
|
||||
/**
|
||||
* Multiple setting class.
|
||||
*
|
||||
* @since 1.0.0
|
||||
* @access public
|
||||
*/
|
||||
class ButterBean_Setting_Multiple extends ButterBean_Setting {
|
||||
|
||||
/**
|
||||
* The type of setting.
|
||||
*
|
||||
* @since 1.0.0
|
||||
* @access public
|
||||
* @var string
|
||||
*/
|
||||
public $type = 'multiple';
|
||||
|
||||
/**
|
||||
* Gets the value of the setting.
|
||||
*
|
||||
* @since 1.0.0
|
||||
* @access public
|
||||
* @return mixed
|
||||
*/
|
||||
public function get_value() {
|
||||
|
||||
return get_post_meta( $this->manager->post_id, $this->name );
|
||||
}
|
||||
|
||||
/**
|
||||
* Sanitizes the value of the setting.
|
||||
*
|
||||
* @since 1.0.0
|
||||
* @access public
|
||||
* @param array $value
|
||||
* @return array
|
||||
*/
|
||||
public function sanitize( $values ) {
|
||||
|
||||
$multi_values = $values && ! is_array( $values ) ? explode( ',', $values ) : $values;
|
||||
|
||||
return $multi_values ? array_map( array( $this, 'map' ), $multi_values ) : array();
|
||||
}
|
||||
|
||||
/**
|
||||
* Helper function for sanitizing each value of the array.
|
||||
*
|
||||
* @since 1.0.0
|
||||
* @access public
|
||||
* @param mixed $value
|
||||
* @return mixed
|
||||
*/
|
||||
public function map( $value ) {
|
||||
|
||||
return apply_filters( "butterbean_{$this->manager->name}_sanitize_{$this->name}", $value, $this );
|
||||
}
|
||||
|
||||
/**
|
||||
* Saves the value of the setting.
|
||||
*
|
||||
* @since 1.0.0
|
||||
* @access public
|
||||
* @return void
|
||||
*/
|
||||
public function save() {
|
||||
|
||||
if ( ! $this->check_capabilities() )
|
||||
return;
|
||||
|
||||
$old_values = $this->get_value();
|
||||
$new_values = $this->get_posted_value();
|
||||
|
||||
// If there's an array of posted values, set them.
|
||||
if ( is_array( $new_values ) )
|
||||
$this->set_values( $new_values, $old_values );
|
||||
|
||||
// If no array of posted values but we have old values, delete them.
|
||||
else if ( $old_values )
|
||||
$this->delete_values();
|
||||
}
|
||||
|
||||
/**
|
||||
* Loops through new and old meta values and updates.
|
||||
*
|
||||
* @since 1.0.0
|
||||
* @access public
|
||||
* @param array $new_values
|
||||
* @param array $old_values
|
||||
* @return void
|
||||
*/
|
||||
public function set_values( $new_values, $old_values ) {
|
||||
|
||||
foreach ( $new_values as $new ) {
|
||||
|
||||
if ( ! in_array( $new, $old_values ) )
|
||||
$this->add_value( $new );
|
||||
}
|
||||
|
||||
foreach ( $old_values as $old ) {
|
||||
|
||||
if ( ! in_array( $old, $new_values ) )
|
||||
$this->remove_value( $old );
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Deletes old meta values.
|
||||
*
|
||||
* @since 1.0.0
|
||||
* @access public
|
||||
* @return void
|
||||
*/
|
||||
public function delete_values() {
|
||||
|
||||
return delete_post_meta( $this->manager->post_id, $this->name );
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds a single meta value.
|
||||
*
|
||||
* @since 1.0.0
|
||||
* @access public
|
||||
* @param mixed $value
|
||||
* @return bool
|
||||
*/
|
||||
public function add_value( $value ) {
|
||||
|
||||
return add_post_meta( $this->manager->post_id, $this->name, $value, false );
|
||||
}
|
||||
|
||||
/**
|
||||
* Deletes a single meta value.
|
||||
*
|
||||
* @since 1.0.0
|
||||
* @access public
|
||||
* @param mixed $value
|
||||
* @return bool
|
||||
*/
|
||||
public function remove_value( $value ) {
|
||||
|
||||
return delete_post_meta( $this->manager->post_id, $this->name, $value );
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user