|
1 | 1 | <?php |
2 | 2 | /** |
3 | | - * REST API endpoint for collection of Abandoned Carts. |
| 3 | + * Controller for wc/cc-woo/abandoned-carts endpoint. |
4 | 4 | * |
5 | | - * @author George Gecewicz <[email protected]> |
6 | | - * @package WebDevStudios\CCForWoo\Rest\V1 |
| 5 | + * @package WebDevStudios\CCForWoo\Rest\AbandonedCarts |
7 | 6 | * @since 2019-10-16 |
8 | 7 | */ |
9 | 8 |
|
10 | | -namespace WebDevStudios\CCForWoo\Rest\V1\Endpoints; |
| 9 | +namespace WebDevStudios\CCForWoo\Rest\AbandonedCarts; |
11 | 10 |
|
12 | 11 | use WP_REST_Server; |
13 | 12 | use WP_REST_Request; |
14 | 13 | use WP_REST_Controller; |
15 | 14 | use WP_REST_Response; |
| 15 | +use WP_Error; |
16 | 16 | use WC_Product; |
17 | 17 |
|
18 | 18 | use WebDevStudios\CCForWoo\AbandonedCarts\CartsTable; |
19 | 19 | use WebDevStudios\CCForWoo\AbandonedCarts\Cart; |
20 | | -use WebDevStudios\CCForWoo\Rest\V1\Registrar; |
| 20 | +use WebDevStudios\CCForWoo\Rest\Registrar; |
21 | 21 |
|
22 | 22 | /** |
23 | | - * Class AbandonedCarts |
| 23 | + * Class AbandonedCarts\Controller |
24 | 24 | * |
25 | | - * @author George Gecewicz <[email protected]> |
26 | | - * @package WebDevStudios\CCForWoo\Rest\V1 |
| 25 | + * @package WebDevStudios\CCForWoo\Rest\AbandonedCarts |
27 | 26 | * @since 2019-10-16 |
28 | 27 | */ |
29 | | -class AbandonedCarts extends WP_REST_Controller { |
| 28 | +class Controller extends WP_REST_Controller { |
30 | 29 |
|
31 | 30 | /** |
32 | 31 | * This endpoint's rest base. |
@@ -55,17 +54,37 @@ public function __construct() { |
55 | 54 | */ |
56 | 55 | public function register_routes() { |
57 | 56 | register_rest_route( |
58 | | - Registrar::$namespace, '/' . $this->rest_base, |
| 57 | + Registrar::$namespace, |
| 58 | + '/' . $this->rest_base, |
59 | 59 | [ |
60 | 60 | [ |
61 | | - 'methods' => WP_REST_Server::READABLE, |
62 | | - 'callback' => [ $this, 'get_items' ], |
| 61 | + 'methods' => WP_REST_Server::READABLE, |
| 62 | + 'callback' => [ $this, 'get_items' ], |
| 63 | + 'permission_callback' => [ $this, 'get_items_permissions_check' ], |
| 64 | + 'args' => Schema::get_collection_params(), |
63 | 65 | ], |
64 | | - 'schema' => null, |
| 66 | + 'schema' => [ '\WebDevStudios\CCForWoo\Rest\AbandonedCarts\Schema', 'get_public_item_schema' ], |
65 | 67 | ] |
66 | 68 | ); |
67 | 69 | } |
68 | 70 |
|
| 71 | + /** |
| 72 | + * Check whether a given request has permission to show abandoned carts. |
| 73 | + * |
| 74 | + * @author George Gecewicz <[email protected]> |
| 75 | + * @since 2019-11-12 |
| 76 | + * |
| 77 | + * @param WP_REST_Request $request Full details about the request. |
| 78 | + * @return WP_Error|boolean |
| 79 | + */ |
| 80 | + public function get_items_permissions_check( $request ) { |
| 81 | + if ( ! wc_rest_check_manager_permissions( 'settings', 'read' ) ) { |
| 82 | + return new WP_Error( 'cc-woo-rest-not-allowed', esc_html__( 'Sorry, you cannot list resources.', 'cc-woo' ), [ 'status' => rest_authorization_required_code() ] ); |
| 83 | + } |
| 84 | + |
| 85 | + return true; |
| 86 | + } |
| 87 | + |
69 | 88 | /** |
70 | 89 | * Register the Abandoned Carts endpoint. |
71 | 90 | * |
@@ -188,7 +207,7 @@ private function get_cart_data( int $per_page, int $offset, string $date_min, st |
188 | 207 | ); |
189 | 208 | // phpcs:enable WordPress.DB.PreparedSQL |
190 | 209 |
|
191 | | - return $this->prepare_cart_data_for_api( $data ); |
| 210 | + return $this->prepare_cart_data_for_api_response( $data ); |
192 | 211 | } |
193 | 212 |
|
194 | 213 | /** |
@@ -230,7 +249,7 @@ private function get_dates_where( string $date_min, string $date_max ) : string |
230 | 249 | * @param array $data The carts whose fields need preparation. |
231 | 250 | * @return array |
232 | 251 | */ |
233 | | - private function prepare_cart_data_for_api( array $data ) { |
| 252 | + private function prepare_cart_data_for_api_response( array $data ) { |
234 | 253 | foreach ( $data as $cart ) { |
235 | 254 | $cart->cart_contents = maybe_unserialize( $cart->cart_contents ); |
236 | 255 | $cart->cart_contents = $this->get_additional_product_fields( $cart->cart_contents ); |
|
0 commit comments