/wp-includes/class-wp-user.php

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