* @package AnyComment\Admin
*/
class AnyCommentField {
const TYPE_TEXTAREA = 'textarea';
const TYPE_SELECT = 'select';
const TYPE_CHECKBOX = 'checkbox';
const TYPE_TEXT = 'text';
const TYPE_NUMBER = 'number';
const TYPE_COLOR = 'color';
/**
* @var string Field id.
*/
protected $id;
/**
* @var string Field title.
*/
protected $title;
/**
* @var string|null For attribute type pointing to the form field itself.
*/
protected $label_for;
/**
* @var string Field type.
*/
protected $type;
/**
* @var string|null Description displayed below the field.
*/
protected $description = null;
/**
* @var null|string|callable Custom content displayed before field.
*/
protected $before = null;
/**
* @var null|string|callable Custom content displayed after field.
*/
protected $after = null;
/**
* @var string|null
*/
protected $hint = null;
/**
* @var array List of additional arguments.
*/
protected $args = [];
/**
* @var array List of on events.
*/
protected $client_events = [];
/**
* @var null|string Page slug to which option belongs.
*/
protected $option_name = null;
/**
* @var string Wrapper class name.
*/
protected $wrapper_class = 'woption-field';
/**
* @var string Field wrapping element.
*/
protected $wrapper = '
{content}
';
/**
* AnyCommentField constructor.
*
* @param array $options Associative list of options to set object properties.
*/
public function __construct( array $options = [] ) {
if ( ! empty( $options ) ) {
foreach ( $options as $key => $value ) {
if ( property_exists( $this, $key ) ) {
$this->$key = $value;
}
}
}
}
/**
* @return string
*/
public function get_id() {
return $this->id;
}
/**
* @param string $id
*
* @return $this
*/
public function set_id( $id ) {
$this->id = $id;
return $this;
}
/**
* @return string
*/
public function get_title() {
return $this->title;
}
/**
* @param string $title
*
* @return $this
*/
public function set_title( $title ) {
$this->title = $title;
return $this;
}
/**
* @return string
*/
public function get_label_for() {
$label_for = $this->label_for;
if ( ! empty( $label_for ) ) {
return $label_for;
}
return $this->get_id();
}
/**
* @param string $label_for
*
* @return $this
*/
public function set_label_for( $label_for ) {
$this->label_for = $label_for;
return $this;
}
/**
* @return string
*/
public function get_type() {
return $this->type;
}
/**
* @param string $type
*
* @return $this
*/
public function set_type( $type ) {
$this->type = $type;
return $this;
}
/**
* @return null|string
*/
public function get_description() {
return $this->description;
}
/**
* @param null|string $description
*
* @return $this
*/
public function set_description( $description ) {
$this->description = $description;
return $this;
}
/**
* @return null|callable|string
*/
public function get_before() {
return $this->before;
}
/**
* @param null|callable|string $before
*
* @return $this
*/
public function set_before( $before ) {
$this->before = $before;
return $this;
}
/**
* @return callable|string
*/
public function get_after() {
return $this->after;
}
/**
* @param callable|string $after
*
* @return $this
*/
public function set_after( $after ) {
if ( is_callable( $after ) ) {
$this->after = call_user_func( $after );
} else {
$this->after = $after;
}
return $this;
}
/**
* @return null|string
*/
public function get_hint() {
return $this->hint;
}
/**
* @param null|string $hint
*
* @return $this
*/
public function set_hint( $hint ) {
$this->hint = $hint;
return $this;
}
/**
* @return array
*/
public function get_args() {
return $this->args;
}
/**
* @param array $args
*
* @return $this
*/
public function set_args( $args ) {
$this->args = $args;
return $this;
}
/**
* Set text type.
*
* @return $this
*/
public function text() {
$this->set_type( self::TYPE_TEXT );
return $this;
}
/**
* Set number type.
*
* @return $this
*/
public function number() {
$this->set_type( self::TYPE_NUMBER );
return $this;
}
/**
* Set color type.
*
* @return $this
*/
public function color() {
$this->set_type( self::TYPE_COLOR );
return $this;
}
/**
* Set checkbox type.
*
* @return $this
*/
public function checkbox() {
$this->set_type( self::TYPE_CHECKBOX );
return $this;
}
/**
* Set select type.
* @return $this
*/
public function select() {
$this->set_type( self::TYPE_SELECT );
return $this;
}
/**
* Set textarea type.
* @return $this
*/
public function textarea() {
$this->set_type( self::TYPE_TEXTAREA );
return $this;
}
/**
* Get field value is was previously set.
*
* @return null|string
*/
public function get_value() {
$options = get_option( $this->option_name, null );
if ( null === $options ) {
return null;
}
$value = isset( $options[ $this->get_id() ] ) ? $options[ $this->get_id() ] : null;
if ( $value === null ) {
return null;
}
switch ( $value ) {
case '1':
case 'true':
case 'on':
return true;
case '0':
case 'false':
case 'off':
return false;
}
return $value;
}
/**
* Helper to render select.
*
* @return string
*/
public function input_select() {
$for = $this->get_label_for();
$name = $this->get_id();
$args = $this->get_args();
$selected_value = $this->get_value();
$options = isset( $args['options'] ) ? $args['options'] : null;
if ( $options === null ) {
return '';
}
$options_html = '';
foreach ( $options as $key => $value ) {
$selected = $value !== null ? ( selected( $selected_value, $key, false ) ) : ( '' );
$options_html .= sprintf( '', $key, $selected, $value );
}
return <<