      1: <?php
      2: /**
      3:  *
      4:  */
      5: class Sidecar_Field {
      6:
      7:   /**
      8:    * @var Sidecar_Plugin_Base
      9:    */
     10:   var $plugin;
     11:
     12:   /**
     13:    * @var Sidecar_Form
     14:    */
     15:   var $form;
     16:
     17:   /**
     18:    * @var array
     19:    */
     20:   var $section;
     21:
     22:   /**
     23:    * @var string
     24:    */
     25:   var $field_name;
     26:
     27:   /**
     28:    * @var string
     29:    */
     30:   var $field_slug;
     31:
     32:   /**
     33:    * @var string
     34:    */
     35:   var $field_label;
     36:
     37:   /**
     38:    * @var string
     39:    */
     40:   var $field_type;
     41:
     42:   /**
     43:    * @var string
     44:    */
     45:   var $field_help;
     46:
     47:   /**
     48:    * @var int
     49:    */
     50:   var $field_size;
     51:
     52:   /**
     53:    * @var int|array
     54:    */
     55:   var $field_validator;
     56:
     57:   /**
     58:    * @var string
     59:    */
     60:   var $field_default;
     61:
     62:   /**
     63:    * @var array
     64:    */
     65:   var $field_options;
     66:
     67:   /**
     68:    * @var bool
     69:    */
     70:   var $field_required = false;
     71:
     72:   /**
     73:    * @var bool|callable
     74:    */
     75:   var $field_handler = false;
     76:
     77:   /**
     78:    * Indicates this field value if used for an API.
     79:    * Defaults to $this->field_name, can be see to another name
     80:    * (i.e. field_name = 'content_type', api_var = 'type'
     81:    * but if set to false it will cause the API to ignore this field.
     82:    * @var null|bool|string
     83:    */
     84:   var $api_var;
     85:
     86:   /**
     87:    * @var array
     88:    */
     89:   var $_extra = array();
     90:
     91:   /**
     92:    * @var array
     93:    */
     94:   var $field_allow_html = false;
     95:
     96:   /**
     97:    * @param string $field_name
     98:    * @param array $args
     99:    */
    100:   function __construct( $field_name, $args = array() ) {
    101:     $this->field_name = $field_name;
    102:     /**
    103:      * Copy properties in from $args, if they exist.
    104:      */
    105:     foreach( $args as $property => $value ) {
    106:       if ( property_exists( $this, $property ) ) {
    107:         $this->$property = $value;
    108:       } else if ( property_exists( $this, $property = "field_{$property}" ) ) {
    109:         $this->$property = $value;
    110:       } else {
    111:         $this->_extra[$property] = $value;
    112:       }
    113:     }
    114:     if ( ! $this->field_type )
    115:       $this->field_type = 'password' == $this->field_name ? 'password' : 'text';
    116:
    117:     if ( 'hidden' == $this->field_type )
    118:       $this->field_label = false;
    119:     else if ( ! $this->field_label )
    120:       $this->field_label = ucwords( $this->field_name );
    121:
    122:     if ( ! $this->field_size )
    123:       $this->field_size = preg_match( '#(text|password)#', $this->field_type ) ? 40 : false;
    124:
    125:     if ( ! $this->field_slug )
    126:       $this->field_slug = str_replace( array( '_', ' ' ), '-', $this->field_name );
    127:
    128:     if ( is_null( $this->api_var ) )
    129:       $this->api_var = $this->field_name;
    130:
    131:   }
    132:
    133:   /**
    134:    * @return string
    135:    */
    136:   function get_input_name() {
    137:     return "{$this->plugin->option_name}[{$this->form->form_name}][{$this->field_name}]";
    138:   }
    139:
    140:   /**
    141:    * Sets HTML id like the following:
    142:    *
    143:    * @example
    144:    *  HTML name = my_plugin_settings[_form-name}[field-name]
    145:    *  HTML id =>  my-plugin-settings--form-name-field-name
    146:    *
    147:    * @return string
    148:    */
    149:   function get_input_id() {
    150:     $input_name = $this->get_input_name();
    151:     $input_id = str_replace( array( '[_', '_', '][', '[', ']' ), array( '--', '-', '-', '-', '' ), $input_name );
    152:     return $input_id;
    153:   }
    154:
    155:   /**
    156:    * @return string
    157:    */
    158:   function get_input_size_html() {
    159:     return $this->field_size ? " size=\"{$this->field_size}\"" : '';
    160:   }
    161:
    162:   /**
    163:    * @return string
    164:    */
    165:   function get_input_help_html() {
    166:     return $this->field_help ? "\n<br />\n<span class=\"{$this->plugin->css_base}-field-help\">{$this->field_help}</span>" : false;
    167:   }
    168:
    169:   /**
    170:    * @return string
    171:    */
    172:   function get_html() {
    173:     /**
    174:      * @todo: Get options with all expected elements initialized
    175:      */
    176:     $form = $this->form;
    177:     $value = $form->get_setting( $this->field_name );
    178:     $input_name = $this->get_input_name();
    179:     $input_id = $this->get_input_id();
    180:     $size_html = $this->get_input_size_html();
    181:     $css_base = $this->plugin->css_base;
    182:     $help_html = $this->get_input_help_html();
    183:
    184:     if ( 'radio' == $this->field_type ) {
    185:       $html = array( "<ul id=\"{$input_id}-radio-field-options\" class=\"radio-field-options\">" );
    186:       foreach( $this->field_options as $option_value => $option_label ) {
    187:         $checked = ( ! empty( $value ) && $option_value == $value ) ? 'checked="checked" ' : false;
    188:         $option_value = esc_attr( $option_value );
    189:         $html[] =<<<HTML
    190: <li><input type="radio" id="{$input_id}" class="{$css_base}-field" name="{$input_name}" value="{$option_value}" {$checked}/>
    191: <label for={$input_id}">{$option_label}</label></li>
    192: HTML;
    193:       }
    194:       $html = implode( "\n", $html ) . "</ul>{$help_html}";
    195:     } else if ( 'select' == $this->field_type ) {
    196:       $html = array( "<select id=\"{$input_id}-select-field-options\" name=\"{$input_name}\" class=\"select-field-options\">" );
    197:       foreach( $this->field_options as $option_value => $option_label ) {
    198:         $selected = ( ! empty( $value ) && $option_value == $value ) ? ' selected="selected"' : false;
    199:         $option_value = esc_attr( $option_value );
    200:         $html[] =<<<HTML
    201: <option value="{$option_value}"{$selected}>{$option_label}</option>
    202: HTML;
    203:       }
    204:       $html = implode( "\n", $html ) . "</select>{$help_html}";
    205:     } else if ( 'checkbox' == $this->field_type ) {
    206:       $checked = ! empty( $value ) ? 'checked="checked" ' : false;
    207:       $html =<<<HTML
    208: <input type="checkbox" id="{$input_id}" class="{$css_base}-field" name="{$input_name}" value="1" {$checked}/>
    209: <label for="{$input_id}">{$this->field_label}</label>
    210: HTML;
    211:     } else if ( 'hidden' == $this->field_type ) {
    212:       $html =<<<HTML
    213: <input type="hidden" id="{$input_id}" name="{$input_name}" value="{$value}" />
    214: HTML;
    215:     } else if ( 'textarea' == $this->field_type ) {
    216: //      $value = htmlentities( $value );
    217:       if ( $rows = $this->get_extra( 'rows' ) )
    218:         $rows = " rows=\"{$rows}\"";
    219:       if ( $cols = $this->get_extra( 'cols' ) )
    220:         $cols = " cols=\"{$cols}\"";
    221:       $html =<<<HTML
    222: <textarea id="{$input_id}" name="{$input_name}"{$rows}{$cols}>{$value}</textarea>{$help_html}
    223: HTML;
    224:     } else {
    225:       $html =<<<HTML
    226: <input type="{$this->field_type}" id="{$input_id}" name="{$input_name}" value="{$value}" class="{$css_base}-field"{$size_html}/>{$help_html}
    227: HTML;
    228:   }
    229:     $field_wrapper_id = $this->get_wrapper_id();
    230:     $html =<<<HTML
    231: <div id="{$field_wrapper_id}" class="{$this->field_type}">{$html}</div>
    232: HTML;
    233:     return $html;
    234:   }
    235:
    236:   /**
    237:    * @param string|$property_name
    238:    *
    239:    * @return null|int|string
    240:    */
    241:   function get_extra( $property_name ) {
    242:     $value = null;
    243:     if ( isset( $this->_extra[$prefixed_name = "field_{$property_name}"] ) ) {
    244:       $value = $this->_extra[$prefixed_name];
    245:     } else if ( isset( $this->_extra[$property_name] ) ) {
    246:       $value = $this->_extra[$property_name];
    247:     }
    248:     return $value;
    249:   }
    250:
    251:   /**
    252:    * @return string
    253:    */
    254:   function get_wrapper_id() {
    255:     return "field-{$this->field_slug}-input";
    256:   }
    257: }
    258: