|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Wordlift\Dataset; |
|
4
|
|
|
|
|
5
|
|
|
use WP_REST_Server; |
|
6
|
|
|
|
|
7
|
|
|
class Sync_Wpjson_Endpoint { |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* @var Sync_Service |
|
11
|
|
|
*/ |
|
12
|
|
|
private $sync_service; |
|
13
|
|
|
|
|
14
|
|
|
function __construct( $sync_service ) { |
|
|
|
|
|
|
15
|
|
|
|
|
16
|
|
|
add_action( 'rest_api_init', array( $this, 'rest_api_init' ) ); |
|
17
|
|
|
$this->sync_service = $sync_service; |
|
18
|
|
|
} |
|
19
|
|
|
|
|
20
|
|
|
function rest_api_init() { |
|
|
|
|
|
|
21
|
|
|
|
|
22
|
|
|
// register_rest_route() handles more arguments but we are going to stick to the basics for now. |
|
23
|
|
|
register_rest_route( 'wordlift/v1', '/dataset/count', array( |
|
24
|
|
|
// By using this constant we ensure that when the WP_REST_Server changes our readable endpoints will work as intended. |
|
25
|
|
|
'methods' => WP_REST_Server::READABLE, |
|
26
|
|
|
// Here we register our callback. The callback is fired when this endpoint is matched by the WP_REST_Server class. |
|
27
|
|
|
'callback' => array( $this, 'count' ), |
|
28
|
|
|
) ); |
|
29
|
|
|
|
|
30
|
|
|
register_rest_route( 'wordlift/v1', '/dataset/next', array( |
|
31
|
|
|
// By using this constant we ensure that when the WP_REST_Server changes our readable endpoints will work as intended. |
|
32
|
|
|
'methods' => WP_REST_Server::READABLE, |
|
33
|
|
|
// Here we register our callback. The callback is fired when this endpoint is matched by the WP_REST_Server class. |
|
34
|
|
|
'callback' => array( $this, 'next' ), |
|
35
|
|
|
) ); |
|
36
|
|
|
|
|
37
|
|
|
register_rest_route( 'wordlift/v1', '/dataset/info', array( |
|
38
|
|
|
// By using this constant we ensure that when the WP_REST_Server changes our readable endpoints will work as intended. |
|
39
|
|
|
'methods' => WP_REST_Server::READABLE, |
|
40
|
|
|
// Here we register our callback. The callback is fired when this endpoint is matched by the WP_REST_Server class. |
|
41
|
|
|
'callback' => array( $this, 'info' ), |
|
42
|
|
|
) ); |
|
43
|
|
|
|
|
44
|
|
|
// register_rest_route() handles more arguments but we are going to stick to the basics for now. |
|
45
|
|
|
register_rest_route( 'wordlift/v1', '/dataset/sync', array( |
|
46
|
|
|
// By using this constant we ensure that when the WP_REST_Server changes our readable endpoints will work as intended. |
|
47
|
|
|
'methods' => WP_REST_Server::CREATABLE, |
|
48
|
|
|
// Here we register our callback. The callback is fired when this endpoint is matched by the WP_REST_Server class. |
|
49
|
|
|
'callback' => array( $this, 'post__dataset__sync' ), |
|
50
|
|
|
) ); |
|
51
|
|
|
|
|
52
|
|
|
// register_rest_route() handles more arguments but we are going to stick to the basics for now. |
|
53
|
|
|
register_rest_route( 'wordlift/v1', '/dataset/sync', array( |
|
54
|
|
|
// By using this constant we ensure that when the WP_REST_Server changes our readable endpoints will work as intended. |
|
55
|
|
|
'methods' => WP_REST_Server::DELETABLE, |
|
56
|
|
|
// Here we register our callback. The callback is fired when this endpoint is matched by the WP_REST_Server class. |
|
57
|
|
|
'callback' => array( $this, 'delete__dataset__sync' ), |
|
58
|
|
|
) ); |
|
59
|
|
|
|
|
60
|
|
|
|
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
function count() { |
|
|
|
|
|
|
64
|
|
|
return rest_ensure_response( $this->sync_service->count() ); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
function next() { |
|
|
|
|
|
|
68
|
|
|
return rest_ensure_response( $this->sync_service->next() ); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
function post__dataset__sync() { |
|
|
|
|
|
|
72
|
|
|
|
|
73
|
|
|
$this->sync_service->start(); |
|
74
|
|
|
|
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
function delete__dataset__sync() { |
|
|
|
|
|
|
78
|
|
|
|
|
79
|
|
|
$this->sync_service->request_cancel(); |
|
80
|
|
|
|
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
function info() { |
|
|
|
|
|
|
84
|
|
|
return rest_ensure_response( $this->sync_service->info() ); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
} |
|
88
|
|
|
|
Adding explicit visibility (
private,protected, orpublic) is generally recommend to communicate to other developers how, and from where this method is intended to be used.