-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRecentOrders_Api_Endpoint.php
63 lines (54 loc) · 1.46 KB
/
RecentOrders_Api_Endpoint.php
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
<?php
/*
Plugin Name: RecentOrders API Endpoint
Description: https://gist.github.com/thenbrent/6514417
Version: 0.1
Usage: /?wc_recent_orders=<int>
Author: Andrey Kozhuhov
Author URL: http://www.cimpleo.com
*/
class RecentOrders_API_Endpoint {
public function __construct() {
add_filter( 'query_vars', array( $this, 'add_query_vars' ), 0 );
add_action( 'parse_request', array( $this, 'parse' ), 0 );
add_action( 'init', array( $this, 'add_endpoint' ), 0 );
}
public function add_query_vars( $vars ) {
$vars[] = 'wc_recent_orders';
return $vars;
}
public function parse() {
global $wp;
if ( isset( $wp->query_vars['wc_recent_orders'] ) ) {
$this->handle();
exit;
}
}
protected function handle() {
global $wp;
$count = $wp->query_vars['wc_recent_orders'];
is_numeric( $count ) || $count = get_option( 'posts_per_page' );
$this->response( '200 OK', $this->getRecentOrders( $count ) );
}
protected function getRecentOrders( $count ) {
$args = array(
'numberposts' => $count,
'orderby' => 'post_date',
'order' => 'DESC',
'post_type' => 'shop_order',
);
$recent_posts = wp_get_recent_posts( $args );
foreach ( $recent_posts as $post ) {
$orders[] = new WC_Order( $post['ID'] );
}
return $orders;
}
protected function response( $msg, $orders = '' ) {
$response['message'] = $msg;
header( 'content-type: application/json; charset=utf-8' );
echo json_encode( $orders );
exit;
}
}
new RecentOrders_API_Endpoint();
?>