Skip to:
Content

bbPress.org

Changeset 5668


Ignore:
Timestamp:
03/28/2015 04:46:00 AM (10 years ago)
Author:
netweb
Message:

Tools: Introduce "Repair closed topics tool" bbp_admin_repair_closed_topics()

See #2653

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/includes/admin/tools.php

    r5637 r5668  
    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 );
     
    12131214    // Complete results
    12141215    return array( 0, sprintf( $statement, __( 'Complete!', 'bbpress' ) ) );
     1216}
     1217
     1218/**
     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 (r5668)
     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 closed topic status meta
     1232 * @uses update_post_meta To update the topics closed status 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        = __( 'No closed topics to repair.', '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 ) );
    12151266}
    12161267
Note: See TracChangeset for help on using the changeset viewer.