get_meta( '_sofrah_board_history' ); $history[] = array( 'at' => time(), 'by' => $user->exists() ? $user->user_login : sofrah_board_route(), 'refused' => $what, 'route' => sofrah_board_route(), ); $order->update_meta_data( '_sofrah_board_history', $history ); if ( $save ) { $order->save_meta_data(); } $GLOBALS['sofrah_board_refusals'][] = array( 'order' => $order->get_id(), 'what' => $what, 'kind' => $kind, ); } // The notice is shown on the next admin screen, since most refusals end in a redirect. add_action( 'shutdown', function () { if ( empty( $GLOBALS['sofrah_board_refusals'] ) || ! get_current_user_id() || ( defined( 'REST_REQUEST' ) && REST_REQUEST ) ) { return; } set_transient( 'sofrah_board_refused_' . get_current_user_id(), $GLOBALS['sofrah_board_refusals'], 5 * MINUTE_IN_SECONDS ); }, 1 ); add_action( 'admin_notices', function () { $key = 'sofrah_board_refused_' . get_current_user_id(); $rows = get_transient( $key ); if ( ! $rows ) { return; } delete_transient( $key ); echo '

' . esc_html__( 'Refused: these orders change only on the order board.', 'sofrah-order-board' ) . ' ' . esc_html__( 'Open the order board', 'sofrah-order-board' ) . '

