1: <?php
2: 3: 4:
5: class Sidecar_Field {
6:
7: 8: 9:
10: var $plugin;
11:
12: 13: 14:
15: var $form;
16:
17: 18: 19:
20: var $section;
21:
22: 23: 24:
25: var $field_name;
26:
27: 28: 29:
30: var $field_slug;
31:
32: 33: 34:
35: var $field_label;
36:
37: 38: 39:
40: var $field_type;
41:
42: 43: 44:
45: var $field_help;
46:
47: 48: 49:
50: var $field_size;
51:
52: 53: 54:
55: var $field_validator;
56:
57: 58: 59:
60: var $field_default;
61:
62: 63: 64:
65: var $field_options;
66:
67: 68: 69:
70: var $field_required = false;
71:
72: 73: 74:
75: var $field_handler = false;
76:
77: 78: 79: 80: 81: 82: 83:
84: var $api_var;
85:
86: 87: 88:
89: var = array();
90:
91: 92: 93:
94: var $field_allow_html = false;
95:
96: 97: 98: 99:
100: function __construct( $field_name, $args = array() ) {
101: $this->field_name = $field_name;
102: 103: 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: 135:
136: function get_input_name() {
137: return "{$this->plugin->option_name}[{$this->form->form_name}][{$this->field_name}]";
138: }
139:
140: 141: 142: 143: 144: 145: 146: 147: 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: 157:
158: function get_input_size_html() {
159: return $this->field_size ? " size=\"{$this->field_size}\"" : '';
160: }
161:
162: 163: 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: 171:
172: function get_html() {
173: 174: 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:
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: 238: 239: 240:
241: function ( $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: 253:
254: function get_wrapper_id() {
255: return "field-{$this->field_slug}-input";
256: }
257: }
258: