这是indexloc提供的服务,不要输入任何密码
Skip to content

Pass user_roles query parameter in proxy setup URL #1640

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

Merged
merged 7 commits into from
Jun 10, 2020

Conversation

felixarntz
Copy link
Member

Summary

Addresses issue #1639

Relevant technical choices

Checklist

  • My code is tested and passes existing unit tests.
  • My code has an appropriate set of unit tests which all pass.
  • My code is backward-compatible with WordPress 4.7 and PHP 5.6.
  • My code follows the WordPress coding standards.
  • My code has proper inline documentation.
  • I have signed the Contributor License Agreement (see https://cla.developers.google.com/).

@felixarntz felixarntz requested a review from aaemnnosttv June 5, 2020 00:35
Copy link
Collaborator

@aaemnnosttv aaemnnosttv left a comment

Choose a reason for hiding this comment

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

This looks good @felixarntz - just a few suggestions to possibly improve, but otherwise 👍

public function get_user_fields() {
$current_user = wp_get_current_user();

$user_roles = $current_user && $current_user->exists() && ! empty( $current_user->roles ) ? $current_user->roles : array();
Copy link
Collaborator

Choose a reason for hiding this comment

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

This should be safe to simplify here as wp_get_current_user always returns a WP_User instance, which has an empty $roles = array() by default.

Suggested change
$user_roles = $current_user && $current_user->exists() && ! empty( $current_user->roles ) ? $current_user->roles : array();
$user_roles = wp_get_current_user()->roles;

Then the $current_user variable would no longer be necessary 👍

if ( is_multisite() && current_user_can( 'manage_network' ) ) {
$user_roles[] = 'network_administrator';
}
$user_roles = array_values( array_unique( $user_roles ) );
Copy link
Collaborator

Choose a reason for hiding this comment

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

Using array_values here should be unnecessary as implode only uses values anyways.

Suggested change
$user_roles = array_values( array_unique( $user_roles ) );
$user_roles = array_unique( $user_roles );


// WordPress technically allows for multiple roles, and some plugins
// make use of that feature - we can just fake it like below.
wp_get_current_user()->roles[] = 'shop_vendor';
Copy link
Collaborator

Choose a reason for hiding this comment

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

Can we use add_role instead of mutating its property directly?

Suggested change
wp_get_current_user()->roles[] = 'shop_vendor';
wp_get_current_user()->add_role( 'shop_vendor' );

Copy link
Member Author

Choose a reason for hiding this comment

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

The way that WP works here is that we would also need to register shop_vendor as an available role if we want to use WP_User::add_role, otherwise it will be interpreted as a capability (WP is weird). So for testing our behavior specifically I think modifying the roles property is simpler and accurate.

@felixarntz felixarntz requested a review from aaemnnosttv June 10, 2020 18:00
Copy link
Collaborator

@aaemnnosttv aaemnnosttv left a comment

Choose a reason for hiding this comment

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

LGTM 👍

@aaemnnosttv aaemnnosttv merged commit 3590c62 into develop Jun 10, 2020
@aaemnnosttv aaemnnosttv deleted the enhance/1639-user-roles-query-param branch June 10, 2020 20:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants