-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathWPCartAPI.php
More file actions
135 lines (113 loc) · 2.9 KB
/
Copy pathWPCartAPI.php
File metadata and controls
135 lines (113 loc) · 2.9 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
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
<?php
/**
* Class WPCartAPI
*
* Classe para integrar com a API WordPress, a classe WPCart
*/
class WPCartAPI extends WP_REST_Controller {
const ADD = WP_REST_Server::CREATABLE;
const EDIT = WP_REST_Server::EDITABLE;
const DELETE = WP_REST_Server::DELETABLE;
const GET = WP_REST_Server::READABLE;
const ALL = WP_REST_Server::ALLMETHODS;
const ID = "/(?P<id>\d+)";
private $base = UPWPCART_API_BASE;
private $route = '/' . UPWPCART_API_ROUTE;
private $initialized = false;
public $cart = null;
/**
* WPCartAPI constructor.
*
* @param WPCart $cart
*/
function __construct( $cart ) {
$this->cart = $cart;
}
/**
* Register the routes
*/
function register_routes() {
if ( $this->initialized ) {
return false;
}
$this->initialized = true;
$base = $this->base;
$route = $this->route;
// base routes
register_rest_route( $base, $route, array(
array(
'methods' => self::GET,
'callback' => $this->getCallback( 'get' )
),
array(
'methods' => self::ADD,
'callback' => $this->getCallback( 'add' ),
'args' => array(
'id' => array(
'required' => true,
'validate_callback' => array( $this, 'validateNumeric' ),
),
'amount' => array(
'default' => 1,
'validate_callback' => array( $this, 'validateNumeric' ),
)
),
),
array(
'methods' => self::DELETE,
'callback' => $this->getCallback( 'clean' ),
),
) );
// item routes
register_rest_route( $base, $route . self::ID, array(
array(
'methods' => self::GET,
'callback' => $this->getCallback( 'getItem' ),
),
array(
'methods' => self::EDIT,
'callback' => $this->getCallback( 'update' ),
'args' => array(
'amount' => array(
'required' => false,
'validate_callback' => array( $this, 'validateNumeric' ),
)
),
),
array(
'methods' => WP_REST_Server::DELETABLE,
'callback' => array( $this, 'remove' ),
),
) );
return true;
}
public function get() {
return $this->cart->get();
}
public function getItem( WP_REST_Request $request ) {
$id = $request['id'];
return $this->cart->getItem( intval( $id ) );
}
public function add( WP_REST_Request $request ) {
$id = $request['id'];
$amount = $request['amount'];
return $this->cart->add( intval( $id ), intval( $amount ) )->get();
}
public function remove( WP_REST_Request $request ) {
return $this->cart->remove( intval( $request['id'] ) )->get();
}
public function update( WP_REST_Request $request ) {
return $this->cart->update( intval( $request['id'] ), intval( $request['amount'] ) )->get();
}
public function clean() {
return $this->cart->clean()->get();
}
// private methods
private function getCallback( $name ) {
return array( $this, $name );
}
// Validators
public function validateNumeric( $value ) {
return is_numeric( $value );
}
}