scheduler = $scheduler; } /** * Seed default options. * * @return void */ public function seed_defaults() { if ( false === get_option( self::MANUAL_MODE_OPTION ) ) { $legacy = strtolower( sanitize_key( (string) get_option( 'abct_manual_store_status', 'open' ) ) ); $mode = 'closed' === $legacy ? 'offline' : 'online'; add_option( self::MANUAL_MODE_OPTION, $mode ); } if ( false === get_option( self::CLOSED_MESSAGE_OPTION ) ) { add_option( self::CLOSED_MESSAGE_OPTION, '' ); } if ( false === get_option( self::MANUAL_OVERRIDE_OPTION ) ) { $override = array(); if ( 'offline' === $this->get_stored_manual_mode() ) { $override = $this->build_manual_override_payload( 'offline' ); } add_option( self::MANUAL_OVERRIDE_OPTION, $override ); } } /** * Get manual mode. * * @return string online|offline */ public function get_manual_mode() { $override = $this->get_active_manual_override(); if ( ! empty( $override['mode'] ) ) { return (string) $override['mode']; } return 'online'; } /** * Set manual mode. * * @param string $mode Mode. * @return void */ public function set_manual_mode( $mode ) { $normalized = 'offline' === strtolower( sanitize_key( $mode ) ) ? 'offline' : 'online'; update_option( self::MANUAL_OVERRIDE_OPTION, $this->build_manual_override_payload( $normalized ) ); $this->sync_stored_manual_mode( $normalized ); $this->clear_state_cache(); } /** * Get effective store state. * * @return string online|offline_manual|offline_schedule */ public function get_state() { $cache_ttl = max( 1, absint( apply_filters( 'abct_store_state_cache_ttl_seconds', 5 ) ) ); $override = $this->get_active_manual_override(); if ( ! empty( $override ) && 'offline' === $override['mode'] ) { $cached = get_transient( 'abct_store_state_cache' ); if ( is_array( $cached ) && isset( $cached['state'], $cached['expires_at'] ) && 'offline_manual' === $cached['state'] && $cached['expires_at'] >= time() ) { return 'offline_manual'; } set_transient( 'abct_store_state_cache', array( 'state' => 'offline_manual', 'expires_at' => time() + $cache_ttl, ), $cache_ttl ); return 'offline_manual'; } return $this->scheduler->is_open_by_schedule() ? 'online' : 'offline_schedule'; } /** * Check if store is online. * * @return bool */ public function is_online() { return 'online' === $this->get_state(); } /** * Check whether the highlighted product-page notice should be shown. * * @return bool */ public function should_show_closed_message() { $settings = $this->scheduler->get_settings(); return ! empty( $settings['show_closed_message'] ); } /** * Get the next opening as formatted text. * * @param bool $strict_future Whether the result must be strictly in the future. * @return string */ public function get_next_opening_text( $strict_future = true ) { if ( ! $strict_future ) { return (string) $this->scheduler->get_next_opening_label(); } $next = $this->scheduler->get_next_opening_datetime( null, (bool) $strict_future ); if ( ! $next instanceof DateTimeInterface ) { return (string) $this->scheduler->get_next_opening_label(); } return wp_date( get_option( 'date_format' ) . ' ' . get_option( 'time_format' ), $next->getTimestamp() ); } /** * Replace the next opening token while preserving the operator-defined text. * * @param string $message Message template. * @param string $fallback Fallback message when the token cannot be resolved. * @return string */ public function interpolate_next_opening_text( $message, $fallback = '' ) { $message = (string) $message; if ( ! $this->contains_next_opening_token( $message ) ) { return $message; } $next_label = $this->get_next_opening_text(); if ( '' === $next_label ) { return '' !== (string) $fallback ? (string) $fallback : $this->get_default_closed_notice(); } return str_replace( '{{next_opening}}', $next_label, $message ); } /** * Get closed message. * * @return string */ public function get_closed_message() { $state = $this->get_state(); if ( 'online' === $state ) { return ''; } $message = sanitize_textarea_field( (string) get_option( self::CLOSED_MESSAGE_OPTION, '' ) ); if ( '' === $message ) { if ( 'offline_manual' === $state ) { return $this->get_default_closed_notice(); } return $this->get_default_schedule_closed_message(); } if ( 'offline_manual' === $state ) { $manual_message = $this->strip_next_opening_token( $message ); return '' !== $manual_message ? $manual_message : $this->get_default_closed_notice(); } if ( ! $this->contains_next_opening_token( $message ) ) { return $message; } return $this->interpolate_next_opening_text( $message, $this->get_default_closed_notice() ); } /** * Get the default closed notice without schedule details. * * @return string */ private function get_default_closed_notice() { return __( 'Store is currently closed.', 'abct-smart-orders' ); } /** * Get the default schedule-based closed message. * * @return string */ private function get_default_schedule_closed_message() { $next_label = $this->get_next_opening_text(); if ( '' !== $next_label ) { return sprintf( /* translators: %s: next opening datetime */ __( 'Store is currently closed. Next opening: %s', 'abct-smart-orders' ), $next_label ); } return $this->get_default_closed_notice(); } /** * Check if a custom message contains the next opening token. * * @param string $message Message template. * @return bool */ private function contains_next_opening_token( $message ) { return false !== strpos( (string) $message, '{{next_opening}}' ); } /** * Remove next opening token from a message template for manual offline mode. * * @param string $message Message template. * @return string */ private function strip_next_opening_token( $message ) { $clean = trim( str_replace( '{{next_opening}}', '', (string) $message ) ); $clean = preg_replace( '/[ \t]+/', ' ', $clean ); $clean = preg_replace( '/\s+([,.;:!?])/', '$1', (string) $clean ); return trim( (string) $clean ); } /** * Get normalized stored manual mode without override expiry checks. * * @return string */ private function get_stored_manual_mode() { $mode = strtolower( sanitize_key( (string) get_option( self::MANUAL_MODE_OPTION, 'online' ) ) ); return in_array( $mode, array( 'online', 'offline' ), true ) ? $mode : 'online'; } /** * Build manual override payload for current store configuration. * * @param string $mode Manual mode. * @return array */ private function build_manual_override_payload( $mode ) { $normalized = 'offline' === strtolower( sanitize_key( (string) $mode ) ) ? 'offline' : 'online'; $settings = $this->scheduler->get_settings(); return array( 'mode' => $normalized, 'activated_at' => $this->get_current_timestamp(), 'source' => ! empty( $settings['open_24_hours'] ) ? '24h' : 'schedule', ); } /** * Get the currently active manual override, clearing it when expired. * * @return array */ private function get_active_manual_override() { $override = $this->get_manual_override_payload(); if ( empty( $override ) ) { return array(); } $settings = $this->scheduler->get_settings(); if ( ! empty( $settings['open_24_hours'] ) ) { return $override; } if ( '24h' === $override['source'] ) { $this->clear_manual_override(); return array(); } $expires_at = $this->get_manual_override_expiration( $override ); if ( $expires_at instanceof DateTimeInterface && $expires_at->getTimestamp() <= $this->get_current_timestamp() ) { $this->clear_manual_override(); return array(); } return $override; } /** * Get sanitized manual override payload. * * @return array */ private function get_manual_override_payload() { $raw = get_option( self::MANUAL_OVERRIDE_OPTION, array() ); if ( ! is_array( $raw ) || empty( $raw ) ) { if ( 'offline' === $this->get_stored_manual_mode() ) { $legacy_override = $this->build_manual_override_payload( 'offline' ); update_option( self::MANUAL_OVERRIDE_OPTION, $legacy_override ); return $legacy_override; } return array(); } $mode = isset( $raw['mode'] ) ? strtolower( sanitize_key( (string) $raw['mode'] ) ) : ''; $activated_at = isset( $raw['activated_at'] ) ? absint( $raw['activated_at'] ) : 0; $source = isset( $raw['source'] ) ? sanitize_key( (string) $raw['source'] ) : 'schedule'; if ( ! in_array( $mode, array( 'online', 'offline' ), true ) || $activated_at <= 0 ) { return array(); } if ( ! in_array( $source, array( '24h', 'schedule' ), true ) ) { $source = 'schedule'; } return array( 'mode' => $mode, 'activated_at' => $activated_at, 'source' => $source, ); } /** * Resolve the expiration datetime for a manual override. * * @param array $override Manual override payload. * @return DateTimeInterface|null */ private function get_manual_override_expiration( $override ) { $mode = isset( $override['mode'] ) ? sanitize_key( (string) $override['mode'] ) : ''; $activated_at = isset( $override['activated_at'] ) ? absint( $override['activated_at'] ) : 0; if ( $activated_at <= 0 || ! in_array( $mode, array( 'online', 'offline' ), true ) ) { return null; } $activated_datetime = $this->get_datetime_from_timestamp( $activated_at ); if ( ! $activated_datetime instanceof DateTimeInterface ) { return null; } if ( 'offline' === $mode ) { return $this->scheduler->get_next_opening_datetime( $activated_datetime, true ); } return $this->scheduler->get_next_closing_datetime( $activated_datetime ); } /** * Reset manual override and fall back to schedule state. * * @return void */ private function clear_manual_override() { update_option( self::MANUAL_OVERRIDE_OPTION, array() ); $this->sync_stored_manual_mode( 'online' ); $this->clear_state_cache(); } /** * Sync legacy/manual mode storage. * * @param string $mode Manual mode. * @return void */ private function sync_stored_manual_mode( $mode ) { $normalized = 'offline' === strtolower( sanitize_key( (string) $mode ) ) ? 'offline' : 'online'; update_option( self::MANUAL_MODE_OPTION, $normalized ); update_option( 'abct_manual_store_status', 'offline' === $normalized ? 'closed' : 'open' ); } /** * Clear cached effective store state. * * @return void */ private function clear_state_cache() { delete_transient( 'abct_store_state_cache' ); } /** * Convert a local timestamp into a timezone-aware immutable datetime. * * @param int $timestamp Local WordPress timestamp. * @return DateTimeInterface */ private function get_datetime_from_timestamp( $timestamp ) { $datetime = new DateTimeImmutable( '@' . absint( $timestamp ) ); return $datetime->setTimezone( wp_timezone() ); } /** * Get current WordPress-local timestamp. * * @return int */ private function get_current_timestamp() { if ( function_exists( 'current_datetime' ) ) { return (int) current_datetime()->getTimestamp(); } return time(); } }