Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like WP_User 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 WP_User, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 36 | class WP_User { |
||
| 37 | /** |
||
| 38 | * User data container. |
||
| 39 | * |
||
| 40 | * @since 2.0.0 |
||
| 41 | * @var object |
||
| 42 | */ |
||
| 43 | public $data; |
||
| 44 | |||
| 45 | /** |
||
| 46 | * The user's ID. |
||
| 47 | * |
||
| 48 | * @since 2.1.0 |
||
| 49 | * @access public |
||
| 50 | * @var int |
||
| 51 | */ |
||
| 52 | public $ID = 0; |
||
| 53 | |||
| 54 | /** |
||
| 55 | * The individual capabilities the user has been given. |
||
| 56 | * |
||
| 57 | * @since 2.0.0 |
||
| 58 | * @access public |
||
| 59 | * @var array |
||
| 60 | */ |
||
| 61 | public $caps = array(); |
||
| 62 | |||
| 63 | /** |
||
| 64 | * User metadata option name. |
||
| 65 | * |
||
| 66 | * @since 2.0.0 |
||
| 67 | * @access public |
||
| 68 | * @var string |
||
| 69 | */ |
||
| 70 | public $cap_key; |
||
| 71 | |||
| 72 | /** |
||
| 73 | * The roles the user is part of. |
||
| 74 | * |
||
| 75 | * @since 2.0.0 |
||
| 76 | * @access public |
||
| 77 | * @var array |
||
| 78 | */ |
||
| 79 | public $roles = array(); |
||
| 80 | |||
| 81 | /** |
||
| 82 | * All capabilities the user has, including individual and role based. |
||
| 83 | * |
||
| 84 | * @since 2.0.0 |
||
| 85 | * @access public |
||
| 86 | * @var array |
||
| 87 | */ |
||
| 88 | public $allcaps = array(); |
||
| 89 | |||
| 90 | /** |
||
| 91 | * The filter context applied to user data fields. |
||
| 92 | * |
||
| 93 | * @since 2.9.0 |
||
| 94 | * @access public |
||
| 95 | * @var string |
||
| 96 | */ |
||
| 97 | public $filter = null; |
||
| 98 | |||
| 99 | /** |
||
| 100 | * @static |
||
| 101 | * @since 3.3.0 |
||
| 102 | * @access private |
||
| 103 | * @var array |
||
| 104 | */ |
||
| 105 | private static $back_compat_keys; |
||
| 106 | |||
| 107 | /** |
||
| 108 | * Constructor. |
||
| 109 | * |
||
| 110 | * Retrieves the userdata and passes it to WP_User::init(). |
||
| 111 | * |
||
| 112 | * @since 2.0.0 |
||
| 113 | * @access public |
||
| 114 | * |
||
| 115 | * @global wpdb $wpdb WordPress database abstraction object. |
||
| 116 | * |
||
| 117 | * @param int|string|stdClass|WP_User $id User's ID, a WP_User object, or a user object from the DB. |
||
|
|
|||
| 118 | * @param string $name Optional. User's username |
||
| 119 | * @param int $blog_id Optional Site ID, defaults to current site. |
||
| 120 | */ |
||
| 121 | public function __construct( $id = 0, $name = '', $blog_id = '' ) { |
||
| 159 | |||
| 160 | /** |
||
| 161 | * Sets up object properties, including capabilities. |
||
| 162 | * |
||
| 163 | * @since 3.3.0 |
||
| 164 | * |
||
| 165 | * @param object $data User DB row object. |
||
| 166 | * @param int $blog_id Optional. The site ID to initialize for. |
||
| 167 | */ |
||
| 168 | public function init( $data, $blog_id = '' ) { |
||
| 174 | |||
| 175 | /** |
||
| 176 | * Return only the main user fields |
||
| 177 | * |
||
| 178 | * @since 3.3.0 |
||
| 179 | * @since 4.4.0 Added 'ID' as an alias of 'id' for the `$field` parameter. |
||
| 180 | * |
||
| 181 | * @static |
||
| 182 | * |
||
| 183 | * @global wpdb $wpdb WordPress database abstraction object. |
||
| 184 | * |
||
| 185 | * @param string $field The field to query against: 'id', 'ID', 'slug', 'email' or 'login'. |
||
| 186 | * @param string|int $value The field value |
||
| 187 | * @return object|false Raw user object |
||
| 188 | */ |
||
| 189 | public static function get_data_by( $field, $value ) { |
||
| 248 | |||
| 249 | /** |
||
| 250 | * Makes private/protected methods readable for backward compatibility. |
||
| 251 | * |
||
| 252 | * @since 4.3.0 |
||
| 253 | * @access public |
||
| 254 | * |
||
| 255 | * @param callable $name Method to call. |
||
| 256 | * @param array $arguments Arguments to pass when calling. |
||
| 257 | * @return mixed|false Return value of the callback, false otherwise. |
||
| 258 | */ |
||
| 259 | public function __call( $name, $arguments ) { |
||
| 265 | |||
| 266 | /** |
||
| 267 | * Magic method for checking the existence of a certain custom field. |
||
| 268 | * |
||
| 269 | * @since 3.3.0 |
||
| 270 | * @access public |
||
| 271 | * |
||
| 272 | * @param string $key User meta key to check if set. |
||
| 273 | * @return bool Whether the given user meta key is set. |
||
| 274 | */ |
||
| 275 | public function __isset( $key ) { |
||
| 295 | |||
| 296 | /** |
||
| 297 | * Magic method for accessing custom fields. |
||
| 298 | * |
||
| 299 | * @since 3.3.0 |
||
| 300 | * @access public |
||
| 301 | * |
||
| 302 | * @param string $key User meta key to retrieve. |
||
| 303 | * @return mixed Value of the given user meta key (if set). If `$key` is 'id', the user ID. |
||
| 304 | */ |
||
| 305 | public function __get( $key ) { |
||
| 331 | |||
| 332 | /** |
||
| 333 | * Magic method for setting custom user fields. |
||
| 334 | * |
||
| 335 | * This method does not update custom fields in the database. It only stores |
||
| 336 | * the value on the WP_User instance. |
||
| 337 | * |
||
| 338 | * @since 3.3.0 |
||
| 339 | * @access public |
||
| 340 | * |
||
| 341 | * @param string $key User meta key. |
||
| 342 | * @param mixed $value User meta value. |
||
| 343 | */ |
||
| 344 | public function __set( $key, $value ) { |
||
| 359 | |||
| 360 | /** |
||
| 361 | * Magic method for unsetting a certain custom field. |
||
| 362 | * |
||
| 363 | * @since 4.4.0 |
||
| 364 | * @access public |
||
| 365 | * |
||
| 366 | * @param string $key User meta key to unset. |
||
| 367 | */ |
||
| 368 | public function __unset( $key ) { |
||
| 387 | |||
| 388 | /** |
||
| 389 | * Determine whether the user exists in the database. |
||
| 390 | * |
||
| 391 | * @since 3.4.0 |
||
| 392 | * @access public |
||
| 393 | * |
||
| 394 | * @return bool True if user exists in the database, false if not. |
||
| 395 | */ |
||
| 396 | public function exists() { |
||
| 399 | |||
| 400 | /** |
||
| 401 | * Retrieve the value of a property or meta key. |
||
| 402 | * |
||
| 403 | * Retrieves from the users and usermeta table. |
||
| 404 | * |
||
| 405 | * @since 3.3.0 |
||
| 406 | * |
||
| 407 | * @param string $key Property |
||
| 408 | * @return mixed |
||
| 409 | */ |
||
| 410 | public function get( $key ) { |
||
| 413 | |||
| 414 | /** |
||
| 415 | * Determine whether a property or meta key is set |
||
| 416 | * |
||
| 417 | * Consults the users and usermeta tables. |
||
| 418 | * |
||
| 419 | * @since 3.3.0 |
||
| 420 | * |
||
| 421 | * @param string $key Property |
||
| 422 | * @return bool |
||
| 423 | */ |
||
| 424 | public function has_prop( $key ) { |
||
| 427 | |||
| 428 | /** |
||
| 429 | * Return an array representation. |
||
| 430 | * |
||
| 431 | * @since 3.5.0 |
||
| 432 | * |
||
| 433 | * @return array Array representation. |
||
| 434 | */ |
||
| 435 | public function to_array() { |
||
| 438 | |||
| 439 | /** |
||
| 440 | * Set up capability object properties. |
||
| 441 | * |
||
| 442 | * Will set the value for the 'cap_key' property to current database table |
||
| 443 | * prefix, followed by 'capabilities'. Will then check to see if the |
||
| 444 | * property matching the 'cap_key' exists and is an array. If so, it will be |
||
| 445 | * used. |
||
| 446 | * |
||
| 447 | * @access protected |
||
| 448 | * @since 2.1.0 |
||
| 449 | * |
||
| 450 | * @global wpdb $wpdb WordPress database abstraction object. |
||
| 451 | * |
||
| 452 | * @param string $cap_key Optional capability key |
||
| 453 | */ |
||
| 454 | protected function _init_caps( $cap_key = '' ) { |
||
| 469 | |||
| 470 | /** |
||
| 471 | * Retrieve all of the role capabilities and merge with individual capabilities. |
||
| 472 | * |
||
| 473 | * All of the capabilities of the roles the user belongs to are merged with |
||
| 474 | * the users individual roles. This also means that the user can be denied |
||
| 475 | * specific roles that their role might have, but the specific user isn't |
||
| 476 | * granted permission to. |
||
| 477 | * |
||
| 478 | * @since 2.0.0 |
||
| 479 | * @access public |
||
| 480 | * |
||
| 481 | * @return array List of all capabilities for the user. |
||
| 482 | */ |
||
| 483 | public function get_role_caps() { |
||
| 500 | |||
| 501 | /** |
||
| 502 | * Add role to user. |
||
| 503 | * |
||
| 504 | * Updates the user's meta data option with capabilities and roles. |
||
| 505 | * |
||
| 506 | * @since 2.0.0 |
||
| 507 | * @access public |
||
| 508 | * |
||
| 509 | * @param string $role Role name. |
||
| 510 | */ |
||
| 511 | View Code Duplication | public function add_role( $role ) { |
|
| 531 | |||
| 532 | /** |
||
| 533 | * Remove role from user. |
||
| 534 | * |
||
| 535 | * @since 2.0.0 |
||
| 536 | * @access public |
||
| 537 | * |
||
| 538 | * @param string $role Role name. |
||
| 539 | */ |
||
| 540 | View Code Duplication | public function remove_role( $role ) { |
|
| 558 | |||
| 559 | /** |
||
| 560 | * Set the role of the user. |
||
| 561 | * |
||
| 562 | * This will remove the previous roles of the user and assign the user the |
||
| 563 | * new one. You can set the role to an empty string and it will remove all |
||
| 564 | * of the roles from the user. |
||
| 565 | * |
||
| 566 | * @since 2.0.0 |
||
| 567 | * @access public |
||
| 568 | * |
||
| 569 | * @param string $role Role name. |
||
| 570 | */ |
||
| 571 | public function set_role( $role ) { |
||
| 601 | |||
| 602 | /** |
||
| 603 | * Choose the maximum level the user has. |
||
| 604 | * |
||
| 605 | * Will compare the level from the $item parameter against the $max |
||
| 606 | * parameter. If the item is incorrect, then just the $max parameter value |
||
| 607 | * will be returned. |
||
| 608 | * |
||
| 609 | * Used to get the max level based on the capabilities the user has. This |
||
| 610 | * is also based on roles, so if the user is assigned the Administrator role |
||
| 611 | * then the capability 'level_10' will exist and the user will get that |
||
| 612 | * value. |
||
| 613 | * |
||
| 614 | * @since 2.0.0 |
||
| 615 | * @access public |
||
| 616 | * |
||
| 617 | * @param int $max Max level of user. |
||
| 618 | * @param string $item Level capability name. |
||
| 619 | * @return int Max Level. |
||
| 620 | */ |
||
| 621 | public function level_reduction( $max, $item ) { |
||
| 629 | |||
| 630 | /** |
||
| 631 | * Update the maximum user level for the user. |
||
| 632 | * |
||
| 633 | * Updates the 'user_level' user metadata (includes prefix that is the |
||
| 634 | * database table prefix) with the maximum user level. Gets the value from |
||
| 635 | * the all of the capabilities that the user has. |
||
| 636 | * |
||
| 637 | * @since 2.0.0 |
||
| 638 | * @access public |
||
| 639 | * |
||
| 640 | * @global wpdb $wpdb WordPress database abstraction object. |
||
| 641 | */ |
||
| 642 | public function update_user_level_from_caps() { |
||
| 647 | |||
| 648 | /** |
||
| 649 | * Add capability and grant or deny access to capability. |
||
| 650 | * |
||
| 651 | * @since 2.0.0 |
||
| 652 | * @access public |
||
| 653 | * |
||
| 654 | * @param string $cap Capability name. |
||
| 655 | * @param bool $grant Whether to grant capability to user. |
||
| 656 | */ |
||
| 657 | public function add_cap( $cap, $grant = true ) { |
||
| 663 | |||
| 664 | /** |
||
| 665 | * Remove capability from user. |
||
| 666 | * |
||
| 667 | * @since 2.0.0 |
||
| 668 | * @access public |
||
| 669 | * |
||
| 670 | * @param string $cap Capability name. |
||
| 671 | */ |
||
| 672 | public function remove_cap( $cap ) { |
||
| 681 | |||
| 682 | /** |
||
| 683 | * Remove all of the capabilities of the user. |
||
| 684 | * |
||
| 685 | * @since 2.1.0 |
||
| 686 | * @access public |
||
| 687 | * |
||
| 688 | * @global wpdb $wpdb WordPress database abstraction object. |
||
| 689 | */ |
||
| 690 | public function remove_all_caps() { |
||
| 697 | |||
| 698 | /** |
||
| 699 | * Whether user has capability or role name. |
||
| 700 | * |
||
| 701 | * While checking against particular roles in place of a capability is supported |
||
| 702 | * in part, this practice is discouraged as it may produce unreliable results. |
||
| 703 | * |
||
| 704 | * @since 2.0.0 |
||
| 705 | * @access public |
||
| 706 | * |
||
| 707 | * @see map_meta_cap() |
||
| 708 | * |
||
| 709 | * @param string $cap Capability name. |
||
| 710 | * @param int $object_id,... Optional. ID of the specific object to check against if `$cap` is a "meta" cap. |
||
| 711 | * "Meta" capabilities, e.g. 'edit_post', 'edit_user', etc., are capabilities used |
||
| 712 | * by map_meta_cap() to map to other "primitive" capabilities, e.g. 'edit_posts', |
||
| 713 | * 'edit_others_posts', etc. The parameter is accessed via func_get_args() and passed |
||
| 714 | * to map_meta_cap(). |
||
| 715 | * @return bool Whether the current user has the given capability. If `$cap` is a meta cap and `$object_id` is |
||
| 716 | * passed, whether the current user has the given meta capability for the given object. |
||
| 717 | */ |
||
| 718 | public function has_cap( $cap ) { |
||
| 759 | |||
| 760 | /** |
||
| 761 | * Convert numeric level to level capability name. |
||
| 762 | * |
||
| 763 | * Prepends 'level_' to level number. |
||
| 764 | * |
||
| 765 | * @since 2.0.0 |
||
| 766 | * @access public |
||
| 767 | * |
||
| 768 | * @param int $level Level number, 1 to 10. |
||
| 769 | * @return string |
||
| 770 | */ |
||
| 771 | public function translate_level_to_cap( $level ) { |
||
| 774 | |||
| 775 | /** |
||
| 776 | * Set the site to operate on. Defaults to the current site. |
||
| 777 | * |
||
| 778 | * @since 3.0.0 |
||
| 779 | * |
||
| 780 | * @global wpdb $wpdb WordPress database abstraction object. |
||
| 781 | * |
||
| 782 | * @param int $blog_id Optional. Site ID, defaults to current site. |
||
| 783 | */ |
||
| 784 | public function for_blog( $blog_id = '' ) { |
||
| 792 | } |
||
| 793 |