Order board. Open orders by stage, waiting orders first, refreshed every few seconds.
*
* @package SofrahOrderBoard
*/
defined( 'ABSPATH' ) || exit;
add_action(
'admin_menu',
function () {
$hook = add_submenu_page(
'woocommerce',
__( 'Order board', 'sofrah-order-board' ),
__( 'Order board', 'sofrah-order-board' ),
'edit_shop_orders',
'sob-board',
'sofrah_board_board_page'
);
add_action(
'admin_enqueue_scripts',
function ( $current ) use ( $hook ) {
if ( $current !== $hook ) {
return;
}
wp_enqueue_style( 'sob-board', SOFRAH_BOARD_URL . 'assets/board.css', array(), SOFRAH_BOARD_VERSION );
wp_enqueue_script( 'sob-board', SOFRAH_BOARD_URL . 'assets/board.js', array( 'wp-api-fetch', 'wp-i18n' ), SOFRAH_BOARD_VERSION, true );
wp_set_script_translations( 'sob-board', 'sofrah-order-board' );
wp_add_inline_script(
'sob-board',
'var sofrahBoard = ' . wp_json_encode(
array(
'path' => '/sofrah-order-board/v1',
'refresh' => (int) sofrah_board_setting( 'refresh' ),
'sound' => 'yes' === sofrah_board_setting( 'sound' ),
'minutes' => (int) sofrah_board_setting( 'lead_minutes' ),
'canCancel' => current_user_can( 'manage_woocommerce' ),
'initial' => sofrah_board_board_data(),
)
) . ';',
'before'
);
}
);
},
55
);
/**
* The board screen. The cards are drawn by board.js from the data above.
*/
function sofrah_board_board_page() {
update_option( 'sofrah_board_board_seen', time(), false );
?>
100,
'orderby' => 'date',
'order' => 'ASC',
'meta_key' => '_sofrah_board_status', // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_key -- meta_key/value/compare work with both order storage modes.
'meta_value' => SOFRAH_BOARD_OPEN, // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_value
'meta_compare' => 'IN',
)
);
$closed = wc_get_orders(
array(
'limit' => 30,
'orderby' => 'modified',
'order' => 'DESC',
'date_modified' => '>' . ( time() - 12 * HOUR_IN_SECONDS ),
'meta_key' => '_sofrah_board_status', // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_key
'meta_value' => SOFRAH_BOARD_OPEN, // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_value
'meta_compare' => 'NOT IN',
)
);
$state = sofrah_board_ordering_state();
return array(
'now' => time(),
'ordering' => array(
'open' => $state['open'],
'paused' => 'paused' === $state['state'],
'message' => sofrah_board_ordering_message(),
),
'orders' => array_map( 'sofrah_board_board_order', array_merge( $open, $closed ) ),
);
}
/**
* One order as the board shows it.
*
* @param WC_Order $order Order.
* @return array
*/
function sofrah_board_board_order( WC_Order $order ) {
$status = sofrah_board_get_status( $order );
$items = array();
foreach ( $order->get_items() as $item ) {
$meta = array();
foreach ( $item->get_formatted_meta_data() as $m ) {
$meta[] = html_entity_decode( wp_strip_all_tags( $m->display_key . ': ' . $m->display_value ), ENT_QUOTES );
}
$items[] = array(
'qty' => $item->get_quantity(),
'name' => $item->get_name(),
'meta' => $meta,
);
}
$history = array();
foreach ( (array) $order->get_meta( '_sofrah_board_history' ) as $h ) {
if ( ! is_array( $h ) ) {
continue;
}
$history[] = isset( $h['refused'] )
/* translators: 1: time, 2: who, 3: what was refused, 4: route */
? sprintf( __( '%1$s %2$s: refused %3$s (%4$s)', 'sofrah-order-board' ), wp_date( get_option( 'time_format' ), $h['at'] ), $h['by'], $h['refused'], $h['route'] )
/* translators: 1: time, 2: who, 3: status, 4: reason */
: trim( sprintf( __( '%1$s %2$s: %3$s %4$s', 'sofrah-order-board' ), wp_date( get_option( 'time_format' ), $h['at'] ), $h['by'], sofrah_board_status_label( $h['to'] ), $h['reason'] ? '(' . $h['reason'] . ')' : '' ) );
}
$check = array();
if ( sofrah_board_expected_wc_status( $status ) !== $order->get_status() ) {
/* translators: 1: WooCommerce status shown, 2: expected status */
$check[] = sprintf( __( 'WooCommerce shows "%1$s" but the board expects "%2$s". It was changed outside the board; tell the manager.', 'sofrah-order-board' ), wc_get_order_status_name( $order->get_status() ), wc_get_order_status_name( sofrah_board_expected_wc_status( $status ) ) );
}
if ( $order->get_date_paid( 'edit' ) && ! $order->get_meta( '_sofrah_board_paid_at' ) ) {
$check[] = __( 'WooCommerce shows a paid date the board did not record.', 'sofrah-order-board' );
}
$actions = array();
foreach ( SOFRAH_BOARD_ALLOWED[ $status ] ?? array() as $to ) {
if ( 'cancelled_diner' === $to ) {
continue;
}
$actions[] = array(
'to' => $to,
'label' => sofrah_board_action_label( $to ),
'reason' => in_array( $to, SOFRAH_BOARD_NEEDS_REASON, true ),
'minutes' => 'accepted' === $to,
);
}
return array(
'id' => $order->get_id(),
'number' => $order->get_order_number(),
'status' => $status,
'label' => sofrah_board_status_label( $status ),
'open' => in_array( $status, SOFRAH_BOARD_OPEN, true ),
'placed' => (int) $order->get_meta( '_sofrah_board_placed_at' ),
'pickup' => (int) $order->get_meta( '_sofrah_board_pickup_at' ),
'name' => trim( $order->get_billing_first_name() . ' ' . $order->get_billing_last_name() ),
'phone' => $order->get_billing_phone(),
'total' => html_entity_decode( wp_strip_all_tags( $order->get_formatted_order_total() ), ENT_QUOTES ),
'paid' => (bool) $order->get_meta( '_sofrah_board_paid_at' ),
'reason' => (string) $order->get_meta( '_sofrah_board_reason' ),
'note' => (string) $order->get_customer_note(),
'items' => $items,
'history' => $history,
'check' => $check,
'actions' => $actions,
'edit' => $order->get_edit_order_url(),
);
}