PodsField_Text

The Pods - Custom Content Types and Fields PodsField Text class.

Defined (1)

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

/classes/fields/text.php  
  1. class PodsField_Text extends PodsField { 
  2.  
  3. /** 
  4. * Field Type Group 
  5. * @var string 
  6. * @since 2.0 
  7. */ 
  8. public static $group = 'Text'; 
  9.  
  10. /** 
  11. * Field Type Identifier 
  12. * @var string 
  13. * @since 2.0 
  14. */ 
  15. public static $type = 'text'; 
  16.  
  17. /** 
  18. * Field Type Label 
  19. * @var string 
  20. * @since 2.0 
  21. */ 
  22. public static $label = 'Plain Text'; 
  23.  
  24. /** 
  25. * Field Type Preparation 
  26. * @var string 
  27. * @since 2.0 
  28. */ 
  29. public static $prepare = '%s'; 
  30.  
  31. /** 
  32. * Do things like register/enqueue scripts and stylesheets 
  33. * @since 2.0 
  34. */ 
  35. public function __construct () { 
  36.  
  37.  
  38. /** 
  39. * Add options and set defaults to 
  40. * @return array 
  41. * @since 2.0 
  42. */ 
  43. public function options () { 
  44. $options = array( 
  45. self::$type . '_repeatable' => array( 
  46. 'label' => __( 'Repeatable Field', 'pods' ),  
  47. 'default' => 0,  
  48. 'type' => 'boolean',  
  49. 'help' => __( 'Making a field repeatable will add controls next to the field which allows users to Add/Remove/Reorder additional values. These values are saved in the database as an array, so searching and filtering by them may require further adjustments".', 'pods' ),  
  50. 'boolean_yes_label' => '',  
  51. 'dependency' => true,  
  52. 'developer_mode' => true 
  53. ),  
  54. 'output_options' => array( 
  55. 'label' => __( 'Output Options', 'pods' ),  
  56. 'group' => array( 
  57. self::$type . '_allow_shortcode' => array( 
  58. 'label' => __( 'Allow Shortcodes?', 'pods' ),  
  59. 'default' => 0,  
  60. 'type' => 'boolean',  
  61. 'dependency' => true 
  62. ),  
  63. self::$type . '_allow_html' => array( 
  64. 'label' => __( 'Allow HTML?', 'pods' ),  
  65. 'default' => 0,  
  66. 'type' => 'boolean',  
  67. 'dependency' => true 
  68. ),  
  69. self::$type . '_allowed_html_tags' => array( 
  70. 'label' => __( 'Allowed HTML Tags', 'pods' ),  
  71. 'depends-on' => array( self::$type . '_allow_html' => true ),  
  72. 'default' => 'strong em a ul ol li b i',  
  73. 'type' => 'text' 
  74. ),  
  75. self::$type . '_max_length' => array( 
  76. 'label' => __( 'Maximum Length', 'pods' ),  
  77. 'default' => 255,  
  78. 'type' => 'number',  
  79. 'help' => __( 'Set to -1 for no limit', 'pods' ) 
  80. )/**,  
  81. self::$type . '_size' => array( 
  82. 'label' => __( 'Field Size', 'pods' ),  
  83. 'default' => 'medium',  
  84. 'type' => 'pick',  
  85. 'data' => array( 
  86. 'small' => __( 'Small', 'pods' ),  
  87. 'medium' => __( 'Medium', 'pods' ),  
  88. 'large' => __( 'Large', 'pods' ) 
  89. )*/ 
  90. ); 
  91.  
  92. return $options; 
  93.  
  94. /** 
  95. * Define the current field's schema for DB table storage 
  96. * @param array $options 
  97. * @return array 
  98. * @since 2.0 
  99. */ 
  100. public function schema ( $options = null ) { 
  101. $length = (int) pods_var( self::$type . '_max_length', $options, 255 ); 
  102.  
  103. $schema = 'VARCHAR(' . $length . ')'; 
  104.  
  105. if ( 255 < $length || $length < 1 ) 
  106. $schema = 'LONGTEXT'; 
  107.  
  108. return $schema; 
  109.  
  110. /** 
  111. * Change the way the value of the field is displayed with Pods::get 
  112. * @param mixed $value 
  113. * @param string $name 
  114. * @param array $options 
  115. * @param array $pod 
  116. * @param int $id 
  117. * @return mixed|null|string 
  118. * @since 2.0 
  119. */ 
  120. public function display ( $value = null, $name = null, $options = null, $pod = null, $id = null ) { 
  121. $value = $this->strip_html( $value, $options ); 
  122.  
  123. if ( 1 == pods_var( self::$type . '_allow_shortcode', $options ) ) 
  124. $value = do_shortcode( $value ); 
  125.  
  126. return $value; 
  127.  
  128. /** 
  129. * Customize output of the form field 
  130. * @param string $name 
  131. * @param mixed $value 
  132. * @param array $options 
  133. * @param array $pod 
  134. * @param int $id 
  135. * @since 2.0 
  136. */ 
  137. public function input ( $name, $value = null, $options = null, $pod = null, $id = null ) { 
  138. $options = (array) $options; 
  139. $form_field_type = PodsForm::$field_type; 
  140.  
  141. if ( is_array( $value ) ) 
  142. $value = implode( ' ', $value ); 
  143.  
  144. if ( isset( $options[ 'name' ] ) && false === PodsForm::permission( self::$type, $options[ 'name' ], $options, null, $pod, $id ) ) { 
  145. if ( pods_var( 'read_only', $options, false ) ) 
  146. $options[ 'readonly' ] = true; 
  147. else 
  148. return; 
  149. elseif ( !pods_has_permissions( $options ) && pods_var( 'read_only', $options, false ) ) 
  150. $options[ 'readonly' ] = true; 
  151.  
  152. pods_view( PODS_DIR . 'ui/fields/text.php', compact( array_keys( get_defined_vars() ) ) ); 
  153.  
  154. /** 
  155. * Validate a value before it's saved 
  156. * @param mixed $value 
  157. * @param string $name 
  158. * @param array $options 
  159. * @param array $fields 
  160. * @param array $pod 
  161. * @param int $id 
  162. * @param null $params 
  163. * @return array|bool 
  164. * @since 2.0 
  165. */ 
  166. public function validate ( $value, $name = null, $options = null, $fields = null, $pod = null, $id = null, $params = null ) { 
  167. $errors = array(); 
  168.  
  169. $check = $this->pre_save( $value, $id, $name, $options, $fields, $pod, $params ); 
  170.  
  171. if ( is_array( $check ) ) 
  172. $errors = $check; 
  173. else { 
  174. if ( 0 < strlen( $value ) && strlen( $check ) < 1 ) { 
  175. if ( 1 == pods_var( 'required', $options ) ) 
  176. $errors[] = __( 'This field is required.', 'pods' ); 
  177.  
  178. if ( !empty( $errors ) ) 
  179. return $errors; 
  180.  
  181. return true; 
  182.  
  183. /** 
  184. * Change the value or perform actions after validation but before saving to the DB 
  185. * @param mixed $value 
  186. * @param int $id 
  187. * @param string $name 
  188. * @param array $options 
  189. * @param array $fields 
  190. * @param array $pod 
  191. * @param object $params 
  192. * @return mixed|string 
  193. * @since 2.0 
  194. */ 
  195. public function pre_save ( $value, $id = null, $name = null, $options = null, $fields = null, $pod = null, $params = null ) { 
  196. $value = $this->strip_html( $value, $options ); 
  197.  
  198. $length = (int) pods_var( self::$type . '_max_length', $options, 255 ); 
  199.  
  200. if ( 0 < $length && $length < pods_mb_strlen( $value ) ) { 
  201. $value = pods_mb_substr( $value, 0, $length ); 
  202.  
  203. return $value; 
  204.  
  205. /** 
  206. * Customize the Pods UI manage table column output 
  207. * @param int $id 
  208. * @param mixed $value 
  209. * @param string $name 
  210. * @param array $options 
  211. * @param array $fields 
  212. * @param array $pod 
  213. * @return mixed|string 
  214. * @since 2.0 
  215. */ 
  216. public function ui ( $id, $value, $name = null, $options = null, $fields = null, $pod = null ) { 
  217. $value = $this->strip_html( $value, $options ); 
  218.  
  219. if ( 0 == pods_var( self::$type . '_allow_html', $options, 0, null, true ) ) 
  220. $value = wp_trim_words( $value ); 
  221.  
  222. return $value; 
  223.  
  224. /** 
  225. * Strip HTML based on options 
  226. * @param string $value 
  227. * @param array $options 
  228. * @return string 
  229. */ 
  230. public function strip_html ( $value, $options = null ) { 
  231. if ( is_array( $value ) ) 
  232. $value = @implode( ' ', $value ); 
  233.  
  234. $value = trim( $value ); 
  235.  
  236. if ( empty( $value ) ) 
  237. return $value; 
  238.  
  239. $options = (array) $options; 
  240.  
  241. if ( 1 == pods_var( self::$type . '_allow_html', $options, 0, null, true ) ) { 
  242. $allowed_html_tags = ''; 
  243.  
  244. if ( 0 < strlen( pods_var( self::$type . '_allowed_html_tags', $options ) ) ) { 
  245. $allowed_html_tags = explode( ' ', trim( pods_var( self::$type . '_allowed_html_tags', $options ) ) ); 
  246. $allowed_html_tags = '<' . implode( '><', $allowed_html_tags ) . '>'; 
  247.  
  248. if ( !empty( $allowed_html_tags ) && '<>' != $allowed_html_tags ) 
  249. $value = strip_tags( $value, $allowed_html_tags ); 
  250. else 
  251. $value = strip_tags( $value ); 
  252.  
  253. return $value;