From ef2c0d45f34fd4999db9390cedc0ae21efad1a07 Mon Sep 17 00:00:00 2001 From: Lehan Date: Thu, 15 Jul 2021 16:45:14 -0400 Subject: [PATCH] temp commit for Hai's check: add support for multiple roles --- src/conf.cls.php | 30 ++++++++++++++++++++++-------- src/control.cls.php | 32 ++++++++++++++++++++++++-------- src/core.cls.php | 6 +++--- src/router.cls.php | 18 +++++++++--------- src/vary.cls.php | 19 ++++++++++++------- 5 files changed, 70 insertions(+), 35 deletions(-) diff --git a/src/conf.cls.php b/src/conf.cls.php index 4000fdd3c..06488678b 100644 --- a/src/conf.cls.php +++ b/src/conf.cls.php @@ -633,19 +633,33 @@ public function network_update( $id, $val ) { * @since 1.6 * @access public * @param string $role The user role - * @return int The set value if already set + * @return boolean */ public function in_optm_exc_roles( $role = null ) { - // Get user role - if ( $role === null ) { - $role = Router::get_role(); - } - if ( ! $role ) { return false; } + return in_array( $role, $this->conf( self::O_OPTM_EXC_ROLES ) ) ? true : false; + } - return in_array( $role, $this->conf( self::O_OPTM_EXC_ROLES ) ) ? $role : false; + /** + * Check if the user has role(s) that should be excluded from optm + * + * @since 4.2 to support single user multiple roles scenarios + * @access public + * @param array $roles The roles of the current user + * @return boolean + */ + public function has_optm_exc_role( $roles = null ) { + //Get user roles + if ( $roles == null ) { + $roles = Router::get_roles(); + } + + if ( ! $roles ) { + return false; + } + return array_intersect( $roles, $this->conf( self::O_OPTM_EXC_ROLES ) ) ? true : false; } /** @@ -720,4 +734,4 @@ public function handler() { Admin::redirect(); } -} +} \ No newline at end of file diff --git a/src/control.cls.php b/src/control.cls.php index fc404f505..6624d3f85 100644 --- a/src/control.cls.php +++ b/src/control.cls.php @@ -79,14 +79,9 @@ public function vary_add_role_exclude( $vary ) { * @since 3.0 Moved here from conf.cls * @access public * @param string $role The user role - * @return int The set value if already set + * @return boolean */ public function in_cache_exc_roles( $role = null ) { - // Get user role - if ( $role === null ) { - $role = Router::get_role(); - } - if ( ! $role ) { return false; } @@ -94,6 +89,27 @@ public function in_cache_exc_roles( $role = null ) { return in_array( $role, $this->conf( Base::O_CACHE_EXC_ROLES ) ) ? $role : false; } + /** + * Check if the user has role(s) that should be exluded from caching + * + * @since 4.2 to support single user multiple roles scenarios + * @access public + * @param array $roles The roles of the current user + * @return boolean + */ + public function has_cache_exc_role( $role = null ) { + //Get user roles + if ( $roles == null) { + $roles = Router::get_roles(); + } + + if ( ! $roles ) { + return false; + } + + return array_intersect( $roles, $this->conf( self::O_CACHE_EXC_ROLES ) ) ? true : false; + } + /** * 1. Initialize cacheable status for `wp` hook * 2. Hook error page tags for cacheable pages @@ -769,8 +785,8 @@ private function _setting_cacheable() { } // Check if is exclude roles ( Need to set Vary too ) - if ( $result = $this->in_cache_exc_roles() ) { - return $this->_no_cache_for( 'Role Excludes setting ' . $result ); + if ( $this->in_cache_exc_roles() ) { + return $this->_no_cache_for( 'Role Excludes setting.' ); } } diff --git a/src/core.cls.php b/src/core.cls.php index 6144a5ed0..534c05b9b 100644 --- a/src/core.cls.php +++ b/src/core.cls.php @@ -200,8 +200,8 @@ public function after_user_init() { */ $this->cls( 'ESI' )->init(); - if ( ! is_admin() && ! defined( 'LITESPEED_GUEST_OPTM' ) && $result = $this->cls( 'Conf' )->in_optm_exc_roles() ) { - Debug2::debug( '[Core] ⛑️ bypass_optm: hit Role Excludes setting: ' . $result ); + if ( ! is_admin() && ! defined( 'LITESPEED_GUEST_OPTM' ) && $result = $this->cls( 'Conf' )->has_optm_exc_role() ) { + Debug2::debug( '[Core] ⛑️ bypass_optm: hit Role Excludes setting.' ); ! defined( 'LITESPEED_NO_OPTM' ) && define( 'LITESPEED_NO_OPTM', true ); } @@ -602,4 +602,4 @@ public function send_headers( $is_forced = false ) { } -} +} \ No newline at end of file diff --git a/src/router.cls.php b/src/router.cls.php index c0c4e1bcd..b8116ed4b 100644 --- a/src/router.cls.php +++ b/src/router.cls.php @@ -276,8 +276,9 @@ public static function get_hash() { * Get user role * * @since 1.6.2 + * @since 4.2 update to support single user multiple roles scenarios */ - public static function get_role( $uid = null ) { + public static function get_roles( $uid = null ) { if ( defined( 'LITESPEED_WP_ROLE' ) ) { return LITESPEED_WP_ROLE; } @@ -286,18 +287,17 @@ public static function get_role( $uid = null ) { $uid = get_current_user_id(); } - $role = false; + $roles = array(); if ( $uid ) { $user = get_userdata( $uid ); if ( isset( $user->roles ) && is_array( $user->roles ) ) { - $tmp = array_values( $user->roles ); - $role = array_shift( $tmp ); + $roles = array_values( $user->roles ); } } - Debug2::debug( '[Router] get_role: ' . $role ); + Debug2::debug( '[Router] get_roles: ' . var_export( $roles, true ) ); - if ( ! $role ) { - return $role; + if ( ! $roles ) { + return $roles; // Guest user Debug2::debug( '[Router] role: guest' ); @@ -308,11 +308,11 @@ public static function get_role( $uid = null ) { * @since 2.9.8 Won't assign const if in login process */ if ( substr_compare( wp_login_url(), $GLOBALS[ 'pagenow' ], -strlen( $GLOBALS[ 'pagenow' ] ) ) === 0 ) { - return $role; + return $roles; } } - define( 'LITESPEED_WP_ROLE', $role ); + define( 'LITESPEED_WP_ROLE', $roles ); return LITESPEED_WP_ROLE; } diff --git a/src/vary.cls.php b/src/vary.cls.php index 3f2a8e3e0..5e681f2b0 100644 --- a/src/vary.cls.php +++ b/src/vary.cls.php @@ -468,16 +468,21 @@ public function finalize_default_vary( $uid = false ) { Debug2::debug( '[Vary] uid: ' . $uid ); } - // get user's group id - $role = Router::get_role( $uid ); + // get user's group ids + $roles = Router::get_roles( $uid ); - if ( $uid > 0 && $role ) { + if ( $uid > 0 && $roles ) { $vary[ 'logged-in' ] = 1; - // parse role group from settings - if ( $role_group = $this->in_vary_group( $role ) ) { - $vary[ 'role' ] = $role_group; + // parse roles groups from settings + $groups = array(); + foreach ( $roles as $role ) { + if ( $role_group = $this->in_vary_group( $role ) ) { + $groups[] = $role_group; + } } + asort( array_unique( $groups, SORT_NUMERIC ), SORT_NUMERIC ); + $vary[ 'role' ] = implode(",", $groups ); // Get admin bar set // see @_get_admin_bar_pref() @@ -724,4 +729,4 @@ private function _cookie($val = false, $expire = false, $path = false) { setcookie( self::$_vary_name, $val, $expire, $path?: COOKIEPATH, COOKIE_DOMAIN, $is_ssl, true ); } -} +} \ No newline at end of file