Making WordPress.org


Ignore:
Timestamp:
01/23/2026 04:13:33 AM (10 months ago)
Author:
dd32
Message:

Add a helper method to allow other WordPress.org sites to add a note to a user, or block (disable) a user.

File:
1 edited

Legend:

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

    r14301 r14640  
    8888         * Saves a note to a users meta data.
    8989         *
    90          * @param int    $user_id   The user ID.
    91          * @param string $note_text The note text to add.
    92          * @param int    $post_id   The support thread this text is related to. Optional.
    93          * @param int    $note_id   The note ID to edit. Optional.
    94          */
    95         public function add_user_note( $user_id, $note_text, $post_id = 0, $note_id = 0 ) {
     90         * @param int        $user_id   The user ID.
     91         * @param string     $note_text The note text to add.
     92         * @param int|string $post_id   The support thread, or URL, this note is related to. Optional.
     93         * @param int        $note_id   The note ID to edit. Optional.
     94         * @param int|null   $author    The user ID of the note author. Optional. Default null (current user).
     95         * @param int|null   $site_id   The site ID where the note is being added. Optional. Default null (current site).
     96         */
     97        public function add_user_note( $user_id, $note_text, $post_id = 0, $note_id = 0, $author = null, $site_id = null ) {
    9698                // Make sure the user exists.
    9799                $user = get_userdata( $user_id );
    … …  
    100102                }
    101103
     104                $author  ??= wp_get_current_user()->user_nicename;
     105                $site_id ??= get_current_blog_id();
     106
     107                if ( is_numeric( $author ) || $author instanceof \WP_User ) {
     108                        $author = get_userdata( $author )->user_nicename;
     109                }
     110
    102111                // Get an array of existing notes, or create an array if there are none.
    103112                $user_notes = get_user_meta( $user_id, self::META, true );
    … …  
    106115                }
    107116
    108                 $edit_note = isset( $user_notes[ $note_id ] );
     117                $edit_note = $note_id && isset( $user_notes[ $note_id ] );
    109118
    110119                if ( ! $edit_note ) {
    … …  
    115124                                'text'      => $note_text,
    116125                                'date'      => current_time( 'mysql' ),
    117                                 'post_id'   => $post_id,
    118                                 'site_id'   => get_current_blog_id(),
    119                                 'moderator' => wp_get_current_user()->user_nicename
     126                                'post_id'   => 0,
     127                                'site_id'   => $site_id,
     128                                'moderator' => $author,
    120129                        );
     130
     131                        // Associate the note with a post if provided.
     132                        if ( is_numeric( $post_id ) && $post_id ) {
     133                                $user_notes[ $note_id ]->post_id = $post_id;
     134                        } elseif ( $post_id ) {
     135                                $user_notes[ $note_id ]->url = esc_url_raw( $post_id );
     136                        }
    121137                } else {
    122138                        // Only keymasters or the note author can edit a note.
    … …  
    134150                        // Add site ID if missing.
    135151                        if ( ! isset( $user_notes[ $note_id ]->site_id ) ) {
    136                                 $user_notes[ $note_id ]->site_id = get_current_blog_id();
     152                                $user_notes[ $note_id ]->site_id = $site_id;
     153                        }
     154
     155                        // Associate the note with a post if provided.
     156                        if ( is_numeric( $post_id ) && $post_id ) {
     157                                $user_notes[ $note_id ]->post_id = $post_id;
     158                        } elseif ( $post_id ) {
     159                                $user_notes[ $note_id ]->url = esc_url_raw( $post_id );
    137160                        }
    138161                }
    … …  
    295318                foreach ( $user_notes as $key => $note ) {
    296319                        $post_site_id       = isset( $note->site_id ) ? (int) $note->site_id : get_current_blog_id();
    297                         $post_permalink     = $this->get_user_note_post_permalink( $note->post_id, $user_id, $post_site_id );
     320                        if ( ! empty( $note->url ) ) {
     321                                $post_permalink = $note->url;
     322                                $mod_url        = 'https://profiles.wordpress.org/' . urlencode( $note->moderator ) . '/';
     323                        } else {
     324                                $post_permalink = $this->get_user_note_post_permalink( $note->post_id, $user_id, $post_site_id );
     325                                $mod_url        = get_home_url( $post_site_id, "/users/{$note->moderator}/" );
     326                        }
    298327                        $redirect_on_delete = $this->get_user_note_post_permalink( get_the_ID(), $user_id, get_current_blog_id() );
    299328
    … …  
    303332                                        __( 'By %1$s on <a href="%2$s">%3$s at %4$s</a>', 'wporg-forums' ),
    304333                                        sprintf( '<a href="%s">%s</a>',
    305                                                 esc_url( get_home_url( $post_site_id, "/users/{$note->moderator}/" ) ),
     334                                                esc_url( $mod_url ),
    306335                                                $note->moderator
    307336                                        ),
Note: See TracChangeset for help on using the changeset viewer.