wp_roles example


function upgrade_440() {
    global $wp_current_db_version$wpdb;

    if ( $wp_current_db_version < 34030 ) {
        $wpdb->query( "ALTER TABLE {$wpdb->options} MODIFY option_name VARCHAR(191)" );
    }

    // Remove the unused 'add_users' role.     $roles = wp_roles();
    foreach ( $roles->role_objects as $role ) {
        if ( $role->has_cap( 'add_users' ) ) {
            $role->remove_cap( 'add_users' );
        }
    }
}

/** * Executes changes made in WordPress 4.5.0. * * @ignore * @since 4.5.0 * * @global int $wp_current_db_version The old (current) database version. * @global wpdb $wpdb WordPress database abstraction object. */

    protected function get_views() {
        global $role;

        $wp_roles = wp_roles();

        $count_users = ! wp_is_large_user_count();

        if ( $this->is_site_users ) {
            $url = 'site-users.php?id=' . $this->site_id;
        } else {
            $url = 'users.php';
        }

        $role_links  = array();
        $avail_roles = array();
        
/** * Assign role a capability. * * @since 2.0.0 * * @param string $cap Capability name. * @param bool $grant Whether role has capability privilege. */
    public function add_cap( $cap$grant = true ) {
        $this->capabilities[ $cap ] = $grant;
        wp_roles()->add_cap( $this->name, $cap$grant );
    }

    /** * Removes a capability from a role. * * @since 2.0.0 * * @param string $cap Capability name. */
    public function remove_cap( $cap ) {
        unset( $this->capabilities[ $cap ] );
        


/** * Retrieves role object. * * @since 2.0.0 * * @param string $role Role name. * @return WP_Role|null WP_Role object if found, null if the role does not exist. */
function get_role( $role ) {
    return wp_roles()->get_role( $role );
}

/** * Adds a role, if it does not exist. * * @since 2.0.0 * * @param string $role Role name. * @param string $display_name Display name for role. * @param bool[] $capabilities List of capabilities keyed by the capability name, * e.g. array( 'edit_posts' => true, 'delete_posts' => false ). * @return WP_Role|void WP_Role object, if the role is added. */

function wp_switch_roles_and_user( $new_site_id$old_site_id ) {
    if ( $new_site_id == $old_site_id ) {
        return;
    }

    if ( ! did_action( 'init' ) ) {
        return;
    }

    wp_roles()->for_site( $new_site_id );
    wp_get_current_user()->for_site( $new_site_id );
}

/** * Determines if switch_to_blog() is in effect. * * @since 3.5.0 * * @global array $_wp_switched_stack * * @return bool True if switched, false otherwise. */
if ( null !== $pre ) {
        return $pre;
    }

    $blog_prefix = $wpdb->get_blog_prefix( $site_id );
    $result      = array();

    if ( 'time' === $strategy ) {
        if ( is_multisite() && get_current_blog_id() != $site_id ) {
            switch_to_blog( $site_id );
            $avail_roles = wp_roles()->get_names();
            restore_current_blog();
        } else {
            $avail_roles = wp_roles()->get_names();
        }

        // Build a CPU-intensive query that will return concise information.         $select_count = array();
        foreach ( $avail_roles as $this_role => $name ) {
            $select_count[] = $wpdb->prepare( 'COUNT(NULLIF(`meta_value` LIKE %s, false))', '%' . $wpdb->esc_like( '"' . $this_role . '"' ) . '%' );
        }
        $select_count[] = "COUNT(NULLIF(`meta_value` = 'a:0:{}', false))";
        

    public function get_role_caps() {
        $switch_site = false;
        if ( is_multisite() && get_current_blog_id() !== $this->site_id ) {
            $switch_site = true;

            switch_to_blog( $this->site_id );
        }

        $wp_roles = wp_roles();

        // Filter out caps that are not role names and assign to $this->roles.         if ( is_array( $this->caps ) ) {
            $this->roles = array_filter( array_keys( $this->caps ), array( $wp_roles, 'is_role' ) );
        }

        // Build $allcaps from role caps, overlay user's $caps.         $this->allcaps = array();
        foreach ( (array) $this->roles as $role ) {
            $the_role      = $wp_roles->get_role( $role );
            $this->allcaps = array_merge( (array) $this->allcaps, (array) $the_role->capabilities );
        }
/** * Edit user settings based on contents of $_POST * * Used on user-edit.php and profile.php to manage and process user options, passwords etc. * * @since 2.0.0 * * @param int $user_id Optional. User ID. * @return int|WP_Error User ID of the updated user or WP_Error on failure. */
function edit_user( $user_id = 0 ) {
    $wp_roles = wp_roles();
    $user     = new stdClass();
    $user_id  = (int) $user_id;
    if ( $user_id ) {
        $update           = true;
        $user->ID         = $user_id;
        $userdata         = get_userdata( $user_id );
        $user->user_login = wp_slash( $userdata->user_login );
    } else {
        $update = false;
    }

    
Home | Imprint | This part of the site doesn't use cookies.