PageRenderTime 50ms CodeModel.GetById 25ms RepoModel.GetById 1ms app.codeStats 0ms

/plugins/the-events-calendar/lib/tribe-field.class.php

https://bitbucket.org/codemen_iftekhar/codemen
PHP | 527 lines | 276 code | 56 blank | 195 comment | 23 complexity | e2c860a1a31a84256040b4fc10cccfd0 MD5 | raw file
  1. <?php
  2. // Don't load directly
  3. if ( !defined('ABSPATH') ) die('-1');
  4. if ( !class_exists('TribeField') ) {
  5. /**
  6. * helper class that creates fields for use in Settings, MetaBoxes, Users, anywhere.
  7. * Instantiate it whenever you need a field
  8. *
  9. * @since 2.0.5
  10. * @author jkudish
  11. */
  12. class TribeField {
  13. /**
  14. * the field's id
  15. * @var string
  16. */
  17. public $id;
  18. /**
  19. * the field's name (also known as it's label)
  20. * @var string
  21. */
  22. public $name;
  23. /**
  24. * the field's arguments
  25. * @var array
  26. */
  27. public $args;
  28. /**
  29. * field defaults (static)
  30. * @var array
  31. */
  32. public static $defaults;
  33. /**
  34. * valid field types (static)
  35. * @var array
  36. */
  37. public static $valid_field_types;
  38. /**
  39. * Class constructor
  40. *
  41. * @since 2.0.5
  42. * @author jkudish
  43. * @param string $id the field id
  44. * @param array $field the field settings
  45. * @param mixed $value the field's current value
  46. * @return void
  47. */
  48. public function __construct($id, $field, $value = null) {
  49. // seetup the defaults
  50. $this->defaults = array(
  51. 'type' => 'html',
  52. 'name' => $id,
  53. 'class' => null,
  54. 'label' => null,
  55. 'tooltip' => null,
  56. 'size' => 'medium',
  57. 'html' => null,
  58. 'error' => false,
  59. 'value' => $value,
  60. 'options' => null,
  61. 'conditional' => true,
  62. 'display_callback' => null,
  63. 'if_empty' => null,
  64. 'can_be_empty' => false,
  65. 'clear_after' => true,
  66. );
  67. // a list of valid field types, to prevent screwy behaviour
  68. $this->valid_field_types = array(
  69. 'heading',
  70. 'html',
  71. 'text',
  72. 'textarea',
  73. 'radio',
  74. 'checkbox_bool',
  75. 'checkbox_list',
  76. 'dropdown',
  77. 'dropdown_chosen',
  78. 'license_key',
  79. );
  80. apply_filters( 'tribe_valid_field_types', $this->valid_field_types );
  81. // parse args with defaults and extract them
  82. $args = wp_parse_args($field, $this->defaults);
  83. extract($args);
  84. // sanitize the values just to be safe
  85. $id = esc_attr($id);
  86. $type = esc_attr($type);
  87. $name = esc_attr($name);
  88. $class = sanitize_html_class($class);
  89. $label = wp_kses($label, array('a' => array('href' => array(),'title' => array()),'br' => array(),'em' => array(),'strong' => array(), 'b' => array(), 'i' => array(), 'u' => array(), 'img' => array('title' => array(), 'src' => array(), 'alt' => array()) ));
  90. $tooltip = wp_kses($tooltip, array('a' => array('href' => array(),'title' => array()),'br' => array(),'em' => array(),'strong' => array(), 'b' => array(), 'i' => array(), 'u' => array(), 'img' => array('title' => array(), 'src' => array(), 'alt' => array()), 'code' => array('span' => array()), 'span' => array() ));
  91. $size = esc_attr($size);
  92. $html = $html;
  93. $error = (bool) $error;
  94. $value = $value;
  95. $conditional = $conditional;
  96. $display_callback = $display_callback;
  97. $clear_after = (bool) $clear_after;
  98. // set the ID
  99. $this->id = apply_filters( 'tribe_field_id', $id );
  100. // set each instance variable and filter
  101. foreach ($this->defaults as $key => $value) {
  102. $this->{$key} = apply_filters( 'tribe_field_'.$key, $$key, $this->id );
  103. }
  104. // epicness
  105. $this->doField();
  106. }
  107. /**
  108. * Determines how to handle this field's creation
  109. * either calls a callback function or runs this class' course of action
  110. * logs an error if it fails
  111. *
  112. * @since 2.0.5
  113. * @author jkudish
  114. * @param string $id the field id
  115. * @param array $field the field settings
  116. * @return void
  117. */
  118. public function doField() {
  119. if ($this->conditional) {
  120. if ( $this->display_callback && is_string($this->display_callback) && function_exists($this->display_callback) ) {
  121. // if there's a callback, run it
  122. call_user_func($this->display_callback);
  123. } elseif ( in_array($this->type, $this->valid_field_types) ) {
  124. // the specified type exists, run the appropriate method
  125. $field = call_user_method($this->type, $this);
  126. // filter the output
  127. $field = apply_filters( 'tribe_field_output_'.$this->type, $field, $this->id, $this );
  128. echo apply_filters( 'tribe_field_output_'.$this->type.'_'.$this->id, $field, $this->id, $this );
  129. } else {
  130. // fail, log the error
  131. TribeEvents::debug( __('Invalid field type specified', 'tribe-events-calendar'), $this->type, 'notice');
  132. }
  133. }
  134. }
  135. /**
  136. * returns the field's start
  137. *
  138. * @since 2.0.5
  139. * @author jkudish
  140. * @return string the field start
  141. */
  142. public function doFieldStart() {
  143. $return = '<fieldset id="tribe-field-'.$this->id.'"';
  144. $return .= ' class="tribe-field tribe-field-'.$this->type;
  145. $return .= ($this->error) ? ' tribe-error' : '';
  146. $return .= ($this->size) ? ' tribe-size-'.$this->size : '';
  147. $return .= ($this->class) ? ' '.$this->class.'"' : '"';
  148. $return .= '>';
  149. return apply_filters( 'tribe_field_start', $return, $this->id, $this->type, $this->error, $this->class, $this );
  150. }
  151. /**
  152. * returns the field's end
  153. *
  154. * @since 2.0.5
  155. * @author jkudish
  156. * @return string the field end
  157. */
  158. public function doFieldEnd() {
  159. $return = '</fieldset>';
  160. $return .= ($this->clear_after) ? '<div class="clear"></div>' : '';
  161. return apply_filters( 'tribe_field_end', $return, $this->id, $this );
  162. }
  163. /**
  164. * returns the field's label
  165. *
  166. * @since 2.0.5
  167. * @author jkudish
  168. * @return string the field label
  169. */
  170. public function doFieldLabel() {
  171. $return = '';
  172. if ($this->label)
  173. $return = '<legend class="tribe-field-label">'.$this->label.'</legend>';
  174. return apply_filters( 'tribe_field_label', $return, $this->label, $this );
  175. }
  176. /**
  177. * returns the field's div start
  178. *
  179. * @since 2.0.5
  180. * @author jkudish
  181. * @return string the field div start
  182. */
  183. public function doFieldDivStart() {
  184. $return = '<div class="tribe-field-wrap">';
  185. return apply_filters( 'tribe_field_div_start', $return, $this );
  186. }
  187. /**
  188. * returns the field's div end
  189. *
  190. * @since 2.0.5
  191. * @author jkudish
  192. * @return string the field div end
  193. */
  194. public function doFieldDivEnd() {
  195. $return = $this->doToolTip();
  196. $return .= '</div>';
  197. return apply_filters( 'tribe_field_div_end', $return, $this );
  198. }
  199. /**
  200. * returns the field's tooltip/description
  201. *
  202. * @since 2.0.5
  203. * @author jkudish
  204. * @return string the field tooltip
  205. */
  206. public function doToolTip() {
  207. $return = '';
  208. if ($this->tooltip)
  209. $return = '<p class="description">'.$this->tooltip.'</p>';
  210. return apply_filters( 'tribe_field_tooltip', $return, $this->tooltip, $this );
  211. }
  212. /**
  213. * returns the screen reader label
  214. *
  215. * @since 2.0.5
  216. * @author jkudish
  217. * @return string the screen reader label
  218. */
  219. public function doScreenReaderLabel() {
  220. $return = '';
  221. if ($this->tooltip)
  222. $return = '<label class="screen-reader-text">'.$this->tooltip.'</label>';
  223. return apply_filters( 'tribe_field_screen_reader_label', $return, $this->tooltip, $this );
  224. }
  225. /**
  226. * returns the field's value
  227. *
  228. * @since 2.0.5
  229. * @author jkudish
  230. * @return string the field value
  231. */
  232. public function doFieldValue() {
  233. $return = '';
  234. if ($this->value)
  235. $return = ' value="'.$this->value.'"';
  236. return apply_filters( 'tribe_field_value', $return, $this->value, $this );
  237. }
  238. /**
  239. * returns the field's name
  240. *
  241. * @since 2.0.5
  242. * @author jkudish
  243. * @return string the field name
  244. */
  245. public function doFieldName($multi = false) {
  246. $return = '';
  247. if ($this->name)
  248. if( $multi ){
  249. $return = ' name="'.$this->name.'[]"';
  250. }else{
  251. $return = ' name="'.$this->name.'"';
  252. }
  253. return apply_filters( 'tribe_field_name', $return, $this->name, $this );
  254. }
  255. /**
  256. * generate a heading field
  257. *
  258. * @since 2.0.5
  259. * @author jkudish
  260. * @return string the field
  261. */
  262. public function heading() {
  263. $field = '<h3>'.$this->label.'</h3>';
  264. return $field;
  265. }
  266. /**
  267. * generate an html field
  268. *
  269. * @since 2.0.5
  270. * @author jkudish
  271. * @return string the field
  272. */
  273. public function html() {
  274. $field = $this->doFieldLabel();
  275. $field .= $this->html;
  276. return $field;
  277. }
  278. /**
  279. * generate a simple text field
  280. *
  281. * @since 2.0.5
  282. * @author jkudish
  283. * @return string the field
  284. */
  285. public function text() {
  286. $field = $this->doFieldStart();
  287. $field .= $this->doFieldLabel();
  288. $field .= $this->doFieldDivStart();
  289. $field .= '<input';
  290. $field .= ' type="text"';
  291. $field .= $this->doFieldName();
  292. $field .= $this->doFieldValue();
  293. $field .= '/>';
  294. $field .= $this->doScreenReaderLabel();
  295. $field .= $this->doFieldDivEnd();
  296. $field .= $this->doFieldEnd();
  297. return $field;
  298. }
  299. /**
  300. * generate a textarea field
  301. *
  302. * @since 2.0.5
  303. * @author jkudish
  304. * @return string the field
  305. */
  306. public function textarea() {
  307. $field = $this->doFieldStart();
  308. $field .= $this->doFieldLabel();
  309. $field .= $this->doFieldDivStart();
  310. $field .= '<textarea';
  311. $field .= $this->doFieldName();
  312. $field .= '>';
  313. $field .= stripslashes($this->value);
  314. $field .= '</textarea>';
  315. $field .= $this->doScreenReaderLabel();
  316. $field .= $this->doFieldDivEnd();
  317. $field .= $this->doFieldEnd();
  318. return $field;
  319. }
  320. /**
  321. * generate a radio button field
  322. *
  323. * @since 2.0.5
  324. * @author jkudish
  325. * @return string the field
  326. */
  327. public function radio() {
  328. $field = $this->doFieldStart();
  329. $field .= $this->doFieldLabel();
  330. $field .= $this->doFieldDivStart();
  331. if ( is_array($this->options) ) {
  332. foreach ($this->options as $option_id => $title) {
  333. $field .= '<label title="'.$title.'">';
  334. $field .= '<input type="radio"';
  335. $field .= $this->doFieldName();
  336. $field .= ' value="'.$option_id.'" '.checked( $this->value, $option_id, false ).'/>';
  337. $field .= $title;
  338. $field .= '</label>';
  339. }
  340. } else {
  341. $field .= '<span class="tribe-error">'.__('No radio options specified', 'tribe-events-calendar').'</span>';
  342. }
  343. $field .= $this->doFieldDivEnd();
  344. $field .= $this->doFieldEnd();
  345. return $field;
  346. }
  347. /**
  348. * generate a checkbox_list field
  349. *
  350. * @since 2.0.5
  351. * @author jkudish, modified by nciske
  352. * @return string the field
  353. */
  354. public function checkbox_list() {
  355. $field = $this->doFieldStart();
  356. $field .= $this->doFieldLabel();
  357. $field .= $this->doFieldDivStart();
  358. if( ! is_array( $this->value ) ){
  359. if( !empty( $this->value ) ){
  360. $this->value = array( $this->value );
  361. } else {
  362. $this->value = array();
  363. }
  364. }
  365. if ( is_array($this->options) ) {
  366. foreach ($this->options as $option_id => $title) {
  367. $field .= '<label title="'.$title.'">';
  368. $field .= '<input type="checkbox"';
  369. $field .= $this->doFieldName(true);
  370. $field .= ' value="'.$option_id.'" '.checked( in_array($option_id, $this->value), true, false ).'/>';
  371. $field .= $title;
  372. $field .= '</label>';
  373. }
  374. } else {
  375. $field .= '<span class="tribe-error">'.__('No checkbox options specified', 'tribe-events-calendar').'</span>';
  376. }
  377. $field .= $this->doFieldDivEnd();
  378. $field .= $this->doFieldEnd();
  379. return $field;
  380. }
  381. /**
  382. * generate a boolean checkbox field
  383. *
  384. * @since 2.0.5
  385. * @author jkudish
  386. * @return string the field
  387. */
  388. public function checkbox_bool() {
  389. $field = $this->doFieldStart();
  390. $field .= $this->doFieldLabel();
  391. $field .= $this->doFieldDivStart();
  392. $field .= '<input type="checkbox"';
  393. $field .= $this->doFieldName();
  394. $field .= ' value="1" '.checked( $this->value, true, false );
  395. $field .= '/>';
  396. $field .= $this->doScreenReaderLabel();
  397. $field .= $this->doFieldDivEnd();
  398. $field .= $this->doFieldEnd();
  399. return $field;
  400. }
  401. /**
  402. * generate a dropdown field
  403. *
  404. * @since 2.0.5
  405. * @author jkudish
  406. * @return string the field
  407. */
  408. public function dropdown() {
  409. $field = $this->doFieldStart();
  410. $field .= $this->doFieldLabel();
  411. $field .= $this->doFieldDivStart();
  412. if ( is_array($this->options) && !empty($this->options) ) {
  413. $field .= '<select';
  414. $field .= $this->doFieldName();
  415. $field .= '>';
  416. foreach ($this->options as $option_id => $title) {
  417. $field .= '<option value="'.$option_id.'"';
  418. $field .= selected( $this->value, $option_id, false );
  419. $field .= isset($this->value[0]) ? selected( $this->value[0], $option_id, false ) : '';
  420. $field .= '>'.$title.'</option>';
  421. }
  422. $field .= '</select>';
  423. $field .= $this->doScreenReaderLabel();
  424. } elseif ($this->if_empty) {
  425. $field .= '<span class="empty-field">'.(string) $this->if_empty.'</span>';
  426. } else {
  427. $field .= '<span class="tribe-error">'.__('No select options specified', 'tribe-events-calendar').'</span>';
  428. }
  429. $field .= $this->doFieldDivEnd();
  430. $field .= $this->doFieldEnd();
  431. return $field;
  432. }
  433. /**
  434. * generate a chosen dropdown field - the same as the
  435. * regular dropdown but wrapped so it can have the
  436. * right css class applied to it
  437. *
  438. * @since 2.0.5
  439. * @author jkudish
  440. * @return string the field
  441. */
  442. public function dropdown_chosen() {
  443. $field = $this->dropdown();
  444. return $field;
  445. }
  446. /**
  447. * generate a license key field
  448. *
  449. * @since 2.0.5
  450. * @author jkudish
  451. * @return string the field
  452. */
  453. public function license_key() {
  454. $field = $this->doFieldStart();
  455. $field .= $this->doFieldLabel();
  456. $field .= $this->doFieldDivStart();
  457. $field .= '<input';
  458. $field .= ' type="text"';
  459. $field .= $this->doFieldName();
  460. $field .= $this->doFieldValue();
  461. $field .= '/>';
  462. $field .= '<img src="'.esc_url( admin_url( 'images/wpspin_light.gif' ) ).'" class="ajax-loading-license" alt="Loading" style="display: none"/>';
  463. $field .= '<span class="valid-key"></span>';
  464. $field .= '<span class="invalid-key"></span>';
  465. $field .= $this->doScreenReaderLabel();
  466. $field .= $this->doFieldDivEnd();
  467. $field .= $this->doFieldEnd();
  468. return $field;
  469. }
  470. } // end class
  471. } // endif class_exists