Skip to content

Commit 82ddd9a

Browse files
committed
add permissions check that inherits WC auth processes
1 parent d078418 commit 82ddd9a

File tree

4 files changed

+56
-154
lines changed

4 files changed

+56
-154
lines changed

src/Rest/Endpoints/AbandonedCarts.php

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
use WP_REST_Request;
1414
use WP_REST_Controller;
1515
use WP_REST_Response;
16+
use WP_Error;
1617
use WC_Product;
1718

1819
use WebDevStudios\CCForWoo\AbandonedCarts\CartsTable;
@@ -55,17 +56,33 @@ public function __construct() {
5556
*/
5657
public function register_routes() {
5758
register_rest_route(
58-
'wc-' . $this->rest_base,
59+
Registrar::$namespace,
60+
'/' . $this->rest_base,
5961
[
6062
[
61-
'methods' => WP_REST_Server::READABLE,
62-
'callback' => [ $this, 'get_items' ],
63+
'methods' => WP_REST_Server::READABLE,
64+
'callback' => [ $this, 'get_items' ],
65+
'permission_callback' => [ $this, 'get_items_permissions_check' ],
6366
],
6467
'schema' => null,
6568
]
6669
);
6770
}
6871

72+
/**
73+
* Check whether a given request has permission to show abandoned carts.
74+
*
75+
* @param WP_REST_Request $request Full details about the request.
76+
* @return WP_Error|boolean
77+
*/
78+
public function get_items_permissions_check( $request ) {
79+
if ( ! wc_rest_check_manager_permissions( 'settings', 'read' ) ) {
80+
return new WP_Error( 'cc-woo-rest-not-allowed', esc_html__( 'Sorry, you cannot list resources.', 'cc-woo' ), [ 'status' => rest_authorization_required_code() ] );
81+
}
82+
83+
return true;
84+
}
85+
6986
/**
7087
* Register the Abandoned Carts endpoint.
7188
*
@@ -188,7 +205,7 @@ private function get_cart_data( int $per_page, int $offset, string $date_min, st
188205
);
189206
// phpcs:enable WordPress.DB.PreparedSQL
190207

191-
return $this->prepare_cart_data_for_api( $data );
208+
return $this->prepare_cart_data_for_api_response( $data );
192209
}
193210

194211
/**
@@ -230,7 +247,7 @@ private function get_dates_where( string $date_min, string $date_max ) : string
230247
* @param array $data The carts whose fields need preparation.
231248
* @return array
232249
*/
233-
private function prepare_cart_data_for_api( array $data ) {
250+
private function prepare_cart_data_for_api_response( array $data ) {
234251
foreach ( $data as $cart ) {
235252
$cart->cart_contents = maybe_unserialize( $cart->cart_contents );
236253
$cart->cart_contents = $this->get_additional_product_fields( $cart->cart_contents );

src/Rest/Registrar.php

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,25 @@
2020
*/
2121
class Registrar extends Service {
2222

23+
/**
24+
* Namespace for the endpoints this registrar registers.
25+
*
26+
* @since 2019-10-16
27+
*
28+
* @var string
29+
*/
30+
public static $namespace = 'wc/cc-woo';
31+
2332
/**
2433
* Register hooks.
2534
*
2635
* @author George Gecewicz <[email protected]>
2736
* @since 2019-11-13
2837
*/
2938
public function register_hooks() {
30-
add_action( 'rest_api_init', [ $this, 'init_rest_endpoints' ] );
31-
}
39+
add_action( 'rest_api_init', [ $this, 'init_rest_endpoints' ] );
40+
add_filter( 'woocommerce_rest_is_request_to_rest_api', [ $this, 'register_endpoints_with_woo_auth_handler' ] );
41+
}
3242

3343
/**
3444
* Initialize REST endpoints.
@@ -38,7 +48,27 @@ public function register_hooks() {
3848
*/
3949
public function init_rest_endpoints() {
4050
( new Endpoints\AbandonedCarts() )->register_routes();
41-
}
51+
}
52+
53+
/**
54+
* Register REST endpoints with WooCommerce's REST auth handler.
55+
*
56+
* @author George Gecewicz <[email protected]>
57+
* @since 2019-11-13
58+
*
59+
* @return bool
60+
*/
61+
public function register_endpoints_with_woo_auth_handler() {
62+
$request_uri = esc_url_raw( wp_unslash( $_SERVER['REQUEST_URI'] ) );
63+
64+
if ( empty( $request_uri ) ) {
65+
return false;
66+
}
67+
68+
$rest_prefix = trailingslashit( rest_get_url_prefix() );
69+
70+
return false !== strpos( $request_uri, $rest_prefix . self::$namespace );
71+
}
4272

4373
}
4474

src/View/Admin/Field/AbandonedCarts/RestEndpoints.php

Lines changed: 0 additions & 100 deletions
This file was deleted.

src/View/Admin/WooTab.php

Lines changed: 1 addition & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -124,14 +124,6 @@ class WooTab extends WC_Settings_Page implements Hookable {
124124
*/
125125
private $import_existing_customer_section = 'customer_data_import';
126126

127-
/**
128-
* The identifier for the Abandoned Carts section.
129-
*
130-
* @since 2019-10-24
131-
* @var string
132-
*/
133-
private $abandoned_carts_section = 'abandoned_carts';
134-
135127
/**
136128
* WooTab constructor.
137129
*
@@ -207,7 +199,6 @@ public function get_sections() {
207199
$sections = [
208200
'' => esc_html__( 'Store Information', 'cc-woo' ),
209201
$this->import_existing_customer_section => esc_html__( 'Import your contacts', 'cc-woo' ),
210-
$this->abandoned_carts_section => esc_html__( 'Abandoned Carts', 'cc-woo' ),
211202
];
212203

213204
return apply_filters( 'woocommerce_get_sections_' . $this->id, $sections );
@@ -280,10 +271,6 @@ private function get_default_settings_options() {
280271
case $this->import_existing_customer_section:
281272
$settings = $this->get_customer_data_settings();
282273
break;
283-
284-
case $this->abandoned_carts_section:
285-
$settings = $this->get_abandoned_carts_settings();
286-
break;
287274
}
288275

289276
$settings = $this->process_errors( $settings );
@@ -306,8 +293,7 @@ public function add_rest_group( $groups ) {
306293
$groups[] = [
307294
'id' => 'cc_woo',
308295
'label' => esc_html__( 'Constant Contact WooCommerce', 'cc-woo' ),
309-
'description' => esc_html__( 'This endpoint provides information for the Constant Contact for WooCommerce plugin.',
310-
'cc-woo' ),
296+
'description' => esc_html__( 'This endpoint provides information for the Constant Contact for WooCommerce plugin.', 'cc-woo' ),
311297
];
312298

313299
return $groups;
@@ -546,37 +532,6 @@ private function get_customer_data_settings() {
546532
return $settings;
547533
}
548534

549-
/**
550-
* Get the Abandoned Carts settings.
551-
*
552-
* @since 2019-10-24
553-
* @author George Gecewicz <[email protected]>
554-
*
555-
* @return array
556-
*/
557-
private function get_abandoned_carts_settings() {
558-
$settings = [
559-
[
560-
'title' => esc_html__( 'Abandoned Cart Settings', 'cc-woo' ),
561-
'id' => 'cc_woo_abandoned_cart_settings',
562-
'type' => 'title',
563-
],
564-
[
565-
'title' => '',
566-
'type' => 'title',
567-
'desc' => esc_html__( 'Settings for the Abandoned Carts functionality, namely its REST API endpoint.', 'cc-woo' ),
568-
],
569-
];
570-
571-
$rest_endpoints_field = new \WebDevStudios\CCForWoo\View\Admin\Field\AbandonedCarts\RestEndpoints();
572-
573-
$settings[] = array_merge( $settings,
574-
$rest_endpoints_field->get_form_field()
575-
);
576-
577-
return $settings;
578-
}
579-
580535
/**
581536
* Displays the Constant Contact connection button when the form is validated and a connection is not already established.
582537
*

0 commit comments

Comments
 (0)