/** * @covers ::gravityview_is_valid_datetime * @since 1.15.2 * @group datetime */ public function test_gravityview_is_valid_datetime() { $falses = array('now', '-1 week', 'gobbily gook', 'first monday of november 2005', 'first day of november 2005', '2001-01-20 12:29:30', '2001-01-40'); foreach ($falses as $false) { $this->assertFalse(gravityview_is_valid_datetime($false), $false); } // YYYY-MM-DD $trues = array('2001-01-20', '2051-11-20'); foreach ($trues as $true) { $this->assertTrue(gravityview_is_valid_datetime($true), $true); } $format_checks = array('01/30/2001', '11/30/2051'); foreach ($format_checks as $format_check) { // Correct format $this->assertTrue(gravityview_is_valid_datetime($format_check, 'm/d/Y'), $format_check); // Wrong format $this->assertFalse(gravityview_is_valid_datetime($format_check, 'm-d-Y'), $format_check); $this->assertFalse(gravityview_is_valid_datetime($format_check, 'Y-m-d'), $format_check); } }
/** * Process the start and end dates for a view - overrides values defined in shortcode (if needed) * * The `start_date` and `end_date` keys need to be in a format processable by GFFormsModel::get_date_range_where(), * which uses \DateTime() format. * * You can set the `start_date` or `end_date` to any value allowed by {@link http://www.php.net//manual/en/function.strtotime.php strtotime()}, * including strings like "now" or "-1 year" or "-3 days". * * @see GFFormsModel::get_date_range_where * * @param array $args View settings * @param array $search_criteria Search being performed, if any * @return array Modified `$search_criteria` array */ public static function process_search_dates($args, $search_criteria = array()) { $return_search_criteria = $search_criteria; foreach (array('start_date', 'end_date') as $key) { // Is the start date or end date set in the view or shortcode? // If so, we want to make sure that the search doesn't go outside the bounds defined. if (!empty($args[$key])) { // Get a timestamp and see if it's a valid date format $date = strtotime($args[$key]); // The date was invalid if (empty($date)) { do_action('gravityview_log_error', __METHOD__ . ' Invalid ' . $key . ' date format: ' . $args[$key]); continue; } // The format that Gravity Forms expects for start_date and day-specific (not hour/second-specific) end_date $datetime_format = 'Y-m-d H:i:s'; $search_is_outside_view_bounds = false; if (!empty($search_criteria[$key])) { $search_date = strtotime($search_criteria[$key]); // The search is for entries before the start date defined by the settings switch ($key) { case 'end_date': /** * If the end date is formatted as 'Y-m-d', it should be formatted without hours and seconds * so that Gravity Forms can convert the day to 23:59:59 the previous day. * * If it's a relative date ("now" or "-1 day"), then it should use the precise date format * * @see GFFormsModel::get_date_range_where */ $datetime_format = gravityview_is_valid_datetime($args[$key]) ? 'Y-m-d' : 'Y-m-d H:i:s'; $search_is_outside_view_bounds = $search_date > $date; break; case 'start_date': $search_is_outside_view_bounds = $search_date < $date; break; } } // If there is no search being performed, or if there is a search being performed that's outside the bounds if (empty($search_criteria[$key]) || $search_is_outside_view_bounds) { // Then we override the search and re-set the start date $return_search_criteria[$key] = date_i18n($datetime_format, $date, true); } } } if (isset($return_search_criteria['start_date']) && isset($return_search_criteria['end_date'])) { // The start date is AFTER the end date. This will result in no results, but let's not force the issue. if (strtotime($return_search_criteria['start_date']) > strtotime($return_search_criteria['end_date'])) { do_action('gravityview_log_error', __METHOD__ . ' Invalid search: the start date is after the end date.', $return_search_criteria); } } return $return_search_criteria; }