/** * Fix due to a bug saving date as array. * * BUGS * 'timestamp' is saved without Hour and Minute appended. * * @param type $value * @param type $field */ function __wpcf_fields_date_check_leftover($value, $field, $use_cache = true) { if (empty($value)) { return $value; } if (!is_object($field)) { $post_id = wpcf_get_post_id(); $field_id = isset($field['id']) ? $field['id'] : false; $meta_id = isset($field['__meta_id']) ? $field['__meta_id'] : false; } else { $post_id = isset($field->meta_object->post_id) ? $field->meta_object->post_id : false; $field_id = isset($field->cf['id']) ? $field->cf['id'] : false; $meta_id = isset($field->meta_object->meta_id) ? $field->meta_object->meta_id : false; } if (empty($post_id) || empty($meta_id) || empty($field_id)) { return $value; } $field_slug = wpcf_types_get_meta_prefix() . $field_id; // Check if cached static $cache = array(); $cache_key = $meta_id; if (isset($cache[$cache_key]) && $use_cache) { return $cache[$cache_key]; } $_meta = wpcf_get_post_meta($post_id, '_wpcf_' . $field_id . '_hour_and_minute', true); /* * If meta exists - it's outdated value * and Hour and Minute should be appended and removed. */ if (!empty($_meta) && is_array($_meta) && isset($_meta[$meta_id])) { $meta = $_meta[$meta_id]; // Return empty date if can not be calculated if (!empty($meta['timestamp']) || !empty($meta['datepicker'])) { $meta['timestamp'] = wpcf_fields_date_value_get_filter($meta, $field, 'timestamp', 'check_leftover'); // Check if calculation needed if (isset($meta['hour']) && $meta['hour'] != adodb_date('H', $meta['timestamp']) || isset($meta['minute']) && $meta['minute'] != adodb_date('i', $meta['timestamp'])) { $value = wpcf_fields_date_calculate_time($meta); } } } // Cache it if ($use_cache) { $cache[$cache_key] = $value; } return $value; }
/** * Parses date meta. * * @param type $value * @param type $field Field data * @return type */ function wpcf_fields_date_value_get_filter($value, $field) { global $wpcf; if (is_array($value)) { /* * Since Types 1.2 */ $value = wp_parse_args($value, array('datepicker' => null, 'hour' => 8, 'minute' => 0)); } else { /* * We assume it is timestamp from earlier versions */ $value = array('datepicker' => $value, 'hour' => 8, 'minute' => 0); } /* * 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 * CHECKPOINT * * We need meta_id here. */ 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 $value['timestamp'] = wpcf_fields_date_calculate_time($value); // Set datepicker to use formatted date if (!empty($value['datepicker'])) { // Test if already formatted if (strtotime($value['datepicker']) === false) { $value['datepicker'] = date(wpcf_get_date_format(), intval($value['datepicker'])); } } return $value; }
/** * Fix due to a bug saving date as array. * * BUGS * 'timestamp' is saved without Hour and Minute appended. * * @param type $value * @param type $field */ function __wpcf_fields_date_check_leftover($value, $field, $use_cache = true) { if (empty($value)) { return $value; } if (!is_object($field)) { $post_id = wpcf_get_post_id(); $field_id = isset($field['id']) ? $field['id'] : false; $meta_id = isset($field['__meta_id']) ? $field['__meta_id'] : false; } else { $post_id = isset($field->meta_object->post_id) ? $field->meta_object->post_id : false; $field_id = isset($field->cf['id']) ? $field->cf['id'] : false; $meta_id = isset($field->meta_object->meta_id) ? $field->meta_object->meta_id : false; } if (empty($post_id) || empty($meta_id) || empty($field_id)) { return $value; } $field_slug = wpcf_types_get_meta_prefix() . $field_id; // Check if cached static $cache = array(); $cache_key = $meta_id; if (isset($cache[$cache_key]) && $use_cache) { return $cache[$cache_key]; } $_meta = wpcf_get_post_meta($post_id, '_wpcf_' . $field_id . '_hour_and_minute', true); /* * If meta exists - it's outdated value * and Hour and Minute should be appended and removed. */ if (!empty($_meta) && is_array($_meta) && isset($_meta[$meta_id])) { $meta = $_meta[$meta_id]; // Return empty date if can not be calculated if (!empty($meta['timestamp']) || !empty($meta['datepicker'])) { $meta['timestamp'] = wpcf_fields_date_value_get_filter($meta, $field, 'timestamp', 'check_leftover'); // Check if calculation needed if (isset($meta['hour']) && $meta['hour'] != adodb_date('H', $meta['timestamp']) || isset($meta['minute']) && $meta['minute'] != adodb_date('i', $meta['timestamp'])) { $value = wpcf_fields_date_calculate_time($meta); /* * * If enabling clearing old values here, * pay attention if field is repetitive. * * For now - old data is cleared on date save. * wpcf_fields_date_value_save_filter() */ // Update meta // $success = update_post_meta( $post_id, $field_slug, $value ); // Remove additional meta // if ( $success ) { // delete_post_meta( $post_id, // '_wpcf_' . $field_id . '_hour_and_minute' ); // } } } } // Cache it if ($use_cache) { $cache[$cache_key] = $value; } return $value; }
/** * 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; }