WordPress.org

Making WordPress.org

Changeset 5675


Ignore:
Timestamp:
07/17/2017 11:27:44 AM (12 months ago)
Author:
SergeyBiryukov
Message:

Support Forums: Update the necessary meta data when editing a topic created before 2017-07-17, as those topics can have potentially inaccurate data.

See #1971, #2043.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • sites/trunk/wordpress.org/public_html/wp-content/plugins/support-forums/inc/class-dropin.php

    r5674 r5675  
    2626        remove_action( 'bbp_unapproved_reply', 'bbp_update_reply_walker' );
    2727
    28         add_action( 'bbp_trashed_reply',       array( $this, 'bbp_update_reply_walker' ) );
    29         add_action( 'bbp_untrashed_reply',     array( $this, 'bbp_update_reply_walker' ) );
    30         add_action( 'bbp_deleted_reply',       array( $this, 'bbp_update_reply_walker' ) );
    31         add_action( 'bbp_spammed_reply',       array( $this, 'bbp_update_reply_walker' ) );
    32         add_action( 'bbp_unspammed_reply',     array( $this, 'bbp_update_reply_walker' ) );
    33         add_action( 'bbp_approved_reply',      array( $this, 'bbp_update_reply_walker' ) );
    34         add_action( 'bbp_unapproved_reply',    array( $this, 'bbp_update_reply_walker' ) );
     28        add_action( 'bbp_trashed_reply',       array( $this, 'update_reply_topic_meta' ) );
     29        add_action( 'bbp_untrashed_reply',     array( $this, 'update_reply_topic_meta' ) );
     30        add_action( 'bbp_deleted_reply',       array( $this, 'update_reply_topic_meta' ) );
     31        add_action( 'bbp_spammed_reply',       array( $this, 'update_reply_topic_meta' ) );
     32        add_action( 'bbp_unspammed_reply',     array( $this, 'update_reply_topic_meta' ) );
     33        add_action( 'bbp_approved_reply',      array( $this, 'update_reply_topic_meta' ) );
     34        add_action( 'bbp_unapproved_reply',    array( $this, 'update_reply_topic_meta' ) );
     35
     36        add_action( 'bbp_edit_topic',          array( $this, 'update_old_topic_meta' ) );
    3537
    3638        // Avoid bbp_update_topic_walker().
     
    5759     * @param int $reply_id Reply ID.
    5860     */
    59     function bbp_update_reply_walker( $reply_id ) {
    60         // Get the topic ID
     61    function update_reply_topic_meta( $reply_id ) {
    6162        $topic_id = bbp_get_reply_topic_id( $reply_id );
    6263
    63         // Make every effort to get topic id
     64        // Make every effort to get topic ID.
    6465        // https://bbpress.trac.wordpress.org/ticket/2529
    6566        if ( empty( $topic_id ) && ( current_filter() === 'bbp_deleted_reply' ) ) {
     
    6768        }
    6869
    69         // Last reply and active ID's
    7070        bbp_update_topic_last_reply_id( $topic_id );
    7171        bbp_update_topic_last_active_id( $topic_id );
    72 
    73         // Get the last active time
    7472        bbp_update_topic_last_active_time( $topic_id );
    75 
    76         // Counts
    7773        bbp_update_topic_voice_count( $topic_id );
     74    }
     75
     76    /**
     77     * Adjust the total hidden reply count of a topic (hidden includes trashed,
     78     * spammed, pending, and archived replies).
     79     *
     80     * Extends the native bbPress bbp_update_topic_reply_count_hidden() function
     81     * to include 'archived' status.
     82     *
     83     * @see https://bbpress.trac.wordpress.org/ticket/3128
     84     *
     85     * @param int $topic_id    Optional. Topic ID to update.
     86     * @param int $reply_count Optional. Set the reply count manually.
     87     * @return int Topic hidden reply count.
     88     */
     89    function bbp_update_topic_reply_count_hidden( $topic_id = 0, $reply_count = 0 ) {
     90
     91        // If it's a reply, then get the parent (topic id)
     92        $topic_id = bbp_is_reply( $topic_id )
     93            ? bbp_get_reply_topic_id( $topic_id )
     94            : bbp_get_topic_id( $topic_id );
     95
     96        // Get replies of topic
     97        if ( empty( $reply_count ) ) {
     98            $statuses    = array( bbp_get_trash_status_id(), bbp_get_spam_status_id(), bbp_get_pending_status_id(), Moderators::ARCHIVED );
     99            $post_status = "'" . implode( "','", $statuses ) . "'";
     100            $bbp_db      = bbp_db();
     101            $query       = $bbp_db->prepare( "SELECT COUNT(ID) FROM {$bbp_db->posts} WHERE post_parent = %d AND post_status IN ( {$post_status} ) AND post_type = %s", $topic_id, bbp_get_reply_post_type() );
     102            $reply_count = $bbp_db->get_var( $query );
     103        }
     104
     105        $reply_count = (int) $reply_count;
     106
     107        update_post_meta( $topic_id, '_bbp_reply_count_hidden', $reply_count );
     108
     109        // Filter & return
     110        return (int) apply_filters( 'bbp_update_topic_reply_count_hidden', $reply_count, $topic_id );
     111    }
     112
     113    /**
     114     * Update the necessary meta data when editing a topic created before
     115     * 2017-07-17, as those topics can have potentially inaccurate data.
     116     *
     117     * @see https://meta.trac.wordpress.org/ticket/1971
     118     * @see https://meta.trac.wordpress.org/ticket/2043
     119     *
     120     * @param int $topic_id Topic ID.
     121     */
     122    function update_old_topic_meta( $topic_id ) {
     123        // Only run on topics older than 2017-07-17.
     124        if ( get_post_field( 'post_date', $topic_id ) >= '2017-07-17' ) {
     125            return;
     126        }
     127
     128        bbp_update_topic_last_reply_id( $topic_id );
     129        bbp_update_topic_last_active_id( $topic_id );
     130        bbp_update_topic_last_active_time( $topic_id );
     131        bbp_update_topic_voice_count( $topic_id );
     132
     133        bbp_update_topic_reply_count( $topic_id );
     134        $this->bbp_update_topic_reply_count_hidden( $topic_id );
    78135    }
    79136
Note: See TracChangeset for help on using the changeset viewer.