diff --git a/10up-experience.php b/10up-experience.php index 7bd8fb2..3c80409 100644 --- a/10up-experience.php +++ b/10up-experience.php @@ -19,6 +19,7 @@ require_once __DIR__ . '/includes/admin-pages.php'; require_once __DIR__ . '/includes/plugins.php'; require_once __DIR__ . '/includes/rest-api.php'; +require_once __DIR__ . '/includes/limit-roles.php'; require_once __DIR__ . '/vendor/plugin-update-checker/plugin-update-checker.php'; diff --git a/includes/admin-pages.php b/includes/admin-pages.php index 2012118..8140b84 100644 --- a/includes/admin-pages.php +++ b/includes/admin-pages.php @@ -12,6 +12,8 @@ */ function register_admin_pages() { add_submenu_page( null, esc_html__( 'About 10up', 'tenup' ), esc_html__( 'About 10up', 'tenup' ), 'edit_posts', '10up-about', __NAMESPACE__ . '\main_screen' ); + + add_submenu_page( 'users.php', esc_html__( '10up Limit Roles', 'tenup' ), esc_html__( '10up Limit Roles', 'tenup' ), 'edit_users', '10up-limit-roles', __NAMESPACE__ . '\limit_role_screen' ); } add_action( 'admin_menu', __NAMESPACE__ . '\register_admin_pages' ); diff --git a/includes/limit-roles.php b/includes/limit-roles.php new file mode 100644 index 0000000..ec23e7e --- /dev/null +++ b/includes/limit-roles.php @@ -0,0 +1,206 @@ +%s', esc_url( admin_url( 'users.php?page=10up-limit-roles' ) ), esc_html__( 'update your whitelisted domains', 'tenup' ) ); + /* translators: %s is a placeholder for the current role trying to be assigned to a user */ + $error->add( 'invalid_email', sprintf( __( 'ERROR: Sorry, the domain "%1$s" is ineligible for the %2$s role. Please %3$s or talk to an Administrator.', 'tenup' ), esc_html( $email_domain ), esc_html( $role ), $edit_link ) ); + } +} + +add_action( 'user_profile_update_errors', __NAMESPACE__ . '\confirm_user_email_is_not_whitelisted', 10, 3 ); + +/** + * Confirm that the users email and role are whitelisted before allowing + * them to be added to a blog + * + * @param bool|WP_Error $boolean True if the user should be added to the site, false + * or error object otherwise. + * @param int $user_id User ID. + * @param string $role User role. + * @param int $blog_id Site ID. + * + * @return bool + */ +function confirm_user_email_is_not_whitelisted_add_to_blog( $boolean, $user_id, $role, $blog_id ) { + + $user = get_user_by( 'id', $user_id ); + $boolean = can_create_user( $user, $role ); + + return $boolean; +} + +add_filter( 'can_add_user_to_blog', __NAMESPACE__ . '\confirm_user_email_is_not_whitelisted_add_to_blog', 10, 4 ); + +/** + * Register limit role settings + */ +function limit_roles_settings() { + + register_setting( + 'tenup_limit_role_fields', + 'tenup_limit_roles', + __NAMESPACE__ . '\sanitize_options' + ); + + add_settings_section( + 'limit_roles', + '', + '__return_false', + '10up-limit-roles' + ); + + add_settings_field( + 'whitelisted_domains', + __( 'Whitelisted domains', 'tenup' ), + __NAMESPACE__ . '\domain_text_area', + '10up-limit-roles', + 'limit_roles' + ); + + add_settings_field( + 'roles', + __( 'Role(s)', 'tenup' ), + __NAMESPACE__ . '\roles_checkbox', + '10up-limit-roles', + 'limit_roles' + ); +} + +add_action( 'admin_init', __NAMESPACE__ . '\limit_roles_settings' ); + +/** + * Output domain text area + */ +function domain_text_area() { + $options = get_option( 'tenup_limit_roles', array() ); + $value = ! empty( $options['whitelisted-domains'] ) ? $options['whitelisted-domains'] : ''; + printf( '', esc_textarea( $value ) ); + printf( '
%s
', esc_html__( 'Enter each domain on a new line.', 'tenup' ) ); +} + +/** + * Output list of roles available on the site + */ +function roles_checkbox() { + $options = get_option( 'tenup_limit_roles', array() ); + $selected_roles = ! empty( $options['roles'] ) ? array_flip( $options['roles'] ) : array(); + echo '