PluginProbe
WCPOS – Point of Sale (POS) plugin for WooCommerce / 1.10.17
WCPOS – Point of Sale (POS) plugin for WooCommerce v1.10.17
1.10.18 1.10.17 1.10.16 1.10.15 1.10.13 1.10.14 1.10.12 1.10.11 1.10.10 1.10.9 1.10.8 untagged-3d9b7ccddc54df87c672 1.10.7 1.10.6 1.10.5 1.10.3 1.10.4 1.10.2 1.10.1 1.10.0 1.9.17 1.9.15 1.9.16 1.9.14 1.9.13 All 162 releases
woocommerce-pos / includes / API / V1 / Stores.php

Stores.php in WCPOS – Point of Sale (POS) plugin for WooCommerce 1.10.17, at includes/API/V1/Stores.php

185 lines 4.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Stores API.
4 *
5 * @package WCPOS\WooCommercePOS
6 */
7
8 namespace WCPOS\WooCommercePOS\API\V1;
9
10 \defined( 'ABSPATH' ) || die;
11
12 if ( ! class_exists( 'WP_REST_Controller' ) ) {
13 return;
14 }
15
16 use Exception;
17 use WCPOS\WooCommercePOS\Abstracts\Store;
18 use const WCPOS\WooCommercePOS\SHORT_NAME;
19 use WP_Error;
20 use WP_REST_Controller;
21 use WP_REST_Request;
22 use WP_REST_Response;
23
24 /**
25 * Stores API.
26 */
27 class Stores extends WP_REST_Controller {
28 /**
29 * Endpoint namespace.
30 *
31 * @var string
32 */
33 protected $namespace = SHORT_NAME . '/v1';
34
35 /**
36 * Route base.
37 *
38 * @var string
39 */
40 protected $rest_base = 'stores';
41
42
43 /**
44 * Register the routes for the objects of the controller.
45 */
46 public function register_routes(): void {
47 register_rest_route(
48 $this->namespace,
49 '/' . $this->rest_base,
50 array(
51 'methods' => 'GET',
52 'callback' => array( $this, 'get_items' ),
53 'permission_callback' => array( $this, 'check_permissions' ),
54 )
55 );
56
57 register_rest_route(
58 $this->namespace,
59 '/' . $this->rest_base . '/(?P<id>[\d]+)',
60 array(
61 'methods' => 'GET',
62 'callback' => array( $this, 'get_item' ),
63 'permission_callback' => array( $this, 'check_permissions' ),
64 )
65 );
66 }
67
68 /**
69 * Retrieve store data.
70 *
71 * @param WP_REST_Request $request Full details about the request.
72 *
73 * @return WP_Error|WP_REST_Response Response object on success, or WP_Error object on failure.
74 */
75 public function get_items( $request ) {
76 try {
77 $stores = wcpos_get_stores();
78
79 $response = array();
80 foreach ( $stores as $store ) {
81 $data = $this->prepare_item_for_response( $store, $request );
82 $response[] = $this->prepare_response_for_collection( $data );
83 }
84
85 $response = rest_ensure_response( $response );
86 $response->header( 'X-WP-Total', (string) \count( $stores ) );
87 $response->header( 'X-WP-TotalPages', (string) 1 );
88
89 return $response;
90 } catch ( Exception $e ) {
91 return new \WP_Error(
92 'woocommerce_pos_store_retrieval_failed',
93 esc_html__( 'Failed to retrieve store data', 'woocommerce-pos' ),
94 array( 'status' => 500 )
95 );
96 }
97 }
98
99 /**
100 * Retrieve a single store.
101 *
102 * @param WP_REST_Request $request Full details about the request.
103 *
104 * @return WP_Error|WP_REST_Response Response object on success, or WP_Error object on failure.
105 */
106 public function get_item( $request ) {
107 $store = wcpos_get_store( $request['id'] );
108
109 if ( ! $store ) {
110 return new \WP_Error(
111 'woocommerce_pos_store_not_found',
112 /* translators: REST API schema field label or error message. */
113 esc_html__( 'Store not found', 'woocommerce-pos' ),
114 array( 'status' => 404 )
115 );
116 }
117
118 $data = $this->prepare_item_for_response( $store, $request );
119
120 return rest_ensure_response( $data );
121 }
122
123 /**
124 * Prepare a single product output for response.
125 *
126 * @param Store $store Store object.
127 * @param WP_REST_Request $request Request object.
128 *
129 * @return WP_REST_Response
130 */
131 public function prepare_item_for_response( $store, $request ) {
132 $data = $store->get_data();
133 $response = rest_ensure_response( $data );
134 $response->add_links( $this->prepare_links( $store, $request ) );
135
136 /*
137 * Filter the data for a response.
138 *
139 * The dynamic portion of the hook name, $this->post_type, refers to post_type of the post being
140 * prepared for the response.
141 *
142 * @param WP_REST_Response $response The response object.
143 * @param Store $store Store object.
144 * @param WP_REST_Request $request Request object.
145 */
146 return apply_filters( 'woocommerce_pos_rest_prepare_store', $response, $store, $request );
147 }
148
149 /**
150 * Check if the user is logged in.
151 *
152 * @return bool|WP_Error True if the user is logged in, WP_Error otherwise.
153 */
154 public function check_permissions() {
155 if ( ! is_user_logged_in() ) {
156 return new \WP_Error(
157 'woocommerce_pos_rest_forbidden',
158 esc_html__( 'You do not have permissions to view this data.', 'woocommerce-pos' ),
159 array( 'status' => rest_authorization_required_code() )
160 );
161 }
162
163 return true;
164 }
165
166 /**
167 * Prepare links for the request.
168 *
169 * @param Store $store Store object.
170 * @param WP_REST_Request $request Request object.
171 *
172 * @return array Links for the given store.
173 */
174 protected function prepare_links( $store, $request ) {
175 return array(
176 'self' => array(
177 'href' => rest_url( \sprintf( '/%s/%s/%d', $this->namespace, $this->rest_base, $store->get_id() ) ),
178 ),
179 'collection' => array(
180 'href' => rest_url( \sprintf( '/%s/%s', $this->namespace, $this->rest_base ) ),
181 ),
182 );
183 }
184 }
185