2013-02-13 12:38:15 +00:00
|
|
|
<?php
|
|
|
|
/**
|
2019-01-30 17:38:41 +00:00
|
|
|
* WooCommerce API class loader.
|
2013-02-20 17:14:46 +00:00
|
|
|
*
|
2019-01-30 17:38:41 +00:00
|
|
|
* This handles APIs in WooCommerce. These include:
|
|
|
|
* - wc-api endpoint - Commonly used by Payment gateways for callbacks.
|
|
|
|
* - Legacy REST API - Deprecated in 2.6.0. @see class-wc-legacy-api.php
|
|
|
|
* - WP REST API - The main REST API in WooCommerce which is built on top of the WP REST API.
|
2013-02-20 17:14:46 +00:00
|
|
|
*
|
2018-03-15 20:35:40 +00:00
|
|
|
* @package WooCommerce/API
|
|
|
|
* @since 2.0.0
|
2013-02-13 12:38:15 +00:00
|
|
|
*/
|
2013-11-04 00:29:45 +00:00
|
|
|
|
2018-03-15 20:35:40 +00:00
|
|
|
defined( 'ABSPATH' ) || exit;
|
2013-11-04 00:29:45 +00:00
|
|
|
|
2017-10-20 17:52:57 +00:00
|
|
|
/**
|
2019-01-30 17:38:41 +00:00
|
|
|
* WC_API class.
|
2017-10-20 17:52:57 +00:00
|
|
|
*/
|
2016-06-07 10:03:16 +00:00
|
|
|
class WC_API extends WC_Legacy_API {
|
2014-07-12 21:51:08 +00:00
|
|
|
|
2013-02-13 12:38:15 +00:00
|
|
|
/**
|
2015-11-03 13:31:20 +00:00
|
|
|
* Setup class.
|
2017-10-20 17:52:57 +00:00
|
|
|
*
|
2013-11-04 00:29:45 +00:00
|
|
|
* @since 2.0
|
2013-02-13 12:38:15 +00:00
|
|
|
*/
|
|
|
|
public function __construct() {
|
2019-01-30 17:38:41 +00:00
|
|
|
$this->wc_api_init();
|
|
|
|
$this->rest_api_init();
|
|
|
|
}
|
2013-11-04 00:29:45 +00:00
|
|
|
|
2019-01-30 17:38:41 +00:00
|
|
|
/**
|
|
|
|
* Init the WC API by adding endpoints for those requests.
|
|
|
|
*/
|
|
|
|
private function wc_api_init() {
|
|
|
|
add_filter( 'query_vars', array( $this, 'add_query_vars' ), 0 );
|
2015-04-27 20:49:35 +00:00
|
|
|
add_action( 'init', array( $this, 'add_endpoint' ), 0 );
|
2014-07-12 21:51:08 +00:00
|
|
|
add_action( 'parse_request', array( $this, 'handle_api_requests' ), 0 );
|
2019-01-30 17:38:41 +00:00
|
|
|
}
|
2015-06-05 13:36:45 +00:00
|
|
|
|
2019-01-30 17:38:41 +00:00
|
|
|
/**
|
|
|
|
* Init WP REST API by hooking into `rest_api_init`.
|
|
|
|
*
|
|
|
|
* @since 2.6.0
|
|
|
|
*/
|
|
|
|
private function rest_api_init() {
|
|
|
|
add_action( 'rest_api_init', array( $this, 'rest_api_includes' ), 5 );
|
|
|
|
add_action( 'rest_api_init', array( $this, 'register_rest_routes' ), 10 );
|
2013-02-13 12:38:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-04-27 19:50:07 +00:00
|
|
|
* Add new query vars.
|
2013-02-13 12:38:15 +00:00
|
|
|
*
|
2013-11-04 00:29:45 +00:00
|
|
|
* @since 2.0
|
2017-10-20 17:52:57 +00:00
|
|
|
* @param array $vars Query vars.
|
2014-09-07 23:37:55 +00:00
|
|
|
* @return string[]
|
2013-02-13 12:38:15 +00:00
|
|
|
*/
|
|
|
|
public function add_query_vars( $vars ) {
|
2016-06-07 10:03:16 +00:00
|
|
|
$vars = parent::add_query_vars( $vars );
|
2013-02-13 12:38:15 +00:00
|
|
|
$vars[] = 'wc-api';
|
|
|
|
return $vars;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-06-07 10:03:16 +00:00
|
|
|
* WC API for payment gateway IPNs, etc.
|
2017-10-20 17:52:57 +00:00
|
|
|
*
|
2013-11-04 00:29:45 +00:00
|
|
|
* @since 2.0
|
2013-02-13 12:38:15 +00:00
|
|
|
*/
|
2015-03-20 14:20:12 +00:00
|
|
|
public static function add_endpoint() {
|
2016-06-07 10:03:16 +00:00
|
|
|
parent::add_endpoint();
|
2013-02-13 12:38:15 +00:00
|
|
|
add_rewrite_endpoint( 'wc-api', EP_ALL );
|
|
|
|
}
|
|
|
|
|
2014-07-12 21:51:08 +00:00
|
|
|
/**
|
2015-06-05 13:36:45 +00:00
|
|
|
* API request - Trigger any API requests.
|
2014-07-12 21:51:08 +00:00
|
|
|
*
|
2016-02-16 20:22:12 +00:00
|
|
|
* @since 2.0
|
|
|
|
* @version 2.4
|
2014-07-12 21:51:08 +00:00
|
|
|
*/
|
|
|
|
public function handle_api_requests() {
|
|
|
|
global $wp;
|
|
|
|
|
2017-10-20 17:52:57 +00:00
|
|
|
if ( ! empty( $_GET['wc-api'] ) ) { // WPCS: input var okay, CSRF ok.
|
|
|
|
$wp->query_vars['wc-api'] = sanitize_key( wp_unslash( $_GET['wc-api'] ) ); // WPCS: input var okay, CSRF ok.
|
2014-07-12 21:51:08 +00:00
|
|
|
}
|
|
|
|
|
2016-02-17 19:40:41 +00:00
|
|
|
// wc-api endpoint requests.
|
2014-07-12 21:51:08 +00:00
|
|
|
if ( ! empty( $wp->query_vars['wc-api'] ) ) {
|
|
|
|
|
2016-02-17 19:40:41 +00:00
|
|
|
// Buffer, we won't want any output here.
|
2014-07-12 21:51:08 +00:00
|
|
|
ob_start();
|
|
|
|
|
2016-02-17 19:40:41 +00:00
|
|
|
// No cache headers.
|
2017-11-08 15:07:00 +00:00
|
|
|
wc_nocache_headers();
|
2014-07-12 21:51:08 +00:00
|
|
|
|
2016-02-17 19:40:41 +00:00
|
|
|
// Clean the API request.
|
2017-11-20 02:07:15 +00:00
|
|
|
$api_request = strtolower( wc_clean( $wp->query_vars['wc-api'] ) );
|
2015-06-05 13:36:45 +00:00
|
|
|
|
2019-01-30 17:38:41 +00:00
|
|
|
// Make sure gateways are available for request.
|
|
|
|
WC()->payment_gateways();
|
|
|
|
|
2016-02-17 19:40:41 +00:00
|
|
|
// Trigger generic action before request hook.
|
2015-06-05 13:36:45 +00:00
|
|
|
do_action( 'woocommerce_api_request', $api_request );
|
|
|
|
|
2016-02-17 19:40:41 +00:00
|
|
|
// Is there actually something hooked into this API request? If not trigger 400 - Bad request.
|
2015-06-05 13:36:45 +00:00
|
|
|
status_header( has_action( 'woocommerce_api_' . $api_request ) ? 200 : 400 );
|
2014-07-12 21:51:08 +00:00
|
|
|
|
2016-02-17 19:40:41 +00:00
|
|
|
// Trigger an action which plugins can hook into to fulfill the request.
|
2015-06-05 13:36:45 +00:00
|
|
|
do_action( 'woocommerce_api_' . $api_request );
|
2014-07-12 21:51:08 +00:00
|
|
|
|
2016-02-17 19:40:41 +00:00
|
|
|
// Done, clear buffer and exit.
|
2014-07-12 21:51:08 +00:00
|
|
|
ob_end_clean();
|
2016-02-17 19:40:41 +00:00
|
|
|
die( '-1' );
|
2014-07-12 21:51:08 +00:00
|
|
|
}
|
|
|
|
}
|
2016-02-16 20:59:18 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Include REST API classes.
|
2017-02-09 20:40:35 +00:00
|
|
|
*
|
2016-02-16 20:59:18 +00:00
|
|
|
* @since 2.6.0
|
|
|
|
*/
|
2019-01-30 17:38:41 +00:00
|
|
|
public function rest_api_includes() {
|
2016-02-16 20:59:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Register REST API routes.
|
2017-10-20 17:52:57 +00:00
|
|
|
*
|
2016-02-16 20:59:18 +00:00
|
|
|
* @since 2.6.0
|
|
|
|
*/
|
|
|
|
public function register_rest_routes() {
|
2016-07-26 18:08:29 +00:00
|
|
|
// Register settings to the REST API.
|
|
|
|
$this->register_wp_admin_settings();
|
2016-02-16 20:59:18 +00:00
|
|
|
}
|
2016-06-16 21:43:03 +00:00
|
|
|
|
|
|
|
/**
|
2016-07-26 18:08:29 +00:00
|
|
|
* Register WC settings from WP-API to the REST API.
|
2017-10-20 17:52:57 +00:00
|
|
|
*
|
2017-03-15 16:36:53 +00:00
|
|
|
* @since 3.0.0
|
2016-06-16 21:43:03 +00:00
|
|
|
*/
|
2016-07-26 18:08:29 +00:00
|
|
|
public function register_wp_admin_settings() {
|
2016-06-16 21:43:03 +00:00
|
|
|
$pages = WC_Admin_Settings::get_settings_pages();
|
|
|
|
foreach ( $pages as $page ) {
|
2016-09-07 22:05:45 +00:00
|
|
|
new WC_Register_WP_Admin_Settings( $page, 'page' );
|
|
|
|
}
|
|
|
|
|
|
|
|
$emails = WC_Emails::instance();
|
|
|
|
foreach ( $emails->get_emails() as $email ) {
|
|
|
|
new WC_Register_WP_Admin_Settings( $email, 'email' );
|
2016-06-16 21:43:03 +00:00
|
|
|
}
|
|
|
|
}
|
2013-11-04 00:29:45 +00:00
|
|
|
}
|