Skip to:
Content

bbPress.org

Changeset 7234


Ignore:
Timestamp:
02/17/2022 08:04:32 PM (3 years ago)
Author:
johnjamesjacoby
Message:

Moderation: use the correct post_status when untrashing a topic/reply.

This change fixes a bug/regression (since WordPress 5.6.0) that was causing untrashed topics & replies to use an unintended post_status value.

It fixes it by adding a new function ('bbp_fix_untrash_post_status()') and hooking it to the wp_untrash_post_status filter in WordPress, and overriding the results as needed.

Props r-a-y.

In branches/2.6 for 2.6.10. Fixes #3433.

Location:
branches/2.6
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • branches/2.6/src/includes/common/functions.php

    r7151 r7234  
    186186
    187187    return $data;
     188}
     189
     190/**
     191 * Use the previous status when restoring a topic or reply.
     192 *
     193 * Fixes an issue since WordPress 5.6.0. See
     194 * {@link https://bbpress.trac.wordpress.org/ticket/3433}.
     195 *
     196 * @since 2.6.10 bbPress (r7233)
     197 *
     198 * @param string $new_status      New status to use when untrashing. Default: 'draft'
     199 * @param int    $post_id         Post ID
     200 * @param string $previous_status Previous post status from '_wp_trash_meta_status' meta key. Default: 'pending'
     201 */
     202function bbp_fix_untrash_post_status( $new_status = 'draft', $post_id = 0, $previous_status = 'pending' ) {
     203
     204    // Bail if not Topic or Reply
     205    if ( ! bbp_is_topic( $post_id ) && ! bbp_is_reply( $post_id ) ) {
     206        return $new_status;
     207    }
     208
     209    // Prefer the previous status, falling back to the new status
     210    $retval = ! empty( $previous_status )
     211        ? $previous_status
     212        : $new_status;
     213
     214    return $retval;
    188215}
    189216
  • branches/2.6/src/includes/core/filters.php

    r7124 r7234  
    5252// Fix post author id for anonymous posts (set it back to 0) when the post status is changed
    5353add_filter( 'wp_insert_post_data', 'bbp_fix_post_author', 30, 2 );
     54
     55// Fix untrash post status after a topic or reply is re-instated
     56add_filter( 'wp_untrash_post_status', 'bbp_fix_untrash_post_status', 10, 3 );
    5457
    5558// Force comments_status on bbPress post types
  • branches/2.6/tests/phpunit/testcases/topics/functions/topic.php

    r6330 r7234  
    424424    /**
    425425     * @covers ::bbp_untrash_topic
    426      * @todo   Implement test_bbp_untrash_topic().
    427426     */
    428427    public function test_bbp_untrash_topic() {
    429         // Remove the following lines when you implement this test.
    430         $this->markTestIncomplete(
    431             'This test has not been implemented yet.'
    432         );
     428        $f = $this->factory->forum->create();
     429        $t = $this->factory->topic->create( array(
     430            'post_parent' => $f,
     431            'topic_meta' => array(
     432                'forum_id' => $f,
     433            ),
     434        ) );
     435
     436        wp_trash_post( $t );
     437
     438        wp_untrash_post( $t );
     439
     440        $this->assertTrue( 'publish' === get_post_status( $t ) );
    433441    }
    434442
    435443    /**
    436444     * @covers ::bbp_untrash_topic_replies
    437      * @todo   Implement test_bbp_untrash_topic_replies().
    438445     */
    439446    public function test_bbp_untrash_topic_replies() {
    440         // Remove the following lines when you implement this test.
    441         $this->markTestIncomplete(
    442             'This test has not been implemented yet.'
    443         );
     447        $f = $this->factory->forum->create();
     448        $t = $this->factory->topic->create( array(
     449            'post_parent' => $f,
     450            'topic_meta' => array(
     451                'forum_id' => $f,
     452            ),
     453        ) );
     454        $r = $this->factory->reply->create_many( 2, array(
     455            'post_parent' => $t,
     456            'reply_meta' => array(
     457                'forum_id' => $f,
     458                'topic_id' => $t,
     459            ),
     460        ) );
     461
     462        wp_trash_post( $t );
     463
     464        wp_untrash_post( $t );
     465
     466        $expected = [];
     467        $expected[] = get_post_status( $r[0] );
     468        $expected[] = get_post_status( $r[1] );
     469
     470        $this->assertSame( [ 'publish', 'publish' ], $expected );
    444471    }
    445472
Note: See TracChangeset for help on using the changeset viewer.