-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbackbone_api_controller.php
More file actions
110 lines (100 loc) · 3.59 KB
/
backbone_api_controller.php
File metadata and controls
110 lines (100 loc) · 3.59 KB
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
<?php
class BackboneAPIController {
public static $routes;
var $wpdb;
var $_params = false;
function BackboneAPIController() {
global $wpdb;
$this->wpdb = $wpdb;
$this->_routes = static::$routes;
add_action('wp_router_generate_routes', array($this, 'add_routes'), 20);
}
function add_routes($router) {
foreach ($this->_routes as $name => $attrs) {
$router_args = array(
'path' => $attrs['path'],
'template' => false,
'query_vars' => array(),
'page_arguments' => array()
);
if (isset($attrs['callback'])) {
$router_args['page_callback'] = array($this, 'dispatch');
}
if (isset($attrs['params'])) {
$attrs['params'] = is_array($attrs['params']) ? $attrs['params'] : array($attrs['params']);
foreach ($attrs['params'] as $index => $param_name) {
$router_args['query_vars'][$param_name] = $index + 1;
}
}
$router->add_route($name,$router_args);
}
}
function dispatch() {
global $wp;
$this->_current_route_name = $wp->query_vars['WP_Route'];
$this->_current_route = $this->_routes[$this->_current_route_name];
// Set up get and post vars
$this->get = $_GET;
// Map url params into the get array
foreach ($wp->query_vars as $key => $val) {
if ($key != 'WP_Route') {
$this->get[$key] = $val;
}
}
// If the request method is put or if the content type is json,
// php won't fill out the post array with the vars so we handle it ourselves
if ($_SERVER['REQUEST_METHOD'] == 'PUT') {
$php_input = file_get_contents("php://input");
if (strpos($_SERVER['CONTENT_TYPE'],'application/json') !== false) {
$this->post = json_decode($php_input, true);
} else {
parse_str($php_input, $this->post);
}
} elseif ($_SERVER['REQUEST_METHOD'] == 'POST') {
if (strpos($_SERVER['CONTENT_TYPE'],'application/json') !== false) {
$this->post = json_decode(file_get_contents('php://input'), true);
} else {
$this->post = $_POST;
}
}
$method = (isset($_REQUEST['_action'])) ? $_REQUEST['action'] : $_SERVER['REQUEST_METHOD'];
$callback = false;
$before_callback = false;
if ( is_callable(array($this, $this->_current_route['callback']))) {
$callback = array($this, $this->_current_route['callback']);
if (is_callable(array($this, 'before_'.$this->_current_route['callback'])))
$before_callback = array($this, 'before_'.$this->_current_route['callback']);
} else {
if (is_array($this->_current_route['callback'])) {
if ($method && isset($this->_current_route['callback'][$method]) && is_callable(array($this, $this->_current_route['callback'][$method]))) {
$callback = array($this, $this->_current_route['callback'][$method]);
if (is_callable(array($this, 'before_'.$this->_current_route['callback'][$method])))
$before_callback = array($this, 'before_'.$this->_current_route['callback'][$method]);
} else {
$this->respond_404();
}
}
}
if ($before_callback != false) {
call_user_func($before_callback);
}
call_user_func($callback);
}
function respond_404($msg = "Resource could not be found") {
return $this->send_json(array('error' => $msg), 'HTTP/1.0 404 Not Found');
}
function respond_403($msg = "You don't have permission to access this resource") {
return $this->send_json(array('error' => $msg), 'HTTP/1.0 403 Forbidden');
}
function send_json($payload, $extra_headers = false) {
header('Content-Type: application/json');
if ($extra_headers) {
$extra_headers = is_array($extra_headers) ? $extra_headers : array($extra_headers);
foreach ($extra_headers as $extra_header) {
header($extra_header);
}
}
echo json_encode($payload);
exit();
}
}