/wp-content/plugins/pods/classes/fields/phone.php

https://bitbucket.org/mshmsh5000/wp-demo · PHP · 259 lines · 131 code · 29 blank · 99 comment · 24 complexity · 2267b2174d56e751f1ae66b0b4767d71 MD5 · raw file

  1. <?php
  2. /**
  3. * @package Pods\Fields
  4. */
  5. class PodsField_Phone extends PodsField {
  6. /**
  7. * Field Type Group
  8. *
  9. * @var string
  10. * @since 2.0
  11. */
  12. public static $group = 'Text';
  13. /**
  14. * Field Type Identifier
  15. *
  16. * @var string
  17. * @since 2.0
  18. */
  19. public static $type = 'phone';
  20. /**
  21. * Field Type Label
  22. *
  23. * @var string
  24. * @since 2.0
  25. */
  26. public static $label = 'Phone';
  27. /**
  28. * Field Type Preparation
  29. *
  30. * @var string
  31. * @since 2.0
  32. */
  33. public static $prepare = '%s';
  34. /**
  35. * Do things like register/enqueue scripts and stylesheets
  36. *
  37. * @since 2.0
  38. */
  39. public function __construct () {
  40. }
  41. /**
  42. * Add options and set defaults to
  43. *
  44. * @param array $options
  45. *
  46. * @since 2.0
  47. */
  48. public function options () {
  49. $options = array(
  50. self::$type . '_repeatable' => array(
  51. 'label' => __( 'Repeatable Field', 'pods' ),
  52. 'default' => 0,
  53. 'type' => 'boolean',
  54. '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' ),
  55. 'boolean_yes_label' => '',
  56. 'dependency' => true,
  57. 'developer_mode' => true
  58. ),
  59. self::$type . '_format' => array(
  60. 'label' => __( 'Format', 'pods' ),
  61. 'default' => '999-999-9999 x999',
  62. 'type' => 'pick',
  63. 'data' => array(
  64. __( 'US', 'pods' ) => array(
  65. '999-999-9999 x999' => '123-456-7890 x123',
  66. '(999) 999-9999 x999' => '(123) 456-7890 x123',
  67. '999.999.9999 x999' => '123.456.7890 x123'
  68. ),
  69. __( 'International', 'pods' ) => array(
  70. 'international' => __( 'Any (no validation available)', 'pods' )
  71. )
  72. )
  73. ),
  74. self::$type . '_options' => array(
  75. 'label' => __( 'Phone Options', 'pods' ),
  76. 'group' => array(
  77. self::$type . '_enable_phone_extension' => array(
  78. 'label' => __( 'Enable Phone Extension?', 'pods' ),
  79. 'default' => 1,
  80. 'type' => 'boolean'
  81. )
  82. )
  83. ),
  84. self::$type . '_max_length' => array(
  85. 'label' => __( 'Maximum Length', 'pods' ),
  86. 'default' => 25,
  87. 'type' => 'number',
  88. 'help' => __( 'Set to 0 for no limit', 'pods' )
  89. ),
  90. self::$type . '_html5' => array(
  91. 'label' => __( 'Enable HTML5 Input Field?', 'pods' ),
  92. 'default' => apply_filters( 'pods_form_ui_field_html5', 0, self::$type ),
  93. 'type' => 'boolean'
  94. )/*,
  95. self::$type . '_size' => array(
  96. 'label' => __( 'Field Size', 'pods' ),
  97. 'default' => 'medium',
  98. 'type' => 'pick',
  99. 'data' => array(
  100. 'small' => __( 'Small', 'pods' ),
  101. 'medium' => __( 'Medium', 'pods' ),
  102. 'large' => __( 'Large', 'pods' )
  103. )
  104. )*/
  105. );
  106. return $options;
  107. }
  108. /**
  109. * Define the current field's schema for DB table storage
  110. *
  111. * @param array $options
  112. *
  113. * @return array
  114. * @since 2.0
  115. */
  116. public function schema ( $options = null ) {
  117. $length = (int) pods_var( self::$type . '_max_length', $options, 25, null, true );
  118. $schema = 'VARCHAR(' . $length . ')';
  119. if ( 255 < $length || $length < 1 )
  120. $schema = 'LONGTEXT';
  121. return $schema;
  122. }
  123. /**
  124. * Customize output of the form field
  125. *
  126. * @param string $name
  127. * @param mixed $value
  128. * @param array $options
  129. * @param array $pod
  130. * @param int $id
  131. *
  132. * @since 2.0
  133. */
  134. public function input ( $name, $value = null, $options = null, $pod = null, $id = null ) {
  135. $options = (array) $options;
  136. $form_field_type = PodsForm::$field_type;
  137. if ( is_array( $value ) )
  138. $value = implode( ' ', $value );
  139. pods_view( PODS_DIR . 'ui/fields/phone.php', compact( array_keys( get_defined_vars() ) ) );
  140. }
  141. /**
  142. * Validate a value before it's saved
  143. *
  144. * @param mixed $value
  145. * @param string $name
  146. * @param array $options
  147. * @param array $fields
  148. * @param array $pod
  149. * @param int $id
  150. *
  151. * @since 2.0
  152. */
  153. public function validate ( &$value, $name = null, $options = null, $fields = null, $pod = null, $id = null, $params = null ) {
  154. $errors = array();
  155. $label = strip_tags( pods_var_raw( 'label', $options, ucwords( str_replace( '_', ' ', $name ) ) ) );
  156. $check = $this->pre_save( $value, $id, $name, $options, $fields, $pod, $params );
  157. if ( is_array( $check ) )
  158. $errors = $check;
  159. else {
  160. if ( 0 < strlen( $value ) && strlen( $check ) < 1 ) {
  161. if ( 1 == pods_var( 'required', $options ) )
  162. $errors[] = sprintf( __( 'The %s field is required.', 'pods' ), $label );
  163. else
  164. $errors[] = sprintf( __( 'Invalid phone number provided for the field %s.', 'pods' ), $label );
  165. }
  166. }
  167. if ( !empty( $errors ) )
  168. return $errors;
  169. return true;
  170. }
  171. /**
  172. * Change the value or perform actions after validation but before saving to the DB
  173. *
  174. * @param mixed $value
  175. * @param int $id
  176. * @param string $name
  177. * @param array $options
  178. * @param array $fields
  179. * @param array $pod
  180. * @param object $params
  181. *
  182. * @since 2.0
  183. */
  184. public function pre_save ( $value, $id = null, $name = null, $options = null, $fields = null, $pod = null, $params = null ) {
  185. $options = (array) $options;
  186. if ( 'international' == pods_var( self::$type . '_format', $options ) ) {
  187. // no validation/changes
  188. }
  189. else {
  190. // Clean input
  191. $number = preg_replace( '/([^0-9ext])/', '', $value );
  192. $number = str_replace(
  193. array( '-', '.', 'ext', 'x', 't', 'e', '(', ')' ),
  194. array( '', '', '|', '|', '', '', '', '', ),
  195. $number
  196. );
  197. // Get extension
  198. $extension = explode( '|', $number );
  199. if ( 1 < count( $extension ) ) {
  200. $number = preg_replace( '/([^0-9])/', '', $extension[ 0 ] );
  201. $extension = preg_replace( '/([^0-9])/', '', $extension[ 1 ] );
  202. }
  203. else
  204. $extension = '';
  205. // Build number array
  206. $numbers = str_split( $number, 3 );
  207. if ( isset( $numbers[ 3 ] ) ) {
  208. $numbers[ 2 ] .= $numbers[ 3 ];
  209. $numbers = array( $numbers[ 0 ], $numbers[ 1 ], $numbers[ 2 ] );
  210. }
  211. elseif ( isset( $numbers[ 1 ] ) )
  212. $numbers = array( $numbers[ 0 ], $numbers[ 1 ] );
  213. // Format number
  214. if ( '(999) 999-9999 x999' == pods_var( self::$type . '_format', $options ) ) {
  215. if ( 2 == count( $numbers ) )
  216. $value = implode( '-', $numbers );
  217. else
  218. $value = '(' . $numbers[ 0 ] . ') ' . $numbers[ 1 ] . '-' . $numbers[ 2 ];
  219. }
  220. elseif ( '999.999.9999 x999' == pods_var( self::$type . '_format', $options ) )
  221. $value = implode( '.', $numbers );
  222. else //if ( '999-999-9999 x999' == pods_var( self::$type . '_format', $options ) )
  223. $value = implode( '-', $numbers );
  224. // Add extension
  225. if ( 1 == pods_var( self::$type . '_enable_phone_extension', $options ) && 0 < strlen( $extension ) )
  226. $value .= ' x' . $extension;
  227. }
  228. return $value;
  229. }
  230. }