Making WordPress.org


Ignore:
Timestamp:
10/19/2018 10:56:22 PM (6 years ago)
Author:
coffee2code
Message:

developer.wordpress.org: Allow users to edit their own submitted notes that are still in moderation.

Props keesiemeijer.
Fixes #3572.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • sites/trunk/wordpress.org/public_html/wp-content/themes/pub/wporg-developer/inc/template-tags.php

    r7753 r7754  
    114114            // Check if the current page is a reply to a note.
    115115            $reply_id = 0;
    116             if ( isset( $_GET['replytocom'] ) && $_GET['replytocom'] ) {
     116            if ( isset( $_GET['replytocom'] ) && $_GET['replytocom'] ) {       
     117                /* Javascript uses preventDefault() when clicking links with '?replytocom={comment_ID}'
     118                 * We assume Javascript is disabled when visiting a page with this query var.
     119                 * There are no consequences if Javascript is enabled.
     120                 */
    117121                $reply_id = absint( $_GET['replytocom'] );
    118122            }
     
    143147                }
    144148
    145                 if ( ! $show_editor && ( $reply_id && ( $reply_id === (int) $comment->comment_ID ) ) ) {
    146                     // Show the editor when replying to this parent comment
     149                if ( ! $show_editor && ( $reply_id && ( $reply_id === (int) $comment->comment_ID ) ) ) {
     150                    /* The query var 'replytocom' is used and the value is the same as the current comment ID.
     151                     * We show the editor for the current comment because we assume Javascript is disabled.
     152                     * If Javascript is not disabled the editor is hidden (as normal) by the class 'hide-if-js'.
     153                     */
    147154                    $comments[ $key ]->show_editor = true;
    148155                    $show_editor = true;
     
    165172         */
    166173        function wporg_developer_list_notes( $comments, $args ) {
     174            $is_user_content    = class_exists( 'DevHub_User_Submitted_Content' );
    167175            $is_user_logged_in  = is_user_logged_in();
    168176            $can_user_post_note = DevHub\can_user_post_note( true, get_the_ID() );
    169             $user_content       = class_exists( 'DevHub_User_Submitted_Content' );
    170             $display_editor     = $is_user_logged_in && $can_user_post_note && $user_content;
     177            $is_user_verified   = $is_user_logged_in && $can_user_post_note;
     178       
     179            $args['updated_note'] = 0;
     180            if ( isset( $_GET['updated-note'] ) && $_GET['updated-note'] ) {
     181                $args['updated_note'] = absint( $_GET['updated-note'] );
     182            }
    171183
    172184            foreach ( $comments as $comment ) {
     
    177189                wporg_developer_user_note( $comment, $args, 1 );
    178190
    179                 // Show or hide feedback notes.
    180                 $class = $comment->show_editor ? '' : ' hide-if-js';
    181                 echo "<section id='feedback-{$comment_id}' class='feedback{$class}'>\n";
     191                /* Use hide-if-js class to hide the feedback section if Javascript is enabled.
     192                 * Users can display the section with Javascript.
     193                 */
     194                echo "<section id='feedback-{$comment_id}' class='feedback hide-if-js'>\n";
    182195
    183196                // Display child comments.
     
    193206
    194207                // Add a feedback form for logged in users.
    195                 if ( $display_editor ) {
    196                     // Show or hide the editor depending if we're replying to a note.
     208                if ( $is_user_content && $is_user_verified ) {
     209                    /* Show the feedback editor if we're replying to a note and Javascript is disabled.
     210                     * If Javascript is enabled the editor is hidden (as normal) by the 'hide-if-js' class.
     211                     */
    197212                    $display = $comment->show_editor ? 'show' : 'hide';
    198213                    echo DevHub_User_Submitted_Content::wp_editor_feedback( $comment, $display );
     
    203218                echo "<footer class='feedback-links' >\n";
    204219                if ( $can_user_post_note ) {
    205                     echo "EEE";
    206220                    $feedback_link = trailingslashit( get_permalink() ) . "?replytocom={$comment_id}#feedback-editor-{$comment_id}";
    207221                    $display       = '';
    208                     $aria          = '';
    209222                    if ( ! $is_user_logged_in ) {
    210223                        $class         = 'login';
     
    214227                        $class         ='add';
    215228                        $feedback_text = __( 'Add feedback to this note', 'wporg' );
    216                         $aria          = '';//" aria-expanded='false' aria_controls='feedback-editor-{$comment_id}' aria-label='" . esc_attr( $feedback_text ) . "'";
    217 
    218                         // Hide 'add feedback' link if editor is displayed.
    219                         $display = $display_editor && $comment->show_editor ? ' style="display:none"' : '';
     229
     230                        /* Hide the feedback link if the current user is logged in and the
     231                         * feedback editor is displayed (because Javascript is disabled).
     232                         * If Javascript is enabled the editor is hidden and the feedback link is displayed (as normal).
     233                         */
     234                        $display = $is_user_verified && $comment->show_editor ? ' style="display:none"' : '';
    220235                    }
    221                     echo '<a class="feedback-' . $class . '" href="' . esc_url( $feedback_link ) . '"' . $display . $aria .' rel="nofollow">' . $feedback_text . '</a>';
     236                    echo '<a role="button" class="feedback-' . $class . '" href="' . esc_url( $feedback_link ) . '"' . $display . ' rel="nofollow">' . $feedback_text . '</a>';
    222237                }
    223238
     
    241256            $GLOBALS['comment_depth'] = $depth;
    242257
     258            static $note_number = 0;
     259
    243260            $approved       = ( 0 < (int) $comment->comment_approved ) ? true : false;
    244261            $is_parent      = ( 0 === (int) $comment->comment_parent ) ? true : false;
     
    246263            $count          = $is_voting ? (int)  DevHub_User_Contributed_Notes_Voting::count_votes( $comment->comment_ID, 'difference' ) : 0;
    247264            $curr_user_note = $is_voting ? (bool) DevHub_User_Contributed_Notes_Voting::is_current_user_note( $comment->comment_ID ) : false;
    248 
    249             // Classes
     265            $edited_note_id = isset( $args['updated_note'] ) ? $args['updated_note'] : 0;
     266            $is_edited_note = ( $edited_note_id === (int) $comment->comment_ID );
     267            $note_author    = \DevHub\get_note_author_link( $comment );
     268            $can_edit_note  = \DevHub\can_user_edit_note( $comment->comment_ID );
     269            $has_edit_cap   = current_user_can( 'edit_comment', $comment->comment_ID );
     270
     271            // CSS Classes
    250272            $comment_class = array();
    251273
     
    260282            if ( ! $approved ) {
    261283                $comment_class[] = 'user-note-moderated';
    262             }
    263 
    264             // This would all be moot if core passed the $comment context for 'get_comment_author_link' filter.
    265             if ( $comment->user_id ) {
    266                 $commenter = get_user_by( 'id', $comment->user_id );
    267                 $url = 'https://profiles.wordpress.org/' . sanitize_key( $commenter->user_nicename ) . '/';
    268                 $author = get_the_author_meta( 'display_name', $comment->user_id );
    269             } else {
    270                 $url = $comment->comment_author_url;
    271                 $author = $comment->comment_author;
    272             }
    273 
    274             $comment_author_link = $author;
    275             if ( $url ) {
    276                 $comment_author_link = "<a href='$url' rel='external nofollow' class='url'>$author</a>";
    277284            }
    278285
     
    286293
    287294            <?php if ( $is_parent ) : ?>
    288                 <a href="#comment-content-<?php echo $comment->comment_ID; ?>" class="screen-reader-text"><?php _e( 'Skip to note content', 'wporg' ); ?></a>
     295                <a href="#comment-content-<?php echo $comment->comment_ID; ?>" class="screen-reader-text"><?php printf( __( 'Skip to note %d content', 'wporg' ), ++ $note_number ); ?></a>
    289296                <header class="comment-meta">
    290297
     
    301308                        }
    302309
    303                         printf( __( 'Contributed by %s', 'wporg' ), sprintf( '<cite class="fn">%s</cite>', $comment_author_link ) );
     310                        printf( __( 'Contributed by %s', 'wporg' ), sprintf( '<cite class="fn">%s</cite>', $note_author ) );
    304311                        ?>
    305312
    306313                        </span>
    307314                        &mdash;
    308                         <a href="<?php echo esc_url( get_comment_link( $comment->comment_ID ) ); ?>">
     315                        <a class="comment-date" href="<?php echo esc_url( get_comment_link( $comment->comment_ID ) ); ?>">
    309316                            <time datetime="<?php comment_time( 'c' ); ?>">
    310317                            <?php echo $date; ?>
    311318                            </time>
    312319                        </a>
     320
    313321                        <?php edit_comment_link( __( 'Edit', 'wporg' ), '<span class="edit-link">&mdash; ', '</span>' ); ?>
     322                        <?php if ( ! $has_edit_cap && $can_edit_note ) : ?>
     323                            &mdash; <span class="comment-author-edit-link">
     324                                <!-- Front end edit comment link -->
     325                                <a class="comment-edit-link" href="<?php echo site_url( "/reference/comment/edit/{$comment->comment_ID}" ); ?>"><?php _e( 'Edit', 'wporg' ); ?></a>
     326                            </span>
     327                        <?php endif; ?>
     328                        <?php if ( $can_edit_note && $is_edited_note ) : ?>
     329                            &mdash; <span class="comment-edited">
     330                            <?php _e( 'edited', 'wporg' ); ?>
     331                            </span>
     332                        <?php endif; ?>
    314333                        <?php if ( ! $approved ) : ?>
    315334                            &mdash; <span class="comment-awaiting-moderation"><?php _e( 'awaiting moderation', 'wporg' ); ?></span>
     
    326345                } else {
    327346                    $text = get_comment_text()  . ' &mdash; ';
    328                     $text .= sprintf( __( 'By %s', 'wporg' ), sprintf( '<cite class="fn">%s</cite>', $comment_author_link ) ) . ' &mdash; ';
    329                     $text .= ' <a href="'. esc_url( get_comment_link( $comment->comment_ID ) ) . '">';
     347                    $text .= sprintf( __( 'By %s', 'wporg' ), sprintf( '<cite class="fn">%s</cite>', $note_author ) ) . ' &mdash; ';
     348                    $text .= ' <a class="comment-date" href="'. esc_url( get_comment_link( $comment->comment_ID ) ) . '">';
    330349                    $text .= '<time datetime="' . get_comment_time( 'c' ) . '">' . $date . '</time></a>';
    331350
    332                     if ( current_user_can( 'edit_comment', $comment->comment_ID ) ) {
     351                    if ( $has_edit_cap ) {
     352                        // WP admin edit comment link.
    333353                        $text .= ' &mdash; <a class="comment-edit-link" href="' . get_edit_comment_link( $comment->comment_ID ) .'">';
    334354                        $text .= __( 'Edit', 'wporg' ) . '</a>';
     355                    } elseif ( $can_edit_note ) {
     356                        // Front end edit comment link.
     357                        $text .= ' &mdash; <a class="comment-edit-link" href="' . site_url( "/reference/comment/edit/{$comment->comment_ID}" ) . '">';
     358                        $text .= __( 'Edit', 'wporg' ) . '</a>';
     359                    }
     360
     361                    if ( $can_edit_note && $is_edited_note ) {
     362                        $text .= ' &mdash; <span class="comment-edited">' . __( 'edited', 'wporg' ) . '</span>';
    335363                    }
    336364
     
    14081436
    14091437    /**
     1438     * Indicates if the current user can edit a user contibuted note.
     1439     *
     1440     * A user can only edit their own notes if it's in moderation and
     1441     * if it's a note for a parsed post type.
     1442     *     
     1443     * Users with the 'edit_comment' capability can edit
     1444     * all notes from a parsed post type (regardless if it's in moderation).
     1445     *
     1446     * @param integer $note_id Note ID.
     1447     * @return bool True if the current user can edit notes.
     1448     */
     1449    function can_user_edit_note( $note_id = 0 ) {
     1450        $user = get_current_user_id();
     1451        $note = get_comment( $note_id );
     1452        if ( ! $user || ! $note ) {
     1453            return false;
     1454        }
     1455
     1456        $post_id        = isset( $note->comment_post_ID ) ? (int) $note->comment_post_ID : 0;
     1457        $is_note_author = isset( $note->user_id ) && ( (int) $note->user_id === $user );
     1458        $is_approved    = isset( $note->comment_approved ) && ( 0 < (int) $note->comment_approved );
     1459        $can_edit_notes = isset( $note->comment_ID ) && current_user_can( 'edit_comment', $note->comment_ID );
     1460        $is_parsed_type = is_parsed_post_type( get_post_type( $post_id ) );
     1461
     1462        if ( $is_parsed_type && ( $can_edit_notes || ( $is_note_author && ! $is_approved ) ) ) {
     1463            return true;
     1464        }
     1465
     1466        return false;
     1467    }
     1468
     1469    /**
     1470     * Get the note author link to the profiles.wordpress.org author's URL.
     1471     *
     1472     * @param WP_Comment|int $comment Comment object or comment ID.
     1473     * @return string The HTML link to the profiles.wordpress.org author's URL.
     1474     */
     1475    function get_note_author_link( $comment ) {
     1476        return get_note_author( $comment, true );
     1477    }
     1478
     1479    /**
     1480     * Get the note author nicename.
     1481     *
     1482     * @param WP_Comment|int $comment Comment object or comment ID.
     1483     * @param bool           $link. Whether to return a link to the author's profiles. Default false.
     1484     * @return string The comment author name or HTML link.
     1485     */
     1486    function get_note_author( $comment, $link = false ) {
     1487        $comment   = get_comment( $comment );
     1488        $user_id   = isset( $comment->user_id ) ? $comment->user_id : 0;
     1489        $commenter = get_user_by( 'id', $comment->user_id );
     1490        $author    = '';
     1491
     1492        if ( $user_id && isset( $commenter->user_nicename ) ) {
     1493            $url    = 'https://profiles.wordpress.org/' . sanitize_key( $commenter->user_nicename ) . '/';
     1494            $author = get_the_author_meta( 'display_name', $comment->user_id );
     1495        } else {
     1496            $url    = isset( $comment->comment_author_url ) ? $comment->comment_author_url : '';
     1497            $author = isset( $comment->comment_author ) ?  $comment->comment_author : '';
     1498        }
     1499
     1500        if ( $link && ( $url && $author ) ) {
     1501            $author = sprintf( '<a href="%s" rel="external nofollow" class="url">%s</a>', esc_url( $url ), $author );
     1502        }
     1503
     1504        return $author;
     1505    }
     1506
     1507    /**
    14101508     * Gets the summary.
    14111509     *
Note: See TracChangeset for help on using the changeset viewer.