args = $args;
$this->_id = $args['id'];
if ( $options ) {
$this->options = $options;
}
$this->_type = $type;
$this->_object = $object;
$this->extra_css_class = 'xts-' . $this->args['type'] . '-control';
$this->extra_css_class .= ' xts-' . $this->args['id'] . '-field';
if ( $this->dependency_class() ) {
$this->extra_css_class .= ' ' . $this->dependency_class();
}
if ( isset( $this->args['class'] ) ) {
$this->extra_css_class .= ' ' . $this->args['class'];
}
add_action( 'admin_enqueue_scripts', array( $this, 'enqueue' ), 300 );
add_action( 'wp_enqueue_scripts', array( $this, 'frontend_enqueue' ), 300 );
}
/**
* Validate field value. For example check file ID and URL.
*
* @since 1.0.0
*
* @param string or array $value Field value.
*
* @return mixed
*/
public function validate( $value ) {
return $value;
}
/**
* ID getter
*
* @since 1.0.0
*
* @return int field id value.
*/
public function get_id() {
return $this->_id;
}
/**
* Update options array. Needed for presets functionality on the demo website.
*
* @since 1.0.0
*
* @param array $options New options array.
*/
public function override_options( $options ) {
$this->options = $options;
}
/**
* Set post
*
* @since 1.0.0
*
* @param object $object Post object for metaboxes fields.
*/
public function set_post( $object ) {
if ( is_a( $object, 'WP_Post' ) && 'metabox' === $this->_type ) {
$this->_post = $object;
}
}
/**
* Render the field HTML based on the control class.
*
* @since 1.0.0
*
* @param object $object Post or Term object for metaboxes fields.
* @param bool $preset Current field preset ID.
*/
public function render( $object = null, $preset = false ) {
if ( $preset ) {
$this->_presets = array( $preset );
}
if ( $preset && $this->is_inherit_value() ) {
$this->extra_css_class .= ' xts-field-disabled';
}
if ( is_a( $object, 'WP_Post' ) ) {
$this->_post = $object;
} elseif ( is_a( $object, 'WP_Term' ) ) {
$this->_term = $object;
}
$this->before();
echo '
';
$this->render_control();
echo '
';
$this->after();
}
/**
* Before the control output.
*
* @since 1.0.0
*/
public function before() {
?>
opt_name . '-options';
$name .= '[' . $this->args['id'] . ']';
if ( 'metabox' === $this->_type ) {
$name = $this->args['id'];
}
if ( false !== $subkey ) {
$name .= '[' . $subkey . ']';
}
if ( false !== $subkey2 ) {
$name .= '[' . $subkey2 . ']';
}
if ( false !== $subkey3 ) {
$name .= '[' . $subkey3 . ']';
}
return $name;
}
/**
* Get field value from options array or from post meta data.
*
* @since 1.0.0
*
* @param bool $subkey Subkey for array fields.
*
* @return mixed Field value.
*/
public function get_field_value( $subkey = false ) {
$val = '';
$object = $this->_post ? $this->_post : $this->_term;
if ( 'metabox' === $this->_type && ! is_null( $object ) ) {
$object_id = $this->_post ? $this->_post->ID : $this->_term->term_id;
$val = get_metadata( $this->_object, $object_id, $this->get_input_name(), true );
} elseif ( false !== $this->_presets ) {
foreach ( $this->_presets as $preset_id ) {
if ( isset( $this->options[ $preset_id ] ) && isset( $this->options[ $preset_id ][ $this->args['id'] ] ) ) {
$val = $this->options[ $preset_id ][ $this->args['id'] ];
}
}
if ( empty( $val ) && '0' !== $val ) {
$val = $this->options[ $this->args['id'] ];
}
} elseif ( isset( $this->options[ $this->args['id'] ] ) ) {
$val = $this->options[ $this->args['id'] ];
}
// Single metadata value, or array of values. If the $meta_type or $object_id parameters are invalid, false is returned. If the meta value isn't set, an empty string or array is returned, respectively.
if ( 'metabox' === $this->_type && empty( $val ) && '0' !== $val ) {
$val = isset( $this->args['default'] ) ? $this->args['default'] : '';
}
$val = $this->validate( $val );
if ( $subkey ) {
return isset( $val[ $subkey ] ) ? $val[ $subkey ] : '';
}
if ( isset( $val['{{index}}'] ) ) {
unset( $val['{{index}}'] );
}
return $val;
}
/**
* Get field options array. For select or buttons set field type.
*
* @since 1.0.0
*
* @return array Field options array.
*/
public function get_field_options() {
if ( ! isset( $this->args['options'] ) ) {
return array();
}
return $this->args['options'];
}
/**
* Enqueue required scripts and styles for controls.
*
* @since 1.0.0
*/
public function enqueue() {}
/**
* Enqueue required scripts and styles on frontend.
*
* @since 1.0.0
*/
public function frontend_enqueue() {}
/**
* Output field's css code on frontend based on the control and its value.
*
* @since 1.0.0
*/
public function css_output() {}
/**
* Sanitize field and its value before saving.
*
* @since 1.0.0
*
* @param string $value Field value string.
*
* @return mixed.
*/
public function sanitize( $value ) {
$sanitization = new Sanitize( $this, $value );
return $sanitization->sanitize();
}
}