*/ public static function caps(): array { return array( self::BRIEFS => __( 'See changes and briefs', 'outercite' ), self::DRAFTS => __( 'Start drafts and mark live by hand', 'outercite' ), self::RESULTS => __( 'See results', 'outercite' ), ); } /** * Role to capabilities, with the defaults filled in. * * @return array */ public static function matrix(): array { $saved = get_option( self::OPTION, null ); if ( is_array( $saved ) ) { return $saved; } $all = self::ALL; return array( 'editor' => $all, self::CLIENT => array( self::RESULTS ), ); } /** * Give a user Outercite capabilities from their roles. * * @param array $allcaps The user's capabilities. * @param string[] $caps Primitive capabilities being checked. * @param array $args The original check. * @param \WP_User $user The user. * @return array */ public static function grant( $allcaps, $caps, $args, $user ) { $ours = array_intersect( (array) $caps, self::ALL ); if ( ! $ours ) { return $allcaps; } if ( ! empty( $allcaps['manage_options'] ) ) { foreach ( $ours as $cap ) { $allcaps[ $cap ] = true; } return $allcaps; } $matrix = self::matrix(); foreach ( (array) $user->roles as $role ) { foreach ( $ours as $cap ) { if ( in_array( $cap, (array) ( $matrix[ $role ] ?? array() ), true ) ) { $allcaps[ $cap ] = true; } } } return $allcaps; } /** * Add the Client viewer role once per site (roles are per site on a network). */ public static function ensure_client_role(): void { if ( '1' === get_option( self::VERSION ) && get_role( self::CLIENT ) ) { return; } if ( ! get_role( self::CLIENT ) ) { add_role( self::CLIENT, __( 'Client viewer (Outercite)', 'outercite' ), array( 'read' => true ) ); } update_option( self::VERSION, '1', false ); } /** * Save the roles table from the settings screen. */ public static function handle_save(): void { check_admin_referer( 'outercite_save_roles' ); if ( ! current_user_can( self::CONNECT ) ) { wp_die( esc_html__( 'You are not allowed to do that.', 'outercite' ) ); } $posted = isset( $_POST['roles'] ) && is_array( $_POST['roles'] ) ? wp_unslash( $_POST['roles'] ) : array(); // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized -- each key and value is checked against known roles and capabilities below. $known = array_keys( wp_roles()->get_names() ); $caps = self::ALL; $matrix = array(); foreach ( $posted as $role => $granted ) { $role = sanitize_key( (string) $role ); if ( 'administrator' === $role || ! in_array( $role, $known, true ) || ! is_array( $granted ) ) { continue; } $matrix[ $role ] = array_values( array_intersect( array_map( 'sanitize_key', array_keys( $granted ) ), $caps ) ); } update_option( self::OPTION, $matrix, false ); Activity::add( 'roles', __( 'Changed who can see briefs, start drafts and see results', 'outercite' ) ); Admin::flash( 'success', __( 'Roles saved.', 'outercite' ) ); wp_safe_redirect( Admin::settings_url( array( 'tab' => 'roles' ) ) ); exit; } /** * The roles tab on the settings screen. */ public static function render(): void { $matrix = self::matrix(); echo '

' . esc_html__( 'Who can do what', 'outercite' ) . '

'; echo '

' . esc_html__( 'Administrators can always do everything. Give clients the Client viewer role to show them results only.', 'outercite' ) . '

'; echo '
'; wp_nonce_field( 'outercite_save_roles' ); echo ''; echo ''; foreach ( self::caps() as $label ) { echo ''; } echo ''; foreach ( wp_roles()->get_names() as $role => $name ) { $admin = 'administrator' === $role; echo ''; foreach ( array_keys( self::caps() ) as $cap ) { $on = $admin || in_array( $cap, (array) ( $matrix[ $role ] ?? array() ), true ); printf( '', esc_attr( $role ), esc_attr( $cap ), checked( $on, true, false ), disabled( $admin, true, false ), esc_attr( translate_user_role( $name ) . ': ' . self::caps()[ $cap ] ) ); } echo ''; } echo '
' . esc_html__( 'Role', 'outercite' ) . '' . esc_html( $label ) . '
' . esc_html( translate_user_role( $name ) ) . '
'; // Keep roles with every box cleared in the saved setting. foreach ( array_keys( wp_roles()->get_names() ) as $role ) { echo ''; } echo '

'; } }