Changeset 3895
- Timestamp:
- 08/31/2016 07:52:23 PM (8 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
sites/trunk/wordpress.org/public_html/wp-content/plugins/support-forums/inc/class-performance-optimizations.php
r3860 r3895 5 5 class Performance_Optimizations { 6 6 7 var $term = null; 8 var $query = null; 9 7 10 function __construct() { 8 11 // Gravatar suppression on lists of topics. 9 add_filter( 'bbp_after_get_topic_author_link_parse_args', array( __CLASS__, 'get_author_link' ) );10 add_filter( 'bbp_after_get_reply_author_link_parse_args', array( __CLASS__, 'get_author_link' ) );12 add_filter( 'bbp_after_get_topic_author_link_parse_args', array( $this, 'get_author_link' ) ); 13 add_filter( 'bbp_after_get_reply_author_link_parse_args', array( $this, 'get_author_link' ) ); 11 14 12 15 // Query simplification. 13 add_filter( 'bbp_after_has_topics_parse_args', array( __CLASS__, 'has_topics' ) );16 add_filter( 'bbp_after_has_topics_parse_args', array( $this, 'has_topics' ) ); 14 17 } 15 18 … … 17 20 * Remove unnecessary Gravatar display on lists of topics. 18 21 */ 19 public staticfunction get_author_link( $r ) {22 public function get_author_link( $r ) { 20 23 if ( ! bbp_is_single_topic() || bbp_is_topic_edit() ) { 21 24 $r['type'] = 'name'; … … 25 28 26 29 /** 27 * Optimize queries for has_topics as much as possible to avoid breaking things.30 * Optimize queries for has_topics as much as possible. 28 31 */ 29 public staticfunction has_topics( $r ) {32 public function has_topics( $r ) { 30 33 /** 31 * Filter view queries so they only look at the last N days of topics. 32 */ 33 if ( bbp_is_single_view() ) { 34 $view = bbp_get_view_id(); 35 // Exclude plugin and theme views from this restriction. 36 // @todo Update date to a reasonable range once we're done importing. 37 if ( ! in_array( $view, array( 'plugin', 'theme', 'review' ) ) ) { 38 $r['date_query'] = array( 'after' => '19 months ago' ); 39 } else { 40 $term = self::get_term(); 41 42 // If there are a lot of results for a single plugin or theme, 43 // order by post_date to avoid an INNER JOIN ON. 44 if ( $term && ! is_wp_error( $term ) && property_exists( $term, 'count' ) ) { 45 if ( $term->count > 10000 ) { 46 unset( $r['meta_key'] ); 47 unset( $r['meta_type'] ); 48 49 $r['orderby'] = 'post_date'; 50 } 51 } 52 } 53 54 /** 55 * Filter forum queries so they are not sorted by the post meta value of 34 * Filter queries so they are not sorted by the post meta value of 56 35 * `_bbp_last_active_time`. This query needs additional optimization 57 36 * to run over large sets of posts. … … 59 38 * - https://bbpress.trac.wordpress.org/ticket/1925 60 39 */ 61 } elseif ( bbp_is_single_forum() ) { 62 unset( $r['meta_key'] ); 63 unset( $r['meta_type'] ); 40 if ( isset( $r['post_type'] ) && 'topic' == $r['post_type'] ) { 41 // Theme and plugin views rely on taxonomy queries. 42 if ( isset( $r['tax_query'] ) ) { 43 return $r; 44 } 64 45 65 // This only works because we don't edit dates on forum topics. 66 $r['orderby'] = 'post_date'; 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'; 51 } 52 53 // 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() ) { 55 $r['no_found_rows'] = true; 56 add_filter( 'bbp_topic_pagination', array( $this, 'forum_pagination' ) ); 57 $this->query = $r; 58 } 67 59 } 60 return $r; 61 } 62 63 public function forum_pagination( $r ) { 64 global $wpdb; 65 66 // Try the stored topic count. 67 $count = get_post_meta( $this->query['post_parent'], '_bbp_topic_count', true ); 68 if ( ! empty( $count ) ) { 69 $r['total'] = $count / bbp_get_topics_per_page(); 70 return $r; 71 } 72 73 // Try SQL. 74 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'] ) ); 76 if ( $count ) { 77 $r['total'] = $count / bbp_get_topics_per_page(); 78 return $r; 79 } 80 } 81 82 // If all else fails... 83 $r['total'] = 10; 68 84 return $r; 69 85 } … … 72 88 * Get the term for a plugin or theme view from query_var. 73 89 */ 74 public static function get_term() { 90 public function get_term() { 91 if ( null !== $this->term ) { 92 return $this->term; 93 } 94 95 $slug = false; 75 96 if ( ! empty( get_query_var( Plugin::get_instance()->plugins->query_var() ) ) ) { 76 97 $slug = Plugin::get_instance()->plugins->slug(); … … 80 101 $tax = Plugin::get_instance()->themes->taxonomy(); 81 102 } 82 $term = get_term( $slug, $tax ); 103 if ( $slug ) { 104 $term = get_term_by( 'slug', $slug, $tax ); 105 } else { 106 return false; 107 } 83 108 return $term; 84 109 }
Note: See TracChangeset
for help on using the changeset viewer.