|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* @since 1.1.0 |
|
4
|
|
|
* @author Naveen Muthusamy <[email protected]> |
|
5
|
|
|
*/ |
|
6
|
|
|
|
|
7
|
|
|
namespace Wordlift\Vocabulary\Api; |
|
8
|
|
|
|
|
9
|
|
|
use Wordlift\Vocabulary\Analysis_Background_Process; |
|
10
|
|
|
use Wordlift\Vocabulary\Analysis_Background_Service; |
|
11
|
|
|
use Wordlift\Vocabulary\Options_Cache; |
|
12
|
|
|
use Wordlift\Vocabulary\Sync_State; |
|
13
|
|
|
use WP_REST_Server; |
|
14
|
|
|
|
|
15
|
|
|
class Background_Analysis_Endpoint { |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* @var Analysis_Background_Service |
|
19
|
|
|
*/ |
|
20
|
|
|
private $background_service; |
|
21
|
|
|
/** |
|
22
|
|
|
* @var Options_Cache |
|
23
|
|
|
*/ |
|
24
|
|
|
private $cache_service; |
|
25
|
|
|
|
|
26
|
|
|
public function __construct( $background_service, $cache_service ) { |
|
27
|
|
|
$this->background_service = $background_service; |
|
28
|
|
|
$this->cache_service = $cache_service; |
|
29
|
|
|
$this->register_routes(); |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
public function register_routes() { |
|
33
|
|
|
$that = $this; |
|
34
|
|
|
add_action( 'rest_api_init', |
|
35
|
|
|
function () use ( $that ) { |
|
36
|
|
|
$that->register_start_route(); |
|
37
|
|
|
$that->register_stop_route(); |
|
38
|
|
|
$that->register_stats_route(); |
|
39
|
|
|
$that->register_restart_route(); |
|
40
|
|
|
} ); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
View Code Duplication |
private function register_start_route() { |
|
|
|
|
|
|
44
|
|
|
register_rest_route( |
|
45
|
|
|
Api_Config::REST_NAMESPACE, |
|
46
|
|
|
'/background_analysis/start', |
|
47
|
|
|
array( |
|
48
|
|
|
'methods' => WP_REST_Server::CREATABLE, |
|
49
|
|
|
'callback' => array( $this, 'start' ), |
|
50
|
|
|
'permission_callback' => function () { |
|
51
|
|
|
return current_user_can( 'manage_options' ); |
|
52
|
|
|
}, |
|
53
|
|
|
) |
|
54
|
|
|
); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
View Code Duplication |
private function register_stop_route() { |
|
|
|
|
|
|
58
|
|
|
register_rest_route( |
|
59
|
|
|
Api_Config::REST_NAMESPACE, |
|
60
|
|
|
'/background_analysis/stop', |
|
61
|
|
|
array( |
|
62
|
|
|
'methods' => WP_REST_Server::CREATABLE, |
|
63
|
|
|
'callback' => array( $this, 'stop' ), |
|
64
|
|
|
'permission_callback' => function () { |
|
65
|
|
|
return current_user_can( 'manage_options' ); |
|
66
|
|
|
}, |
|
67
|
|
|
) |
|
68
|
|
|
); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
View Code Duplication |
private function register_stats_route() { |
|
|
|
|
|
|
72
|
|
|
register_rest_route( |
|
73
|
|
|
Api_Config::REST_NAMESPACE, |
|
74
|
|
|
'/background_analysis/stats', |
|
75
|
|
|
array( |
|
76
|
|
|
'methods' => WP_REST_Server::CREATABLE, |
|
77
|
|
|
'callback' => array( $this, 'get_stats' ), |
|
78
|
|
|
'permission_callback' => function () { |
|
79
|
|
|
return current_user_can( 'manage_options' ); |
|
80
|
|
|
}, |
|
81
|
|
|
) |
|
82
|
|
|
); |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
|
|
86
|
|
View Code Duplication |
private function register_restart_route() { |
|
|
|
|
|
|
87
|
|
|
register_rest_route( |
|
88
|
|
|
Api_Config::REST_NAMESPACE, |
|
89
|
|
|
'/background_analysis/restart', |
|
90
|
|
|
array( |
|
91
|
|
|
'methods' => WP_REST_Server::CREATABLE, |
|
92
|
|
|
'callback' => array( $this, 'restart' ), |
|
93
|
|
|
'permission_callback' => function () { |
|
94
|
|
|
return current_user_can( 'manage_options' ); |
|
95
|
|
|
}, |
|
96
|
|
|
) |
|
97
|
|
|
); |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
|
|
101
|
|
|
public function get_stats() { |
|
102
|
|
|
/** |
|
103
|
|
|
* @var $state Sync_State |
|
104
|
|
|
*/ |
|
105
|
|
|
$state = get_option( Analysis_Background_Process::WL_CMKG_ANALYSIS_BACKGROUND_PROCESS, Sync_State::unknown() ); |
|
106
|
|
|
|
|
107
|
|
|
return $state->get_array(); |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
public function start() { |
|
111
|
|
|
$this->background_service->start(); |
|
112
|
|
|
|
|
113
|
|
|
return true; |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
|
|
117
|
|
|
public function restart() { |
|
118
|
|
|
$this->background_service->cancel(); |
|
119
|
|
|
// clear the flags and restart again. |
|
120
|
|
|
global $wpdb; |
|
121
|
|
|
$query = <<<EOF |
|
122
|
|
|
DELETE FROM $wpdb->termmeta WHERE meta_key=%s OR meta_key=%s |
|
123
|
|
|
EOF; |
|
124
|
|
|
// Remove the flags, if the tag is already accepted we wont remove that ui flag. |
|
125
|
|
|
$query = $wpdb->prepare( $query, array( |
|
126
|
|
|
Analysis_Background_Service::ANALYSIS_DONE_FLAG, |
|
127
|
|
|
Analysis_Background_Service::ENTITIES_PRESENT_FOR_TERM |
|
128
|
|
|
) ); |
|
129
|
|
|
$wpdb->query( $query ); |
|
130
|
|
|
// clear the cache |
|
131
|
|
|
$this->cache_service->flush_all(); |
|
132
|
|
|
$this->background_service->start(); |
|
133
|
|
|
|
|
134
|
|
|
return array( 'status' => 'restart_complete' ); |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
|
|
|
|
138
|
|
|
public function stop() { |
|
139
|
|
|
$this->background_service->stop(); |
|
140
|
|
|
|
|
141
|
|
|
return true; |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
|
|
|
|
145
|
|
|
} |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.