Complex classes like Wordlift_User_Service often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Wordlift_User_Service, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 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 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 | * The {@link Wordlift_Sparql_Service} instance. |
||
| 54 | * |
||
| 55 | * @since 3.18.0 |
||
| 56 | * @access private |
||
| 57 | * @var \Wordlift_Sparql_Service $sparql_service The {@link Wordlift_Sparql_Service} instance. |
||
| 58 | */ |
||
| 59 | private $sparql_service; |
||
| 60 | |||
| 61 | /** |
||
| 62 | * The Entity service. |
||
| 63 | * |
||
| 64 | * @since 3.18.0 |
||
| 65 | * @access private |
||
| 66 | * @var \Wordlift_Entity_Service $entity_service The Entity service. |
||
| 67 | */ |
||
| 68 | private $entity_service; |
||
| 69 | |||
| 70 | /** |
||
| 71 | * Create an instance of the User service. |
||
| 72 | * |
||
| 73 | * @since 3.1.7 |
||
| 74 | * |
||
| 75 | * @param \Wordlift_Sparql_Service $sparql_service The {@link Wordlift_Sparql_Service} instance. |
||
| 76 | * @param \Wordlift_Entity_Service $entity_service The {@link Wordlift_Entity_Service} instance. |
||
| 77 | */ |
||
| 78 | public function __construct( $sparql_service, $entity_service ) { |
||
| 89 | |||
| 90 | /** |
||
| 91 | * Get the singleton instance of the User service. |
||
| 92 | * |
||
| 93 | * @since 3.1.7 |
||
| 94 | * @return \Wordlift_User_Service The singleton instance of the User service. |
||
| 95 | */ |
||
| 96 | public static function get_instance() { |
||
| 100 | |||
| 101 | /** |
||
| 102 | * Get the URI for a user. |
||
| 103 | * |
||
| 104 | * @since 3.1.7 |
||
| 105 | * |
||
| 106 | * @param int $user_id The user id |
||
| 107 | * |
||
| 108 | * @return false|string The user's URI or false in case of failure. |
||
| 109 | */ |
||
| 110 | public function get_uri( $user_id ) { |
||
| 127 | |||
| 128 | /** |
||
| 129 | * Set the `id` of the entity representing a {@link WP_User}. |
||
| 130 | * |
||
| 131 | * If the `id` is set to 0 (or less) then the meta is deleted. |
||
| 132 | * |
||
| 133 | * @since 3.14.0 |
||
| 134 | * |
||
| 135 | * @param int $user_id The {@link WP_User}. |
||
| 136 | * @param int $value The entity {@link WP_Post} `id`. |
||
| 137 | * |
||
| 138 | * @return bool|int Meta ID if the key didn't exist, true on successful update, false on failure. |
||
| 139 | */ |
||
| 140 | public function set_entity( $user_id, $value ) { |
||
| 146 | |||
| 147 | /** |
||
| 148 | * Get the {@link WP_Post} `id` of the entity representing a {@link WP_User}. |
||
| 149 | * |
||
| 150 | * @since 3.14.0 |
||
| 151 | * |
||
| 152 | * @param int $user_id The {@link WP_User}'s `id`. |
||
| 153 | * |
||
| 154 | * @return string|false The entity {@link WP_Post} `id` or an empty string if not set or false if the object id is invalid |
||
| 155 | */ |
||
| 156 | public function get_entity( $user_id ) { |
||
| 160 | |||
| 161 | /** |
||
| 162 | * Get the user's URI stored in the user's meta. |
||
| 163 | * |
||
| 164 | * @since 3.1.7 |
||
| 165 | * |
||
| 166 | * @param int $user_id The user id. |
||
| 167 | * |
||
| 168 | * @return false|string The user's URI or false if not found. |
||
| 169 | */ |
||
| 170 | private function _get_uri( $user_id ) { |
||
| 180 | |||
| 181 | /** |
||
| 182 | * Build an URI for a user. |
||
| 183 | * |
||
| 184 | * @since 3.1.7 |
||
| 185 | * |
||
| 186 | * @param int $user_id The user's id. |
||
| 187 | * |
||
| 188 | * @return false|string The user's URI or false in case of failure. |
||
| 189 | */ |
||
| 190 | private function _build_uri( $user_id ) { |
||
| 204 | |||
| 205 | /** |
||
| 206 | * Store the URI in user's meta. |
||
| 207 | * |
||
| 208 | * @since 3.1.7 |
||
| 209 | * |
||
| 210 | * @param int $user_id The user's id. |
||
| 211 | * @param string $user_uri The user's uri. |
||
| 212 | * |
||
| 213 | * @return int|bool Meta ID if the key didn't exist, true on successful update, false on failure. |
||
| 214 | */ |
||
| 215 | private function _set_uri( $user_id, $user_uri ) { |
||
| 219 | |||
| 220 | // /** |
||
| 221 | // * Get the delete query. |
||
| 222 | // * |
||
| 223 | // * @since 3.1.7 |
||
| 224 | // * |
||
| 225 | // * @param int $user_id The user id. |
||
| 226 | // * |
||
| 227 | // * @return false|string The delete query or false in case of failure. |
||
| 228 | // */ |
||
| 229 | // private function get_delete_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 | // // Build the delete query. |
||
| 237 | // $query = Wordlift_Query_Builder::new_instance()->delete() |
||
| 238 | // ->statement( $user_uri, Wordlift_Query_Builder::RDFS_TYPE_URI, '?o' ) |
||
| 239 | // ->build() |
||
| 240 | // . Wordlift_Query_Builder::new_instance()->delete() |
||
| 241 | // ->statement( $user_uri, Wordlift_Query_Builder::RDFS_LABEL_URI, '?o' ) |
||
| 242 | // ->build() |
||
| 243 | // . Wordlift_Query_Builder::new_instance()->delete() |
||
| 244 | // ->statement( $user_uri, Wordlift_Query_Builder::SCHEMA_GIVEN_NAME_URI, '?o' ) |
||
| 245 | // ->build() |
||
| 246 | // . Wordlift_Query_Builder::new_instance()->delete() |
||
| 247 | // ->statement( $user_uri, Wordlift_Query_Builder::SCHEMA_FAMILY_NAME_URI, '?o' ) |
||
| 248 | // ->build() |
||
| 249 | // . Wordlift_Query_Builder::new_instance()->delete() |
||
| 250 | // ->statement( $user_uri, Wordlift_Query_Builder::SCHEMA_URL_URI, '?o' ) |
||
| 251 | // ->build(); |
||
| 252 | // |
||
| 253 | // return $query; |
||
| 254 | // } |
||
| 255 | |||
| 256 | // /** |
||
| 257 | // * Get the insert query. |
||
| 258 | // * |
||
| 259 | // * @since 3.1.7 |
||
| 260 | // * |
||
| 261 | // * @param int $user_id The user id. |
||
| 262 | // * |
||
| 263 | // * @return false|string The insert query or false in case of failure. |
||
| 264 | // */ |
||
| 265 | // private function get_insert_query( $user_id ) { |
||
| 266 | // |
||
| 267 | // // Get the URI, return if there's none. |
||
| 268 | // if ( false === ( $user_uri = $this->get_uri( $user_id ) ) ) { |
||
| 269 | // return false; |
||
| 270 | // } |
||
| 271 | // |
||
| 272 | // // Try to get the user data, in case of failure return false. |
||
| 273 | // if ( false === ( $user = get_userdata( $user_id ) ) ) { |
||
| 274 | // return false; |
||
| 275 | // }; |
||
| 276 | // |
||
| 277 | // // Build the insert query. |
||
| 278 | // $query = Wordlift_Query_Builder::new_instance() |
||
| 279 | // ->insert() |
||
| 280 | // ->statement( $user_uri, Wordlift_Query_Builder::RDFS_TYPE_URI, Wordlift_Query_Builder::SCHEMA_PERSON_URI ) |
||
| 281 | // ->statement( $user_uri, Wordlift_Query_Builder::RDFS_LABEL_URI, $user->display_name ) |
||
| 282 | // ->statement( $user_uri, Wordlift_Query_Builder::SCHEMA_GIVEN_NAME_URI, $user->user_firstname ) |
||
| 283 | // ->statement( $user_uri, Wordlift_Query_Builder::SCHEMA_FAMILY_NAME_URI, $user->user_lastname ) |
||
| 284 | // ->statement( $user_uri, Wordlift_Query_Builder::SCHEMA_URL_URI, ( ! empty( $user->user_url ) ? $user->user_url : get_author_posts_url( $user_id ) ) ) |
||
| 285 | // ->build(); |
||
| 286 | // |
||
| 287 | // return $query; |
||
| 288 | // } |
||
| 289 | |||
| 290 | /** |
||
| 291 | * Mark an editor user as denied from editing entities. |
||
| 292 | * Does nothing if the user is not an editor |
||
| 293 | * |
||
| 294 | * @since 3.14.0 |
||
| 295 | * |
||
| 296 | * @param integer $user_id The ID of the user |
||
| 297 | */ |
||
| 298 | public function deny_editor_entity_create( $user_id ) { |
||
| 309 | |||
| 310 | /** |
||
| 311 | * Remove the "deny entity editing" mark from an editor user. |
||
| 312 | * Does nothing if the user is not an editor |
||
| 313 | * |
||
| 314 | * @since 3.14.0 |
||
| 315 | * |
||
| 316 | * @param integer $user_id The ID of the user |
||
| 317 | */ |
||
| 318 | public function allow_editor_entity_create( $user_id ) { |
||
| 329 | |||
| 330 | /** |
||
| 331 | * Get whether the 'deny editor entity editing' flag is set. |
||
| 332 | * |
||
| 333 | * @since 3.14.0 |
||
| 334 | * |
||
| 335 | * @param int $user_id The {@link WP_User} `id`. |
||
| 336 | * |
||
| 337 | * @return int bool True if editing is denied otherwise false. |
||
| 338 | */ |
||
| 339 | public function is_deny_editor_entity_create( $user_id ) { |
||
| 343 | |||
| 344 | /** |
||
| 345 | * Check whether the {@link WP_User} with the specified `id` is an editor, |
||
| 346 | * i.e. has the `editor` role. |
||
| 347 | * |
||
| 348 | * @since 3.14.0 |
||
| 349 | * |
||
| 350 | * @param int $user_id The {@link WP_User} `id`. |
||
| 351 | * |
||
| 352 | * @return bool True if the {@link WP_User} is an editor otherwise false. |
||
| 353 | */ |
||
| 354 | public function is_editor( $user_id ) { |
||
| 362 | |||
| 363 | /** |
||
| 364 | * Check if an editor can create entities. |
||
| 365 | * |
||
| 366 | * @since 3.14.0 |
||
| 367 | * |
||
| 368 | * @param int $user_id The user id of the user being checked. |
||
| 369 | * |
||
| 370 | * @return bool false if it is an editor that is denied from edit entities, true otherwise. |
||
| 371 | */ |
||
| 372 | public function editor_can_create_entities( $user_id ) { |
||
| 382 | |||
| 383 | /** |
||
| 384 | * Filter capabilities of user. |
||
| 385 | * |
||
| 386 | * Deny the capability of managing and editing entities for some users. |
||
| 387 | * |
||
| 388 | * @since 3.14.0 |
||
| 389 | * |
||
| 390 | * @param array $allcaps All the capabilities of the user |
||
| 391 | * @param array $cap [0] Required capability |
||
| 392 | * @param array $args [0] Requested capability |
||
| 393 | * [1] User ID |
||
| 394 | * [2] Associated object ID |
||
| 395 | * |
||
| 396 | * @return array The capabilities array. |
||
| 397 | */ |
||
| 398 | public function has_cap( $allcaps, $cap, $args ) { |
||
| 434 | |||
| 435 | /** |
||
| 436 | * Hook on update user meta to check if the user author has changed. |
||
| 437 | * If so we need to execute sparql query that will update all user posts author triple. |
||
| 438 | * |
||
| 439 | * @since 3.18.0 |
||
| 440 | * |
||
| 441 | * @param null $null |
||
| 442 | * @param int $object_id The user ID. |
||
| 443 | * @param string $meta_key The meta key name. |
||
| 444 | * @param mixed $meta_value Meta value. |
||
| 445 | * @param mixed $prev_value The previous metadata value. |
||
| 446 | * |
||
| 447 | * @return null Null if the `meta_key` is not `Wordlift_User_Service::ENTITY_META_KEY` |
||
| 448 | * or if the author has not changed. |
||
| 449 | */ |
||
| 450 | public function update_user_metadata( $null, $object_id, $meta_key, $meta_value, $prev_value ) { |
||
| 481 | |||
| 482 | /** |
||
| 483 | * Hook on delete user meta to execute sparql query |
||
| 484 | * that will update all user posts author triple. |
||
| 485 | * |
||
| 486 | * @since 3.18.0 |
||
| 487 | * |
||
| 488 | * @param null $null |
||
| 489 | * @param int $object_id The user ID. |
||
| 490 | * @param string $meta_key The meta key name. |
||
| 491 | * @param mixed $meta_value Meta value. |
||
| 492 | * @param bool $delete_all Whether to delete the matching metadata entries |
||
| 493 | * for all objects. |
||
| 494 | * |
||
| 495 | * @return null Null if the `meta_key` is not `Wordlift_User_Service::ENTITY_META_KEY` |
||
| 496 | * or if the author has not changed. |
||
| 497 | */ |
||
| 498 | public function delete_user_metadata( $null, $object_id, $meta_key, $meta_value, $delete_all ) { |
||
| 522 | |||
| 523 | /** |
||
| 524 | * Update the schema:author when the user author is changed. |
||
| 525 | * |
||
| 526 | * @since 3.18.0 |
||
| 527 | * |
||
| 528 | * @param string $old_uri The old uri to remove. |
||
| 529 | * @param string $new_uri The new uri to add. |
||
| 530 | */ |
||
| 531 | private function update_author( $old_uri, $new_uri ) { |
||
| 555 | |||
| 556 | } |
||
| 557 |
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.