Make WordPress Core

Ticket #37703: 37703.unifinished.patch

File 37703.unifinished.patch, 8.0 KB (added by Mte90, 5 years ago)

Unifinished patch

  • src/wp-includes/comment.php

    diff --git src/wp-includes/comment.php src/wp-includes/comment.php
    index b451c5b641..1d8a51c2c5 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                // Move children up a level.
     1406                $children = $wpdb->get_col( $wpdb->prepare( "SELECT comment_ID FROM $wpdb->comments WHERE comment_parent = %d", $comment->comment_ID ) );
     1407                if ( ! empty( $children ) ) {
     1408                        $wpdb->update( $wpdb->comments, array( 'comment_parent' => $comment->comment_parent ), array( 'comment_parent' => $comment->comment_ID ) );
     1409                        clean_comment_cache( $children );
     1410                }
    14081411        }
    14091412
    14101413        // Delete metadata
    function wp_delete_comment( $comment_id, $force_delete = false ) { 
    14421445        return true;
    14431446}
    14441447
     1448/**
     1449 * Trashes or deletes comments.
     1450 *
     1451 * The comments are moved to trash instead of permanently deleted unless trash is
     1452 * disabled, item is already in the trash, or $force_delete is true.
     1453 *
     1454 * The post comment count will be updated if the comments were approved and have a
     1455 * post ID available.
     1456 *
     1457 * @since x.x.x
     1458 *
     1459 * @global wpdb $wpdb WordPress database abstraction object.
     1460 *
     1461 * @param array $comments_id   Comment ID or WP_Comment object.
     1462 * @param bool  $force_delete Whether to bypass trash and force deletion. Default is false.
     1463 * @return bool True on success, false on failure.
     1464 */
     1465function wp_delete_comments( $comments_id, $force_delete = false ) {
     1466        global $wpdb;
     1467        $grandparents = array();
     1468        $ancestors = array();
     1469        if ( empty( $comments_id ) ) {
     1470                return true;
     1471        }
     1472
     1473        // TODO: define a way to sanitize ids
     1474        $comments_string = '(' . implode( ",", $comments_id ) . ')';
     1475        // Determine if a parent comment is being deleted from a comment that is not being deleted
     1476        $parents = $wpdb->get_results( "SELECT comment_parent FROM $wpdb->comments WHERE comment_parent IN $comments_string AND comment_ID NOT IN $comments_string" , ARRAY_N);
     1477        // Determine grandparents
     1478        if ( !empty( $parents ) ) {
     1479                // The next line doesn't work because is an associative arrays and we need only the values
     1480                $parents_string = '(' . implode( ",", $parents ) . ')';
     1481                $grandparents = $wpdb->get_results( "SELECT comment_parent FROM $wpdb->comments WHERE comment_ID NOT IN $parents_string", ARRAY_N );
     1482                // Determine very old ancestors
     1483                if ( !empty( $grandparents ) ) {
     1484                        $grandparents_string = '(' . implode( ",", $grandparents ) . ')';
     1485                        $ancestors = $wpdb->get_results( "SELECT comment_parent FROM $wpdb->comments WHERE comment_ID NOT IN $grandparents_string", ARRAY_N );
     1486                }
     1487        }
     1488
     1489        // TODO: Find grandfathers of this parents and do reparenting before delete them
     1490
     1491        foreach ( $comments_id as $comment_id ) {
     1492                $single_comment = wp_delete_comment( $comment_id, $force_delete, true );
     1493                if ( !$single_comment ) {
     1494                        return false;
     1495                }
     1496        }
     1497
     1498        /**
     1499         * Fires immediately after comments are deleted from the database.
     1500         *
     1501         * @since x.x.x
     1502         *
     1503         * @param int        $comment_id The comment ID.
     1504         */
     1505        do_action( 'deleted_comments', $comments_id );
     1506        return true;
     1507}
     1508
    14451509/**
    14461510 * Moves a comment to the Trash
    14471511 *
  • 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