コード例 #1
0
ファイル: functions.php プロジェクト: nhainam/wordpress4
/**
 * Converts date to time on post saving.
 * 
 * @param array $value Use 'datepicker' to convert to timestamp.
 * @return int timestamp 
 */
function wpcf_fields_date_value_save_filter($value, $field, $field_object)
{
    if (defined('WPTOOLSET_FORMS_VERSION')) {
        if (empty($value) || empty($value['datepicker'])) {
            return false;
        }
        if (is_numeric($value['datepicker'])) {
            $timestamp = $value['datepicker'];
        } else {
            $timestamp = wptoolset_strtotime($value['datepicker']);
        }
        // Append Hour and Minute
        if ($timestamp !== false && isset($value['hour']) && isset($value['minute'])) {
            $timestamp_date = adodb_date('dmY', $timestamp);
            $date = adodb_mktime(intval($value['hour']), intval($value['minute']), 0, substr($timestamp_date, 2, 2), substr($timestamp_date, 0, 2), substr($timestamp_date, 4, 4));
            $timestamp = $date;
            /*
            $date = new DateTime( $value['datepicker'] );
                        try {
                            $date->add( new DateInterval( 'PT' . intval( $value['hour'] ) . 'H' . intval( $value['minute'] ) . 'M' ) );
                        } catch (Exception $e) {
                            return $timestamp;
                        }
                        $timestamp = $date->format( "U" );
            */
        }
        return $timestamp;
    }
    // I understand this below is not executed anymore?
    global $wpcf;
    // Remove additional meta if any
    if (isset($field_object->post->ID)) {
        delete_post_meta($field_object->post->ID, '_wpcf_' . $field_object->cf['id'] . '_hour_and_minute');
    }
    if (empty($value) || empty($value['datepicker'])) {
        return false;
    }
    $value['timestamp'] = wpcf_fields_date_convert_datepicker_to_timestamp($value['datepicker']);
    if (!wpcf_fields_date_timestamp_is_valid($value['timestamp'])) {
        $wpcf->debug->errors['date_save_failed'][] = array('value' => $value, 'field' => $field);
        return false;
    }
    // Append Hour and Minute
    if (isset($value['hour']) && isset($value['minute'])) {
        $value['timestamp'] = wpcf_fields_date_calculate_time($value);
    }
    return $value['timestamp'];
}
コード例 #2
0
 /**
  * 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)
 {
     if (is_numeric($check)) {
         return WPToolset_Field_Date_Scripts::_isTimestampInRange($check);
     }
     // TODO Change to use date strtotime
     $valid = wptoolset_strtotime($check);
     return $valid !== false;
     $cake_date_formats = array('F j, Y' => 'Mdy', 'Y/m/d' => 'ymd', 'm/d/Y' => 'mdy', 'd/m/Y' => 'dmy');
     $format = isset($cake_date_formats[$format]) ? $cake_date_formats[$format] : $format;
     $_this =& WPToolset_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;
 }