NavigationFavorites.php
4.83 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
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
202
203
204
<?php
/**
* REST API Navigation Favorites controller
*
* Handles requests to the navigation favorites endpoint
*/
namespace Automattic\WooCommerce\Admin\API;
defined( 'ABSPATH' ) || exit;
use Automattic\WooCommerce\Admin\Features\Navigation\Favorites;
/**
* REST API Favorites controller class.
*
* @internal
* @extends WC_REST_CRUD_Controller
*/
class NavigationFavorites extends \WC_REST_Data_Controller {
/**
* Endpoint namespace.
*
* @var string
*/
protected $namespace = 'wc-admin';
/**
* Route base.
*
* @var string
*/
protected $rest_base = 'navigation/favorites';
/**
* Error code to status code mapping.
*
* @var array
*/
protected $error_to_status_map = array(
'woocommerce_favorites_invalid_request' => 400,
'woocommerce_favorites_already_exists' => 409,
'woocommerce_favorites_does_not_exist' => 404,
'woocommerce_favorites_invalid_user' => 400,
'woocommerce_favorites_unauthenticated' => 401,
);
/**
* Register the routes
*/
public function register_routes() {
register_rest_route(
$this->namespace,
'/' . $this->rest_base . '/me',
array(
array(
'methods' => \WP_REST_Server::READABLE,
'callback' => array( $this, 'get_items' ),
'permission_callback' => array( $this, 'current_user_permissions_check' ),
),
array(
'methods' => \WP_REST_Server::CREATABLE,
'callback' => array( $this, 'add_item' ),
'permission_callback' => array( $this, 'current_user_permissions_check' ),
'args' => array(
'item_id' => array(
'required' => true,
),
),
),
array(
'methods' => \WP_REST_Server::DELETABLE,
'callback' => array( $this, 'delete_item' ),
'permission_callback' => array( $this, 'current_user_permissions_check' ),
'args' => array(
'item_id' => array(
'required' => true,
),
),
),
'schema' => array( $this, 'get_public_item_schema' ),
)
);
}
/**
* Get all favorites.
*
* @param WP_REST_Request $request Request data.
* @return WP_REST_Response
*/
public function get_items( $request ) {
$response = Favorites::get_all( get_current_user_id() );
if ( is_wp_error( $response ) || ! $response ) {
return rest_ensure_response( $this->prepare_error( $response ) );
}
return rest_ensure_response(
array_map( 'stripslashes', $response )
);
}
/**
* Add a favorite.
*
* @param WP_REST_Request $request Request data.
* @return WP_REST_Response
*/
public function add_item( $request ) {
$user_id = get_current_user_id();
$fav_id = $request->get_param( 'item_id' );
$user = get_userdata( $user_id );
if ( false === $user ) {
return $this->prepare_error(
new \WP_Error(
'woocommerce_favorites_invalid_user',
__( 'Invalid user_id provided', 'woocommerce' )
)
);
}
$response = Favorites::add_item( $fav_id, $user_id );
if ( is_wp_error( $response ) || ! $response ) {
return rest_ensure_response( $this->prepare_error( $response ) );
}
return rest_ensure_response( Favorites::get_all( $user_id ) );
}
/**
* Delete a favorite.
*
* @param WP_REST_Request $request Request data.
* @return WP_REST_Response
*/
public function delete_item( $request ) {
$user_id = get_current_user_id();
$fav_id = $request->get_param( 'item_id' );
$response = Favorites::remove_item( $fav_id, $user_id );
if ( is_wp_error( $response ) || ! $response ) {
return rest_ensure_response( $this->prepare_error( $response ) );
}
return rest_ensure_response( Favorites::get_all( $user_id ) );
}
/**
* Check whether a given request has permission to create favorites.
*
* @param WP_REST_Request $request Full details about the request.
* @return WP_Error|boolean
*/
public function add_item_permissions_check( $request ) {
return current_user_can( 'edit_users' );
}
/**
* Check whether a given request has permission to delete notes.
*
* @param WP_REST_Request $request Full details about the request.
* @return WP_Error|boolean
*/
public function delete_item_permissions_check( $request ) {
return current_user_can( 'edit_users' );
}
/**
* Always allow for operations that only impact current user
*
* @param WP_REST_Request $request Full details about the request.
* @return WP_Error|boolean
*/
public function current_user_permissions_check( $request ) {
return true;
}
/**
* Accept an instance of WP_Error and add the appropriate data for REST transit.
*
* @param WP_Error $error Error to prepare.
* @return WP_Error
*/
protected function prepare_error( $error ) {
if ( ! is_wp_error( $error ) ) {
return $error;
}
$error->add_data(
array(
'status' => $this->error_to_status_map[ $error->get_error_code() ] ?? 500,
)
);
return $error;
}
}