/wp-includes/class-wp-roles.php

https://github.com/livinglab/openlab · PHP · 372 lines · 130 code · 44 blank · 198 comment · 23 complexity · d6c53f161854efa576bf9ff07c55ff79 MD5 · raw file

  1. <?php
  2. /**
  3. * User API: WP_Roles class
  4. *
  5. * @package WordPress
  6. * @subpackage Users
  7. * @since 4.4.0
  8. */
  9. /**
  10. * Core class used to implement a user roles API.
  11. *
  12. * The role option is simple, the structure is organized by role name that store
  13. * the name in value of the 'name' key. The capabilities are stored as an array
  14. * in the value of the 'capability' key.
  15. *
  16. * array (
  17. * 'rolename' => array (
  18. * 'name' => 'rolename',
  19. * 'capabilities' => array()
  20. * )
  21. * )
  22. *
  23. * @since 2.0.0
  24. */
  25. class WP_Roles {
  26. /**
  27. * List of roles and capabilities.
  28. *
  29. * @since 2.0.0
  30. * @var array[]
  31. */
  32. public $roles;
  33. /**
  34. * List of the role objects.
  35. *
  36. * @since 2.0.0
  37. * @var WP_Role[]
  38. */
  39. public $role_objects = array();
  40. /**
  41. * List of role names.
  42. *
  43. * @since 2.0.0
  44. * @var string[]
  45. */
  46. public $role_names = array();
  47. /**
  48. * Option name for storing role list.
  49. *
  50. * @since 2.0.0
  51. * @var string
  52. */
  53. public $role_key;
  54. /**
  55. * Whether to use the database for retrieval and storage.
  56. *
  57. * @since 2.1.0
  58. * @var bool
  59. */
  60. public $use_db = true;
  61. /**
  62. * The site ID the roles are initialized for.
  63. *
  64. * @since 4.9.0
  65. * @var int
  66. */
  67. protected $site_id = 0;
  68. /**
  69. * Constructor
  70. *
  71. * @since 2.0.0
  72. * @since 4.9.0 The `$site_id` argument was added.
  73. *
  74. * @global array $wp_user_roles Used to set the 'roles' property value.
  75. *
  76. * @param int $site_id Site ID to initialize roles for. Default is the current site.
  77. */
  78. public function __construct( $site_id = null ) {
  79. global $wp_user_roles;
  80. $this->use_db = empty( $wp_user_roles );
  81. $this->for_site( $site_id );
  82. }
  83. /**
  84. * Make private/protected methods readable for backward compatibility.
  85. *
  86. * @since 4.0.0
  87. *
  88. * @param string $name Method to call.
  89. * @param array $arguments Arguments to pass when calling.
  90. * @return mixed|false Return value of the callback, false otherwise.
  91. */
  92. public function __call( $name, $arguments ) {
  93. if ( '_init' === $name ) {
  94. return $this->_init( ...$arguments );
  95. }
  96. return false;
  97. }
  98. /**
  99. * Set up the object properties.
  100. *
  101. * The role key is set to the current prefix for the $wpdb object with
  102. * 'user_roles' appended. If the $wp_user_roles global is set, then it will
  103. * be used and the role option will not be updated or used.
  104. *
  105. * @since 2.1.0
  106. * @deprecated 4.9.0 Use WP_Roles::for_site()
  107. */
  108. protected function _init() {
  109. _deprecated_function( __METHOD__, '4.9.0', 'WP_Roles::for_site()' );
  110. $this->for_site();
  111. }
  112. /**
  113. * Reinitialize the object
  114. *
  115. * Recreates the role objects. This is typically called only by switch_to_blog()
  116. * after switching wpdb to a new site ID.
  117. *
  118. * @since 3.5.0
  119. * @deprecated 4.7.0 Use WP_Roles::for_site()
  120. */
  121. public function reinit() {
  122. _deprecated_function( __METHOD__, '4.7.0', 'WP_Roles::for_site()' );
  123. $this->for_site();
  124. }
  125. /**
  126. * Add role name with capabilities to list.
  127. *
  128. * Updates the list of roles, if the role doesn't already exist.
  129. *
  130. * The capabilities are defined in the following format `array( 'read' => true );`
  131. * To explicitly deny a role a capability you set the value for that capability to false.
  132. *
  133. * @since 2.0.0
  134. *
  135. * @param string $role Role name.
  136. * @param string $display_name Role display name.
  137. * @param bool[] $capabilities List of capabilities keyed by the capability name,
  138. * e.g. array( 'edit_posts' => true, 'delete_posts' => false ).
  139. * @return WP_Role|void WP_Role object, if role is added.
  140. */
  141. public function add_role( $role, $display_name, $capabilities = array() ) {
  142. if ( empty( $role ) || isset( $this->roles[ $role ] ) ) {
  143. return;
  144. }
  145. $this->roles[ $role ] = array(
  146. 'name' => $display_name,
  147. 'capabilities' => $capabilities,
  148. );
  149. if ( $this->use_db ) {
  150. update_option( $this->role_key, $this->roles );
  151. }
  152. $this->role_objects[ $role ] = new WP_Role( $role, $capabilities );
  153. $this->role_names[ $role ] = $display_name;
  154. return $this->role_objects[ $role ];
  155. }
  156. /**
  157. * Remove role by name.
  158. *
  159. * @since 2.0.0
  160. *
  161. * @param string $role Role name.
  162. */
  163. public function remove_role( $role ) {
  164. if ( ! isset( $this->role_objects[ $role ] ) ) {
  165. return;
  166. }
  167. unset( $this->role_objects[ $role ] );
  168. unset( $this->role_names[ $role ] );
  169. unset( $this->roles[ $role ] );
  170. if ( $this->use_db ) {
  171. update_option( $this->role_key, $this->roles );
  172. }
  173. if ( get_option( 'default_role' ) == $role ) {
  174. update_option( 'default_role', 'subscriber' );
  175. }
  176. }
  177. /**
  178. * Add capability to role.
  179. *
  180. * @since 2.0.0
  181. *
  182. * @param string $role Role name.
  183. * @param string $cap Capability name.
  184. * @param bool $grant Optional. Whether role is capable of performing capability.
  185. * Default true.
  186. */
  187. public function add_cap( $role, $cap, $grant = true ) {
  188. if ( ! isset( $this->roles[ $role ] ) ) {
  189. return;
  190. }
  191. $this->roles[ $role ]['capabilities'][ $cap ] = $grant;
  192. if ( $this->use_db ) {
  193. update_option( $this->role_key, $this->roles );
  194. }
  195. }
  196. /**
  197. * Remove capability from role.
  198. *
  199. * @since 2.0.0
  200. *
  201. * @param string $role Role name.
  202. * @param string $cap Capability name.
  203. */
  204. public function remove_cap( $role, $cap ) {
  205. if ( ! isset( $this->roles[ $role ] ) ) {
  206. return;
  207. }
  208. unset( $this->roles[ $role ]['capabilities'][ $cap ] );
  209. if ( $this->use_db ) {
  210. update_option( $this->role_key, $this->roles );
  211. }
  212. }
  213. /**
  214. * Retrieve role object by name.
  215. *
  216. * @since 2.0.0
  217. *
  218. * @param string $role Role name.
  219. * @return WP_Role|null WP_Role object if found, null if the role does not exist.
  220. */
  221. public function get_role( $role ) {
  222. if ( isset( $this->role_objects[ $role ] ) ) {
  223. return $this->role_objects[ $role ];
  224. } else {
  225. return null;
  226. }
  227. }
  228. /**
  229. * Retrieve list of role names.
  230. *
  231. * @since 2.0.0
  232. *
  233. * @return string[] List of role names.
  234. */
  235. public function get_names() {
  236. return $this->role_names;
  237. }
  238. /**
  239. * Whether role name is currently in the list of available roles.
  240. *
  241. * @since 2.0.0
  242. *
  243. * @param string $role Role name to look up.
  244. * @return bool
  245. */
  246. public function is_role( $role ) {
  247. return isset( $this->role_names[ $role ] );
  248. }
  249. /**
  250. * Initializes all of the available roles.
  251. *
  252. * @since 4.9.0
  253. */
  254. public function init_roles() {
  255. if ( empty( $this->roles ) ) {
  256. return;
  257. }
  258. $this->role_objects = array();
  259. $this->role_names = array();
  260. foreach ( array_keys( $this->roles ) as $role ) {
  261. $this->role_objects[ $role ] = new WP_Role( $role, $this->roles[ $role ]['capabilities'] );
  262. $this->role_names[ $role ] = $this->roles[ $role ]['name'];
  263. }
  264. /**
  265. * After the roles have been initialized, allow plugins to add their own roles.
  266. *
  267. * @since 4.7.0
  268. *
  269. * @param WP_Roles $this A reference to the WP_Roles object.
  270. */
  271. do_action( 'wp_roles_init', $this );
  272. }
  273. /**
  274. * Sets the site to operate on. Defaults to the current site.
  275. *
  276. * @since 4.9.0
  277. *
  278. * @global wpdb $wpdb WordPress database abstraction object.
  279. *
  280. * @param int $site_id Site ID to initialize roles for. Default is the current site.
  281. */
  282. public function for_site( $site_id = null ) {
  283. global $wpdb;
  284. if ( ! empty( $site_id ) ) {
  285. $this->site_id = absint( $site_id );
  286. } else {
  287. $this->site_id = get_current_blog_id();
  288. }
  289. $this->role_key = $wpdb->get_blog_prefix( $this->site_id ) . 'user_roles';
  290. if ( ! empty( $this->roles ) && ! $this->use_db ) {
  291. return;
  292. }
  293. $this->roles = $this->get_roles_data();
  294. $this->init_roles();
  295. }
  296. /**
  297. * Gets the ID of the site for which roles are currently initialized.
  298. *
  299. * @since 4.9.0
  300. *
  301. * @return int Site ID.
  302. */
  303. public function get_site_id() {
  304. return $this->site_id;
  305. }
  306. /**
  307. * Gets the available roles data.
  308. *
  309. * @since 4.9.0
  310. *
  311. * @global array $wp_user_roles Used to set the 'roles' property value.
  312. *
  313. * @return array Roles array.
  314. */
  315. protected function get_roles_data() {
  316. global $wp_user_roles;
  317. if ( ! empty( $wp_user_roles ) ) {
  318. return $wp_user_roles;
  319. }
  320. if ( is_multisite() && get_current_blog_id() != $this->site_id ) {
  321. remove_action( 'switch_blog', 'wp_switch_roles_and_user', 1 );
  322. $roles = get_blog_option( $this->site_id, $this->role_key, array() );
  323. add_action( 'switch_blog', 'wp_switch_roles_and_user', 1, 2 );
  324. return $roles;
  325. }
  326. return get_option( $this->role_key, array() );
  327. }
  328. }