'', 'phone' => '', 'hours' => array_fill( 0, 7, array( 'open' => '11:00', 'close' => '22:00' ) ), 'last_order' => 30, 'closures' => array(), 'paused' => false, ); $saved = get_option( 'sofrah_board_venue', array() ); $v = wp_parse_args( is_array( $saved ) ? $saved : array(), $defaults ); for ( $d = 0; $d < 7; $d++ ) { $v['hours'][ $d ] = wp_parse_args( (array) ( $v['hours'][ $d ] ?? array() ), array( 'open' => '', 'close' => '' ) ); } return $v; } /** * Whether the venue has been set up at all (hours saved by the owner). * * @return bool */ function sofrah_board_venue_configured() { return false !== get_option( 'sofrah_board_venue', false ); } /** * Minutes since midnight for "HH:MM", or null. * * @param string $hm Time. * @return int|null */ function sofrah_board_minutes( $hm ) { return preg_match( '/^([01]\d|2[0-3]):([0-5]\d)$/', (string) $hm, $m ) ? (int) $m[1] * 60 + (int) $m[2] : null; } /** * The ordering window of the day that starts on $day, as two DateTimeImmutable (open, last order), or null if closed that day. * * @param DateTimeImmutable $day Any time on that day, in the site's time zone. * @return array|null */ function sofrah_board_window_for( DateTimeImmutable $day ) { $v = sofrah_board_venue(); if ( in_array( $day->format( 'Y-m-d' ), (array) $v['closures'], true ) ) { return null; } $h = $v['hours'][ (int) $day->format( 'w' ) ]; $open = sofrah_board_minutes( $h['open'] ); $close = sofrah_board_minutes( $h['close'] ); if ( null === $open || null === $close ) { return null; } if ( $close <= $open ) { $close += 24 * 60; // Closes after midnight. } $start = $day->setTime( 0, 0 )->modify( '+' . $open . ' minutes' ); $last = $day->setTime( 0, 0 )->modify( '+' . max( $open, $close - (int) $v['last_order'] ) . ' minutes' ); return array( $start, $last ); } /** * Whether pickup orders can be placed right now, and what to tell the diner. * * @param DateTimeImmutable|null $now Time to check (for tests); now by default. * @return array { open: bool, state: open|paused|closed, until: DateTimeImmutable|null, next: DateTimeImmutable|null } */ function sofrah_board_ordering_state( $now = null ) { $now = $now ?: new DateTimeImmutable( 'now', wp_timezone() ); $v = sofrah_board_venue(); if ( $v['paused'] ) { return array( 'open' => false, 'state' => 'paused', 'until' => null, 'next' => null ); } if ( ! sofrah_board_venue_configured() ) { return array( 'open' => true, 'state' => 'open', 'until' => null, 'next' => null ); // No hours set yet: never refuse orders. } // Yesterday's window may run past midnight. foreach ( array( $now->modify( '-1 day' ), $now ) as $day ) { $w = sofrah_board_window_for( $day ); if ( $w && $now >= $w[0] && $now < $w[1] ) { return array( 'open' => true, 'state' => 'open', 'until' => $w[1], 'next' => null ); } } for ( $i = 0; $i < 14; $i++ ) { $w = sofrah_board_window_for( $now->modify( '+' . $i . ' days' ) ); if ( $w && $w[0] > $now ) { return array( 'open' => false, 'state' => 'closed', 'until' => null, 'next' => $w[0] ); } } return array( 'open' => false, 'state' => 'closed', 'until' => null, 'next' => null ); } /** * Whether pickup orders are accepted now. * * @return bool */ function sofrah_board_ordering_open() { return sofrah_board_ordering_state()['open']; } /** * One sentence about ordering, for diners. * * @return string */ function sofrah_board_ordering_message() { $s = sofrah_board_ordering_state(); $fmt = get_option( 'time_format' ); if ( 'paused' === $s['state'] ) { return __( 'Ordering is paused right now. Please check back shortly.', 'sofrah-order-board' ); } if ( $s['open'] && ! $s['until'] ) { return __( 'Taking pickup orders', 'sofrah-order-board' ); } if ( $s['open'] ) { /* translators: %s: time */ return sprintf( __( 'Taking pickup orders until %s', 'sofrah-order-board' ), wp_date( $fmt, $s['until']->getTimestamp() ) ); } if ( $s['next'] ) { $today = ( new DateTimeImmutable( 'now', wp_timezone() ) )->format( 'Y-m-d' ); $tomorrow = ( new DateTimeImmutable( 'tomorrow', wp_timezone() ) )->format( 'Y-m-d' ); $day = $s['next']->format( 'Y-m-d' ); $time = wp_date( $fmt, $s['next']->getTimestamp() ); if ( $day === $today ) { /* translators: %s: time */ return sprintf( __( 'Ordering is closed. Opens today at %s.', 'sofrah-order-board' ), $time ); } if ( $day === $tomorrow ) { /* translators: %s: time */ return sprintf( __( 'Ordering is closed. Opens tomorrow at %s.', 'sofrah-order-board' ), $time ); } /* translators: 1: weekday, 2: time */ return sprintf( __( 'Ordering is closed. Opens %1$s at %2$s.', 'sofrah-order-board' ), wp_date( 'l', $s['next']->getTimestamp() ), $time ); } return __( 'Ordering is closed.', 'sofrah-order-board' ); } /** * Whether a REST request places a block checkout order. WordPress matches routes without regard to case, and the * Store API answers on both wc/store/v1 and wc/store, so every spelling of the checkout route counts. * * @param WP_REST_Request $request Request. * @return bool */ function sofrah_board_is_store_checkout( $request ) { return 'POST' === strtoupper( $request->get_method() ) && 1 === preg_match( '#^/wc/store(?:/v1)?/checkout/?$#i', $request->get_route() ); } // The server refuses pickup orders while ordering is closed or paused: block checkout (Store API) and classic checkout. add_filter( 'rest_pre_dispatch', function ( $result, $server, $request ) { if ( null !== $result || ! sofrah_board_is_store_checkout( $request ) ) { return $result; } if ( sofrah_board_is_pickup_method( (string) $request->get_param( 'payment_method' ) ) && ! sofrah_board_ordering_open() ) { return new WP_Error( 'sofrah_board_ordering_closed', sofrah_board_ordering_message(), array( 'status' => 409 ) ); } return $result; }, 4, 3 ); // The same check inside WooCommerce's own checkout step, so it holds whatever route the request came in on. add_action( 'woocommerce_store_api_checkout_update_order_from_request', function ( $order, $request ) { if ( sofrah_board_is_pickup_method( (string) $request->get_param( 'payment_method' ) ) && ! sofrah_board_ordering_open() ) { throw new \Automattic\WooCommerce\StoreApi\Exceptions\RouteException( 'sofrah_board_ordering_closed', esc_html( sofrah_board_ordering_message() ), 409 ); } }, 10, 2 ); add_action( 'woocommerce_after_checkout_validation', function ( $data, $errors ) { if ( sofrah_board_is_pickup_method( $data['payment_method'] ?? '' ) && ! sofrah_board_ordering_open() ) { $errors->add( 'sofrah_board_ordering_closed', sofrah_board_ordering_message() ); } }, 10, 2 ); // A notice above the cart and checkout while ordering is closed. foreach ( array( 'woocommerce/cart', 'woocommerce/checkout' ) as $sofrah_board_block ) { add_filter( 'render_block_' . $sofrah_board_block, function ( $html ) { if ( sofrah_board_ordering_open() ) { return $html; } return '
' . esc_html( sofrah_board_ordering_message() ) . '
'; } ); add_shortcode( 'sofrah_venue', function ( $atts ) { $atts = shortcode_atts( array( 'field' => 'address' ), $atts ); $v = sofrah_board_venue(); if ( 'name' === $atts['field'] ) { return esc_html( get_bloginfo( 'name' ) ); } if ( 'phone' === $atts['field'] && $v['phone'] ) { return '' . esc_html( $v['phone'] ) . ''; } if ( 'street' === $atts['field'] ) { $lines = preg_split( '/\R/', (string) $v['address'] ); return esc_html( trim( $lines[0] ) ); } return 'address' === $atts['field'] ? nl2br( esc_html( $v['address'] ) ) : ''; } ); add_shortcode( 'sofrah_hours', function () { $v = sofrah_board_venue(); $names = array( __( 'Sunday', 'sofrah-order-board' ), __( 'Monday', 'sofrah-order-board' ), __( 'Tuesday', 'sofrah-order-board' ), __( 'Wednesday', 'sofrah-order-board' ), __( 'Thursday', 'sofrah-order-board' ), __( 'Friday', 'sofrah-order-board' ), __( 'Saturday', 'sofrah-order-board' ) ); $start = (int) get_option( 'start_of_week', 1 ); $today = (int) wp_date( 'w' ); $rows = ''; for ( $i = 0; $i < 7; $i++ ) { $d = ( $start + $i ) % 7; $h = $v['hours'][ $d ]; $open = sofrah_board_minutes( $h['open'] ); $close = sofrah_board_minutes( $h['close'] ); $text = ( null === $open || null === $close ) ? __( 'Closed', 'sofrah-order-board' ) : sofrah_board_format_hm( $h['open'] ) . '–' . sofrah_board_format_hm( $h['close'] ); $rows .= '' . esc_html( sprintf( __( 'Online orders close %d minutes before we do.', 'sofrah-order-board' ), (int) $v['last_order'] ) ) . '
' : ''; return '