ACFFieldDateTimePicker

The Advanced Custom Fields: Date and Time Picker ACFFieldDateTimePicker class.

Defined (2)

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

/date-time-picker-v4.php  
  1. class ACFFieldDateTimePicker extends acf_field { 
  2. // vars 
  3. var $settings // will hold info such as dir / path 
  4. , $defaults // will hold default field options 
  5. , $domain // holds the language domain 
  6. , $lang; 
  7.  
  8. /** 
  9. * __construct 
  10. * Set name / label needed for actions / filters 
  11. * @since 3.6 
  12. * @date 23/01/13 
  13. */ 
  14. function __construct() { 
  15. // vars 
  16. $this->name = 'date_time_picker'; 
  17. $this->label = __( 'Date and Time Picker' ); 
  18. $this->category = __( 'jQuery', 'acf-field-date-time-picker' ); // Basic, Content, Choice, etc 
  19. $this->defaults = array( 
  20. 'label' => __( 'Choose Time', 'acf-field-date-time-picker' ),  
  21. 'time_format' => 'h:mm tt',  
  22. 'show_date' => 'true',  
  23. 'date_format' => 'm/d/y',  
  24. 'show_week_number' => 'false',  
  25. 'picker' => 'slider',  
  26. 'save_as_timestamp' => 'true',  
  27. 'get_as_timestamp' => 'false',  
  28. ); 
  29.  
  30. // do not delete! 
  31. parent::__construct(); 
  32.  
  33. // settings 
  34. $this->settings = array( 
  35. 'path' => apply_filters( 'acf/helpers/get_path', __FILE__ ), // @codingStandardsIgnoreLine 
  36. 'dir' => apply_filters( 'acf/helpers/get_dir', __FILE__ ), // @codingStandardsIgnoreLine 
  37. 'version' => ACFFIELDDATETIMEPICKER_VERSION,  
  38. ); 
  39.  
  40. /** 
  41. * create_options() 
  42. * Create extra options for your field. This is rendered when editing a field. 
  43. * The value of $field['name'] can be used (like bellow) to save extra data to the $field 
  44. * @type action 
  45. * @since 3.6 
  46. * @date 23/01/13 
  47. * @param $field - an array holding all the field's data 
  48. */ 
  49. function create_options( $field ) { 
  50.  
  51. $field = array_merge( $this->defaults, $field ); 
  52. $key = $field['name']; 
  53. ?> 
  54. <tr class="field_option field_option_<?php echo $this->name; ?> timepicker_choice"> 
  55. <td class="label"> 
  56. <label for=""><?php _e( 'Date and Time Picker?', 'acf-field-date-time-picker' ); ?></label> 
  57. </td> 
  58. <td> 
  59. <?php 
  60. do_action('acf/create_field', array( // @codingStandardsIgnoreLine 
  61. 'type' => 'radio',  
  62. 'name' => 'fields[' . $key . '][show_date]',  
  63. 'value' => $field['show_date'],  
  64. 'layout' => 'horizontal',  
  65. 'choices' => array( 
  66. 'true' => __( 'Date and Time Picker', 'acf-field-date-time-picker' ),  
  67. 'false' => __( 'Time Picker', 'acf-field-date-time-picker' ),  
  68. ),  
  69. )); 
  70. ?> 
  71. </td> 
  72. </tr> 
  73. <tr class="field_option field_option_<?php echo $this->name; ?> timepicker_dateformat"> 
  74. <td class="label"> 
  75. <label><?php _e( 'Date Format', 'acf-field-date-time-picker' ); ?></label> 
  76. <p class="description"><?php printf( __( 'eg. mm/dd/yy. read more about <a href="%s" target="_blank">formatting date</a>', 'acf-field-date-time-picker' ), 'http://docs.jquery.com/UI/Datepicker/formatDate' ); ?></p> 
  77. </td> 
  78. <td> 
  79. <?php 
  80. do_action('acf/create_field', array( // @codingStandardsIgnoreLine 
  81. 'type' => 'text',  
  82. 'name' => 'fields[' . $key . '][date_format]',  
  83. 'value' => $field['date_format'],  
  84. )); 
  85. /** 
  86. do_action('acf/create_field', array( 
  87. 'type' => 'select',  
  88. 'name' => 'fields['.$key.'][date_format]',  
  89. 'value' => $field['date_format'],  
  90. 'choices' => array( 
  91. 'm/d/y' => 'm/d/y (5/27/13)' 
  92. , 'mm/dd/yy' => 'mm/dd/yy (05/27/2013)' 
  93. , 'yy/mm/dd' => 'yy/mm/dd (2013/05/27)' 
  94. , 'yy-mm-dd' => 'yy-mm-dd (2013-05-27)' 
  95. , 'dd.mm.yy' => 'dd.mm.yy (27.05.2013)' 
  96. , 'dd-mm-yy' => 'dd-mm-yy (27-05-2013)' 
  97. , 'yy-M-dd' => 'yy-M-dd (2013-May-27)' 
  98. )); 
  99. */ 
  100. ?> 
  101. </td> 
  102. </tr> 
  103. <tr class="field_option field_option_<?php echo $this->name; ?> timepicker_timeformat"> 
  104. <td class="label"> 
  105. <label><?php _e( 'Time Format', 'acf-field-date-time-picker' ); ?></label> 
  106. <p class="description"><?php printf( __( 'eg. hh:mm. read more about <a href="%s" target="_blank">formatting time</a>', 'acf-field-date-time-picker' ), 'http://trentrichardson.com/examples/timepicker/#tp-formatting' ); ?></p> 
  107. </td> 
  108. <td> 
  109. <?php 
  110. do_action('acf/create_field', array( // @codingStandardsIgnoreLine 
  111. 'type' => 'text',  
  112. 'name' => 'fields[' . $key . '][time_format]',  
  113. 'value' => $field['time_format'],  
  114. )); 
  115. /** 
  116. do_action('acf/create_field', array( 
  117. 'type' => 'select',  
  118. 'name' => 'fields['.$key.'][time_format]',  
  119. 'value' => $field['time_format'],  
  120. 'choices' => array( 
  121. 'h:mm tt' => 'h:mm tt (9:59 am)' 
  122. , 'hh:mm tt' => 'hh:mm tt (09:59 am)' 
  123. , 'H:mm' => 'H:mm (9:59)' 
  124. , 'HH:mm' => 'HH:mm (09:59)' 
  125. )); 
  126. */ 
  127. ?> 
  128. </td> 
  129. </tr> 
  130. <tr class="field_option field_option_<?php echo $this->name; ?> timepicker_week_number"> 
  131. <td class="label"> 
  132. <label for=""><?php _e( 'Display Week Number?', 'acf-field-date-time-picker' ); ?></label> 
  133. </td> 
  134. <td> 
  135. <?php 
  136. do_action('acf/create_field', array( // @codingStandardsIgnoreLine 
  137. 'type' => 'radio',  
  138. 'name' => 'fields[' . $key . '][show_week_number]',  
  139. 'value' => $field['show_week_number'],  
  140. 'layout' => 'horizontal',  
  141. 'choices' => array( 
  142. 'true' => __( 'Yes', 'acf-field-date-time-picker' ),  
  143. 'false' => __( 'No', 'acf-field-date-time-picker' ),  
  144. ),  
  145. )); 
  146. ?> 
  147. </td> 
  148. </tr> 
  149. <tr class="field_option field_option_<?php echo $this->name; ?> timepicker_week_number"> 
  150. <td class="label"> 
  151. <label for=""><?php _e( 'Time Picker style?', 'acf-field-date-time-picker' ); ?></label> 
  152. </td> 
  153. <td> 
  154. <?php 
  155. do_action('acf/create_field', array( // @codingStandardsIgnoreLine 
  156. 'type' => 'radio',  
  157. 'name' => 'fields[' . $key . '][picker]',  
  158. 'value' => $field['picker'],  
  159. 'layout' => 'horizontal',  
  160. 'choices' => array( 
  161. 'slider' => __( 'Slider', 'acf-field-date-time-picker' ),  
  162. 'select' => __( 'Dropdown', 'acf-field-date-time-picker' ),  
  163. ),  
  164. )); 
  165. ?> 
  166. </td> 
  167. </tr> 
  168. <tr class="field_option field_option_<?php echo $this->name; ?> timepicker_week_number"> 
  169. <td class="label"> 
  170. <label for=""><?php _e( 'Save as timestamp?', 'acf-field-date-time-picker' ); ?></label> 
  171. <p class="description"><?php printf( __( 'Most users should leave this untouched, only set it to "No" if you need a date and time format not supported by <a href="%s" target="_blank">strtotime</a>', 'acf-field-date-time-picker' ), 'http://php.net/manual/en/function.strtotime.php' ); ?></p> 
  172. </td> 
  173. <td> 
  174. <?php 
  175. do_action('acf/create_field', array( // @codingStandardsIgnoreLine 
  176. 'type' => 'radio',  
  177. 'name' => 'fields[' . $key . '][save_as_timestamp]',  
  178. 'value' => $field['save_as_timestamp'],  
  179. 'layout' => 'horizontal',  
  180. 'choices' => array( 
  181. 'true' => __( 'Yes', 'acf-field-date-time-picker' ),  
  182. 'false' => __( 'No', 'acf-field-date-time-picker' ),  
  183. ),  
  184. )); 
  185. ?> 
  186. </td> 
  187. </tr> 
  188. <tr class="field_option field_option_<?php echo $this->name; ?> timepicker_week_number"> 
  189. <td class="label"> 
  190. <label for=""><?php _e( 'Get field as timestamp?', 'acf-field-date-time-picker' ); ?></label> 
  191. <p class="description"><?php printf( __( 'Most users should leave this untouched, only set it to "Yes" if you need get the date and time field as a timestamp using <a href="%1$s" target="_blank">the_field()</a> or <a href="%2$s" target="_blank">get_field()</a> ', 'acf-field-date-time-picker' ), 'http://www.advancedcustomfields.com/resources/functions/the_field/',  
  192. 'http://www.advancedcustomfields.com/resources/functions/get_field/'); ?></p> 
  193. </td> 
  194. <td> 
  195. <?php 
  196. do_action('acf/create_field', array( // @codingStandardsIgnoreLine 
  197. 'type' => 'radio',  
  198. 'name' => 'fields[' . $key . '][get_as_timestamp]',  
  199. 'value' => $field['get_as_timestamp'],  
  200. 'layout' => 'horizontal',  
  201. 'choices' => array( 
  202. 'true' => __( 'Yes', 'acf-field-date-time-picker' ),  
  203. 'false' => __( 'No', 'acf-field-date-time-picker' ),  
  204. ),  
  205. )); 
  206. ?> 
  207. </td> 
  208. </tr> 
  209. <?php 
  210.  
  211. /** 
  212. * create_field() 
  213. * Create the HTML interface for your field 
  214. * @param $field - an array holding all the field's data 
  215. * @type action 
  216. * @since 3.6 
  217. * @date 23/01/13 
  218. */ 
  219. function create_field( $field ) { 
  220.  
  221. if ( 'true' !== $field['show_date'] ) { 
  222. echo '<input type="text" value="' . $field['value'] . '" name="' . $field['name'] . '" class="ps_timepicker" value="" data-picker="' . $field['picker'] . '" data-time_format="' . $field['time_format'] . '" title="' . $field['label'] . '" />'; 
  223. } else { 
  224. echo '<input type="text" value="' . $field['value'] . '" name="' . $field['name'] . '" class="ps_timepicker" value="" data-picker="' . $field['picker'] . '" data-date_format="' . $field['date_format'] . '" data-time_format="' . $field['time_format'] . '" data-show_week_number="' . $field['show_week_number'] . '" title="' . $field['label'] . '" />'; 
  225.  
  226. //function load_field_defaults( $field ) { return $field; } 
  227.  
  228. function format_value( $value, $post_id, $field ) { 
  229.  
  230. $field = array_merge( $this->defaults, $field ); 
  231. if ( '' != $value && 'true' == $field['save_as_timestamp'] && $this->isValidTimeStamp( $value ) ) { 
  232. if ( 'true' == $field['show_date'] ) { 
  233. $value = date_i18n( sprintf( '%s %s', $this->js_to_php_dateformat( $field['date_format'] ), $this->js_to_php_timeformat( $field['time_format'] ) ), $value ); 
  234. } else { 
  235. $value = date_i18n( sprintf( '%s', $this->js_to_php_timeformat( $field['time_format'] ) ), $value ); 
  236. return $value; 
  237.  
  238. function format_value_for_api( $value, $post_id, $field ) { 
  239.  
  240. $field = array_merge( $this->defaults, $field ); 
  241. if ( '' != $value && 'true' == $field['save_as_timestamp'] && 'true' != $field['get_as_timestamp'] && $this->isValidTimeStamp( $value ) ) { 
  242. if ( 'true' == $field['show_date'] ) { 
  243. $value = date_i18n( sprintf( '%s %s', $this->js_to_php_dateformat( $field['date_format'] ), $this->js_to_php_timeformat( $field['time_format'] ) ), $value ); 
  244. } else { 
  245. $value = date_i18n( sprintf( '%s', $this->js_to_php_timeformat( $field['time_format'] ) ), $value ); 
  246. return $value; 
  247.  
  248. function js_to_php_dateformat( $date_format ) { 
  249.  
  250. $chars = array( 
  251. // Day 
  252. 'dd' => 'd',  
  253. 'd' => 'j',  
  254. 'DD' => 'l',  
  255. 'D' => 'D',  
  256. 'o' => 'z',  
  257. // Month 
  258. 'mm' => 'm',  
  259. 'm' => 'n',  
  260. 'MM' => 'F',  
  261. 'M' => 'M',  
  262. // Year 
  263. 'yy' => 'Y',  
  264. 'y' => 'y',  
  265. ); 
  266.  
  267. return strtr( (string) $date_format, $chars ); 
  268.  
  269. function js_to_php_timeformat( $time_format ) { 
  270.  
  271. $chars = array( 
  272. //hour 
  273. 'HH' => 'H',  
  274. 'H' => 'G',  
  275. 'hh' => 'h',  
  276. 'h' => 'g',  
  277. //minute 
  278. 'mm' => 'i',  
  279. 'm' => 'i',  
  280. //second 
  281. 'ss' => 's',  
  282. 's' => 's',  
  283. //am/pm 
  284. 'TT' => 'A',  
  285. 'T' => 'A',  
  286. 'tt' => 'a',  
  287. 't' => 'a',  
  288. ); 
  289.  
  290. return strtr( (string) $time_format, $chars ); 
  291.  
  292. function isValidTimeStamp( $timestamp ) { 
  293.  
  294. return ( (string) (int) $timestamp === (string) $timestamp ); 
  295.  
  296. /** 
  297. * update_value() 
  298. * This filter is appied to the $value before it is updated in the db 
  299. * @type filter 
  300. * @since 3.6 
  301. * @date 23/01/13 
  302. * @param $value - the value which will be saved in the database 
  303. * @param $post_id - the $post_id of which the value will be saved 
  304. * @param $field - the field array holding all the field options 
  305. * @return $value - the modified value 
  306. */ 
  307. function update_value( $value, $post_id, $field ) { 
  308.  
  309. $field = array_merge( $this->defaults, $field ); 
  310. if ( '' != $value && 'true' == $field['save_as_timestamp'] ) { 
  311. if ( preg_match( '/^dd?\//', $field['date_format'] ) ) { //if start with dd/ or d/ (not supported by strtotime()) 
  312. $value = str_replace( '/', '-', $value ); 
  313. $value = strtotime( $value ); 
  314.  
  315. return $value; 
  316.  
  317. /** 
  318. * input_admin_enqueue_scripts() 
  319. * This action is called in the admin_enqueue_scripts action on the edit screen where your field is created. 
  320. * Use this action to add css + javascript to assist your create_field() action. 
  321. * $info http://codex.wordpress.org/Plugin_API/Action_Reference/admin_enqueue_scripts 
  322. * @type action 
  323. * @since 3.6 
  324. * @date 23/01/13 
  325. */ 
  326. function input_admin_enqueue_scripts() { 
  327. global $wp_locale; 
  328.  
  329. $has_locale = false; 
  330. $js_locale = $this->get_js_locale( get_locale() ); 
  331.  
  332. wp_enqueue_script('jquery-ui-timepicker', $this->settings['dir'] . 'js/jquery-ui-timepicker-addon.js', array( 
  333. 'acf-input',  
  334. 'jquery-ui-slider',  
  335. ), $this->settings['version'], true); 
  336.  
  337. if ( file_exists( $this->settings['path'] . '/js/localization/jquery-ui-timepicker-' . $js_locale . '.js' ) ) { 
  338. wp_enqueue_script('timepicker-localization', $this->settings['dir'] . 'js/localization/jquery-ui-timepicker-' . $js_locale . '.js', array( 
  339. 'jquery-ui-timepicker' 
  340. ), $this->settings['version'], true); 
  341. wp_enqueue_script('timepicker', $this->settings['dir'] . 'js/timepicker.js', array( 
  342. 'timepicker-localization' 
  343. ), $this->settings['version'], true); 
  344. $has_locale = true; 
  345. } else { 
  346. wp_enqueue_script('timepicker', $this->settings['dir'] . 'js/timepicker.js', array( 
  347. 'jquery-ui-timepicker' 
  348. ), $this->settings['version'], true); 
  349.  
  350. if ( ! $has_locale && 'en' != $js_locale ) { 
  351. $timepicker_locale = array( 
  352. 'closeText' => __( 'Done', 'acf-field-date-time-picker' ),  
  353. 'currentText' => __( 'Today', 'acf-field-date-time-picker' ),  
  354. 'prevText' => __( 'Prev', 'acf-field-date-time-picker' ),  
  355. 'nextText' => __( 'Next', 'acf-field-date-time-picker' ),  
  356. 'monthStatus' => __( 'Show a different month', 'acf-field-date-time-picker' ),  
  357. 'weekHeader' => __( 'Wk', 'acf-field-date-time-picker' ),  
  358. 'timeText' => __( 'Time', 'acf-field-date-time-picker' ),  
  359. 'hourText' => __( 'Hour', 'acf-field-date-time-picker' ),  
  360. 'minuteText' => __( 'Minute', 'acf-field-date-time-picker' ),  
  361. 'secondText' => __( 'Second', 'acf-field-date-time-picker' ),  
  362. 'millisecText' => __( 'Millisecond', 'acf-field-date-time-picker' ),  
  363. 'timezoneText' => __( 'Time Zone', 'acf-field-date-time-picker' ),  
  364. 'isRTL' => $wp_locale->is_rtl(),  
  365. ); 
  366. $timepicker_wp_locale = array( 
  367. 'monthNames' => $this->strip_array_indices( $wp_locale->month ),  
  368. 'monthNamesShort' => $this->strip_array_indices( $wp_locale->month_abbrev ),  
  369. 'dayNames' => $this->strip_array_indices( $wp_locale->weekday ),  
  370. 'dayNamesShort' => $this->strip_array_indices( $wp_locale->weekday_abbrev ),  
  371. 'dayNamesMin' => $this->strip_array_indices( $wp_locale->weekday_initial ),  
  372. 'showMonthAfterYear' => false,  
  373. 'showWeek' => false,  
  374. 'firstDay' => get_option( 'start_of_week' ),  
  375. ); 
  376.  
  377. $l10n = (isset( $timepicker_locale )) ? array_merge( $timepicker_wp_locale, $timepicker_locale ) : $timepicker_wp_locale; 
  378. wp_localize_script( 'timepicker', 'timepicker_objectL10n', $l10n ); 
  379.  
  380. wp_enqueue_style('jquery-style', $this->settings['dir'] . 'css/jquery-ui.css', array( 
  381. 'acf-datepicker' 
  382. ), $this->settings['version']); 
  383. wp_enqueue_style('timepicker', $this->settings['dir'] . 'css/jquery-ui-timepicker-addon.css', array( 
  384. 'jquery-style' 
  385. ), $this->settings['version']); 
  386.  
  387. /** 
  388. * helper function, see: http://www.renegadetechconsulting.com/tutorials/jquery-datepicker-and-wordpress-i18n 
  389. * @param array $ArrayToStrip 
  390. * @return array 
  391. */ 
  392. function strip_array_indices( $array_to_strip ) { 
  393.  
  394. foreach ( $array_to_strip as $obj_array_item ) { 
  395. $new_array[] = $obj_array_item; 
  396.  
  397. return $new_array; 
  398.  
  399. function get_js_locale( $locale ) { 
  400.  
  401. $dir_path = $this->settings['path'] . 'js/localization/'; 
  402. $exclude_list = array( '.', '..' ); 
  403. $languages = $this->ps_preg_filter( '/jquery-ui-timepicker-(.*?)\.js/', '$1', array_diff( scandir( $dir_path ), $exclude_list ) ); 
  404.  
  405. $locale = strtolower( str_replace( '_', '-', $locale ) ); 
  406.  
  407. if ( false !== strpos( $locale, '-' ) ) { 
  408. $l = explode( '-', $locale ); 
  409. switch ( $l[0] ) { 
  410. case 'en': 
  411. case 'fr': 
  412. case 'de': 
  413. $pattern = array( '/' . $l[0] . '/' ); 
  414. break; 
  415.  
  416. default: 
  417. $pattern = array( '/' . $l[1] . '/' ); 
  418. break; 
  419. } else { 
  420. $pattern = array( '/' . $locale . '/' ); 
  421. $res = $this->ps_preg_filter( $pattern, '$0', $languages, -1, $count ); 
  422. return ($count) ? implode( '', $res ) : 'en'; 
  423.  
  424. function ps_preg_filter( $pattern, $replace, $subject, $limit = -1, &$count = 0 ) { 
  425.  
  426. if ( function_exists( 'preg_filter' ) ) { 
  427. return preg_filter( $pattern, $replace, $subject, $limit, $count ); 
  428. } else { 
  429. return array_diff( preg_replace( $pattern, $replace, $subject, $limit, $count ), $subject ); 
/date-time-picker-v3.php  
  1. class ACFFieldDateTimePicker extends acf_Field { 
  2. // vars 
  3. var $settings // will hold info such as dir / path 
  4. , $defaults // will hold default field options 
  5. , $domain; // holds the language domain 
  6.  
  7.  
  8. /**-------------------------------------------------------------------------------------- 
  9. * Constructor 
  10. * - This function is called when the field class is initalized on each page. 
  11. * - Here you can add filters / actions and setup any other functionality for your field 
  12. * @author Elliot Condon 
  13. * @since 2.2.0 
  14. *-------------------------------------------------------------------------------------*/ 
  15.  
  16. function __construct( $parent ) { 
  17. // do not delete! 
  18. parent::__construct( $parent ); 
  19.  
  20. // set name / title 
  21. $this->name = 'date_time_picker'; 
  22. $this->title = __( 'Date and Time Picker' ); 
  23. $this->defaults = array( 
  24. 'label' => __( 'Choose Time', 'acf-field-date-time-picker' ),  
  25. 'time_format' => 'hh:mm',  
  26. 'show_date' => 'true',  
  27. 'date_format' => 'yy-mm-dd',  
  28. 'show_week_number' => 'false',  
  29. 'picker' => 'slider',  
  30. 'save_as_timestamp' => 'true',  
  31. 'get_as_timestamp' => 'false',  
  32. ); 
  33.  
  34. $this->settings = array( 
  35. 'path' => $this->helpers_get_path( __FILE__ ),  
  36. 'dir' => $this->helpers_get_dir( __FILE__ ),  
  37. 'version' => ACFFIELDDATETIMEPICKER_VERSION,  
  38. ); 
  39.  
  40. /** 
  41. * helpers_get_path 
  42. * @description: calculates the path (works for plugin / theme folders) 
  43. * @since: 3.6 
  44. * @created: 30/01/13 
  45. */ 
  46.  
  47. function helpers_get_path( $file ) { 
  48.  
  49. return trailingslashit( dirname( $file ) ); 
  50.  
  51. /** 
  52. * helpers_get_dir 
  53. * @description: calculates the directory (works for plugin / theme folders) 
  54. * @since: 3.6 
  55. * @created: 30/01/13 
  56. */ 
  57.  
  58. function helpers_get_dir( $file ) { 
  59.  
  60. $dir = trailingslashit( dirname( $file ) ); 
  61. $count = 0; 
  62.  
  63. // sanitize for Win32 installs 
  64. $dir = str_replace( '\\', '/', $dir ); 
  65.  
  66. // if file is in plugins folder 
  67. $wp_plugin_dir = str_replace( '\\', '/', WP_PLUGIN_DIR ); 
  68. $dir = str_replace( $wp_plugin_dir, WP_PLUGIN_URL, $dir, $count ); 
  69.  
  70. if ( $count < 1 ) { 
  71. // if file is in wp-content folder 
  72. $wp_content_dir = str_replace( '\\', '/', WP_CONTENT_DIR ); 
  73. $dir = str_replace( $wp_content_dir, WP_CONTENT_URL, $dir, $count ); 
  74.  
  75. if ( $count < 1 ) { 
  76. // if file is in ??? folder 
  77. $wp_dir = str_replace( '\\', '/', ABSPATH ); 
  78. $dir = str_replace( $wp_dir, site_url( '/' ), $dir ); 
  79.  
  80. return $dir; 
  81.  
  82. /**-------------------------------------------------------------------------------------- 
  83. * create_options 
  84. * - this function is called from core/field_meta_box.php to create extra options 
  85. * for your field 
  86. * @params 
  87. * - $key (int) - the $_POST obejct key required to save the options to the field 
  88. * - $field (array) - the field object 
  89. * @author Elliot Condon 
  90. * @since 2.2.0 
  91. *-------------------------------------------------------------------------------------*/ 
  92.  
  93. function create_options( $key, $field ) { 
  94.  
  95. $field = array_merge( $this->defaults, $field ); 
  96. ?> 
  97. <tr class="field_option field_option_<?php echo $this->name; ?> timepicker_choice"> 
  98. <td class="label"> 
  99. <label for=""><?php _e( 'Date and Time Picker?', 'acf-field-date-time-picker' ); ?></label> 
  100. </td> 
  101. <td> 
  102. <?php 
  103. $this->parent->create_field(array( 
  104. 'type' => 'radio',  
  105. 'name' => 'fields[' . $key . '][show_date]',  
  106. 'value' => $field['show_date'],  
  107. 'layout' => 'horizontal',  
  108. 'choices' => array( 
  109. 'true' => __( 'Date and Time Picker', 'acf-field-date-time-picker' ),  
  110. 'false' => __( 'Time Picker', 'acf-field-date-time-picker' ),  
  111. ),  
  112. )); 
  113. ?> 
  114. </td> 
  115. </tr> 
  116. <tr class="field_option field_option_<?php echo $this->name; ?> timepicker_dateformat"> 
  117. <td class="label"> 
  118. <label><?php _e( 'Date Format', 'acf-field-date-time-picker' ); ?></label> 
  119. <p class="description"><?php _e( 'eg. mm/dd/yy. read more about', 'acf-field-date-time-picker' ); ?> <a href="http://docs.jquery.com/UI/Datepicker/formatDate">formatDate</a></p> 
  120. </td> 
  121. <td> 
  122. <?php 
  123. $this->parent->create_field(array( 
  124. 'type' => 'text',  
  125. 'name' => 'fields[' . $key . '][date_format]',  
  126. 'value' => $field['date_format'],  
  127. )); 
  128. ?> 
  129. </td> 
  130. </tr> 
  131. <tr class="field_option field_option_<?php echo $this->name; ?> timepicker_timeformat"> 
  132. <td class="label"> 
  133. <label><?php _e( 'Time Format', 'acf-field-date-time-picker' ); ?></label> 
  134. <p class="description"><?php printf( __( 'eg. hh:mm. read more about <a href="%s" target="_blank">formatting time</a>', 'acf-field-date-time-picker' ), 'http://trentrichardson.com/examples/timepicker/#tp-formatting' ); ?></p> 
  135. </td> 
  136. <td> 
  137. <?php 
  138. $this->parent->create_field(array( 
  139. 'type' => 'text',  
  140. 'name' => 'fields[' . $key . '][time_format]',  
  141. 'value' => $field['time_format'],  
  142. )); 
  143. ?> 
  144. </td> 
  145. </tr> 
  146. <tr class="field_option field_option_<?php echo $this->name; ?> timepicker_week_number"> 
  147. <td class="label"> 
  148. <label for=""><?php _e( 'Display Week Number?', 'acf-field-date-time-picker' ); ?></label> 
  149. </td> 
  150. <td> 
  151. <?php 
  152. $this->parent->create_field(array( 
  153. 'type' => 'radio',  
  154. 'name' => 'fields[' . $key . '][show_week_number]',  
  155. 'value' => $field['show_week_number'],  
  156. 'layout' => 'horizontal',  
  157. 'choices' => array( 
  158. 'true' => __( 'Yes', 'acf-field-date-time-picker' ),  
  159. 'false' => __( 'No', 'acf-field-date-time-picker' ),  
  160. ),  
  161. )); 
  162. ?> 
  163. </td> 
  164. </tr> 
  165. <tr class="field_option field_option_<?php echo $this->name; ?> timepicker_week_number"> 
  166. <td class="label"> 
  167. <label for=""><?php _e( 'Time Picker Style?', 'acf-field-date-time-picker' ); ?></label> 
  168. </td> 
  169. <td> 
  170. <?php 
  171. $this->parent->create_field(array( 
  172. 'type' => 'radio',  
  173. 'name' => 'fields[' . $key . '][picker]',  
  174. 'value' => $field['picker'],  
  175. 'layout' => 'horizontal',  
  176. 'choices' => array( 
  177. 'slider' => __( 'Slider', 'acf-field-date-time-picker' ),  
  178. 'select' => __( 'Dropdown', 'acf-field-date-time-picker' ),  
  179. ),  
  180. )); 
  181. ?> 
  182. </td> 
  183. </tr> 
  184. <tr class="field_option field_option_<?php echo $this->name; ?> timepicker_week_number"> 
  185. <td class="label"> 
  186. <label for=""><?php _e( 'Save as timestamp?', 'acf-field-date-time-picker' ); ?></label> 
  187. <p class="description"><?php printf( __( 'Most users should leave this untouched, only set it to "No" if you need a date and time format not supported by <a href="%s" target="_blank">strtotime</a>', 'acf-field-date-time-picker' ), 'http://php.net/manual/en/function.strtotime.php' ); ?></p> 
  188. </td> 
  189. <td> 
  190. <?php 
  191. $this->parent->create_field(array( 
  192. 'type' => 'radio',  
  193. 'name' => 'fields[' . $key . '][save_as_timestamp]',  
  194. 'value' => $field['save_as_timestamp'],  
  195. 'layout' => 'horizontal',  
  196. 'choices' => array( 
  197. 'true' => __( 'Yes', 'acf-field-date-time-picker' ),  
  198. 'false' => __( 'No', 'acf-field-date-time-picker' ),  
  199. ),  
  200. )); 
  201. ?> 
  202. </td> 
  203. </tr> 
  204. <tr class="field_option field_option_<?php echo $this->name; ?> timepicker_week_number"> 
  205. <td class="label"> 
  206. <label for=""><?php _e( 'Get field as a timestamp?', 'acf-field-date-time-picker' ); ?></label> 
  207. <p class="description"><?php printf( __( 'Most users should leave this untouched, only set it to "Yes" if you need get the date and time field as a timestamp using <a href="%1$s" target="_blank">the_field()</a> or <a href="%2$s" target="_blank">get_field()</a> ', 'acf-field-date-time-picker' ), 'http://www.advancedcustomfields.com/resources/functions/the_field/',  
  208. 'http://www.advancedcustomfields.com/resources/functions/get_field/'); ?></p> 
  209. </td> 
  210. <td> 
  211. <?php 
  212. $this->parent->create_field(array( 
  213. 'type' => 'radio',  
  214. 'name' => 'fields[' . $key . '][get_as_timestamp]',  
  215. 'value' => $field['get_as_timestamp'],  
  216. 'layout' => 'horizontal',  
  217. 'choices' => array( 
  218. 'true' => __( 'Yes', 'acf-field-date-time-picker' ),  
  219. 'false' => __( 'No', 'acf-field-date-time-picker' ),  
  220. ),  
  221. )); 
  222. ?> 
  223. </td> 
  224. </tr> 
  225. <?php 
  226.  
  227.  
  228. /**-------------------------------------------------------------------------------------- 
  229. * create_field 
  230. * - this function is called on edit screens to produce the html for this field 
  231. * @author Elliot Condon 
  232. * @since 2.2.0 
  233. *-------------------------------------------------------------------------------------*/ 
  234.  
  235. function create_field( $field ) { 
  236.  
  237. $field = array_merge( $this->defaults, $field ); 
  238.  
  239. if ( true != $field['show_date'] ) { 
  240. echo '<input type="text" value="' . $field['value'] . '" name="' . $field['name'] . '" class="ps_timepicker" value="" data-picker="' . $field['picker'] . '" data-time_format="' . $field['time_format'] . '" title="' . $field['label'] . '" />'; 
  241. } else { 
  242. echo '<input type="text" value="' . $field['value'] . '" name="' . $field['name'] . '" class="ps_timepicker" value="" data-picker="' . $field['picker'] . '" data-date_format="' . $field['date_format'] . '" data-time_format="' . $field['time_format'] . '" data-show_week_number="' . $field['show_week_number'] . '" title="' . $field['label'] . '" />'; 
  243.  
  244. /**-------------------------------------------------------------------------------------- 
  245. * update_value 
  246. * - this function is called when saving a post object that your field is assigned to. 
  247. * the function will pass through the 3 parameters for you to use. 
  248. * @params 
  249. * - $post_id (int) - usefull if you need to save extra data or manipulate the current 
  250. * post object 
  251. * - $field (array) - usefull if you need to manipulate the $value based on a field option 
  252. * - $value (mixed) - the new value of your field. 
  253. * @author Elliot Condon 
  254. * @since 2.2.0 
  255. *-------------------------------------------------------------------------------------*/ 
  256.  
  257. function update_value( $post_id, $field, $value ) { 
  258.  
  259. $field = array_merge( $this->defaults, $field ); 
  260. if ( '' != $value && 'true' == $field['save_as_timestamp'] ) { 
  261. if ( preg_match( '/^dd?\//', $field['date_format'] ) ) { //if start with dd/ or d/ (not supported by strtotime()) 
  262. $value = str_replace( '/', '-', $value ); 
  263. $value = strtotime( $value ); 
  264.  
  265. parent::update_value( $post_id, $field, $value ); 
  266.  
  267. /**-------------------------------------------------------------------------------------- 
  268. * get_value 
  269. * - called from the edit page to get the value of your field. This function is useful 
  270. * if your field needs to collect extra data for your create_field() function. 
  271. * @params 
  272. * - $post_id (int) - the post ID which your value is attached to 
  273. * - $field (array) - the field object. 
  274. * @author Elliot Condon 
  275. * @since 2.2.0 
  276. *-------------------------------------------------------------------------------------*/ 
  277.  
  278. function get_value( $post_id, $field ) { 
  279.  
  280. $field = array_merge( $this->defaults, $field ); 
  281. $value = parent::get_value( $post_id, $field ); 
  282.  
  283. if ( '' != $value && 'true' == $field['save_as_timestamp'] && $this->isValidTimeStamp( $value ) ) { 
  284. if ( 'true' == $field['show_date'] ) { 
  285. $value = date( sprintf( '%s %s', $this->js_to_php_dateformat( $field['date_format'] ), $this->js_to_php_timeformat( $field['time_format'] ) ), $value ); 
  286. } else { 
  287. $value = date( sprintf( '%s', $this->js_to_php_timeformat( $field['time_format'] ) ), $value ); 
  288.  
  289. return $value; 
  290.  
  291. function get_value_for_api( $post_id, $field ) { 
  292.  
  293. $field = array_merge( $this->defaults, $field ); 
  294. $value = parent::get_value( $post_id, $field ); 
  295.  
  296. if ( '' != $value && 'true' == $field['save_as_timestamp'] && 'true' != $field['get_as_timestamp'] && $this->isValidTimeStamp( $value ) ) { 
  297. if ( 'true' == $field['show_date'] ) { 
  298. $value = date( sprintf( '%s %s', $this->js_to_php_dateformat( $field['date_format'] ), $this->js_to_php_timeformat( $field['time_format'] ) ), $value ); 
  299. } else { 
  300. $value = date( sprintf( '%s', $this->js_to_php_timeformat( $field['time_format'] ) ), $value ); 
  301.  
  302. return $value; 
  303.  
  304. function js_to_php_dateformat( $date_format ) { 
  305.  
  306. $chars = array( 
  307. // Day 
  308. 'dd' => 'd',  
  309. 'd' => 'j',  
  310. 'DD' => 'l',  
  311. 'D' => 'D',  
  312. 'o' => 'z',  
  313. // Month 
  314. 'mm' => 'm',  
  315. 'm' => 'n',  
  316. 'MM' => 'F',  
  317. 'M' => 'M',  
  318. // Year 
  319. 'yy' => 'Y',  
  320. 'y' => 'y',  
  321. ); 
  322.  
  323. return strtr( (string) $date_format, $chars ); 
  324.  
  325. function js_to_php_timeformat( $time_format ) { 
  326.  
  327. $chars = array( 
  328. //hour 
  329. 'HH' => 'H',  
  330. 'H' => 'G',  
  331. 'hh' => 'h',  
  332. 'h' => 'g',  
  333. //minute 
  334. 'mm' => 'i',  
  335. 'm' => 'i',  
  336. //second 
  337. 'ss' => 's',  
  338. 's' => 's',  
  339. //am/pm 
  340. 'TT' => 'A',  
  341. 'T' => 'A',  
  342. 'tt' => 'a',  
  343. 't' => 'a',  
  344. ); 
  345.  
  346. return strtr( (string) $time_format, $chars ); 
  347.  
  348. function isValidTimeStamp( $timestamp ) { 
  349.  
  350. return ( (string) (int) $timestamp === (string) $timestamp ); 
  351.  
  352. /**-------------------------------------------------------------------------------------- 
  353. * admin_print_scripts / admin_print_styles 
  354. * - this function is called in the admin_print_scripts / admin_print_styles where 
  355. * your field is created. Use this function to register css and javascript to assist 
  356. * your create_field() function. 
  357. * @author Elliot Condon 
  358. * @since 3.0.0 
  359. *-------------------------------------------------------------------------------------*/ 
  360.  
  361. function admin_print_scripts() { 
  362.  
  363. global $wp_locale; 
  364.  
  365. $has_locale = false; 
  366. $js_locale = $this->get_js_locale( get_locale() ); 
  367. wp_enqueue_script('jquery-ui-timepicker', $this->settings['dir'] . 'js/jquery-ui-timepicker-addon.js', array( 
  368. 'acf-datepicker',  
  369. 'jquery-ui-slider',  
  370. ), $this->settings['version'], true); 
  371.  
  372. if ( file_exists( dirname( __FILE__ ) . '/js/localization/jquery-ui-timepicker-' . $js_locale . '.js' ) ) { 
  373. wp_enqueue_script('timepicker-localization', $this->settings['dir'] . 'js/localization/jquery-ui-timepicker-' . $js_locale . '.js', array( 
  374. 'jquery-ui-timepicker' 
  375. ), $this->settings['version'], true); 
  376. wp_enqueue_script('timepicker', $this->settings['dir'] . 'js/timepicker.js', array( 
  377. 'timepicker-localization' 
  378. ), $this->settings['version'], true); 
  379. $has_locale = true; 
  380. } else { 
  381. wp_enqueue_script('timepicker', $this->settings['dir'] . 'js/timepicker.js', array( 
  382. 'jquery-ui-timepicker' 
  383. ), $this->settings['version'], true); 
  384.  
  385. if ( ! $has_locale && 'en' != $js_locale ) { 
  386. $timepicker_locale = array( 
  387. 'closeText' => __( 'Done', 'acf-field-date-time-picker' ),  
  388. 'currentText' => __( 'Today', 'acf-field-date-time-picker' ),  
  389. 'prevText' => __( 'Prev', 'acf-field-date-time-picker' ),  
  390. 'nextText' => __( 'Next', 'acf-field-date-time-picker' ),  
  391. 'monthStatus' => __( 'Show a different month', 'acf-field-date-time-picker' ),  
  392. 'weekHeader' => __( 'Wk', 'acf-field-date-time-picker' ),  
  393. 'timeText' => __( 'Time', 'acf-field-date-time-picker' ),  
  394. 'hourText' => __( 'Hour', 'acf-field-date-time-picker' ),  
  395. 'minuteText' => __( 'Minute', 'acf-field-date-time-picker' ),  
  396. 'secondText' => __( 'Second', 'acf-field-date-time-picker' ),  
  397. 'millisecText' => __( 'Millisecond', 'acf-field-date-time-picker' ),  
  398. 'timezoneText' => __( 'Time Zone', 'acf-field-date-time-picker' ),  
  399. 'isRTL' => $wp_locale->is_rtl(),  
  400. ); 
  401. $timepicker_wp_locale = array( 
  402. 'monthNames' => $this->strip_array_indices( $wp_locale->month ),  
  403. 'monthNamesShort' => $this->strip_array_indices( $wp_locale->month_abbrev ),  
  404. 'dayNames' => $this->strip_array_indices( $wp_locale->weekday ),  
  405. 'dayNamesShort' => $this->strip_array_indices( $wp_locale->weekday_abbrev ),  
  406. 'dayNamesMin' => $this->strip_array_indices( $wp_locale->weekday_initial ),  
  407. 'showMonthAfterYear' => false,  
  408. 'showWeek' => false,  
  409. 'firstDay' => get_option( 'start_of_week' ),  
  410. ); 
  411.  
  412. $l10n = (isset( $timepicker_locale )) ? array_merge( $timepicker_wp_locale, $timepicker_locale ) : $timepicker_wp_locale; 
  413. wp_localize_script( 'timepicker', 'timepicker_objectL10n', $l10n ); 
  414.  
  415.  
  416. /** 
  417. * helper function, see: http://www.renegadetechconsulting.com/tutorials/jquery-datepicker-and-wordpress-i18n 
  418. * @param array $ArrayToStrip 
  419. * @return array 
  420. */ 
  421. function strip_array_indices( $array_to_strip ) { 
  422.  
  423. foreach ( $array_to_strip as $obj_array_item ) { 
  424. $new_array[] = $obj_array_item; 
  425.  
  426. return $new_array; 
  427.  
  428. function get_js_locale( $locale ) { 
  429.  
  430. $dir_path = $this->settings['path'] . 'js/localization/'; 
  431. $exclude_list = array( '.', '..' ); 
  432. $languages = $this->ps_preg_filter( '/jquery-ui-timepicker-(.*?)\.js/', '$1', array_diff( scandir( $dir_path ), $exclude_list ) ); 
  433.  
  434. $locale = strtolower( str_replace( '_', '-', $locale ) ); 
  435.  
  436. if ( false !== strpos( $locale, '-' ) ) { 
  437. $l = explode( '-', $locale ); 
  438. switch ( $l[0] ) { 
  439. case 'en': 
  440. case 'fr': 
  441. case 'de': 
  442. $pattern = array( '/' . $l[0] . '/' ); 
  443. break; 
  444.  
  445. default: 
  446. $pattern = array( '/' . $l[1] . '/' ); 
  447. break; 
  448. } else { 
  449. $pattern = array( '/' . $locale . '/' ); 
  450. $res = $this->ps_preg_filter( $pattern, '$0', $languages, -1, $count ); 
  451.  
  452. return ($count) ? implode( '', $res ) : 'en'; 
  453.  
  454. function ps_preg_filter( $pattern, $replace, $subject, $limit = -1, &$count = 0 ) { 
  455.  
  456. if ( function_exists( 'preg_filter' ) ) { 
  457. return preg_filter( $pattern, $replace, $subject, $limit, $count ); 
  458. } else { 
  459. return array_diff( preg_replace( $pattern, $replace, $subject, $limit, $count ), $subject ); 
  460.  
  461. function admin_print_styles() { 
  462.  
  463. wp_enqueue_style( 'jquery-style', $this->settings['dir'] . 'css/jquery-ui.css' ); 
  464. wp_enqueue_style('timepicker', $this->settings['dir'] . 'css/jquery-ui-timepicker-addon.css', array( 
  465. 'jquery-style' 
  466. ), $this->settings['version']);