Making WordPress.org

Changeset 12161


Ignore:
Timestamp:
10/31/2022 03:47:05 AM (3 years ago)
Author:
dd32
Message:

Support Forums: Create a user notes entry every time a user is flagged or unflagged.

This also adds a generic User_Notes function that updates the previous user-note if made within 5 minutes, as used by the forum role changer, so that the flagged/unflagged note is added to any recent descriptive reason.

Fixes #6554.

Location:
sites/trunk/wordpress.org/public_html/wp-content/plugins/support-forums/inc
Files:
2 edited

Legend:

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

    r12146 r12161  
    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
    138142        // Break users sessions / passwords when they get blocked, on the main forums only.
    139143        if ( 'wordpress.org' === get_blog_details()->domain ) {
     
    14501454        if ( $note_text ) {
    14511455            // Add a user note about this action.
    1452             $notes   = Plugin::get_instance()->user_notes->get_user_notes( $user->ID );
    1453             $note_id = 0;
    1454 
    1455             // Check to see if the last note added was from the current user in the last few minutes, and if so, append to it.
    1456             if ( $notes->count ) {
    1457                 $last_note_id = array_key_last( $notes->raw );
    1458                 $last_note    = $notes->raw[ $last_note_id ];
    1459                 if (
    1460                     // Note from the current user
    1461                     $last_note->moderator === wp_get_current_user()->user_nicename &&
    1462                     // ..and created within 5 minutes.
    1463                     absint( time() - strtotime( $last_note->date ) ) <= 5 * MINUTE_IN_SECONDS
    1464                 ) {
    1465                     $note_id = $last_note_id;
    1466 
    1467                     // Prefix the existing message.
    1468                     $note_text = trim( $last_note->text . "\n\n" . $note_text );
    1469                 }
    1470             }
    1471 
    1472             // Add a user note about this action.
    1473             Plugin::get_instance()->user_notes->add_user_note(
     1456            Plugin::get_instance()->user_notes->add_user_note_or_update_previous(
    14741457                $user->ID,
    1475                 $note_text,
    1476                 null,
    1477                 $note_id
     1458                $note_text
    14781459            );
    14791460        }
    14801461
    14811462    }
     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 () ) ? 'flaged' : '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    }
    14821477}
  • sites/trunk/wordpress.org/public_html/wp-content/plugins/support-forums/inc/class-user-notes.php

    r11527 r12161  
    147147
    148148    /**
     149     * Saves a note to a users meta data. Suffixes to previous user note if by same moderator within a timeframe.
     150     *
     151     * @param int    $user_id   The user ID.
     152     * @param string $note_text The note text to add.
     153     * @param int    $post_id   The support thread this text is related to. Optional.
     154     * @param int    $timeframe The timeframe used to determine if the previous note should be updated. Optional. Default 5 minutes.
     155     */
     156    public function add_user_note_or_update_previous( $user_id, $note_text, $post_id = 0, $timeframe = 300 ) {
     157        // Default to adding a new note..
     158        $note_id = 0;
     159
     160        // Add a user note about this action.
     161        $existing_notes = $this->get_user_notes( $user_id );
     162
     163        // Check to see if the last note added was from the current user in the last few minutes, and if so, append to it.
     164        if ( $existing_notes->count ) {
     165            $last_note_id = array_key_last( $existing_notes->raw );
     166            $last_note    = $existing_notes->raw[ $last_note_id ];
     167            if (
     168                // Note from the current user
     169                $last_note->moderator === wp_get_current_user()->user_nicename &&
     170                // ..and created within $timeframe seconds
     171                absint( time() - strtotime( $last_note->date ) ) <= $timeframe
     172            ) {
     173                $note_id = $last_note_id;
     174
     175                // Prefix the existing message.
     176                $note_text = trim( $last_note->text . "\n\n" . $note_text );
     177            }
     178        }
     179
     180        return $this->add_user_note( $user_id, $note_text, $post_id, $note_id );
     181    }
     182
     183    /**
    149184     * Deletes a previously added note from user's meta data.
    150185     *
Note: See TracChangeset for help on using the changeset viewer.