Skip to:
Content

bbPress.org

Changeset 3040


Ignore:
Timestamp:
04/25/2011 08:38:35 AM (15 years ago)
Author:
johnjamesjacoby
Message:

Add forum/topic last activity sync function to recount tool. Fixes #1503. Props Nightgunner5.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/plugin/bbp-admin/bbp-functions.php

    r2993 r3040  
    125125function bbp_recount_list() {
    126126    $recount_list = array(
    127         5  => array( 'bbp-forum-topics',          __( 'Count topics in each forum',                    'bbpress' ), 'bbp_recount_forum_topics'         ),
    128         10 => array( 'bbp-forum-replies',         __( 'Count replies in each forum',                   'bbpress' ), 'bbp_recount_forum_replies'        ),
    129         15 => array( 'bbp-topic-replies',         __( 'Count replies in each topic',                   'bbpress' ), 'bbp_recount_topic_replies'        ),
    130         20 => array( 'bbp-topic-voices',          __( 'Count voices in each topic',                    'bbpress' ), 'bbp_recount_topic_voices'         ),
    131         25 => array( 'bbp-topic-hidden-replies',  __( 'Count spammed & trashed replies in each topic', 'bbpress' ), 'bbp_recount_topic_hidden_replies' ),
    132         30 => array( 'bbp-topics-replied',        __( 'Count replies for each user',                   'bbpress' ), 'bbp_recount_user_topics_replied'  ),
    133         35 => array( 'bbp-clean-favorites',       __( 'Remove trashed topics from user favorites',     'bbpress' ), 'bbp_recount_clean_favorites'      ),
    134         40 => array( 'bbp-clean-subscriptions',   __( 'Remove trashed topics from user subscriptions', 'bbpress' ), 'bbp_recount_clean_subscriptions'  )
    135         //45 => array( 'bbp-topic-tag-count',       __( 'Count tags for every topic',                    'bbpress' ), 'bbp_recount_topic_tags'           ),
    136         //50 => array( 'bbp-tags-tag-count',        __( 'Count topics for every tag',                    'bbpress' ), 'bbp_recount_tag_topics'           ),
    137         //55 => array( 'bbp-tags-delete-empty',     __( 'Delete tags with no topics',                    'bbpress' ), 'bbp_recount_tag_delete_empty'     )
     127        5  => array( 'bbp-forum-topics',           __( 'Count topics in each forum',                        'bbpress' ), 'bbp_recount_forum_topics'         ),
     128        10 => array( 'bbp-forum-replies',          __( 'Count replies in each forum',                       'bbpress' ), 'bbp_recount_forum_replies'        ),
     129        15 => array( 'bbp-topic-replies',          __( 'Count replies in each topic',                       'bbpress' ), 'bbp_recount_topic_replies'        ),
     130        20 => array( 'bbp-topic-voices',           __( 'Count voices in each topic',                        'bbpress' ), 'bbp_recount_topic_voices'         ),
     131        25 => array( 'bbp-topic-hidden-replies',   __( 'Count spammed & trashed replies in each topic',     'bbpress' ), 'bbp_recount_topic_hidden_replies' ),
     132        30 => array( 'bbp-topics-replied',         __( 'Count replies for each user',                       'bbpress' ), 'bbp_recount_user_topics_replied'  ),
     133        35 => array( 'bbp-clean-favorites',        __( 'Remove trashed topics from user favorites',         'bbpress' ), 'bbp_recount_clean_favorites'      ),
     134        40 => array( 'bbp-clean-subscriptions',    __( 'Remove trashed topics from user subscriptions',     'bbpress' ), 'bbp_recount_clean_subscriptions'  ),
     135        //45 => array( 'bbp-topic-tag-count',        __( 'Count tags for every topic',                        'bbpress' ), 'bbp_recount_topic_tags'           ),
     136        //50 => array( 'bbp-tags-tag-count',         __( 'Count topics for every tag',                        'bbpress' ), 'bbp_recount_tag_topics'           ),
     137        //55 => array( 'bbp-tags-delete-empty',      __( 'Delete tags with no topics',                        'bbpress' ), 'bbp_recount_tag_delete_empty'     ),
     138        60 => array( 'bbp-sync-all-topics-forums', __( 'Recalculate last activity in each topic and forum', 'bbpress' ), 'bbp_recount_rewalk'               )
    138139    );
    139140
     
    190191        return array( 1, sprintf( $statement, $result ) );
    191192
    192     $sql = "INSERT INTO `{$wpdb->postmeta}` (`post_id`, `meta_key`, `meta_value`) (SELECT `ID`, '_bbp_voice_count', COUNT(DISTINCT `post_author`) as `meta_value` FROM `{$wpdb->posts}` WHERE `post_type` IN ( '" . bbp_get_topic_post_type() . "', '" . bbp_get_reply_post_type() . "' ) AND `post_status` = 'publish' GROUP BY `post_parent`);";
     193    $sql = "INSERT INTO `{$wpdb->postmeta}` (`post_id`, `meta_key`, `meta_value`) (SELECT `post_parent`, '_bbp_voice_count', COUNT(DISTINCT `post_author`) as `meta_value` FROM `{$wpdb->posts}` WHERE `post_type` = '" . bbp_get_reply_post_type() . "' AND `post_status` = 'publish' GROUP BY `post_parent`);";
    193194    if ( is_wp_error( $wpdb->query( $sql ) ) )
    194195        return array( 2, sprintf( $statement, $result ) );
     
    749750}
    750751
     752/**
     753 * Recaches the last post in every topic and forum
     754 *
     755 * @since bbPress (r3040)
     756 *
     757 * @uses wpdb::query() To run our recount sql queries
     758 * @uses is_wp_error() To check if the executed query returned {@link WP_Error}
     759 * @return array An array of the status code and the message
     760 */
     761function bbp_recount_rewalk() {
     762    global $wpdb;
     763
     764    $statement = __( 'Recomputing latest post in every topic and forum… %s', 'bbpress' );
     765    $result    = __( 'Failed!', 'bbpress' );
     766
     767    // First, delete everything.
     768    if ( is_wp_error( $wpdb->query( "DELETE FROM `$wpdb->postmeta` WHERE `meta_key` IN ( '_bbp_last_reply_id', '_bbp_last_topic_id', '_bbp_last_active_id' );" ) ) )
     769        return array( 1, sprintf( $statement, $result ) );
     770
     771    // Next, give all the topics with replies the ID their last reply.
     772    if ( is_wp_error( $wpdb->query( "INSERT INTO `$wpdb->postmeta` (`post_id`, `meta_key`, `meta_value`)
     773            ( SELECT `topic`.`ID`, '_bbp_last_reply_id', MAX( `reply`.`ID` )
     774            FROM `$wpdb->posts` AS `topic` INNER JOIN `$wpdb->posts` AS `reply` ON `topic`.`ID` = `reply`.`post_parent`
     775            WHERE `reply`.`post_status` IN ( 'publish' ) AND `topic`.`post_type` = 'topic' AND `reply`.`post_type` = 'reply'
     776            GROUP BY `topic`.`ID` );" ) ) )
     777        return array( 2, sprintf( $statement, $result ) );
     778
     779    // For any remaining topics, give a reply ID of 0.
     780    if ( is_wp_error( $wpdb->query( "INSERT INTO `$wpdb->postmeta` (`post_id`, `meta_key`, `meta_value`)
     781            ( SELECT `ID`, '_bbp_last_reply_id', 0
     782            FROM `$wpdb->posts` AS `topic` LEFT JOIN `$wpdb->postmeta` AS `reply`
     783            ON `topic`.`ID` = `reply`.`post_id` AND `reply`.`meta_key` = '_bbp_last_reply_id'
     784            WHERE `reply`.`meta_id` IS NULL AND `topic`.`post_type` = 'topic' );" ) ) )
     785        return array( 3, sprintf( $statement, $result ) );
     786
     787    // Now we give all the forums with topics the ID their last topic.
     788    if ( is_wp_error( $wpdb->query( "INSERT INTO `$wpdb->postmeta` (`post_id`, `meta_key`, `meta_value`)
     789            ( SELECT `forum`.`ID`, '_bbp_last_topic_id', `topic`.`ID`
     790            FROM `$wpdb->posts` AS `forum` INNER JOIN `$wpdb->posts` AS `topic` ON `forum`.`ID` = `topic`.`post_parent`
     791            WHERE `reply`.`post_status` IN ( 'publish' ) AND `forum`.`post_type` = 'forum' AND `topic`.`post_type` = 'topic'
     792            GROUP BY `forum`.`ID` );" ) ) )
     793        return array( 4, sprintf( $statement, $result ) );
     794
     795    // For any remaining forums, give a topic ID of 0.
     796    if ( is_wp_error( $wpdb->query( "INSERT INTO `$wpdb->postmeta` (`post_id`, `meta_key`, `meta_value`)
     797            ( SELECT `ID`, '_bbp_last_topic_id', 0
     798            FROM `$wpdb->posts` AS `forum` LEFT JOIN `$wpdb->postmeta` AS `topic`
     799            ON `forum`.`ID` = `topic`.`post_id` AND `topic`.`meta_key` = '_bbp_last_topic_id'
     800            WHERE `topic`.`meta_id` IS NULL AND `forum`.`post_type` = 'forum' );" ) ) )
     801        return array( 5, sprintf( $statement, $result ) );
     802
     803    // After that, we give all the topics with replies the ID their last reply (again, this time for a different reason).
     804    if ( is_wp_error( $wpdb->query( "INSERT INTO `$wpdb->postmeta` (`post_id`, `meta_key`, `meta_value`)
     805            ( SELECT `topic`.`ID`, '_bbp_last_active_id', MAX( `reply`.`ID` )
     806            FROM `$wpdb->posts` AS `topic` INNER JOIN `$wpdb->posts` AS `reply` ON `topic`.`ID` = `reply`.`post_parent`
     807            WHERE `reply`.`post_status` IN ( 'publish' ) AND `topic`.`post_type` = 'topic' AND `reply`.`post_type` = 'reply'
     808            GROUP BY `topic`.`ID` );" ) ) )
     809        return array( 6, sprintf( $statement, $result ) );
     810
     811    // For any remaining topics, give a reply ID of themself.
     812    if ( is_wp_error( $wpdb->query( "INSERT INTO `$wpdb->postmeta` (`post_id`, `meta_key`, `meta_value`)
     813            ( SELECT `ID`, '_bbp_last_active_id', `ID`
     814            FROM `$wpdb->posts` AS `topic` LEFT JOIN `$wpdb->postmeta` AS `reply`
     815            ON `topic`.`ID` = `reply`.`post_id` AND `reply`.`meta_key` = '_bbp_last_active_id'
     816            WHERE `reply`.`meta_id` IS NULL AND `topic`.`post_type` = 'topic' );" ) ) )
     817        return array( 7, sprintf( $statement, $result ) );
     818
     819    // Give topics with replies their last update time.
     820    if ( is_wp_error( $wpdb->query( "INSERT INTO `$wpdb->postmeta` (`post_id`, `meta_key`, `meta_value`)
     821            ( SELECT `topic`.`ID`, '_bbp_last_active_time', MAX( `reply`.`post_date` )
     822            FROM `$wpdb->posts` AS `topic` INNER JOIN `$wpdb->posts` AS `reply` ON `topic`.`ID` = `reply`.`post_parent`
     823            WHERE `reply`.`post_status` IN ( 'publish' ) AND `topic`.`post_type` = 'topic' AND `reply`.`post_type` = 'reply'
     824            GROUP BY `topic`.`ID` );" ) ) )
     825        return array( 8, sprintf( $statement, $result ) );
     826
     827    // Give topics without replies their last update time.
     828    if ( is_wp_error( $wpdb->query( "INSERT INTO `$wpdb->postmeta` (`post_id`, `meta_key`, `meta_value`)
     829            ( SELECT `ID`, '_bbp_last_active_time', `post_date`
     830            FROM `$wpdb->posts` AS `topic` LEFT JOIN `$wpdb->postmeta` AS `reply`
     831            ON `topic`.`ID` = `reply`.`post_id` AND `reply`.`meta_key` = '_bbp_last_active_time'
     832            WHERE `reply`.`meta_id` IS NULL AND `topic`.`post_type` = 'topic' );" ) ) )
     833        return array( 9, sprintf( $statement, $result ) );
     834
     835    // Forums need to know what their last active item is as well. Now it gets a bit more complex to do in the database.
     836    $forums = $wpdb->get_col( "SELECT `ID` FROM `$wpdb->posts` WHERE `post_type` = 'forum';" );
     837    if ( is_wp_error( $forums ) )
     838        return array( 10, sprintf( $statement, $result ) );
     839
     840    foreach ( $forums as $forum ) {
     841        bbp_update_forum_last_active_id( $forum );
     842        bbp_update_forum_last_active_time( $forum );
     843    }
     844
     845    $result = __( 'Complete!', 'bbpress' );
     846    return array( 0, sprintf( $statement, $result ) );
     847}
     848
    751849?>
Note: See TracChangeset for help on using the changeset viewer.