Commit 706f34de authored by John James Jacoby's avatar John James Jacoby
Browse files

First pass at "Created" template part.

Add filters and functions to support third party extensions.
parent efeb37cc
Pipeline #1101 skipped
......@@ -51,8 +51,7 @@ function wp_join_page_maybe_do_request() {
if ( 'cancel' === $_REQUEST['access'] ) {
wp_join_page_clear_user_capabilities( get_current_user_id() );
} elseif ( 'request' === $_REQUEST['access'] ) {
wp_join_page_clear_user_capabilities( get_current_user_id() );
wp_get_current_user()->set_role( 'pending' );
wp_join_page_set_user_role( get_current_user_id(), wp_join_page_get_pending_role() );
}
// Redirect back
......
......@@ -20,7 +20,7 @@ defined( 'ABSPATH' ) || exit;
*/
function wp_join_page_get_pending_users() {
return get_users( array(
'role' => 'pending'
'role' => wp_join_page_get_pending_role()
) );
}
......@@ -76,16 +76,17 @@ function wp_join_page_filter_user_views( $views = array() ) {
// Get users
$users = wp_join_page_get_pending_users();
$role = wp_join_page_get_pending_role();
// Bail if no users
if ( ! empty( $users ) ) {
$current = ( ! empty( $_REQUEST['role'] ) && ( 'pending' === $_REQUEST['role'] ) ) ? 'current' : '';
$current = ( ! empty( $_REQUEST['role'] ) && ( $role === $_REQUEST['role'] ) ) ? 'current' : '';
$text = esc_html__( 'Pending', 'wp-join-page' );
$url = add_query_arg( array( 'role' => 'pending' ), admin_url( 'users.php' ) );
$url = add_query_arg( array( 'role' => $role ), admin_url( 'users.php' ) );
$count = '<span class="count">(' . number_format_i18n( count( $users ) ) . ')</span>';
$link = '<a href="' . esc_url( $url ) . '" class="' . esc_attr( $current ) . '">' . $text . ' ' . $count . '</a>';
$views['pending'] = $link;
$views[ $role ] = $link;
}
return $views;
......@@ -108,9 +109,8 @@ function wp_join_page_user_row_actions( $actions = array(), $user_object = array
// Get the URL
$url = add_query_arg( array(
'action' => 'approve_user',
'users' => array( $user_object->ID ),
//'new_role' => get_option( 'default_role' )
'action' => 'approve_user',
'users' => array( $user_object->ID ),
), admin_url( 'users.php' ) );
$url = wp_nonce_url( $url, 'bulk-users' );
......@@ -148,22 +148,19 @@ function wp_join_page_admin_maybe_approve_user() {
foreach ( (array) $_REQUEST['users'] as $user_id ) {
$user_id = (int) $user_id;
// Skip if current user cannot promote this user
if ( ! current_user_can( 'promote_user', $user_id ) ) {
continue;
}
// Clear all roles & caps
wp_join_page_clear_user_capabilities( $user_id );
// Make default role
$user = new WP_User( $user_id );
$user->add_role( get_option( 'default_role' ) );
// Approve user
wp_join_page_approve_request( $user_id );
}
// Where to redirect
$redirect_to = add_query_arg( array(
'update' => 'approved',
'role' => 'pending'
'role' => wp_join_page_get_pending_role()
), admin_url( 'users.php' ) );
// Redirect
......@@ -181,10 +178,11 @@ function wp_join_page_admin_maybe_approve_user() {
* @return array
*/
function wp_join_page_hide_pending_users( $args = array() ) {
$role = wp_join_page_get_pending_role();
// Do not show Pending roles
if ( empty( $_REQUEST['role'] ) || ( 'pending' !== $_REQUEST['role'] ) ) {
$args['role__not_in'] = 'pending';
if ( empty( $_REQUEST['role'] ) || ( $role !== $_REQUEST['role'] ) ) {
$args['role__not_in'] = $role;
}
return $args;
......
......@@ -33,8 +33,8 @@ function wp_join_page_get_string( $key = '' ) {
'success_create' => esc_html__( 'Your account was successfully created, and we\'ve notified the site owner that you wish to join.', 'wp-join-page' ),
// Templates
'already_member' => esc_html__( 'You are already a member of this site. If this isn\'t you, click "This isn\'t me" to sign out.', 'wp-join-page' ),
'already_logged_in' => esc_html__( 'You are already logged in. If this isn\'t you, click "This isn\'t me" to sign out.', 'wp-join-page' ),
'already_member' => esc_html__( 'If this isn\'t you, click "This isn\'t me" to sign out.', 'wp-join-page' ),
'already_logged_in' => esc_html__( 'If this isn\'t you, click "This isn\'t me" to sign out.', 'wp-join-page' ),
'go_to_dashboard' => esc_html__( 'Go to Dashboard', 'wp-join-page' ),
'or' => esc_html__( 'Or', 'wp-join-page' ),
'this_is_not_me' => esc_html__( 'This is not me', 'wp-join-page' ),
......
......@@ -18,6 +18,28 @@ function wp_join_page() {
wp_join_page_get_template_part( 'wrapper' );
}
/**
* Return the pending role
*
* @since 0.1.0
*
* @return string
*/
function wp_join_page_get_pending_role() {
return apply_filters( 'wp_join_page_get_pending_role', 'pending' );
}
/**
* Return the default role
*
* @since 0.1.0
*
* @return string
*/
function wp_join_page_get_default_role() {
return apply_filters( 'wp_join_page_get_default_role', get_option( 'default_role' ) );
}
/**
* Output a URL to a specific calendar date
*
......@@ -54,13 +76,14 @@ function wp_join_page_is_user_pending( $user_id = 0 ) {
$user_id = get_current_user_id();
}
// Super admins are not pending
if ( is_super_admin( $user_id ) ) {
return false;
}
// Get default role
$user = new WP_User( $user_id );
$role = wp_join_page_get_pending_role();
// User is pending
if ( user_can( $user_id, 'pending' ) ) {
if ( in_array( $role, $user->roles, true ) ) {
return true;
} elseif ( in_array( $role, array_keys( $user->caps ), true ) ) {
return true;
}
......@@ -98,6 +121,7 @@ function wp_get_join_page_url() {
function wp_join_page_clear_user_capabilities( $user_id = 0 ) {
delete_user_option( $user_id, 'capabilities' );
delete_user_option( $user_id, 'user_level' );
clean_user_cache( $user_id );
}
/**
......@@ -111,10 +135,13 @@ function wp_join_page_clear_user_capabilities( $user_id = 0 ) {
*/
function wp_join_page_get_login_url( $args = array() ) {
// Redirect to
$redirect_to = apply_filters( 'wp_join_page_get_login_redirect_to', wp_get_join_page_url() );
// Parse args
$r = wp_parse_args( array(
$r = wp_parse_args( $args, array(
'args' => array(),
'url' => user_trailingslashit( home_url( 'join' ) )
'url' => $redirect_to
) );
// Create the URL
......@@ -138,7 +165,7 @@ function wp_join_page_get_login_url( $args = array() ) {
* @since 0.1.0
*/
function wp_join_page_get_dashboard_url() {
return apply_filters( 'wp_join_page_get_dashboard_link', admin_url() );
return apply_filters( 'wp_join_page_get_dashboard_url', get_dashboard_url() );
}
/**
......@@ -206,7 +233,7 @@ function wp_join_page_create_account() {
if ( is_multisite() && apply_filters( 'wp_join_page_requires_activation', true ) ) {
wpmu_signup_user( $username, $email, array(
'add_to_blog' => get_current_blog_id(),
'new_role' => get_option( 'default_role' ),
'new_role' => wp_join_page_get_pending_role(),
'first_name' => $firstname,
'last_name' => $lastname,
) );
......@@ -248,7 +275,7 @@ function wp_join_page_create_account() {
wp_join_page_add_request( $user_id, $firstname, $lastname );
// Notify the site admin
wp_join_page_notify_site_admin( $email, $firstname, $lastname );
wp_join_page_notify_site_admins( $email, $firstname, $lastname );
// Redirect
$args = array( 'success' => 'yay' );
......@@ -267,7 +294,7 @@ function wp_join_page_create_account() {
* @param string $first_name
* @param string $last_name
*/
function wp_join_page_notify_site_admin( $user_email = '', $first_name = '', $last_name = '' ) {
function wp_join_page_notify_site_admins( $user_email = '', $first_name = '', $last_name = '' ) {
// Email subject
$subject = sprintf( wp_join_page_get_string( 'email_notify_admin_subject' ), wp_specialchars_decode( get_option( 'blogname' ) ) );
......@@ -275,13 +302,19 @@ function wp_join_page_notify_site_admin( $user_email = '', $first_name = '', $la
/* translators: 1: First Name, 2: Last Name, 3. Email address, 4: Site name, 3: activation URL */
$body = wp_join_page_get_string( 'email_notify_admin_body' );
// Get dashboard URL
$role = wp_join_page_get_pending_role();
$dashboard_url = add_query_arg( array(
'role' => $role
), admin_url( 'users.php' ) );
// Email message
$message = sprintf( $body,
$first_name,
$last_name,
$user_email,
home_url(),
add_query_arg( array( 'role' => 'pending' ), admin_url( 'users.php' ) )
$dashboard_url
);
// Get users to notify
......@@ -291,7 +324,7 @@ function wp_join_page_notify_site_admin( $user_email = '', $first_name = '', $la
) );
// Action before mails go out
do_action( 'wp_join_page_notify_site_admin', $users, $subject, $message );
do_action( 'wp_join_page_notify_site_admins', $users, $subject, $message );
// Send emails
foreach ( $users as $user ) {
......@@ -309,8 +342,13 @@ function wp_join_page_notify_site_admin( $user_email = '', $first_name = '', $la
* @param string $last_name
*/
function wp_join_page_add_request( $user_id = 0 ) {
wp_join_page_set_user_role( $user_id, wp_join_page_get_pending_role() );
}
function wp_join_page_set_user_role( $user_id = 0, $role = '' ) {
wp_join_page_clear_user_capabilities( $user_id );
$user = new WP_User( $user_id );
$user->add_role( 'pending' );
$user->add_role( $role );
}
/**
......@@ -322,7 +360,7 @@ function wp_join_page_add_request( $user_id = 0 ) {
* @return string
*/
function wp_join_page_default_child_role( $role = '' ) {
$role = 'pending';
$role = wp_join_page_get_pending_role();
return $role;
}
......@@ -335,16 +373,26 @@ function wp_join_page_default_child_role( $role = '' ) {
*/
function wp_join_page_approve_request( $user_id = 0 ) {
// Set role to default
// Get user, to get email address
$user = new WP_User( $user_id );
wp_join_page_clear_user_capabilities( $user->ID );
$user->add_role( get_option( 'default_role' ) );
// Bail if no user
if ( empty( $user ) ) {
return;
}
// Set the user role
wp_join_page_set_user_role( $user_id, wp_join_page_get_default_role() );
// Options
$name = get_option( 'blogname' );
$home = home_url();
// Email contents
$subject = sprintf( wp_join_page_get_string( 'email_notify_user_approved_subject' ), wp_specialchars_decode( get_option( 'blogname' ) ) );
$subject = sprintf( wp_join_page_get_string( 'email_notify_user_approved_subject' ), wp_specialchars_decode( $name ) );
/* translators: 1: First Name, 2: Last Name, 3. Email address, 4: Site name, 3: activation URL */
$message = sprintf( wp_join_page_get_string( 'email_notify_user_approved_body' ), get_option( 'blogname' ), home_url() );
$message = sprintf( wp_join_page_get_string( 'email_notify_user_approved_body' ), $name, $home );
// Send email
wp_mail( $user->user_email, $subject, $message );
......@@ -361,6 +409,12 @@ function wp_join_page_deny_request( $user_id = 0 ) {
// Remove pending role
$user = new WP_User( $user_id );
// Bail if no user
if ( empty( $user ) ) {
return;
}
wp_join_page_clear_user_capabilities( $user->ID );
// Email contents
......
<div class="ui blue message">
<i class="dashboard icon"></i><?php echo wp_join_page_get_string( 'success_create' ); ?>
</div>
<div class="ui basic segment">
<div class="ui two column middle aligned very relaxed stackable grid">
<div class="column"><?php
do_action( 'wp_join_page_created_left_column' );
?></div>
<div class="column">
<a href="<?php echo esc_url( wp_join_page_get_login_url() ); ?>" class="ui right floated blue button"><?php esc_html_e( 'Dashboard', 'wp-join-page' ); ?></a>
</div>
</div>
</div>
......@@ -15,15 +15,9 @@ if ( ! empty( $_REQUEST['error'] ) ) :
elseif ( 'unknown' === $_REQUEST['error'] ) :
?><div class="ui red message"><i class="warning circle icon"></i><?php echo wp_join_page_get_string( 'err_unknown' ); ?></div><?php
endif;
endif;
// Errors have occurred, so alert the user
if ( ! empty( $_REQUEST['success'] ) && ( 'yay' === $_REQUEST['success'] ) ) :
?><div class="ui green message"><i class="rocket icon"></i><?php echo wp_join_page_get_string( 'success_create' ); ?></div><?php
endif;
// Only show message if no success or errors
if ( empty( $_REQUEST['success'] ) && empty( $_REQUEST['error'] ) ) :
else :
?><div class="ui blue message"><i class="dashboard icon"></i><?php echo wp_join_page_get_string( 'create_account' ); ?></div><?php
endif;
......
......@@ -4,19 +4,23 @@
if ( is_user_logged_in() ) {
// Not already a member of this site
if ( ! is_user_member_of_blog() ) {
wp_join_page_get_template_part( 'forms/request-access' );
if ( ! wp_join_page_is_user_pending() ) {
if ( ! is_user_member_of_blog() ) {
wp_join_page_get_template_part( 'forms/request-access' );
} else {
wp_join_page_get_template_part( 'forms/already-member' );
}
// Logged in, maybe a member, maybe already pending
} elseif ( is_user_member_of_blog() || wp_join_page_is_user_pending() ) {
} elseif ( wp_join_page_is_user_pending() ) {
wp_join_page_get_template_part( 'forms/already-member' );
// Some kind of other setup (super admin, etc...)
} else {
wp_join_page_get_template_part( 'forms/cannot-join' );
}
// Signing up
} else {
wp_join_page_get_template_part( 'forms/sign-up' );
if ( isset( $_GET['success'] ) && ( 'yay' === $_GET['success'] ) ) {
wp_join_page_get_template_part( 'forms/created' );
} else {
wp_join_page_get_template_part( 'forms/sign-up' );
}
}
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