SimpleCalendarAbstractsField

The Field.

Defined (1)

The class is defined in the following location(s).

/includes/abstracts/field.php  
  1. abstract class Field { 
  2.  
  3. /** 
  4. * Field type. 
  5. * @access public 
  6. * @var string 
  7. */ 
  8. public $type = ''; 
  9.  
  10. /** 
  11. * Field name. 
  12. * @access public 
  13. * @var string 
  14. */ 
  15. public $name = ''; 
  16.  
  17. /** 
  18. * Field Id. 
  19. * @access public 
  20. * @var string 
  21. */ 
  22. public $id = ''; 
  23.  
  24. /** 
  25. * Field title (label). 
  26. * @access public 
  27. * @var string 
  28. */ 
  29. public $title = ''; 
  30.  
  31. /** 
  32. * Field type class. 
  33. * @access protected 
  34. * @var string 
  35. */ 
  36. protected $type_class = ''; 
  37.  
  38. /** 
  39. * CSS classes. 
  40. * @access public 
  41. * @var string 
  42. */ 
  43. public $class = ''; 
  44.  
  45. /** 
  46. * CSS styles. 
  47. * @access public 
  48. * @var string 
  49. */ 
  50. public $style = ''; 
  51.  
  52. /** 
  53. * Description. 
  54. * @access public 
  55. * @var string 
  56. */ 
  57. public $description = ''; 
  58.  
  59. /** 
  60. * Tooltip text. 
  61. * @access public 
  62. * @var string 
  63. */ 
  64. public $tooltip = ''; 
  65.  
  66. /** 
  67. * Attributes. 
  68. * @access public 
  69. * @var string 
  70. */ 
  71. public $attributes = ''; 
  72.  
  73. /** 
  74. * Placeholder. 
  75. * @access public 
  76. * @var string 
  77. */ 
  78. public $placeholder = ''; 
  79.  
  80. /** 
  81. * Options. 
  82. * @access public 
  83. * @var array 
  84. */ 
  85. public $options = array(); 
  86.  
  87. /** 
  88. * Value. 
  89. * @access public 
  90. * @var array|string 
  91. */ 
  92. public $value = ''; 
  93.  
  94. /** 
  95. * Default value. 
  96. * @access public 
  97. * @var array|string 
  98. */ 
  99. public $default = ''; 
  100.  
  101. public $text = ''; 
  102.  
  103. /** 
  104. * Validation result. 
  105. * @access public 
  106. * @var string|true 
  107. */ 
  108. public $validation = true; 
  109.  
  110. /** 
  111. * Construct the field. 
  112. * Escapes and sets every field property. 
  113. * @since 3.0.0 
  114. * @param array $field Field data. 
  115. */ 
  116. public function __construct( $field ) { 
  117.  
  118. // Field properties. 
  119. if ( isset( $field['title'] ) ) { 
  120. $this->title = esc_attr( $field['title'] ); 
  121. if ( isset( $field['description'] ) ) { 
  122. $this->description = wp_kses_post( $field['description'] ); 
  123. if ( isset( $field['type'] ) ) { 
  124. $this->type = esc_attr( $field['type'] ); 
  125. if ( isset( $field['name'] ) ) { 
  126. $this->name = esc_attr( $field['name'] ); 
  127. if ( isset( $field['id'] ) ) { 
  128. $this->id = esc_attr( $field['id'] ); 
  129. if ( isset( $field['placeholder'] ) ) { 
  130. $this->placeholder = esc_attr( $field['placeholder'] ); 
  131. if ( isset( $field['options'] ) && is_array( $field['options'] ) ) { 
  132. $this->options = array_map( 'esc_attr', $field['options'] ); 
  133. if ( isset( $field['text'] ) ) { 
  134. $this->text = $field['text']; 
  135.  
  136. // Escaping. 
  137. if ( ! empty( $field['escaping'] ) && ( is_string( $field['escaping'] ) || is_array( $field['escaping'] ) ) ) { 
  138. if ( isset( $field['default'] ) ) { 
  139. $this->default = $this->escape_callback( $field['escaping'], $field['default'] ); 
  140. if ( isset( $field['value'] ) ) { 
  141. $this->value = $this->escape_callback( $field['escaping'], $field['value'] ); 
  142. } else { 
  143. if ( isset( $field['default'] ) ) { 
  144. $this->default = $this->escape( $field['default'] ); 
  145. if ( isset( $field['value'] ) ) { 
  146. $this->value = $this->escape( $field['value'] ); 
  147.  
  148. // Validation. 
  149. if ( ! empty( $field['validation'] ) ) { 
  150. $this->validation = $this->validate( $field['validation'], $this->value ); 
  151.  
  152. // CSS classes and styles. 
  153. $classes = isset( $field['class'] ) ? $field['class'] : ''; 
  154. $this->set_class( $classes ); 
  155. if ( isset( $field['style'] ) ) { 
  156. $this->set_style( $field['style'] ); 
  157.  
  158. // Custom attributes. 
  159. if ( isset( $field['attributes'] ) ) { 
  160. $this->set_attributes( $field['attributes'] ); 
  161.  
  162. // Tooltip markup. 
  163. if ( isset( $field['tooltip'] ) ) { 
  164. $this->tooltip = ' <i class="simcal-icon-help simcal-help-tip" data-tip="' . esc_attr( $field['tooltip'] ) . '"></i> ' ; 
  165.  
  166. /** 
  167. * Set custom HTML attributes. 
  168. * @since 3.0.0 
  169. * @param array $attributes 
  170. * @return void 
  171. */ 
  172. public function set_attributes( $attributes ) { 
  173.  
  174. $attr = ''; 
  175.  
  176. if ( ! empty( $attributes ) && is_array( $attributes ) ) { 
  177. foreach ( $attributes as $k => $v ) { 
  178. $attr .= esc_attr( $k ) . '="' . esc_attr( $v ) . '" '; 
  179.  
  180. $this->attributes = $attr; 
  181.  
  182. /** 
  183. * Set CSS styles. 
  184. * @since 3.0.0 
  185. * @param array $css 
  186. * @return void 
  187. */ 
  188. public function set_style( $css ) { 
  189.  
  190. $styles = ''; 
  191.  
  192. if ( ! empty( $css ) && is_array( $css ) ) { 
  193. foreach ( $css as $k => $v ) { 
  194. $styles .= esc_attr( $k ) . ': ' . esc_attr( $v ) . '; '; 
  195.  
  196. $this->style = $styles; 
  197.  
  198. /** 
  199. * Set classes. 
  200. * @since 3.0.0 
  201. * @param array $class 
  202. * @return void 
  203. */ 
  204. public function set_class( $class ) { 
  205.  
  206. $classes = ''; 
  207. $type_class = ''; 
  208. $error = ''; 
  209.  
  210. if ( ! empty( $class ) && is_array( $class ) ) { 
  211. $classes = implode( ' ', array_map( 'esc_attr', $class ) ); 
  212. if ( ! empty( $this->type_class ) ) { 
  213. $type_class = esc_attr( $this->type_class ); 
  214. if ( true !== $this->validation && ! empty( $this->validation ) ) { 
  215. $error = 'simcal-field-error '; 
  216.  
  217. $this->class = trim( $error . 'simcal-field ' . $type_class . ' ' . $classes ); 
  218.  
  219. /** 
  220. * Escape field value. 
  221. * Default escape function, a wrapper for esc_attr(). 
  222. * @since 3.0.0 
  223. * @access protected 
  224. * @param array|string|int $value 
  225. * @return array|string 
  226. */ 
  227. protected function escape( $value ) { 
  228. return ! empty( $value ) ? ( is_array( $value ) ? array_map( 'esc_attr', $value ) : esc_attr( $value ) ) : ''; 
  229.  
  230. /** 
  231. * Escape callback. 
  232. * Custom escaping function set in field args. 
  233. * @since 3.0.0 
  234. * @access protected 
  235. * @param array|string $callback 
  236. * @param mixed $value 
  237. * @return mixed 
  238. */ 
  239. protected function escape_callback( $callback, $value ) { 
  240. if ( $callback && ( is_string( $callback ) || is_array( $callback ) ) ) { 
  241. return call_user_func( $callback, $value ); 
  242. return esc_attr( $value ); 
  243.  
  244. /** 
  245. * Validation callback. 
  246. * Custom field validation callback set in field args. 
  247. * @since 3.0.0 
  248. * @access protected 
  249. * @param array|string $callback 
  250. * @param string $value 
  251. * @return true|string Expected to return bool (true) if passes, message string if not. 
  252. */ 
  253. protected function validate( $callback, $value ) { 
  254. if ( $callback && ( is_string( $callback ) || is_array( $callback ) ) ) { 
  255. $screen = function_exists( 'get_current_screen' ) ? get_current_screen() : ''; 
  256. return call_user_func( $callback, $value, $screen ); 
  257. return true; 
  258.  
  259. /** 
  260. * Outputs the field markup. 
  261. * @since 3.0.0 
  262. * @return void 
  263. */ 
  264. abstract public function html(); 
  265.