blob: e67baa98ede3b14f4e8ee7cb53cfd98fbaf59f28 [file] [log] [blame]
swissChilif0cbdc32023-01-05 17:21:38 -05001<?php
2
3if ( ! defined( 'ABSPATH' ) ) {
4 exit;
5}
6
7// phpcs:disable WordPress.Files.FileName
8
9/**
10 * Stripe Connect base REST controller class.
11 */
12abstract class WC_Stripe_Connect_REST_Controller extends WP_REST_Controller {
13
14 /**
15 * Endpoint namespace.
16 *
17 * @var string
18 */
19 protected $namespace = 'wc/v1';
20
21 /**
22 * Stripe connect api.
23 *
24 * @var object $api
25 */
26 private $api;
27
28 /**
29 * Constructor.
30 *
31 * @param WC_Stripe_Connect_API $api stripe connect api.
32 */
33 public function __construct( WC_Stripe_Connect_API $api ) {
34
35 $this->api = $api;
36 }
37
38 /**
39 * Registers rest routes for stripe connect functionality
40 */
41 public function register_routes() {
42
43 if ( method_exists( $this, 'get' ) ) {
44 register_rest_route(
45 $this->namespace,
46 '/' . $this->rest_base,
47 [
48 [
49 'methods' => 'GET',
50 'callback' => [ $this, 'get_internal' ],
51 'permission_callback' => [ $this, 'check_permission' ],
52 ],
53 ]
54 );
55 }
56
57 if ( method_exists( $this, 'post' ) ) {
58 register_rest_route(
59 $this->namespace,
60 '/' . $this->rest_base,
61 [
62 [
63 'methods' => 'POST',
64 'callback' => [ $this, 'post_internal' ],
65 'permission_callback' => [ $this, 'check_permission' ],
66 ],
67 ]
68 );
69 }
70
71 if ( method_exists( $this, 'delete' ) ) {
72 register_rest_route(
73 $this->namespace,
74 '/' . $this->rest_base,
75 [
76 [
77 'methods' => 'DELETE',
78 'callback' => [ $this, 'delete_internal' ],
79 'permission_callback' => [ $this, 'check_permission' ],
80 ],
81 ]
82 );
83 }
84 }
85
86 /**
87 * Send get request.
88 *
89 * @param array $request request.
90 *
91 * @return array
92 */
93 public function get_internal( $request ) {
94
95 $this->prevent_route_caching();
96
97 return $this->get( $request );
98 }
99
100 /**
101 * Send post request.
102 *
103 * @param array $request request.
104 *
105 * @return array
106 */
107 public function post_internal( $request ) {
108
109 $this->prevent_route_caching();
110
111 return $this->post( $request );
112 }
113
114 /**
115 * Sends delete request.
116 *
117 * @param array $request request.
118 *
119 * @return array
120 */
121 public function delete_internal( $request ) {
122
123 $this->prevent_route_caching();
124
125 return $this->delete( $request );
126 }
127
128 /**
129 * Validate the requester's permissions
130 *
131 * @param array $request request.
132 *
133 * @return bool
134 */
135 public function check_permission( $request ) {
136
137 return current_user_can( 'manage_woocommerce' );
138 }
139
140 /**
141 * Consolidate cache prevention mechanisms.
142 */
143 public function prevent_route_caching() {
144
145 if ( ! defined( 'DONOTCACHEPAGE' ) ) {
146 define( 'DONOTCACHEPAGE', true ); // Play nice with WP-Super-Cache.
147 }
148
149 // Prevent our REST API endpoint responses from being added to browser cache.
150 add_filter( 'rest_post_dispatch', [ $this, 'send_nocache_header' ], PHP_INT_MAX, 2 );
151 }
152
153 /**
154 * Send a no-cache header for WCS REST API responses. Prompted by cache issues
155 * on the Pantheon hosting platform.
156 *
157 * See: https://pantheon.io/docs/cache-control/
158 *
159 * @param WP_REST_Response $response REST API response.
160 * @param WP_REST_Server $server server.
161 *
162 * @return WP_REST_Response passthrough $response parameter
163 */
164 public function send_nocache_header( $response, $server ) {
165
166 $server->send_header( 'Cache-Control', 'no-cache, must-revalidate, max-age=0' );
167
168 return $response;
169 }
170}