Make WordPress Core

Changeset 33614


Ignore:
Timestamp:
08/13/2015 10:30:26 PM (9 years ago)
Author:
ocean90
Message:

Capabilities: Fall back to the edit_posts capability for orphaned comments.

Also avoid PHP notices because of orphaned comments in the comments list table.
Includes unit test.

props pento, dd32.
fixes #33154.

Location:
trunk
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-admin/includes/class-wp-comments-list-table.php

    r33490 r33614  
    473473        }
    474474
    475         $post = get_post();
    476 
    477475        $the_comment_status = wp_get_comment_status( $comment->comment_ID );
    478476
     
    535533            $format = '<a data-comment-id="%d" data-post-id="%d" data-action="%s" class="%s" title="%s" href="#">%s</a>';
    536534
    537             $actions['quickedit'] = sprintf( $format, $comment->comment_ID, $post->ID, 'edit', 'vim-q comment-inline',esc_attr__( 'Edit this item inline' ), __( 'Quick&nbsp;Edit' ) );
    538 
    539             $actions['reply'] = sprintf( $format, $comment->comment_ID, $post->ID, 'replyto', 'vim-r comment-inline', esc_attr__( 'Reply to this comment' ), __( 'Reply' ) );
     535            $actions['quickedit'] = sprintf( $format, $comment->comment_ID, $comment->comment_post_ID, 'edit', 'vim-q comment-inline',esc_attr__( 'Edit this item inline' ), __( 'Quick&nbsp;Edit' ) );
     536
     537            $actions['reply'] = sprintf( $format, $comment->comment_ID, $comment->comment_post_ID, 'replyto', 'vim-r comment-inline', esc_attr__( 'Reply to this comment' ), __( 'Reply' ) );
    540538        }
    541539
     
    673671        $post = get_post();
    674672
     673        if ( ! $post ) {
     674            return;
     675        }
     676
    675677        if ( isset( $this->pending_count[$post->ID] ) ) {
    676678            $pending_comments = $this->pending_count[$post->ID];
  • trunk/src/wp-includes/capabilities.php

    r33357 r33614  
    13061306            break;
    13071307        $post = get_post( $comment->comment_post_ID );
    1308         $caps = map_meta_cap( 'edit_post', $user_id, $post->ID );
     1308
     1309        /*
     1310         * If the post doesn't exist, we have an orphaned comment.
     1311         * Fall back to the edit_posts capability, instead.
     1312         */
     1313        if ( $post ) {
     1314            $caps = map_meta_cap( 'edit_post', $user_id, $post->ID );
     1315        } else {
     1316            $caps = map_meta_cap( 'edit_posts', $user_id );
     1317        }
    13091318        break;
    13101319    case 'unfiltered_upload':
  • trunk/tests/phpunit/tests/ajax/EditComment.php

    r25002 r33614  
    4747        ) );
    4848        $comment = array_pop( $comments );
     49
     50        // Set up a default request
     51        $_POST['_ajax_nonce-replyto-comment'] = wp_create_nonce( 'replyto-comment' );
     52        $_POST['comment_ID']                  = $comment->comment_ID;
     53        $_POST['content']                     = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit.';
     54
     55        // Make the request
     56        try {
     57            $this->_handleAjax( 'edit-comment' );
     58        } catch ( WPAjaxDieContinueException $e ) {
     59            unset( $e );
     60        }
     61
     62        // Get the response
     63        $xml = simplexml_load_string( $this->_last_response, 'SimpleXMLElement', LIBXML_NOCDATA );
     64
     65        // Check the meta data
     66        $this->assertEquals( -1, (string) $xml->response[0]->edit_comment['position'] );
     67        $this->assertEquals( $comment->comment_ID, (string) $xml->response[0]->edit_comment['id'] );
     68        $this->assertEquals( 'edit-comment_' . $comment->comment_ID, (string) $xml->response['action'] );
     69
     70        // Check the payload
     71        $this->assertNotEmpty( (string) $xml->response[0]->edit_comment[0]->response_data );
     72
     73        // And supplemental is empty
     74        $this->assertEmpty( (string) $xml->response[0]->edit_comment[0]->supplemental );
     75    }
     76
     77    /**
     78     * @ticket 33154
     79     */
     80    function test_editor_can_edit_orphan_comments() {
     81        global $wpdb;
     82
     83        // Become an editor
     84        $this->_setRole( 'editor' );
     85
     86        // Get a comment
     87        $comments = get_comments( array(
     88            'post_id' => $this->_comment_post->ID
     89        ) );
     90        $comment = array_pop( $comments );
     91
     92        // Manually update the comment_post_ID, because wp_update_comment() will prevent it.
     93        $wpdb->query( "UPDATE {$wpdb->comments} SET comment_post_ID=0 WHERE comment_ID={$comment->comment_ID}" );
     94        clean_comment_cache( $comment->comment_ID );
    4995
    5096        // Set up a default request
Note: See TracChangeset for help on using the changeset viewer.