PageRenderTime 51ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 1ms

/wp-content/themes/digitallight/includes/plugins/meta-box/inc/fields/datetime.php

https://gitlab.com/hector20091/digital_light
PHP | 276 lines | 164 code | 26 blank | 86 comment | 15 complexity | 7ae7a28862642e3394f429507d41371a MD5 | raw file
  1. <?php
  2. /**
  3. * Datetime field class.
  4. */
  5. class RWMB_Datetime_Field extends RWMB_Text_Field
  6. {
  7. /**
  8. * Translate date format from jQuery UI date picker to PHP date()
  9. * It's used to store timestamp value of the field
  10. * Missing: '!' => '', 'oo' => '', '@' => '', "''" => "'"
  11. * @var array
  12. */
  13. protected static $date_formats = array(
  14. 'd' => 'j', 'dd' => 'd', 'oo' => 'z', 'D' => 'D', 'DD' => 'l',
  15. 'm' => 'n', 'mm' => 'm', 'M' => 'M', 'MM' => 'F', 'y' => 'y', 'yy' => 'Y', 'o' => 'z',
  16. );
  17. /**
  18. * Translate time format from jQuery UI time picker to PHP date()
  19. * It's used to store timestamp value of the field
  20. * Missing: 't' => '', T' => '', 'm' => '', 's' => ''
  21. * @var array
  22. */
  23. protected static $time_formats = array(
  24. 'H' => 'G', 'HH' => 'H', 'h' => 'g', 'hh' => 'h',
  25. 'mm' => 'i', 'ss' => 's', 'l' => 'u', 'tt' => 'a', 'TT' => 'A',
  26. );
  27. /**
  28. * Register scripts and styles
  29. */
  30. public static function admin_register_scripts()
  31. {
  32. $url = RWMB_CSS_URL . 'jqueryui';
  33. wp_register_style( 'jquery-ui-core', "{$url}/jquery.ui.core.css", array(), '1.8.17' );
  34. wp_register_style( 'jquery-ui-theme', "{$url}/jquery.ui.theme.css", array(), '1.8.17' );
  35. wp_register_style( 'wp-datepicker', RWMB_CSS_URL . 'datepicker.css', array( 'jquery-ui-core', 'jquery-ui-theme' ), '1.8.17' );
  36. wp_register_style( 'jquery-ui-datepicker', "{$url}/jquery.ui.datepicker.css", array( 'wp-datepicker' ), '1.8.17' );
  37. wp_register_style( 'jquery-ui-slider', "{$url}/jquery.ui.slider.css", array( 'jquery-ui-core', 'jquery-ui-theme' ), '1.8.17' );
  38. wp_register_style( 'jquery-ui-timepicker', "{$url}/jquery-ui-timepicker-addon.min.css", array( 'jquery-ui-datepicker', 'jquery-ui-slider', 'wp-datepicker' ), '1.5.0' );
  39. $url = RWMB_JS_URL . 'jqueryui';
  40. wp_register_script( 'jquery-ui-timepicker', "{$url}/jquery-ui-timepicker-addon.min.js", array( 'jquery-ui-datepicker', 'jquery-ui-slider' ), '1.5.0', true );
  41. wp_register_script( 'jquery-ui-timepicker-i18n', "{$url}/jquery-ui-timepicker-addon-i18n.min.js", array( 'jquery-ui-timepicker' ), '1.5.0', true );
  42. /**
  43. * Localization
  44. * Use 1 minified JS file for timepicker which contains all languages for simplicity (in version < 4.4.2 we use separated JS files).
  45. * The language is set in Javascript
  46. *
  47. * Note: we use full locale (de-DE) and fallback to short locale (de)
  48. */
  49. $locale = str_replace( '_', '-', get_locale() );
  50. $locale_short = substr( $locale, 0, 2 );
  51. $date_paths = array( 'jqueryui/datepicker-i18n/jquery.ui.datepicker-' . $locale . '.js' );
  52. if ( strlen( $locale ) > 2 )
  53. {
  54. // Also check alternate i18n filenames
  55. // (e.g. jquery.ui.datepicker-de.js instead of jquery.ui.datepicker-de-DE.js)
  56. $date_paths[] = 'jqueryui/datepicker-i18n/jquery.ui.datepicker-' . substr( $locale, 0, 2 ) . '.js';
  57. }
  58. $deps = array( 'jquery-ui-timepicker-i18n' );
  59. foreach ( $date_paths as $date_path )
  60. {
  61. if ( file_exists( RWMB_DIR . 'js/' . $date_path ) )
  62. {
  63. wp_register_script( 'jquery-ui-datepicker-i18n', RWMB_JS_URL . $date_path, array( 'jquery-ui-datepicker' ), '1.8.17', true );
  64. $deps[] = 'jquery-ui-datepicker-i18n';
  65. break;
  66. }
  67. }
  68. wp_register_script( 'rwmb-datetime', RWMB_JS_URL . 'datetime.js', $deps, RWMB_VER, true );
  69. wp_register_script( 'rwmb-date', RWMB_JS_URL . 'date.js', $deps, RWMB_VER, true );
  70. wp_register_script( 'rwmb-time', RWMB_JS_URL . 'time.js', array( 'jquery-ui-timepicker-i18n' ), RWMB_VER, true );
  71. /**
  72. * Prevent loading localized string twice.
  73. * @link https://github.com/rilwis/meta-box/issues/850
  74. */
  75. $wp_scripts = wp_scripts();
  76. if ( ! $wp_scripts->get_data( 'rwmb-datetime', 'data' ) )
  77. {
  78. wp_localize_script( 'rwmb-datetime', 'RWMB_Datetimepicker', array(
  79. 'locale' => $locale,
  80. 'localeShort' => $locale_short,
  81. ) );
  82. }
  83. if ( ! $wp_scripts->get_data( 'rwmb-time', 'data' ) )
  84. {
  85. wp_localize_script( 'rwmb-time', 'RWMB_Timepicker', array(
  86. 'locale' => $locale,
  87. 'localeShort' => $locale_short,
  88. ) );
  89. }
  90. }
  91. /**
  92. * Enqueue scripts and styles
  93. */
  94. public static function admin_enqueue_scripts()
  95. {
  96. self::admin_register_scripts();
  97. wp_enqueue_style( 'jquery-ui-timepicker' );
  98. wp_enqueue_script( 'rwmb-datetime' );
  99. }
  100. /**
  101. * Get field HTML
  102. *
  103. * @param mixed $meta
  104. * @param array $field
  105. *
  106. * @return string
  107. */
  108. public static function html( $meta, $field )
  109. {
  110. $output = '';
  111. if ( $field['timestamp'] )
  112. {
  113. $name = $field['field_name'];
  114. $field = wp_parse_args( array( 'field_name' => $name . '[formatted]' ), $field );
  115. $output .= sprintf(
  116. '<input type="hidden" name="%s" class="rwmb-datetime-timestamp" value="%s">',
  117. esc_attr( $name . '[timestamp]' ),
  118. isset( $meta['timestamp'] ) ? intval( $meta['timestamp'] ) : ''
  119. );
  120. $meta = isset( $meta['formatted'] ) ? $meta['formatted'] : '';
  121. }
  122. $output .= parent::html( $meta, $field );
  123. if ( $field['inline'] )
  124. {
  125. $output .= '<div class="rwmb-datetime-inline"></div>';
  126. }
  127. return $output;
  128. }
  129. /**
  130. * Calculates the timestamp from the datetime string and returns it
  131. * if $field['timestamp'] is set or the datetime string if not
  132. *
  133. * @param mixed $new
  134. * @param mixed $old
  135. * @param int $post_id
  136. * @param array $field
  137. *
  138. * @return string|int
  139. */
  140. public static function value( $new, $old, $post_id, $field )
  141. {
  142. if ( ! $field['timestamp'] )
  143. return $new;
  144. if ( $field['clone'] )
  145. {
  146. foreach ( $new as $key => $value )
  147. {
  148. $new[$key] = isset( $value['timestamp'] ) ? $value['timestamp'] : null;
  149. }
  150. return $new;
  151. }
  152. return isset( $new['timestamp'] ) ? $new['timestamp'] : null;
  153. }
  154. /**
  155. * Get meta value
  156. *
  157. * @param int $post_id
  158. * @param bool $saved
  159. * @param array $field
  160. *
  161. * @return mixed
  162. */
  163. public static function meta( $post_id, $saved, $field )
  164. {
  165. $meta = parent::meta( $post_id, $saved, $field );
  166. if ( ! $field['timestamp'] )
  167. {
  168. return $meta;
  169. }
  170. $method = array( RW_Meta_Box::get_class_name( $field ), 'translate_format' );
  171. if ( is_array( $meta ) )
  172. {
  173. foreach ( $meta as $key => $value )
  174. {
  175. $meta[$key] = array(
  176. 'timestamp' => ( $value != "" ) ? $value : null,
  177. 'formatted' => ( $value != "" ) ? date( call_user_func( $method, $field ), intval( $value ) ) : "",
  178. );
  179. }
  180. }
  181. else
  182. {
  183. $meta = array(
  184. 'timestamp' => ( $meta != "" ) ? $meta : null,
  185. 'formatted' => ( $meta != "" ) ? date( call_user_func( $method, $field ), intval( $meta ) ) : "",
  186. );
  187. }
  188. return $meta;
  189. }
  190. /**
  191. * Normalize parameters for field
  192. *
  193. * @param array $field
  194. * @return array
  195. */
  196. public static function normalize( $field )
  197. {
  198. $field = wp_parse_args( $field, array(
  199. 'timestamp' => false,
  200. 'inline' => false,
  201. 'js_options' => array(),
  202. ) );
  203. // Deprecate 'format', but keep it for backward compatible
  204. // Use 'js_options' instead
  205. $field['js_options'] = wp_parse_args( $field['js_options'], array(
  206. 'timeFormat' => 'HH:mm',
  207. 'separator' => ' ',
  208. 'dateFormat' => empty( $field['format'] ) ? 'yy-mm-dd' : $field['format'],
  209. 'showButtonPanel' => true,
  210. ) );
  211. if ( $field['inline'] )
  212. {
  213. $field['js_options'] = wp_parse_args( $field['js_options'], array(
  214. 'altFieldTimeOnly' => false,
  215. ) );
  216. }
  217. $field = RWMB_Text_Field::normalize( $field );
  218. return $field;
  219. }
  220. /**
  221. * Get the attributes for a field
  222. *
  223. * @param array $field
  224. * @param mixed $value
  225. *
  226. * @return array
  227. */
  228. public static function get_attributes( $field, $value = null )
  229. {
  230. $attributes = parent::get_attributes( $field, $value );
  231. $attributes = wp_parse_args( $attributes, array(
  232. 'data-options' => wp_json_encode( $field['js_options'] ),
  233. ) );
  234. return $attributes;
  235. }
  236. /**
  237. * Returns a date() compatible format string from the JavaScript format
  238. *
  239. * @link http://www.php.net/manual/en/function.date.php
  240. * @param array $field
  241. *
  242. * @return string
  243. */
  244. public static function translate_format( $field )
  245. {
  246. return strtr( $field['js_options']['dateFormat'], self::$date_formats )
  247. . $field['js_options']['separator']
  248. . strtr( $field['js_options']['timeFormat'], self::$time_formats );
  249. }
  250. }