diff --git a/src/wp-admin/includes/class-wp-comments-list-table.php b/src/wp-admin/includes/class-wp-comments-list-table.php index 3578c8d93d..fcfcd1e36b 100644 --- a/src/wp-admin/includes/class-wp-comments-list-table.php +++ b/src/wp-admin/includes/class-wp-comments-list-table.php @@ -472,8 +472,6 @@ class WP_Comments_List_Table extends WP_List_Table { return; } - $post = get_post(); - $the_comment_status = wp_get_comment_status( $comment->comment_ID ); $out = ''; @@ -534,9 +532,9 @@ class WP_Comments_List_Table extends WP_List_Table { $format = '%s'; - $actions['quickedit'] = sprintf( $format, $comment->comment_ID, $post->ID, 'edit', 'vim-q comment-inline',esc_attr__( 'Edit this item inline' ), __( 'Quick Edit' ) ); + $actions['quickedit'] = sprintf( $format, $comment->comment_ID, $comment->comment_post_ID, 'edit', 'vim-q comment-inline',esc_attr__( 'Edit this item inline' ), __( 'Quick Edit' ) ); - $actions['reply'] = sprintf( $format, $comment->comment_ID, $post->ID, 'replyto', 'vim-r comment-inline', esc_attr__( 'Reply to this comment' ), __( 'Reply' ) ); + $actions['reply'] = sprintf( $format, $comment->comment_ID, $comment->comment_post_ID, 'replyto', 'vim-r comment-inline', esc_attr__( 'Reply to this comment' ), __( 'Reply' ) ); } /** This filter is documented in wp-admin/includes/dashboard.php */ @@ -672,6 +670,10 @@ class WP_Comments_List_Table extends WP_List_Table { public function column_response() { $post = get_post(); + if ( ! $post ) { + return; + } + if ( isset( $this->pending_count[$post->ID] ) ) { $pending_comments = $this->pending_count[$post->ID]; } else { diff --git a/src/wp-includes/capabilities.php b/src/wp-includes/capabilities.php index dd97d98098..42133f6feb 100644 --- a/src/wp-includes/capabilities.php +++ b/src/wp-includes/capabilities.php @@ -1305,7 +1305,16 @@ function map_meta_cap( $cap, $user_id ) { if ( empty( $comment ) ) break; $post = get_post( $comment->comment_post_ID ); - $caps = map_meta_cap( 'edit_post', $user_id, $post->ID ); + + /* + * If the post doesn't exist, we have an orphaned comment. + * Fall back to the edit_posts capability, instead. + */ + if ( $post ) { + $caps = map_meta_cap( 'edit_post', $user_id, $post->ID ); + } else { + $caps = map_meta_cap( 'edit_posts', $user_id ); + } break; case 'unfiltered_upload': if ( defined('ALLOW_UNFILTERED_UPLOADS') && ALLOW_UNFILTERED_UPLOADS && ( !is_multisite() || is_super_admin( $user_id ) ) ) diff --git a/tests/phpunit/tests/ajax/EditComment.php b/tests/phpunit/tests/ajax/EditComment.php index 8c6ebdfcea..5d82229e70 100644 --- a/tests/phpunit/tests/ajax/EditComment.php +++ b/tests/phpunit/tests/ajax/EditComment.php @@ -74,6 +74,52 @@ class Tests_Ajax_EditComment extends WP_Ajax_UnitTestCase { $this->assertEmpty( (string) $xml->response[0]->edit_comment[0]->supplemental ); } + /** + * @ticket 33154 + */ + function test_editor_can_edit_orphan_comments() { + global $wpdb; + + // Become an editor + $this->_setRole( 'editor' ); + + // Get a comment + $comments = get_comments( array( + 'post_id' => $this->_comment_post->ID + ) ); + $comment = array_pop( $comments ); + + // Manually update the comment_post_ID, because wp_update_comment() will prevent it. + $wpdb->query( "UPDATE {$wpdb->comments} SET comment_post_ID=0 WHERE comment_ID={$comment->comment_ID}" ); + clean_comment_cache( $comment->comment_ID ); + + // Set up a default request + $_POST['_ajax_nonce-replyto-comment'] = wp_create_nonce( 'replyto-comment' ); + $_POST['comment_ID'] = $comment->comment_ID; + $_POST['content'] = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit.'; + + // Make the request + try { + $this->_handleAjax( 'edit-comment' ); + } catch ( WPAjaxDieContinueException $e ) { + unset( $e ); + } + + // Get the response + $xml = simplexml_load_string( $this->_last_response, 'SimpleXMLElement', LIBXML_NOCDATA ); + + // Check the meta data + $this->assertEquals( -1, (string) $xml->response[0]->edit_comment['position'] ); + $this->assertEquals( $comment->comment_ID, (string) $xml->response[0]->edit_comment['id'] ); + $this->assertEquals( 'edit-comment_' . $comment->comment_ID, (string) $xml->response['action'] ); + + // Check the payload + $this->assertNotEmpty( (string) $xml->response[0]->edit_comment[0]->response_data ); + + // And supplemental is empty + $this->assertEmpty( (string) $xml->response[0]->edit_comment[0]->supplemental ); + } + /** * Get comments as a non-privileged user (subscriber) * Expects test to fail