Making WordPress.org


Ignore:
Timestamp:
02/16/2024 04:46:29 AM (2 years ago)
Author:
dd32
Message:

Registration: Add bulk block, and add room for a reason to be provided, query by country.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • sites/trunk/wordpress.org/public_html/wp-content/themes/pub/wporg-login/admin/ui.php

    r13201 r13216  
    8989
    9090                        var url = $this.prop('href') + '&ajax=1';
     91                        if ( url.indexOf( 'block_account' ) !== -1 ) {
     92                                if ( ! $('block_reason').val() ) {
     93                                        $('block_reason').val( prompt( 'Reason for blocking?' ) || '' );
     94                                }
     95                                url += '&block_reason=' + encodeURIComponent( $('block_reason').val() );
     96                        }
    9197
    9298                        $.get( url, function( data ) {
     
    375381        check_admin_referer( 'block_' . $email );
    376382
    377         $user = wporg_get_pending_user( $email );
    378         if ( $user ) {
    379                 $user['cleared']             = 0;
    380                 $user['user_activation_key'] = '';
    381                 $user['user_profile_key']    = '';
    382 
    383                 wporg_update_pending_user( $user );
    384         }
     383        wporg_login_block_registration( $user );
    385384
    386385        if ( isset( $_GET['ajax'] ) ) {
     
    396395} );
    397396
     397function wporg_login_block_registration( $user ) {
     398        $user = wporg_get_pending_user( $user );
     399        if ( $user ) {
     400                $user['cleared']             = 0;
     401                $user['user_activation_key'] = '';
     402                $user['user_profile_key']    = '';
     403
     404                wporg_update_pending_user( $user );
     405
     406                return true;
     407        }
     408
     409        return false;
     410}
     411
    398412add_action( 'admin_post_login_delete', function() {
    399413        if ( ! current_user_can( 'promote_users' ) ) {
     
    427441        }
    428442
    429         if ( empty( $_REQUEST['user'] ) ) {
     443        $user   = $_REQUEST['user'] ?? '';
     444        $reason = $_REQUEST['block_reason'] ?? '';
     445        if ( empty( $user ) ) {
    430446                die();
    431447        }
    432448
    433         $pending_user = wporg_get_pending_user( $_REQUEST['user'] );
    434         if ( ! $pending_user || ! $pending_user['created'] ) {
    435                 die();
    436         }
    437 
    438         $user = get_user_by( 'slug', $pending_user['user_login'] );
     449        $pending_user = wporg_get_pending_user( $user );
    439450        if ( ! $user ) {
    440451                die();
    441452        }
    442453
    443         $table = new User_Registrations_List_Table();
    444 
    445         ob_start();
    446         $pending_as_object       = (object) $pending_user;
    447         $pending_as_object->meta = (object) $pending_as_object->meta;
    448         $pending_as_object->user = $user;
    449 
    450         unset( $pending_as_object->meta->registration_ip, $pending_as_object->meta->confirmed_ip );
    451 
    452         $table->column_meta( $pending_as_object );
    453         $meta_column = ob_get_clean();
    454         $meta_column = wp_strip_all_tags( str_replace( '<br>', "\n", $meta_column ), false );
     454        $user = get_user_by( 'slug', $pending_user['user_login'] );
    455455
    456456        check_admin_referer( 'block_account_' . $user->ID );
    457457
    458         if ( $user && defined( 'WPORG_SUPPORT_FORUMS_BLOGID' ) ) {
    459 
    460                 // Switch first so that bbPress loads with the correct context.
    461                 // This also ensures that the bbp_participant code doesn't kick in.
    462                 switch_to_blog( WPORG_SUPPORT_FORUMS_BLOGID );
    463 
    464                 // Load the support forums..
    465                 include_once WP_PLUGIN_DIR . '/bbpress/bbpress.php';
    466                 include_once WP_PLUGIN_DIR . '/support-forums/support-forums.php';
    467 
    468                 // bbPress roles still aren't quite right, need to switch away and back..
    469                 // This is hacky, but otherwise the bbp_set_user_role() call below will appear to succeed, but no role alteration will actually happen.
    470                 restore_current_blog();
    471                 switch_to_blog( WPORG_SUPPORT_FORUMS_BLOGID );
    472 
    473                 add_filter( 'wporg_bbp_forum_role_changed_note_text', function( $text ) use ( $meta_column ) {
    474                         return trim( "{$meta_column}\n\n{$text}" );
    475                 } );
    476 
    477                 // Set the user to blocked. Support forum hooks will take care of the rest.
    478                 bbp_set_user_role( $user->ID, bbp_get_blocked_role() );
    479 
    480                 restore_current_blog();
     458        $result = wporg_login_block_account( $pending_user, $reason );
     459        if ( ! $result ) {
     460                die();
    481461        }
    482462
     
    493473} );
    494474
     475function wporg_login_block_account( $user, $reason = '' ) {
     476        $pending_user = wporg_get_pending_user( $user );
     477        if ( ! $pending_user || ! $pending_user['created'] ) {
     478                return false;
     479        }
     480
     481        $user = get_user_by( 'slug', $pending_user['user_login'] );
     482        if ( ! $user ) {
     483                return false;
     484        }
     485
     486        $table = new User_Registrations_List_Table();
     487
     488        ob_start();
     489        $pending_as_object       = (object) $pending_user;
     490        $pending_as_object->meta = (object) $pending_as_object->meta;
     491        $pending_as_object->user = $user;
     492
     493        unset( $pending_as_object->meta->registration_ip, $pending_as_object->meta->confirmed_ip );
     494
     495        $table->column_meta( $pending_as_object );
     496        $meta_column = ob_get_clean();
     497        $meta_column = wp_strip_all_tags( str_replace( '<br>', "\n", $meta_column ), false );
     498
     499        if ( $user && defined( 'WPORG_SUPPORT_FORUMS_BLOGID' ) ) {
     500
     501                // Switch first so that bbPress loads with the correct context.
     502                // This also ensures that the bbp_participant code doesn't kick in.
     503                switch_to_blog( WPORG_SUPPORT_FORUMS_BLOGID );
     504
     505                // Load the support forums..
     506                include_once WP_PLUGIN_DIR . '/bbpress/bbpress.php';
     507                include_once WP_PLUGIN_DIR . '/support-forums/support-forums.php';
     508
     509                // bbPress roles still aren't quite right, need to switch away and back..
     510                // This is hacky, but otherwise the bbp_set_user_role() call below will appear to succeed, but no role alteration will actually happen.
     511                restore_current_blog();
     512                switch_to_blog( WPORG_SUPPORT_FORUMS_BLOGID );
     513
     514                // Load the Support Forums, for logging and whatnot.
     515                WordPressdotorg\Forums\Plugin::get_instance();
     516
     517                $callback = function( $text ) use ( $callback, $reason, $meta_column ) {
     518                        remove_filter( 'wporg_bbp_forum_role_changed_note_text', $callback );
     519
     520                        return trim( "{$reason}\n{$meta_column}\n\n{$text}" );
     521                };
     522                add_filter( 'wporg_bbp_forum_role_changed_note_text', $callback );
     523
     524                // Set the user to blocked. Support forum hooks will take care of the rest.
     525                bbp_set_user_role( $user->ID, bbp_get_blocked_role() );
     526
     527                restore_current_blog();
     528        }
     529
     530        return true;
     531}
     532
     533add_action( 'load-toplevel_page_user-registrations', function() {
     534        // Perform bulk actions.
     535        $action = $_REQUEST['action'] ?? ( $_REQUEST['action2'] ?? '' );
     536        if (
     537                empty( $_REQUEST['pending_ids'] ) ||
     538                'reg_block' !== $action ||
     539                ! wp_verify_nonce( $_REQUEST['_wpnonce'], 'bulk-toplevel_page_user-registrations' )
     540        ) {
     541                return;
     542        }
     543
     544        $reason = $_REQUEST['block_reason'] ?? '';
     545        foreach ( (array) $_REQUEST['pending_ids'] as $pending_id ) {
     546                $pending_user = wporg_get_pending_user( $pending_id );
     547                if ( ! $pending_user ) {
     548                        continue;
     549                }
     550
     551                if ( $pending_user['created'] ) {
     552                        wporg_login_block_account( $pending_user, $reason );
     553                } else {
     554                        wporg_login_block_registration( $pending_user );
     555                }
     556        }
     557
     558        $url = remove_query_arg( array( 'pending_ids', 'action', 'action2', '_wpnonce', '_wp_http_referer' ) );
     559        $url = add_query_arg( 'action', 'blocked_account', $url );
     560        wp_safe_redirect( $url );
     561        exit;
     562} );
Note: See TracChangeset for help on using the changeset viewer.