Completed
Pull Request — master (#1267)
by
unknown
01:48
created

WC_Stripe_Connect_REST_Controller::post_internal()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 1
dl 0
loc 6
rs 10
c 0
b 0
f 0
1
<?php
2
3
if ( ! defined( 'ABSPATH' ) ) {
4
	exit;
5
}
6
7
if ( ! class_exists( 'WC_Stripe_Connect_REST_Controller' ) ) {
8
	/**
9
	 * Stripe Connect base REST controller class.
10
	 */
11
	class WC_Stripe_Connect_REST_Controller extends WP_REST_Controller {
12
13
		/**
14
		 * Endpoint namespace.
15
		 *
16
		 * @var string
17
		 */
18
		protected $namespace = 'wc/v1';
19
20
		/**
21
		 * Stripe connect api.
22
		 *
23
		 * @var object $api
24
		 */
25
		private $api;
26
27
		/**
28
		 * Constructor.
29
		 *
30
		 * @param WC_Stripe_Connect_API $api stripe connect api.
31
		 */
32
		public function __construct( WC_Stripe_Connect_API $api ) {
33
34
			$this->api = $api;
35
		}
36
37
		/**
38
		 * Registers rest routes for stripe connect functionality
39
		 */
40
		public function register_routes() {
41
42 View Code Duplication
			if ( method_exists( $this, 'get' ) ) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
43
				register_rest_route(
44
					$this->namespace,
45
					'/' . $this->rest_base,
46
					array(
47
						array(
48
							'methods'             => 'GET',
49
							'callback'            => array( $this, 'get_internal' ),
50
							'permission_callback' => array( $this, 'check_permission' ),
51
						),
52
					)
53
				);
54
			}
55
56 View Code Duplication
			if ( method_exists( $this, 'post' ) ) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
57
				register_rest_route(
58
					$this->namespace,
59
					'/' . $this->rest_base,
60
					array(
61
						array(
62
							'methods'             => 'POST',
63
							'callback'            => array( $this, 'post_internal' ),
64
							'permission_callback' => array( $this, 'check_permission' ),
65
						),
66
					)
67
				);
68
			}
69
70 View Code Duplication
			if ( method_exists( $this, 'delete' ) ) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
71
				register_rest_route(
72
					$this->namespace,
73
					'/' . $this->rest_base,
74
					array(
75
						array(
76
							'methods'             => 'DELETE',
77
							'callback'            => array( $this, 'delete_internal' ),
78
							'permission_callback' => array( $this, 'check_permission' ),
79
						),
80
					)
81
				);
82
			}
83
		}
84
85
		/**
86
		 * Send get request.
87
		 *
88
		 * @param array $request request.
89
		 *
90
		 * @return array
91
		 */
92
		public function get_internal( $request ) {
93
94
			$this->prevent_route_caching();
95
96
			return $this->get( $request );
97
		}
98
99
		/**
100
		 * Send post request.
101
		 *
102
		 * @param array $request request.
103
		 *
104
		 * @return array
105
		 */
106
		public function post_internal( $request ) {
107
108
			$this->prevent_route_caching();
109
110
			return $this->post( $request );
111
		}
112
113
		/**
114
		 * Sends delete request.
115
		 *
116
		 * @param array $request request.
117
		 *
118
		 * @return array
119
		 */
120
		public function delete_internal( $request ) {
121
122
			$this->prevent_route_caching();
123
124
			return $this->delete( $request );
125
		}
126
127
		/**
128
		 * Validate the requester's permissions
129
		 *
130
		 * @param array $request request.
131
		 *
132
		 * @return bool
133
		 */
134
		public function check_permission( $request ) {
135
136
			return current_user_can( 'manage_woocommerce' );
137
		}
138
139
		/**
140
		 * Consolidate cache prevention mechanisms.
141
		 */
142
		public function prevent_route_caching() {
143
144
			if ( ! defined( 'DONOTCACHEPAGE' ) ) {
145
				define( 'DONOTCACHEPAGE', true ); // Play nice with WP-Super-Cache.
146
			}
147
148
			// Prevent our REST API endpoint responses from being added to browser cache.
149
			add_filter( 'rest_post_dispatch', array( $this, 'send_nocache_header' ), PHP_INT_MAX, 2 );
150
		}
151
152
		/**
153
		 * Send a no-cache header for WCS REST API responses. Prompted by cache issues
154
		 * on the Pantheon hosting platform.
155
		 *
156
		 * See: https://pantheon.io/docs/cache-control/
157
		 *
158
		 * @param  WP_REST_Response $response REST API response.
159
		 * @param  WP_REST_Server   $server   server.
160
		 *
161
		 * @return WP_REST_Response passthrough $response parameter
162
		 */
163
		public function send_nocache_header( $response, $server ) {
164
165
			$server->send_header( 'Cache-Control', 'no-cache, must-revalidate, max-age=0' );
166
167
			return $response;
168
		}
169
170
171
	}
172
}
173