From ee2113584c89afd31cab1f1691bfd8f11d87b858 Mon Sep 17 00:00:00 2001
From: Vibol Hou <vibol@hou.cc>
Date: Thu, 7 Feb 2013 16:51:03 -0800
Subject: [PATCH] bbPress performance enhancements /
http://bbpress.trac.wordpress.org/ticket/1925
---
.../plugins/bbpress/includes/admin/tools.php | 32 +++++++++++---------
.../plugins/bbpress/includes/common/widgets.php | 3 +-
.../plugins/bbpress/includes/forums/functions.php | 15 ++++-----
.../bbpress/includes/forums/template-tags.php | 2 +-
.../plugins/bbpress/includes/topics/functions.php | 9 +++---
.../bbpress/includes/topics/template-tags.php | 8 ++---
6 files changed, 36 insertions(+), 33 deletions(-)
mode change 100644 => 100755 wp-content/plugins/bbpress/includes/admin/tools.php
mode change 100644 => 100755 wp-content/plugins/bbpress/includes/forums/functions.php
mode change 100644 => 100755 wp-content/plugins/bbpress/includes/forums/template-tags.php
mode change 100644 => 100755 wp-content/plugins/bbpress/includes/replies/functions.php
mode change 100644 => 100755 wp-content/plugins/bbpress/includes/topics/functions.php
diff --git a/wp-content/plugins/bbpress/includes/admin/tools.php b/wp-content/plugins/bbpress/includes/admin/tools.php
old mode 100644
new mode 100755
index ad608ad..6481e92
a
|
b
|
function bbp_admin_repair_freshness() { |
744 | 744 | $statement = __( 'Recomputing latest post in every topic and forum… %s', 'bbpress' ); |
745 | 745 | $result = __( 'Failed!', 'bbpress' ); |
746 | 746 | |
747 | | // First, delete everything. |
| 747 | /* First, delete everything. |
| 748 | We're not using _bbp_last_active_time anymore, but |
| 749 | it makes sense to keep it here to help migrate to the |
| 750 | new data model. |
| 751 | */ |
748 | 752 | 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', '_bbp_last_active_time' );" ) ) ) |
749 | 753 | return array( 1, sprintf( $statement, $result ) ); |
750 | 754 | |
… |
… |
function bbp_admin_repair_freshness() { |
797 | 801 | return array( 7, sprintf( $statement, $result ) ); |
798 | 802 | |
799 | 803 | // Give topics with replies their last update time. |
800 | | if ( is_wp_error( $wpdb->query( "INSERT INTO `$wpdb->postmeta` (`post_id`, `meta_key`, `meta_value`) |
801 | | ( SELECT `topic`.`ID`, '_bbp_last_active_time', MAX( `reply`.`post_date` ) |
802 | | FROM `$wpdb->posts` AS `topic` INNER JOIN `$wpdb->posts` AS `reply` ON `topic`.`ID` = `reply`.`post_parent` |
803 | | WHERE `reply`.`post_status` IN ( '" . bbp_get_public_status_id() . "' ) AND `topic`.`post_type` = 'topic' AND `reply`.`post_type` = 'reply' |
804 | | GROUP BY `topic`.`ID` );" ) ) ) |
805 | | return array( 8, sprintf( $statement, $result ) ); |
| 804 | $sql = $wpdb->prepare( |
| 805 | "UPDATE $wpdb->posts p1 |
| 806 | LEFT JOIN (SELECT post_parent, |
| 807 | MAX(post_date) AS max_date |
| 808 | FROM $wpdb->posts |
| 809 | WHERE post_status = %s AND post_type = 'reply' |
| 810 | GROUP BY post_parent |
| 811 | ) p2 |
| 812 | ON p1.ID = p2.post_parent |
| 813 | SET p1.post_modified = p2.max_date", bbp_get_public_status_id() |
| 814 | ); |
806 | 815 | |
807 | | // Give topics without replies their last update time. |
808 | | if ( is_wp_error( $wpdb->query( "INSERT INTO `$wpdb->postmeta` (`post_id`, `meta_key`, `meta_value`) |
809 | | ( SELECT `ID`, '_bbp_last_active_time', `post_date` |
810 | | FROM `$wpdb->posts` AS `topic` LEFT JOIN `$wpdb->postmeta` AS `reply` |
811 | | ON `topic`.`ID` = `reply`.`post_id` AND `reply`.`meta_key` = '_bbp_last_active_time' |
812 | | WHERE `reply`.`meta_id` IS NULL AND `topic`.`post_type` = 'topic' );" ) ) ) |
813 | | return array( 9, sprintf( $statement, $result ) ); |
| 816 | if ( is_wp_error( $wpdb->query( $sql ) ) ) |
| 817 | return array( 8, sprintf( $statement, $result ) ); |
814 | 818 | |
815 | 819 | // Forums need to know what their last active item is as well. Now it gets a bit more complex to do in the database. |
816 | 820 | $forums = $wpdb->get_col( "SELECT `ID` FROM `$wpdb->posts` WHERE `post_type` = 'forum' and `post_status` != 'auto-draft';" ); |
diff --git a/wp-content/plugins/bbpress/includes/common/widgets.php b/wp-content/plugins/bbpress/includes/common/widgets.php
index 42c0b3a..f0e6c83 100644
a
|
b
|
class BBP_Topics_Widget extends WP_Widget { |
544 | 544 | 'posts_per_page' => $max_shown, |
545 | 545 | 'post_status' => join( ',', array( bbp_get_public_status_id(), bbp_get_closed_status_id() ) ), |
546 | 546 | 'show_stickes' => false, |
547 | | 'meta_key' => '_bbp_last_active_time', |
548 | | 'orderby' => 'meta_value', |
| 547 | 'orderby' => 'modified', |
549 | 548 | 'order' => 'DESC', |
550 | 549 | 'meta_query' => array( bbp_exclude_forum_ids( 'meta_query' ) ) |
551 | 550 | ); |
diff --git a/wp-content/plugins/bbpress/includes/forums/functions.php b/wp-content/plugins/bbpress/includes/forums/functions.php
old mode 100644
new mode 100755
index 8011cda..db1be1b
a
|
b
|
function bbp_update_forum_last_topic_id( $forum_id = 0, $topic_id = 0 ) { |
1102 | 1102 | $post_vars = array( |
1103 | 1103 | 'post_parent' => $forum_id, |
1104 | 1104 | 'post_type' => bbp_get_topic_post_type(), |
1105 | | 'meta_key' => '_bbp_last_active_time', |
1106 | | 'orderby' => 'meta_value', |
| 1105 | 'orderby' => 'modified', |
| 1106 | 'order' => 'DESC', |
1107 | 1107 | 'numberposts' => 1 |
1108 | 1108 | ); |
1109 | 1109 | |
… |
… |
function bbp_update_forum_last_active_id( $forum_id = 0, $active_id = 0 ) { |
1272 | 1272 | * @return bool True on success, false on failure |
1273 | 1273 | */ |
1274 | 1274 | function bbp_update_forum_last_active_time( $forum_id = 0, $new_time = '' ) { |
| 1275 | global $wpdb; |
1275 | 1276 | $forum_id = bbp_get_forum_id( $forum_id ); |
1276 | | |
1277 | 1277 | // Check time and use current if empty |
1278 | 1278 | if ( empty( $new_time ) ) |
1279 | 1279 | $new_time = get_post_field( 'post_date', bbp_get_forum_last_active_id( $forum_id ) ); |
1280 | 1280 | |
1281 | 1281 | // Update only if there is a time |
1282 | | if ( !empty( $new_time ) ) |
1283 | | update_post_meta( $forum_id, '_bbp_last_active_time', $new_time ); |
| 1282 | if ( !empty( $new_time ) ) { |
| 1283 | $wpdb->query($wpdb->prepare("UPDATE $wpdb->posts SET post_modified=%s WHERE ID=%d", $new_time, $forum_id)); |
| 1284 | } |
1284 | 1285 | |
1285 | 1286 | return (int) apply_filters( 'bbp_update_forum_last_active', $new_time, $forum_id ); |
1286 | 1287 | } |
… |
… |
function bbp_forum_query_subforum_ids( $forum_id ) { |
1763 | 1764 | * @return Position change based on sort |
1764 | 1765 | */ |
1765 | 1766 | function _bbp_forum_query_usort_subforum_ids( $a = 0, $b = 0 ) { |
1766 | | $ta = get_post_meta( $a, '_bbp_last_active_time', true ); |
1767 | | $tb = get_post_meta( $b, '_bbp_last_active_time', true ); |
| 1767 | $ta = get_post_field( 'post_modified', $a ); |
| 1768 | $tb = get_post_field( 'post_modified', $b ); |
1768 | 1769 | return ( $ta < $tb ) ? -1 : 1; |
1769 | 1770 | } |
1770 | 1771 | |
diff --git a/wp-content/plugins/bbpress/includes/forums/template-tags.php b/wp-content/plugins/bbpress/includes/forums/template-tags.php
old mode 100644
new mode 100755
index d33b75d..5f34bc0
a
|
b
|
function bbp_forum_last_active_time( $forum_id = 0 ) { |
452 | 452 | function bbp_get_forum_last_active_time( $forum_id = 0 ) { |
453 | 453 | $forum_id = bbp_get_forum_id( $forum_id ); |
454 | 454 | |
455 | | $last_active = get_post_meta( $forum_id, '_bbp_last_active_time', true ); |
| 455 | $last_active = get_post_field( 'post_modified', $forum_id ); |
456 | 456 | if ( empty( $last_active ) ) { |
457 | 457 | $reply_id = bbp_get_forum_last_reply_id( $forum_id ); |
458 | 458 | if ( !empty( $reply_id ) ) { |
diff --git a/wp-content/plugins/bbpress/includes/replies/functions.php b/wp-content/plugins/bbpress/includes/replies/functions.php
old mode 100644
new mode 100755
diff --git a/wp-content/plugins/bbpress/includes/topics/functions.php b/wp-content/plugins/bbpress/includes/topics/functions.php
old mode 100644
new mode 100755
index b122367..4a9276b
a
|
b
|
function bbp_update_topic_last_active_id( $topic_id = 0, $active_id = 0 ) { |
2397 | 2397 | * @return bool True on success, false on failure |
2398 | 2398 | */ |
2399 | 2399 | function bbp_update_topic_last_active_time( $topic_id = 0, $new_time = '' ) { |
2400 | | |
| 2400 | global $wpdb; |
2401 | 2401 | // If it's a reply, then get the parent (topic id) |
2402 | 2402 | if ( bbp_is_reply( $topic_id ) ) |
2403 | 2403 | $topic_id = bbp_get_reply_topic_id( $topic_id ); |
… |
… |
function bbp_update_topic_last_active_time( $topic_id = 0, $new_time = '' ) { |
2409 | 2409 | $new_time = get_post_field( 'post_date', bbp_get_public_child_last_id( $topic_id, bbp_get_reply_post_type() ) ); |
2410 | 2410 | |
2411 | 2411 | // Update only if published |
2412 | | if ( !empty( $new_time ) ) |
2413 | | update_post_meta( $topic_id, '_bbp_last_active_time', $new_time ); |
| 2412 | if ( !empty( $new_time ) ) { |
| 2413 | $wpdb->query($wpdb->prepare("UPDATE $wpdb->posts SET post_modified=%s WHERE ID=%d", $new_time, $topic_id)); |
| 2414 | } |
2414 | 2415 | |
2415 | 2416 | return apply_filters( 'bbp_update_topic_last_active_time', $new_time, $topic_id ); |
2416 | 2417 | } |
… |
… |
function bbp_display_topics_feed_rss2( $topics_query = array() ) { |
3255 | 3256 | <guid><?php bbp_topic_permalink(); ?></guid> |
3256 | 3257 | <title><![CDATA[<?php bbp_topic_title(); ?>]]></title> |
3257 | 3258 | <link><?php bbp_topic_permalink(); ?></link> |
3258 | | <pubDate><?php echo mysql2date('D, d M Y H:i:s +0000', get_post_meta( bbp_get_topic_id(), '_bbp_last_active_time', true ) ); ?></pubDate> |
| 3259 | <pubDate><?php echo mysql2date('D, d M Y H:i:s +0000', get_post_field( 'post_modified', bbp_get_topic_id() ) ); ?></pubDate> |
3259 | 3260 | <dc:creator><?php the_author() ?></dc:creator> |
3260 | 3261 | |
3261 | 3262 | <?php if ( !post_password_required() ) : ?> |
diff --git a/wp-content/plugins/bbpress/includes/topics/template-tags.php b/wp-content/plugins/bbpress/includes/topics/template-tags.php
index eb360fc..6e6d354 100644
a
|
b
|
function bbp_has_topics( $args = '' ) { |
101 | 101 | 'post_type' => bbp_get_topic_post_type(), // Narrow query down to bbPress topics |
102 | 102 | 'post_parent' => $default_post_parent, // Forum ID |
103 | 103 | 'post_status' => $default_post_status, // Post Status |
104 | | 'meta_key' => '_bbp_last_active_time', // Make sure topic has some last activity time |
105 | | 'orderby' => 'meta_value', // 'meta_value', 'author', 'date', 'title', 'modified', 'parent', rand', |
| 104 | 'orderby' => 'modified', // 'meta_value', 'author', 'date', 'title', 'modified', 'parent', rand', |
106 | 105 | 'order' => 'DESC', // 'ASC', 'DESC' |
107 | 106 | 'posts_per_page' => bbp_get_topics_per_page(), // Topics per page |
108 | 107 | 'paged' => bbp_get_paged(), // Page Number |
… |
… |
function bbp_has_topics( $args = '' ) { |
186 | 185 | 'post_type' => bbp_get_topic_post_type(), |
187 | 186 | 'post_parent' => 'any', |
188 | 187 | 'post_status' => $default_post_status, |
189 | | 'meta_key' => '_bbp_last_active_time', |
190 | | 'orderby' => 'meta_value', |
| 188 | 'orderby' => 'modified', |
191 | 189 | 'order' => 'DESC', |
192 | 190 | 'include' => $stickies |
193 | 191 | ); |
… |
… |
function bbp_topic_last_active_time( $topic_id = 0 ) { |
1647 | 1645 | $topic_id = bbp_get_topic_id( $topic_id ); |
1648 | 1646 | |
1649 | 1647 | // Try to get the most accurate freshness time possible |
1650 | | $last_active = get_post_meta( $topic_id, '_bbp_last_active_time', true ); |
| 1648 | $last_active = get_post_field( 'post_modified', $topic_id ); |
1651 | 1649 | if ( empty( $last_active ) ) { |
1652 | 1650 | $reply_id = bbp_get_topic_last_reply_id( $topic_id ); |
1653 | 1651 | if ( !empty( $reply_id ) ) { |