-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathInit.php
187 lines (165 loc) · 5.28 KB
/
Init.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
<?php
/**
* Load required classes.
*
* @author Paul Kilmurray <[email protected]>
*
* @see http://wcpos.com
* @package WooCommercePOS
*/
namespace WCPOS\WooCommercePOS;
use WCPOS\WooCommercePOS\Services\Settings as SettingsService;
use WCPOS\WooCommercePOS\Services\Auth as AuthService;
use WP_HTTP_Response;
use WP_REST_Request;
use WP_REST_Server;
use const DOING_AJAX;
/**
*
*/
class Init {
/**
* Constructor.
*/
public function __construct() {
// global helper functions
require_once PLUGIN_PATH . 'includes/wcpos-functions.php';
require_once PLUGIN_PATH . 'includes/wcpos-store-functions.php';
// Init hooks
add_action( 'init', array( $this, 'init' ) );
add_action( 'rest_api_init', array( $this, 'init_rest_api' ), 20 );
add_filter( 'query_vars', array( $this, 'query_vars' ) );
// Headers for API discoverability
add_filter( 'rest_pre_serve_request', array( $this, 'rest_pre_serve_request' ), 5, 4 );
add_action( 'send_headers', array( $this, 'send_headers' ), 99, 1 );
}
/**
* Load the required resources.
*/
public function init(): void {
$this->init_common();
$this->init_frontend();
$this->init_admin();
$this->init_integrations();
}
/**
* Common initializations
*/
private function init_common() {
// init the Services
SettingsService::instance();
AuthService::instance();
// init other functionality needed by both frontend and admin
new i18n();
new Gateways();
new Products();
new Orders();
}
/**
* Frontend specific initializations
*/
private function init_frontend() {
if ( ! is_admin() ) {
new Templates();
new Form_Handler();
}
}
/**
* Admin specific initializations
*/
private function init_admin() {
if ( is_admin() ) {
if ( defined( 'DOING_AJAX' ) && DOING_AJAX ) {
new AJAX();
} else {
new Admin();
}
}
}
/**
* Integrations
*/
private function init_integrations() {
// WooCommerce Bookings - http://www.woothemes.com/products/woocommerce-bookings/
// if ( class_exists( 'WC-Bookings' ) ) {
// new Integrations\Bookings();
// }
// Yoast SEO - https://wordpress.org/plugins/wordpress-seo/
if ( class_exists( 'WPSEO_Options' ) ) {
new Integrations\WPSEO();
}
// wePOS alters the WooCommerce REST API, breaking the expected schema
// It's very bad form on their part, but we need to work around it
new Integrations\WePOS();
}
/**
* Loads the POS API and duck punches the WC REST API.
*/
public function init_rest_api(): void {
if ( woocommerce_pos_request() ) {
new API();
} else {
new WC_API();
}
}
/**
* Adds 'wcpos' to the query variables allowed before processing.
*
* Allows (publicly allowed) query vars to be added, removed, or changed prior
* to executing the query. Needed to allow custom rewrite rules using your own arguments
* to work, or any other custom query variables you want to be publicly available.
*
* @param string[] $query_vars The array of allowed query variable names.
*
* @return string[] The array of allowed query variable names.
*/
public function query_vars( array $query_vars ): array {
$query_vars[] = SHORT_NAME;
return $query_vars;
}
/**
* Allow pre-flight requests from WCPOS Desktop and Mobile Apps
* Note: pre-flight requests cannot have headers, so I can't filter by pos request
* See: https://fetch.spec.whatwg.org/#cors-preflight-fetch.
*
* @param bool $served Whether the request has already been served.
* Default false.
* @param WP_HTTP_Response $result Result to send to the client. Usually a `WP_REST_Response`.
* @param WP_REST_Request $request Request used to generate the response.
* @param WP_REST_Server $server Server instance.
*
* @return bool $served
*/
public function rest_pre_serve_request( $served, WP_HTTP_Response $result, WP_REST_Request $request, WP_REST_Server $server ) {
if ( 'OPTIONS' == $request->get_method() ) {
$allow_headers = array(
'Authorization', // For user-agent authentication with a server.
'X-WP-Nonce', // WordPress-specific header, used for CSRF protection.
'Content-Disposition', // Informs how to process the response data.
'Content-MD5', // For verifying data integrity.
'Content-Type', // Specifies the media type of the resource.
'X-HTTP-Method-Override', // Used to override the HTTP method.
'X-WCPOS', // Used to identify WCPOS requests.
);
$server->send_header( 'Access-Control-Allow-Origin', '*' );
$server->send_header( 'Access-Control-Allow-Methods', 'GET, POST, PUT, PATCH, DELETE' );
$server->send_header( 'Access-Control-Allow-Headers', implode( ', ', $allow_headers ) );
}
return $served;
}
/**
* Allow HEAD checks for WP API Link URL and server uptime
* Fires once the requested HTTP headers for caching, content type, etc. have been sent.
*
* FIXME: Why is Link header not exposed sometimes on my development machine?
*
* @return void
*/
public function send_headers(): void {
// some server convert HEAD to GET method, so use this query param instead
if ( isset( $_GET['_method'] ) && 'head' == strtolower( $_GET['_method'] ) ) {
header( 'Access-Control-Allow-Origin: *' );
header( 'Access-Control-Expose-Headers: Link' );
}
}
}