/** * wpv_array_date_validate * * Validate each element in an array of values, given a date field type * * @param $value (array) * @param $validate (string) * <year|month|week|day|hour|minute|second|dayofweek|dayofyear> * * @return $value (array) * * @since 1.8.0 */ function wpv_array_date_validate($value, $validate = '') { if (!empty($validate) && is_array($value) && !empty($value)) { switch ($validate) { case 'year': case 'month': case 'week': case 'day': case 'hour': case 'minute': case 'second': case 'dayofweek': case 'dayofyear': foreach ($value as $val_key => $val_candidate) { if (wpv_integer_date_validate($val_candidate, $validate) === false) { $value[$val_key] = null; } } break; } $existing_values = count($value); $value = array_filter($value, 'wpv_is_valid_non_empty_value_to_filter'); $validated_values = count($value); if ($existing_values != $validated_values) { $value = array(); } } return $value; }
/** * wpv_resolve_group_date_query * * Generate the date_query entry for IN, NOT IN, BETWEEN and NOT BETWEEN comparisons * * In case of BETWEEN and NOT BETWEEN, we also need to manage the cases where one of the values is $no_parameter_found * If so, we will transform them into the right greater-or-equal-than or lower-or-equal-than statements. * * @param array $value * @param array $date_condition * * @return (array|boolean) The date_query instance on success, false otherwise * * @since 1.9.0 */ function wpv_resolve_group_date_query( $value, $date_condition ) { global $no_parameter_found; $start_of_week = get_option( 'start_of_week' ); $date_columns = array( 'post_date', 'post_date_gmt', 'post_modified', 'post_modified_gmt' ); $date_condition_operator = $date_condition['date_operator']; $adjust_start_of_week = false; if ( $date_condition['date_multiple_selected'] == 'dayofweek' && $start_of_week == 1 ) { /* * Based on the $start_of_week setting, $indexed_v is 1 ( Monday ) to 7 ( Sunday ) * We must translate it to values that the date_query['dayofweek'] attribute understands * That is, 1 ( Sunday ) to 7 ( Saturday ) */ $adjust_start_of_week = true; } $date_query_instance = array(); if ( isset( $date_condition['date_column'] ) && in_array( $date_condition['date_column'], $date_columns ) ) { $date_query_instance['column'] = $date_condition['date_column']; } if ( in_array( $no_parameter_found, $value ) && ( 'BETWEEN' == $date_condition_operator || 'NOT BETWEEN' == $date_condition_operator ) ) { // Clean from empty values, the ones we care about are $no_parameter_found instead $value = array_filter( $value ); $value_count = count( $value ); if ( 0 == $value_count ) { return false; } else if ( 1 == $value_count ) { if ( $value[0] == $no_parameter_found ) { return false; } else { if ( $date_condition_operator == 'BETWEEN' ) { $date_condition_operator = '>='; } else { $date_condition_operator = '<='; } $single_value = $value[0]; $is_valid_value = wpv_integer_date_validate( $single_value, $date_condition['date_multiple_selected'] ); if ( $is_valid_value !== false ) { if ( $adjust_start_of_week ) { $single_value = ( $single_value % 7 ) + 1; } $date_query_instance[$date_condition['date_multiple_selected']] = $single_value; $date_query_instance['compare'] = $date_condition_operator; return $date_query_instance; } } } else { if ( $value[0] == $no_parameter_found && $value[1] == $no_parameter_found ) { return false; } if ( $value[0] == $no_parameter_found ) { if ( $date_condition_operator == 'BETWEEN' ) { $date_condition_operator = '<='; } else { $date_condition_operator = '>='; } $single_value = $value[1]; $is_valid_value = wpv_integer_date_validate( $single_value, $date_condition['date_multiple_selected'] ); if ( $is_valid_value !== false ) { if ( $adjust_start_of_week ) { $single_value = ( $single_value % 7 ) + 1; } $date_query_instance[$date_condition['date_multiple_selected']] = $single_value; $date_query_instance['compare'] = $date_condition_operator; return $date_query_instance; } } elseif ( $value[1] == $no_parameter_found ) { if ( $date_condition_operator == 'BETWEEN' ) { $date_condition_operator = '>='; } else { $date_condition_operator = '<='; } $single_value = $value[0]; $is_valid_value = wpv_integer_date_validate( $single_value, $date_condition['date_multiple_selected'] ); if ( $is_valid_value !== false ) { if ( $adjust_start_of_week ) { $single_value = ( $single_value % 7 ) + 1; } $date_query_instance[$date_condition['date_multiple_selected']] = $single_value; $date_query_instance['compare'] = $date_condition_operator; return $date_query_instance; } } else { $value = array_filter( $value, 'wpv_is_valid_non_empty_value_to_filter' ); $value = wpv_array_date_validate( $value, $date_condition['date_multiple_selected'] ); if ( ! empty( $value ) ) { $indexed_values = array_values( $value ); if ( $adjust_start_of_week ) { foreach ( $indexed_values as $indexed_k => $indexed_v ) { $indexed_values[$indexed_k] = ( $indexed_v % 7 ) + 1; } } $date_query_instance[$date_condition['date_multiple_selected']] = $indexed_values; $date_query_instance['compare'] = $date_condition_operator; return $date_query_instance; } } } } else { $value = array_filter( $value, 'wpv_is_valid_non_empty_value_to_filter' ); $value = wpv_array_date_validate( $value, $date_condition['date_multiple_selected'] ); if ( ! empty( $value ) ) { $indexed_values = array_values( $value ); if ( $adjust_start_of_week ) { foreach ( $indexed_values as $indexed_k => $indexed_v ) { $indexed_values[$indexed_k] = ( $indexed_v % 7 ) + 1; } } $date_query_instance[$date_condition['date_multiple_selected']] = $indexed_values; $date_query_instance['compare'] = $date_condition_operator; return $date_query_instance; } } return false; }