Completed
Push — develop ( d4ea35...6805d7 )
by David
10:37
created

Wordlift_User_Service::get_entity()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 4
rs 10
c 0
b 0
f 0
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 meta key holding the entity id representing a {@link WP_User}.
22
	 *
23
	 * @since 3.14.0
24
	 */
25
	const ENTITY_META_KEY = '_wl_entity';
26
27
	/**
28
	 * The Log service.
29
	 *
30
	 * @since  3.1.7
31
	 * @access private
32
	 * @var \Wordlift_Log_Service $log_service The Log service.
33
	 */
34
	private $log_service;
35
36
	/**
37
	 * The singleton instance of the User service.
38
	 *
39
	 * @since  3.1.7
40
	 * @access private
41
	 * @var \Wordlift_User_Service $user_service The singleton instance of the User service.
42
	 */
43
	private static $instance;
44
45
	/**
46
	 * Create an instance of the User service.
47
	 *
48
	 * @since 3.1.7
49
	 */
50
	public function __construct() {
51
52
		$this->log_service = Wordlift_Log_Service::get_logger( 'Wordlift_User_Service' );
53
54
		self::$instance = $this;
55
56
	}
57
58
	/**
59
	 * Get the singleton instance of the User service.
60
	 *
61
	 * @since 3.1.7
62
	 * @return \Wordlift_User_Service The singleton instance of the User service.
63
	 */
64
	public static function get_instance() {
65
66
		return self::$instance;
67
	}
68
69
	/**
70
	 * Get the URI for a user.
71
	 *
72
	 * @since 3.1.7
73
	 *
74
	 * @param int $user_id The user id
75
	 *
76
	 * @return false|string The user's URI or false in case of failure.
77
	 */
78
	public function get_uri( $user_id ) {
79
80
		// Try to get the URI stored in the user's meta and return it if available.
81
		if ( false !== ( $user_uri = $this->_get_uri( $user_id ) ) ) {
82
			return $user_uri;
83
		}
84
85
		// Try to build an URI, return false in case of failure.
86
		if ( false === ( $user_uri = $this->_build_uri( $user_id ) ) ) {
87
			return false;
88
		}
89
90
		// Store the URI for future requests (we need a "permanent" URI).
91
		$this->_set_uri( $user_id, $user_uri );
92
93
		return $user_uri;
94
	}
95
96
	/**
97
	 * Receives wp_insert_post events.
98
	 *
99
	 * @since 3.1.7
100
	 *
101
	 * @param int     $post_id Post ID.
102
	 * @param WP_Post $post    Post object.
103
	 * @param bool    $update  Whether this is an existing post being updated or not.
104
	 */
105
	public function wp_insert_post( $post_id, $post, $update ) {
0 ignored issues
show
Unused Code introduced by
The parameter $update is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
106
107
		// If the post is not published, return.
108
		if ( 'publish' !== get_post_status( $post_id ) ) {
109
			return;
110
		}
111
112
		// We expect a numeric author id.
113
		if ( ! is_numeric( $post->post_author ) ) {
114
			return;
115
		}
116
117
		// Get the delete query,or return in case of failure.
118
		if ( false === ( $delete = $this->get_delete_query( $post->post_author ) ) ) {
119
			return;
120
		}
121
122
		// Get the insert query,or return in case of failure.
123
		if ( false === ( $insert = $this->get_insert_query( $post->post_author ) ) ) {
124
			return;
125
		}
126
127
		// Send the query to the triple store.
128
		rl_execute_sparql_update_query( $delete . $insert );
129
130
	}
131
132
	/**
133
	 * Set the `id` of the entity representing a {@link WP_User}.
134
	 *
135
	 * If the `id` is set to 0 (or less) then the meta is deleted.
136
	 *
137
	 * @since 3.14.0
138
	 *
139
	 * @param int $user_id The {@link WP_User}.
140
	 * @param int $value   The entity {@link WP_Post} `id`.
141
	 *
142
	 * @return bool|int  Meta ID if the key didn't exist, true on successful update, false on failure.
143
	 */
144
	public function set_entity( $user_id, $value ) {
145
146
		return 0 < $value
147
			? update_user_meta( $user_id, self::ENTITY_META_KEY, $value )
148
			: delete_user_meta( $user_id, self::ENTITY_META_KEY );
149
	}
150
151
	/**
152
	 * Get the {@link WP_Post} `id` of the entity representing a {@link WP_User}.
153
	 *
154
	 * @since 3.14.0
155
	 *
156
	 * @param int $user_id The {@link WP_User}'s `id`.
157
	 *
158
	 * @return string The entity {@link WP_Post} `id` or an empty string if not set.
159
	 */
160
	public function get_entity( $user_id ) {
161
162
		return get_user_meta( $user_id, self::ENTITY_META_KEY, true );
163
	}
164
165
	/**
166
	 * Get the user's URI stored in the user's meta.
167
	 *
168
	 * @since 3.1.7
169
	 *
170
	 * @param int $user_id The user id.
171
	 *
172
	 * @return false|string The user's URI or false if not found.
173
	 */
