disable_registration = !empty( $users['disable_frontend_user_registration'] ); if( $this->disable_registration ) { add_action( 'init', [ $this, 'disable_registration_setting' ], 1 ); add_action( 'login_init', [ $this, 'block_wp_registration_form' ] ); add_filter( 'register_url', [ $this, 'remove_register_url' ] ); add_action( 'rest_api_init', [ $this, 'block_rest_registration' ], 1 ); } } /** * Disables the user registration setting if the disable_registration property is true * and the "users_can_register" option is currently enabled. * @return void */ public function disable_registration_setting(): void { if( $this->disable_registration && get_option( 'users_can_register' ) ) { update_option( 'users_can_register', 0 ); } } /** * Blocks the WordPress registration form by showing an error message * if the "action" query parameter is set to "register". * @return void */ public function block_wp_registration_form(): void { // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Reading GET parameter to display message, no form processing if( !isset( $_GET['action'] ) ) { return; } // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Reading GET parameter to display message, no form processing $action = sanitize_key( wp_unslash( $_GET['action'] ) ); if( 'register' === $action ) { wp_die( esc_html__( 'User registration is disabled on this site.', 'adminease' ), esc_html__( 'Registration Disabled', 'adminease' ), [ 'response' => 403 ] ); } } /** * Removes the registration URL by returning an empty string. * * @param string $url The original registration URL. * * @return string Always returns an empty string. */ public function remove_register_url( string $url ): string { return ''; } /** * Blocks the REST API endpoint for user registration by overriding the default behavior. * Returns an error response when a registration request is made, indicating that user registration is disabled. * @return void */ public function block_rest_registration(): void { register_rest_route( 'wp/v2', '/users', [ 'methods' => 'POST', 'callback' => function() { return new \WP_Error( 'registration_disabled', esc_html__( 'User registration is disabled.', 'adminease' ), [ 'status' => 403 ] ); }, 'permission_callback' => '__return_false', ] ); } }