Skip to:
Content

bbPress.org

Changeset 4695


Ignore:
Timestamp:
01/22/2013 04:10:01 AM (14 years ago)
Author:
johnjamesjacoby
Message:

Introduce tool for repairing each forum's sticky topic ID's. Props gawain. Fixes #2157.

File:
1 edited

Legend:

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

    r4690 r4695  
    162162                10 => array( 'bbp-sync-forum-visibility',  __( 'Recalculate private and hidden forums',               'bbpress' ), 'bbp_admin_repair_forum_visibility'         ),
    163163                15 => array( 'bbp-sync-all-topics-forums', __( 'Recalculate last activity in each topic and forum',   'bbpress' ), 'bbp_admin_repair_freshness'                ),
    164                 20 => array( 'bbp-group-forums',           __( 'Repair BuddyPress Group Forum relationships',         'bbpress' ), 'bbp_admin_repair_group_forum_relationship' ),
    165                 25 => array( 'bbp-forum-topics',           __( 'Count topics in each forum',                          'bbpress' ), 'bbp_admin_repair_forum_topic_count'        ),
    166                 30 => array( 'bbp-forum-replies',          __( 'Count replies in each forum',                         'bbpress' ), 'bbp_admin_repair_forum_reply_count'        ),
    167                 35 => array( 'bbp-topic-replies',          __( 'Count replies in each topic',                         'bbpress' ), 'bbp_admin_repair_topic_reply_count'        ),
    168                 40 => array( 'bbp-topic-voices',           __( 'Count voices in each topic',                          'bbpress' ), 'bbp_admin_repair_topic_voice_count'        ),
    169                 45 => array( 'bbp-topic-hidden-replies',   __( 'Count spammed & trashed replies in each topic',       'bbpress' ), 'bbp_admin_repair_topic_hidden_reply_count' ),
    170                 50 => array( 'bbp-user-topics',            __( 'Count topics for each user',                          'bbpress' ), 'bbp_admin_repair_user_topic_count'         ),
    171                 55 => array( 'bbp-user-replies',           __( 'Count replies for each user',                         'bbpress' ), 'bbp_admin_repair_user_reply_count'         ),
    172                 60 => array( 'bbp-user-favorites',         __( 'Remove trashed topics from user favorites',           'bbpress' ), 'bbp_admin_repair_user_favorites'           ),
    173                 65 => array( 'bbp-user-subscriptions',     __( 'Remove trashed topics from user subscriptions',       'bbpress' ), 'bbp_admin_repair_user_subscriptions'       ),
    174                 70 => array( 'bbp-user-role-map',          __( 'Remap existing users to default forum roles',         'bbpress' ), 'bbp_admin_repair_user_roles'               )
     164                20 => array( 'bbp-sync-all-topics-sticky', __( 'Recalculate the sticky replationship of each topic',  'bbpress' ), 'bbp_admin_repair_sticky'                   ),
     165                25 => array( 'bbp-group-forums',           __( 'Repair BuddyPress Group Forum relationships',         'bbpress' ), 'bbp_admin_repair_group_forum_relationship' ),
     166                30 => array( 'bbp-forum-topics',           __( 'Count topics in each forum',                          'bbpress' ), 'bbp_admin_repair_forum_topic_count'        ),
     167                35 => array( 'bbp-forum-replies',          __( 'Count replies in each forum',                         'bbpress' ), 'bbp_admin_repair_forum_reply_count'        ),
     168                40 => array( 'bbp-topic-replies',          __( 'Count replies in each topic',                         'bbpress' ), 'bbp_admin_repair_topic_reply_count'        ),
     169                45 => array( 'bbp-topic-voices',           __( 'Count voices in each topic',                          'bbpress' ), 'bbp_admin_repair_topic_voice_count'        ),
     170                50 => array( 'bbp-topic-hidden-replies',   __( 'Count spammed & trashed replies in each topic',       'bbpress' ), 'bbp_admin_repair_topic_hidden_reply_count' ),
     171                55 => array( 'bbp-user-topics',            __( 'Count topics for each user',                          'bbpress' ), 'bbp_admin_repair_user_topic_count'         ),
     172                60 => array( 'bbp-user-replies',           __( 'Count replies for each user',                         'bbpress' ), 'bbp_admin_repair_user_reply_count'         ),
     173                65 => array( 'bbp-user-favorites',         __( 'Remove trashed topics from user favorites',           'bbpress' ), 'bbp_admin_repair_user_favorites'           ),
     174                70 => array( 'bbp-user-subscriptions',     __( 'Remove trashed topics from user subscriptions',       'bbpress' ), 'bbp_admin_repair_user_subscriptions'       ),
     175                75 => array( 'bbp-user-role-map',          __( 'Remap existing users to default forum roles',         'bbpress' ), 'bbp_admin_repair_user_roles'               )
    175176        );
    176177        ksort( $repair_list );
     
    705706                // If no role map exists, give the default forum role (bbp-participant)
    706707                $new_role = isset( $role_map[$role] ) ? $role_map[$role] : $default_role;
    707                        
     708
    708709                // Get users of this site, limited to 1000
    709710                while ( $users = get_users( array(
     
    830831                        bbp_update_forum( array( 'forum_id' => $forum_id ) );
    831832                }
     833        }
     834
     835        // Complete results
     836        return array( 0, sprintf( $statement, __( 'Complete!', 'bbpress' ) ) );
     837}
     838
     839/**
     840 * Repairs the relationship of sticky topics to the actual parent forum
     841 *
     842 * @since bbPress (r4695)
     843 *
     844 * @uses wpdb::get_col() To run our recount sql queries
     845 * @uses is_wp_error() To check if the executed query returned {@link WP_Error}
     846 * @return array An array of the status code and the message
     847 */
     848function bbp_admin_repair_sticky() {
     849        global $wpdb;
     850
     851        $statement = __( 'Repairing the sticky topic to the parent forum relationships… %s', 'bbpress' );
     852        $result    = __( 'Failed!', 'bbpress' );
     853        $forums    = $wpdb->get_col( "SELECT ID FROM `{$wpdb->posts}` WHERE `post_type` = 'forum';" );
     854
     855        // Bail if no forums found
     856        if ( empty( $forums ) || is_wp_error( $forums ) )
     857                return array( 1, sprintf( $statement, $result ) );
     858
     859        // Loop through forums and get their sticky topics
     860        foreach ( $forums as $forum ) {
     861                $forum_stickies[$forum] = get_post_meta( $forum, '_bbp_sticky_topics', true );
     862        }
     863
     864        // Cleanup
     865        unset( $forums, $forum );
     866
     867        // Loop through each forum with sticky topics
     868        foreach ( $forum_stickies as $forum_id => $stickies ) {
     869
     870                // Skip if no stickies
     871                if ( empty( $stickies ) ) {
     872                        continue;
     873                }
     874
     875                // Loop through each sticky topic
     876                foreach ( $stickies as $id => $topic_id ) {
     877
     878                        // If the topic is not a super sticky, and the forum ID does not
     879                        // match the topic's forum ID, unset the forum's sticky meta.
     880                        if ( ! bbp_is_topic_super_sticky( $topic_id ) && $forum_id != bbp_get_topic_forum_id( $topic_id ) ) {
     881                                unset( $forum_stickies[$forum_id][$id] );
     882                        }
     883                }
     884
     885                // Get sticky topic ID's, or use empty string
     886                $stickers = empty( $forum_stickies[$forum_id] ) ? '' : array_values( $forum_stickies[$forum_id] );
     887
     888                // Update the forum's sticky topics meta
     889                update_post_meta( $forum_id, '_bbp_sticky_topics', $stickers );
    832890        }
    833891
Note: See TracChangeset for help on using the changeset viewer.