1 <?php
2 3 4 5 6 7 8 9 10 11
12
13 if ( ! defined( 'ABSPATH' ) ) exit;
14
15 class WC_API {
16
17 18 19
20 const VERSION = 1;
21
22
23 public $server;
24
25 26 27 28 29 30 31
32 public function __construct() {
33
34
35 add_filter( 'query_vars', array( $this, 'add_query_vars'), 0 );
36
37
38 add_action( 'init', array( $this, 'add_endpoint'), 0 );
39
40
41 add_action( 'parse_request', array( $this, 'handle_api_requests'), 0 );
42 }
43
44 45 46 47 48 49 50 51
52 public function add_query_vars( $vars ) {
53 $vars[] = 'wc-api';
54 $vars[] = 'wc-api-route';
55 return $vars;
56 }
57
58 59 60 61 62 63 64
65 public function add_endpoint() {
66
67
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
72 add_rewrite_endpoint( 'wc-api', EP_ALL );
73 }
74
75
76 77 78 79 80 81 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
93 if ( ! empty( $wp->query_vars['wc-api-route'] ) ) {
94
95 define( 'WC_API_REQUEST', true );
96
97
98 $this->includes();
99
100 $this->server = new WC_API_Server( $wp->query_vars['wc-api-route'] );
101
102
103 $this->register_resources( $this->server );
104
105
106 $this->server->serve_request();
107
108 exit;
109 }
110
111
112 if ( ! empty( $wp->query_vars['wc-api'] ) ) {
113
114
115 ob_start();
116
117
118 $api = strtolower( esc_attr( $wp->query_vars['wc-api'] ) );
119
120
121 if ( class_exists( $api ) )
122 $api_class = new $api();
123
124
125 do_action( 'woocommerce_api_' . $api );
126
127
128 ob_end_clean();
129 die('1');
130 }
131 }
132
133
134 135 136 137 138
139 private function includes() {
140
141
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
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
159 do_action( 'woocommerce_api_loaded' );
160 }
161
162 163 164 165 166 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