diff --git a/includes/API/Orders_Controller.php b/includes/API/Orders_Controller.php index 4a13eff..5b24ef7 100644 --- a/includes/API/Orders_Controller.php +++ b/includes/API/Orders_Controller.php @@ -14,12 +14,13 @@ use WC_Order_Query; use WC_REST_Orders_Controller; use WCPOS\WooCommercePOS\Logger; -use const WCPOS\WooCommercePOS\PLUGIN_NAME; use WP_REST_Request; use WP_REST_Response; use WP_REST_Server; use Automattic\WooCommerce\Utilities\OrderUtil; +use const WCPOS\WooCommercePOS\PLUGIN_NAME; + /** * Orders controller class. * @@ -55,7 +56,55 @@ public function __construct() { } /** - * Add custom fields to the product schema. + * Register routes. + */ + public function register_routes() { + parent::register_routes(); + + register_rest_route( + $this->namespace, + '/' . $this->rest_base . '/(?P[\d]+)/email', + array( + array( + 'methods' => WP_REST_Server::CREATABLE, + 'callback' => array( $this, 'wcpos_send_email' ), + 'permission_callback' => array( $this, 'wcpos_send_email_permissions_check' ), + 'args' => array_merge( + $this->get_endpoint_args_for_item_schema( WP_REST_Server::CREATABLE ), + array( + 'email' => array( + 'type' => 'string', + 'description' => __( 'Email address', 'woocommerce-pos' ), + 'required' => true, + ), + 'save_to' => array( + 'type' => 'string', + 'description' => __( 'Save email to order', 'woocommerce-pos' ), + 'required' => false, + ), + ) + ), + ), + 'schema' => array(), + ) + ); + + register_rest_route( + $this->namespace, + '/' . $this->rest_base . '/statuses', + array( + array( + 'methods' => WP_REST_Server::READABLE, + 'callback' => array( $this, 'wcpos_get_order_statuses' ), + 'permission_callback' => array( $this, 'get_item_permissions_check' ), + ), + 'schema' => array( $this, 'wcpos_get_public_order_statuses_schema' ), + ) + ); + } + + /** + * Add custom fields to the order schema. */ public function get_item_schema() { $schema = parent::get_item_schema(); @@ -230,41 +279,6 @@ public function get_collection_params() { return $params; } - /** - * Add route for sending emails. - */ - public function register_routes(): void { - parent::register_routes(); - - register_rest_route( - $this->namespace, - '/orders/(?P[\d]+)/email', - array( - array( - 'methods' => WP_REST_Server::CREATABLE, - 'callback' => array( $this, 'wcpos_send_email' ), - 'permission_callback' => array( $this, 'wcpos_send_email_permissions_check' ), - 'args' => array_merge( - $this->get_endpoint_args_for_item_schema( WP_REST_Server::CREATABLE ), - array( - 'email' => array( - 'type' => 'string', - 'description' => __( 'Email address', 'woocommerce-pos' ), - 'required' => true, - ), - 'save_to' => array( - 'type' => 'string', - 'description' => __( 'Save email to order', 'woocommerce-pos' ), - 'required' => false, - ), - ) - ), - ), - 'schema' => array(), - ) - ); - } - /** * Send order email, optionally add email address. * @@ -350,6 +364,53 @@ public function wcpos_dispatch_request( $dispatch_result, WP_REST_Request $reque return $dispatch_result; } + /** + * + */ + public function wcpos_get_order_statuses() { + $statuses = wc_get_order_statuses(); + $formatted_statuses = array(); + + foreach ( $statuses as $status_key => $status_name ) { + // Remove the 'wc-' prefix from the status key + $status_id = 'wc-' === substr( $status_key, 0, 3 ) ? substr( $status_key, 3 ) : $status_key; + + $formatted_statuses[] = array( + 'id' => $status_id, + 'name' => $status_name, + ); + } + + return rest_ensure_response( $formatted_statuses ); + } + + /** + * + */ + public function wcpos_get_public_order_statuses_schema() { + $schema = array( + '$schema' => 'http://json-schema.org/draft-04/schema#', + 'title' => 'order_status', + 'type' => 'object', + 'properties' => array( + 'id' => array( + 'description' => __( 'Unique identifier for the order status.', 'woocommerce-pos-pro' ), + 'type' => 'string', + 'context' => array( 'view', 'edit' ), + 'readonly' => true, + ), + 'name' => array( + 'description' => __( 'Display name of the order status.', 'woocommerce-pos-pro' ), + 'type' => 'string', + 'context' => array( 'view', 'edit' ), + 'readonly' => true, + ), + ), + ); + + return $schema; + } + /** * Register hooks to modify WC REST API response. * diff --git a/includes/Templates/Frontend.php b/includes/Templates/Frontend.php index bbcfb04..ba010fe 100644 --- a/includes/Templates/Frontend.php +++ b/includes/Templates/Frontend.php @@ -43,8 +43,17 @@ public function get_template(): void { // disable cache plugins $this->no_cache(); - // last chance before template is rendered - do_action( 'woocommerce_pos_template_redirect' ); + // last chance before frontend template is rendered + do_action( 'woocommerce_pos_frontend_template_redirect' ); + + /** + * Deprecated action. + * + * @TODO remove in 1.5.0 + */ + if ( has_action( 'woocommerce_pos_template_redirect' ) ) { + do_action_deprecated( 'woocommerce_pos_template_redirect', array(), 'Version_1.4.0', 'woocommerce_pos_frontend_template_redirect' ); + } // add head & footer actions add_action( 'woocommerce_pos_head', array( $this, 'head' ) ); @@ -119,6 +128,7 @@ function ( $store ) { ), 'wp_credentials' => $auth_service->get_user_data( $user ), 'stores' => $stores, + 'store_id' => isset( $_GET['store'] ) ? $_GET['store'] : null, ); /** diff --git a/includes/Templates/Login.php b/includes/Templates/Login.php index 02aa87f..51de265 100644 --- a/includes/Templates/Login.php +++ b/includes/Templates/Login.php @@ -1,6 +1,9 @@ assertArrayHasKey( '/wcpos/v1/orders/(?P[\d]+)/email', $routes ); + $this->assertArrayHasKey( '/wcpos/v1/orders/statuses', $routes ); } /** @@ -584,4 +585,30 @@ public function test_order_save_line_item_with_null_parent_name(): void { $this->assertEquals( 201, $response->get_status() ); $this->assertEquals( 'woocommerce-pos', $data['created_via'] ); } + + /** + * Retrieve all order statuses. + */ + public function test_get_order_statuses(): void { + $request = $this->wp_rest_get_request( '/wcpos/v1/orders/statuses' ); + $response = $this->server->dispatch( $request ); + $data = $response->get_data(); + + $this->assertEquals( 200, $response->get_status() ); + $this->assertNotEmpty( $data ); + + // Ensure that the response is an array + $this->assertIsArray( $data ); + + // Check if each element in the array has the required structure + foreach ( $data as $status ) { + $this->assertIsArray( $status ); + $this->assertArrayHasKey( 'id', $status ); + $this->assertArrayHasKey( 'name', $status ); + + // Check if the 'id' and 'name' fields are strings + $this->assertIsString( $status['id'] ); + $this->assertIsString( $status['name'] ); + } + } }