Making WordPress.org


Ignore:
Timestamp:
07/20/2023 04:01:22 AM (3 years ago)
Author:
dd32
Message:

Support Forums: Improve the user notes for role changes to include the previous non-participant role, capture moderator/spectator role sets, and generally have cleaner code.

See #5707.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • sites/trunk/wordpress.org/public_html/wp-content/plugins/support-forums/inc/class-hooks.php

    r12171 r12749  
    136136                add_filter( 'pre_oembed_result', array( $this, 'pre_oembed_result_dont_embed_wordpress_org_anchors' ), 20, 2 );
    137137
    138                 // Add a user note when flagging/unflagging a user.
    139                 add_filter( 'wporg_bbp_flag_user', array( $this, 'log_user_flag_changes' ) );
    140                 add_filter( 'wporg_bbp_unflag_user', array( $this, 'log_user_flag_changes' ) );
    141 
    142138                // Break users sessions / passwords when they get blocked, on the main forums only.
    143139                if ( 'wordpress.org' === get_blog_details()->domain ) {
    144                         add_action( 'bbp_set_user_role', array( $this, 'user_blocked_password_handler' ), 10, 3 );
     140                        add_filter( 'bbp_set_user_role', array( $this, 'user_blocked_password_handler' ), 10, 3 );
    145141                }
    146142        }
     
    13601356         *
    13611357         * Note: This method is called even when the users role is not changed.
     1358         *
     1359         * See Audit_Log class for where the note is set/updated.
    13621360         */
    13631361        public function user_blocked_password_handler( $new_role, $user_id, \WP_User $user ) {
     
    13661364                // ~~~ is a reset password on WordPress.org. Let's ignore those.
    13671365                if ( '~~~' === $user->user_pass ) {
    1368                         return;
     1366                        return $new_role;
    13691367                }
    13701368
     
    13741372                $blocked_role    = bbp_get_blocked_role();
    13751373                $password_broken = ( 0 === strpos( $user->user_pass, $blocked_prefix ) );
    1376                 $note_text       = false;
    13771374
    13781375                // WP_User::has_role() does not exist, and WP_User::has_cap( 'bbp_blocked' ) will be truthful for super admins.
    13791376                $user_has_blocked_role = ! empty( $user->roles ) && in_array( $blocked_role, $user->roles, true );
    1380 
    1381                 // Define what has blocked the user.
    1382                 if ( ! ms_is_switched() ) {
    1383                         $where_from = preg_replace( '!^https?://!i', '', home_url( is_admin() ? '/wp-admin' : '' ) );
    1384                 } else {
    1385                         // When we're switched, we can't determine the source of the switch, so we use a bit of URL parsing magic.
    1386                         $where_from = $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
    1387                         if ( str_contains( $where_from, '?' ) ) {
    1388                                 list( $where_from, ) = explode( '?', $where_from );
    1389                         }
    1390                         // Trim actual filename off, just the major path component.
    1391                         $where_from = preg_replace( '!/[^/?]+\.[a-z]{3}$!i', '', $where_from  );
    1392                 }
    13931377
    13941378                if (
     
    14131397                        $manager = \WP_Session_Tokens::get_instance( $user->ID );
    14141398                        $manager->destroy_all();
    1415 
    1416                         // Add a user note about this action.
    1417                         $note_text = sprintf(
    1418                                 $where_from ? 'Forum role changed to %s via %s.' : 'Forum role changed to %s.',
    1419                                 get_role( $new_role )->name,
    1420                                 $where_from
    1421                         );
    1422 
    1423                         // Used in wporg-login to add context.
    1424                         $note_text = apply_filters( 'wporg_bbp_forum_role_changed_note_text', $note_text, $user );
    14251399                } else if (
    14261400                        $password_broken &&
     
    14401414
    14411415                        clean_user_cache( $user );
    1442 
    1443                         // Add a user note about this action.
    1444                         $note_text = sprintf(
    1445                                 $where_from ? 'Forum role changed to %s via %s.' : 'Forum role changed to %s.',
    1446                                 get_role( $new_role )->name,
    1447                                 $where_from
    1448                         );
    1449 
    1450                         // Unused, here for consistency with above.
    1451                         $note_text = apply_filters( 'wporg_bbp_forum_role_changed_note_text', $note_text, $user );
    1452                 }
    1453 
    1454                 if ( $note_text ) {
    1455                         // Add a user note about this action.
    1456                         Plugin::get_instance()->user_notes->add_user_note_or_update_previous(
    1457                                 $user->ID,
    1458                                 $note_text
    1459                         );
    1460                 }
    1461 
    1462         }
    1463 
    1464         /**
    1465          * Add a user note when a user is flagged / unflagged.
    1466          */
    1467         function log_user_flag_changes( $user_id ) {
    1468                 $flag_action = ( 'wporg_bbp_flag_user' === current_filter () ) ? 'flagged' : 'unflagged';
    1469 
    1470                 $note_text = "User {$flag_action}.";
    1471 
    1472                 Plugin::get_instance()->user_notes->add_user_note_or_update_previous(
    1473                         $user_id,
    1474                         $note_text
    1475                 );
    1476         }
     1416                }
     1417
     1418                // It's a filter, return the value.
     1419                return $new_role;
     1420        }
     1421
    14771422}
Note: See TracChangeset for help on using the changeset viewer.