|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* @since 1.0.0 |
|
4
|
|
|
* @author Naveen Muthusamy <[email protected]> |
|
5
|
|
|
*/ |
|
6
|
|
|
|
|
7
|
|
|
namespace Wordlift\Vocabulary\Api; |
|
8
|
|
|
|
|
9
|
|
|
use Wordlift\Vocabulary\Data\Entity_List\Entity_List_Factory; |
|
10
|
|
|
use WP_REST_Server; |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* @since 1.0.0 |
|
14
|
|
|
* @author Naveen Muthusamy <[email protected]> |
|
15
|
|
|
*/ |
|
16
|
|
|
class Entity_Rest_Endpoint { |
|
17
|
|
|
|
|
18
|
|
|
const SAME_AS_META_KEY = 'entity_same_as'; |
|
19
|
|
|
const ALTERNATIVE_LABEL_META_KEY = '_wl_alt_label'; |
|
20
|
|
|
const DESCRIPTION_META_KEY = 'entity_description'; |
|
21
|
|
|
const TYPE_META_KEY = 'entity_type'; |
|
22
|
|
|
const EXTERNAL_ENTITY_META_KEY = '_wl_is_external'; |
|
23
|
|
|
const IGNORE_TAG_FROM_LISTING = '_wl_cmkg_ignore_tag_from_ui'; |
|
24
|
|
|
|
|
25
|
|
|
|
|
26
|
|
|
public function register_routes() { |
|
27
|
|
|
$that = $this; |
|
28
|
|
|
add_action( 'rest_api_init', |
|
29
|
|
|
function () use ( $that ) { |
|
30
|
|
|
$that->register_accept_route(); |
|
31
|
|
|
$that->register_undo_route(); |
|
32
|
|
|
$that->register_nomatch_route(); |
|
33
|
|
|
$that->register_reject_route(); |
|
34
|
|
|
} ); |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
|
|
38
|
|
View Code Duplication |
public function accept_entity( $request ) { |
|
|
|
|
|
|
39
|
|
|
$data = $request->get_params(); |
|
40
|
|
|
$term_id = (int) $data['term_id']; |
|
41
|
|
|
$entity_data = (array) $data['entity']; |
|
42
|
|
|
$entity = Entity_List_Factory::get_instance( $term_id ); |
|
43
|
|
|
$entity->save_jsonld_data( $entity_data ); |
|
44
|
|
|
update_term_meta( $term_id, self::IGNORE_TAG_FROM_LISTING, 1 ); |
|
45
|
|
|
|
|
46
|
|
|
return $term_id; |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
public function undo( $request ) { |
|
50
|
|
|
$data = $request->get_params(); |
|
51
|
|
|
$term_id = (int) $data['term_id']; |
|
52
|
|
|
$entity = Entity_List_Factory::get_instance( $term_id ); |
|
53
|
|
|
$entity->clear_data(); |
|
54
|
|
|
delete_term_meta( $term_id, self::IGNORE_TAG_FROM_LISTING ); |
|
55
|
|
|
|
|
56
|
|
|
return $term_id; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
|
|
60
|
|
|
public function mark_as_no_match( $request ) { |
|
61
|
|
|
$data = $request->get_params(); |
|
62
|
|
|
$term_id = (int) $data['term_id']; |
|
63
|
|
|
return update_term_meta( $term_id, self::IGNORE_TAG_FROM_LISTING, 1 ); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
|
|
67
|
|
View Code Duplication |
private function register_undo_route() { |
|
|
|
|
|
|
68
|
|
|
register_rest_route( |
|
69
|
|
|
Api_Config::REST_NAMESPACE, |
|
70
|
|
|
'/entity/undo', |
|
71
|
|
|
array( |
|
72
|
|
|
'methods' => WP_REST_Server::CREATABLE, |
|
73
|
|
|
'callback' => array( $this, 'undo' ), |
|
74
|
|
|
'permission_callback' => function () { |
|
75
|
|
|
return current_user_can( 'manage_options' ); |
|
76
|
|
|
}, |
|
77
|
|
|
'args' => array( |
|
78
|
|
|
'term_id' => array( |
|
79
|
|
|
'validate_callback' => function ( $param, $request, $key ) { |
|
|
|
|
|
|
80
|
|
|
return is_numeric( $param ) && $param; |
|
81
|
|
|
}, |
|
82
|
|
|
'required' => true, |
|
83
|
|
|
), |
|
84
|
|
|
), |
|
85
|
|
|
) |
|
86
|
|
|
); |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
private function register_accept_route() { |
|
90
|
|
|
$route = '/entity/accept'; |
|
91
|
|
|
$this->register_entity_accept_or_reject_route( $route, array( $this, 'accept_entity' ) ); |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
private function register_reject_route() { |
|
95
|
|
|
$route = '/entity/reject'; |
|
96
|
|
|
$this->register_entity_accept_or_reject_route( $route, array( $this, 'reject_entity' ) ); |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
View Code Duplication |
public function reject_entity( $request ) { |
|
|
|
|
|
|
100
|
|
|
$data = $request->get_params(); |
|
101
|
|
|
$term_id = (int) $data['term_id']; |
|
102
|
|
|
$entity_data = (array) $data['entity']; |
|
103
|
|
|
$entity = Entity_List_Factory::get_instance( $term_id ); |
|
104
|
|
|
$entity->remove_entity_by_id( $entity_data['@id'] ); |
|
105
|
|
|
return $term_id; |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
View Code Duplication |
private function register_nomatch_route() { |
|
|
|
|
|
|
109
|
|
|
register_rest_route( |
|
110
|
|
|
Api_Config::REST_NAMESPACE, |
|
111
|
|
|
'/entity/no_match', |
|
112
|
|
|
array( |
|
113
|
|
|
'methods' => WP_REST_Server::CREATABLE, |
|
114
|
|
|
'callback' => array( $this, 'mark_as_no_match' ), |
|
115
|
|
|
'permission_callback' => function () { |
|
116
|
|
|
return current_user_can( 'manage_options' ); |
|
117
|
|
|
}, |
|
118
|
|
|
'args' => array( |
|
119
|
|
|
'term_id' => array( |
|
120
|
|
|
'validate_callback' => function ( $param, $request, $key ) { |
|
|
|
|
|
|
121
|
|
|
return is_numeric( $param ) && $param; |
|
122
|
|
|
}, |
|
123
|
|
|
'required' => true, |
|
124
|
|
|
), |
|
125
|
|
|
), |
|
126
|
|
|
) |
|
127
|
|
|
); |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
|
|
131
|
|
|
/** |
|
132
|
|
|
* @param $accept_route |
|
133
|
|
|
*/ |
|
134
|
|
|
private function register_entity_accept_or_reject_route( $accept_route, $callback ) { |
|
135
|
|
|
|
|
136
|
|
|
register_rest_route( |
|
137
|
|
|
Api_Config::REST_NAMESPACE, |
|
138
|
|
|
$accept_route, |
|
139
|
|
|
array( |
|
140
|
|
|
'methods' => WP_REST_Server::CREATABLE, |
|
141
|
|
|
'callback' => $callback, |
|
142
|
|
|
'permission_callback' => function () { |
|
143
|
|
|
return current_user_can( 'manage_options' ); |
|
144
|
|
|
}, |
|
145
|
|
|
'args' => array( |
|
146
|
|
|
'term_id' => array( |
|
147
|
|
|
'validate_callback' => function ( $param, $request, $key ) { |
|
|
|
|
|
|
148
|
|
|
return is_numeric( $param ) && $param; |
|
149
|
|
|
}, |
|
150
|
|
|
'required' => true, |
|
151
|
|
|
), |
|
152
|
|
|
'entity' => array( |
|
153
|
|
|
'validate_callback' => function ( $param, $request, $key ) { |
|
|
|
|
|
|
154
|
|
|
return is_array( $param ); |
|
155
|
|
|
}, |
|
156
|
|
|
'required' => true, |
|
157
|
|
|
), |
|
158
|
|
|
), |
|
159
|
|
|
) |
|
160
|
|
|
); |
|
161
|
|
|
|
|
162
|
|
|
} |
|
163
|
|
|
|
|
164
|
|
|
} |
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.