label, caps */ public static function groups() { return array( 'general' => array( __( 'General', 'ai-guardwall' ), array( 'read', 'edit_dashboard', 'manage_options', 'moderate_comments', 'manage_categories', 'manage_links', 'unfiltered_html', 'upload_files', 'unfiltered_upload', 'export', 'import' ) ), 'posts' => array( __( 'Posts', 'ai-guardwall' ), array( 'edit_posts', 'edit_others_posts', 'edit_published_posts', 'edit_private_posts', 'publish_posts', 'read_private_posts', 'delete_posts', 'delete_others_posts', 'delete_published_posts', 'delete_private_posts' ) ), 'pages' => array( __( 'Pages', 'ai-guardwall' ), array( 'edit_pages', 'edit_others_pages', 'edit_published_pages', 'edit_private_pages', 'publish_pages', 'read_private_pages', 'delete_pages', 'delete_others_pages', 'delete_published_pages', 'delete_private_pages' ) ), 'themes' => array( __( 'Appearance', 'ai-guardwall' ), array( 'switch_themes', 'edit_theme_options', 'customize', 'install_themes', 'update_themes', 'delete_themes', 'edit_themes' ) ), 'plugins' => array( __( 'Plugins', 'ai-guardwall' ), array( 'activate_plugins', 'install_plugins', 'update_plugins', 'delete_plugins', 'edit_plugins' ) ), 'users' => array( __( 'Users', 'ai-guardwall' ), array( 'list_users', 'create_users', 'edit_users', 'promote_users', 'delete_users', 'remove_users' ) ), 'core' => array( __( 'Updates and files', 'ai-guardwall' ), array( 'update_core', 'edit_files' ) ), ); } private static function core_caps() { $caps = array(); foreach ( self::groups() as $g ) { $caps = array_merge( $caps, $g[1] ); } for ( $i = 0; $i <= 10; $i++ ) { $caps[] = 'level_' . $i; } return $caps; } /** * Every capability any role has, plus the core ones and our custom ones. */ private static function all_caps() { $caps = self::core_caps(); foreach ( wp_roles()->roles as $role ) { $caps = array_merge( $caps, array_keys( (array) $role['capabilities'] ) ); } $caps = array_merge( $caps, (array) get_option( self::CUSTOM, array() ) ); $caps = array_values( array_unique( array_filter( array_map( 'strval', $caps ) ) ) ); sort( $caps ); return $caps; } public static function overview() { $counts = count_users(); $roles = array(); foreach ( wp_roles()->roles as $slug => $role ) { $granted = array_keys( array_filter( (array) $role['capabilities'] ) ); $roles[] = array( 'slug' => $slug, 'name' => translate_user_role( $role['name'] ), 'users' => isset( $counts['avail_roles'][ $slug ] ) ? (int) $counts['avail_roles'][ $slug ] : 0, 'caps' => $granted, 'default' => get_option( 'default_role' ) === $slug, 'core' => in_array( $slug, array( 'administrator', 'editor', 'author', 'contributor', 'subscriber' ), true ), ); } $core = self::core_caps(); $groups = array(); foreach ( self::groups() as $id => $g ) { $groups[] = array( 'id' => $id, 'label' => $g[0], 'caps' => $g[1], ); } $other = array_values( array_diff( self::all_caps(), $core ) ); $levels = array(); for ( $i = 0; $i <= 10; $i++ ) { $levels[] = 'level_' . $i; } $groups[] = array( 'id' => 'other', 'label' => __( 'From plugins and custom', 'ai-guardwall' ), 'caps' => $other, ); $groups[] = array( 'id' => 'levels', 'label' => __( 'Old user levels (kept for very old plugins)', 'ai-guardwall' ), 'caps' => $levels, ); return array( 'roles' => $roles, 'groups' => $groups, 'risky' => self::RISKY, 'admin_keep' => self::ADMIN_KEEPS, 'custom' => array_values( (array) get_option( self::CUSTOM, array() ) ), 'core' => $core, 'has_backup' => (bool) get_option( self::BACKUP ), ); } /** Keep the roles as they were before the first change. */ private static function backup() { if ( ! get_option( self::BACKUP ) ) { update_option( self::BACKUP, wp_roles()->roles, false ); } } /** * Whether the current user would still manage the site with these roles changed. * * @param string $role Role being changed. * @param array $caps Its capabilities after the change (cap => bool); null when deleted. * @return bool */ private static function keeps_access( $role, $caps ) { $user = wp_get_current_user(); if ( ! in_array( $role, (array) $user->roles, true ) || ( is_multisite() && is_super_admin( $user->ID ) ) ) { return true; } // Capabilities given to the user directly, outside any role. $own = get_user_meta( $user->ID, $GLOBALS['wpdb']->get_blog_prefix() . 'capabilities', true ); if ( is_array( $own ) && ! empty( $own['manage_options'] ) ) { return true; } foreach ( (array) $user->roles as $r ) { if ( $r === $role ) { if ( is_array( $caps ) && ! empty( $caps['manage_options'] ) ) { return true; } continue; } $obj = get_role( $r ); if ( $obj && $obj->has_cap( 'manage_options' ) ) { return true; } } return false; } /** * Set the capabilities of a role. * * @param string $slug Role. * @param array $granted Capabilities it should have. * @return array|WP_Error */ public static function save_role( $slug, array $granted ) { $role = get_role( $slug ); if ( ! $role ) { return new WP_Error( 'svabg_roles', __( 'That role does not exist.', 'ai-guardwall' ) ); } $granted = array_values( array_unique( array_filter( array_map( array( __CLASS__, 'clean_cap' ), $granted ) ) ) ); if ( 'administrator' === $slug ) { $missing = array_diff( self::ADMIN_KEEPS, $granted ); if ( $missing ) { return new WP_Error( 'svabg_roles', sprintf( /* translators: %s: list of missing */ __( 'Administrators must keep these capabilities, or nobody can run the site: %s', 'ai-guardwall' ), implode( ', ', $missing ) ) ); } } $after = array_fill_keys( $granted, true ); if ( ! self::keeps_access( $slug, $after ) ) { return new WP_Error( 'svabg_roles', __( 'This would take away your own access to the settings (manage_options). Give it to another of your roles first.', 'ai-guardwall' ) ); } self::backup(); $before = array_keys( array_filter( (array) $role->capabilities ) ); $added = array_diff( $granted, $before ); $removed = array_diff( $before, $granted ); foreach ( $added as $cap ) { $role->add_cap( $cap ); } foreach ( $removed as $cap ) { $role->remove_cap( $cap ); } $risky = array_intersect( $added, self::RISKY ); if ( $added || $removed ) { Softvolt_Ai_Bodyguard_Logger::log( $risky ? 'warning' : 'notice', 'role_changed', sprintf( /* translators: 1: slug, 2: value, 3: value */ __( 'Role %1$s changed: added %2$s; removed %3$s', 'ai-guardwall' ), $slug, $added ? implode( ', ', $added ) : '-', $removed ? implode( ', ', $removed ) : '-' ) ); } return array( 'message' => sprintf( /* translators: 1: number of added, 2: number of removed */ __( 'Saved: %1$d added, %2$d removed.', 'ai-guardwall' ), count( $added ), count( $removed ) ), 'risky' => array_values( $risky ), ); } public static function clean_cap( $cap ) { $cap = strtolower( trim( (string) $cap ) ); return preg_match( '/^[a-z0-9_]{1,100}$/', $cap ) ? $cap : ''; } /** * Add a role. * * @param string $slug Role id. * @param string $name Display name. * @param string $copy Copy the capabilities of this role. * @return array|WP_Error */ public static function add( $slug, $name, $copy = '' ) { $slug = sanitize_key( str_replace( ' ', '_', strtolower( (string) $slug ) ) ); $name = sanitize_text_field( (string) $name ); if ( '' === $slug && '' !== $name ) { $slug = sanitize_key( str_replace( ' ', '_', strtolower( $name ) ) ); } if ( '' === $slug ) { return new WP_Error( 'svabg_roles', __( 'Give the role a name.', 'ai-guardwall' ) ); } if ( get_role( $slug ) ) { return new WP_Error( 'svabg_roles', sprintf( /* translators: %s: slug */ __( 'A role called "%s" already exists.', 'ai-guardwall' ), $slug ) ); } $caps = array( 'read' => true ); $from = '' !== $copy ? get_role( $copy ) : null; if ( $from ) { $caps = array_filter( (array) $from->capabilities ); } self::backup(); add_role( $slug, '' !== $name ? $name : ucwords( str_replace( '_', ' ', $slug ) ), $caps ); Softvolt_Ai_Bodyguard_Logger::log( 'notice', 'role_added', sprintf( /* translators: 1: slug, 2: value */ __( 'Role added: %1$s (copied from %2$s)', 'ai-guardwall' ), $slug, $from ? $copy : '-' ) ); return array( 'message' => __( 'Role added.', 'ai-guardwall' ), 'slug' => $slug, ); } public static function rename( $slug, $name ) { $name = sanitize_text_field( (string) $name ); $roles = wp_roles(); if ( ! isset( $roles->roles[ $slug ] ) || '' === $name ) { return new WP_Error( 'svabg_roles', __( 'Type the new name.', 'ai-guardwall' ) ); } self::backup(); $roles->roles[ $slug ]['name'] = $name; $roles->role_names[ $slug ] = $name; update_option( $roles->role_key, $roles->roles ); Softvolt_Ai_Bodyguard_Logger::log( 'info', 'role_renamed', sprintf( /* translators: 1: slug, 2: name */ __( 'Role %1$s renamed to %2$s', 'ai-guardwall' ), $slug, $name ) ); return array( 'message' => __( 'Renamed.', 'ai-guardwall' ) ); } /** * Delete a role; its users move to another role. */ public static function delete( $slug, $move_to ) { if ( 'administrator' === $slug ) { return new WP_Error( 'svabg_roles', __( 'The Administrator role cannot be deleted.', 'ai-guardwall' ) ); } if ( get_option( 'default_role' ) === $slug ) { return new WP_Error( 'svabg_roles', __( 'This is the role new users get (Settings › General). Choose another default role first.', 'ai-guardwall' ) ); } if ( ! get_role( $slug ) ) { return new WP_Error( 'svabg_roles', __( 'That role does not exist.', 'ai-guardwall' ) ); } if ( ! self::keeps_access( $slug, null ) ) { return new WP_Error( 'svabg_roles', __( 'You have this role and it is what lets you manage the site. Deleting it would lock you out.', 'ai-guardwall' ) ); } $move_to = get_role( $move_to ) ? $move_to : get_option( 'default_role', 'subscriber' ); if ( $move_to === $slug ) { return new WP_Error( 'svabg_roles', __( 'Choose another role for its users.', 'ai-guardwall' ) ); } self::backup(); $moved = 0; foreach ( get_users( array( 'role' => $slug ) ) as $user ) { $user->remove_role( $slug ); if ( ! $user->roles ) { $user->add_role( $move_to ); } ++$moved; } remove_role( $slug ); Softvolt_Ai_Bodyguard_Logger::log( 'notice', 'role_deleted', sprintf( /* translators: 1: slug, 2: moved, 3: move to */ __( 'Role %1$s deleted; %2$d users moved to %3$s', 'ai-guardwall' ), $slug, $moved, $move_to ) ); return array( 'message' => sprintf( /* translators: %d: moved */ _n( 'Role deleted; %d user was moved.', 'Role deleted; %d users were moved.', $moved, 'ai-guardwall' ), $moved ) ); } /** * Add a capability of your own (given to Administrators so it exists). */ public static function add_cap( $cap ) { $cap = self::clean_cap( $cap ); if ( '' === $cap ) { return new WP_Error( 'svabg_roles', __( 'Capabilities use lower-case letters, digits and underscores, such as "manage_events".', 'ai-guardwall' ) ); } $custom = (array) get_option( self::CUSTOM, array() ); if ( in_array( $cap, self::all_caps(), true ) ) { return new WP_Error( 'svabg_roles', __( 'That capability already exists.', 'ai-guardwall' ) ); } self::backup(); $custom[] = $cap; update_option( self::CUSTOM, array_values( array_unique( $custom ) ), false ); $admin = get_role( 'administrator' ); if ( $admin ) { $admin->add_cap( $cap ); } Softvolt_Ai_Bodyguard_Logger::log( 'info', 'cap_added', sprintf( /* translators: %s: cap */ __( 'Capability added: %s', 'ai-guardwall' ), $cap ) ); return array( 'message' => __( 'Capability added and given to Administrators.', 'ai-guardwall' ) ); } /** * Remove a capability from every role (not WordPress's own ones). */ public static function delete_cap( $cap ) { $cap = self::clean_cap( $cap ); if ( '' === $cap || in_array( $cap, self::core_caps(), true ) ) { return new WP_Error( 'svabg_roles', __( 'WordPress\'s own capabilities cannot be deleted; untick them on the roles instead.', 'ai-guardwall' ) ); } self::backup(); foreach ( array_keys( wp_roles()->roles ) as $slug ) { $role = get_role( $slug ); if ( $role && isset( $role->capabilities[ $cap ] ) ) { $role->remove_cap( $cap ); } } update_option( self::CUSTOM, array_values( array_diff( (array) get_option( self::CUSTOM, array() ), array( $cap ) ) ), false ); Softvolt_Ai_Bodyguard_Logger::log( 'notice', 'cap_deleted', sprintf( /* translators: %s: cap */ __( 'Capability removed from every role: %s', 'ai-guardwall' ), $cap ) ); return array( 'message' => __( 'Capability removed from every role.', 'ai-guardwall' ) ); } /** * Put the roles back as they were before the first change made here. */ public static function restore() { $backup = get_option( self::BACKUP ); if ( ! is_array( $backup ) || empty( $backup['administrator'] ) ) { return new WP_Error( 'svabg_roles', __( 'There is no earlier copy of the roles.', 'ai-guardwall' ) ); } $roles = wp_roles(); update_option( $roles->role_key, $backup ); $roles->for_site(); Softvolt_Ai_Bodyguard_Logger::log( 'warning', 'roles_restored', __( 'Roles put back as they were before the first change.', 'ai-guardwall' ) ); return array( 'message' => __( 'The roles are back as they were before the first change.', 'ai-guardwall' ) ); } /* ------------------------------ users ------------------------------ */ public static function find_users( $search ) { $search = trim( (string) $search ); $args = array( 'number' => 20, 'orderby' => 'login', 'fields' => array( 'ID', 'user_login', 'display_name', 'user_email' ), ); if ( '' !== $search ) { $args['search'] = '*' . $search . '*'; $args['search_columns'] = array( 'user_login', 'user_email', 'display_name' ); } $out = array(); foreach ( get_users( $args ) as $u ) { $user = get_userdata( $u->ID ); $out[] = array( 'id' => (int) $u->ID, 'login' => $u->user_login, 'name' => $u->display_name, 'email' => $u->user_email, 'roles' => array_values( (array) $user->roles ), ); } return array( 'users' => $out ); } /** * Give a user one or more roles. * * @param int $user_id User. * @param array $roles Roles (the first one is the main role). * @return array|WP_Error */ public static function set_user_roles( $user_id, array $roles ) { $user = get_userdata( (int) $user_id ); if ( ! $user ) { return new WP_Error( 'svabg_roles', __( 'That user does not exist.', 'ai-guardwall' ) ); } if ( ! current_user_can( 'promote_user', $user->ID ) ) { return new WP_Error( 'svabg_roles', __( 'You may not change this user\'s roles.', 'ai-guardwall' ) ); } $valid = array_keys( wp_roles()->roles ); $roles = array_values( array_unique( array_intersect( array_map( 'sanitize_key', $roles ), $valid ) ) ); if ( ! $roles ) { return new WP_Error( 'svabg_roles', __( 'Choose at least one role.', 'ai-guardwall' ) ); } if ( get_current_user_id() === $user->ID ) { $keeps = false; foreach ( $roles as $r ) { $obj = get_role( $r ); if ( $obj && $obj->has_cap( 'manage_options' ) ) { $keeps = true; } } if ( ! $keeps ) { return new WP_Error( 'svabg_roles', __( 'You cannot take away your own administrator access.', 'ai-guardwall' ) ); } } $before = (array) $user->roles; $user->set_role( $roles[0] ); foreach ( array_slice( $roles, 1 ) as $r ) { $user->add_role( $r ); } Softvolt_Ai_Bodyguard_Logger::log( in_array( 'administrator', array_diff( $roles, $before ), true ) ? 'warning' : 'notice', 'user_roles', sprintf( /* translators: 1: user login, 2: list of before, 3: list of roles */ __( 'Roles of %1$s changed from %2$s to %3$s', 'ai-guardwall' ), $user->user_login, implode( ', ', $before ), implode( ', ', $roles ) ) ); return array( 'message' => __( 'Roles saved.', 'ai-guardwall' ) ); } }