roles; return isset( $roles[ $slug ]['name'] ) ? translate_user_role( (string) $roles[ $slug ]['name'] ) : (string) $slug; } public static function is_role_admin() { return current_user_can( 'promote_users' ); } private static function editable() { if ( ! function_exists( 'get_editable_roles' ) ) { require_once ABSPATH . 'wp-admin/includes/user.php'; } return (array) get_editable_roles(); } private static function admin_may( $role ) { return self::is_role_admin() && isset( self::editable()[ (string) $role ] ); } private static function delegate_may( $role ) { return current_user_can( self::cap( $role ) ) && AIEO_Safe_Roles::is_safe( (string) $role ); } public static function may_assign( $role ) { return null !== get_role( (string) $role ) && ( self::admin_may( $role ) || self::delegate_may( $role ) ); } public static function assignable() { $out = array(); foreach ( wp_roles()->roles as $slug => $data ) { if ( self::may_assign( (string) $slug ) ) { $out[ (string) $slug ] = translate_user_role( (string) ( $data['name'] ?? $slug ) ); } } return $out; } public static function can_use() { return self::is_role_admin() || array() !== self::assignable(); } public static function check( $user_id, $role ) { $user_id = (int) $user_id; $role = (string) $role; if ( null === get_role( $role ) || ! get_userdata( $user_id ) ) { return new WP_Error( 'aieo_roles_missing', __( 'There is no such account or role.', 'ai-eshop-optimizer' ) ); } if ( self::admin_may( $role ) && current_user_can( 'promote_user', $user_id ) ) { return true; } if ( ! self::delegate_may( $role ) ) { return new WP_Error( 'aieo_roles_role', __( 'You may not give or take this role.', 'ai-eshop-optimizer' ) ); } if ( get_current_user_id() === $user_id ) { return new WP_Error( 'aieo_roles_self', __( 'An administrator changes your own roles.', 'ai-eshop-optimizer' ) ); } if ( ! AIEO_Safe_Roles::is_customer_account( $user_id ) ) { return new WP_Error( 'aieo_roles_account', __( 'An administrator manages this account’s roles.', 'ai-eshop-optimizer' ) ); } return true; } public static function may_manage( $user_id ) { foreach ( array_keys( self::assignable() ) as $role ) { if ( true === self::check( $user_id, $role ) ) { return true; } } return false; } public static function give( $user_id, $role, $source = 'manual' ) { $ok = self::check( $user_id, $role ); if ( true !== $ok ) { return $ok; } $user = get_userdata( (int) $user_id ); if ( ! in_array( (string) $role, (array) $user->roles, true ) ) { $before = AIEO_Safe_Roles::$source; AIEO_Safe_Roles::$source = (string) $source; $user->add_role( (string) $role ); AIEO_Safe_Roles::$source = $before; } return true; } public static function set( $user_id, array $wanted, $source = 'manual' ) { $user = get_userdata( (int) $user_id ); if ( ! $user instanceof WP_User ) { return new WP_Error( 'aieo_roles_missing', __( 'There is no such account or role.', 'ai-eshop-optimizer' ) ); } $have = array_values( array_map( 'strval', (array) $user->roles ) ); $wanted = array_values( array_unique( array_map( 'strval', $wanted ) ) ); $add = array(); $remove = array(); foreach ( array_unique( array_merge( $have, $wanted ) ) as $role ) { $want = in_array( $role, $wanted, true ); if ( in_array( $role, $have, true ) === $want || true !== self::check( $user->ID, $role ) ) { continue; } if ( $want ) { $add[] = $role; } else { $remove[] = $role; } } $kept = ''; $after = array_values( array_diff( array_merge( $have, $add ), $remove ) ); if ( array() === $after ) { $kept = AIEO_Safe_Roles::fallback_role(); if ( '' === $kept ) { return new WP_Error( 'aieo_roles_none', __( 'An account keeps at least one role, and this site has no customer role to give it.', 'ai-eshop-optimizer' ) ); } if ( in_array( $kept, $remove, true ) ) { $remove = array_values( array_diff( $remove, array( $kept ) ) ); } else { $add[] = $kept; } $after = array( $kept ); } if ( get_current_user_id() === (int) $user->ID && self::is_role_admin() && ! ( is_multisite() && is_super_admin() ) && ! self::grants( $after, (array) $user->caps, 'promote_users' ) ) { return new WP_Error( 'aieo_roles_lockout', __( 'You cannot take from yourself the role that lets you manage roles.', 'ai-eshop-optimizer' ) ); } if ( in_array( 'administrator', $remove, true ) && count( get_users( array( 'role' => 'administrator', 'fields' => 'ID', 'number' => 2 ) ) ) < 2 ) { return new WP_Error( 'aieo_roles_last_admin', __( 'The site keeps at least one administrator.', 'ai-eshop-optimizer' ) ); } $before = AIEO_Safe_Roles::$source; AIEO_Safe_Roles::$source = (string) $source; foreach ( $add as $role ) { $user->add_role( $role ); } foreach ( $remove as $role ) { $user->remove_role( $role ); } AIEO_Safe_Roles::$source = $before; return array( 'added' => $add, 'removed' => $remove, 'kept' => $kept, ); } private static function grants( array $roles, array $caps, $cap ) { if ( ! empty( $caps[ $cap ] ) ) { return true; } foreach ( $roles as $role ) { $obj = get_role( (string) $role ); if ( $obj && $obj->has_cap( $cap ) ) { return true; } } return false; } public static function staff_roles() { $out = array(); foreach ( wp_roles()->roles as $slug => $data ) { if ( 'administrator' !== $slug && ! AIEO_Safe_Roles::is_safe( (string) $slug ) ) { $out[ (string) $slug ] = translate_user_role( (string) ( $data['name'] ?? $slug ) ); } } return $out; } public static function delegations() { $out = array(); foreach ( wp_roles()->roles as $slug => $data ) { foreach ( (array) ( $data['capabilities'] ?? array() ) as $cap => $granted ) { if ( $granted && 0 === strpos( (string) $cap, self::CAP_PREFIX ) ) { $out[ (string) $slug ][] = substr( (string) $cap, strlen( self::CAP_PREFIX ) ); } } } return $out; } public static function set_delegation( $staff_role, $role, $granted ) { if ( ! self::is_role_admin() ) { return new WP_Error( 'aieo_roles_denied', __( 'Only an administrator decides who may give roles.', 'ai-eshop-optimizer' ) ); } $staff = get_role( (string) $staff_role ); if ( ! $staff || ! isset( self::staff_roles()[ (string) $staff_role ] ) ) { return new WP_Error( 'aieo_roles_staff', __( 'That role cannot be given roles to manage.', 'ai-eshop-optimizer' ) ); } if ( $granted ) { if ( ! AIEO_Safe_Roles::is_safe( (string) $role ) || ! self::admin_may( $role ) ) { return new WP_Error( 'aieo_roles_unsafe', __( 'Only a customer role, one that gives no power over the shop, can be given to staff to manage.', 'ai-eshop-optimizer' ) ); } $staff->add_cap( self::cap( $role ) ); } else { $staff->remove_cap( self::cap( $role ) ); } return true; } public static function handed_out() { $s = class_exists( 'AIEO_DMM_Settings' ) ? (array) AIEO_DMM_Settings::get_all() : (array) get_option( 'aieo_dmm_settings', array() ); $roles = array_merge( array( 'customer' ), (array) ( $s['enabled_roles'] ?? array() ), array_keys( (array) ( $s['gf_role_map'] ?? array() ) ) ); $roles[] = '' !== (string) ( $s['loyalty_role'] ?? '' ) ? (string) $s['loyalty_role'] : (string) get_option( 's1erp_prod_woo_loyalty_role', '' ); $roles[] = (string) ( $s['pro_signup_role_professional'] ?? '' ); $roles[] = (string) ( $s['pro_signup_role_student'] ?? '' ); return array_values( array_diff( array_unique( array_filter( array_map( 'strval', $roles ) ) ), AIEO_Safe_Roles::manual_only() ) ); } public static function notes() { $s = class_exists( 'AIEO_DMM_Settings' ) ? (array) AIEO_DMM_Settings::get_all() : (array) get_option( 'aieo_dmm_settings', array() ); $out = array(); $add = static function ( $slug, $note ) use ( &$out ) { if ( '' !== (string) $slug ) { $out[ (string) $slug ][] = $note; } }; foreach ( (array) ( $s['enabled_roles'] ?? array() ) as $slug ) { $add( $slug, __( 'Role Pricing prices it', 'ai-eshop-optimizer' ) ); } $add( '' !== (string) ( $s['loyalty_role'] ?? '' ) ? (string) $s['loyalty_role'] : (string) get_option( 's1erp_prod_woo_loyalty_role', '' ), __( 'The Bonus card’s Join gives it', 'ai-eshop-optimizer' ) ); if ( class_exists( 'AIEO_Pro_Signup' ) ) { $add( AIEO_Pro_Signup::role( 'professional' ), __( 'An approved professional gets it', 'ai-eshop-optimizer' ) ); $add( AIEO_Pro_Signup::role( 'student' ), __( 'An approved student gets it', 'ai-eshop-optimizer' ) ); } foreach ( array_keys( (array) ( $s['gf_role_map'] ?? array() ) ) as $slug ) { $add( $slug, __( 'The Gravity Forms registration gives it', 'ai-eshop-optimizer' ) ); } foreach ( AIEO_Safe_Roles::protected_roles() as $slug ) { $add( $slug, __( 'Login security protects it', 'ai-eshop-optimizer' ) ); } if ( class_exists( 'AIEO_Catalogue_Mode' ) && AIEO_Catalogue_Mode::settings()['viewers'] ) { $add( AIEO_Catalogue_Mode::ROLE, __( 'Catalogue mode shows its holders their prices and lets them buy; given by hand only', 'ai-eshop-optimizer' ) ); } if ( class_exists( 'AIEO_Report_Roles' ) ) { foreach ( array_keys( AIEO_Report_Roles::roles() ) as $slug ) { $add( $slug, __( 'Rooster One’s report access', 'ai-eshop-optimizer' ) ); } } $filtered = apply_filters( 'rooster_role_notes', $out ); return is_array( $filtered ) ? $filtered : $out; } public static function counts() { return array_map( 'intval', (array) ( count_users()['avail_roles'] ?? array() ) ); } public static function why_keep( $slug ) { $slug = (string) $slug; $reasons = array(); if ( ! AIEO_Safe_Roles::is_safe( $slug ) ) { $reasons[] = __( 'It is a staff role; only customer roles are deleted here.', 'ai-eshop-optimizer' ); } if ( in_array( $slug, array( 'customer', 'subscriber', (string) get_option( 'default_role', '' ) ), true ) ) { $reasons[] = __( 'WordPress or WooCommerce needs it.', 'ai-eshop-optimizer' ); } $count = (int) ( self::counts()[ $slug ] ?? 0 ); if ( $count > 0 ) { $reasons[] = sprintf( _n( '%s account holds it.', '%s accounts hold it.', $count, 'ai-eshop-optimizer' ), number_format_i18n( $count ) ); } return array_merge( $reasons, (array) ( self::notes()[ $slug ] ?? array() ) ); } public static function create_role( $name, $slug = '' ) { if ( ! self::is_role_admin() ) { return new WP_Error( 'aieo_roles_denied', __( 'Only an administrator makes roles.', 'ai-eshop-optimizer' ) ); } $name = trim( sanitize_text_field( (string) $name ) ); if ( '' === $name || mb_strlen( $name ) > 60 ) { return new WP_Error( 'aieo_roles_name', __( 'Give the role a name of up to 60 characters.', 'ai-eshop-optimizer' ) ); } if ( '' === trim( (string) $slug ) ) { $latin = remove_accents( $name ); $slug = preg_match( '/[^\x20-\x7E]/', $latin ) ? '' : str_replace( '-', '_', sanitize_title( $latin ) ); } $slug = sanitize_key( (string) $slug ); if ( '' === $slug || strlen( $slug ) > 40 ) { return new WP_Error( 'aieo_roles_slug', __( 'Give the role a slug of up to 40 Latin letters, digits and underscores.', 'ai-eshop-optimizer' ) ); } if ( null !== get_role( $slug ) ) { return new WP_Error( 'aieo_roles_exists', __( 'This site has a role with that slug already.', 'ai-eshop-optimizer' ) ); } add_role( $slug, $name, array( 'read' => true ) ); return $slug; } public static function rename_role( $slug, $name ) { if ( ! self::is_role_admin() ) { return new WP_Error( 'aieo_roles_denied', __( 'Only an administrator renames roles.', 'ai-eshop-optimizer' ) ); } $roles = wp_roles(); $slug = (string) $slug; if ( ! isset( $roles->roles[ $slug ] ) ) { return new WP_Error( 'aieo_roles_missing', __( 'There is no such account or role.', 'ai-eshop-optimizer' ) ); } if ( ! AIEO_Safe_Roles::is_safe( $slug ) ) { return new WP_Error( 'aieo_roles_unsafe', __( 'Only a customer role is renamed here.', 'ai-eshop-optimizer' ) ); } $name = trim( sanitize_text_field( (string) $name ) ); if ( '' === $name || mb_strlen( $name ) > 60 ) { return new WP_Error( 'aieo_roles_name', __( 'Give the role a name of up to 60 characters.', 'ai-eshop-optimizer' ) ); } $roles->roles[ $slug ]['name'] = $name; $roles->role_names[ $slug ] = $name; if ( $roles->use_db ) { update_option( $roles->role_key, $roles->roles ); } return true; } public static function delete_role( $slug ) { if ( ! self::is_role_admin() ) { return new WP_Error( 'aieo_roles_denied', __( 'Only an administrator deletes roles.', 'ai-eshop-optimizer' ) ); } $slug = (string) $slug; if ( null === get_role( $slug ) ) { return new WP_Error( 'aieo_roles_missing', __( 'There is no such account or role.', 'ai-eshop-optimizer' ) ); } $why = self::why_keep( $slug ); if ( array() !== $why ) { return new WP_Error( 'aieo_roles_kept', implode( ' ', $why ) ); } foreach ( wp_roles()->role_objects as $obj ) { if ( $obj->has_cap( self::cap( $slug ) ) ) { $obj->remove_cap( self::cap( $slug ) ); } } remove_role( $slug ); return true; } public static function seed_once() { if ( (int) get_option( self::SEEDED_OPTION, 0 ) >= self::SEED_VERSION ) { return false; } $manager = get_role( 'shop_manager' ); if ( ! $manager ) { return false; } foreach ( self::handed_out() as $role ) { if ( AIEO_Safe_Roles::is_safe( $role ) ) { $manager->add_cap( self::cap( $role ) ); } } update_option( self::SEEDED_OPTION, self::SEED_VERSION, false ); return true; } }