wp/wp-includes/class-wp-roles.php
changeset 21 48c4eec2b7e6
parent 19 3d72ae0968f4
child 22 8c2e4d02f4ef
equal deleted inserted replaced
20:7b1b88e27a20 21:48c4eec2b7e6
    21  *          )
    21  *          )
    22  *     )
    22  *     )
    23  *
    23  *
    24  * @since 2.0.0
    24  * @since 2.0.0
    25  */
    25  */
       
    26 #[AllowDynamicProperties]
    26 class WP_Roles {
    27 class WP_Roles {
    27 	/**
    28 	/**
    28 	 * List of roles and capabilities.
    29 	 * List of roles and capabilities.
    29 	 *
    30 	 *
    30 	 * @since 2.0.0
    31 	 * @since 2.0.0
    71 	 * @var int
    72 	 * @var int
    72 	 */
    73 	 */
    73 	protected $site_id = 0;
    74 	protected $site_id = 0;
    74 
    75 
    75 	/**
    76 	/**
    76 	 * Constructor
    77 	 * Constructor.
    77 	 *
    78 	 *
    78 	 * @since 2.0.0
    79 	 * @since 2.0.0
    79 	 * @since 4.9.0 The `$site_id` argument was added.
    80 	 * @since 4.9.0 The `$site_id` argument was added.
    80 	 *
    81 	 *
    81 	 * @global array $wp_user_roles Used to set the 'roles' property value.
    82 	 * @global array $wp_user_roles Used to set the 'roles' property value.
    89 
    90 
    90 		$this->for_site( $site_id );
    91 		$this->for_site( $site_id );
    91 	}
    92 	}
    92 
    93 
    93 	/**
    94 	/**
    94 	 * Make private/protected methods readable for backward compatibility.
    95 	 * Makes private/protected methods readable for backward compatibility.
    95 	 *
    96 	 *
    96 	 * @since 4.0.0
    97 	 * @since 4.0.0
    97 	 *
    98 	 *
    98 	 * @param string $name      Method to call.
    99 	 * @param string $name      Method to call.
    99 	 * @param array  $arguments Arguments to pass when calling.
   100 	 * @param array  $arguments Arguments to pass when calling.
   105 		}
   106 		}
   106 		return false;
   107 		return false;
   107 	}
   108 	}
   108 
   109 
   109 	/**
   110 	/**
   110 	 * Set up the object properties.
   111 	 * Sets up the object properties.
   111 	 *
   112 	 *
   112 	 * The role key is set to the current prefix for the $wpdb object with
   113 	 * The role key is set to the current prefix for the $wpdb object with
   113 	 * 'user_roles' appended. If the $wp_user_roles global is set, then it will
   114 	 * 'user_roles' appended. If the $wp_user_roles global is set, then it will
   114 	 * be used and the role option will not be updated or used.
   115 	 * be used and the role option will not be updated or used.
   115 	 *
   116 	 *
   121 
   122 
   122 		$this->for_site();
   123 		$this->for_site();
   123 	}
   124 	}
   124 
   125 
   125 	/**
   126 	/**
   126 	 * Reinitialize the object
   127 	 * Reinitializes the object.
   127 	 *
   128 	 *
   128 	 * Recreates the role objects. This is typically called only by switch_to_blog()
   129 	 * Recreates the role objects. This is typically called only by switch_to_blog()
   129 	 * after switching wpdb to a new site ID.
   130 	 * after switching wpdb to a new site ID.
   130 	 *
   131 	 *
   131 	 * @since 3.5.0
   132 	 * @since 3.5.0
   136 
   137 
   137 		$this->for_site();
   138 		$this->for_site();
   138 	}
   139 	}
   139 
   140 
   140 	/**
   141 	/**
   141 	 * Add role name with capabilities to list.
   142 	 * Adds a role name with capabilities to the list.
   142 	 *
   143 	 *
   143 	 * Updates the list of roles, if the role doesn't already exist.
   144 	 * Updates the list of roles, if the role doesn't already exist.
   144 	 *
   145 	 *
   145 	 * The capabilities are defined in the following format `array( 'read' => true );`
   146 	 * The capabilities are defined in the following format: `array( 'read' => true )`.
   146 	 * To explicitly deny a role a capability you set the value for that capability to false.
   147 	 * To explicitly deny the role a capability, set the value for that capability to false.
   147 	 *
   148 	 *
   148 	 * @since 2.0.0
   149 	 * @since 2.0.0
   149 	 *
   150 	 *
   150 	 * @param string $role         Role name.
   151 	 * @param string $role         Role name.
   151 	 * @param string $display_name Role display name.
   152 	 * @param string $display_name Role display name.
   152 	 * @param bool[] $capabilities List of capabilities keyed by the capability name,
   153 	 * @param bool[] $capabilities Optional. List of capabilities keyed by the capability name,
   153 	 *                             e.g. array( 'edit_posts' => true, 'delete_posts' => false ).
   154 	 *                             e.g. `array( 'edit_posts' => true, 'delete_posts' => false )`.
   154 	 * @return WP_Role|void WP_Role object, if role is added.
   155 	 *                             Default empty array.
       
   156 	 * @return WP_Role|void WP_Role object, if the role is added.
   155 	 */
   157 	 */
   156 	public function add_role( $role, $display_name, $capabilities = array() ) {
   158 	public function add_role( $role, $display_name, $capabilities = array() ) {
   157 		if ( empty( $role ) || isset( $this->roles[ $role ] ) ) {
   159 		if ( empty( $role ) || isset( $this->roles[ $role ] ) ) {
   158 			return;
   160 			return;
   159 		}
   161 		}
   169 		$this->role_names[ $role ]   = $display_name;
   171 		$this->role_names[ $role ]   = $display_name;
   170 		return $this->role_objects[ $role ];
   172 		return $this->role_objects[ $role ];
   171 	}
   173 	}
   172 
   174 
   173 	/**
   175 	/**
   174 	 * Remove role by name.
   176 	 * Removes a role by name.
   175 	 *
   177 	 *
   176 	 * @since 2.0.0
   178 	 * @since 2.0.0
   177 	 *
   179 	 *
   178 	 * @param string $role Role name.
   180 	 * @param string $role Role name.
   179 	 */
   181 	 */
   188 
   190 
   189 		if ( $this->use_db ) {
   191 		if ( $this->use_db ) {
   190 			update_option( $this->role_key, $this->roles );
   192 			update_option( $this->role_key, $this->roles );
   191 		}
   193 		}
   192 
   194 
   193 		if ( get_option( 'default_role' ) == $role ) {
   195 		if ( get_option( 'default_role' ) === $role ) {
   194 			update_option( 'default_role', 'subscriber' );
   196 			update_option( 'default_role', 'subscriber' );
   195 		}
   197 		}
   196 	}
   198 	}
   197 
   199 
   198 	/**
   200 	/**
   199 	 * Add capability to role.
   201 	 * Adds a capability to role.
   200 	 *
   202 	 *
   201 	 * @since 2.0.0
   203 	 * @since 2.0.0
   202 	 *
   204 	 *
   203 	 * @param string $role  Role name.
   205 	 * @param string $role  Role name.
   204 	 * @param string $cap   Capability name.
   206 	 * @param string $cap   Capability name.
   215 			update_option( $this->role_key, $this->roles );
   217 			update_option( $this->role_key, $this->roles );
   216 		}
   218 		}
   217 	}
   219 	}
   218 
   220 
   219 	/**
   221 	/**
   220 	 * Remove capability from role.
   222 	 * Removes a capability from role.
   221 	 *
   223 	 *
   222 	 * @since 2.0.0
   224 	 * @since 2.0.0
   223 	 *
   225 	 *
   224 	 * @param string $role Role name.
   226 	 * @param string $role Role name.
   225 	 * @param string $cap  Capability name.
   227 	 * @param string $cap  Capability name.
   234 			update_option( $this->role_key, $this->roles );
   236 			update_option( $this->role_key, $this->roles );
   235 		}
   237 		}
   236 	}
   238 	}
   237 
   239 
   238 	/**
   240 	/**
   239 	 * Retrieve role object by name.
   241 	 * Retrieves a role object by name.
   240 	 *
   242 	 *
   241 	 * @since 2.0.0
   243 	 * @since 2.0.0
   242 	 *
   244 	 *
   243 	 * @param string $role Role name.
   245 	 * @param string $role Role name.
   244 	 * @return WP_Role|null WP_Role object if found, null if the role does not exist.
   246 	 * @return WP_Role|null WP_Role object if found, null if the role does not exist.
   250 			return null;
   252 			return null;
   251 		}
   253 		}
   252 	}
   254 	}
   253 
   255 
   254 	/**
   256 	/**
   255 	 * Retrieve list of role names.
   257 	 * Retrieves a list of role names.
   256 	 *
   258 	 *
   257 	 * @since 2.0.0
   259 	 * @since 2.0.0
   258 	 *
   260 	 *
   259 	 * @return string[] List of role names.
   261 	 * @return string[] List of role names.
   260 	 */
   262 	 */
   261 	public function get_names() {
   263 	public function get_names() {
   262 		return $this->role_names;
   264 		return $this->role_names;
   263 	}
   265 	}
   264 
   266 
   265 	/**
   267 	/**
   266 	 * Whether role name is currently in the list of available roles.
   268 	 * Determines whether a role name is currently in the list of available roles.
   267 	 *
   269 	 *
   268 	 * @since 2.0.0
   270 	 * @since 2.0.0
   269 	 *
   271 	 *
   270 	 * @param string $role Role name to look up.
   272 	 * @param string $role Role name to look up.
   271 	 * @return bool
   273 	 * @return bool
   290 			$this->role_objects[ $role ] = new WP_Role( $role, $this->roles[ $role ]['capabilities'] );
   292 			$this->role_objects[ $role ] = new WP_Role( $role, $this->roles[ $role ]['capabilities'] );
   291 			$this->role_names[ $role ]   = $this->roles[ $role ]['name'];
   293 			$this->role_names[ $role ]   = $this->roles[ $role ]['name'];
   292 		}
   294 		}
   293 
   295 
   294 		/**
   296 		/**
   295 		 * After the roles have been initialized, allow plugins to add their own roles.
   297 		 * Fires after the roles have been initialized, allowing plugins to add their own roles.
   296 		 *
   298 		 *
   297 		 * @since 4.7.0
   299 		 * @since 4.7.0
   298 		 *
   300 		 *
   299 		 * @param WP_Roles $wp_roles A reference to the WP_Roles object.
   301 		 * @param WP_Roles $wp_roles A reference to the WP_Roles object.
   300 		 */
   302 		 */
   355 
   357 
   356 		if ( ! empty( $wp_user_roles ) ) {
   358 		if ( ! empty( $wp_user_roles ) ) {
   357 			return $wp_user_roles;
   359 			return $wp_user_roles;
   358 		}
   360 		}
   359 
   361 
   360 		if ( is_multisite() && get_current_blog_id() != $this->site_id ) {
   362 		if ( is_multisite() && get_current_blog_id() !== $this->site_id ) {
   361 			remove_action( 'switch_blog', 'wp_switch_roles_and_user', 1 );
   363 			remove_action( 'switch_blog', 'wp_switch_roles_and_user', 1 );
   362 
   364 
   363 			$roles = get_blog_option( $this->site_id, $this->role_key, array() );
   365 			$roles = get_blog_option( $this->site_id, $this->role_key, array() );
   364 
   366 
   365 			add_action( 'switch_blog', 'wp_switch_roles_and_user', 1, 2 );
   367 			add_action( 'switch_blog', 'wp_switch_roles_and_user', 1, 2 );