seed_defaults(); abct_get_status_manager()->seed_defaults(); } /** * Bootstrap schedule/status integrations. * * @return void */ function abct_bootstrap_schedule_modules() { static $bootstrapped = false; if ( $bootstrapped ) { return; } $bootstrapped = true; $status = abct_get_status_manager(); $storefront = new ABCT_Storefront_Control( $status ); $storefront->register(); $shortcode = new ABCT_Store_Status_Shortcode( $status, abct_get_scheduler() ); $shortcode->register(); } add_action( 'init', 'abct_bootstrap_schedule_modules', 20 ); /** * AJAX store state for live UI updates. * * @return void */ function abct_ajax_get_store_state() { nocache_headers(); $nonce = isset( $_REQUEST['nonce'] ) ? sanitize_text_field( wp_unslash( $_REQUEST['nonce'] ) ) : ''; if ( ! wp_verify_nonce( $nonce, 'abct_store_status_nonce' ) ) { wp_send_json_error( array( 'message' => __( 'Invalid request.', 'abct-smart-orders' ) ), 403 ); } $status = abct_get_status_manager(); wp_send_json_success( array( 'state' => $status->get_state(), 'manual_mode' => $status->get_manual_mode(), 'message' => $status->get_closed_message(), 'next_opening' => abct_get_next_opening_text(), ) ); } add_action( 'wp_ajax_nopriv_abct_get_store_state', 'abct_ajax_get_store_state' ); add_action( 'wp_ajax_abct_get_store_state', 'abct_ajax_get_store_state' ); /** * Backward compatible AJAX validator endpoint. * * @return void */ function abct_ajax_validate_store_open() { nocache_headers(); $nonce = isset( $_REQUEST['nonce'] ) ? sanitize_text_field( wp_unslash( $_REQUEST['nonce'] ) ) : ''; if ( ! wp_verify_nonce( $nonce, 'abct_orders_nonce' ) && ! wp_verify_nonce( $nonce, 'abct_store_status_nonce' ) ) { wp_send_json_error( array( 'message' => __( 'Invalid request.', 'abct-smart-orders' ) ), 403 ); } if ( abct_is_store_open() ) { wp_send_json_success( array( 'message' => __( 'Store is open.', 'abct-smart-orders' ) ) ); } wp_send_json_error( array( 'message' => abct_get_restaurant_closed_message() ), 403 ); } add_action( 'wp_ajax_nopriv_abct_validate_store_open', 'abct_ajax_validate_store_open' ); add_action( 'wp_ajax_abct_validate_store_open', 'abct_ajax_validate_store_open' ); /** * Backward compatible settings getter. * * @return array */ function abct_get_opening_hours_settings() { return abct_get_scheduler()->get_settings(); } /** * Backward compatible sanitizer. * * @param array|mixed $input Input payload. * @return array */ function abct_sanitize_opening_hours( $input ) { return abct_get_scheduler()->sanitize_settings( $input ); } /** * Backward compatible open-by-schedule checker. * * @return bool */ function abct_restaurant_is_open() { return abct_get_scheduler()->is_open_by_schedule(); } /** * Backward compatible store online checker. * * @return bool */ function abct_is_store_open() { return abct_get_status_manager()->is_online(); } /** * Backward compatible manual status getter. * * @return string open|closed */ function abct_get_manual_store_status() { return 'offline' === abct_get_status_manager()->get_manual_mode() ? 'closed' : 'open'; } /** * Backward compatible state getter. * * @return string open|closed_manual|closed_schedule */ function abct_get_store_state() { $state = abct_get_status_manager()->get_state(); if ( 'online' === $state ) { return 'open'; } if ( 'offline_manual' === $state ) { return 'closed_manual'; } return 'closed_schedule'; } /** * Backward compatible closed message. * * @return string */ function abct_get_restaurant_closed_message() { return abct_get_status_manager()->get_closed_message(); } /** * Get the next opening as formatted text. * * @param bool $strict_future Whether the opening must be strictly in the future. * @return string */ function abct_get_next_opening_text( $strict_future = true ) { return abct_get_status_manager()->get_next_opening_text( $strict_future ); } /** * Interpolate the {{next_opening}} token in arbitrary text. * * @param string $message Message template. * @param string $fallback Fallback text when the opening cannot be resolved. * @return string */ function abct_interpolate_next_opening_text( $message, $fallback = '' ) { return abct_get_status_manager()->interpolate_next_opening_text( $message, $fallback ); } /** * Backward compatible checkout/add-to-cart guard. * * @return true|string */ function abct_maybe_block_orders_when_closed() { if ( abct_is_store_open() ) { return true; } $message = abct_get_restaurant_closed_message(); return '' !== $message ? $message : __( 'Store is currently closed.', 'abct-smart-orders' ); }