Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

temp commit for Hai's check: add support for multiple roles #374

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 22 additions & 8 deletions src/conf.cls.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If dropped $role assignment in in_optm_exc_roles, will this break the existing code?

}

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 ) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why don't reuse or rename in_optm_exc_roles function?

//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;
}

/**
Expand Down Expand Up @@ -720,4 +734,4 @@ public function handler() {

Admin::redirect();
}
}
}
32 changes: 24 additions & 8 deletions src/control.cls.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,21 +79,37 @@ 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;
}

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
Expand Down Expand Up @@ -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.' );
}
}

Expand Down
6 changes: 3 additions & 3 deletions src/core.cls.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
}

Expand Down Expand Up @@ -602,4 +602,4 @@ public function send_headers( $is_forced = false ) {

}

}
}
18 changes: 9 additions & 9 deletions src/router.cls.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -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 ) );
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

debug function supports 2nd param which can be an array.


if ( ! $role ) {
return $role;
if ( ! $roles ) {
return $roles;
// Guest user
Debug2::debug( '[Router] role: guest' );

Expand All @@ -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 );
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did you test this on PHP5.3?


return LITESPEED_WP_ROLE;
}
Expand Down
19 changes: 12 additions & 7 deletions src/vary.cls.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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 );
}

}
}