roles ); // Until an admin picks roles, CRM access granted to a single user counts too. if ( ! $staff && ! self::configured() ) { $staff = self::has_crm_caps( (array) $user->allcaps ); } } /** * Filter whether the current user counts as back-office staff. * * @param bool $staff Staff. */ return (bool) apply_filters( 'goconsent_is_staff', $staff ); } /** * Staff roles in effect: the admin's choice, or auto-detected. * * @return string[] */ public static function roles() { $roles = self::configured() ? (array) goconsent()->settings->get( 'staff_roles' ) : self::detected_roles(); /** * Roles treated as back-office staff (in addition to anyone who can edit posts). * * @param string[] $roles Role slugs. */ return array_values( array_unique( array_map( 'strval', (array) apply_filters( 'goconsent_staff_roles', $roles ) ) ) ); } /** * Roles on this site that look like back-office or CRM roles. * * @return string[] */ public static function detected_roles() { $out = array(); foreach ( self::site_roles() as $slug => $role ) { $caps = isset( $role['capabilities'] ) ? (array) $role['capabilities'] : array(); if ( ! empty( $caps['edit_posts'] ) || in_array( $slug, self::KNOWN_ROLES, true ) || false !== stripos( $slug, 'crm' ) || self::has_crm_caps( $caps ) ) { $out[] = (string) $slug; } } return $out; } /** * Roles that can edit content (always staff). * * @return string[] */ public static function editor_roles() { $out = array(); foreach ( self::site_roles() as $slug => $role ) { if ( ! empty( $role['capabilities']['edit_posts'] ) ) { $out[] = (string) $slug; } } return $out; } /** * All roles on the site: slug => [ name, capabilities ]. * * @return array */ public static function site_roles() { $wp_roles = function_exists( 'wp_roles' ) ? wp_roles() : null; return ( $wp_roles && is_array( $wp_roles->roles ) ) ? $wp_roles->roles : array(); } /** * Whether a capability set grants CRM access. * * @param array $caps cap => granted. * @return bool */ private static function has_crm_caps( array $caps ) { foreach ( $caps as $cap => $granted ) { if ( $granted && ( false !== stripos( (string) $cap, 'crm' ) || in_array( $cap, self::KNOWN_CAPS, true ) ) ) { return true; } } return false; } /** * Whether an admin has chosen the staff roles. * * @return bool */ private static function configured() { return (bool) goconsent()->settings->get( 'staff_roles_set' ); } /** * Staff previewing the banner from GoConsent's "Open site" button. * * @return bool */ private static function previewing() { return isset( $_GET[ self::PREVIEW ] ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- presence check only; it only turns the banner on for the viewer. } }