|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* Manage user-related functions. This class receives notifications when a post is created/updated and pushes the author's |
|
5
|
|
|
* data to the triple store. It does NOT receive notification when a user is create/updated because we don't want to send |
|
6
|
|
|
* to the triple stores users that eventually do not write posts (therefore if user data change, the triple store is updated |
|
7
|
|
|
* only when the user creates/updates a new post). |
|
8
|
|
|
* |
|
9
|
|
|
* @since 3.1.7 |
|
10
|
|
|
*/ |
|
11
|
|
|
class Wordlift_User_Service { |
|
|
|
|
|
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* The meta key where the user's URI is stored. |
|
15
|
|
|
* |
|
16
|
|
|
* @since 3.1.7 |
|
17
|
|
|
*/ |
|
18
|
|
|
const URI_META_KEY = '_wl_uri'; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* The Log service. |
|
22
|
|
|
* |
|
23
|
|
|
* @since 3.1.7 |
|
24
|
|
|
* @access private |
|
25
|
|
|
* @var \Wordlift_Log_Service $log_service The Log service. |
|
26
|
|
|
*/ |
|
27
|
|
|
private $log_service; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* The singleton instance of the User service. |
|
31
|
|
|
* |
|
32
|
|
|
* @since 3.1.7 |
|
33
|
|
|
* @access private |
|
34
|
|
|
* @var \Wordlift_User_Service $user_service The singleton instance of the User service. |
|
35
|
|
|
*/ |
|
36
|
|
|
private static $instance; |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* Create an instance of the User service. |
|
40
|
|
|
* |
|
41
|
|
|
* @since 3.1.7 |
|
42
|
|
|
*/ |
|
43
|
|
|
public function __construct() { |
|
44
|
|
|
|
|
45
|
|
|
$this->log_service = Wordlift_Log_Service::get_logger( 'Wordlift_User_Service' ); |
|
46
|
|
|
|
|
47
|
|
|
self::$instance = $this; |
|
48
|
|
|
|
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* Get the singleton instance of the User service. |
|
53
|
|
|
* |
|
54
|
|
|
* @since 3.1.7 |
|
55
|
|
|
* @return \Wordlift_User_Service The singleton instance of the User service. |
|
56
|
|
|
*/ |
|
57
|
|
|
public static function get_instance() { |
|
58
|
|
|
|
|
59
|
|
|
return self::$instance; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* Get the URI for a user. |
|
64
|
|
|
* |
|
65
|
|
|
* @since 3.1.7 |
|
66
|
|
|
* |
|
67
|
|
|
* @param int $user_id The user id |
|
68
|
|
|
* |
|
69
|
|
|
* @return false|string The user's URI or false in case of failure. |
|
70
|
|
|
*/ |
|
71
|
|
|
public function get_uri( $user_id ) { |
|
72
|
|
|
|
|
73
|
|
|
// Try to get the URI stored in the user's meta and return it if available. |
|
74
|
|
|
if ( false !== ( $user_uri = $this->_get_uri( $user_id ) ) ) { |
|
75
|
|
|
return $user_uri; |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
// Try to build an URI, return false in case of failure. |
|
79
|
|
|
if ( false === ( $user_uri = $this->_build_uri( $user_id ) ) ) { |
|
80
|
|
|
return false; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
// Store the URI for future requests (we need a "permanent" URI). |
|
84
|
|
|
$this->_set_uri( $user_id, $user_uri ); |
|
85
|
|
|
|
|
86
|
|
|
return $user_uri; |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
/** |
|
90
|
|
|
* Receives wp_insert_post events. |
|
91
|
|
|
* |
|
92
|
|
|
* @since 3.1.7 |
|
93
|
|
|
* |
|
94
|
|
|
* @param int $post_id Post ID. |
|
95
|
|
|
* @param WP_Post $post Post object. |
|
96
|
|
|
* @param bool $update Whether this is an existing post being updated or not. |
|
97
|
|
|
*/ |
|
98
|
|
|
public function wp_insert_post( $post_id, $post, $update ) { |
|
|
|
|
|
|
99
|
|
|
|
|
100
|
|
|
// If the post is not published, return. |
|
101
|
|
|
if ( 'publish' !== get_post_status( $post_id ) ) { |
|
102
|
|
|
return; |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
// We expect a numeric author id. |
|
106
|
|
|
if ( ! is_numeric( $post->post_author ) ) { |
|
107
|
|
|
return; |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
// Get the delete query,or return in case of failure. |
|
111
|
|
|
if ( false === ( $delete = $this->get_delete_query( $post->post_author ) ) ) { |
|
112
|
|
|
return; |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
// Get the insert query,or return in case of failure. |
|
116
|
|
|
if ( false === ( $insert = $this->get_insert_query( $post->post_author ) ) ) { |
|
117
|
|
|
return; |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
// Send the query to the triple store. |
|
121
|
|
|
rl_execute_sparql_update_query( $delete . $insert ); |
|
122
|
|
|
|
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
/** |
|
126
|
|
|
* Get the user's URI stored in the user's meta. |
|
127
|
|
|
* |
|
128
|
|
|
* @since 3.1.7 |
|
129
|
|
|
* |
|
130
|
|
|
* @param int $user_id The user id. |
|
131
|
|
|
* |
|
132
|
|
|
* @return false|string The user's URI or false if not found. |
|
133
|
|
|
*/ |
|
134
|
|
|
private function _get_uri( $user_id ) { |
|
|
|
|
|
|
135
|
|
|
|
|
136
|
|
|
$user_uri = get_user_meta( $user_id, self::URI_META_KEY, true ); |
|
137
|
|
|
|
|
138
|
|
|
if ( empty( $user_uri ) ) { |
|
139
|
|
|
return false; |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
return $user_uri; |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
/** |
|
146
|
|
|
* Build an URI for a user. |
|
147
|
|
|
* |
|
148
|
|
|
* @since 3.1.7 |
|
149
|
|
|
* |
|
150
|
|
|
* @param int $user_id The user's id. |
|
151
|
|
|
* |
|
152
|
|
|
* @return false|string The user's URI or false in case of failure. |
|
153
|
|
|
*/ |
|
154
|
|
|
private function _build_uri( $user_id ) { |
|
|
|
|
|
|
155
|
|
|
|
|
156
|
|
|
// Get the user, return false in case of failure. |
|
157
|
|
|
if ( false === ( $user = get_userdata( $user_id ) ) ) { |
|
158
|
|
|
return false; |
|
159
|
|
|
}; |
|
160
|
|
|
|
|
161
|
|
|
// If the nicename is not set, return a failure. |
|
162
|
|
|
if ( empty( $user->user_nicename ) ) { |
|
163
|
|
|
return false; |
|
164
|
|
|
} |
|
165
|
|
|
|
|
166
|
|
|
return wl_configuration_get_redlink_dataset_uri() . "/user/$user->user_nicename"; |
|
167
|
|
|
} |
|
168
|
|
|
|
|
169
|
|
|
/** |
|
170
|
|
|
* Store the URI in user's meta. |
|
171
|
|
|
* |
|
172
|
|
|
* @since 3.1.7 |
|
173
|
|
|
* |
|
174
|
|
|
* @param int $user_id The user's id. |
|
175
|
|
|
* @param string $user_uri The user's uri. |
|
176
|
|
|
* |
|
177
|
|
|
* @return int|bool Meta ID if the key didn't exist, true on successful update, false on failure. |
|
178
|
|
|
*/ |
|
179
|
|
|
private function _set_uri( $user_id, $user_uri ) { |
|
|
|
|
|
|
180
|
|
|
|
|
181
|
|
|
return update_user_meta( $user_id, self::URI_META_KEY, $user_uri ); |
|
182
|
|
|
} |
|
183
|
|
|
|
|
184
|
|
|
/** |
|
185
|
|
|
* Get the delete query. |
|
186
|
|
|
* |
|
187
|
|
|
* @since 3.1.7 |
|
188
|
|
|
* |
|
189
|
|
|
* @param int $user_id The user id. |
|
190
|
|
|
* |
|
191
|
|
|
* @return false|string The delete query or false in case of failure. |
|
192
|
|
|
*/ |
|
193
|
|
|
private function get_delete_query( $user_id ) { |
|
194
|
|
|
|
|
195
|
|
|
// Get the URI, return if there's none. |
|
196
|
|
|
if ( false === ( $user_uri = $this->get_uri( $user_id ) ) ) { |
|
197
|
|
|
return false; |
|
198
|
|
|
} |
|
199
|
|
|
|
|
200
|
|
|
// Build the delete query. |
|
201
|
|
|
$query = Wordlift_Query_Builder::new_instance()->delete() |
|
202
|
|
|
->statement( $user_uri, Wordlift_Query_Builder::RDFS_TYPE_URI, '?o' ) |
|
203
|
|
|
->build() |
|
204
|
|
|
. Wordlift_Query_Builder::new_instance()->delete() |
|
205
|
|
|
->statement( $user_uri, Wordlift_Query_Builder::RDFS_LABEL_URI, '?o' ) |
|
206
|
|
|
->build() |
|
207
|
|
|
. Wordlift_Query_Builder::new_instance()->delete() |
|
208
|
|
|
->statement( $user_uri, Wordlift_Query_Builder::SCHEMA_GIVEN_NAME_URI, '?o' ) |
|
209
|
|
|
->build() |
|
210
|
|
|
. Wordlift_Query_Builder::new_instance()->delete() |
|
211
|
|
|
->statement( $user_uri, Wordlift_Query_Builder::SCHEMA_FAMILY_NAME_URI, '?o' ) |
|
212
|
|
|
->build() |
|
213
|
|
|
. Wordlift_Query_Builder::new_instance()->delete() |
|
214
|
|
|
->statement( $user_uri, Wordlift_Query_Builder::SCHEMA_URL_URI, '?o' ) |
|
215
|
|
|
->build(); |
|
216
|
|
|
|
|
217
|
|
|
return $query; |
|
218
|
|
|
} |
|
219
|
|
|
|
|
220
|
|
|
/** |
|
221
|
|
|
* Get the insert query. |
|
222
|
|
|
* |
|
223
|
|
|
* @since 3.1.7 |
|
224
|
|
|
* |
|
225
|
|
|
* @param int $user_id The user id. |
|
226
|
|
|
* |
|
227
|
|
|
* @return false|string The insert query or false in case of failure. |
|
228
|
|
|
*/ |
|
229
|
|
|
private function get_insert_query( $user_id ) { |
|
230
|
|
|
|
|
231
|
|
|
// Get the URI, return if there's none. |
|
232
|
|
|
if ( false === ( $user_uri = $this->get_uri( $user_id ) ) ) { |
|
233
|
|
|
return false; |
|
234
|
|
|
} |
|
235
|
|
|
|
|
236
|
|
|
// Try to get the user data, in case of failure return false. |
|
237
|
|
|
if ( false === ( $user = get_userdata( $user_id ) ) ) { |
|
238
|
|
|
return false; |
|
239
|
|
|
}; |
|
240
|
|
|
|
|
241
|
|
|
// Build the insert query. |
|
242
|
|
|
$query = Wordlift_Query_Builder::new_instance() |
|
243
|
|
|
->insert() |
|
244
|
|
|
->statement( $user_uri, Wordlift_Query_Builder::RDFS_TYPE_URI, Wordlift_Query_Builder::SCHEMA_PERSON_URI ) |
|
245
|
|
|
->statement( $user_uri, Wordlift_Query_Builder::RDFS_LABEL_URI, $user->display_name ) |
|
246
|
|
|
->statement( $user_uri, Wordlift_Query_Builder::SCHEMA_GIVEN_NAME_URI, $user->user_firstname ) |
|
247
|
|
|
->statement( $user_uri, Wordlift_Query_Builder::SCHEMA_FAMILY_NAME_URI, $user->user_lastname ) |
|
248
|
|
|
->statement( $user_uri, Wordlift_Query_Builder::SCHEMA_URL_URI, ( ! empty( $user->user_url ) ? $user->user_url : get_author_posts_url( $user_id ) ) ) |
|
249
|
|
|
->build(); |
|
250
|
|
|
|
|
251
|
|
|
return $query; |
|
252
|
|
|
} |
|
253
|
|
|
|
|
254
|
|
|
} |
|
|
|
|
|
|
255
|
|
|
|