Skip to content

Commit ee6bc99

Browse files
committed
🏗️ Expand options for filtering roles
1 parent 17036c0 commit ee6bc99

File tree

5 files changed

+53
-21
lines changed

5 files changed

+53
-21
lines changed

inc/class-wp-rainbow-login-functionality.php

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -80,18 +80,21 @@ public function filter_nonce_life_filtered(): int {
8080
/**
8181
* Provide filter for address roles. Defaults to subscriber.
8282
*
83-
* @param string $address Address for user.
83+
* @param string $address Address for user.
84+
* @param string $filtered_infura_id Filtered Infura ID.
85+
* @param WP_User|false $user User object, if available.
8486
*
85-
* @return mixed|void Filtered role for a given address.
87+
* @return string Filtered role for a given address.
8688
*/
87-
public function get_role_for_address_filtered( string $address ) {
89+
public function get_role_for_address_filtered( string $address, string $filtered_infura_id, $user ): string {
8890
/**
8991
* Filter the default role for WP Rainbow users.
9092
*
91-
* @param string $default Default role for new users.
92-
* @param string $address Address of user being added.
93+
* @param string $default Default role for new users.
94+
* @param string $address Address of user being added.
95+
* @param WP_User|false $user User object, if available.
9396
*/
94-
return apply_filters( 'wp_rainbow_role_for_address', 'subscriber', $address );
97+
return apply_filters( 'wp_rainbow_role_for_address', 'subscriber', $address, $filtered_infura_id, $user );
9598
}
9699

97100
// API ROUTES.
@@ -229,10 +232,12 @@ public function login_callback( WP_REST_Request $request ): WP_REST_Response {
229232
]
230233
);
231234

232-
if ( ! empty( $wp_rainbow_options['wp_rainbow_field_required_token'] ) && ! empty( $wp_rainbow_options['wp_rainbow_field_infura_id'] ) ) {
235+
$filtered_infura_id = WP_Rainbow::instance()->get_infura_id_filtered();
236+
237+
if ( ! empty( $wp_rainbow_options['wp_rainbow_field_required_token'] ) && ! empty( $filtered_infura_id ) ) {
233238
// @TODO Figure out if ABI should be an option (or formatted differently).
234239
$example_abi = '[{"constant":true,"inputs":[{"internalType":"address","name":"owner","type":"address"}],"name":"balanceOf","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"}]';
235-
$contract = new Contract( 'https://mainnet.infura.io/v3/' . $wp_rainbow_options['wp_rainbow_field_infura_id'], $example_abi );
240+
$contract = new Contract( 'https://mainnet.infura.io/v3/' . $filtered_infura_id, $example_abi );
236241
$contract->at( $wp_rainbow_options['wp_rainbow_field_required_token'] )->call(
237242
'balanceOf',
238243
$address,
@@ -247,6 +252,8 @@ function ( $err, $balance ) use ( $wp_rainbow_options ) {
247252
// Lookup or generate user and then sign them in.
248253
$user = get_user_by( 'login', $address );
249254
$sanitized_display_name = sanitize_text_field( $display_name );
255+
$role = $this->get_role_for_address_filtered( $address, $filtered_infura_id, $user );
256+
250257
if ( ! $user ) {
251258
// If there's not a user already, double check registration settings.
252259
$users_can_register = get_option( 'users_can_register', false );
@@ -262,7 +269,7 @@ function ( $err, $balance ) use ( $wp_rainbow_options ) {
262269
wp_update_user(
263270
[
264271
'ID' => $user->ID,
265-
'role' => $this->get_role_for_address_filtered( $address ),
272+
'role' => $role,
266273
'display_name' => $sanitized_display_name,
267274
]
268275
);
@@ -278,20 +285,22 @@ function ( $err, $balance ) use ( $wp_rainbow_options ) {
278285
*/
279286
do_action( 'wp_rainbow_user_created', $user->ID, $address, $sanitized_display_name );
280287

281-
} elseif ( $user->display_name !== $sanitized_display_name ) {
288+
} else {
282289
wp_update_user(
283290
[
284291
'ID' => $user->ID,
285292
'display_name' => $sanitized_display_name,
286293
]
287294
);
288295

296+
$user->set_role( $role );
297+
289298
/**
290-
* Fires when a WP Rainbow user's display name is updated.
299+
* Fires when a WP Rainbow user's is updated on login.
291300
*
292-
* @param int $user_id ID of new user account.
293-
* @param string $address Address of new user.
294-
* @param string $sanitized_display_name Display name of new user (either ENS or address).
301+
* @param int $user_id ID of existing user account.
302+
* @param string $address Address of existing user.
303+
* @param string $sanitized_display_name Display name of existing user (either ENS or address).
295304
*/
296305
do_action( 'wp_rainbow_user_updated', $user->ID, $address, $sanitized_display_name );
297306
}

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "wp-rainbow",
3-
"version": "0.2.10",
3+
"version": "0.2.11",
44
"description": "RainbowKit Login (Web3 Integration for Sign-In With Ethereum)",
55
"author": "Davis Shaver",
66
"license": "GPL-2.0-or-later",

readme.txt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ Tags: WordPress, web3, SIWE, Ethereum, RainbowKit, Sign-In With Ethereum
44
Tested up to: 6.0
55
Requires at least: 5.9
66
Requires PHP: 7.0
7-
Stable tag: 0.2.8
7+
Stable tag: 0.2.11
88
License: GPLv2 or later
99
License URI: https://www.gnu.org/licenses/gpl-2.0.html
1010

@@ -45,6 +45,9 @@ Find reference implementations of all filters in [example plugin here](https://g
4545

4646
== Changelog ==
4747

48+
= 0.2.11 =
49+
* Enhance options for filtering roles
50+
4851
= 0.2.10 =
4952
* Add RainbowKit Cool Mode support
5053

src/connect.jsx

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -113,9 +113,29 @@ export function WPRainbowConnect( {
113113
window.location = redirectURL || REDIRECT_URL || ADMIN_URL;
114114
}
115115
} else {
116-
const error = await verifyRes.json();
117-
onError( error );
118-
setState( ( x ) => ( { ...x, error, loading: false } ) );
116+
// Handle errors as strings or as encoded JSON strings.
117+
let parsedError = null;
118+
const rawError = await verifyRes.json();
119+
try {
120+
parsedError = JSON.parse( rawError );
121+
} catch {
122+
parsedError = rawError;
123+
}
124+
if ( typeof parsedError === 'string' ) {
125+
onError( parsedError );
126+
setState( ( x ) => ( {
127+
...x,
128+
error: parsedError,
129+
loading: false,
130+
} ) );
131+
} else if ( typeof parsedError === 'object' ) {
132+
if (
133+
parsedError?.type === 'redirect' &&
134+
parsedError.redirectURL
135+
) {
136+
window.location = parsedError.redirectURL;
137+
}
138+
}
119139
}
120140
} catch ( error ) {
121141
setState( ( x ) => ( { ...x, error, loading: false } ) );

wp-rainbow.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
* Plugin Name: RainbowKit Login (Web3 Integration for Sign-In With Ethereum)
1111
* Plugin URI: https://wp-rainbow.davisshaver.com/
1212
* Description: RainbowKit Login allows WordPress users to log in with Ethereum using the Sign-In With Ethereum standard, powered by RainbowKit.
13-
* Version: 0.2.10
13+
* Version: 0.2.11
1414
* Author: Davis Shaver
1515
* Author URI: https://davisshaver.com/
1616
* License: GPL v2 or later
@@ -27,7 +27,7 @@
2727
*
2828
* @var string
2929
*/
30-
define( 'WP_RAINBOW_ASSETS_VERSION', '0.2.8' );
30+
define( 'WP_RAINBOW_ASSETS_VERSION', '0.2.11' );
3131

3232
// Include the autoloader.
3333
add_action(

0 commit comments

Comments
 (0)