PodsField_Website

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

Defined (1)

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

/classes/fields/website.php  
  1. class PodsField_Website 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 = 'website'; 
  16.  
  17. /** 
  18. * Field Type Label 
  19. * @var string 
  20. * @since 2.0 
  21. */ 
  22. public static $label = 'Website'; 
  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. * @param array $options 
  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. self::$type . '_format' => array( 
  55. 'label' => __( 'Format', 'pods' ),  
  56. 'default' => 'normal',  
  57. 'type' => 'pick',  
  58. 'data' => array( 
  59. 'normal' => __( 'http://example.com/', 'pods' ),  
  60. 'no-www' => __( 'http://example.com/ (remove www)', 'pods' ),  
  61. 'force-www' => __( 'http://www.example.com/ (force www if no sub-domain provided)', 'pods' ),  
  62. 'no-http' => __( 'example.com', 'pods' ),  
  63. 'no-http-no-www' => __( 'example.com (force removal of www)', 'pods' ),  
  64. 'no-http-force-www' => __( 'www.example.com (force www if no sub-domain provided)', 'pods' ) 
  65. ),  
  66. self::$type . '_max_length' => array( 
  67. 'label' => __( 'Maximum Length', 'pods' ),  
  68. 'default' => 255,  
  69. 'type' => 'number',  
  70. 'help' => __( 'Set to -1 for no limit', 'pods' ) 
  71. ),  
  72. self::$type . '_html5' => array( 
  73. 'label' => __( 'Enable HTML5 Input Field?', 'pods' ),  
  74. 'default' => apply_filters( 'pods_form_ui_field_html5', 0, self::$type ),  
  75. 'type' => 'boolean' 
  76. )/**,  
  77. self::$type . '_size' => array( 
  78. 'label' => __( 'Field Size', 'pods' ),  
  79. 'default' => 'medium',  
  80. 'type' => 'pick',  
  81. 'data' => array( 
  82. 'small' => __( 'Small', 'pods' ),  
  83. 'medium' => __( 'Medium', 'pods' ),  
  84. 'large' => __( 'Large', 'pods' ) 
  85. )*/ 
  86. ); 
  87. return $options; 
  88.  
  89. /** 
  90. * Define the current field's schema for DB table storage 
  91. * @param array $options 
  92. * @return array 
  93. * @since 2.0 
  94. */ 
  95. public function schema ( $options = null ) { 
  96. $length = (int) pods_var( self::$type . '_max_length', $options, 255 ); 
  97.  
  98. $schema = 'VARCHAR(' . $length . ')'; 
  99.  
  100. if ( 255 < $length || $length < 1 ) 
  101. $schema = 'LONGTEXT'; 
  102.  
  103. return $schema; 
  104.  
  105. /** 
  106. * Customize output of the form field 
  107. * @param string $name 
  108. * @param mixed $value 
  109. * @param array $options 
  110. * @param array $pod 
  111. * @param int $id 
  112. * @since 2.0 
  113. */ 
  114. public function input ( $name, $value = null, $options = null, $pod = null, $id = null ) { 
  115. $options = (array) $options; 
  116. $form_field_type = PodsForm::$field_type; 
  117.  
  118. if ( is_array( $value ) ) 
  119. $value = implode( ' ', $value ); 
  120.  
  121. $field_type = 'website'; 
  122.  
  123. if ( isset( $options[ 'name' ] ) && false === PodsForm::permission( self::$type, $options[ 'name' ], $options, null, $pod, $id ) ) { 
  124. if ( pods_var( 'read_only', $options, false ) ) { 
  125. $options[ 'readonly' ] = true; 
  126.  
  127. $field_type = 'text'; 
  128. else 
  129. return; 
  130. elseif ( !pods_has_permissions( $options ) && pods_var( 'read_only', $options, false ) ) { 
  131. $options[ 'readonly' ] = true; 
  132.  
  133. $field_type = 'text'; 
  134.  
  135. pods_view( PODS_DIR . 'ui/fields/' . $field_type . '.php', compact( array_keys( get_defined_vars() ) ) ); 
  136.  
  137. /** 
  138. * Validate a value before it's saved 
  139. * @param mixed $value 
  140. * @param string $name 
  141. * @param array $options 
  142. * @param array $fields 
  143. * @param array $pod 
  144. * @param int $id 
  145. * @since 2.0 
  146. */ 
  147. public function validate ( $value, $name = null, $options = null, $fields = null, $pod = null, $id = null, $params = null ) { 
  148. $errors = array(); 
  149.  
  150. $label = strip_tags( pods_var_raw( 'label', $options, ucwords( str_replace( '_', ' ', $name ) ) ) ); 
  151.  
  152. $check = $this->pre_save( $value, $id, $name, $options, $fields, $pod, $params ); 
  153.  
  154. if ( is_array( $check ) ) 
  155. $errors = $check; 
  156. else { 
  157. if ( 0 < strlen( $value ) && strlen( $check ) < 1 ) { 
  158. if ( 1 == pods_var( 'required', $options ) ) 
  159. $errors[] = sprintf( __( 'The %s field is required.', 'pods' ), $label ); 
  160. else 
  161. $errors[] = sprintf( __( 'Invalid website provided for the field %s.', 'pods' ), $label ); 
  162.  
  163. if ( !empty( $errors ) ) 
  164. return $errors; 
  165.  
  166. return true; 
  167.  
  168. /** 
  169. * Change the value or perform actions after validation but before saving to the DB 
  170. * @param mixed $value 
  171. * @param int $id 
  172. * @param string $name 
  173. * @param array $options 
  174. * @param array $fields 
  175. * @param array $pod 
  176. * @param object $params 
  177. * @since 2.0 
  178. */ 
  179. public function pre_save ( $value, $id = null, $name = null, $options = null, $fields = null, $pod = null, $params = null ) { 
  180. $options = (array) $options; 
  181.  
  182. if ( is_array( $value ) ) { 
  183. if ( isset( $value[ 'scheme' ] ) ) 
  184. $value = $this->build_url( $value ); 
  185. else 
  186. $value = implode( '', $value ); 
  187.  
  188. if ( false === strpos( $value, '://' ) && 0 !== strpos( $value, '//' ) ) 
  189. $value = 'http://' . $value; 
  190.  
  191. $url = @parse_url( $value ); 
  192.  
  193. if ( empty( $url ) || count( $url ) < 2 ) 
  194. $value = ''; 
  195. else { 
  196. $defaults = array( 
  197. 'scheme' => 'http',  
  198. 'host' => '',  
  199. 'path' => '/',  
  200. 'query' => '',  
  201. 'fragment' => '' 
  202. ); 
  203.  
  204. $url = array_merge( $defaults, $url ); 
  205.  
  206. if ( 'normal' == pods_var( self::$type . '_format', $options ) ) 
  207. $value = $this->build_url( $url ); 
  208. elseif ( 'no-www' == pods_var( self::$type . '_format', $options ) ) { 
  209. if ( 0 === strpos( $url[ 'host' ], 'www.' ) ) 
  210. $url[ 'host' ] = substr( $url[ 'host' ], 4 ); 
  211.  
  212. $value = $this->build_url( $url ); 
  213. elseif ( 'force-www' == pods_var( self::$type . '_format', $options ) ) { 
  214. if ( false !== strpos( $url[ 'host' ], '.' ) && false === strpos( $url[ 'host' ], '.', 1 ) ) 
  215. $url[ 'host' ] = 'www.' . $url[ 'host' ]; 
  216.  
  217. $value = $this->build_url( $url ); 
  218. elseif ( 'no-http' == pods_var( self::$type . '_format', $options ) ) { 
  219. $value = $this->build_url( $url ); 
  220. $value = str_replace( trim( $url[ 'scheme' ] . '://', ':' ), '', $value ); 
  221.  
  222. if ( '/' == $url[ 'path' ] ) 
  223. $value = trim( $value, '/' ); 
  224. elseif ( 'no-http-no-www' == pods_var( self::$type . '_format', $options ) ) { 
  225. if ( 0 === strpos( $url[ 'host' ], 'www.' ) ) 
  226. $url[ 'host' ] = substr( $url[ 'host' ], 4 ); 
  227.  
  228. $value = $this->build_url( $url ); 
  229. $value = str_replace( trim( $url[ 'scheme' ] . '://', ':' ), '', $value ); 
  230.  
  231. if ( '/' == $url[ 'path' ] ) 
  232. $value = trim( $value, '/' ); 
  233. elseif ( 'no-http-force-www' == pods_var( self::$type . '_format', $options ) ) { 
  234. if ( false !== strpos( $url[ 'host' ], '.' ) && false === strpos( $url[ 'host' ], '.', 1 ) ) 
  235. $url[ 'host' ] = 'www.' . $url[ 'host' ]; 
  236.  
  237. $value = $this->build_url( $url ); 
  238. $value = str_replace( trim( $url[ 'scheme' ] . '://', ':' ), '', $value ); 
  239.  
  240. if ( '/' == $url[ 'path' ] ) 
  241. $value = trim( $value, '/' ); 
  242.  
  243. $length = (int) pods_var( self::$type . '_max_length', $options, 255 ); 
  244.  
  245. if ( 0 < $length && $length < pods_mb_strlen( $value ) ) { 
  246. $value = pods_mb_substr( $value, 0, $length ); 
  247.  
  248. return $value; 
  249.  
  250. /** 
  251. * Customize the Pods UI manage table column output 
  252. * @param int $id 
  253. * @param mixed $value 
  254. * @param string $name 
  255. * @param array $options 
  256. * @param array $fields 
  257. * @param array $pod 
  258. * @since 2.0 
  259. */ 
  260. public function ui ( $id, $value, $name = null, $options = null, $fields = null, $pod = null ) { 
  261. if ( 'website' == pods_var( self::$type . '_format_type', $options ) && 0 < strlen( pods_var( self::$type . '_format', $options ) ) ) 
  262. $value = make_clickable( $value ); 
  263.  
  264. return $value; 
  265.  
  266. /** 
  267. * Build an url 
  268. * @param array|string $url 
  269. * @return string 
  270. */ 
  271. public function build_url ( $url ) { 
  272. if ( function_exists( 'http_build_url' ) ) 
  273. return http_build_url( $url ); 
  274.  
  275. $defaults = array( 
  276. 'scheme' => 'http',  
  277. 'host' => '',  
  278. 'path' => '/',  
  279. 'query' => '',  
  280. 'fragment' => '' 
  281. ); 
  282.  
  283. $url = array_merge( $defaults, (array) $url ); 
  284.  
  285. $new_url = trim( $url[ 'scheme' ] . '://', ':' ) . $url[ 'host' ] . '/' . ltrim( $url[ 'path' ], '/' ); 
  286.  
  287. if ( !empty( $url[ 'query' ] ) ) 
  288. $new_url .= '?' . ltrim( $url[ 'query' ], '?' ); 
  289.  
  290. if ( !empty( $url[ 'fragment' ] ) ) 
  291. $new_url .= '#' . ltrim( $url[ 'fragment' ], '#' ); 
  292.  
  293. return $new_url;