'; } ); // WooCommerce reports "N orders changed" for every selected order; take out the refused ones. add_filter( 'wp_redirect', function ( $location ) { if ( empty( $GLOBALS['sofrah_board_refusals'] ) ) { return $location; } $refused = count( array_unique( array_column( array_filter( $GLOBALS['sofrah_board_refusals'], fn( $r ) => 'status' === $r['kind'] ), 'order' ) ) ); $q = array(); wp_parse_str( (string) wp_parse_url( $location, PHP_URL_QUERY ), $q ); if ( isset( $q['changed'] ) && $refused ) { $left = max( 0, (int) $q['changed'] - $refused ); $location = $left ? add_query_arg( 'changed', $left, $location ) : remove_query_arg( array( 'bulk_action', 'changed', 'ids' ), $location ); } if ( isset( $q['message'], $q['action'] ) && 'edit' === $q['action'] ) { $location = remove_query_arg( 'message', $location ); // Not "Order updated." when it was not. } return $location; } ); // Before any save, from any route: undo a status change the board did not make, and keep WooCommerce's paid date in step with the board. add_action( 'woocommerce_before_order_object_save', function ( $order ) { if ( ! sofrah_board_is_board_order( $order ) || ! empty( $GLOBALS['sofrah_board_in_transition'] ) ) { return; } $changes = $order->get_changes(); $data = $order->get_data(); if ( isset( $changes['status'] ) && $changes['status'] !== $data['status'] ) { $order->set_status( $data['status'], false ); // false: no "status changed" note for a change that did not happen. if ( array_key_exists( 'date_completed', $changes ) ) { $order->set_date_completed( $data['date_completed'] ); } /* translators: 1: old WooCommerce status, 2: requested WooCommerce status */ sofrah_board_refused( $order, sprintf( __( 'status change from %1$s to %2$s', 'sofrah-order-board' ), wc_get_order_status_name( $data['status'] ), wc_get_order_status_name( $changes['status'] ) ), 'status' ); } if ( ! $order->get_meta( '_sofrah_board_paid_at' ) ) { $what = array(); if ( $order->get_date_paid( 'edit' ) ) { $order->set_date_paid( null ); $what[] = __( 'paid date', 'sofrah-order-board' ); } if ( '' !== (string) $order->get_transaction_id( 'edit' ) ) { $order->set_transaction_id( '' ); $what[] = __( 'payment reference', 'sofrah-order-board' ); } if ( $what && ! isset( $changes['status'] ) ) { sofrah_board_refused( $order, implode( ', ', $what ), 'payment' ); } } }, 1 ); // Inside the board's own changes WooCommerce may still set a paid date on completion; it stays only if payment was recorded. add_action( 'woocommerce_before_order_object_save', function ( $order ) { if ( sofrah_board_is_board_order( $order ) && ! empty( $GLOBALS['sofrah_board_in_transition'] ) && ! $order->get_meta( '_sofrah_board_paid_at' ) && $order->get_date_paid( 'edit' ) ) { $order->set_date_paid( null ); } }, 2 ); // "Paid" means what the board recorded. add_filter( 'woocommerce_order_is_paid', function ( $paid, $order ) { return sofrah_board_is_board_order( $order ) ? (bool) $order->get_meta( '_sofrah_board_paid_at' ) : $paid; }, 10, 2 ); // Refunds while open: refused with a message; the refund button is hidden. add_action( 'woocommerce_create_refund', function ( $refund ) { $order = wc_get_order( $refund->get_parent_id() ); if ( sofrah_board_is_open( $order ) ) { sofrah_board_refused( $order, __( 'refund', 'sofrah-order-board' ), 'refund', true ); throw new Exception( esc_html__( 'Refused: this order is still open. Cancel it on the order board instead.', 'sofrah-order-board' ) ); } }, 1 ); add_filter( 'woocommerce_admin_order_should_render_refunds', function ( $render, $order_id ) { return sofrah_board_is_open( wc_get_order( $order_id ) ) ? false : $render; }, 10, 2 ); // Trash and deletion while open: refused. add_filter( 'woocommerce_pre_delete_order', function ( $check, $order, $force ) { if ( sofrah_board_is_open( $order ) ) { sofrah_board_refused( $order, $force ? __( 'deletion', 'sofrah-order-board' ) : __( 'move to trash', 'sofrah-order-board' ), 'delete', true ); return false; } return $check; }, 10, 3 ); // WooCommerce's REST API, which its mobile app uses: a clear refusal instead of an apparent success. add_filter( 'rest_pre_dispatch', function ( $result, $server, $request ) { // Someone who may not edit orders gets WooCommerce's own refusal: the same answer for every order number, // and nothing written to the order. if ( null !== $result || ! current_user_can( 'edit_shop_orders' ) ) { return $result; } $route = $request->get_route(); $method = strtoupper( $request->get_method() ); $touches = fn( $p ) => isset( $p['status'] ) || ! empty( $p['set_paid'] ) || isset( $p['date_paid'] ) || isset( $p['date_paid_gmt'] ) || isset( $p['transaction_id'] ); $refuse = function ( $order, $what ) { sofrah_board_refused( $order, $what, 'rest', true ); return new WP_Error( 'sofrah_board_refused', __( 'Refused: the status and payment of this order change only on the order board.', 'sofrah-order-board' ), array( 'status' => 409, 'order' => $order->get_id() ) ); }; // Routes match without regard to case or a trailing slash, as WordPress matches them. if ( preg_match( '#^/wc/v[1-3]/orders/(\d+)/?$#i', $route, $m ) ) { $order = wc_get_order( (int) $m[1] ); if ( in_array( $method, array( 'PUT', 'POST', 'PATCH' ), true ) && sofrah_board_is_board_order( $order ) && $touches( $request->get_params() ) ) { return $refuse( $order, __( 'status or payment change through the REST API', 'sofrah-order-board' ) ); } if ( 'DELETE' === $method && sofrah_board_is_open( $order ) ) { return $refuse( $order, __( 'deletion through the REST API', 'sofrah-order-board' ) ); } } if ( 'POST' === $method && preg_match( '#^/wc/v[1-3]/orders/(\d+)/refunds/?$#i', $route, $m ) ) { $order = wc_get_order( (int) $m[1] ); if ( sofrah_board_is_open( $order ) ) { return $refuse( $order, __( 'refund through the REST API', 'sofrah-order-board' ) ); } } if ( 'POST' === $method && preg_match( '#^/wc/v[1-3]/orders/batch/?$#i', $route ) ) { foreach ( (array) $request->get_param( 'update' ) as $u ) { $order = isset( $u['id'] ) ? wc_get_order( (int) $u['id'] ) : null; if ( sofrah_board_is_board_order( $order ) && $touches( $u ) ) { return $refuse( $order, __( 'status or payment change through the REST API', 'sofrah-order-board' ) ); } } foreach ( (array) $request->get_param( 'delete' ) as $d ) { $order = wc_get_order( (int) $d ); if ( sofrah_board_is_open( $order ) ) { return $refuse( $order, __( 'deletion through the REST API', 'sofrah-order-board' ) ); } } } return $result; }, 20, 3 );