diff --git a/includes/API/Stores.php b/includes/API/Stores.php index 500f506..d78e0d5 100644 --- a/includes/API/Stores.php +++ b/includes/API/Stores.php @@ -54,19 +54,17 @@ public function register_routes(): void { */ public function get_items( $request ) { try { - $store = new Store(); - - // Check if store data is available - if ( ! $store ) { - return new \WP_Error( - 'woocommerce_pos_store_not_found', - esc_html__( 'Store not found', 'woocommerce-pos' ), - array( 'status' => 404 ) - ); + $stores = wcpos_get_stores(); + + $response = array(); + foreach ( $stores as $store ) { + $data = $this->prepare_item_for_response( $store, $request ); + $response[] = $this->prepare_response_for_collection( $data ); } - $data = $store->get_data(); - $response = rest_ensure_response( array( $data ) ); + $response = rest_ensure_response( $response ); + $response->header( 'X-WP-Total', count( $stores ) ); + $response->header( 'X-WP-TotalPages', 1 ); return $response; @@ -79,6 +77,31 @@ public function get_items( $request ) { } } + /** + * Prepare a single product output for response. + * + * @param Store $store Store object. + * @param WP_REST_Request $request Request object. + * @return WP_REST_Response + */ + public function prepare_item_for_response( $store, $request ) { + $data = $store->get_data(); + $response = rest_ensure_response( $data ); + $response->add_links( $this->prepare_links( $store, $request ) ); + + /** + * Filter the data for a response. + * + * The dynamic portion of the hook name, $this->post_type, refers to post_type of the post being + * prepared for the response. + * + * @param WP_REST_Response $response The response object. + * @param Store $store Store object. + * @param WP_REST_Request $request Request object. + */ + return apply_filters( 'woocommerce_pos_rest_prepare_store', $response, $store, $request ); + } + /** * Check if the user is logged in. * @@ -95,4 +118,24 @@ public function check_permissions() { return true; } + + /** + * Prepare links for the request. + * + * @param WC_Product $product Product object. + * @param WP_REST_Request $request Request object. + * @return array Links for the given product. + */ + protected function prepare_links( $store, $request ) { + $links = array( + 'self' => array( + 'href' => rest_url( sprintf( '/%s/%s/%d', $this->namespace, $this->rest_base, $store->get_id() ) ), + ), + 'collection' => array( + 'href' => rest_url( sprintf( '/%s/%s', $this->namespace, $this->rest_base ) ), + ), + ); + + return $links; + } } diff --git a/includes/Init.php b/includes/Init.php index 8698e92..af5ac4b 100644 --- a/includes/Init.php +++ b/includes/Init.php @@ -23,6 +23,7 @@ class Init { public function __construct() { // global helper functions require_once PLUGIN_PATH . 'includes/wcpos-functions.php'; + require_once PLUGIN_PATH . 'includes/wcpos-store-functions.php'; // Init hooks add_action( 'init', array( $this, 'init' ) ); diff --git a/includes/Services/Store.php b/includes/Services/Store.php index 6f974af..0488cfe 100644 --- a/includes/Services/Store.php +++ b/includes/Services/Store.php @@ -14,7 +14,7 @@ class Store extends WC_Data { * * @var string */ - protected $object_type = 'product'; + protected $object_type = 'store'; /** * Stores product data. diff --git a/includes/Templates/Frontend.php b/includes/Templates/Frontend.php index 33026f6..625525d 100644 --- a/includes/Templates/Frontend.php +++ b/includes/Templates/Frontend.php @@ -75,8 +75,13 @@ public function footer(): void { $development = isset( $_ENV['DEVELOPMENT'] ) && $_ENV['DEVELOPMENT']; $user = wp_get_current_user(); $github_url = 'https://wcpos.github.io/managed-expo/'; - $store = new Store(); $auth_service = new Auth(); + $stores = array_map( + function ( $store ) { + return $store->get_data(); + }, + wcpos_get_stores() + ); $site_uuid = get_option( 'woocommerce_pos_uuid' ); if ( ! $site_uuid ) { @@ -110,7 +115,7 @@ public function footer(): void { 'use_jwt_as_param' => woocommerce_pos_get_settings( 'tools', 'use_jwt_as_param' ), ), 'wp_credentials' => $auth_service->get_user_data( $user ), - 'stores' => array( $store->get_data() ), + 'stores' => $stores, ); /** diff --git a/includes/wcpos-store-functions.php b/includes/wcpos-store-functions.php new file mode 100644 index 0000000..fc88c60 --- /dev/null +++ b/includes/wcpos-store-functions.php @@ -0,0 +1,47 @@ +