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 188 189 190 191 192 193 194 195 196 197 198 199 200 201
<?php
defined( 'ABSPATH' ) || exit();
class WC_Braintree_Rest_API {
private $controllers = array();
private $force_routes = false;
public function __construct() {
$this->include_classes();
add_action( 'rest_api_init', array( $this, 'register_routes' ) );
}
public static function init() {
add_action( 'wc_ajax_wc_braintree_frontend_request', array( __CLASS__, 'process_frontend_ajax' ) );
}
public function __get( $key ) {
$controller = isset( $this->controllers[ $key ] ) ? $this->controllers[ $key ] : '';
if ( empty( $controller ) ) {
wc_doing_it_wrong( __FUNCTION__, sprintf( __( '%1$s is an invalid controller name.', 'woo-payment-gateway' ), $key ), braintree()->version );
}
return $controller;
}
public function __set( $key, $value ) {
$this->controllers[ $key ] = $value;
}
private function include_classes() {
include_once WC_BRAINTREE_PATH . 'includes/api/class-wc-braintree-rest-webhook-authentication.php';
include_once WC_BRAINTREE_PATH . 'includes/abstract/abstract-class-wc-braintree-rest-controller.php';
include_once WC_BRAINTREE_PATH . 'includes/abstract/abstract-class-wc-braintree-controller-frontend.php';
include_once WC_BRAINTREE_PATH . 'includes/api/class-wc-braintree-controller-3ds.php';
include_once WC_BRAINTREE_PATH . 'includes/api/class-wc-braintree-controller-order-actions.php';
include_once WC_BRAINTREE_PATH . 'includes/api/class-wc-braintree-controller-client-token.php';
include_once WC_BRAINTREE_PATH . 'includes/api/class-wc-braintree-controller-payment-tokens.php';
include_once WC_BRAINTREE_PATH . 'includes/api/class-wc-braintree-controller-plan.php';
include_once WC_BRAINTREE_PATH . 'includes/api/class-wc-braintree-controller-kount.php';
include_once WC_BRAINTREE_PATH . 'includes/api/class-wc-braintree-controller-webhook.php';
include_once WC_BRAINTREE_PATH . 'includes/api/class-wc-braintree-controller-checkout.php';
include_once WC_BRAINTREE_PATH . 'includes/api/class-wc-braintree-controller-cart.php';
include_once WC_BRAINTREE_PATH . 'includes/api/class-wc-braintree-controller-applepay.php';
include_once WC_BRAINTREE_PATH . 'includes/api/class-wc-braintree-controller-googlepay.php';
include_once WC_BRAINTREE_PATH . 'includes/api/class-wc-braintree-controller-merchant-account.php';
include_once WC_BRAINTREE_PATH . 'includes/api/class-wc-braintree-controller-local-payment.php';
include_once WC_BRAINTREE_PATH . 'includes/api/class-wc-braintree-controller-data-migration.php';
include_once WC_BRAINTREE_PATH . 'includes/api/class-wc-braintree-controller-settings.php';
include_once WC_BRAINTREE_PATH . 'includes/api/class-wc-braintree-controller-paypal.php';
include_once WC_BRAINTREE_PATH . 'includes/api/class-wc-braintree-controller-product.php';
foreach ( $this->get_controllers() as $key => $class_name ) {
if ( class_exists( $class_name ) ) {
$this->{$key} = new $class_name();
}
}
}
public function register_routes() {
if ( self::is_rest_api_request() || $this->force_routes ) {
foreach ( $this->controllers as $key => $controller ) {
if ( is_callable( array( $controller, 'register_routes' ) ) ) {
$controller->register_routes();
}
}
}
}
public function get_controllers() {
$controllers = array(
'checkout' => 'WC_Braintree_Controller_Checkout',
'cart' => 'WC_Braintree_Controller_Cart',
'_3ds' => 'WC_Braintree_Controller_3ds',
'order_actions' => 'WC_Braintree_Controller_Order_Actions',
'client_token' => 'WC_Braintree_Controller_Client_Token',
'tokens' => 'WC_Braintree_Controller_Payment_Tokens',
'plans' => 'WC_Braintree_Controller_Plan',
'kount' => 'WC_Braintree_Controller_Kount',
'applepay' => 'WC_Braintree_Controller_ApplePay',
'merchant_account' => 'WC_Braintree_Controller_Merchant_Accounts',
'local_payment' => 'WC_Braintree_Controller_Local_Payment',
'data_migration' => 'WC_Braintree_Controller_Data_Migration',
'googlepay' => 'WC_Braintree_Controller_GooglePay',
'settings' => 'WC_Braintree_Controller_Settings',
'paypal' => 'WC_Braintree_Controller_PayPal',
'product' => 'WC_Braintree_Controller_Product',
'webhook' => 'WC_Braintree_Controller_Webhook'
);
return apply_filters( 'wc_braintree_api_controllers', $controllers );
}
public function rest_url() {
return braintree()->rest_url();
}
public function rest_uri() {
return braintree()->rest_uri();
}
public static function is_rest_api_request() {
global $wp;
if ( ! empty( $wp->query_vars['rest_route'] ) && strpos( $wp->query_vars['rest_route'], braintree()->rest_uri() ) !== false ) {
return true;
}
if ( ! empty( $_SERVER['REQUEST_URI'] ) && strpos( $_SERVER['REQUEST_URI'], braintree()->rest_uri() ) !== false ) {
return true;
}
return false;
}
public static function is_frontend_rest_request() {
return ! empty( $_GET['wc-ajax'] ) && $_GET['wc-ajax'] === 'wc_braintree_frontend_request';
}
public static function is_wp_rest_request() {
if ( function_exists( 'WC' ) && property_exists( WC(), 'is_rest_api_request' ) ) {
return WC()->is_rest_api_request();
}
return ! empty( $_SERVER['REQUEST_URI'] ) && strpos( $_SERVER['REQUEST_URI'], trailingslashit( rest_get_url_prefix() ) ) !== false;
}
public function force( $bool ) {
$this->force_routes = $bool;
}
public static function process_frontend_ajax() {
if ( isset( $_GET['path'] ) ) {
global $wp;
$wp->set_query_var( 'rest_route', sanitize_text_field( $_GET['path'] ) );
rest_api_loaded();
}
}
public static function get_endpoint( $path ) {
return add_query_arg( 'path', '/' . trim( $path, '/' ), WC_AJAX::get_endpoint( 'wc_braintree_frontend_request' ) );
}
}
WC_Braintree_Rest_API::init();