true, self::CAP => true ) ); } $admin = get_role( 'administrator' ); if ( $admin && ! $admin->has_cap( self::CAP ) ) { $admin->add_cap( self::CAP ); } } /** The memo of who answers follows every change to users and roles made anywhere in WordPress. */ public static function init() { foreach ( array( 'set_user_role', 'add_user_role', 'remove_user_role', 'deleted_user', 'user_register', 'profile_update' ) as $h ) { add_action( $h, array( __CLASS__, 'forget' ) ); } } /** Give the capability to the chosen users and take it from the ones no longer chosen (admins untouched). */ public static function sync_operators( array $ids, array $before = array() ) { $ids = array_values( array_unique( array_map( 'intval', $ids ) ) ); $before = array_values( array_unique( array_map( 'intval', $before ) ) ); $had = array(); foreach ( get_users( array( 'capability' => self::CAP, 'fields' => array( 'ID' ) ) ) as $u ) { $had[] = (int) $u->ID; } foreach ( array_unique( array_merge( $ids, $before, $had ) ) as $uid ) { $user = get_user_by( 'id', $uid ); if ( ! $user || in_array( 'administrator', (array) $user->roles, true ) ) { continue; } if ( in_array( $uid, $ids, true ) ) { if ( ! $user->has_cap( self::CAP ) ) { $user->add_cap( self::CAP ); } } elseif ( $user->has_cap( self::CAP ) && ! in_array( self::ROLE, (array) $user->roles, true ) ) { $user->remove_cap( self::CAP ); } } self::forget(); } /** The memo of who answers is dropped: after a settings save, a role change, a new administrator. */ public static function forget() { self::$operators = null; delete_transient( 'chatdozo_operators' ); } /** * Users who can answer: admins plus anyone with the capability. The two user queries behind this run on * every admin page (the menu badge, the admin bar), so the ids are kept for five minutes. */ public static function operators() { if ( self::$operators !== null ) { return self::$operators; } $ids = get_transient( 'chatdozo_operators' ); if ( ! is_array( $ids ) ) { $ids = array(); $lists = array( get_users( array( 'role' => 'administrator', 'orderby' => 'display_name', 'fields' => array( 'ID' ) ) ), get_users( array( 'capability' => self::CAP, 'orderby' => 'display_name', 'fields' => array( 'ID' ) ) ), ); foreach ( $lists as $users ) { foreach ( $users as $u ) { $ids[] = (int) $u->ID; } } $ids = array_values( array_unique( $ids ) ); set_transient( 'chatdozo_operators', $ids, 5 * MINUTE_IN_SECONDS ); } $out = array(); if ( $ids ) { foreach ( get_users( array( 'include' => $ids, 'orderby' => 'display_name', 'fields' => 'all' ) ) as $u ) { if ( user_can( $u, self::CAP ) ) { $out[] = $u; } } } return self::$operators = $out; } /** A person's role, as WordPress names it ("Administrator", "Shop manager"); blank if none. */ public static function label( $u ) { $roles = (array) $u->roles; if ( ! $roles ) { return ''; } $names = wp_roles()->get_names(); $slug = $roles[0]; return isset( $names[ $slug ] ) ? translate_user_role( $names[ $slug ] ) : $slug; } /** The people the settings page shows: administrators and the ticked operators; nobody else. */ public static function answering( array $o ) { $ids = array(); foreach ( get_users( array( 'role' => 'administrator', 'fields' => array( 'ID' ) ) ) as $u ) { $ids[] = (int) $u->ID; } foreach ( (array) ( $o['operators'] ?? array() ) as $id ) { $ids[] = (int) $id; } $ids = array_values( array_unique( array_filter( $ids ) ) ); if ( ! $ids ) { return array(); } return get_users( array( 'include' => $ids, 'orderby' => 'display_name', 'fields' => 'all' ) ); } /** The roles on this site with how many people hold each, administrators first. */ public static function role_counts() { $names = wp_roles()->get_names(); $counts = count_users(); $out = array(); foreach ( $names as $slug => $label ) { $n = isset( $counts['avail_roles'][ $slug ] ) ? (int) $counts['avail_roles'][ $slug ] : 0; if ( $n > 0 ) { $out[ $slug ] = array( 'label' => translate_user_role( $label ), 'count' => $n ); } } uksort( $out, function ( $a, $b ) { if ( $a === 'administrator' ) { return -1; } if ( $b === 'administrator' ) { return 1; } return 0; } ); return $out; } /** People to pick from: one role, or a search by name, login or email; never the whole site at once. */ public static function find( $role = '', $q = '', $limit = 60 ) { $args = array( 'number' => $limit, 'orderby' => 'display_name', 'fields' => 'all' ); if ( $role !== '' ) { $args['role'] = $role; } if ( $q !== '' ) { $args['search'] = '*' . $q . '*'; $args['search_columns'] = array( 'user_login', 'user_email', 'display_name', 'user_nicename' ); } if ( $role === '' && $q === '' ) { return array(); } return get_users( $args ); } /** REST permission callback: the request object is ignored. */ public static function can_rest() { return current_user_can( self::CAP ); } /** Can this user (the current one by default) answer chats? */ public static function can( $uid = 0 ) { if ( $uid ) { return user_can( (int) $uid, self::CAP ); } return current_user_can( self::CAP ); } }