Commit 0ecd2795 authored by John James Jacoby's avatar John James Jacoby
Browse files

First pass at a custom `newbloguser` handler.

parent d643bb4e
......@@ -63,3 +63,56 @@ function wp_join_page_maybe_do_request() {
wp_safe_redirect( $url );
die;
}
/**
* Add a new user to a blog by visiting /newbloguser/username/.
*
* This will only work when the user's details are saved as an option
* keyed as 'new_user_x', where 'x' is the username of the user to be
* added, as when a user is invited through the regular WP Add User interface.
*
* @since 0.1.0
*/
function wp_join_page_maybe_add_existing_user_to_blog() {
// "newbloguser" is a hard-coded URL segment, for now
if ( false === strpos( $_SERVER[ 'REQUEST_URI' ], '/newbloguser/' ) ) {
return;
}
// Break apart the request
$parts = explode( '/', $_SERVER[ 'REQUEST_URI' ] );
$key = array_pop( $parts );
// Get username from URL
if ( empty( $key ) ) {
$key = array_pop( $parts );
}
// Clean-up
$option_key = "new_user_{$key}";
$details = get_option( $option_key );
if ( ! empty( $details ) ) {
delete_option( $option_key );
}
// Get blog name
$blog_name = get_option( 'blogname' );
$home_url = home_url();
// Failure
if ( empty( $details ) || is_wp_error( add_existing_user_to_blog( $details ) ) ) {
wp_die(
sprintf( __( 'An error occurred adding you to this site. Return to <a href="%s">%s</a>.'), $home_url, $home_url ),
sprintf( __( '%s &rsaquo; Error' ), $blog_name ),
array( 'response' => 500 )
);
}
// Success
wp_die(
sprintf( __( 'You have been added to this site. Now you can <a href="%s">log in</a> using your username & password.' ), wp_login_url( $home_url ) ),
sprintf( __( '%s &rsaquo; Success' ), $blog_name ),
array( 'response' => 200 )
);
}
......@@ -25,8 +25,8 @@ function wp_join_page_get_string( $key = '' ) {
$dictionary = array(
// Errors
'err_username' => esc_html__( 'This username is already in use. Do you already have an account?', 'wp-join-page' ),
'err_email' => esc_html__( 'This email address is already registered. Do you already have an account?', 'wp-join-page' ),
'err_username' => esc_html__( 'That username is already in use. Do you already have an account?', 'wp-join-page' ),
'err_email' => esc_html__( 'That email address is already registered. Do you already have an account?', 'wp-join-page' ),
'err_unknown' => esc_html__( 'An unknown error occurred. Please try again with a more unique username.', 'wp-join-page' ),
// Success
......
......@@ -15,6 +15,9 @@ remove_action( 'wpmu_activate_user', 'wpmu_welcome_user_notification', 10, 3 );
remove_action( 'after_signup_user', 'wpmu_signup_user_notification', 10, 4 );
remove_action( 'init', 'maybe_add_existing_user_to_blog' );
// Custom user invitation acceptance action
add_action( 'init', 'wp_join_page_maybe_add_existing_user_to_blog' );
// Enqueue assets
add_action( 'wp_enqueue_scripts', 'wp_join_page_enqueue_scripts' );
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment