1 <?php
  2 /**
  3  * WooCommerce API
  4  *
  5  * Handles WC-API endpoint requests
  6  *
  7  * @author      WooThemes
  8  * @category    API
  9  * @package     WooCommerce/API
 10  * @since       2.0
 11  */
 12 
 13 if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
 14 
 15 class WC_API {
 16 
 17     /** This is the major version for the REST API and takes
 18      * first-order position in endpoint URLs
 19      */
 20     const VERSION = 1;
 21 
 22     /** @var WC_API_Server the REST API server */
 23     public $server;
 24 
 25     /**
 26      * Setup class
 27      *
 28      * @access public
 29      * @since 2.0
 30      * @return WC_API
 31      */
 32     public function __construct() {
 33 
 34         // add query vars
 35         add_filter( 'query_vars', array( $this, 'add_query_vars'), 0 );
 36 
 37         // register API endpoints
 38         add_action( 'init', array( $this, 'add_endpoint'), 0 );
 39 
 40         // handle REST/legacy API request
 41         add_action( 'parse_request', array( $this, 'handle_api_requests'), 0 );
 42     }
 43 
 44     /**
 45      * add_query_vars function.
 46      *
 47      * @access public
 48      * @since 2.0
 49      * @param $vars
 50      * @return array
 51      */
 52     public function add_query_vars( $vars ) {
 53         $vars[] = 'wc-api';
 54         $vars[] = 'wc-api-route';
 55         return $vars;
 56     }
 57 
 58     /**
 59      * add_endpoint function.
 60      *
 61      * @access public
 62      * @since 2.0
 63      * @return void
 64      */
 65     public function add_endpoint() {
 66 
 67         // REST API
 68         add_rewrite_rule( '^wc-api\/v' . self::VERSION . '/?$', 'index.php?wc-api-route=/', 'top' );
 69         add_rewrite_rule( '^wc-api\/v' . self::VERSION .'(.*)?', 'index.php?wc-api-route=$matches[1]', 'top' );
 70 
 71         // legacy API for payment gateway IPNs
 72         add_rewrite_endpoint( 'wc-api', EP_ALL );
 73     }
 74 
 75 
 76     /**
 77      * API request - Trigger any API requests
 78      *
 79      * @access public
 80      * @since 2.0
 81      * @return void
 82      */
 83     public function handle_api_requests() {
 84         global $wp;
 85 
 86         if ( ! empty( $_GET['wc-api'] ) )
 87             $wp->query_vars['wc-api'] = $_GET['wc-api'];
 88 
 89         if ( ! empty( $_GET['wc-api-route'] ) )
 90             $wp->query_vars['wc-api-route'] = $_GET['wc-api-route'];
 91 
 92         // REST API request
 93         if ( ! empty( $wp->query_vars['wc-api-route'] ) ) {
 94 
 95             define( 'WC_API_REQUEST', true );
 96 
 97             // load required files
 98             $this->includes();
 99 
100             $this->server = new WC_API_Server( $wp->query_vars['wc-api-route'] );
101 
102             // load API resource classes
103             $this->register_resources( $this->server );
104 
105             // Fire off the request
106             $this->server->serve_request();
107 
108             exit;
109         }
110 
111         // legacy API requests
112         if ( ! empty( $wp->query_vars['wc-api'] ) ) {
113 
114             // Buffer, we won't want any output here
115             ob_start();
116 
117             // Get API trigger
118             $api = strtolower( esc_attr( $wp->query_vars['wc-api'] ) );
119 
120             // Load class if exists
121             if ( class_exists( $api ) )
122                 $api_class = new $api();
123 
124             // Trigger actions
125             do_action( 'woocommerce_api_' . $api );
126 
127             // Done, clear buffer and exit
128             ob_end_clean();
129             die('1');
130         }
131     }
132 
133 
134     /**
135      * Include required files for REST API request
136      *
137      * @since 2.1
138      */
139     private function includes() {
140 
141         // API server / response handlers
142         include_once( 'api/class-wc-api-server.php' );
143         include_once( 'api/interface-wc-api-handler.php' );
144         include_once( 'api/class-wc-api-json-handler.php' );
145         include_once( 'api/class-wc-api-xml-handler.php' );
146 
147         // authentication
148         include_once( 'api/class-wc-api-authentication.php' );
149         $this->authentication = new WC_API_Authentication();
150 
151         include_once( 'api/class-wc-api-resource.php' );
152         include_once( 'api/class-wc-api-orders.php' );
153         include_once( 'api/class-wc-api-products.php' );
154         include_once( 'api/class-wc-api-coupons.php' );
155         include_once( 'api/class-wc-api-customers.php' );
156         include_once( 'api/class-wc-api-reports.php' );
157 
158         // allow plugins to load other response handlers or resource classes
159         do_action( 'woocommerce_api_loaded' );
160     }
161 
162     /**
163      * Register available API resources
164      *
165      * @since 2.1
166      * @param object $server the REST server
167      */
168     public function register_resources( $server ) {
169 
170         $api_classes = apply_filters( 'woocommerce_api_classes',
171             array(
172                 'WC_API_Customers',
173                 'WC_API_Orders',
174                 'WC_API_Products',
175                 'WC_API_Coupons',
176                 'WC_API_Reports',
177             )
178         );
179 
180         foreach ( $api_classes as $api_class ) {
181             $this->$api_class = new $api_class( $server );
182         }
183     }
184 
185 }
186 
WooCommerce API documentation generated by ApiGen 2.8.0