Skip to content

Commit 9aa0c99

Browse files
committed
✨ Support mapping of ENS text records to user meta
1 parent 08b1f45 commit 9aa0c99

8 files changed

+159
-16
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
"scripts": {
1717
"lint": "@phpcs",
1818
"lint:fix": "@phpcbf",
19-
"phpcbf": "phpcbf .",
19+
"phpcbf": "phpcbf . --runtime-set text_domain wp-rainbow --runtime-set prefixes wp_rainbow",
2020
"phpcs": "phpcs . --runtime-set text_domain wp-rainbow --runtime-set prefixes wp_rainbow",
2121
"phpunit": "phpunit --config=phpunit.xml",
2222
"test": "@phpunit"

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

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -91,10 +91,10 @@ public function get_role_for_address_filtered( string $address, string $filtered
9191
/**
9292
* Filter the default role for WP Rainbow users.
9393
*
94-
* @param string $default Default role for new users.
95-
* @param string $address Address of user being added.
96-
* @param string $filtered_infura_id Filtered Infura ID.
97-
* @param string $filtered_infura_network Filtered Infura network.
94+
* @param string $default Default role for new users.
95+
* @param string $address Address of user being added.
96+
* @param string $filtered_infura_id Filtered Infura ID.
97+
* @param string $filtered_infura_network Filtered Infura network.
9898
* @param WP_User|false $user User object, if available.
9999
*/
100100
return apply_filters( 'wp_rainbow_role_for_address', 'subscriber', $address, $filtered_infura_id, $filtered_infura_network, $user );
@@ -193,6 +193,7 @@ public function login_callback( WP_REST_Request $request ): WP_REST_Response {
193193
$signature = $request->get_param( 'signature' );
194194
$display_name = $request->get_param( 'displayName' );
195195
$siwe_payload = $request->get_param( 'siwePayload' );
196+
$attributes = $request->get_param( 'attributes' );
196197
if ( empty( $address ) || empty( $signature ) ) {
197198
throw new Exception( __( 'Malformed authentication request.', 'wp-rainbow' ) );
198199
}
@@ -300,6 +301,30 @@ function ( $err, $balance ) use ( $wp_rainbow_options ) {
300301

301302
$user->set_role( $role );
302303

304+
$user_attributes_mapping = $wp_rainbow->get_parsed_user_attributes_mapping();
305+
foreach ( $user_attributes_mapping as $mapping ) {
306+
if ( isset( $attributes[ $mapping[0] ] ) ) {
307+
if ( in_array(
308+
$mapping[1],
309+
[
310+
'user_email',
311+
'user_url',
312+
],
313+
true
314+
) ) {
315+
wp_update_user(
316+
[
317+
'ID' => $user->ID,
318+
$mapping[1] => $attributes[ $mapping[0] ],
319+
]
320+
);
321+
} else {
322+
update_user_meta( $user->ID, $mapping[1], $attributes[ $mapping[0] ] );
323+
324+
}
325+
}
326+
}
327+
303328
/**
304329
* Fires when a WP Rainbow user's is updated on login.
305330
*

inc/class-wp-rainbow-settings.php

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,17 @@ public function action_admin_init() {
175175
'label_for' => 'wp_rainbow_field_cool_mode',
176176
]
177177
);
178+
179+
add_settings_field(
180+
'wp_rainbow_field_user_attributes_mapping',
181+
__( 'ENS Text Records to User Meta Mapping', 'wp-rainbow' ),
182+
[ self::$instance, 'wp_rainbow_field_user_attributes_mapping_callback' ],
183+
'wp_rainbow',
184+
'wp_rainbow_connection_options',
185+
[
186+
'label_for' => 'wp_rainbow_field_user_attributes_mapping',
187+
],
188+
);
178189
}
179190

180191
/**
@@ -444,6 +455,31 @@ public function wp_rainbow_infura_network_callback() {
444455
<?php
445456
}
446457

458+
/**
459+
* Print field for ENS text attributes to WP user meta option.
460+
*/
461+
public function wp_rainbow_field_user_attributes_mapping_callback() {
462+
$options = get_option( 'wp_rainbow_options', [ 'wp_rainbow_field_user_attributes_mapping' => '' ] );
463+
$mapping_field = ! empty( $options['wp_rainbow_field_user_attributes_mapping'] ) ? $options['wp_rainbow_field_user_attributes_mapping'] : '';
464+
?>
465+
<textarea
466+
id='wp_rainbow_field_user_attributes_mapping'
467+
name='wp_rainbow_options[wp_rainbow_field_user_attributes_mapping]'
468+
rows='5'
469+
type='textarea'
470+
><?php echo esc_textarea( $mapping_field ); ?></textarea>
471+
<p>
472+
<em>
473+
<small>
474+
<?php
475+
esc_html_e( 'Enter a mapping of ENS text attributes to WordPress user meta, one on each line. Example: url,user_url', 'wp-rainbow' );
476+
?>
477+
</small>
478+
</em>
479+
</p>
480+
<?php
481+
}
482+
447483
/**
448484
* Print header for connection options.
449485
*

inc/class-wp-rainbow.php

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,7 @@ public function action_init() {
164164
'SITE_TITLE' => get_bloginfo( 'name' ),
165165
'COOL_MODE' => (bool) $this->get_cool_mode_filtered(),
166166
'NETWORK' => esc_textarea( $this->get_infura_network_filtered() ),
167+
'ATTRIBUTES' => $this->get_frontend_attributes(),
167168
]
168169
);
169170
wp_localize_script(
@@ -180,10 +181,66 @@ public function action_init() {
180181
'LOGOUT_URL' => wp_logout_url(),
181182
'COOL_MODE' => (bool) $this->get_cool_mode_filtered(),
182183
'NETWORK' => esc_textarea( $this->get_infura_network_filtered() ),
184+
'ATTRIBUTES' => $this->get_frontend_attributes(),
183185
]
184186
);
185187
}
186188

189+
/**
190+
* Get a parsed version of filtered user attributes mapping.
191+
*/
192+
public function get_parsed_user_attributes_mapping() {
193+
$csv = explode( "\n", $this->get_user_attributes_mapping_filtered() );
194+
195+
return array_map(
196+
function ( $row ) {
197+
return explode( ',', $row );
198+
},
199+
$csv
200+
);
201+
}
202+
203+
/**
204+
* Get and parse use attributes for frontend.
205+
*
206+
* @return array Attributes for frontend
207+
*/
208+
public function get_frontend_attributes() {
209+
return array_reduce(
210+
$this->get_parsed_user_attributes_mapping(),
211+
function ( $agg, $item ) {
212+
if ( ! empty( $item ) && ! empty( $item[0] ) ) {
213+
$agg[] = $item[0];
214+
}
215+
216+
return $agg;
217+
},
218+
[]
219+
);
220+
}
221+
222+
/**
223+
* Provide filter for user attributes mapping.
224+
*
225+
* @return array Filtered user attributes mapping
226+
*/
227+
public function get_user_attributes_mapping_filtered() {
228+
$options = get_option(
229+
'wp_rainbow_options',
230+
[
231+
'wp_rainbow_field_user_attributes_mapping' =>
232+
'url,user_url',
233+
]
234+
);
235+
236+
/**
237+
* Filter the user attributes mapping for WP Rainbow users.
238+
*
239+
* @param array $default Default user attributes mapping.
240+
*/
241+
return apply_filters( 'wp_rainbow_user_attributes_mapping', $options['wp_rainbow_field_user_attributes_mapping'] ?? '' );
242+
}
243+
187244
// LOGIN SCRIPTS.
188245

189246
/**
@@ -219,6 +276,7 @@ public function action_login_enqueue_scripts() {
219276
'SITE_TITLE' => get_bloginfo( 'name' ),
220277
'COOL_MODE' => (bool) $this->get_cool_mode_filtered(),
221278
'NETWORK' => esc_textarea( $this->get_infura_network_filtered() ),
279+
'ATTRIBUTES' => $this->get_frontend_attributes(),
222280
]
223281
);
224282
}

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.15",
3+
"version": "0.2.16",
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.15
7+
Stable tag: 0.2.16
88
License: GPLv2 or later
99
License URI: https://www.gnu.org/licenses/gpl-2.0.html
1010

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

4848
== Changelog ==
4949

50+
= 0.2.16 =
51+
* Add support for mapping ENS text records to user fields
52+
5053
= 0.2.15 =
5154
* Fix bug with override redirect URL on login block
5255

src/connect.jsx

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,23 @@ import {
44
useAccount,
55
useDisconnect,
66
useEnsName,
7+
useProvider,
78
useNetwork,
89
useSignMessage,
910
} from 'wagmi';
1011
import stylePropType from 'react-style-proptype';
1112
import { SiweMessage } from 'siwe';
1213
import PropTypes from 'prop-types';
1314

14-
const { ADMIN_URL, LOGGED_IN, LOGIN_API, NONCE_API, REDIRECT_URL, SITE_TITLE } =
15-
wpRainbowData;
15+
const {
16+
ADMIN_URL,
17+
ATTRIBUTES,
18+
LOGGED_IN,
19+
LOGIN_API,
20+
NONCE_API,
21+
REDIRECT_URL,
22+
SITE_TITLE,
23+
} = wpRainbowData;
1624

1725
/**
1826
* WP Rainbow Connect Button.
@@ -57,6 +65,9 @@ export function WPRainbowConnect( {
5765
address,
5866
chainId: 1,
5967
} );
68+
69+
const provider = useProvider( { chainId: 1 } );
70+
6071
const { disconnectAsync } = useDisconnect();
6172

6273
const signIn = React.useCallback( async () => {
@@ -85,6 +96,20 @@ export function WPRainbowConnect( {
8596
version: '1',
8697
};
8798
const message = new SiweMessage( siwePayload );
99+
const attributes = {};
100+
if ( ensName ) {
101+
try {
102+
const ensProvider = await provider.getResolver( ensName );
103+
await ATTRIBUTES.forEach( async ( attributeKey ) => {
104+
const attributeValue = await ensProvider.getText(
105+
attributeKey
106+
);
107+
attributes[ attributeKey ] = attributeValue;
108+
} );
109+
} catch ( error ) {
110+
console.log( error );
111+
}
112+
}
88113
const signature = await signMessageAsync( {
89114
message: message.prepareMessage(),
90115
} );
@@ -99,6 +124,7 @@ export function WPRainbowConnect( {
99124
},
100125
body: JSON.stringify( {
101126
address,
127+
attributes,
102128
displayName: ensName ?? address,
103129
nonce,
104130
signature,
@@ -108,11 +134,6 @@ export function WPRainbowConnect( {
108134
if ( verifyRes.ok ) {
109135
setState( ( x ) => ( { ...x, address, loading: false } ) );
110136
onLogin();
111-
console.log( {
112-
redirectURL,
113-
REDIRECT_URL,
114-
ADMIN_URL,
115-
} );
116137
if ( redirectBoomerang ) {
117138
window.location.reload();
118139
} else {

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.15
13+
* Version: 0.2.16
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.15' );
30+
define( 'WP_RAINBOW_ASSETS_VERSION', '0.2.16' );
3131

3232
// Include the autoloader.
3333
add_action(

0 commit comments

Comments
 (0)