WordPress.org

Making WordPress.org

Changeset 3903


Ignore:
Timestamp:
09/01/2016 03:15:59 AM (3 years ago)
Author:
jmdodd
Message:

Support Forums: Improve performance.

Use fuzzy numbers for forum totals, as these are constantly changing.

Bound views by the last six months of posts. This can be adjusted later not performant enough.

Location:
sites/trunk/wordpress.org/public_html/wp-content/plugins/support-forums
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • sites/trunk/wordpress.org/public_html/wp-content/plugins/support-forums/inc/class-performance-optimizations.php

    r3895 r3903  
    77    var $term = null;
    88    var $query = null;
     9    var $bound_id = array();
    910
    1011    function __construct() {
     
    4445            }
    4546
    46             // has_topics() uses this by default.
    47             if ( isset( $r['meta_key'] ) && '_bbp_last_active_time' == $r['meta_key'] ) {
    48                 unset( $r['meta_key'] );
    49                 unset( $r['meta_type'] );
    50                 $r['orderby'] = 'ID';
     47            if ( isset( $r['meta_key'] ) ) {
     48                // has_topics() uses this by default.
     49                if ( '_bbp_last_active_time' == $r['meta_key'] ) {
     50                    unset( $r['meta_key'] );
     51                    unset( $r['meta_type'] );
     52                    $r['orderby'] = 'ID';
     53                // Some views use meta key lookups and should only look at known
     54                // open topics.
     55                } elseif ( ! empty( $r['meta_key'] ) ) {
     56                    $r['orderby'] = 'ID';
     57                    add_filter( 'posts_where', array( $this, 'posts_in_six_months' ) );
     58                }
    5159            }
    5260
    5361            // If this is a forum, limit the number of pages we're dealing with.
    54             if ( isset( $r['post_parent'] ) && get_post_type( $r['post_parent'] ) === bbp_get_forum_post_type() ) {
     62            if ( bbp_is_single_forum() && isset( $r['post_parent'] ) && get_post_type( $r['post_parent'] ) === bbp_get_forum_post_type() ) {
    5563                $r['no_found_rows'] = true;
    5664                add_filter( 'bbp_topic_pagination', array( $this, 'forum_pagination' ) );
     
    5967        }
    6068        return $r;
     69    }
     70
     71    public function posts_in_last_six_months( $w ) {
     72        global $wpdb;
     73
     74        $bound_id = $this->get_bound_id( '6 MONTH' );
     75        $w .= $wpdb->prepare( " AND ( $wpdb->posts.ID >= %d )", $bound_id );
     76        return $w;
    6177    }
    6278
     
    7389        // Try SQL.
    7490        if ( ! is_null( $this->query ) ) {
    75             $count = $wpdb->query( $wpdb->prepare( "SELECT COUNT(*) FROM $wpdb->posts WHERE post_parent = %d AND post_type = 'topic' AND post_status = 'publish' LIMIT 1", $this->query['post_parent'] ) );
     91            $count = $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(*) FROM $wpdb->posts WHERE post_parent = %d AND post_type = 'topic' AND post_status IN ( 'publish', 'closed' ) LIMIT 1", $this->query['post_parent'] ) );
    7692            if ( $count ) {
    7793                $r['total'] = $count / bbp_get_topics_per_page();
     94                update_post_meta( $this->query['post_parent'], '_bbp_topic_count', $count );
     95                update_post_meta( $this->query['post_parent'], '_bbp_total_topic_count', $count );
    7896                return $r;
    7997            }
    8098        }
    8199
    82         // If all else fails...
     100        // Give a reasonable default to fall back on.
    83101        $r['total'] = 10;
    84102        return $r;
     
    108126        return $term;
    109127    }
     128
     129    /**
     130     * Get the ID from a topic one year ago so that we can only look at topics
     131     * after that ID.
     132     */
     133    public function get_bound_id( $interval ) {
     134        global $wpdb;
     135
     136        if ( ! in_array( $interval, array( '1 WEEK', '1 MONTH', '6 MONTH', '1 YEAR' ) ) ) {
     137            $interval = '1 WEEK';
     138        }
     139
     140        if ( array_key_exists( $interval, $this->bound_id ) ) {
     141            return $this->bound_id[ $interval ];
     142        }
     143
     144        // Check cache.
     145        $cache_key = str_replace( ' ', '-', $interval );
     146        $cache_group = 'topic-bound-ids';
     147        $bound_id = wp_cache_get( $cache_key, $cache_group );
     148        if ( false === $bound_id ) {
     149
     150            // Use the type_status_date index.
     151            $bound_id = $wpdb->get_var( "
     152                SELECT `ID`
     153                FROM $wpdb->posts
     154                WHERE post_type = 'topic'
     155                    AND post_status IN ( 'publish', 'closed' )
     156                    AND post_date < DATE_SUB( NOW(), INTERVAL $interval )
     157                ORDER BY `ID` DESC
     158                LIMIT 1 " );
     159            // Set the bound id to 1 if there is not a suitable range.
     160            if ( ! $bound_id ) {
     161                $bound_id = 1;
     162            }
     163            $this->bound_id[ $interval ] = $bound_id;
     164
     165            wp_cache_set( $cache_key, $bound_id, $cache_group, 86400 );
     166        }
     167        return $bound_id;
     168    }
    110169}
  • sites/trunk/wordpress.org/public_html/wp-content/plugins/support-forums/inc/class-plugin.php

    r3884 r3903  
    3535        $this->performance = new Performance_Optimizations;
    3636        $this->users       = new Users;
     37        $this->hooks       = new Hooks;
    3738
    3839        // These modifications are specific to https://wordpress.org/support/
    3940        $blog_id = get_current_blog_id();
    4041        if ( $blog_id && defined( 'WPORG_SUPPORT_FORUMS_BLOGID' ) && WPORG_SUPPORT_FORUMS_BLOGID == $blog_id ) {
     42            $this->dropin  = new Dropin;
    4143            $this->themes  = new Theme_Directory_Compat;
    4244            $this->plugins = new Plugin_Directory_Compat;
  • sites/trunk/wordpress.org/public_html/wp-content/plugins/support-forums/support-forums.php

    r3884 r3903  
    1717include( dirname( __FILE__ ) . '/inc/class-plugin.php' );
    1818include( dirname( __FILE__ ) . '/inc/class-users.php' );
     19include( dirname( __FILE__ ) . '/inc/class-dropin.php' );
     20include( dirname( __FILE__ ) . '/inc/class-hooks.php' );
    1921include( dirname( __FILE__ ) . '/inc/class-directory-compat.php' );
    2022include( dirname( __FILE__ ) . '/inc/class-theme-directory-compat.php' );
Note: See TracChangeset for help on using the changeset viewer.