174
	private function _get_uri( $user_id ) {
175
176
		$user_uri = get_user_meta( $user_id, self::URI_META_KEY, true );
177
178
		if ( empty( $user_uri ) ) {
179
			return false;
180
		}
181
182
		return $user_uri;
183
	}
184
185
	/**
186
	 * Build an URI for a user.
187
	 *
188
	 * @since 3.1.7
189
	 *
190
	 * @param int $user_id The user's id.
191
	 *
192
	 * @return false|string The user's URI or false in case of failure.
193
	 */
194
	private function _build_uri( $user_id ) {
195
196
		// Get the user, return false in case of failure.
197
		if ( false === ( $user = get_userdata( $user_id ) ) ) {
198
			return false;
199
		};
200
201
		// If the nicename is not set, return a failure.
202
		if ( empty( $user->user_nicename ) ) {
203
			return false;
204
		}
205
206
		return wl_configuration_get_redlink_dataset_uri() . "/user/$user->user_nicename";
0 ignored issues
show
Deprecated Code introduced by
The function wl_configuration_get_redlink_dataset_uri() has been deprecated with message: use Wordlift_Configuration_Service::get_instance()->get_dataset_uri();

This function has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed from the class and what other function to use instead.

Loading history...
207
	}
208
209
	/**
210
	 * Store the URI in user's meta.
211
	 *
212
	 * @since 3.1.7
213
	 *
214
	 * @param int    $user_id  The user's id.
215
	 * @param string $user_uri The user's uri.
216
	 *
217
	 * @return int|bool Meta ID if the key didn't exist, true on successful update, false on failure.
218
	 */
219
	private function _set_uri( $user_id, $user_uri ) {
220
221
		return update_user_meta( $user_id, self::URI_META_KEY, $user_uri );
222
	}
223
224
	/**
225
	 * Get the delete query.
226
	 *
227
	 * @since 3.1.7
228
	 *
229
	 * @param int $user_id The user id.
230
	 *
231
	 * @return false|string The delete query or false in case of failure.
232
	 */
233
	private function get_delete_query( $user_id ) {
234
235
		// Get the URI, return if there's none.
236
		if ( false === ( $user_uri = $this->get_uri( $user_id ) ) ) {
237
			return false;
238
		}
239
240
		// Build the delete query.
241
		$query = Wordlift_Query_Builder::new_instance()->delete()
242
		                               ->statement( $user_uri, Wordlift_Query_Builder::RDFS_TYPE_URI, '?o' )
243
		                               ->build()
244
		         . Wordlift_Query_Builder::new_instance()->delete()
245
		                                 ->statement( $user_uri, Wordlift_Query_Builder::RDFS_LABEL_URI, '?o' )
246
		                                 ->build()
247
		         . Wordlift_Query_Builder::new_instance()->delete()
248
		                                 ->statement( $user_uri, Wordlift_Query_Builder::SCHEMA_GIVEN_NAME_URI, '?o' )
249
		                                 ->build()
250
		         . Wordlift_Query_Builder::new_instance()->delete()
251
		                                 ->statement( $user_uri, Wordlift_Query_Builder::SCHEMA_FAMILY_NAME_URI, '?o' )
252
		                                 ->build()
253
		         . Wordlift_Query_Builder::new_instance()->delete()
254
		                                 ->statement( $user_uri, Wordlift_Query_Builder::SCHEMA_URL_URI, '?o' )
255
		                                 ->build();
256
257
		return $query;
258
	}
259
260
	/**
261
	 * Get the insert query.
262
	 *
263
	 * @since 3.1.7
264
	 *
265
	 * @param int $user_id The user id.
266
	 *
267
	 * @return false|string The insert query or false in case of failure.
268
	 */
269
	private function get_insert_query( $user_id ) {
270
271
		// Get the URI, return if there's none.
272
		if ( false === ( $user_uri = $this->get_uri( $user_id ) ) ) {
273
			return false;
274
		}
275
276
		// Try to get the user data, in case of failure return false.
277
		if ( false === ( $user = get_userdata( $user_id ) ) ) {
278
			return false;
279
		};
280
281
		// Build the insert query.
282
		$query = Wordlift_Query_Builder::new_instance()
283
		                               ->insert()
284
		                               ->statement( $user_uri, Wordlift_Query_Builder::RDFS_TYPE_URI, Wordlift_Query_Builder::SCHEMA_PERSON_URI )
285
		                               ->statement( $user_uri, Wordlift_Query_Builder::RDFS_LABEL_URI, $user->display_name )
286
		                               ->statement( $user_uri, Wordlift_Query_Builder::SCHEMA_GIVEN_NAME_URI, $user->user_firstname )
287
		                               ->statement( $user_uri, Wordlift_Query_Builder::SCHEMA_FAMILY_NAME_URI, $user->user_lastname )
288
		                               ->statement( $user_uri, Wordlift_Query_Builder::SCHEMA_URL_URI, ( ! empty( $user->user_url ) ? $user->user_url : get_author_posts_url( $user_id ) ) )
289
		                               ->build();
290
291
		return $query;
292
	}
293
294
}
295