Making WordPress.org

Changeset 13216


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

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

Location:
sites/trunk/wordpress.org/public_html/wp-content/themes/pub/wporg-login
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • sites/trunk/wordpress.org/public_html/wp-content/themes/pub/wporg-login/admin/class-user-registrations-list-table.php

    r13200 r13216  
    4343                $current_view = $_REQUEST['view'] ?? $default;
    4444
    45                 if ( isset( $_GET['s'] ) ) {
     45                if ( ! empty( $_GET['s'] ) ) {
    4646                        $default = 'search';
    4747                        $views[0] = [
     
    144144        }
    145145
    146         function get_columns() {
     146        public function get_columns() {
    147147                return [
     148                        'cb'              => '<input type="checkbox" />',
    148149                        'user_login'      => 'User Login',
    149150                        'meta'            => 'Meta',
     
    159160                        'user_registered' => array( 'user_registered', true ),
    160161                ];
    161          }
     162        }
     163
     164        protected function get_bulk_actions() {
     165                return array(
     166                        'reg_block' => 'Block Reg / Ban user',
     167                );
     168        }
    162169
    163170        function prepare_items() {
     
    184191                }
    185192
    186                 $per_page     = $this->get_items_per_page( 'users_per_page', 100 );
     193                $per_page     = $_GET['per_page'] ?? $this->get_items_per_page( 'users_per_page', 100 );
    187194                $current_page = $this->get_pagenum();
    188195
     
    215222                ]);
    216223
     224        }
     225
     226        protected function bulk_actions( $which = '' ) {
     227                parent::bulk_actions( $which );
     228
     229                if ( 'top' !== $which ) {
     230                        return;
     231                }
     232                ?>
     233
     234                <fieldset class="alignleft actions">
     235                        <input name="block_reason" id="block_reason" placeholder="Ban/Block reason. Used for bulk + single." style="width: 32em;padding: 0.4em;margin: 0;" value="<?php echo esc_attr( $_REQUEST['block_reason'] ?? '' ); ?>" />
     236                </fieldset>
     237                <?php
    217238        }
    218239
     
    249270        }
    250271
     272        public function column_cb( $item ) {
     273                return sprintf(
     274                        '<input type="checkbox" name="pending_ids[]" value="%1$s" />',
     275                        esc_attr( $item->pending_id ),
     276                );
     277        }
     278
    251279        function column_default( $item, $column_name ) {
    252280                echo esc_html( $item->$column_name );
     
    348376                echo '<div>';
    349377
    350                 echo implode( ', ',
    351                         array_map(
    352                                 function( $ip ) {
    353                                         return $this->link_to_search( $ip ) .
    354                                                 ( is_callable( 'WordPressdotorg\GeoIP\query' ) ?
    355                                                         ' ' . \WordPressdotorg\GeoIP\query( $ip, 'country_short' ) : '' );
    356                                 },
    357                                 array_filter( array_unique( [
    358                                         $meta->registration_ip ?? false,
    359                                         $meta->confirmed_ip ?? false
    360                                 ] ) )
    361                         )
    362                 );
     378                $ips = [];
     379                foreach ( [ 'registration', 'confirmed' ] as $field ) {
     380                        if ( empty( $meta->{$field . '_ip'} ) ) {
     381                                continue;
     382                        }
     383                        $ip = $meta->{$field . '_ip'};
     384
     385                        $meta->{$field . '_ip_country'} ??= ( is_callable( 'WordPressdotorg\GeoIP\query' ) ? ' ' . \WordPressdotorg\GeoIP\query( $ip, 'country_short' ) : '' );
     386
     387                        $ips[] = $ip . ' ' . $meta->{$field . '_ip_country'};
     388                }
     389
     390                echo implode( ', ', array_map( array( $this, 'link_to_Search' ), array_unique( $ips ) ) );
    363391
    364392                echo '<hr>';
     
    446474
    447475                return implode( '', array_map( function( $s ) {
    448                         if ( strlen( $s ) >= 3 ) {
     476                        if ( strlen( $s ) >= 3 || preg_match( '/^[A-Z]{2}$/', $s ) /* country */ ) {
    449477                                return '<a href="' . add_query_arg( 's', urlencode( $s ), admin_url( 'admin.php?page=user-registrations' ) ) . '">' . esc_html( $s ) . '</a>';
    450478                        }
  • 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} );
  • sites/trunk/wordpress.org/public_html/wp-content/themes/pub/wporg-login/functions-registration.php

    r13215 r13216  
    8383                'meta' => $meta + array(
    8484                        'registration_ip'  => $_SERVER['REMOTE_ADDR'], // Spam & fraud control. Will be discarded after the account is created.
     85                        'registration_ip_country' => ( is_callable( 'WordPressdotorg\GeoIP\query' ) ? ' ' . \WordPressdotorg\GeoIP\query( $_SERVER['REMOTE_ADDR'], 'country_short' ) : '' )
    8586                ),
    8687                'scores' => array(
     
    191192/**
    192193 * Fetches a pending user record from the database by username or Email.
    193  */
    194 function wporg_get_pending_user( $login_or_email ) {
     194 *
     195 * @param string|int $who The username, email address, or user ID.
     196 */
     197function wporg_get_pending_user( $who ) {
    195198        global $wpdb;
    196199
    197200        // Is it a pending user object already?
    198         if ( is_array( $login_or_email ) && isset( $login_or_email['pending_id'] ) ) {
    199                 return $login_or_email;
    200         }
    201 
    202         $login_or_email = trim( $login_or_email );
    203         if ( ! $login_or_email ) {
     201        if ( is_array( $who ) && isset( $who['pending_id'] ) ) {
     202                return $who;
     203        }
     204
     205        if ( is_numeric( $who ) && (int) $who == $who ) {
     206                $field = 'pending_id';
     207        } elseif ( str_contains( $who, '@' ) ) {
     208                $field = 'user_email';
     209        } else {
     210                $field = 'user_login';
     211        }
     212
     213        $who = trim( $who );
     214        if ( ! $who ) {
    204215                return false;
    205216        }
    206217
    207218        $pending_user = $wpdb->get_row( $wpdb->prepare(
    208                 "SELECT * FROM `{$wpdb->base_prefix}user_pending_registrations` WHERE ( `user_login` = %s OR `user_email` = %s ) LIMIT 1",
    209                 $login_or_email,
    210                 $login_or_email
     219                "SELECT * FROM `{$wpdb->base_prefix}user_pending_registrations` WHERE %i = %s LIMIT 1",
     220                $field,
     221                $who
    211222        ), ARRAY_A );
    212223
     
    323334
    324335        // Update the pending record with the new details.
    325         $pending_user['created'] = 1;
    326         $pending_user['created_date'] = gmdate( 'Y-m-d H:i:s' );
    327         $pending_user['meta']['confirmed_ip'] = $_SERVER['REMOTE_ADDR']; // Spam/Fraud purposes, will be deleted once not needed.
     336        $pending_user['created']                      = 1;
     337        $pending_user['created_date']                 = gmdate( 'Y-m-d H:i:s' );
     338        $pending_user['meta']['confirmed_ip']         = $_SERVER['REMOTE_ADDR'];
     339        $pending_user['meta']['confirmed_ip_country'] = ( is_callable( 'WordPressdotorg\GeoIP\query' ) ? ' ' . \WordPressdotorg\GeoIP\query( $_SERVER['REMOTE_ADDR'], 'country_short' ): '' );
    328340
    329341        // reCaptcha v3 logging.
Note: See TracChangeset for help on using the changeset viewer.