PluginProbe ʕ •ᴥ•ʔ
Pods – Custom Content Types and Fields / 3.3.9.2
Pods – Custom Content Types and Fields v3.3.9.2
2.7.31.4 2.8.23.5 2.9.19.5 3.0.10.5 3.1.4.3 3.2.8.4 3.3.9.2 2.8.23.4 2.9.19.4 3.0.10.4 3.1.4.2 3.2.8.3 3.3.9.1 trunk 1.14.8 2.7.31.3 2.8.23.3 2.9.19.3 3.0.10.3 3.1.4.1 3.2.0 3.2.1 3.2.1.1 3.2.2 3.2.4 3.2.5 3.2.6 3.2.7 3.2.7.1 3.2.8 3.2.8.1 3.2.8.2 3.3.0 3.3.1 3.3.2 3.3.3 3.3.4 3.3.5 3.3.6 3.3.7 3.3.8 3.3.9
pods / ui / fields / datetime.php
pods / ui / fields Last commit date
_comment.php 2 days ago _db.php 2 days ago _hidden.php 2 days ago _label.php 2 days ago _row.php 2 days ago attachment.php 2 days ago checkbox.php 2 days ago cleditor.php 2 days ago codemirror.php 2 days ago color.php 2 days ago currency.php 2 days ago date.php 2 days ago datetime.php 2 days ago email.php 2 days ago link.php 2 days ago number.php 2 days ago oembed.php 2 days ago password.php 2 days ago phone.php 2 days ago radio.php 2 days ago select.php 2 days ago slider.php 2 days ago slug.php 2 days ago text.php 2 days ago textarea.php 2 days ago time.php 2 days ago tinymce.php 2 days ago website.php 2 days ago
datetime.php
212 lines
1 <?php
2
3 // Don't load directly.
4 if ( ! defined( 'ABSPATH' ) ) {
5 die( '-1' );
6 }
7
8 // phpcs:ignoreFile WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedVariableFound
9
10 /**
11 * @var string $form_field_type
12 * @var array $options
13 * @var $value
14 */
15
16 $use_time = ( 'time' === $form_field_type || 'datetime' === $form_field_type );
17 $use_date = ( 'date' === $form_field_type || 'datetime' === $form_field_type );
18
19 wp_enqueue_script( 'jquery-ui-datepicker' );
20 pods_form_enqueue_style( 'pods-styles' );
21
22 if ( $use_time ) {
23 wp_enqueue_script( 'jquery-ui-timepicker' );
24 wp_enqueue_style( 'jquery-ui-timepicker' );
25 }
26
27 PodsForm::field_method( $form_field_type, 'enqueue_jquery_ui_i18n' );
28
29 $attributes = array();
30
31 $html5 = false;
32 $type = 'text';
33
34 if ( pods_v( $form_field_type . '_html5', $options, false ) ) {
35 $html5 = true;
36 $type = $form_field_type;
37 }
38
39 $attributes['type'] = $type;
40 $attributes['tabindex'] = 2;
41
42 $format = PodsForm::field_method( $form_field_type, 'format_' . $form_field_type, $options );
43
44 $method = $form_field_type . 'picker';
45
46 $mysql_date_format = 'Y-m-d';
47 $mysql_time_format = 'H:i:s';
48
49 $args = array(
50 'altField' => '', // Done with JS.
51 'altFieldTimeOnly' => false,
52 );
53
54 if ( $use_date ) {
55 $args['dateFormat'] = PodsForm::field_method( $form_field_type, 'format_date', $options, true );
56 $args['altFormat'] = PodsForm::field_method( $form_field_type, 'convert_format', $mysql_date_format, array( 'type' => 'date' ) );
57 $args['changeMonth'] = true;
58 $args['changeYear'] = true;
59 $args['firstDay'] = (int) get_option( 'start_of_week', 0 );
60
61 $year_range = pods_v( $form_field_type . '_year_range_custom', $options, '' );
62 if ( $year_range ) {
63 $args['yearRange'] = $year_range;
64 }
65 }
66 if ( $use_time ) {
67 $args['timeFormat'] = PodsForm::field_method( $form_field_type, 'format_time', $options, true );
68 $args['altTimeFormat'] = PodsForm::field_method( $form_field_type, 'convert_format', $mysql_time_format, array( 'type' => 'time' ) );
69 $args['ampm'] = ( false !== stripos( $args['timeFormat'], 'tt' ) );
70 $args['parse'] = 'loose';
71 }
72
73 $mysql_format = '';
74
75 switch ( $form_field_type ) {
76 case 'datetime':
77 $mysql_format = $mysql_date_format . ' ' . $mysql_time_format;
78
79 $format_value = pods_v( $form_field_type . '_format', $options, 'mdy', true );
80
81 if ( 'format' === pods_v( $form_field_type . '_type', $options, 'format', true ) && 'c' === $format_value ) {
82 $args['ampm'] = false;
83 $args['separator'] = 'T';
84 $args['timeFormat'] = 'HH:mm:ssz';
85
86 // $args[ 'showTimezone' ] = true;
87 $timezone = (int) get_option( 'gmt_offset' );
88 $timezone *= 60;
89
90 if ( 0 <= $timezone ) {
91 $timezone = '+' . (string) $timezone;
92 }
93
94 $args['timezone'] = (string) $timezone;
95 }
96
97 break;
98 case 'date':
99 $mysql_format = $mysql_date_format;
100 break;
101 case 'time':
102 $mysql_format = $mysql_time_format;
103 break;
104 }
105
106 $date = PodsForm::field_method( $form_field_type, 'createFromFormat', $format, (string) $value );
107 $date_default = PodsForm::field_method( $form_field_type, 'createFromFormat', $mysql_format, (string) $value );
108
109 $formatted_value = PodsForm::field_method( $form_field_type, 'format_value_display', $value, $options, true );
110 $mysql_value = $value;
111
112 if (
113 pods_v( $form_field_type . '_allow_empty', $options, true )
114 && PodsForm::field_method( $form_field_type, 'is_empty', $value )
115 ) {
116 $formatted_value = '';
117 $value = '';
118 } else {
119
120 if ( false !== $date ) {
121 $mysql_value = $date->format( $mysql_format );
122 } elseif ( false !== $date_default ) {
123 $mysql_value = $date_default->format( $mysql_format );
124 } elseif ( ! empty( $value ) ) {
125 $mysql_value = date_i18n( $mysql_format, strtotime( (string) $value ) );
126 } else {
127 $mysql_value = date_i18n( $mysql_format );
128 }
129
130 if ( $html5 ) {
131 /**
132 * HTML5 uses mysql date format separated with a T.
133 * @link https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input/datetime-local
134 */
135 $value = str_replace( ' ', 'T', $mysql_value );
136 } else {
137 $value = $formatted_value;
138 }
139 }
140
141 $args = apply_filters( 'pods_form_ui_field_' . $form_field_type . '_args', $args, $type, $options, $attributes, $name, $form_field_type );
142
143 $attributes['value'] = $value;
144
145 if ( $html5 && 'datetime' === $type ) {
146 // Fix deprecated `datetime` input type.
147 $type = 'datetime-local';
148 $attributes['type'] = 'datetime-local';
149 }
150
151 $attributes = PodsForm::merge_attributes( $attributes, $name, $form_field_type, $options );
152 ?>
153 <input<?php PodsForm::attributes( $attributes, $name, $form_field_type, $options ); ?> />
154
155 <script>
156 jQuery( function ( $ ) {
157 var $container = $( '<div>' ).appendTo( 'body' ).addClass( 'pods-compat-container' ),
158 $element = $( 'input#<?php echo esc_js( $attributes['id'] ); ?>' ),
159 $alt = null,
160 args = <?php echo wp_json_encode( $args ); ?>;
161
162 <?php
163 if ( 'text' !== $type ) {
164 ?>
165 // Test whether or not the browser supports date inputs
166 function podsCheckHtml5 () {
167 var input = document.createElement('input');
168 input.setAttribute( 'type', '<?php echo esc_js( $type ); ?>' );
169
170 var notADateValue = 'not-a-date';
171 input.setAttribute( 'value', notADateValue );
172
173 return ( input.value !== notADateValue );
174 }
175
176 if ( ! podsCheckHtml5() ) {
177 $element.val( '<?php echo esc_js( $formatted_value ); ?>' );
178 jQueryField();
179 }
180 <?php
181 } else {
182 ?>
183 jQueryField();
184 <?php
185 } //end if
186 ?>
187 function jQueryField() {
188
189 // Create alt field.
190 $alt = $element.clone();
191 $alt.attr( 'type', 'hidden' );
192 $alt.val( '<?php echo esc_attr( $mysql_value ) ?>' );
193 $element.after( $alt );
194 $element.attr( 'name', $element.attr( 'name' ) + '__ui' );
195 $element.attr( 'id', $element.attr( 'id' ) + '__ui' );
196
197 // Add alt field option.
198 args.altField = 'input#' + $alt.attr( 'id' );
199 // Fix manual user input changes.
200 args.onClose = function() {
201 $element.<?php echo esc_js( $method ); ?>( 'setDate', $element.val() );
202 };
203 // Wrapper.
204 args.beforeShow = function( textbox, instance ) {
205 $( '#ui-datepicker-div' ).appendTo( $container );
206 };
207
208 $element.<?php echo esc_js( $method ); ?>( args );
209 }
210 } );
211 </script>
212