Completed
Push — master ( c63cd8...c4c8fb )
by Claudio
10:23
created

WC_API::is_rest_api_loaded()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * WC-API endpoint handler.
4
 *
5
 * This handles API related functionality in WooCommerce.
6
 * - wc-api endpoint - Commonly used by Payment gateways for callbacks.
7
 * - Legacy REST API - Deprecated in 2.6.0. @see class-wc-legacy-api.php
8
 * - WP REST API - The main REST API in WooCommerce which is built on top of the WP REST API.
9
 *
10
 * @package WooCommerce/API
11
 * @since   2.0.0
12
 */
13
14
defined( 'ABSPATH' ) || exit;
15
16
/**
17
 * WC_API class.
18
 */
19
class WC_API extends WC_Legacy_API {
20
21
	/**
22
	 * Init the API by setting up action and filter hooks.
23
	 */
24
	public function init() {
25
		parent::init();
26
		add_action( 'init', array( $this, 'add_endpoint' ), 0 );
27
		add_filter( 'query_vars', array( $this, 'add_query_vars' ), 0 );
28
		add_action( 'parse_request', array( $this, 'handle_api_requests' ), 0 );
29
		add_action( 'rest_api_init', array( $this, 'register_wp_admin_settings' ) );
30
	}
31
32
	/**
33
	 * Get the version of the REST API package being ran.
34
	 *
35
	 * @since 3.7.0
36
	 * @return string|null
37
	 */
38 1
	public function get_rest_api_package_version() {
39 1
		if ( ! $this->is_rest_api_loaded() ) {
40
			return null;
41
		}
42 1
		return \Automattic\WooCommerce\RestApi\Package::get_version();
43
	}
44
45
	/**
46
	 * Get the version of the REST API package being ran.
47
	 *
48
	 * @since 3.7.0
49
	 * @return string
50
	 */
51 1
	public function get_rest_api_package_path() {
52 1
		if ( ! $this->is_rest_api_loaded() ) {
53
			return null;
54
		}
55 1
		return \Automattic\WooCommerce\RestApi\Package::get_path();
56
	}
57
58
	/**
59
	 * Return if the rest API classes were already loaded.
60
	 *
61
	 * @since 3.7.0
62
	 * @return boolean
63
	 */
64 2
	protected function is_rest_api_loaded() {
65 2
		return class_exists( '\Automattic\WooCommerce\RestApi\Package', false );
66
	}
67
68
	/**
69
	 * Get data from a WooCommerce API endpoint.
70
	 *
71
	 * @since 3.7.0
72
	 * @param string $endpoint Endpoint.
73
	 * @param array  $params Params to passwith request.
74
	 * @return array|\WP_Error
75
	 */
76 1
	public function get_endpoint_data( $endpoint, $params = array() ) {
77 1
		if ( ! $this->is_rest_api_loaded() ) {
78
			return new WP_Error( 'rest_api_unavailable', __( 'The Rest API is unavailable.', 'woocommerce' ) );
79
		}
80 1
		$request = new \WP_REST_Request( 'GET', $endpoint );
81 1
		if ( $params ) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $params of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
82
			$request->set_query_params( $params );
83
		}
84 1
		$response = rest_do_request( $request );
85 1
		$server   = rest_get_server();
86 1
		$json     = wp_json_encode( $server->response_to_data( $response, false ) );
87 1
		return json_decode( $json, true );
88
	}
89
90
	/**
91
	 * Add new query vars.
92
	 *
93
	 * @since 2.0
94
	 * @param array $vars Query vars.
95
	 * @return string[]
96
	 */
97
	public function add_query_vars( $vars ) {
98
		$vars   = parent::add_query_vars( $vars );
99
		$vars[] = 'wc-api';
100
		return $vars;
101
	}
102
103
	/**
104
	 * WC API for payment gateway IPNs, etc.
105
	 *
106
	 * @since 2.0
107
	 */
108 2
	public static function add_endpoint() {
109 2
		parent::add_endpoint();
110 2
		add_rewrite_endpoint( 'wc-api', EP_ALL );
111
	}
112
113
	/**
114
	 * API request - Trigger any API requests.
115
	 *
116
	 * @since   2.0
117
	 * @version 2.4
118
	 */
119
	public function handle_api_requests() {
120
		global $wp;
121
122
		if ( ! empty( $_GET['wc-api'] ) ) { // WPCS: input var okay, CSRF ok.
123
			$wp->query_vars['wc-api'] = sanitize_key( wp_unslash( $_GET['wc-api'] ) ); // WPCS: input var okay, CSRF ok.
124
		}
125
126
		// wc-api endpoint requests.
127
		if ( ! empty( $wp->query_vars['wc-api'] ) ) {
128
129
			// Buffer, we won't want any output here.
130
			ob_start();
131
132
			// No cache headers.
133
			wc_nocache_headers();
134
135
			// Clean the API request.
136
			$api_request = strtolower( wc_clean( $wp->query_vars['wc-api'] ) );
137
138
			// Make sure gateways are available for request.
139
			WC()->payment_gateways();
140
141
			// Trigger generic action before request hook.
142
			do_action( 'woocommerce_api_request', $api_request );
143
144
			// Is there actually something hooked into this API request? If not trigger 400 - Bad request.
145
			status_header( has_action( 'woocommerce_api_' . $api_request ) ? 200 : 400 );
146
147
			// Trigger an action which plugins can hook into to fulfill the request.
148
			do_action( 'woocommerce_api_' . $api_request );
149
150
			// Done, clear buffer and exit.
151
			ob_end_clean();
152
			die( '-1' );
153
		}
154
	}
155
156
	/**
157
	 * Register WC settings from WP-API to the REST API.
158
	 *
159
	 * @since  3.0.0
160
	 */
161 1
	public function register_wp_admin_settings() {
162 1
		$pages = WC_Admin_Settings::get_settings_pages();
163 1
		foreach ( $pages as $page ) {
164 1
			new WC_Register_WP_Admin_Settings( $page, 'page' );
165
		}
166
167 1
		$emails = WC_Emails::instance();
168 1
		foreach ( $emails->get_emails() as $email ) {
169 1
			new WC_Register_WP_Admin_Settings( $email, 'email' );
170
		}
171
	}
172
}
173