$this->parse_schedule_timestamp( $ad->get_meta( 'start_date' ), $tz ), 'end_ts' => $this->parse_schedule_timestamp( $ad->get_meta( 'end_date' ), $tz ), 'days' => $is_premium ? Util::get_checked_keys( $ad->get_meta( 'schedule_days', [] ) ) : [], 'time_mode' => $is_premium ? $ad->get_meta( 'schedule_time_mode', 'all_day' ) : 'all_day', 'time_start' => $is_premium ? $ad->get_meta( 'schedule_time_start', '' ) : '', 'time_end' => $is_premium ? $ad->get_meta( 'schedule_time_end', '' ) : '', // PHP's DateTime interprets a naive "Y-m-d H:i:s" string as being in $tz // the frontend needs to know which timezone is meant 'timezone' => wp_timezone_string(), ]; } /** * Parses a stored schedule date string ("Y-m-d H:i:s" or similar) into a Unix timestamp. * * @param mixed $date_string * @param DateTimeZone $tz * * @return int|null */ private function parse_schedule_timestamp( mixed $date_string, DateTimeZone $tz ): ?int { if ( empty( $date_string ) || ! is_string( $date_string ) ) { return null; } try { return ( new DateTime( $date_string, $tz ) )->getTimestamp(); } catch ( Exception ) { return null; } } /** * Checks whether an ad may be displayed NOW based on its schedules. * * @param Ad $ad The ad to check. * * @return bool True, if the ad may be displayed now. */ public function is_ad_active( Ad $ad ): bool { if ( ! $this->is_scheduling_enabled( $ad ) ) { return true; } try { // Get the WP timezone of the user. $tz = wp_timezone(); $now = new DateTime( 'now', $tz ); // date check $start_date = $ad->get_meta( 'start_date' ); // expects Y-m-d H:i:s or Timestamp $end_date = $ad->get_meta( 'end_date' ); if ( ! empty( $start_date ) ) { $start = new DateTime( $start_date, $tz ); if ( $now < $start ) { return false; } // not started yet. } if ( ! empty( $end_date ) ) { $end = new DateTime( $end_date, $tz ); if ( $now > $end ) { return false; } // already ended. } } catch ( Exception ) { return false; } // Weekday and hour-of-day checks are Pro-only if ( ! Util::is_pro_active() ) { return true; } // Weekday check. $allowed_days = Util::get_checked_keys( $ad->get_meta( 'schedule_days', [] ) ); // Wenn not set, we assume that all days are allowed. if ( ! empty( $allowed_days ) && is_array( $allowed_days ) ) { // Map the day format to our internal format. $day_map = [ 1 => 'mon', 2 => 'tue', 3 => 'wed', 4 => 'thu', 5 => 'fri', 6 => 'sat', 7 => 'sun' ]; $current_day_code = $day_map[ (int) $now->format( 'N' ) ]; if ( ! in_array( $current_day_code, $allowed_days, true ) ) { return false; // Wrong weekday. } } // Last check = Time check. return $this->is_in_schedule_time( $ad, $now, $tz ); } /** * Whether the "Show Scheduling Options" toggle is effectively on for this ad. * * @param Ad $ad * * @return bool */ public function is_scheduling_enabled( Ad $ad ): bool { $has_legacy_schedule = ! empty( $ad->get_meta( 'start_date', '' ) ) || ! empty( $ad->get_meta( 'end_date', '' ) ) || ! empty( Util::get_checked_keys( $ad->get_meta( 'schedule_days', [] ) ) ) || $ad->get_meta( 'schedule_time_mode', 'all_day' ) === 'scheduled'; return (bool) $ad->get_meta( 'scheduling_enabled', $has_legacy_schedule ? '1' : '' ); } /** * Whether this ad currently has an actual schedule restriction configured. * * @param Ad $ad * * @return bool */ public function has_effective_schedule_restriction( Ad $ad ): bool { if ( ! $this->is_scheduling_enabled( $ad ) ) { return false; } if ( ! empty( $ad->get_meta( 'start_date', '' ) ) || ! empty( $ad->get_meta( 'end_date', '' ) ) ) { return true; } // Weekday/hour-of-day restrictions are Pro-only if ( ! Util::is_pro_active() ) { return false; } $allowed_days = Util::get_checked_keys( $ad->get_meta( 'schedule_days', [] ) ); if ( ! empty( $allowed_days ) && is_array( $allowed_days ) ) { return true; } // empty start/end time isn't actually a restriction either. if ( $ad->get_meta( 'schedule_time_mode', 'all_day' ) === 'scheduled' && ! empty( $ad->get_meta( 'schedule_time_start', '' ) ) && ! empty( $ad->get_meta( 'schedule_time_end', '' ) ) ) { return true; } return false; } /** * Checks the ads for hours scheduling. * * @param Ad $ad * @param DateTime $now * @param DateTimeZone $tz * * @return bool */ private function is_in_schedule_time( Ad $ad, DateTime $now, DateTimeZone $tz ): bool { $mode = $ad->get_meta( 'schedule_time_mode', 'all_day' ); if ( $mode !== 'scheduled' ) { return true; // "All day" is always true } // Get the expected times (expected Format "H:i", e.g. "14:30") $start_str = $ad->get_meta( 'schedule_time_start' ); $end_str = $ad->get_meta( 'schedule_time_end' ); if ( empty( $start_str ) || empty( $end_str ) ) { return true; // Fallback: when not correctly configured. } try { // Comparing objects. $start_dt = DateTime::createFromFormat( 'Y-m-d H:i', $now->format( 'Y-m-d' ) . ' ' . $start_str, $tz ); $end_dt = DateTime::createFromFormat( 'Y-m-d H:i', $now->format( 'Y-m-d' ) . ' ' . $end_str, $tz ); if ( ! $start_dt || ! $end_dt ) { return true; } $now_ts = $now->getTimestamp(); $start_ts = $start_dt->getTimestamp(); $end_ts = $end_dt->getTimestamp(); if ( $start_ts < $end_ts ) { // Normal day. return ( $now_ts >= $start_ts && $now_ts < $end_ts ); } elseif ( $start_ts > $end_ts ) { // Time defined over the night (e.g. 22:00 to 06:00). return ( $now_ts >= $start_ts || $now_ts < $end_ts ); } else { // Start == End (no duration) return false; } } catch ( Exception ) { return true; } } }