@@ -10,437 +10,437 @@ |
||
| 10 | 10 | */ |
| 11 | 11 | class Wordlift_User_Service { |
| 12 | 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 user meta key where the deny entity edit flag is stored. |
|
| 22 | - * |
|
| 23 | - * @since 3.14.0 |
|
| 24 | - */ |
|
| 25 | - const DENY_ENTITY_CREATE_META_KEY = '_wl_deny_entity_create'; |
|
| 26 | - |
|
| 27 | - /** |
|
| 28 | - * The meta key holding the entity id representing a {@link WP_User}. |
|
| 29 | - * |
|
| 30 | - * @since 3.14.0 |
|
| 31 | - */ |
|
| 32 | - const ENTITY_META_KEY = '_wl_entity'; |
|
| 33 | - |
|
| 34 | - /** |
|
| 35 | - * The Log service. |
|
| 36 | - * |
|
| 37 | - * @since 3.1.7 |
|
| 38 | - * @access private |
|
| 39 | - * @var \Wordlift_Log_Service $log_service The Log service. |
|
| 40 | - */ |
|
| 41 | - private $log_service; |
|
| 42 | - |
|
| 43 | - /** |
|
| 44 | - * The singleton instance of the User service. |
|
| 45 | - * |
|
| 46 | - * @since 3.1.7 |
|
| 47 | - * @access private |
|
| 48 | - * @var \Wordlift_User_Service $user_service The singleton instance of the User service. |
|
| 49 | - */ |
|
| 50 | - private static $instance; |
|
| 51 | - |
|
| 52 | - /** |
|
| 53 | - * Create an instance of the User service. |
|
| 54 | - * |
|
| 55 | - * @since 3.1.7 |
|
| 56 | - */ |
|
| 57 | - public function __construct() { |
|
| 58 | - |
|
| 59 | - $this->log_service = Wordlift_Log_Service::get_logger( 'Wordlift_User_Service' ); |
|
| 60 | - |
|
| 61 | - self::$instance = $this; |
|
| 62 | - |
|
| 63 | - add_filter( 'user_has_cap', array( $this, 'has_cap' ), 10, 3 ); |
|
| 64 | - } |
|
| 65 | - |
|
| 66 | - /** |
|
| 67 | - * Get the singleton instance of the User service. |
|
| 68 | - * |
|
| 69 | - * @since 3.1.7 |
|
| 70 | - * @return \Wordlift_User_Service The singleton instance of the User service. |
|
| 71 | - */ |
|
| 72 | - public static function get_instance() { |
|
| 73 | - |
|
| 74 | - return self::$instance; |
|
| 75 | - } |
|
| 76 | - |
|
| 77 | - /** |
|
| 78 | - * Get the URI for a user. |
|
| 79 | - * |
|
| 80 | - * @since 3.1.7 |
|
| 81 | - * |
|
| 82 | - * @param int $user_id The user id |
|
| 83 | - * |
|
| 84 | - * @return false|string The user's URI or false in case of failure. |
|
| 85 | - */ |
|
| 86 | - public function get_uri( $user_id ) { |
|
| 87 | - |
|
| 88 | - // Try to get the URI stored in the user's meta and return it if available. |
|
| 89 | - if ( false !== ( $user_uri = $this->_get_uri( $user_id ) ) ) { |
|
| 90 | - return $user_uri; |
|
| 91 | - } |
|
| 92 | - |
|
| 93 | - // Try to build an URI, return false in case of failure. |
|
| 94 | - if ( false === ( $user_uri = $this->_build_uri( $user_id ) ) ) { |
|
| 95 | - return false; |
|
| 96 | - } |
|
| 97 | - |
|
| 98 | - // Store the URI for future requests (we need a "permanent" URI). |
|
| 99 | - $this->_set_uri( $user_id, $user_uri ); |
|
| 100 | - |
|
| 101 | - return $user_uri; |
|
| 102 | - } |
|
| 103 | - |
|
| 104 | - /** |
|
| 105 | - * Receives wp_insert_post events. |
|
| 106 | - * |
|
| 107 | - * @since 3.1.7 |
|
| 108 | - * |
|
| 109 | - * @param int $post_id Post ID. |
|
| 110 | - * @param WP_Post $post Post object. |
|
| 111 | - * @param bool $update Whether this is an existing post being updated or not. |
|
| 112 | - */ |
|
| 113 | - public function wp_insert_post( $post_id, $post, $update ) { |
|
| 114 | - |
|
| 115 | - // If the post is not published, return. |
|
| 116 | - if ( 'publish' !== get_post_status( $post_id ) ) { |
|
| 117 | - return; |
|
| 118 | - } |
|
| 119 | - |
|
| 120 | - // We expect a numeric author id. |
|
| 121 | - if ( ! is_numeric( $post->post_author ) ) { |
|
| 122 | - return; |
|
| 123 | - } |
|
| 124 | - |
|
| 125 | - // Get the delete query,or return in case of failure. |
|
| 126 | - if ( false === ( $delete = $this->get_delete_query( $post->post_author ) ) ) { |
|
| 127 | - return; |
|
| 128 | - } |
|
| 129 | - |
|
| 130 | - // Get the insert query,or return in case of failure. |
|
| 131 | - if ( false === ( $insert = $this->get_insert_query( $post->post_author ) ) ) { |
|
| 132 | - return; |
|
| 133 | - } |
|
| 134 | - |
|
| 135 | - // Send the query to the triple store. |
|
| 136 | - rl_execute_sparql_update_query( $delete . $insert ); |
|
| 137 | - |
|
| 138 | - } |
|
| 139 | - |
|
| 140 | - /** |
|
| 141 | - * Set the `id` of the entity representing a {@link WP_User}. |
|
| 142 | - * |
|
| 143 | - * If the `id` is set to 0 (or less) then the meta is deleted. |
|
| 144 | - * |
|
| 145 | - * @since 3.14.0 |
|
| 146 | - * |
|
| 147 | - * @param int $user_id The {@link WP_User}. |
|
| 148 | - * @param int $value The entity {@link WP_Post} `id`. |
|
| 149 | - * |
|
| 150 | - * @return bool|int Meta ID if the key didn't exist, true on successful update, false on failure. |
|
| 151 | - */ |
|
| 152 | - public function set_entity( $user_id, $value ) { |
|
| 153 | - |
|
| 154 | - return 0 < $value |
|
| 155 | - ? update_user_meta( $user_id, self::ENTITY_META_KEY, $value ) |
|
| 156 | - : delete_user_meta( $user_id, self::ENTITY_META_KEY ); |
|
| 157 | - } |
|
| 158 | - |
|
| 159 | - /** |
|
| 160 | - * Get the {@link WP_Post} `id` of the entity representing a {@link WP_User}. |
|
| 161 | - * |
|
| 162 | - * @since 3.14.0 |
|
| 163 | - * |
|
| 164 | - * @param int $user_id The {@link WP_User}'s `id`. |
|
| 165 | - * |
|
| 166 | - * @return string The entity {@link WP_Post} `id` or an empty string if not set. |
|
| 167 | - */ |
|
| 168 | - public function get_entity( $user_id ) { |
|
| 169 | - |
|
| 170 | - return get_user_meta( $user_id, self::ENTITY_META_KEY, true ); |
|
| 171 | - } |
|
| 172 | - |
|
| 173 | - /** |
|
| 174 | - * Get the user's URI stored in the user's meta. |
|
| 175 | - * |
|
| 176 | - * @since 3.1.7 |
|
| 177 | - * |
|
| 178 | - * @param int $user_id The user id. |
|
| 179 | - * |
|
| 180 | - * @return false|string The user's URI or false if not found. |
|
| 181 | - */ |
|
| 182 | - private function _get_uri( $user_id ) { |
|
| 183 | - |
|
| 184 | - $user_uri = get_user_meta( $user_id, self::URI_META_KEY, true ); |
|
| 185 | - |
|
| 186 | - if ( empty( $user_uri ) ) { |
|
| 187 | - return false; |
|
| 188 | - } |
|
| 189 | - |
|
| 190 | - return $user_uri; |
|
| 191 | - } |
|
| 192 | - |
|
| 193 | - /** |
|
| 194 | - * Build an URI for a user. |
|
| 195 | - * |
|
| 196 | - * @since 3.1.7 |
|
| 197 | - * |
|
| 198 | - * @param int $user_id The user's id. |
|
| 199 | - * |
|
| 200 | - * @return false|string The user's URI or false in case of failure. |
|
| 201 | - */ |
|
| 202 | - private function _build_uri( $user_id ) { |
|
| 203 | - |
|
| 204 | - // Get the user, return false in case of failure. |
|
| 205 | - if ( false === ( $user = get_userdata( $user_id ) ) ) { |
|
| 206 | - return false; |
|
| 207 | - }; |
|
| 208 | - |
|
| 209 | - // If the nicename is not set, return a failure. |
|
| 210 | - if ( empty( $user->user_nicename ) ) { |
|
| 211 | - return false; |
|
| 212 | - } |
|
| 213 | - |
|
| 214 | - return wl_configuration_get_redlink_dataset_uri() . "/user/$user->user_nicename"; |
|
| 215 | - } |
|
| 216 | - |
|
| 217 | - /** |
|
| 218 | - * Store the URI in user's meta. |
|
| 219 | - * |
|
| 220 | - * @since 3.1.7 |
|
| 221 | - * |
|
| 222 | - * @param int $user_id The user's id. |
|
| 223 | - * @param string $user_uri The user's uri. |
|
| 224 | - * |
|
| 225 | - * @return int|bool Meta ID if the key didn't exist, true on successful update, false on failure. |
|
| 226 | - */ |
|
| 227 | - private function _set_uri( $user_id, $user_uri ) { |
|
| 228 | - |
|
| 229 | - return update_user_meta( $user_id, self::URI_META_KEY, $user_uri ); |
|
| 230 | - } |
|
| 231 | - |
|
| 232 | - /** |
|
| 233 | - * Get the delete query. |
|
| 234 | - * |
|
| 235 | - * @since 3.1.7 |
|
| 236 | - * |
|
| 237 | - * @param int $user_id The user id. |
|
| 238 | - * |
|
| 239 | - * @return false|string The delete query or false in case of failure. |
|
| 240 | - */ |
|
| 241 | - private function get_delete_query( $user_id ) { |
|
| 242 | - |
|
| 243 | - // Get the URI, return if there's none. |
|
| 244 | - if ( false === ( $user_uri = $this->get_uri( $user_id ) ) ) { |
|
| 245 | - return false; |
|
| 246 | - } |
|
| 247 | - |
|
| 248 | - // Build the delete query. |
|
| 249 | - $query = Wordlift_Query_Builder::new_instance()->delete() |
|
| 250 | - ->statement( $user_uri, Wordlift_Query_Builder::RDFS_TYPE_URI, '?o' ) |
|
| 251 | - ->build() |
|
| 252 | - . Wordlift_Query_Builder::new_instance()->delete() |
|
| 253 | - ->statement( $user_uri, Wordlift_Query_Builder::RDFS_LABEL_URI, '?o' ) |
|
| 254 | - ->build() |
|
| 255 | - . Wordlift_Query_Builder::new_instance()->delete() |
|
| 256 | - ->statement( $user_uri, Wordlift_Query_Builder::SCHEMA_GIVEN_NAME_URI, '?o' ) |
|
| 257 | - ->build() |
|
| 258 | - . Wordlift_Query_Builder::new_instance()->delete() |
|
| 259 | - ->statement( $user_uri, Wordlift_Query_Builder::SCHEMA_FAMILY_NAME_URI, '?o' ) |
|
| 260 | - ->build() |
|
| 261 | - . Wordlift_Query_Builder::new_instance()->delete() |
|
| 262 | - ->statement( $user_uri, Wordlift_Query_Builder::SCHEMA_URL_URI, '?o' ) |
|
| 263 | - ->build(); |
|
| 264 | - |
|
| 265 | - return $query; |
|
| 266 | - } |
|
| 267 | - |
|
| 268 | - /** |
|
| 269 | - * Get the insert query. |
|
| 270 | - * |
|
| 271 | - * @since 3.1.7 |
|
| 272 | - * |
|
| 273 | - * @param int $user_id The user id. |
|
| 274 | - * |
|
| 275 | - * @return false|string The insert query or false in case of failure. |
|
| 276 | - */ |
|
| 277 | - private function get_insert_query( $user_id ) { |
|
| 278 | - |
|
| 279 | - // Get the URI, return if there's none. |
|
| 280 | - if ( false === ( $user_uri = $this->get_uri( $user_id ) ) ) { |
|
| 281 | - return false; |
|
| 282 | - } |
|
| 283 | - |
|
| 284 | - // Try to get the user data, in case of failure return false. |
|
| 285 | - if ( false === ( $user = get_userdata( $user_id ) ) ) { |
|
| 286 | - return false; |
|
| 287 | - }; |
|
| 288 | - |
|
| 289 | - // Build the insert query. |
|
| 290 | - $query = Wordlift_Query_Builder::new_instance() |
|
| 291 | - ->insert() |
|
| 292 | - ->statement( $user_uri, Wordlift_Query_Builder::RDFS_TYPE_URI, Wordlift_Query_Builder::SCHEMA_PERSON_URI ) |
|
| 293 | - ->statement( $user_uri, Wordlift_Query_Builder::RDFS_LABEL_URI, $user->display_name ) |
|
| 294 | - ->statement( $user_uri, Wordlift_Query_Builder::SCHEMA_GIVEN_NAME_URI, $user->user_firstname ) |
|
| 295 | - ->statement( $user_uri, Wordlift_Query_Builder::SCHEMA_FAMILY_NAME_URI, $user->user_lastname ) |
|
| 296 | - ->statement( $user_uri, Wordlift_Query_Builder::SCHEMA_URL_URI, ( ! empty( $user->user_url ) ? $user->user_url : get_author_posts_url( $user_id ) ) ) |
|
| 297 | - ->build(); |
|
| 298 | - |
|
| 299 | - return $query; |
|
| 300 | - } |
|
| 301 | - |
|
| 302 | - /** |
|
| 303 | - * Mark an editor user as denied from editing entities. |
|
| 304 | - * Does nothing if the user is not an editor |
|
| 305 | - * |
|
| 306 | - * @since 3.14.0 |
|
| 307 | - * |
|
| 308 | - * @param integer $user_id The ID of the user |
|
| 309 | - */ |
|
| 310 | - public function deny_editor_entity_create( $user_id ) { |
|
| 311 | - |
|
| 312 | - // Bail out if the user is not an editor. |
|
| 313 | - if ( ! $this->is_editor( $user_id ) ) { |
|
| 314 | - return; |
|
| 315 | - } |
|
| 316 | - |
|
| 317 | - // The user explicitly do not have the capability. |
|
| 318 | - update_user_option( $user_id, self::DENY_ENTITY_CREATE_META_KEY, 'yes' ); |
|
| 319 | - |
|
| 320 | - } |
|
| 321 | - |
|
| 322 | - /** |
|
| 323 | - * Remove the "deny entity editing" mark from an editor user. |
|
| 324 | - * Does nothing if the user is not an editor |
|
| 325 | - * |
|
| 326 | - * @since 3.14.0 |
|
| 327 | - * |
|
| 328 | - * @param integer $user_id The ID of the user |
|
| 329 | - */ |
|
| 330 | - public function allow_editor_entity_create( $user_id ) { |
|
| 331 | - |
|
| 332 | - // Bail out if the user is not an editor. |
|
| 333 | - if ( ! $this->is_editor( $user_id ) ) { |
|
| 334 | - return; |
|
| 335 | - } |
|
| 336 | - |
|
| 337 | - // The user explicitly do not have the capability. |
|
| 338 | - delete_user_option( $user_id, self::DENY_ENTITY_CREATE_META_KEY ); |
|
| 339 | - |
|
| 340 | - } |
|
| 341 | - |
|
| 342 | - /** |
|
| 343 | - * Get whether the 'deny editor entity editing' flag is set. |
|
| 344 | - * |
|
| 345 | - * @since 3.14.0 |
|
| 346 | - * |
|
| 347 | - * @param int $user_id The {@link WP_User} `id`. |
|
| 348 | - * |
|
| 349 | - * @return int bool True if editing is denied otherwise false. |
|
| 350 | - */ |
|
| 351 | - public function is_deny_editor_entity_create( $user_id ) { |
|
| 352 | - |
|
| 353 | - return 'yes' === get_user_option( self::DENY_ENTITY_CREATE_META_KEY, $user_id ); |
|
| 354 | - } |
|
| 355 | - |
|
| 356 | - /** |
|
| 357 | - * Check whether the {@link WP_User} with the specified `id` is an editor, |
|
| 358 | - * i.e. has the `editor` role. |
|
| 359 | - * |
|
| 360 | - * @since 3.14.0 |
|
| 361 | - * |
|
| 362 | - * @param int $user_id The {@link WP_User} `id`. |
|
| 363 | - * |
|
| 364 | - * @return bool True if the {@link WP_User} is an editor otherwise false. |
|
| 365 | - */ |
|
| 366 | - public function is_editor( $user_id ) { |
|
| 367 | - |
|
| 368 | - // Get the user. |
|
| 369 | - $user = get_user_by( 'id', $user_id ); |
|
| 370 | - |
|
| 371 | - // Return true, if the user is found and has the `editor` role. |
|
| 372 | - return is_a( $user, 'WP_User' ) && in_array( 'editor', (array) $user->roles ); |
|
| 373 | - } |
|
| 374 | - |
|
| 375 | - /** |
|
| 376 | - * Check if an editor can create entities. |
|
| 377 | - * |
|
| 378 | - * @since 3.14.0 |
|
| 379 | - * |
|
| 380 | - * @param int $user_id The user id of the user being checked. |
|
| 381 | - * |
|
| 382 | - * @return bool false if it is an editor that is denied from edit entities, true otherwise. |
|
| 383 | - */ |
|
| 384 | - public function editor_can_create_entities( $user_id ) { |
|
| 385 | - |
|
| 386 | - // Return true if not an editor. |
|
| 387 | - if ( ! $this->is_editor( $user_id ) ) { |
|
| 388 | - return true; |
|
| 389 | - } |
|
| 390 | - |
|
| 391 | - // Check if the user explicitly denied. |
|
| 392 | - return ! $this->is_deny_editor_entity_create( $user_id ); |
|
| 393 | - } |
|
| 394 | - |
|
| 395 | - /** |
|
| 396 | - * Filter capabilities of user. |
|
| 397 | - * |
|
| 398 | - * Deny the capability of managing and editing entities for some users. |
|
| 399 | - * |
|
| 400 | - * @since 3.14.0 |
|
| 401 | - * |
|
| 402 | - * @param array $allcaps All the capabilities of the user |
|
| 403 | - * @param array $cap [0] Required capability |
|
| 404 | - * @param array $args [0] Requested capability |
|
| 405 | - * [1] User ID |
|
| 406 | - * [2] Associated object ID |
|
| 407 | - * |
|
| 408 | - * @return array The capabilities array. |
|
| 409 | - */ |
|
| 410 | - public function has_cap( $allcaps, $cap, $args ) { |
|
| 411 | - /* |
|
| 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 user meta key where the deny entity edit flag is stored. |
|
| 22 | + * |
|
| 23 | + * @since 3.14.0 |
|
| 24 | + */ |
|
| 25 | + const DENY_ENTITY_CREATE_META_KEY = '_wl_deny_entity_create'; |
|
| 26 | + |
|
| 27 | + /** |
|
| 28 | + * The meta key holding the entity id representing a {@link WP_User}. |
|
| 29 | + * |
|
| 30 | + * @since 3.14.0 |
|
| 31 | + */ |
|
| 32 | + const ENTITY_META_KEY = '_wl_entity'; |
|
| 33 | + |
|
| 34 | + /** |
|
| 35 | + * The Log service. |
|
| 36 | + * |
|
| 37 | + * @since 3.1.7 |
|
| 38 | + * @access private |
|
| 39 | + * @var \Wordlift_Log_Service $log_service The Log service. |
|
| 40 | + */ |
|
| 41 | + private $log_service; |
|
| 42 | + |
|
| 43 | + /** |
|
| 44 | + * The singleton instance of the User service. |
|
| 45 | + * |
|
| 46 | + * @since 3.1.7 |
|
| 47 | + * @access private |
|
| 48 | + * @var \Wordlift_User_Service $user_service The singleton instance of the User service. |
|
| 49 | + */ |
|
| 50 | + private static $instance; |
|
| 51 | + |
|
| 52 | + /** |
|
| 53 | + * Create an instance of the User service. |
|
| 54 | + * |
|
| 55 | + * @since 3.1.7 |
|
| 56 | + */ |
|
| 57 | + public function __construct() { |
|
| 58 | + |
|
| 59 | + $this->log_service = Wordlift_Log_Service::get_logger( 'Wordlift_User_Service' ); |
|
| 60 | + |
|
| 61 | + self::$instance = $this; |
|
| 62 | + |
|
| 63 | + add_filter( 'user_has_cap', array( $this, 'has_cap' ), 10, 3 ); |
|
| 64 | + } |
|
| 65 | + |
|
| 66 | + /** |
|
| 67 | + * Get the singleton instance of the User service. |
|
| 68 | + * |
|
| 69 | + * @since 3.1.7 |
|
| 70 | + * @return \Wordlift_User_Service The singleton instance of the User service. |
|
| 71 | + */ |
|
| 72 | + public static function get_instance() { |
|
| 73 | + |
|
| 74 | + return self::$instance; |
|
| 75 | + } |
|
| 76 | + |
|
| 77 | + /** |
|
| 78 | + * Get the URI for a user. |
|
| 79 | + * |
|
| 80 | + * @since 3.1.7 |
|
| 81 | + * |
|
| 82 | + * @param int $user_id The user id |
|
| 83 | + * |
|
| 84 | + * @return false|string The user's URI or false in case of failure. |
|
| 85 | + */ |
|
| 86 | + public function get_uri( $user_id ) { |
|
| 87 | + |
|
| 88 | + // Try to get the URI stored in the user's meta and return it if available. |
|
| 89 | + if ( false !== ( $user_uri = $this->_get_uri( $user_id ) ) ) { |
|
| 90 | + return $user_uri; |
|
| 91 | + } |
|
| 92 | + |
|
| 93 | + // Try to build an URI, return false in case of failure. |
|
| 94 | + if ( false === ( $user_uri = $this->_build_uri( $user_id ) ) ) { |
|
| 95 | + return false; |
|
| 96 | + } |
|
| 97 | + |
|
| 98 | + // Store the URI for future requests (we need a "permanent" URI). |
|
| 99 | + $this->_set_uri( $user_id, $user_uri ); |
|
| 100 | + |
|
| 101 | + return $user_uri; |
|
| 102 | + } |
|
| 103 | + |
|
| 104 | + /** |
|
| 105 | + * Receives wp_insert_post events. |
|
| 106 | + * |
|
| 107 | + * @since 3.1.7 |
|
| 108 | + * |
|
| 109 | + * @param int $post_id Post ID. |
|
| 110 | + * @param WP_Post $post Post object. |
|
| 111 | + * @param bool $update Whether this is an existing post being updated or not. |
|
| 112 | + */ |
|
| 113 | + public function wp_insert_post( $post_id, $post, $update ) { |
|
| 114 | + |
|
| 115 | + // If the post is not published, return. |
|
| 116 | + if ( 'publish' !== get_post_status( $post_id ) ) { |
|
| 117 | + return; |
|
| 118 | + } |
|
| 119 | + |
|
| 120 | + // We expect a numeric author id. |
|
| 121 | + if ( ! is_numeric( $post->post_author ) ) { |
|
| 122 | + return; |
|
| 123 | + } |
|
| 124 | + |
|
| 125 | + // Get the delete query,or return in case of failure. |
|
| 126 | + if ( false === ( $delete = $this->get_delete_query( $post->post_author ) ) ) { |
|
| 127 | + return; |
|
| 128 | + } |
|
| 129 | + |
|
| 130 | + // Get the insert query,or return in case of failure. |
|
| 131 | + if ( false === ( $insert = $this->get_insert_query( $post->post_author ) ) ) { |
|
| 132 | + return; |
|
| 133 | + } |
|
| 134 | + |
|
| 135 | + // Send the query to the triple store. |
|
| 136 | + rl_execute_sparql_update_query( $delete . $insert ); |
|
| 137 | + |
|
| 138 | + } |
|
| 139 | + |
|
| 140 | + /** |
|
| 141 | + * Set the `id` of the entity representing a {@link WP_User}. |
|
| 142 | + * |
|
| 143 | + * If the `id` is set to 0 (or less) then the meta is deleted. |
|
| 144 | + * |
|
| 145 | + * @since 3.14.0 |
|
| 146 | + * |
|
| 147 | + * @param int $user_id The {@link WP_User}. |
|
| 148 | + * @param int $value The entity {@link WP_Post} `id`. |
|
| 149 | + * |
|
| 150 | + * @return bool|int Meta ID if the key didn't exist, true on successful update, false on failure. |
|
| 151 | + */ |
|
| 152 | + public function set_entity( $user_id, $value ) { |
|
| 153 | + |
|
| 154 | + return 0 < $value |
|
| 155 | + ? update_user_meta( $user_id, self::ENTITY_META_KEY, $value ) |
|
| 156 | + : delete_user_meta( $user_id, self::ENTITY_META_KEY ); |
|
| 157 | + } |
|
| 158 | + |
|
| 159 | + /** |
|
| 160 | + * Get the {@link WP_Post} `id` of the entity representing a {@link WP_User}. |
|
| 161 | + * |
|
| 162 | + * @since 3.14.0 |
|
| 163 | + * |
|
| 164 | + * @param int $user_id The {@link WP_User}'s `id`. |
|
| 165 | + * |
|
| 166 | + * @return string The entity {@link WP_Post} `id` or an empty string if not set. |
|
| 167 | + */ |
|
| 168 | + public function get_entity( $user_id ) { |
|
| 169 | + |
|
| 170 | + return get_user_meta( $user_id, self::ENTITY_META_KEY, true ); |
|
| 171 | + } |
|
| 172 | + |
|
| 173 | + /** |
|
| 174 | + * Get the user's URI stored in the user's meta. |
|
| 175 | + * |
|
| 176 | + * @since 3.1.7 |
|
| 177 | + * |
|
| 178 | + * @param int $user_id The user id. |
|
| 179 | + * |
|
| 180 | + * @return false|string The user's URI or false if not found. |
|
| 181 | + */ |
|
| 182 | + private function _get_uri( $user_id ) { |
|
| 183 | + |
|
| 184 | + $user_uri = get_user_meta( $user_id, self::URI_META_KEY, true ); |
|
| 185 | + |
|
| 186 | + if ( empty( $user_uri ) ) { |
|
| 187 | + return false; |
|
| 188 | + } |
|
| 189 | + |
|
| 190 | + return $user_uri; |
|
| 191 | + } |
|
| 192 | + |
|
| 193 | + /** |
|
| 194 | + * Build an URI for a user. |
|
| 195 | + * |
|
| 196 | + * @since 3.1.7 |
|
| 197 | + * |
|
| 198 | + * @param int $user_id The user's id. |
|
| 199 | + * |
|
| 200 | + * @return false|string The user's URI or false in case of failure. |
|
| 201 | + */ |
|
| 202 | + private function _build_uri( $user_id ) { |
|
| 203 | + |
|
| 204 | + // Get the user, return false in case of failure. |
|
| 205 | + if ( false === ( $user = get_userdata( $user_id ) ) ) { |
|
| 206 | + return false; |
|
| 207 | + }; |
|
| 208 | + |
|
| 209 | + // If the nicename is not set, return a failure. |
|
| 210 | + if ( empty( $user->user_nicename ) ) { |
|
| 211 | + return false; |
|
| 212 | + } |
|
| 213 | + |
|
| 214 | + return wl_configuration_get_redlink_dataset_uri() . "/user/$user->user_nicename"; |
|
| 215 | + } |
|
| 216 | + |
|
| 217 | + /** |
|
| 218 | + * Store the URI in user's meta. |
|
| 219 | + * |
|
| 220 | + * @since 3.1.7 |
|
| 221 | + * |
|
| 222 | + * @param int $user_id The user's id. |
|
| 223 | + * @param string $user_uri The user's uri. |
|
| 224 | + * |
|
| 225 | + * @return int|bool Meta ID if the key didn't exist, true on successful update, false on failure. |
|
| 226 | + */ |
|
| 227 | + private function _set_uri( $user_id, $user_uri ) { |
|
| 228 | + |
|
| 229 | + return update_user_meta( $user_id, self::URI_META_KEY, $user_uri ); |
|
| 230 | + } |
|
| 231 | + |
|
| 232 | + /** |
|
| 233 | + * Get the delete query. |
|
| 234 | + * |
|
| 235 | + * @since 3.1.7 |
|
| 236 | + * |
|
| 237 | + * @param int $user_id The user id. |
|
| 238 | + * |
|
| 239 | + * @return false|string The delete query or false in case of failure. |
|
| 240 | + */ |
|
| 241 | + private function get_delete_query( $user_id ) { |
|
| 242 | + |
|
| 243 | + // Get the URI, return if there's none. |
|
| 244 | + if ( false === ( $user_uri = $this->get_uri( $user_id ) ) ) { |
|
| 245 | + return false; |
|
| 246 | + } |
|
| 247 | + |
|
| 248 | + // Build the delete query. |
|
| 249 | + $query = Wordlift_Query_Builder::new_instance()->delete() |
|
| 250 | + ->statement( $user_uri, Wordlift_Query_Builder::RDFS_TYPE_URI, '?o' ) |
|
| 251 | + ->build() |
|
| 252 | + . Wordlift_Query_Builder::new_instance()->delete() |
|
| 253 | + ->statement( $user_uri, Wordlift_Query_Builder::RDFS_LABEL_URI, '?o' ) |
|
| 254 | + ->build() |
|
| 255 | + . Wordlift_Query_Builder::new_instance()->delete() |
|
| 256 | + ->statement( $user_uri, Wordlift_Query_Builder::SCHEMA_GIVEN_NAME_URI, '?o' ) |
|
| 257 | + ->build() |
|
| 258 | + . Wordlift_Query_Builder::new_instance()->delete() |
|
| 259 | + ->statement( $user_uri, Wordlift_Query_Builder::SCHEMA_FAMILY_NAME_URI, '?o' ) |
|
| 260 | + ->build() |
|
| 261 | + . Wordlift_Query_Builder::new_instance()->delete() |
|
| 262 | + ->statement( $user_uri, Wordlift_Query_Builder::SCHEMA_URL_URI, '?o' ) |
|
| 263 | + ->build(); |
|
| 264 | + |
|
| 265 | + return $query; |
|
| 266 | + } |
|
| 267 | + |
|
| 268 | + /** |
|
| 269 | + * Get the insert query. |
|
| 270 | + * |
|
| 271 | + * @since 3.1.7 |
|
| 272 | + * |
|
| 273 | + * @param int $user_id The user id. |
|
| 274 | + * |
|
| 275 | + * @return false|string The insert query or false in case of failure. |
|
| 276 | + */ |
|
| 277 | + private function get_insert_query( $user_id ) { |
|
| 278 | + |
|
| 279 | + // Get the URI, return if there's none. |
|
| 280 | + if ( false === ( $user_uri = $this->get_uri( $user_id ) ) ) { |
|
| 281 | + return false; |
|
| 282 | + } |
|
| 283 | + |
|
| 284 | + // Try to get the user data, in case of failure return false. |
|
| 285 | + if ( false === ( $user = get_userdata( $user_id ) ) ) { |
|
| 286 | + return false; |
|
| 287 | + }; |
|
| 288 | + |
|
| 289 | + // Build the insert query. |
|
| 290 | + $query = Wordlift_Query_Builder::new_instance() |
|
| 291 | + ->insert() |
|
| 292 | + ->statement( $user_uri, Wordlift_Query_Builder::RDFS_TYPE_URI, Wordlift_Query_Builder::SCHEMA_PERSON_URI ) |
|
| 293 | + ->statement( $user_uri, Wordlift_Query_Builder::RDFS_LABEL_URI, $user->display_name ) |
|
| 294 | + ->statement( $user_uri, Wordlift_Query_Builder::SCHEMA_GIVEN_NAME_URI, $user->user_firstname ) |
|
| 295 | + ->statement( $user_uri, Wordlift_Query_Builder::SCHEMA_FAMILY_NAME_URI, $user->user_lastname ) |
|
| 296 | + ->statement( $user_uri, Wordlift_Query_Builder::SCHEMA_URL_URI, ( ! empty( $user->user_url ) ? $user->user_url : get_author_posts_url( $user_id ) ) ) |
|
| 297 | + ->build(); |
|
| 298 | + |
|
| 299 | + return $query; |
|
| 300 | + } |
|
| 301 | + |
|
| 302 | + /** |
|
| 303 | + * Mark an editor user as denied from editing entities. |
|
| 304 | + * Does nothing if the user is not an editor |
|
| 305 | + * |
|
| 306 | + * @since 3.14.0 |
|
| 307 | + * |
|
| 308 | + * @param integer $user_id The ID of the user |
|
| 309 | + */ |
|
| 310 | + public function deny_editor_entity_create( $user_id ) { |
|
| 311 | + |
|
| 312 | + // Bail out if the user is not an editor. |
|
| 313 | + if ( ! $this->is_editor( $user_id ) ) { |
|
| 314 | + return; |
|
| 315 | + } |
|
| 316 | + |
|
| 317 | + // The user explicitly do not have the capability. |
|
| 318 | + update_user_option( $user_id, self::DENY_ENTITY_CREATE_META_KEY, 'yes' ); |
|
| 319 | + |
|
| 320 | + } |
|
| 321 | + |
|
| 322 | + /** |
|
| 323 | + * Remove the "deny entity editing" mark from an editor user. |
|
| 324 | + * Does nothing if the user is not an editor |
|
| 325 | + * |
|
| 326 | + * @since 3.14.0 |
|
| 327 | + * |
|
| 328 | + * @param integer $user_id The ID of the user |
|
| 329 | + */ |
|
| 330 | + public function allow_editor_entity_create( $user_id ) { |
|
| 331 | + |
|
| 332 | + // Bail out if the user is not an editor. |
|
| 333 | + if ( ! $this->is_editor( $user_id ) ) { |
|
| 334 | + return; |
|
| 335 | + } |
|
| 336 | + |
|
| 337 | + // The user explicitly do not have the capability. |
|
| 338 | + delete_user_option( $user_id, self::DENY_ENTITY_CREATE_META_KEY ); |
|
| 339 | + |
|
| 340 | + } |
|
| 341 | + |
|
| 342 | + /** |
|
| 343 | + * Get whether the 'deny editor entity editing' flag is set. |
|
| 344 | + * |
|
| 345 | + * @since 3.14.0 |
|
| 346 | + * |
|
| 347 | + * @param int $user_id The {@link WP_User} `id`. |
|
| 348 | + * |
|
| 349 | + * @return int bool True if editing is denied otherwise false. |
|
| 350 | + */ |
|
| 351 | + public function is_deny_editor_entity_create( $user_id ) { |
|
| 352 | + |
|
| 353 | + return 'yes' === get_user_option( self::DENY_ENTITY_CREATE_META_KEY, $user_id ); |
|
| 354 | + } |
|
| 355 | + |
|
| 356 | + /** |
|
| 357 | + * Check whether the {@link WP_User} with the specified `id` is an editor, |
|
| 358 | + * i.e. has the `editor` role. |
|
| 359 | + * |
|
| 360 | + * @since 3.14.0 |
|
| 361 | + * |
|
| 362 | + * @param int $user_id The {@link WP_User} `id`. |
|
| 363 | + * |
|
| 364 | + * @return bool True if the {@link WP_User} is an editor otherwise false. |
|
| 365 | + */ |
|
| 366 | + public function is_editor( $user_id ) { |
|
| 367 | + |
|
| 368 | + // Get the user. |
|
| 369 | + $user = get_user_by( 'id', $user_id ); |
|
| 370 | + |
|
| 371 | + // Return true, if the user is found and has the `editor` role. |
|
| 372 | + return is_a( $user, 'WP_User' ) && in_array( 'editor', (array) $user->roles ); |
|
| 373 | + } |
|
| 374 | + |
|
| 375 | + /** |
|
| 376 | + * Check if an editor can create entities. |
|
| 377 | + * |
|
| 378 | + * @since 3.14.0 |
|
| 379 | + * |
|
| 380 | + * @param int $user_id The user id of the user being checked. |
|
| 381 | + * |
|
| 382 | + * @return bool false if it is an editor that is denied from edit entities, true otherwise. |
|
| 383 | + */ |
|
| 384 | + public function editor_can_create_entities( $user_id ) { |
|
| 385 | + |
|
| 386 | + // Return true if not an editor. |
|
| 387 | + if ( ! $this->is_editor( $user_id ) ) { |
|
| 388 | + return true; |
|
| 389 | + } |
|
| 390 | + |
|
| 391 | + // Check if the user explicitly denied. |
|
| 392 | + return ! $this->is_deny_editor_entity_create( $user_id ); |
|
| 393 | + } |
|
| 394 | + |
|
| 395 | + /** |
|
| 396 | + * Filter capabilities of user. |
|
| 397 | + * |
|
| 398 | + * Deny the capability of managing and editing entities for some users. |
|
| 399 | + * |
|
| 400 | + * @since 3.14.0 |
|
| 401 | + * |
|
| 402 | + * @param array $allcaps All the capabilities of the user |
|
| 403 | + * @param array $cap [0] Required capability |
|
| 404 | + * @param array $args [0] Requested capability |
|
| 405 | + * [1] User ID |
|
| 406 | + * [2] Associated object ID |
|
| 407 | + * |
|
| 408 | + * @return array The capabilities array. |
|
| 409 | + */ |
|
| 410 | + public function has_cap( $allcaps, $cap, $args ) { |
|
| 411 | + /* |
|
| 412 | 412 | * For entity management/editing related capabilities |
| 413 | 413 | * check that an editor was not explicitly denied (in user profile) |
| 414 | 414 | * the capability. |
| 415 | 415 | */ |
| 416 | 416 | |
| 417 | - /* |
|
| 417 | + /* |
|
| 418 | 418 | * Need protection against the case of edit_user and likes which do not |
| 419 | 419 | * require a capability, just request one. |
| 420 | 420 | */ |
| 421 | - if ( empty( $cap ) || ! isset( $cap[0] ) ) { |
|
| 422 | - return $allcaps; |
|
| 423 | - } |
|
| 424 | - |
|
| 425 | - if ( |
|
| 426 | - ( 'edit_wordlift_entity' === $cap[0] ) || |
|
| 427 | - ( 'edit_wordlift_entities' === $cap[0] ) || |
|
| 428 | - ( 'edit_others_wordlift_entities' === $cap[0] ) || |
|
| 429 | - ( 'publish_wordlift_entities' === $cap[0] ) || |
|
| 430 | - ( 'read_private_wordlift_entities' === $cap[0] ) || |
|
| 431 | - ( 'delete_wordlift_entity' === $cap[0] ) || |
|
| 432 | - ( 'delete_wordlift_entities' === $cap[0] ) || |
|
| 433 | - ( 'delete_others_wordlift_entities' === $cap[0] ) || |
|
| 434 | - ( 'delete_published_wordlift_entities' === $cap[0] ) || |
|
| 435 | - ( 'delete_private_wordlift_entities' === $cap[0] ) |
|
| 436 | - ) { |
|
| 437 | - $user_id = $args[1]; |
|
| 438 | - |
|
| 439 | - if ( ! $this->editor_can_create_entities( $user_id ) ) { |
|
| 440 | - $allcaps[ $cap[0] ] = false; |
|
| 441 | - } |
|
| 442 | - } |
|
| 443 | - |
|
| 444 | - return $allcaps; |
|
| 445 | - } |
|
| 421 | + if ( empty( $cap ) || ! isset( $cap[0] ) ) { |
|
| 422 | + return $allcaps; |
|
| 423 | + } |
|
| 424 | + |
|
| 425 | + if ( |
|
| 426 | + ( 'edit_wordlift_entity' === $cap[0] ) || |
|
| 427 | + ( 'edit_wordlift_entities' === $cap[0] ) || |
|
| 428 | + ( 'edit_others_wordlift_entities' === $cap[0] ) || |
|
| 429 | + ( 'publish_wordlift_entities' === $cap[0] ) || |
|
| 430 | + ( 'read_private_wordlift_entities' === $cap[0] ) || |
|
| 431 | + ( 'delete_wordlift_entity' === $cap[0] ) || |
|
| 432 | + ( 'delete_wordlift_entities' === $cap[0] ) || |
|
| 433 | + ( 'delete_others_wordlift_entities' === $cap[0] ) || |
|
| 434 | + ( 'delete_published_wordlift_entities' === $cap[0] ) || |
|
| 435 | + ( 'delete_private_wordlift_entities' === $cap[0] ) |
|
| 436 | + ) { |
|
| 437 | + $user_id = $args[1]; |
|
| 438 | + |
|
| 439 | + if ( ! $this->editor_can_create_entities( $user_id ) ) { |
|
| 440 | + $allcaps[ $cap[0] ] = false; |
|
| 441 | + } |
|
| 442 | + } |
|
| 443 | + |
|
| 444 | + return $allcaps; |
|
| 445 | + } |
|
| 446 | 446 | } |
@@ -56,11 +56,11 @@ discard block |
||
| 56 | 56 | */ |
| 57 | 57 | public function __construct() { |
| 58 | 58 | |
| 59 | - $this->log_service = Wordlift_Log_Service::get_logger( 'Wordlift_User_Service' ); |
|
| 59 | + $this->log_service = Wordlift_Log_Service::get_logger('Wordlift_User_Service'); |
|
| 60 | 60 | |
| 61 | 61 | self::$instance = $this; |
| 62 | 62 | |
| 63 | - add_filter( 'user_has_cap', array( $this, 'has_cap' ), 10, 3 ); |
|
| 63 | + add_filter('user_has_cap', array($this, 'has_cap'), 10, 3); |
|
| 64 | 64 | } |
| 65 | 65 | |
| 66 | 66 | /** |
@@ -83,20 +83,20 @@ discard block |
||
| 83 | 83 | * |
| 84 | 84 | * @return false|string The user's URI or false in case of failure. |
| 85 | 85 | */ |
| 86 | - public function get_uri( $user_id ) { |
|
| 86 | + public function get_uri($user_id) { |
|
| 87 | 87 | |
| 88 | 88 | // Try to get the URI stored in the user's meta and return it if available. |
| 89 | - if ( false !== ( $user_uri = $this->_get_uri( $user_id ) ) ) { |
|
| 89 | + if (false !== ($user_uri = $this->_get_uri($user_id))) { |
|
| 90 | 90 | return $user_uri; |
| 91 | 91 | } |
| 92 | 92 | |
| 93 | 93 | // Try to build an URI, return false in case of failure. |
| 94 | - if ( false === ( $user_uri = $this->_build_uri( $user_id ) ) ) { |
|
| 94 | + if (false === ($user_uri = $this->_build_uri($user_id))) { |
|
| 95 | 95 | return false; |
| 96 | 96 | } |
| 97 | 97 | |
| 98 | 98 | // Store the URI for future requests (we need a "permanent" URI). |
| 99 | - $this->_set_uri( $user_id, $user_uri ); |
|
| 99 | + $this->_set_uri($user_id, $user_uri); |
|
| 100 | 100 | |
| 101 | 101 | return $user_uri; |
| 102 | 102 | } |
@@ -110,30 +110,30 @@ discard block |
||
| 110 | 110 | * @param WP_Post $post Post object. |
| 111 | 111 | * @param bool $update Whether this is an existing post being updated or not. |
| 112 | 112 | */ |
| 113 | - public function wp_insert_post( $post_id, $post, $update ) { |
|
| 113 | + public function wp_insert_post($post_id, $post, $update) { |
|
| 114 | 114 | |
| 115 | 115 | // If the post is not published, return. |
| 116 | - if ( 'publish' !== get_post_status( $post_id ) ) { |
|
| 116 | + if ('publish' !== get_post_status($post_id)) { |
|
| 117 | 117 | return; |
| 118 | 118 | } |
| 119 | 119 | |
| 120 | 120 | // We expect a numeric author id. |
| 121 | - if ( ! is_numeric( $post->post_author ) ) { |
|
| 121 | + if ( ! is_numeric($post->post_author)) { |
|
| 122 | 122 | return; |
| 123 | 123 | } |
| 124 | 124 | |
| 125 | 125 | // Get the delete query,or return in case of failure. |
| 126 | - if ( false === ( $delete = $this->get_delete_query( $post->post_author ) ) ) { |
|
| 126 | + if (false === ($delete = $this->get_delete_query($post->post_author))) { |
|
| 127 | 127 | return; |
| 128 | 128 | } |
| 129 | 129 | |
| 130 | 130 | // Get the insert query,or return in case of failure. |
| 131 | - if ( false === ( $insert = $this->get_insert_query( $post->post_author ) ) ) { |
|
| 131 | + if (false === ($insert = $this->get_insert_query($post->post_author))) { |
|
| 132 | 132 | return; |
| 133 | 133 | } |
| 134 | 134 | |
| 135 | 135 | // Send the query to the triple store. |
| 136 | - rl_execute_sparql_update_query( $delete . $insert ); |
|
| 136 | + rl_execute_sparql_update_query($delete.$insert); |
|
| 137 | 137 | |
| 138 | 138 | } |
| 139 | 139 | |
@@ -149,11 +149,11 @@ discard block |
||
| 149 | 149 | * |
| 150 | 150 | * @return bool|int Meta ID if the key didn't exist, true on successful update, false on failure. |
| 151 | 151 | */ |
| 152 | - public function set_entity( $user_id, $value ) { |
|
| 152 | + public function set_entity($user_id, $value) { |
|
| 153 | 153 | |
| 154 | 154 | return 0 < $value |
| 155 | - ? update_user_meta( $user_id, self::ENTITY_META_KEY, $value ) |
|
| 156 | - : delete_user_meta( $user_id, self::ENTITY_META_KEY ); |
|
| 155 | + ? update_user_meta($user_id, self::ENTITY_META_KEY, $value) |
|
| 156 | + : delete_user_meta($user_id, self::ENTITY_META_KEY); |
|
| 157 | 157 | } |
| 158 | 158 | |
| 159 | 159 | /** |
@@ -165,9 +165,9 @@ discard block |
||
| 165 | 165 | * |
| 166 | 166 | * @return string The entity {@link WP_Post} `id` or an empty string if not set. |
| 167 | 167 | */ |
| 168 | - public function get_entity( $user_id ) { |
|
| 168 | + public function get_entity($user_id) { |
|
| 169 | 169 | |
| 170 | - return get_user_meta( $user_id, self::ENTITY_META_KEY, true ); |
|
| 170 | + return get_user_meta($user_id, self::ENTITY_META_KEY, true); |
|
| 171 | 171 | } |
| 172 | 172 | |
| 173 | 173 | /** |
@@ -179,11 +179,11 @@ discard block |
||
| 179 | 179 | * |
| 180 | 180 | * @return false|string The user's URI or false if not found. |
| 181 | 181 | */ |
| 182 | - private function _get_uri( $user_id ) { |
|
| 182 | + private function _get_uri($user_id) { |
|
| 183 | 183 | |
| 184 | - $user_uri = get_user_meta( $user_id, self::URI_META_KEY, true ); |
|
| 184 | + $user_uri = get_user_meta($user_id, self::URI_META_KEY, true); |
|
| 185 | 185 | |
| 186 | - if ( empty( $user_uri ) ) { |
|
| 186 | + if (empty($user_uri)) { |
|
| 187 | 187 | return false; |
| 188 | 188 | } |
| 189 | 189 | |
@@ -199,19 +199,19 @@ discard block |
||
| 199 | 199 | * |
| 200 | 200 | * @return false|string The user's URI or false in case of failure. |
| 201 | 201 | */ |
| 202 | - private function _build_uri( $user_id ) { |
|
| 202 | + private function _build_uri($user_id) { |
|
| 203 | 203 | |
| 204 | 204 | // Get the user, return false in case of failure. |
| 205 | - if ( false === ( $user = get_userdata( $user_id ) ) ) { |
|
| 205 | + if (false === ($user = get_userdata($user_id))) { |
|
| 206 | 206 | return false; |
| 207 | 207 | }; |
| 208 | 208 | |
| 209 | 209 | // If the nicename is not set, return a failure. |
| 210 | - if ( empty( $user->user_nicename ) ) { |
|
| 210 | + if (empty($user->user_nicename)) { |
|
| 211 | 211 | return false; |
| 212 | 212 | } |
| 213 | 213 | |
| 214 | - return wl_configuration_get_redlink_dataset_uri() . "/user/$user->user_nicename"; |
|
| 214 | + return wl_configuration_get_redlink_dataset_uri()."/user/$user->user_nicename"; |
|
| 215 | 215 | } |
| 216 | 216 | |
| 217 | 217 | /** |
@@ -224,9 +224,9 @@ discard block |
||
| 224 | 224 | * |
| 225 | 225 | * @return int|bool Meta ID if the key didn't exist, true on successful update, false on failure. |
| 226 | 226 | */ |
| 227 | - private function _set_uri( $user_id, $user_uri ) { |
|
| 227 | + private function _set_uri($user_id, $user_uri) { |
|
| 228 | 228 | |
| 229 | - return update_user_meta( $user_id, self::URI_META_KEY, $user_uri ); |
|
| 229 | + return update_user_meta($user_id, self::URI_META_KEY, $user_uri); |
|
| 230 | 230 | } |
| 231 | 231 | |
| 232 | 232 | /** |
@@ -238,28 +238,28 @@ discard block |
||
| 238 | 238 | * |
| 239 | 239 | * @return false|string The delete query or false in case of failure. |
| 240 | 240 | */ |
| 241 | - private function get_delete_query( $user_id ) { |
|
| 241 | + private function get_delete_query($user_id) { |
|
| 242 | 242 | |
| 243 | 243 | // Get the URI, return if there's none. |
| 244 | - if ( false === ( $user_uri = $this->get_uri( $user_id ) ) ) { |
|
| 244 | + if (false === ($user_uri = $this->get_uri($user_id))) { |
|
| 245 | 245 | return false; |
| 246 | 246 | } |
| 247 | 247 | |
| 248 | 248 | // Build the delete query. |
| 249 | 249 | $query = Wordlift_Query_Builder::new_instance()->delete() |
| 250 | - ->statement( $user_uri, Wordlift_Query_Builder::RDFS_TYPE_URI, '?o' ) |
|
| 250 | + ->statement($user_uri, Wordlift_Query_Builder::RDFS_TYPE_URI, '?o') |
|
| 251 | 251 | ->build() |
| 252 | 252 | . Wordlift_Query_Builder::new_instance()->delete() |
| 253 | - ->statement( $user_uri, Wordlift_Query_Builder::RDFS_LABEL_URI, '?o' ) |
|
| 253 | + ->statement($user_uri, Wordlift_Query_Builder::RDFS_LABEL_URI, '?o') |
|
| 254 | 254 | ->build() |
| 255 | 255 | . Wordlift_Query_Builder::new_instance()->delete() |
| 256 | - ->statement( $user_uri, Wordlift_Query_Builder::SCHEMA_GIVEN_NAME_URI, '?o' ) |
|
| 256 | + ->statement($user_uri, Wordlift_Query_Builder::SCHEMA_GIVEN_NAME_URI, '?o') |
|
| 257 | 257 | ->build() |
| 258 | 258 | . Wordlift_Query_Builder::new_instance()->delete() |
| 259 | - ->statement( $user_uri, Wordlift_Query_Builder::SCHEMA_FAMILY_NAME_URI, '?o' ) |
|
| 259 | + ->statement($user_uri, Wordlift_Query_Builder::SCHEMA_FAMILY_NAME_URI, '?o') |
|
| 260 | 260 | ->build() |
| 261 | 261 | . Wordlift_Query_Builder::new_instance()->delete() |
| 262 | - ->statement( $user_uri, Wordlift_Query_Builder::SCHEMA_URL_URI, '?o' ) |
|
| 262 | + ->statement($user_uri, Wordlift_Query_Builder::SCHEMA_URL_URI, '?o') |
|
| 263 | 263 | ->build(); |
| 264 | 264 | |
| 265 | 265 | return $query; |
@@ -274,26 +274,26 @@ discard block |
||
| 274 | 274 | * |
| 275 | 275 | * @return false|string The insert query or false in case of failure. |
| 276 | 276 | */ |
| 277 | - private function get_insert_query( $user_id ) { |
|
| 277 | + private function get_insert_query($user_id) { |
|
| 278 | 278 | |
| 279 | 279 | // Get the URI, return if there's none. |
| 280 | - if ( false === ( $user_uri = $this->get_uri( $user_id ) ) ) { |
|
| 280 | + if (false === ($user_uri = $this->get_uri($user_id))) { |
|
| 281 | 281 | return false; |
| 282 | 282 | } |
| 283 | 283 | |
| 284 | 284 | // Try to get the user data, in case of failure return false. |
| 285 | - if ( false === ( $user = get_userdata( $user_id ) ) ) { |
|
| 285 | + if (false === ($user = get_userdata($user_id))) { |
|
| 286 | 286 | return false; |
| 287 | 287 | }; |
| 288 | 288 | |
| 289 | 289 | // Build the insert query. |
| 290 | 290 | $query = Wordlift_Query_Builder::new_instance() |
| 291 | 291 | ->insert() |
| 292 | - ->statement( $user_uri, Wordlift_Query_Builder::RDFS_TYPE_URI, Wordlift_Query_Builder::SCHEMA_PERSON_URI ) |
|
| 293 | - ->statement( $user_uri, Wordlift_Query_Builder::RDFS_LABEL_URI, $user->display_name ) |
|
| 294 | - ->statement( $user_uri, Wordlift_Query_Builder::SCHEMA_GIVEN_NAME_URI, $user->user_firstname ) |
|
| 295 | - ->statement( $user_uri, Wordlift_Query_Builder::SCHEMA_FAMILY_NAME_URI, $user->user_lastname ) |
|
| 296 | - ->statement( $user_uri, Wordlift_Query_Builder::SCHEMA_URL_URI, ( ! empty( $user->user_url ) ? $user->user_url : get_author_posts_url( $user_id ) ) ) |
|
| 292 | + ->statement($user_uri, Wordlift_Query_Builder::RDFS_TYPE_URI, Wordlift_Query_Builder::SCHEMA_PERSON_URI) |
|
| 293 | + ->statement($user_uri, Wordlift_Query_Builder::RDFS_LABEL_URI, $user->display_name) |
|
| 294 | + ->statement($user_uri, Wordlift_Query_Builder::SCHEMA_GIVEN_NAME_URI, $user->user_firstname) |
|
| 295 | + ->statement($user_uri, Wordlift_Query_Builder::SCHEMA_FAMILY_NAME_URI, $user->user_lastname) |
|
| 296 | + ->statement($user_uri, Wordlift_Query_Builder::SCHEMA_URL_URI, ( ! empty($user->user_url) ? $user->user_url : get_author_posts_url($user_id))) |
|
| 297 | 297 | ->build(); |
| 298 | 298 | |
| 299 | 299 | return $query; |
@@ -307,15 +307,15 @@ discard block |
||
| 307 | 307 | * |
| 308 | 308 | * @param integer $user_id The ID of the user |
| 309 | 309 | */ |
| 310 | - public function deny_editor_entity_create( $user_id ) { |
|
| 310 | + public function deny_editor_entity_create($user_id) { |
|
| 311 | 311 | |
| 312 | 312 | // Bail out if the user is not an editor. |
| 313 | - if ( ! $this->is_editor( $user_id ) ) { |
|
| 313 | + if ( ! $this->is_editor($user_id)) { |
|
| 314 | 314 | return; |
| 315 | 315 | } |
| 316 | 316 | |
| 317 | 317 | // The user explicitly do not have the capability. |
| 318 | - update_user_option( $user_id, self::DENY_ENTITY_CREATE_META_KEY, 'yes' ); |
|
| 318 | + update_user_option($user_id, self::DENY_ENTITY_CREATE_META_KEY, 'yes'); |
|
| 319 | 319 | |
| 320 | 320 | } |
| 321 | 321 | |
@@ -327,15 +327,15 @@ discard block |
||
| 327 | 327 | * |
| 328 | 328 | * @param integer $user_id The ID of the user |
| 329 | 329 | */ |
| 330 | - public function allow_editor_entity_create( $user_id ) { |
|
| 330 | + public function allow_editor_entity_create($user_id) { |
|
| 331 | 331 | |
| 332 | 332 | // Bail out if the user is not an editor. |
| 333 | - if ( ! $this->is_editor( $user_id ) ) { |
|
| 333 | + if ( ! $this->is_editor($user_id)) { |
|
| 334 | 334 | return; |
| 335 | 335 | } |
| 336 | 336 | |
| 337 | 337 | // The user explicitly do not have the capability. |
| 338 | - delete_user_option( $user_id, self::DENY_ENTITY_CREATE_META_KEY ); |
|
| 338 | + delete_user_option($user_id, self::DENY_ENTITY_CREATE_META_KEY); |
|
| 339 | 339 | |
| 340 | 340 | } |
| 341 | 341 | |
@@ -348,9 +348,9 @@ discard block |
||
| 348 | 348 | * |
| 349 | 349 | * @return int bool True if editing is denied otherwise false. |
| 350 | 350 | */ |
| 351 | - public function is_deny_editor_entity_create( $user_id ) { |
|
| 351 | + public function is_deny_editor_entity_create($user_id) { |
|
| 352 | 352 | |
| 353 | - return 'yes' === get_user_option( self::DENY_ENTITY_CREATE_META_KEY, $user_id ); |
|
| 353 | + return 'yes' === get_user_option(self::DENY_ENTITY_CREATE_META_KEY, $user_id); |
|
| 354 | 354 | } |
| 355 | 355 | |
| 356 | 356 | /** |
@@ -363,13 +363,13 @@ discard block |
||
| 363 | 363 | * |
| 364 | 364 | * @return bool True if the {@link WP_User} is an editor otherwise false. |
| 365 | 365 | */ |
| 366 | - public function is_editor( $user_id ) { |
|
| 366 | + public function is_editor($user_id) { |
|
| 367 | 367 | |
| 368 | 368 | // Get the user. |
| 369 | - $user = get_user_by( 'id', $user_id ); |
|
| 369 | + $user = get_user_by('id', $user_id); |
|
| 370 | 370 | |
| 371 | 371 | // Return true, if the user is found and has the `editor` role. |
| 372 | - return is_a( $user, 'WP_User' ) && in_array( 'editor', (array) $user->roles ); |
|
| 372 | + return is_a($user, 'WP_User') && in_array('editor', (array) $user->roles); |
|
| 373 | 373 | } |
| 374 | 374 | |
| 375 | 375 | /** |
@@ -381,15 +381,15 @@ discard block |
||
| 381 | 381 | * |
| 382 | 382 | * @return bool false if it is an editor that is denied from edit entities, true otherwise. |
| 383 | 383 | */ |
| 384 | - public function editor_can_create_entities( $user_id ) { |
|
| 384 | + public function editor_can_create_entities($user_id) { |
|
| 385 | 385 | |
| 386 | 386 | // Return true if not an editor. |
| 387 | - if ( ! $this->is_editor( $user_id ) ) { |
|
| 387 | + if ( ! $this->is_editor($user_id)) { |
|
| 388 | 388 | return true; |
| 389 | 389 | } |
| 390 | 390 | |
| 391 | 391 | // Check if the user explicitly denied. |
| 392 | - return ! $this->is_deny_editor_entity_create( $user_id ); |
|
| 392 | + return ! $this->is_deny_editor_entity_create($user_id); |
|
| 393 | 393 | } |
| 394 | 394 | |
| 395 | 395 | /** |
@@ -407,7 +407,7 @@ discard block |
||
| 407 | 407 | * |
| 408 | 408 | * @return array The capabilities array. |
| 409 | 409 | */ |
| 410 | - public function has_cap( $allcaps, $cap, $args ) { |
|
| 410 | + public function has_cap($allcaps, $cap, $args) { |
|
| 411 | 411 | /* |
| 412 | 412 | * For entity management/editing related capabilities |
| 413 | 413 | * check that an editor was not explicitly denied (in user profile) |
@@ -418,26 +418,26 @@ discard block |
||
| 418 | 418 | * Need protection against the case of edit_user and likes which do not |
| 419 | 419 | * require a capability, just request one. |
| 420 | 420 | */ |
| 421 | - if ( empty( $cap ) || ! isset( $cap[0] ) ) { |
|
| 421 | + if (empty($cap) || ! isset($cap[0])) { |
|
| 422 | 422 | return $allcaps; |
| 423 | 423 | } |
| 424 | 424 | |
| 425 | 425 | if ( |
| 426 | - ( 'edit_wordlift_entity' === $cap[0] ) || |
|
| 427 | - ( 'edit_wordlift_entities' === $cap[0] ) || |
|
| 428 | - ( 'edit_others_wordlift_entities' === $cap[0] ) || |
|
| 429 | - ( 'publish_wordlift_entities' === $cap[0] ) || |
|
| 430 | - ( 'read_private_wordlift_entities' === $cap[0] ) || |
|
| 431 | - ( 'delete_wordlift_entity' === $cap[0] ) || |
|
| 432 | - ( 'delete_wordlift_entities' === $cap[0] ) || |
|
| 433 | - ( 'delete_others_wordlift_entities' === $cap[0] ) || |
|
| 434 | - ( 'delete_published_wordlift_entities' === $cap[0] ) || |
|
| 435 | - ( 'delete_private_wordlift_entities' === $cap[0] ) |
|
| 426 | + ('edit_wordlift_entity' === $cap[0]) || |
|
| 427 | + ('edit_wordlift_entities' === $cap[0]) || |
|
| 428 | + ('edit_others_wordlift_entities' === $cap[0]) || |
|
| 429 | + ('publish_wordlift_entities' === $cap[0]) || |
|
| 430 | + ('read_private_wordlift_entities' === $cap[0]) || |
|
| 431 | + ('delete_wordlift_entity' === $cap[0]) || |
|
| 432 | + ('delete_wordlift_entities' === $cap[0]) || |
|
| 433 | + ('delete_others_wordlift_entities' === $cap[0]) || |
|
| 434 | + ('delete_published_wordlift_entities' === $cap[0]) || |
|
| 435 | + ('delete_private_wordlift_entities' === $cap[0]) |
|
| 436 | 436 | ) { |
| 437 | 437 | $user_id = $args[1]; |
| 438 | 438 | |
| 439 | - if ( ! $this->editor_can_create_entities( $user_id ) ) { |
|
| 440 | - $allcaps[ $cap[0] ] = false; |
|
| 439 | + if ( ! $this->editor_can_create_entities($user_id)) { |
|
| 440 | + $allcaps[$cap[0]] = false; |
|
| 441 | 441 | } |
| 442 | 442 | } |
| 443 | 443 | |