class-wp-user.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793
  1. <?php
  2. /**
  3. * User API: WP_User class
  4. *
  5. * @package WordPress
  6. * @subpackage Users
  7. * @since 4.4.0
  8. */
  9. /**
  10. * Core class used to implement the WP_User object.
  11. *
  12. * @since 2.0.0
  13. *
  14. * @property string $nickname
  15. * @property string $description
  16. * @property string $user_description
  17. * @property string $first_name
  18. * @property string $user_firstname
  19. * @property string $last_name
  20. * @property string $user_lastname
  21. * @property string $user_login
  22. * @property string $user_pass
  23. * @property string $user_nicename
  24. * @property string $user_email
  25. * @property string $user_url
  26. * @property string $user_registered
  27. * @property string $user_activation_key
  28. * @property string $user_status
  29. * @property int $user_level
  30. * @property string $display_name
  31. * @property string $spam
  32. * @property string $deleted
  33. * @property string $locale
  34. */
  35. class WP_User {
  36. /**
  37. * User data container.
  38. *
  39. * @since 2.0.0
  40. * @var object
  41. */
  42. public $data;
  43. /**
  44. * The user's ID.
  45. *
  46. * @since 2.1.0
  47. * @access public
  48. * @var int
  49. */
  50. public $ID = 0;
  51. /**
  52. * The individual capabilities the user has been given.
  53. *
  54. * @since 2.0.0
  55. * @access public
  56. * @var array
  57. */
  58. public $caps = array();
  59. /**
  60. * User metadata option name.
  61. *
  62. * @since 2.0.0
  63. * @access public
  64. * @var string
  65. */
  66. public $cap_key;
  67. /**
  68. * The roles the user is part of.
  69. *
  70. * @since 2.0.0
  71. * @access public
  72. * @var array
  73. */
  74. public $roles = array();
  75. /**
  76. * All capabilities the user has, including individual and role based.
  77. *
  78. * @since 2.0.0
  79. * @access public
  80. * @var array
  81. */
  82. public $allcaps = array();
  83. /**
  84. * The filter context applied to user data fields.
  85. *
  86. * @since 2.9.0
  87. * @access public
  88. * @var string
  89. */
  90. public $filter = null;
  91. /**
  92. * @static
  93. * @since 3.3.0
  94. * @access private
  95. * @var array
  96. */
  97. private static $back_compat_keys;
  98. /**
  99. * Constructor.
  100. *
  101. * Retrieves the userdata and passes it to WP_User::init().
  102. *
  103. * @since 2.0.0
  104. * @access public
  105. *
  106. * @global wpdb $wpdb WordPress database abstraction object.
  107. *
  108. * @param int|string|stdClass|WP_User $id User's ID, a WP_User object, or a user object from the DB.
  109. * @param string $name Optional. User's username
  110. * @param int $blog_id Optional Site ID, defaults to current site.
  111. */
  112. public function __construct( $id = 0, $name = '', $blog_id = '' ) {
  113. if ( ! isset( self::$back_compat_keys ) ) {
  114. $prefix = $GLOBALS['wpdb']->prefix;
  115. self::$back_compat_keys = array(
  116. 'user_firstname' => 'first_name',
  117. 'user_lastname' => 'last_name',
  118. 'user_description' => 'description',
  119. 'user_level' => $prefix . 'user_level',
  120. $prefix . 'usersettings' => $prefix . 'user-settings',
  121. $prefix . 'usersettingstime' => $prefix . 'user-settings-time',
  122. );
  123. }
  124. if ( $id instanceof WP_User ) {
  125. $this->init( $id->data, $blog_id );
  126. return;
  127. } elseif ( is_object( $id ) ) {
  128. $this->init( $id, $blog_id );
  129. return;
  130. }
  131. if ( ! empty( $id ) && ! is_numeric( $id ) ) {
  132. $name = $id;
  133. $id = 0;
  134. }
  135. if ( $id ) {
  136. $data = self::get_data_by( 'id', $id );
  137. } else {
  138. $data = self::get_data_by( 'login', $name );
  139. }
  140. if ( $data ) {
  141. $this->init( $data, $blog_id );
  142. } else {
  143. $this->data = new stdClass;
  144. }
  145. }
  146. /**
  147. * Sets up object properties, including capabilities.
  148. *
  149. * @since 3.3.0
  150. *
  151. * @param object $data User DB row object.
  152. * @param int $blog_id Optional. The site ID to initialize for.
  153. */
  154. public function init( $data, $blog_id = '' ) {
  155. $this->data = $data;
  156. $this->ID = (int) $data->ID;
  157. $this->for_blog( $blog_id );
  158. }
  159. /**
  160. * Return only the main user fields
  161. *
  162. * @since 3.3.0
  163. * @since 4.4.0 Added 'ID' as an alias of 'id' for the `$field` parameter.
  164. *
  165. * @static
  166. *
  167. * @global wpdb $wpdb WordPress database abstraction object.
  168. *
  169. * @param string $field The field to query against: 'id', 'ID', 'slug', 'email' or 'login'.
  170. * @param string|int $value The field value
  171. * @return object|false Raw user object
  172. */
  173. public static function get_data_by( $field, $value ) {
  174. global $wpdb;
  175. // 'ID' is an alias of 'id'.
  176. if ( 'ID' === $field ) {
  177. $field = 'id';
  178. }
  179. if ( 'id' == $field ) {
  180. // Make sure the value is numeric to avoid casting objects, for example,
  181. // to int 1.
  182. if ( ! is_numeric( $value ) )
  183. return false;
  184. $value = intval( $value );
  185. if ( $value < 1 )
  186. return false;
  187. } else {
  188. $value = trim( $value );
  189. }
  190. if ( !$value )
  191. return false;
  192. switch ( $field ) {
  193. case 'id':
  194. $user_id = $value;
  195. $db_field = 'ID';
  196. break;
  197. case 'slug':
  198. $user_id = wp_cache_get($value, 'userslugs');
  199. $db_field = 'user_nicename';
  200. break;
  201. case 'email':
  202. $user_id = wp_cache_get($value, 'useremail');
  203. $db_field = 'user_email';
  204. break;
  205. case 'login':
  206. $value = sanitize_user( $value );
  207. $user_id = wp_cache_get($value, 'userlogins');
  208. $db_field = 'user_login';
  209. break;
  210. default:
  211. return false;
  212. }
  213. if ( false !== $user_id ) {
  214. if ( $user = wp_cache_get( $user_id, 'users' ) )
  215. return $user;
  216. }
  217. if ( !$user = $wpdb->get_row( $wpdb->prepare(
  218. "SELECT * FROM $wpdb->users WHERE $db_field = %s", $value
  219. ) ) )
  220. return false;
  221. update_user_caches( $user );
  222. return $user;
  223. }
  224. /**
  225. * Makes private/protected methods readable for backward compatibility.
  226. *
  227. * @since 4.3.0
  228. * @access public
  229. *
  230. * @param callable $name Method to call.
  231. * @param array $arguments Arguments to pass when calling.
  232. * @return mixed|false Return value of the callback, false otherwise.
  233. */
  234. public function __call( $name, $arguments ) {
  235. if ( '_init_caps' === $name ) {
  236. return call_user_func_array( array( $this, $name ), $arguments );
  237. }
  238. return false;
  239. }
  240. /**
  241. * Magic method for checking the existence of a certain custom field.
  242. *
  243. * @since 3.3.0
  244. * @access public
  245. *
  246. * @param string $key User meta key to check if set.
  247. * @return bool Whether the given user meta key is set.
  248. */
  249. public function __isset( $key ) {
  250. if ( 'id' == $key ) {
  251. _deprecated_argument( 'WP_User->id', '2.1.0',
  252. sprintf(
  253. /* translators: %s: WP_User->ID */
  254. __( 'Use %s instead.' ),
  255. '<code>WP_User->ID</code>'
  256. )
  257. );
  258. $key = 'ID';
  259. }
  260. if ( isset( $this->data->$key ) )
  261. return true;
  262. if ( isset( self::$back_compat_keys[ $key ] ) )
  263. $key = self::$back_compat_keys[ $key ];
  264. return metadata_exists( 'user', $this->ID, $key );
  265. }
  266. /**
  267. * Magic method for accessing custom fields.
  268. *
  269. * @since 3.3.0
  270. * @access public
  271. *
  272. * @param string $key User meta key to retrieve.
  273. * @return mixed Value of the given user meta key (if set). If `$key` is 'id', the user ID.
  274. */
  275. public function __get( $key ) {
  276. if ( 'id' == $key ) {
  277. _deprecated_argument( 'WP_User->id', '2.1.0',
  278. sprintf(
  279. /* translators: %s: WP_User->ID */
  280. __( 'Use %s instead.' ),
  281. '<code>WP_User->ID</code>'
  282. )
  283. );
  284. return $this->ID;
  285. }
  286. if ( isset( $this->data->$key ) ) {
  287. $value = $this->data->$key;
  288. } else {
  289. if ( isset( self::$back_compat_keys[ $key ] ) )
  290. $key = self::$back_compat_keys[ $key ];
  291. $value = get_user_meta( $this->ID, $key, true );
  292. }
  293. if ( $this->filter ) {
  294. $value = sanitize_user_field( $key, $value, $this->ID, $this->filter );
  295. }
  296. return $value;
  297. }
  298. /**
  299. * Magic method for setting custom user fields.
  300. *
  301. * This method does not update custom fields in the database. It only stores
  302. * the value on the WP_User instance.
  303. *
  304. * @since 3.3.0
  305. * @access public
  306. *
  307. * @param string $key User meta key.
  308. * @param mixed $value User meta value.
  309. */
  310. public function __set( $key, $value ) {
  311. if ( 'id' == $key ) {
  312. _deprecated_argument( 'WP_User->id', '2.1.0',
  313. sprintf(
  314. /* translators: %s: WP_User->ID */
  315. __( 'Use %s instead.' ),
  316. '<code>WP_User->ID</code>'
  317. )
  318. );
  319. $this->ID = $value;
  320. return;
  321. }
  322. $this->data->$key = $value;
  323. }
  324. /**
  325. * Magic method for unsetting a certain custom field.
  326. *
  327. * @since 4.4.0
  328. * @access public
  329. *
  330. * @param string $key User meta key to unset.
  331. */
  332. public function __unset( $key ) {
  333. if ( 'id' == $key ) {
  334. _deprecated_argument( 'WP_User->id', '2.1.0',
  335. sprintf(
  336. /* translators: %s: WP_User->ID */
  337. __( 'Use %s instead.' ),
  338. '<code>WP_User->ID</code>'
  339. )
  340. );
  341. }
  342. if ( isset( $this->data->$key ) ) {
  343. unset( $this->data->$key );
  344. }
  345. if ( isset( self::$back_compat_keys[ $key ] ) ) {
  346. unset( self::$back_compat_keys[ $key ] );
  347. }
  348. }
  349. /**
  350. * Determine whether the user exists in the database.
  351. *
  352. * @since 3.4.0
  353. * @access public
  354. *
  355. * @return bool True if user exists in the database, false if not.
  356. */
  357. public function exists() {
  358. return ! empty( $this->ID );
  359. }
  360. /**
  361. * Retrieve the value of a property or meta key.
  362. *
  363. * Retrieves from the users and usermeta table.
  364. *
  365. * @since 3.3.0
  366. *
  367. * @param string $key Property
  368. * @return mixed
  369. */
  370. public function get( $key ) {
  371. return $this->__get( $key );
  372. }
  373. /**
  374. * Determine whether a property or meta key is set
  375. *
  376. * Consults the users and usermeta tables.
  377. *
  378. * @since 3.3.0
  379. *
  380. * @param string $key Property
  381. * @return bool
  382. */
  383. public function has_prop( $key ) {
  384. return $this->__isset( $key );
  385. }
  386. /**
  387. * Return an array representation.
  388. *
  389. * @since 3.5.0
  390. *
  391. * @return array Array representation.
  392. */
  393. public function to_array() {
  394. return get_object_vars( $this->data );
  395. }
  396. /**
  397. * Set up capability object properties.
  398. *
  399. * Will set the value for the 'cap_key' property to current database table
  400. * prefix, followed by 'capabilities'. Will then check to see if the
  401. * property matching the 'cap_key' exists and is an array. If so, it will be
  402. * used.
  403. *
  404. * @access protected
  405. * @since 2.1.0
  406. *
  407. * @global wpdb $wpdb WordPress database abstraction object.
  408. *
  409. * @param string $cap_key Optional capability key
  410. */
  411. protected function _init_caps( $cap_key = '' ) {
  412. global $wpdb;
  413. if ( empty($cap_key) )
  414. $this->cap_key = $wpdb->get_blog_prefix() . 'capabilities';
  415. else
  416. $this->cap_key = $cap_key;
  417. $this->caps = get_user_meta( $this->ID, $this->cap_key, true );
  418. if ( ! is_array( $this->caps ) )
  419. $this->caps = array();
  420. $this->get_role_caps();
  421. }
  422. /**
  423. * Retrieve all of the role capabilities and merge with individual capabilities.
  424. *
  425. * All of the capabilities of the roles the user belongs to are merged with
  426. * the users individual roles. This also means that the user can be denied
  427. * specific roles that their role might have, but the specific user isn't
  428. * granted permission to.
  429. *
  430. * @since 2.0.0
  431. * @access public
  432. *
  433. * @return array List of all capabilities for the user.
  434. */
  435. public function get_role_caps() {
  436. $wp_roles = wp_roles();
  437. //Filter out caps that are not role names and assign to $this->roles
  438. if ( is_array( $this->caps ) )
  439. $this->roles = array_filter( array_keys( $this->caps ), array( $wp_roles, 'is_role' ) );
  440. //Build $allcaps from role caps, overlay user's $caps
  441. $this->allcaps = array();
  442. foreach ( (array) $this->roles as $role ) {
  443. $the_role = $wp_roles->get_role( $role );
  444. $this->allcaps = array_merge( (array) $this->allcaps, (array) $the_role->capabilities );
  445. }
  446. $this->allcaps = array_merge( (array) $this->allcaps, (array) $this->caps );
  447. return $this->allcaps;
  448. }
  449. /**
  450. * Add role to user.
  451. *
  452. * Updates the user's meta data option with capabilities and roles.
  453. *
  454. * @since 2.0.0
  455. * @access public
  456. *
  457. * @param string $role Role name.
  458. */
  459. public function add_role( $role ) {
  460. if ( empty( $role ) ) {
  461. return;
  462. }
  463. $this->caps[$role] = true;
  464. update_user_meta( $this->ID, $this->cap_key, $this->caps );
  465. $this->get_role_caps();
  466. $this->update_user_level_from_caps();
  467. /**
  468. * Fires immediately after the user has been given a new role.
  469. *
  470. * @since 4.3.0
  471. *
  472. * @param int $user_id The user ID.
  473. * @param string $role The new role.
  474. */
  475. do_action( 'add_user_role', $this->ID, $role );
  476. }
  477. /**
  478. * Remove role from user.
  479. *
  480. * @since 2.0.0
  481. * @access public
  482. *
  483. * @param string $role Role name.
  484. */
  485. public function remove_role( $role ) {
  486. if ( !in_array($role, $this->roles) )
  487. return;
  488. unset( $this->caps[$role] );
  489. update_user_meta( $this->ID, $this->cap_key, $this->caps );
  490. $this->get_role_caps();
  491. $this->update_user_level_from_caps();
  492. /**
  493. * Fires immediately after a role as been removed from a user.
  494. *
  495. * @since 4.3.0
  496. *
  497. * @param int $user_id The user ID.
  498. * @param string $role The removed role.
  499. */
  500. do_action( 'remove_user_role', $this->ID, $role );
  501. }
  502. /**
  503. * Set the role of the user.
  504. *
  505. * This will remove the previous roles of the user and assign the user the
  506. * new one. You can set the role to an empty string and it will remove all
  507. * of the roles from the user.
  508. *
  509. * @since 2.0.0
  510. * @access public
  511. *
  512. * @param string $role Role name.
  513. */
  514. public function set_role( $role ) {
  515. if ( 1 == count( $this->roles ) && $role == current( $this->roles ) )
  516. return;
  517. foreach ( (array) $this->roles as $oldrole )
  518. unset( $this->caps[$oldrole] );
  519. $old_roles = $this->roles;
  520. if ( !empty( $role ) ) {
  521. $this->caps[$role] = true;
  522. $this->roles = array( $role => true );
  523. } else {
  524. $this->roles = false;
  525. }
  526. update_user_meta( $this->ID, $this->cap_key, $this->caps );
  527. $this->get_role_caps();
  528. $this->update_user_level_from_caps();
  529. /**
  530. * Fires after the user's role has changed.
  531. *
  532. * @since 2.9.0
  533. * @since 3.6.0 Added $old_roles to include an array of the user's previous roles.
  534. *
  535. * @param int $user_id The user ID.
  536. * @param string $role The new role.
  537. * @param array $old_roles An array of the user's previous roles.
  538. */
  539. do_action( 'set_user_role', $this->ID, $role, $old_roles );
  540. }
  541. /**
  542. * Choose the maximum level the user has.
  543. *
  544. * Will compare the level from the $item parameter against the $max
  545. * parameter. If the item is incorrect, then just the $max parameter value
  546. * will be returned.
  547. *
  548. * Used to get the max level based on the capabilities the user has. This
  549. * is also based on roles, so if the user is assigned the Administrator role
  550. * then the capability 'level_10' will exist and the user will get that
  551. * value.
  552. *
  553. * @since 2.0.0
  554. * @access public
  555. *
  556. * @param int $max Max level of user.
  557. * @param string $item Level capability name.
  558. * @return int Max Level.
  559. */
  560. public function level_reduction( $max, $item ) {
  561. if ( preg_match( '/^level_(10|[0-9])$/i', $item, $matches ) ) {
  562. $level = intval( $matches[1] );
  563. return max( $max, $level );
  564. } else {
  565. return $max;
  566. }
  567. }
  568. /**
  569. * Update the maximum user level for the user.
  570. *
  571. * Updates the 'user_level' user metadata (includes prefix that is the
  572. * database table prefix) with the maximum user level. Gets the value from
  573. * the all of the capabilities that the user has.
  574. *
  575. * @since 2.0.0
  576. * @access public
  577. *
  578. * @global wpdb $wpdb WordPress database abstraction object.
  579. */
  580. public function update_user_level_from_caps() {
  581. global $wpdb;
  582. $this->user_level = array_reduce( array_keys( $this->allcaps ), array( $this, 'level_reduction' ), 0 );
  583. update_user_meta( $this->ID, $wpdb->get_blog_prefix() . 'user_level', $this->user_level );
  584. }
  585. /**
  586. * Add capability and grant or deny access to capability.
  587. *
  588. * @since 2.0.0
  589. * @access public
  590. *
  591. * @param string $cap Capability name.
  592. * @param bool $grant Whether to grant capability to user.
  593. */
  594. public function add_cap( $cap, $grant = true ) {
  595. $this->caps[$cap] = $grant;
  596. update_user_meta( $this->ID, $this->cap_key, $this->caps );
  597. $this->get_role_caps();
  598. $this->update_user_level_from_caps();
  599. }
  600. /**
  601. * Remove capability from user.
  602. *
  603. * @since 2.0.0
  604. * @access public
  605. *
  606. * @param string $cap Capability name.
  607. */
  608. public function remove_cap( $cap ) {
  609. if ( ! isset( $this->caps[ $cap ] ) ) {
  610. return;
  611. }
  612. unset( $this->caps[ $cap ] );
  613. update_user_meta( $this->ID, $this->cap_key, $this->caps );
  614. $this->get_role_caps();
  615. $this->update_user_level_from_caps();
  616. }
  617. /**
  618. * Remove all of the capabilities of the user.
  619. *
  620. * @since 2.1.0
  621. * @access public
  622. *
  623. * @global wpdb $wpdb WordPress database abstraction object.
  624. */
  625. public function remove_all_caps() {
  626. global $wpdb;
  627. $this->caps = array();
  628. delete_user_meta( $this->ID, $this->cap_key );
  629. delete_user_meta( $this->ID, $wpdb->get_blog_prefix() . 'user_level' );
  630. $this->get_role_caps();
  631. }
  632. /**
  633. * Whether user has capability or role name.
  634. *
  635. * While checking against particular roles in place of a capability is supported
  636. * in part, this practice is discouraged as it may produce unreliable results.
  637. *
  638. * @since 2.0.0
  639. * @access public
  640. *
  641. * @see map_meta_cap()
  642. *
  643. * @param string $cap Capability name.
  644. * @param int $object_id,... Optional. ID of the specific object to check against if `$cap` is a "meta" cap.
  645. * "Meta" capabilities, e.g. 'edit_post', 'edit_user', etc., are capabilities used
  646. * by map_meta_cap() to map to other "primitive" capabilities, e.g. 'edit_posts',
  647. * 'edit_others_posts', etc. The parameter is accessed via func_get_args() and passed
  648. * to map_meta_cap().
  649. * @return bool Whether the current user has the given capability. If `$cap` is a meta cap and `$object_id` is
  650. * passed, whether the current user has the given meta capability for the given object.
  651. */
  652. public function has_cap( $cap ) {
  653. if ( is_numeric( $cap ) ) {
  654. _deprecated_argument( __FUNCTION__, '2.0.0', __('Usage of user levels by plugins and themes is deprecated. Use roles and capabilities instead.') );
  655. $cap = $this->translate_level_to_cap( $cap );
  656. }
  657. $args = array_slice( func_get_args(), 1 );
  658. $args = array_merge( array( $cap, $this->ID ), $args );
  659. $caps = call_user_func_array( 'map_meta_cap', $args );
  660. // Multisite super admin has all caps by definition, Unless specifically denied.
  661. if ( is_multisite() && is_super_admin( $this->ID ) ) {
  662. if ( in_array('do_not_allow', $caps) )
  663. return false;
  664. return true;
  665. }
  666. /**
  667. * Dynamically filter a user's capabilities.
  668. *
  669. * @since 2.0.0
  670. * @since 3.7.0 Added the user object.
  671. *
  672. * @param array $allcaps An array of all the user's capabilities.
  673. * @param array $caps Actual capabilities for meta capability.
  674. * @param array $args Optional parameters passed to has_cap(), typically object ID.
  675. * @param WP_User $user The user object.
  676. */
  677. $capabilities = apply_filters( 'user_has_cap', $this->allcaps, $caps, $args, $this );
  678. // Everyone is allowed to exist.
  679. $capabilities['exist'] = true;
  680. // Must have ALL requested caps.
  681. foreach ( (array) $caps as $cap ) {
  682. if ( empty( $capabilities[ $cap ] ) )
  683. return false;
  684. }
  685. return true;
  686. }
  687. /**
  688. * Convert numeric level to level capability name.
  689. *
  690. * Prepends 'level_' to level number.
  691. *
  692. * @since 2.0.0
  693. * @access public
  694. *
  695. * @param int $level Level number, 1 to 10.
  696. * @return string
  697. */
  698. public function translate_level_to_cap( $level ) {
  699. return 'level_' . $level;
  700. }
  701. /**
  702. * Set the site to operate on. Defaults to the current site.
  703. *
  704. * @since 3.0.0
  705. *
  706. * @global wpdb $wpdb WordPress database abstraction object.
  707. *
  708. * @param int $blog_id Optional. Site ID, defaults to current site.
  709. */
  710. public function for_blog( $blog_id = '' ) {
  711. global $wpdb;
  712. if ( ! empty( $blog_id ) )
  713. $cap_key = $wpdb->get_blog_prefix( $blog_id ) . 'capabilities';
  714. else
  715. $cap_key = '';
  716. $this->_init_caps( $cap_key );
  717. }
  718. }