Skip to content

Commit 50a902b

Browse files
authored
Merge pull request #75 from WebDevStudios/feature/CC-87-use-woocommerce-native-API
Use WooCommerce Native API Auth Instead of JWT
2 parents 4903a63 + 82ddd9a commit 50a902b

File tree

13 files changed

+236
-870
lines changed

13 files changed

+236
-870
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
**/.DS_Store
12
/vendor/*
23
/node_modules/*
34
.nvimrc

composer.lock

Lines changed: 135 additions & 131 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Ajax/GenerateSecretKey.php

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

src/Plugin.php

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,7 @@
2121
use WebDevStudios\CCForWoo\AbandonedCarts\CartHandler;
2222
use WebDevStudios\CCForWoo\AbandonedCarts\CartsTable;
2323
use WebDevStudios\CCForWoo\AbandonedCarts\CartRecovery;
24-
use WebDevStudios\CCForWoo\Ajax\GenerateSecretKey;
25-
use WebDevStudios\CCForWoo\Rest\V1\Registrar as RestRegistrar;
26-
use WebDevStudios\CCForWoo\Rest\V1\AuthHandler as RestAuthHandler;
24+
use WebDevStudios\CCForWoo\Rest\Registrar as RestRegistrar;
2725

2826
/**
2927
* "Core" plugin class.
@@ -79,8 +77,6 @@ final class Plugin extends ServiceRegistrar {
7977
CartsTable::class,
8078
CartRecovery::class,
8179
RestRegistrar::class,
82-
RestAuthHandler::class,
83-
GenerateSecretKey::class,
8480
];
8581

8682
/**
Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,18 @@
77
* @since 2019-10-16
88
*/
99

10-
namespace WebDevStudios\CCForWoo\Rest\V1\Endpoints;
10+
namespace WebDevStudios\CCForWoo\Rest\Endpoints;
1111

1212
use WP_REST_Server;
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;
1920
use WebDevStudios\CCForWoo\AbandonedCarts\Cart;
20-
use WebDevStudios\CCForWoo\Rest\V1\Registrar;
21+
use WebDevStudios\CCForWoo\Rest\Registrar;
2122

2223
/**
2324
* Class AbandonedCarts
@@ -55,17 +56,33 @@ public function __construct() {
5556
*/
5657
public function register_routes() {
5758
register_rest_route(
58-
Registrar::$namespace, '/' . $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: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
<?php
2+
/**
3+
* CCforWoo REST Registrar.
4+
*
5+
* @author George Gecewicz <[email protected]>
6+
* @package WebDevStudios\CCForWoo\Rest
7+
* @since 2019-11-13
8+
*/
9+
10+
namespace WebDevStudios\CCForWoo\Rest;
11+
12+
use WebDevStudios\OopsWP\Structure\Service;
13+
14+
/**
15+
* Class Registrar
16+
*
17+
* @author George Gecewicz <[email protected]>
18+
* @package WebDevStudios\CCForWoo\Rest
19+
* @since 2019-11-13
20+
*/
21+
class Registrar extends Service {
22+
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+
32+
/**
33+
* Register hooks.
34+
*
35+
* @author George Gecewicz <[email protected]>
36+
* @since 2019-11-13
37+
*/
38+
public function register_hooks() {
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+
}
42+
43+
/**
44+
* Initialize REST endpoints.
45+
*
46+
* @author George Gecewicz <[email protected]>
47+
* @since 2019-11-13
48+
*/
49+
public function init_rest_endpoints() {
50+
( new Endpoints\AbandonedCarts() )->register_routes();
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+
}
72+
73+
}
74+

src/Rest/V1/AuthHandler.php

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

0 commit comments

Comments
 (0)