*/ public function get_default_settings() { return array( 'open_24_hours' => 0, 'lunch_enabled' => 0, 'lunch_open' => '00:00', 'lunch_close' => '11:59', 'dinner_enabled' => 0, 'dinner_open' => '12:00', 'dinner_close' => '23:59', 'show_closed_message' => 1, 'weekdays' => array( 'mon' => 0, 'tue' => 0, 'wed' => 0, 'thu' => 0, 'fri' => 0, 'sat' => 0, 'sun' => 0, ), ); } /** * Get schedule settings merged with defaults. * * @return array */ public function get_settings() { $defaults = $this->get_default_settings(); $raw = get_option( self::OPTION_KEY, null ); $has_persisted_settings = is_array( $raw ); if ( ! $has_persisted_settings ) { $raw = array(); } if ( isset( $raw['weekdays'] ) && is_array( $raw['weekdays'] ) ) { $weekdays = array(); foreach ( $defaults['weekdays'] as $day => $enabled ) { $weekdays[ $day ] = ! empty( $raw['weekdays'][ $day ] ) ? 1 : 0; } } elseif ( $has_persisted_settings ) { $weekdays = array_fill_keys( array_keys( $defaults['weekdays'] ), 0 ); } else { $weekdays = $defaults['weekdays']; } return array( 'open_24_hours' => ! empty( $raw['open_24_hours'] ) ? 1 : 0, 'lunch_enabled' => ! empty( $raw['lunch_enabled'] ) ? 1 : 0, 'lunch_open' => isset( $raw['lunch_open'] ) ? sanitize_text_field( $raw['lunch_open'] ) : $defaults['lunch_open'], 'lunch_close' => isset( $raw['lunch_close'] ) ? sanitize_text_field( $raw['lunch_close'] ) : $defaults['lunch_close'], 'dinner_enabled' => ! empty( $raw['dinner_enabled'] ) ? 1 : 0, 'dinner_open' => isset( $raw['dinner_open'] ) ? sanitize_text_field( $raw['dinner_open'] ) : $defaults['dinner_open'], 'dinner_close' => isset( $raw['dinner_close'] ) ? sanitize_text_field( $raw['dinner_close'] ) : $defaults['dinner_close'], 'show_closed_message' => array_key_exists( 'show_closed_message', $raw ) ? ( ! empty( $raw['show_closed_message'] ) ? 1 : 0 ) : 1, 'weekdays' => $weekdays, ); } /** * Sanitize schedule settings payload. * * @param array|mixed $input Raw input. * @return array */ public function sanitize_settings( $input ) { $defaults = $this->get_default_settings(); if ( ! is_array( $input ) ) { $input = array(); } $output = array( 'open_24_hours' => isset( $input['open_24_hours'] ) ? 1 : 0, 'lunch_enabled' => isset( $input['lunch_enabled'] ) ? 1 : 0, 'lunch_open' => isset( $input['lunch_open'] ) ? sanitize_text_field( wp_unslash( $input['lunch_open'] ) ) : $defaults['lunch_open'], 'lunch_close' => isset( $input['lunch_close'] ) ? sanitize_text_field( wp_unslash( $input['lunch_close'] ) ) : $defaults['lunch_close'], 'dinner_enabled' => isset( $input['dinner_enabled'] ) ? 1 : 0, 'dinner_open' => isset( $input['dinner_open'] ) ? sanitize_text_field( wp_unslash( $input['dinner_open'] ) ) : $defaults['dinner_open'], 'dinner_close' => isset( $input['dinner_close'] ) ? sanitize_text_field( wp_unslash( $input['dinner_close'] ) ) : $defaults['dinner_close'], 'show_closed_message' => isset( $input['show_closed_message'] ) ? 1 : 0, 'weekdays' => array(), ); $input_weekdays = isset( $input['weekdays'] ) && is_array( $input['weekdays'] ) ? $input['weekdays'] : array(); foreach ( $defaults['weekdays'] as $day => $enabled ) { $output['weekdays'][ $day ] = isset( $input_weekdays[ $day ] ) ? 1 : 0; } foreach ( array( 'lunch_open', 'lunch_close', 'dinner_open', 'dinner_close' ) as $key ) { if ( ! preg_match( '/^(?:[01]\d|2[0-3]):[0-5]\d$/', $output[ $key ] ) ) { $output[ $key ] = $defaults[ $key ]; } } return $output; } /** * Check if current datetime is inside configured schedule. * * @param DateTimeInterface|null $datetime Datetime. * @return bool */ public function is_open_by_schedule( $datetime = null ) { $settings = $this->get_settings(); if ( null === $datetime ) { $datetime = $this->get_current_datetime(); } $weekday = strtolower( substr( $datetime->format( 'D' ), 0, 3 ) ); $current_enabled = ! empty( $settings['weekdays'][ $weekday ] ); if ( ! $current_enabled ) { return false; } // Weekdays always have priority. After the current weekday is confirmed // as enabled, 24-hour mode keeps the store open for the whole day. // If 24-hour mode is disabled, Shift 1 and Shift 2 define the open windows. if ( ! empty( $settings['open_24_hours'] ) ) { return true; } $lunch_open = ! empty( $settings['lunch_enabled'] ) && $this->is_time_in_range( $settings['lunch_open'], $settings['lunch_close'], $datetime, $current_enabled, 'lunch' ); $dinner_open = ! empty( $settings['dinner_enabled'] ) && $this->is_time_in_range( $settings['dinner_open'], $settings['dinner_close'], $datetime, $current_enabled, 'dinner' ); return $lunch_open || $dinner_open; } /** * Return next opening datetime. * * @return DateTimeInterface|null */ public function get_next_opening_datetime( $from_datetime = null, $strict_future = false ) { $settings = $this->get_settings(); if ( null === $from_datetime ) { $from_datetime = $this->get_current_datetime(); } if ( ! $strict_future && $this->is_open_by_schedule( $from_datetime ) ) { return $from_datetime; } if ( ! empty( $settings['open_24_hours'] ) ) { return $this->get_next_24h_weekday_transition_datetime( 'open', $from_datetime ); } return $this->get_next_transition_datetime( 'open', $from_datetime ); } /** * Return next closing datetime. * * @param DateTimeInterface|null $from_datetime Datetime reference. * @return DateTimeInterface|null */ public function get_next_closing_datetime( $from_datetime = null ) { $settings = $this->get_settings(); if ( null === $from_datetime ) { $from_datetime = $this->get_current_datetime(); } if ( ! empty( $settings['open_24_hours'] ) ) { return $this->get_next_24h_weekday_transition_datetime( 'close', $from_datetime ); } return $this->get_next_transition_datetime( 'close', $from_datetime ); } /** * Format next opening time. * * @return string */ public function get_next_opening_label() { $next = $this->get_next_opening_datetime(); if ( ! $next ) { return ''; } return wp_date( get_option( 'date_format' ) . ' ' . get_option( 'time_format' ), $next->getTimestamp() ); } /** * Seed default settings if option is missing. * * @return void */ public function seed_defaults() { if ( false === get_option( self::OPTION_KEY ) ) { add_option( self::OPTION_KEY, $this->get_default_settings() ); } } /** * Check if a datetime falls in a range. * * @param string $open_time Open time HH:MM. * @param string $close_time Close time HH:MM. * @param DateTimeInterface $datetime Current datetime. * @param bool $current_day_enabled Whether current weekday is enabled. * @param string $shift_key Shift identifier. * @return bool */ private function is_time_in_range( $open_time, $close_time, $datetime, $current_day_enabled = true, $shift_key = '' ) { if ( ! $current_day_enabled || ! $this->has_valid_shift_window( $open_time, $close_time, $shift_key ) ) { return false; } $now_ts = $datetime->getTimestamp(); $current_window = $this->build_operational_shift_window( $this->get_day_reference( $datetime ), $open_time, $close_time, $shift_key ); if ( is_array( $current_window ) && $now_ts >= $current_window['open']->getTimestamp() && $now_ts <= $current_window['close']->getTimestamp() ) { return true; } return false; } /** * Check whether a shift window is valid for schedule use. * * @param string $open_time Open time HH:MM. * @param string $close_time Close time HH:MM. * @param string $shift_key Shift identifier. * @return bool */ private function has_valid_shift_window( $open_time, $close_time, $shift_key = '' ) { if ( ! preg_match( '/^(?:[01]\d|2[0-3]):[0-5]\d$/', $open_time ) || ! preg_match( '/^(?:[01]\d|2[0-3]):[0-5]\d$/', $close_time ) ) { return false; } $open_minutes = $this->parse_time_to_minutes( $open_time ); $close_minutes = $this->parse_close_time_to_minutes( $close_time ); return null !== $open_minutes && null !== $close_minutes && $open_minutes < $close_minutes; } /** * Get enabled shift definitions sorted by opening time. * * @param array $settings Schedule settings. * @return array> */ private function get_enabled_shifts( $settings ) { $shifts = array(); $map = array( array( 'key' => 'lunch', 'enabled_key' => 'lunch_enabled', 'open_key' => 'lunch_open', 'close_key' => 'lunch_close', ), array( 'key' => 'dinner', 'enabled_key' => 'dinner_enabled', 'open_key' => 'dinner_open', 'close_key' => 'dinner_close', ), ); foreach ( $map as $shift ) { if ( empty( $settings[ $shift['enabled_key'] ] ) ) { continue; } $open_time = isset( $settings[ $shift['open_key'] ] ) ? (string) $settings[ $shift['open_key'] ] : ''; $close_time = isset( $settings[ $shift['close_key'] ] ) ? (string) $settings[ $shift['close_key'] ] : ''; if ( ! $this->has_valid_shift_window( $open_time, $close_time, $shift['key'] ) ) { continue; } $shifts[] = array( 'key' => $shift['key'], 'open' => $open_time, 'close' => $close_time, ); } usort( $shifts, static function ( $left, $right ) { return strcmp( $left['open'], $right['open'] ); } ); return $shifts; } /** * Resolve the next scheduled transition datetime. * * @param string $transition_type Transition type. * @param DateTimeInterface $from_datetime Datetime reference. * @return DateTimeInterface|null */ /** * Resolve next open/close boundary while 24-hour mode is limited by weekdays. * * @param string $transition_type Transition type. * @param DateTimeInterface $from_datetime Datetime reference. * @return DateTimeInterface|null */ private function get_next_24h_weekday_transition_datetime( $transition_type, DateTimeInterface $from_datetime ) { $settings = $this->get_settings(); if ( ! in_array( $transition_type, array( 'open', 'close' ), true ) ) { return null; } $from_timestamp = $from_datetime->getTimestamp(); $candidate_day = $this->get_day_reference( $from_datetime ); $next_event = null; for ( $day_offset = 0; $day_offset <= 14; ++$day_offset ) { $day_reference = 0 === $day_offset ? $candidate_day : $candidate_day->modify( '+' . $day_offset . ' day' ); $weekday = strtolower( substr( $day_reference->format( 'D' ), 0, 3 ) ); $enabled = ! empty( $settings['weekdays'][ $weekday ] ); if ( 'open' === $transition_type ) { if ( ! $enabled ) { continue; } $event = $day_reference; } else { if ( $enabled ) { continue; } $event = $day_reference; } if ( $event->getTimestamp() <= $from_timestamp ) { continue; } if ( null === $next_event || $event->getTimestamp() < $next_event->getTimestamp() ) { $next_event = $event; } } return $next_event; } private function get_next_transition_datetime( $transition_type, DateTimeInterface $from_datetime ) { $settings = $this->get_settings(); $shifts = $this->get_enabled_shifts( $settings ); if ( empty( $shifts ) || ! in_array( $transition_type, array( 'open', 'close' ), true ) ) { return null; } $from_timestamp = $from_datetime->getTimestamp(); $candidate_day = $this->get_day_reference( $from_datetime ); $next_event = null; for ( $day_offset = -1; $day_offset <= 7; $day_offset++ ) { $day_reference = 0 === $day_offset ? $candidate_day : $candidate_day->modify( ( $day_offset > 0 ? '+' : '' ) . $day_offset . ' day' ); $weekday = strtolower( substr( $day_reference->format( 'D' ), 0, 3 ) ); if ( empty( $settings['weekdays'][ $weekday ] ) ) { continue; } foreach ( $shifts as $shift ) { $window = $this->build_operational_shift_window( $day_reference, $shift['open'], $shift['close'], isset( $shift['key'] ) ? (string) $shift['key'] : '' ); if ( null === $window ) { continue; } $event = 'close' === $transition_type ? $window['close'] : $window['open']; if ( $event->getTimestamp() <= $from_timestamp ) { continue; } if ( null === $next_event || $event->getTimestamp() < $next_event->getTimestamp() ) { $next_event = $event; } } } return $next_event; } /** * Build the effective operational window for a calendar day. * * @param DateTimeInterface $date_reference Base day reference. * @param string $open_time Open time HH:MM. * @param string $close_time Close time HH:MM. * @param string $shift_key Shift identifier. * @return array|null */ private function build_operational_shift_window( DateTimeInterface $date_reference, $open_time, $close_time, $shift_key = '' ) { $window = $this->build_shift_window( $date_reference, $open_time, $close_time, $shift_key ); if ( ! is_array( $window ) ) { return null; } return $window; } /** * Build a shift window for a specific reference day. * * @param DateTimeInterface $date_reference Base day reference. * @param string $open_time Open time HH:MM. * @param string $close_time Close time HH:MM. * @param string $shift_key Shift identifier. * @return array|null */ private function build_shift_window( DateTimeInterface $date_reference, $open_time, $close_time, $shift_key = '' ) { if ( ! $this->has_valid_shift_window( $open_time, $close_time, $shift_key ) ) { return null; } $open_dt = $this->create_datetime_for_time( $date_reference, $open_time ); $close_dt = $this->create_datetime_for_time( $date_reference, $close_time ); if ( '00:00' === $close_time && $open_time !== $close_time && $close_dt instanceof DateTimeImmutable ) { $close_dt = $close_dt->modify( '+1 day' ); } if ( ! $open_dt instanceof DateTimeImmutable || ! $close_dt instanceof DateTimeImmutable ) { return null; } return array( 'open' => $open_dt, 'close' => $close_dt, ); } /** * Parse a HH:MM time string into minutes from midnight. * * @param string $time Time string. * @return int|null */ private function parse_time_to_minutes( $time ) { $time = trim( (string) $time ); if ( ! preg_match( '/^(?:[01]\d|2[0-3]):[0-5]\d$/', $time ) ) { return null; } list( $hours, $minutes ) = array_map( 'intval', explode( ':', $time ) ); return ( $hours * 60 ) + $minutes; } /** * Parse a close time, treating 00:00 as the end of the configured day. * * @param string $time Time string. * @return int|null */ private function parse_close_time_to_minutes( $time ) { $minutes = $this->parse_time_to_minutes( $time ); if ( null === $minutes ) { return null; } return '00:00' === trim( (string) $time ) ? 1440 : $minutes; } /** * Create an immutable datetime for a local date and HH:MM time. * * @param DateTimeInterface $date_reference Base day reference. * @param string $time Time in HH:MM. * @return DateTimeImmutable|null */ private function create_datetime_for_time( DateTimeInterface $date_reference, $time ) { if ( ! preg_match( '/^(?:[01]\d|2[0-3]):[0-5]\d$/', $time ) ) { return null; } return DateTimeImmutable::createFromFormat( 'Y-m-d H:i:s', $date_reference->format( 'Y-m-d' ) . ' ' . $time . ':00', $date_reference->getTimezone() ); } /** * Get an immutable reference day in the site timezone. * * @param DateTimeInterface $datetime Base datetime. * @param int $day_offset Day offset. * @return DateTimeImmutable */ private function get_day_reference( DateTimeInterface $datetime, $day_offset = 0 ) { $reference = new DateTimeImmutable( $datetime->format( 'Y-m-d 00:00:00' ), $datetime->getTimezone() ); if ( 0 !== (int) $day_offset ) { $reference = $reference->modify( ( (int) $day_offset > 0 ? '+' : '' ) . (int) $day_offset . ' day' ); } return $reference; } /** * Get current site datetime using the operational schedule timezone. * * WordPress installs sometimes remain configured as UTC even when the store * operates in Brazil. In that case the schedule can look closed while the * operator sees the local admin clock inside an active shift. Prefer the * WordPress timezone when it is explicitly configured, but provide a safe * Brazil fallback for pt_BR/BR stores. The timezone can be overridden with * the abct_smart_orders_schedule_timezone filter. * * @return DateTimeInterface */ private function get_current_datetime() { $timezone = $this->get_schedule_timezone(); $current = new DateTimeImmutable( 'now', $timezone ); return $current; } /** * Resolve the timezone used by the schedule engine. * * @return DateTimeZone */ private function get_schedule_timezone() { $timezone_string = (string) get_option( 'timezone_string', '' ); $gmt_offset = (float) get_option( 'gmt_offset', 0 ); $fallback_tz = ''; if ( '' !== $timezone_string ) { $fallback_tz = $timezone_string; } elseif ( 0.0 !== $gmt_offset ) { $fallback_tz = timezone_name_from_abbr( '', (int) ( $gmt_offset * HOUR_IN_SECONDS ), 0 ); } elseif ( $this->should_use_brazil_timezone_fallback() ) { $fallback_tz = 'America/Sao_Paulo'; } if ( ! is_string( $fallback_tz ) || '' === $fallback_tz ) { $fallback_tz = 'UTC'; } $timezone_name = apply_filters( 'abct_smart_orders_schedule_timezone', $fallback_tz ); $timezone_name = is_string( $timezone_name ) && '' !== $timezone_name ? $timezone_name : $fallback_tz; try { return new DateTimeZone( $timezone_name ); } catch ( Exception $exception ) { return wp_timezone(); } } /** * Determine whether a Brazil operational fallback is appropriate. * * @return bool */ private function should_use_brazil_timezone_fallback() { $locale = function_exists( 'determine_locale' ) ? determine_locale() : get_locale(); if ( is_string( $locale ) && 0 === strpos( strtolower( $locale ), 'pt_br' ) ) { return true; } if ( function_exists( 'WC' ) && WC() && WC()->countries ) { $base_country = (string) WC()->countries->get_base_country(); if ( 'BR' === strtoupper( $base_country ) ) { return true; } } return false; } }