Skip to:
Content

bbPress.org

Ticket #2876: latest_activity.patch

File latest_activity.patch, 8.8 KB (added by tharsheblows, 9 years ago)

sample fix for latest activity tool

  • includes/admin/tools.php

    diff --git a/includes/admin/tools.php b/includes/admin/tools.php
    index 3803aab..7e22df0 100644
    a b function bbp_admin_repair_forum_reply_count() { 
    500500        if ( is_wp_error( $wpdb->query( $sql_delete ) ) ) {
    501501                return array( 1, sprintf( $statement, $result ) );
    502502        }
    503  
     503
    504504        // Recalculate the metas key _bbp_reply_count and _bbp_total_reply_count for each forum
    505505        $forums = get_posts( array( 'post_type' => bbp_get_forum_post_type(), 'numberposts' => -1 ) );
    506506        if ( !empty( $forums ) ) {
    function bbp_admin_repair_freshness() { 
    864864                return array( 1, sprintf( $statement, $result ) );
    865865
    866866        // Next, give all the topics with replies the ID their last reply.
    867         if ( is_wp_error( $wpdb->query( "INSERT INTO `$wpdb->postmeta` (`post_id`, `meta_key`, `meta_value`)
    868                         ( SELECT `topic`.`ID`, '_bbp_last_reply_id', MAX( `reply`.`ID` )
    869                         FROM `$wpdb->posts` AS `topic` INNER JOIN `$wpdb->posts` AS `reply` ON `topic`.`ID` = `reply`.`post_parent`
    870                         WHERE `reply`.`post_status` IN ( '" . bbp_get_public_status_id() . "' ) AND `topic`.`post_type` = 'topic' AND `reply`.`post_type` = 'reply'
    871                         GROUP BY `topic`.`ID` );" ) ) )
     867        if ( is_wp_error( $wpdb->query( "INSERT INTO `$wpdb->postmeta` (`post_id`, `meta_key`, `meta_value`) (
     868                SELECT `post_parent`, '_bbp_last_reply_id', `ID` FROM `wp_posts` WHERE `post_date` IN
     869                        (SELECT MAX( `reply`.`post_date`) as rDate
     870                        FROM `wp_posts` AS `topic`
     871                                INNER JOIN `wp_posts` AS `reply`
     872                                ON `topic`.`ID` = `reply`.`post_parent`
     873                                WHERE `reply`.`post_status` IN ( 'publish' )
     874                                AND `topic`.`post_type` = 'topic'
     875                                AND `reply`.`post_type` = 'reply'
     876                        GROUP BY `topic`.`ID`) );" ) ) )
    872877                return array( 2, sprintf( $statement, $result ) );
    873878
    874879        // For any remaining topics, give a reply ID of 0.
    function bbp_admin_repair_freshness() { 
    880885                return array( 3, sprintf( $statement, $result ) );
    881886
    882887        // Now we give all the forums with topics the ID their last topic.
    883         if ( is_wp_error( $wpdb->query( "INSERT INTO `$wpdb->postmeta` (`post_id`, `meta_key`, `meta_value`)
    884                         ( SELECT `forum`.`ID`, '_bbp_last_topic_id', `topic`.`ID`
    885                         FROM `$wpdb->posts` AS `forum` INNER JOIN `$wpdb->posts` AS `topic` ON `forum`.`ID` = `topic`.`post_parent`
    886                         WHERE `topic`.`post_status` IN ( '" . bbp_get_public_status_id() . "' ) AND `forum`.`post_type` = 'forum' AND `topic`.`post_type` = 'topic'
    887                         GROUP BY `forum`.`ID` );" ) ) )
     888        // This gives the last topic created vs the last topic updated.
     889        if ( is_wp_error( $wpdb->query( "INSERT INTO `$wpdb->postmeta` (`post_id`, `meta_key`, `meta_value`) (
     890                SELECT `post_parent` as fID,  '_bbp_last_topic_id', `ID` as tID FROM `wp_posts` WHERE `post_date` IN (
     891            SELECT MAX( `topic`.`post_date`) as tDate
     892                        FROM `wp_posts` AS `forum`
     893                                INNER JOIN `wp_posts` AS `topic`
     894                                ON `forum`.`ID` = `topic`.`post_parent`
     895                                WHERE `topic`.`post_status` IN ( 'publish' )
     896                                AND `forum`.`post_type` = 'forum'
     897                                AND `topic`.`post_type` = 'topic'
     898                        GROUP BY `forum`.`ID` ) );" ) ) )
     899
    888900                return array( 4, sprintf( $statement, $result ) );
    889901
    890902        // For any remaining forums, give a topic ID of 0.
    function bbp_admin_repair_freshness() { 
    896908                return array( 5, sprintf( $statement, $result ) );
    897909
    898910        // After that, we give all the topics with replies the ID their last reply (again, this time for a different reason).
    899         if ( is_wp_error( $wpdb->query( "INSERT INTO `$wpdb->postmeta` (`post_id`, `meta_key`, `meta_value`)
    900                         ( SELECT `topic`.`ID`, '_bbp_last_active_id', MAX( `reply`.`ID` )
    901                         FROM `$wpdb->posts` AS `topic` INNER JOIN `$wpdb->posts` AS `reply` ON `topic`.`ID` = `reply`.`post_parent`
    902                         WHERE `reply`.`post_status` IN ( '" . bbp_get_public_status_id() . "' ) AND `topic`.`post_type` = 'topic' AND `reply`.`post_type` = 'reply'
    903                         GROUP BY `topic`.`ID` );" ) ) )
     911        if ( is_wp_error( $wpdb->query( "INSERT INTO `$wpdb->postmeta` (`post_id`, `meta_key`, `meta_value`)(
     912                        SELECT `post_parent`, '_bbp_last_active_id', `ID` FROM `wp_posts` WHERE `post_date` IN
     913                        (SELECT MAX( `reply`.`post_date`) as rDate
     914                        FROM `wp_posts` AS `topic`
     915                                INNER JOIN `wp_posts` AS `reply`
     916                                ON `topic`.`ID` = `reply`.`post_parent`
     917                                WHERE `reply`.`post_status` IN ( 'publish' )
     918                                AND `topic`.`post_type` = 'topic'
     919                                AND `reply`.`post_type` = 'reply'
     920                        GROUP BY `topic`.`ID`) );" ) ) )
    904921                return array( 6, sprintf( $statement, $result ) );
    905922
    906923        // For any remaining topics, give a reply ID of themself.
    function bbp_admin_repair_reply_menu_order() { 
    11491166
    11501167        // Delete cases where `_bbp_reply_to` was accidentally set to itself
    11511168        if ( is_wp_error( $wpdb->query( "DELETE FROM `{$wpdb->postmeta}` WHERE `meta_key` = '_bbp_reply_to' AND `post_id` = `meta_value`;" ) ) ) {
    1152                 return array( 1, sprintf( $statement, $result ) ); 
     1169                return array( 1, sprintf( $statement, $result ) );
    11531170        }
    11541171
    11551172        // Post type
  • includes/common/functions.php

    diff --git a/includes/common/functions.php b/includes/common/functions.php
    index bd5c481..69dcb4e 100644
    a b function bbp_get_public_child_ids( $parent_id = 0, $post_type = 'post' ) { 
    15961596                // Join post statuses together
    15971597                $post_status = "'" . implode( "', '", $post_status ) . "'";
    15981598
    1599                 $child_ids = $wpdb->get_col( $wpdb->prepare( "SELECT ID FROM {$wpdb->posts} WHERE post_parent = %d AND post_status IN ( {$post_status} ) AND post_type = '%s' ORDER BY ID DESC;", $parent_id, $post_type ) );
     1599                $child_ids = $wpdb->get_col( $wpdb->prepare( "SELECT ID FROM {$wpdb->posts} WHERE post_parent = %d AND post_status IN ( {$post_status} ) AND post_type = '%s' ORDER BY post_date DESC;", $parent_id, $post_type ) );
    16001600                wp_cache_set( $cache_id, $child_ids, 'bbpress_posts' );
    16011601        }
    16021602
    function bbp_get_all_child_ids( $parent_id = 0, $post_type = 'post' ) { 
    16591659                // Join post statuses together
    16601660                $post_status = "'" . implode( "', '", $post_status ) . "'";
    16611661
    1662                 $child_ids = $wpdb->get_col( $wpdb->prepare( "SELECT ID FROM {$wpdb->posts} WHERE post_parent = %d AND post_status IN ( {$post_status} ) AND post_type = '%s' ORDER BY ID DESC;", $parent_id, $post_type ) );
     1662                $child_ids = $wpdb->get_col( $wpdb->prepare( "SELECT ID FROM {$wpdb->posts} WHERE post_parent = %d AND post_status IN ( {$post_status} ) AND post_type = '%s' ORDER BY post_date DESC;", $parent_id, $post_type ) );
    16631663                wp_cache_set( $cache_id, $child_ids, 'bbpress_posts' );
    16641664        }
    16651665
  • includes/forums/functions.php

    diff --git a/includes/forums/functions.php b/includes/forums/functions.php
    index 050791d..f92d863 100644
    a b function bbp_update_forum_last_reply_id( $forum_id = 0, $reply_id = 0 ) { 
    12361236
    12371237                // If this forum has topics...
    12381238                $topic_ids = bbp_forum_query_topic_ids( $forum_id );
     1239
    12391240                if ( !empty( $topic_ids ) ) {
    12401241
    12411242                        // ...get the most recent reply from those topics...
    12421243                        $reply_id = bbp_forum_query_last_reply_id( $forum_id, $topic_ids );
    12431244
    1244                         // ...and compare it to the most recent topic id...
    1245                         $reply_id = ( $reply_id > max( $topic_ids ) ) ? $reply_id : max( $topic_ids );
     1245                        // ...and compare it to the most recent topic id... (most recent refers to date not ID)
     1246                        // $reply_id = ( $reply_id > max( $topic_ids ) ) ? $reply_id : max( $topic_ids );
    12461247                }
    12471248        }
    12481249
    function bbp_update_forum_last_active_id( $forum_id = 0, $active_id = 0 ) { 
    13021303                $topic_ids = bbp_forum_query_topic_ids( $forum_id );
    13031304                if ( !empty( $topic_ids ) ) {
    13041305                        $active_id = bbp_forum_query_last_reply_id( $forum_id, $topic_ids );
    1305                         $active_id = $active_id > max( $topic_ids ) ? $active_id : max( $topic_ids );
     1306                        //$active_id = $active_id > max( $topic_ids ) ? $active_id : max( $topic_ids );
    13061307
    13071308                // Forum has no topics
    13081309                } else {
    function bbp_forum_query_last_reply_id( $forum_id, $topic_ids = 0 ) { 
    19331934        $cache_id = 'bbp_get_forum_' . $forum_id . '_reply_id';
    19341935        $reply_id = (int) wp_cache_get( $cache_id, 'bbpress_posts' );
    19351936
    1936         if ( false === $reply_id ) {
     1937        if ( 0 === $reply_id  || false === $reply_id ) {
    19371938
    19381939                if ( empty( $topic_ids ) ) {
    19391940                        $topic_ids = bbp_forum_query_topic_ids( $forum_id );
    19401941                }
    19411942
    19421943                if ( !empty( $topic_ids ) ) {
     1944
    19431945                        $topic_ids = implode( ',', wp_parse_id_list( $topic_ids ) );
    1944                         $reply_id  = (int) $wpdb->get_var( $wpdb->prepare( "SELECT ID FROM {$wpdb->posts} WHERE post_parent IN ( {$topic_ids} ) AND post_status = '%s' AND post_type = '%s' ORDER BY ID DESC LIMIT 1;", bbp_get_public_status_id(), bbp_get_reply_post_type() ) );
     1946                        $reply_id  = (int) $wpdb->get_var( $wpdb->prepare( "SELECT ID FROM {$wpdb->posts} WHERE post_parent IN ( {$topic_ids} ) AND post_status = '%s' AND post_type = '%s' ORDER BY post_date DESC LIMIT 1;", bbp_get_public_status_id(), bbp_get_reply_post_type() ) );
    19451947                        wp_cache_set( $cache_id, $reply_id, 'bbpress_posts' ); // May be (int) 0
     1948
    19461949                } else {
    19471950                        wp_cache_set( $cache_id, '0', 'bbpress_posts' );
    19481951                }