Make WordPress Core

Ticket #37703: 37703.1.patch

File 37703.1.patch, 9.1 KB (added by Mte90, 5 years ago)

finished patch

  • src/wp-includes/comment.php

    diff --git src/wp-includes/comment.php src/wp-includes/comment.php
    index b451c5b641..c7a78d3197 100644
    function wp_count_comments( $post_id = 0 ) { 
    13761376 *
    13771377 * @param int|WP_Comment $comment_id   Comment ID or WP_Comment object.
    13781378 * @param bool           $force_delete Whether to bypass trash and force deletion. Default is false.
     1379 * @param bool           $bulk         Some behaviours are delegate to the bulk function wp_delete_comments. Default is false.
    13791380 * @return bool True on success, false on failure.
    13801381 */
    1381 function wp_delete_comment( $comment_id, $force_delete = false ) {
     1382function wp_delete_comment( $comment_id, $force_delete = false, $bulk = false ) {
    13821383        global $wpdb;
    13831384        $comment = get_comment( $comment_id );
    13841385        if ( ! $comment ) {
    function wp_delete_comment( $comment_id, $force_delete = false ) { 
    14001401         */
    14011402        do_action( 'delete_comment', $comment->comment_ID, $comment );
    14021403
    1403         // Move children up a level.
    1404         $children = $wpdb->get_col( $wpdb->prepare( "SELECT comment_ID FROM $wpdb->comments WHERE comment_parent = %d", $comment->comment_ID ) );
    1405         if ( ! empty( $children ) ) {
    1406                 $wpdb->update( $wpdb->comments, array( 'comment_parent' => $comment->comment_parent ), array( 'comment_parent' => $comment->comment_ID ) );
    1407                 clean_comment_cache( $children );
     1404        if ( !$bulk ) {
     1405                wp_reparentering_comment( $comment->comment_ID, $comment->comment_parent );
    14081406        }
    14091407
    14101408        // Delete metadata
    function wp_delete_comment( $comment_id, $force_delete = false ) { 
    14421440        return true;
    14431441}
    14441442
     1443/**
     1444 * Assign a new parent to a comment
     1445 *
     1446 * The comment get a new parent and the comment cache is cleaned.
     1447 *
     1448 * @since x.x.x
     1449 *
     1450 * @global wpdb $wpdb WordPress database abstraction object.
     1451 *
     1452 * @param string|Array $comment_id     Comment ID.
     1453 * @param string       $comment_parent Comment parent ID.
     1454 * @param array        $has_children   Pass children or find automatically. Default is false.
     1455 * @return bool True on success, false on failure.
     1456 */
     1457function wp_reparentering_comment( $comment_ID, $comment_parent, $has_children = false ) {
     1458        global $wpdb;
     1459        $data = array( 'comment_parent' => $comment_parent );
     1460        $where = array( 'comment_parent' => $comment_ID );
     1461        // Move children up a level.
     1462        if ( !$has_children ) {
     1463                $children = $wpdb->get_col( $wpdb->prepare( "SELECT comment_ID FROM $wpdb->comments WHERE comment_parent = %d", $comment_ID ) );
     1464                if ( ! empty( $children ) ) {
     1465                        clean_comment_cache( $children );
     1466                }
     1467        }
     1468
     1469        $wpdb->update( $wpdb->comments, $data, $where );
     1470
     1471        return true;
     1472}
     1473
     1474/**
     1475 * Trashes or deletes comments.
     1476 *
     1477 * The comments are moved to trash instead of permanently deleted unless trash is
     1478 * disabled, item is already in the trash, or $force_delete is true.
     1479 *
     1480 * The post comment count will be updated if the comments were approved and have a
     1481 * post ID available.
     1482 *
     1483 * @since x.x.x
     1484 *
     1485 * @global wpdb $wpdb WordPress database abstraction object.
     1486 *
     1487 * @param array $comments_id   Comment ID or WP_Comment object.
     1488 * @param bool  $force_delete Whether to bypass trash and force deletion. Default is false.
     1489 * @return bool True on success, false on failure.
     1490 */
     1491function wp_delete_comments( $comments_id, $force_delete = false ) {
     1492        global $wpdb;
     1493        $look_for_grandparents = array();
     1494        $look_for_ancestors = array();
     1495        if ( empty( $comments_id ) ) {
     1496                return true;
     1497        }
     1498
     1499        $comments_string = '(' . implode( ",", array_map( 'intval', $comments_id ) ) . ')';
     1500        // Determine if a parent comment is being deleted from a comment that is not being deleted
     1501        $look_for_parents = $wpdb->get_results( "SELECT comment_ID, comment_parent FROM $wpdb->comments WHERE comment_parent IN $comments_string AND comment_ID NOT IN $comments_string" , ARRAY_N);
     1502        // Determine grandparents
     1503        if ( !empty( $look_for_parents ) ) {
     1504                $parents_to_delete = array_map( function ( $a ) { return intval( $a[1] ); }, $look_for_parents );
     1505                $parents_string = '(' . implode( ",", $parents_to_delete ) . ')';
     1506                $look_for_grandparents = $wpdb->get_results( "SELECT comment_ID, comment_parent FROM $wpdb->comments WHERE comment_ID IN $parents_string", ARRAY_N );
     1507                // Determine very old ancestors
     1508                if ( !empty( $look_for_grandparents ) ) {
     1509                        foreach( $look_for_grandparents as $comment ) {
     1510                                wp_reparentering_comment( $comment[ 0 ], $comment[ 1 ], true );
     1511                        }
     1512                        $grandparents_to_delete = array_map( function ( $a ) { return intval( $a[1] ); }, $look_for_grandparents );
     1513                        $grandparents_string = '(' . implode( ",", $grandparents_to_delete ) . ')';
     1514                        $look_for_ancestors = $wpdb->get_results( "SELECT comment_ID, comment_parent FROM $wpdb->comments WHERE comment_ID IN $grandparents_string", ARRAY_N );
     1515                        foreach( $look_for_ancestors as $comment ) {
     1516                                wp_reparentering_comment( $comment[ 0 ], $comment[ 1 ], true );
     1517                        }
     1518                }
     1519        }
     1520
     1521        foreach ( $comments_id as $comment_id ) {
     1522                $single_comment = wp_delete_comment( $comment_id, $force_delete, true );
     1523                if ( !$single_comment ) {
     1524                        return false;
     1525                }
     1526        }
     1527
     1528        /**
     1529         * Fires immediately after comments are deleted from the database.
     1530         *
     1531         * @since x.x.x
     1532         *
     1533         * @param int        $comment_id The comment ID.
     1534         */
     1535        do_action( 'deleted_comments', $comments_id );
     1536        return true;
     1537}
     1538
    14451539/**
    14461540 * Moves a comment to the Trash
    14471541 *
  • src/wp-includes/post.php

    diff --git src/wp-includes/post.php src/wp-includes/post.php
    index deb215e17d..c44ff0d410 100644
    function wp_delete_post( $postid = 0, $force_delete = false ) { 
    30033003
    30043004        wp_defer_comment_counting( true );
    30053005
    3006         $comment_ids = $wpdb->get_col( $wpdb->prepare( "SELECT comment_ID FROM $wpdb->comments WHERE comment_post_ID = %d", $postid ) );
    3007         foreach ( $comment_ids as $comment_id ) {
    3008                 wp_delete_comment( $comment_id, true );
    3009         }
     3006        $comment_ids = $wpdb->get_col( $wpdb->prepare( "SELECT comment_ID FROM $wpdb->comments WHERE comment_post_ID = %d ORDER BY comment_ID DESC", $postid ) );
     3007        wp_delete_comments( $comment_ids, true );
    30103008
    30113009        wp_defer_comment_counting( false );
    30123010
    function wp_delete_attachment( $post_id, $force_delete = false ) { 
    56105608        wp_defer_comment_counting( true );
    56115609
    56125610        $comment_ids = $wpdb->get_col( $wpdb->prepare( "SELECT comment_ID FROM $wpdb->comments WHERE comment_post_ID = %d", $post_id ) );
    5613         foreach ( $comment_ids as $comment_id ) {
    5614                 wp_delete_comment( $comment_id, true );
    5615         }
     5611        wp_delete_comments( $comment_ids, true );
    56165612
    56175613        wp_defer_comment_counting( false );
    56185614
  • tests/phpunit/tests/comment/query.php

    diff --git tests/phpunit/tests/comment/query.php tests/phpunit/tests/comment/query.php
    index 372fa475c4..027135ec01 100644
    class Tests_Comment_Query extends WP_UnitTestCase { 
    43814381                $this->assertEqualSets( array( $c ), $found );
    43824382        }
    43834383
     4384        /**
     4385         * @ticket 37703
     4386         */
     4387        public function test_delete_comments() {
     4388                $parent1 = self::factory()->comment->create(
     4389                        array(
     4390                                'comment_post_ID'  => self::$post_id,
     4391                                'comment_approved' => '1',
     4392                        )
     4393                );
     4394
     4395                $parent2 = self::factory()->comment->create(
     4396                        array(
     4397                                'comment_post_ID'  => self::$post_id,
     4398                                'comment_approved' => '1',
     4399                                'comment_parent'   => $parent1,
     4400                        )
     4401                );
     4402
     4403                $c1 = self::factory()->comment->create(
     4404                        array(
     4405                                'comment_post_ID'  => self::$post_id,
     4406                                'comment_approved' => '1',
     4407                                'comment_parent'   => $parent2,
     4408                        )
     4409                );
     4410
     4411                $c2 = self::factory()->comment->create(
     4412                        array(
     4413                                'comment_post_ID'  => self::$post_id,
     4414                                'comment_approved' => '1',
     4415                                'comment_parent'   => $parent2,
     4416                        )
     4417                );
     4418
     4419                $c3 = self::factory()->comment->create(
     4420                        array(
     4421                                'comment_post_ID'  => self::$post_id,
     4422                                'comment_approved' => '1',
     4423                                'comment_parent'   => $c2,
     4424                        )
     4425                );
     4426
     4427                $parent3 = self::factory()->comment->create(
     4428                        array(
     4429                                'comment_post_ID'  => self::$post_id,
     4430                                'comment_approved' => '1',
     4431                        )
     4432                );
     4433
     4434                $c4 = self::factory()->comment->create(
     4435                        array(
     4436                                'comment_post_ID'  => self::$post_id,
     4437                                'comment_approved' => '1',
     4438                                'comment_parent'   => $parent3,
     4439                        )
     4440                );
     4441
     4442                self::factory()->comment->create(
     4443                        array(
     4444                                'comment_post_ID'  => self::$post_id,
     4445                                'comment_approved' => '1',
     4446                                'comment_parent'   => $c4,
     4447                        )
     4448                );
     4449
     4450                $c5 = self::factory()->comment->create(
     4451                        array(
     4452                                'comment_post_ID'  => self::$post_id,
     4453                                'comment_approved' => '1',
     4454                        )
     4455                );
     4456
     4457                self::factory()->comment->create(
     4458                        array(
     4459                                'comment_post_ID'  => self::$post_id,
     4460                                'comment_approved' => '1',
     4461                                'comment_parent'   => $c5,
     4462                        )
     4463                );
     4464
     4465                $c6 = self::factory()->comment->create(
     4466                        array(
     4467                                'comment_post_ID'  => self::$post_id,
     4468                                'comment_approved' => '1',
     4469                        )
     4470                );
     4471
     4472                $c7 = self::factory()->comment->create(
     4473                        array(
     4474                                'comment_post_ID'  => self::$post_id,
     4475                                'comment_approved' => '1',
     4476                                'comment_parent'   => $c6,
     4477                        )
     4478                );
     4479
     4480                self::factory()->comment->create(
     4481                        array(
     4482                                'comment_post_ID'  => self::$post_id,
     4483                                'comment_approved' => '1',
     4484                                'comment_parent'   => $c7,
     4485                        )
     4486                );
     4487
     4488                $this->assertTrue( wp_delete_comments( array( $c1, $c2, $c3, $c4, $c5, $c6, $c7 ) ) );
     4489        }
     4490
    43844491        public function test_comment_query_should_be_cached() {
    43854492                global $wpdb;
    43864493