|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Wordlift\Dataset\Background; |
|
4
|
|
|
|
|
5
|
|
|
use WP_REST_Server; |
|
6
|
|
|
|
|
7
|
|
|
class Sync_Background_Process_Wpjson_Endpoint { |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* @var Sync_Background_Process |
|
11
|
|
|
*/ |
|
12
|
|
|
private $sync_background_process; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* Sync_Background_Process_Wpjson_Endpoint constructor. |
|
16
|
|
|
* |
|
17
|
|
|
* @param Sync_Background_Process $sync_background_process |
|
18
|
|
|
*/ |
|
19
|
|
|
function __construct( $sync_background_process ) { |
|
|
|
|
|
|
20
|
|
|
|
|
21
|
|
|
add_action( 'rest_api_init', array( $this, 'rest_api_init' ) ); |
|
22
|
|
|
|
|
23
|
|
|
$this->sync_background_process = $sync_background_process; |
|
24
|
|
|
|
|
25
|
|
|
} |
|
26
|
|
|
|
|
27
|
|
|
function rest_api_init() { |
|
|
|
|
|
|
28
|
|
|
|
|
29
|
|
|
register_rest_route( 'wordlift/v1', '/dataset/background/sync', array( |
|
30
|
|
|
'methods' => WP_REST_Server::CREATABLE, |
|
31
|
|
|
'callback' => array( $this->sync_background_process, 'start' ), |
|
32
|
|
|
'permission_callback' => function () { |
|
33
|
|
|
$user = wp_get_current_user(); |
|
34
|
|
|
|
|
35
|
|
|
return in_array( 'administrator', (array) $user->roles ); |
|
36
|
|
|
} |
|
37
|
|
|
) ); |
|
38
|
|
|
|
|
39
|
|
|
register_rest_route( 'wordlift/v1', '/dataset/background/sync', array( |
|
40
|
|
|
'methods' => WP_REST_Server::READABLE, |
|
41
|
|
|
'callback' => array( $this->sync_background_process, 'get_info' ), |
|
42
|
|
|
'permission_callback' => function () { |
|
43
|
|
|
$user = wp_get_current_user(); |
|
44
|
|
|
|
|
45
|
|
|
return in_array( 'administrator', (array) $user->roles ); |
|
46
|
|
|
} |
|
47
|
|
|
) ); |
|
48
|
|
|
|
|
49
|
|
|
register_rest_route( 'wordlift/v1', '/dataset/background/sync', array( |
|
50
|
|
|
'methods' => WP_REST_Server::DELETABLE, |
|
51
|
|
|
'callback' => array( $this->sync_background_process, 'stop' ), |
|
52
|
|
|
'permission_callback' => function () { |
|
53
|
|
|
$user = wp_get_current_user(); |
|
54
|
|
|
|
|
55
|
|
|
return in_array( 'administrator', (array) $user->roles ); |
|
56
|
|
|
} |
|
57
|
|
|
) ); |
|
58
|
|
|
|
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
} |
|
62
|
|
|
|
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.