/** * Renders inline JS. * TODO this seems DEPRECATED and not used anymore, need to check (although I do not know where) */ function wpcf_fields_date_meta_box_js_inline() { $date_format = wpcf_get_date_format(); $date_format = _wpcf_date_convert_wp_to_js($date_format); $date_format_note = '<span style="margin-left:10px"><i>' . esc_js(sprintf(__('Input format: %s', 'wpcf'), wpcf_get_date_format_text())) . '</i></span>'; $year_range = fields_date_timestamp_neg_supported() ? '1902:2037' : '1970:2037'; ?> <script type="text/javascript"> //<![CDATA[ jQuery(document).ready(function(){ wpcfFieldsDateInit(''); }); function wpcfFieldsDateInit(div) { if (jQuery.isFunction(jQuery.fn.datepicker)) { jQuery(div+' .wpcf-datepicker').each(function(index) { if (!jQuery(this).is(':disabled') && !jQuery(this).hasClass('hasDatepicker')) { jQuery(this).datepicker({ showOn: "button", buttonImage: "<?php echo WPCF_EMBEDDED_RES_RELPATH; ?> /images/calendar.gif", buttonImageOnly: true, buttonText: "<?php _e('Select date', 'wpcf'); ?> ", dateFormat: "<?php echo $date_format; ?> ", altFormat: "<?php echo $date_format; ?> ", changeMonth: true, changeYear: true, yearRange: "<?php echo $year_range; ?> ", onSelect: function(dateText, inst) { jQuery(this).trigger('wpcfDateBlur'); } }); jQuery(this).next().after('<?php echo $date_format_note; ?> '); // Wrap in CSS Scope jQuery("#ui-datepicker-div").each(function(){ if (!jQuery(this).hasClass('wpcf-jquery-ui-wrapped')) { jQuery(this).wrap('<div class="wpcf-jquery-ui" />') .addClass('wpcf-jquery-ui-wrapped'); } }); } }); } } //]]> </script> <?php }
/** * Converts Datepicker to timestamp and checks if valid. * @param type $datepicker * @return boolean */ function wpcf_fields_date_convert_datepicker_to_timestamp($datepicker) { $date_format = wpcf_get_date_format(); if ($date_format == 'd/m/Y') { // strtotime requires a dash or dot separator to determine dd/mm/yyyy format $datepicker = str_replace('/', '-', $datepicker); } $_t = strtotime(strval($datepicker)); if ($_t == false || !wpcf_fields_date_timestamp_is_valid($_t)) { // Failed converting return false; } return $_t; }
/** * Date validation, determines if the string passed is a valid date. * keys that expect full month, day and year will validate leap years * * @param string $check a valid date string * @param mixed $format Use a string or an array of the keys below. Arrays should be passed as array('dmy', 'mdy', etc) * Keys: dmy 27-12-2006 or 27-12-06 separators can be a space, period, dash, forward slash * mdy 12-27-2006 or 12-27-06 separators can be a space, period, dash, forward slash * ymd 2006-12-27 or 06-12-27 separators can be a space, period, dash, forward slash * dMy 27 December 2006 or 27 Dec 2006 * Mdy December 27, 2006 or Dec 27, 2006 comma is optional * My December 2006 or Dec 2006 * my 12/2006 separators can be a space, period, dash, forward slash * @param string $regex If a custom regular expression is used this is the only validation that will occur. * @return boolean Success * @access public */ function date($check, $format = 'ymd', $regex = null) { $date_format = wpcf_get_date_format(); $cake_date_formats = array('F j, Y' => 'Mdy', 'Y/m/d' => 'ymd', 'm/d/Y' => 'mdy', 'd/m/Y' => 'dmy'); $format = $cake_date_formats[$date_format]; $_this =& Wpcf_Cake_Validation::getInstance(); $_this->__reset(); $_this->check = $check; $_this->regex = $regex; if (!is_null($_this->regex)) { return $_this->_check(); } $regex['dmy'] = '%^(?:(?:31(\\/|-|\\.|\\x20)(?:0?[13578]|1[02]))\\1|(?:(?:29|30)(\\/|-|\\.|\\x20)(?:0?[1,3-9]|1[0-2])\\2))(?:(?:1[6-9]|[2-9]\\d)?\\d{2})$|^(?:29(\\/|-|\\.|\\x20)0?2\\3(?:(?:(?:1[6-9]|[2-9]\\d)?(?:0[48]|[2468][048]|[13579][26])|(?:(?:16|[2468][048]|[3579][26])00))))$|^(?:0?[1-9]|1\\d|2[0-8])(\\/|-|\\.|\\x20)(?:(?:0?[1-9])|(?:1[0-2]))\\4(?:(?:1[6-9]|[2-9]\\d)?\\d{2})$%'; $regex['mdy'] = '%^(?:(?:(?:0?[13578]|1[02])(\\/|-|\\.|\\x20)31)\\1|(?:(?:0?[13-9]|1[0-2])(\\/|-|\\.|\\x20)(?:29|30)\\2))(?:(?:1[6-9]|[2-9]\\d)?\\d{2})$|^(?:0?2(\\/|-|\\.|\\x20)29\\3(?:(?:(?:1[6-9]|[2-9]\\d)?(?:0[48]|[2468][048]|[13579][26])|(?:(?:16|[2468][048]|[3579][26])00))))$|^(?:(?:0?[1-9])|(?:1[0-2]))(\\/|-|\\.|\\x20)(?:0?[1-9]|1\\d|2[0-8])\\4(?:(?:1[6-9]|[2-9]\\d)?\\d{2})$%'; $regex['ymd'] = '%^(?:(?:(?:(?:(?:1[6-9]|[2-9]\\d)?(?:0[48]|[2468][048]|[13579][26])|(?:(?:16|[2468][048]|[3579][26])00)))(\\/|-|\\.|\\x20)(?:0?2\\1(?:29)))|(?:(?:(?:1[6-9]|[2-9]\\d)?\\d{2})(\\/|-|\\.|\\x20)(?:(?:(?:0?[13578]|1[02])\\2(?:31))|(?:(?:0?[1,3-9]|1[0-2])\\2(29|30))|(?:(?:0?[1-9])|(?:1[0-2]))\\2(?:0?[1-9]|1\\d|2[0-8]))))$%'; $regex['dMy'] = '/^((31(?!\\ (Feb(ruary)?|Apr(il)?|June?|(Sep(?=\\b|t)t?|Nov)(ember)?)))|((30|29)(?!\\ Feb(ruary)?))|(29(?=\\ Feb(ruary)?\\ (((1[6-9]|[2-9]\\d)(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00)))))|(0?[1-9])|1\\d|2[0-8])\\ (Jan(uary)?|Feb(ruary)?|Ma(r(ch)?|y)|Apr(il)?|Ju((ly?)|(ne?))|Aug(ust)?|Oct(ober)?|(Sep(?=\\b|t)t?|Nov|Dec)(ember)?)\\ ((1[6-9]|[2-9]\\d)\\d{2})$/'; $regex['Mdy'] = '/^(?:(((Jan(uary)?|Ma(r(ch)?|y)|Jul(y)?|Aug(ust)?|Oct(ober)?|Dec(ember)?)\\ 31)|((Jan(uary)?|Ma(r(ch)?|y)|Apr(il)?|Ju((ly?)|(ne?))|Aug(ust)?|Oct(ober)?|(Sept|Nov|Dec)(ember)?)\\ (0?[1-9]|([12]\\d)|30))|(Feb(ruary)?\\ (0?[1-9]|1\\d|2[0-8]|(29(?=,?\\ ((1[6-9]|[2-9]\\d)(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00)))))))\\,?\\ ((1[6-9]|[2-9]\\d)\\d{2}))$/'; $regex['My'] = '%^(Jan(uary)?|Feb(ruary)?|Ma(r(ch)?|y)|Apr(il)?|Ju((ly?)|(ne?))|Aug(ust)?|Oct(ober)?|(Sep(?=\\b|t)t?|Nov|Dec)(ember)?)[ /]((1[6-9]|[2-9]\\d)\\d{2})$%'; $regex['my'] = '%^(((0[123456789]|10|11|12)([- /.])(([1][9][0-9][0-9])|([2][0-9][0-9][0-9]))))$%'; $format = is_array($format) ? array_values($format) : array($format); foreach ($format as $key) { $_this->regex = $regex[$key]; if ($_this->_check() === true) { return true; } } return false; }
/** * Converts date to time on post saving. * * @param type $value * @return type */ function wpcf_fields_date_value_save_filter($value) { if (empty($value)) { return $value; } $date_format = wpcf_get_date_format(); if ($date_format == 'd/m/Y') { // strtotime requires a dash or dot separator to determine dd/mm/yyyy format $value = str_replace('/', '-', $value); } return strtotime(strval($value)); }
/** * Parses date meta. * * Use this as main function. * * @param int $value timestamp * @param type $field Field data * $param string $return Specify to return array or specific element of same array * ( timestamp, datepicker, hour, minute ) * @return mixed array | custom parameter */ function wpcf_fields_date_value_get_filter($value, $field, $return = 'array', $context = 'get', $use_cache = true) { global $wpcf; /* * * Fix for leftover */ if ($context != 'check_leftover') { $value = __wpcf_fields_date_check_leftover($value, $field); } // Check if cached static $cache = array(); $cache_key = md5(serialize($value)); if (isset($cache[$cache_key]) && $use_cache) { // Set return data if necessary if ($return != 'array') { if (isset($cache[$cache_key][strval($return)])) { return $cache[$cache_key][strval($return)]; } } else { return $cache[$cache_key]; } } $value_cloned = $value; $date_format = wpcf_get_date_format(); if (empty($value)) { $value = array('timestamp' => null, 'hour' => 8, 'minute' => 0, 'datepicker' => ''); } else { if (is_array($value)) { /* * Consider this already parsed * but check anyway. */ $value = wpcf_fields_date_value_check($value); } else { $value = array('timestamp' => $value, 'hour' => adodb_date('H', $value), 'minute' => adodb_date('i', $value), 'datepicker' => adodb_date($date_format, $value)); $value = wpcf_fields_date_value_check($value); } } // Debug $wpcf->debug->dates[] = array('original_value' => $value_cloned, 'value' => $value, 'field' => $field, 'context' => $context); // Cache it if ($use_cache) { $cache[$cache_key] = $value; } // Set return data if necessary if ($return != 'array') { $value = isset($value[strval($return)]) ? $value[strval($return)] : null; } return $value; }
/** * Sets data for Hour and Minute. * * @param type $value * @param type $date_format * @return int */ function wpcf_fields_date_set_hour_and_minute($value) { $date_format = wpcf_get_date_format(); $data = array(); if (is_array($value)) { if ($date_format == 'd/m/Y') { // strtotime requires a dash or dot separator to determine dd/mm/yyyy format $value['datepicker'] = str_replace('/', '-', strval($value['datepicker'])); } $data['datepicker'] = strtotime($value['datepicker']); $data['hour'] = isset($value['hour']) ? intval($value['hour']) : 8; $data['minute'] = isset($value['minute']) ? intval($value['minute']) : 0; } else { if ($date_format == 'd/m/Y') { // strtotime requires a dash or dot separator to determine dd/mm/yyyy format $value = str_replace('/', '-', strval($value)); } // Check if date string $_v = strtotime($value); $data['datepicker'] = $_v == false || $_v == -1 ? $value : $_v; $data['hour'] = 8; $data['minute'] = 0; } return $data; }
/** * Renders inline JS. */ function wpcf_fields_date_meta_box_js_inline() { $date_format = wpcf_get_date_format(); $date_format = _wpcf_date_convert_wp_to_js($date_format); $date_format_note = '<span style="margin-left:10px"><i>' . esc_js(sprintf(__('Input format: %s', 'wpcf'), wpcf_get_date_format_text())) . '</i></span>'; ?> <script type="text/javascript"> //<![CDATA[ jQuery(document).ready(function(){ wpcfFieldsDateInit(''); }); function wpcfFieldsDateInit(div) { if (jQuery.isFunction(jQuery.fn.datepicker)) { jQuery(div+' .wpcf-datepicker').each(function(index) { if (!jQuery(this).is(':disabled') && !jQuery(this).hasClass('hasDatepicker')) { jQuery(this).datepicker({ showOn: "button", buttonImage: "<?php echo WPCF_EMBEDDED_RES_RELPATH; ?> /images/calendar.gif", buttonImageOnly: true, buttonText: "<?php _e('Select date', 'wpcf'); ?> ", dateFormat: "<?php echo $date_format; ?> ", altFormat: "<?php echo $date_format; ?> ", onSelect: function(dateText, inst) { jQuery(this).trigger('wpcfDateBlur'); } }); jQuery(this).next().after('<?php echo $date_format_note; ?> '); } }); } } function wpcfFieldsDateEditorCallback(field_id) { var url = "<?php echo admin_url('admin-ajax.php'); ?> ?action=wpcf_ajax&wpcf_action=editor_insert_date&_wpnonce=<?php echo wp_create_nonce('fields_insert'); ?> &field_id="+field_id+"&keepThis=true&TB_iframe=true&width=400&height=400"; tb_show("<?php _e('Insert date', 'wpcf'); ?> ", url); } //]]> </script> <?php }
/** * Parses date meta. * * Use this as main function. * * @uses wpcf_fields_date_calculate_time() * * @param type $value * @param type $field Field data * $param string $return Specify to return array or specific element of same array * ( timestamp, datepicker, hour, minute ) * @return mixed array | custom parameter */ function wpcf_fields_date_value_get_filter($value, $field, $return = 'array', $context = 'get') { global $wpcf; $value_cloned = $value; if (is_array($value)) { /* * See if missing timestamp and datepicker present */ if ((!isset($value['timestamp']) || !is_int($value['timestamp'])) && isset($value['datepicker'])) { $_check = strtotime(strval($value['datepicker'])); if ($_check !== false) { $value['timestamp'] = $_check; } } $value = wp_parse_args($value, array('timestamp' => null, 'hour' => 8, 'minute' => 0, 'datepicker' => '')); } else { if (empty($value)) { return array('timestamp' => null, 'hour' => 8, 'minute' => 0, 'datepicker' => ''); } else { /* * strtotime() returns negative numbers like -49537390513 * * https://icanlocalize.basecamphq.com/projects/7393061-toolset/todo_items/160422568/comments * http://www.php.net/manual/en/function.strtotime.php * * QUOTE * Returns a timestamp on success, FALSE otherwise. * Previous to PHP 5.1.0, this function would return -1 on failure. * * BUT on some hosts it returns negative numbers ( our test sites too ) */ if (!is_numeric($value)) { $_check = strtotime($value); if ($_check !== false && $_check > 1) { $value = $_check; } } $value = array('timestamp' => intval($value), 'hour' => 8, 'minute' => 0, 'datepicker' => ''); } } $value['datepicker'] = trim($value['datepicker']); /* * Since Types 1.2 we require $cf field object */ if ($field instanceof WPCF_Field) { $post = $field->post; } else { // Remove for moment remove_filter('wpcf_fields_type_date_value_get', 'wpcf_fields_date_value_get_filter', 10, 4); // Hide on frontpage where things will go fine because of loop if (is_admin()) { _deprecated_argument('date_obsolete_parameter', '1.2', '<br /><br /><div class="wpcf-error">' . 'Since Types 1.2 $cf field object is required' . '</div><br /><br />'); } /* * Set default objects */ $_field = $field; $field = new WPCF_Field(); $field->context = is_admin() ? 'frontend' : 'group'; $post_id = wpcf_get_post_id($field->context); $post = get_post($post_id); if (empty($post)) { return $value; } $field->set($post, $_field); // Back to filter add_filter('wpcf_fields_type_date_value_get', 'wpcf_fields_date_value_get_filter', 10, 4); } /* * Get hour and minute * We need meta_id here. * * NOT Used for 'save' context. * We already have submitted data in $value */ if (!in_array($context, array('save', 'skip_hour_and_minute'))) { if (!empty($post->ID)) { $_meta_id = isset($_field['__meta_id']) ? $_field['__meta_id'] : $field->meta_object->meta_id; $_hm = get_post_meta($post->ID, '_wpcf_' . $field->cf['id'] . '_hour_and_minute', true); $hm = isset($_hm[$_meta_id]) ? $_hm[$_meta_id] : array(); } else { /* * If $post is not set. * We need to record this */ $wpcf->errors['missing_post'][] = func_get_args(); } /* * Setup hour and minute. */ if (!empty($hm) && is_array($hm) && (isset($hm['hour']) && isset($hm['minute']))) { $value['hour'] = $hm['hour']; $value['minute'] = $hm['minute']; } } // Calculate time IF NOT SET ( otherwise it's same as main meta value ) // Always when using 'get' context on frontend if (!is_admin() && $context == 'get' || (empty($value['timestamp']) || !is_int($value['timestamp']))) { $value['timestamp'] = wpcf_fields_date_calculate_time($value); } /* * Set datepicker to use formatted date IF DO NOT EXISTS * (otherwise it keeps Datepicker string like 'August 9, 2012'. * OR is not time string */ if (!empty($value['timestamp']) && (empty($value['datepicker']) || strtotime(strval($value['datepicker'])) === false)) { $value['datepicker'] = date(wpcf_get_date_format(), intval($value['timestamp'])); } $_return = $value; if ($return != 'array') { if (isset($value[strval($return)])) { $_return = $value[strval($return)]; } } // Debug $wpcf->debug->dates[] = array('original_value' => $value_cloned, 'value' => $value, 'return' => $_return, 'field' => $field->cf, 'context' => $context); return $_return; }