示例#1
0
/**
 * Custom Conditinal Statement hook returns timestamp if array.
 * 
 * NOTE that $null is already filtered to use $_POST values
 * at priority 10.
 * 
 * @param type $null
 * @param type $object_id
 * @param type $meta_key
 * @param type $single
 * @return mixed timestamp or $null
 */
function wpcf_fields_date_custom_conditional_statement_filter($null, $object_id, $meta_key, $single)
{
    $post = get_post($object_id);
    if (!empty($post->ID)) {
        $field = wpcf_admin_fields_get_field($single);
        $field->set($post, $meta_key);
        if (isset($field->cf['type']) && $field->cf['type'] == 'date') {
            $res = wpcf_fields_date_value_get_filter($null, $field, 'timestamp', 'skip_hour_and_minute');
            if (is_int($res)) {
                return $res;
            }
        }
    }
    return $null;
}
示例#2
0
/**
 * 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;
}
示例#3
0
/**
 * View function.
 * 
 * @param type $params 
 */
function wpcf_fields_date_view($params)
{
    global $wp_locale;
    $defaults = array('format' => get_option('date_format'), 'style' => '');
    $params = wp_parse_args($params, $defaults);
    $output = '';
    // Make sure value is right
    $__timestamp = wpcf_fields_date_value_get_filter($params['field_value'], $params['field'], 'timestamp');
    if (is_null($__timestamp)) {
        return '';
    } else {
        $params['field_value'] = $__timestamp;
    }
    switch ($params['style']) {
        case 'calendar':
            $output .= wpcf_fields_date_get_calendar($params, true, false);
            break;
        default:
            $field_name = '';
            // Extract the Full month and Short month from the format.
            // We'll replace with the translated months if possible.
            $format = $params['format'];
            //$format = str_replace( 'F', '#111111#', $format );
            //$format = str_replace( 'M', '#222222#', $format );
            // Same for the Days
            //$format = str_replace( 'D', '#333333#', $format );
            //$format = str_replace( 'l', '#444444#', $format );
            $date_out = adodb_date($format, $params['field_value']);
            //$month = adodb_date( 'm', $params['field_value'] );
            //$month_full = $wp_locale->get_month( $month );
            //$date_out = str_replace( '#111111#', $month_full, $date_out );
            //$month_short = $wp_locale->get_month_abbrev( $month_full );
            //$date_out = str_replace( '#222222#', $month_short, $date_out );
            //$day = adodb_date( 'w', $params['field_value'] );
            //$day_full = $wp_locale->get_weekday( $day );
            //$date_out = str_replace( '#444444#', $day_full, $date_out );
            //$day_short = $wp_locale->get_weekday_abbrev( $day_full );
            //$date_out = str_replace( '#333333#', $day_short, $date_out );
            $output .= $date_out;
            break;
    }
    return $output;
}
示例#4
0
/**
 * 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;
}
示例#5
0
/**
 * Set repetitive old value back to time.
 * 
 * @param type $value
 * @param type $field
 * @param type $post
 * @param type $element
 * @return type 
 */
function wpcf_repetitive_date_old_value_filter($value, $field, $post, $element)
{
    if ($field['type'] == 'date') {
        $value = wpcf_fields_date_value_get_filter($value, $field);
        $time = intval($value['timestamp']);
        // TODO Revise this
        //        $time = strtotime($value);
        if ($time) {
            return $time;
        }
    }
    return $value;
}
示例#6
0
/**
 * View function.
 * 
 * @param type $params 
 */
function wpcf_fields_date_view($params)
{
    global $wp_locale;
    // Append hour and minute if necessary
    $meta = wpcf_fields_date_value_get_filter($params['field_value'], $params['field']);
    $params['field_value'] = intval($meta['timestamp']);
    $defaults = array('format' => get_option('date_format'), 'style' => '');
    $params = wp_parse_args($params, $defaults);
    $output = '';
    switch ($params['style']) {
        case 'calendar':
            $output .= wpcf_fields_date_get_calendar($params, true, false);
            break;
        default:
            $field_name = '';
            // Extract the Full month and Short month from the format.
            // We'll replace with the translated months if possible.
            $format = $params['format'];
            $format = str_replace('F', '#111111#', $format);
            $format = str_replace('M', '#222222#', $format);
            // Same for the Days
            $format = str_replace('D', '#333333#', $format);
            $format = str_replace('l', '#444444#', $format);
            $date_out = date($format, intval($params['field_value']));
            $month = date('m', intval($params['field_value']));
            $month_full = $wp_locale->get_month($month);
            $date_out = str_replace('#111111#', $month_full, $date_out);
            $month_short = $wp_locale->get_month_abbrev($month_full);
            $date_out = str_replace('#222222#', $month_short, $date_out);
            $day = date('w', intval($params['field_value']));
            $day_full = $wp_locale->get_weekday($day);
            $date_out = str_replace('#444444#', $day_full, $date_out);
            $day_short = $wp_locale->get_weekday_abbrev($day_full);
            $date_out = str_replace('#333333#', $day_short, $date_out);
            $output = $date_out;
            break;
    }
    return $output;
}