Skip to:
Content

bbPress.org

Ticket #2653: 2653.3.diff

File 2653.3.diff, 9.6 KB (added by netweb, 7 years ago)
  • src/includes/admin/tools.php

     
    290290                20 => array( 'bbp-sync-all-topics-sticky',   __( 'Recalculate sticky relationship of each topic',     'bbpress' ), 'bbp_admin_repair_sticky'                   ),
    291291                25 => array( 'bbp-sync-all-reply-positions', __( 'Recalculate the position of each reply',            'bbpress' ), 'bbp_admin_repair_reply_menu_order'         ),
    292292                30 => array( 'bbp-group-forums',             __( 'Repair BuddyPress Group Forum relationships',       'bbpress' ), 'bbp_admin_repair_group_forum_relationship' ),
    293                 35 => array( 'bbp-forum-topics',             __( 'Count topics in each forum',                        'bbpress' ), 'bbp_admin_repair_forum_topic_count'        ),
    294                 40 => array( 'bbp-forum-replies',            __( 'Count replies in each forum',                       'bbpress' ), 'bbp_admin_repair_forum_reply_count'        ),
    295                 45 => array( 'bbp-topic-replies',            __( 'Count replies in each topic',                       'bbpress' ), 'bbp_admin_repair_topic_reply_count'        ),
    296                 50 => array( 'bbp-topic-voices',             __( 'Count voices in each topic',                        'bbpress' ), 'bbp_admin_repair_topic_voice_count'        ),
    297                 55 => array( 'bbp-topic-hidden-replies',     __( 'Count spammed & trashed replies in each topic',     'bbpress' ), 'bbp_admin_repair_topic_hidden_reply_count' ),
    298                 60 => array( 'bbp-user-topics',              __( 'Count topics for each user',                        'bbpress' ), 'bbp_admin_repair_user_topic_count'         ),
    299                 65 => array( 'bbp-user-replies',             __( 'Count replies for each user',                       'bbpress' ), 'bbp_admin_repair_user_reply_count'         ),
    300                 70 => array( 'bbp-user-favorites',           __( 'Remove trashed topics from user favorites',         'bbpress' ), 'bbp_admin_repair_user_favorites'           ),
    301                 75 => array( 'bbp-user-topic-subscriptions', __( 'Remove trashed topics from user subscriptions',     'bbpress' ), 'bbp_admin_repair_user_topic_subscriptions' ),
    302                 80 => array( 'bbp-user-forum-subscriptions', __( 'Remove trashed forums from user subscriptions',     'bbpress' ), 'bbp_admin_repair_user_forum_subscriptions' ),
    303                 85 => array( 'bbp-user-role-map',            __( 'Remap existing users to default forum roles',       'bbpress' ), 'bbp_admin_repair_user_roles'               )
     293                35 => array( 'bbp-sync-closed-topics',       __( 'Repair closed topics',                              'bbpress' ), 'bbp_admin_repair_closed_topics'            ),
     294                40 => array( 'bbp-forum-topics',             __( 'Count topics in each forum',                        'bbpress' ), 'bbp_admin_repair_forum_topic_count'        ),
     295                45 => array( 'bbp-forum-replies',            __( 'Count replies in each forum',                       'bbpress' ), 'bbp_admin_repair_forum_reply_count'        ),
     296                50 => array( 'bbp-topic-replies',            __( 'Count replies in each topic',                       'bbpress' ), 'bbp_admin_repair_topic_reply_count'        ),
     297                55 => array( 'bbp-topic-voices',             __( 'Count voices in each topic',                        'bbpress' ), 'bbp_admin_repair_topic_voice_count'        ),
     298                60 => array( 'bbp-topic-hidden-replies',     __( 'Count spammed & trashed replies in each topic',     'bbpress' ), 'bbp_admin_repair_topic_hidden_reply_count' ),
     299                65 => array( 'bbp-user-topics',              __( 'Count topics for each user',                        'bbpress' ), 'bbp_admin_repair_user_topic_count'         ),
     300                70 => array( 'bbp-user-replies',             __( 'Count replies for each user',                       'bbpress' ), 'bbp_admin_repair_user_reply_count'         ),
     301                75 => array( 'bbp-user-favorites',           __( 'Remove trashed topics from user favorites',         'bbpress' ), 'bbp_admin_repair_user_favorites'           ),
     302                80 => array( 'bbp-user-topic-subscriptions', __( 'Remove trashed topics from user subscriptions',     'bbpress' ), 'bbp_admin_repair_user_topic_subscriptions' ),
     303                85 => array( 'bbp-user-forum-subscriptions', __( 'Remove trashed forums from user subscriptions',     'bbpress' ), 'bbp_admin_repair_user_forum_subscriptions' ),
     304                90 => array( 'bbp-user-role-map',            __( 'Remap existing users to default forum roles',       'bbpress' ), 'bbp_admin_repair_user_roles'               )
    304305        );
    305306        ksort( $repair_list );
    306307
     
    12151216}
    12161217
    12171218/**
     1219 * Repair closed topics
     1220 *
     1221 * Closed topics that are missing the postmeta "_bbp_status" key value "publish"
     1222 * result in unexpected behaviour, primarily this would have only occured if you
     1223 * had imported forums from another forum package previous to bbPress v2.6,
     1224 * https://bbpress.trac.wordpress.org/ticket/2577
     1225 *
     1226 * @since bbPress (rXXXX)
     1227 *
     1228 * @uses wpdb::get_col() To run our recount sql queries
     1229 * @uses is_wp_error() To check if the executed query returned {@link WP_Error}
     1230 * @uses bbp_get_topic_post_type() To get the topic post type
     1231 * @uses get_post_meta() To get the sticky topics
     1232 * @uses update_post_meta To update the topics sticky post meta
     1233 * @return array An array of the status code and the message
     1234 */
     1235function bbp_admin_repair_closed_topics() {
     1236        global $wpdb;
     1237
     1238        $statement     = __( 'Repairing closed topics… %s', 'bbpress' );
     1239        $changed       = 0;
     1240        $result        = __( 'Failed!', 'bbpress' );
     1241        $closed_topics = $wpdb->get_col( "SELECT ID FROM `{$wpdb->posts}` WHERE `post_type` = '" . bbp_get_topic_post_type() . "' AND `post_status` = 'closed';" );
     1242
     1243        // Bail if no closed topics found
     1244        if ( empty( $closed_topics ) || is_wp_error( $closed_topics ) )
     1245                return array( 1, sprintf( $statement, $result ) );
     1246
     1247        // Loop through each closed topic
     1248        foreach ( $closed_topics as $closed_topic ) {
     1249
     1250                // Check if the closed topic already has a postmeta _bbp_status value
     1251                $topic_status = get_post_meta( $closed_topic, '_bbp_status', true );
     1252
     1253                // If we don't have a postmeta _bbp_status value
     1254                if( empty($topic_status) ) {
     1255                        update_post_meta($closed_topic, '_bbp_status', 'publish');
     1256                        ++$changed; // Keep a count to display at the end
     1257                }
     1258        }
     1259
     1260        // Cleanup
     1261        unset( $closed_topics, $closed_topic, $topic_status );
     1262
     1263        // Complete results
     1264        $result = sprintf( _n( 'Complete! 1 closed topic repaired.', 'Complete! %s closed topics repaired.', $changed, 'bbpress' ), $changed );
     1265        return array( 0, sprintf( $statement, $result ) );
     1266}
     1267
     1268/**
    12181269 * Recaches the private and hidden forums
    12191270 *
    12201271 * @since bbPress (r4104)
  • src/includes/topics/functions.php

     
    338338
    339339        /** No Errors *************************************************************/
    340340
    341         if ( ! empty( $topic_id ) && ! is_wp_error( $topic_id ) ) {
     341        if ( !empty( $topic_id ) && !is_wp_error( $topic_id ) ) {
    342342
     343                /** Close Check *******************************************************/
     344
     345                // If the topic is closed, close it properly
     346                if ( ( get_post_field( 'post_status', $topic_id ) === bbp_get_closed_status_id() ) || ( $topic_data['post_status'] === bbp_get_closed_status_id() ) ) {
     347
     348                        // Close the topic
     349                        bbp_close_topic( $topic_id );
     350                }
     351
    343352                /** Trash Check *******************************************************/
    344353
    345354                // If the forum is trash, or the topic_status is switched to
     
    27632772 *
    27642773 * @param int $topic_id Topic id
    27652774 * @uses bbp_get_topic() To get the topic
     2775 * @uses get_post_meta() To get the topic status meta
    27662776 * @uses do_action() Calls 'bbp_close_topic' with the topic id
    27672777 * @uses add_post_meta() To add the previous status to a meta
     2778 * @uses post_type_supports() To check if revisions are enabled
     2779 * @uses bbp_get_topic_post_type() To get the topic post type
     2780 * @uses remove_post_type_support() To temporarily remove topic revisions
     2781 * @uses add_post_type_support() To restore topic revisions
    27682782 * @uses wp_update_post() To update the topic with the new status
    27692783 * @uses do_action() Calls 'bbp_opened_topic' with the topic id
    27702784 * @return mixed False or {@link WP_Error} on failure, topic id on success
     
    27772791                return $topic;
    27782792        }
    27792793
    2780         // Bail if already closed
    2781         if ( bbp_get_closed_status_id() === $topic->post_status ) {
     2794        // Get previous topic status meta
     2795        $topic_status = get_post_meta( $topic_id, '_bbp_status', true );
     2796
     2797        // Bail if already closed and topic status meta exists
     2798        if ( bbp_get_closed_status_id() === $topic->post_status && ! empty( $topic_status ) ) {
    27822799                return false;
    27832800        }
    27842801
     2802        // Set status meta public
     2803        $topic_status = bbp_get_public_status_id();
     2804
    27852805        // Execute pre close code
    27862806        do_action( 'bbp_close_topic', $topic_id );
    27872807
    27882808        // Add pre close status
    2789         add_post_meta( $topic_id, '_bbp_status', $topic->post_status );
     2809        add_post_meta( $topic_id, '_bbp_status', $topic_status );
    27902810
    27912811        // Set closed status
    27922812        $topic->post_status = bbp_get_closed_status_id();
    27932813
    2794         // No revisions
    2795         remove_action( 'pre_post_update', 'wp_save_post_revision' );
     2814        // Toggle revisions off as we are not altering content
     2815        if ( post_type_supports( bbp_get_topic_post_type(), 'revisions' ) ) {
     2816                $revisions_removed = true;
     2817                remove_post_type_support( bbp_get_topic_post_type(), 'revisions' );
     2818        }
    27962819
    27972820        // Update topic
    27982821        $topic_id = wp_update_post( $topic );
    27992822
     2823        // Toggle revisions back on
     2824        if ( true === $revisions_removed ) {
     2825                $revisions_removed = false;
     2826                add_post_type_support( bbp_get_topic_post_type(), 'revisions' );
     2827        }
     2828
    28002829        // Execute post close code
    28012830        do_action( 'bbp_closed_topic', $topic_id );
    28022831