| @@ -2,8 +2,60 @@ | ||
| 2 | 2 | if ( ! class_exists( 'bbPress' ) ) { |
| 3 | 3 | return; |
| 4 | 4 | } |
| 5 | 5 | |
| 6 | +if ( ! function_exists( 'forumax_admin_reply_stats' ) ) { | |
| 7 | + /** | |
| 8 | + * Reply counts and pending reply IDs for many topics in two queries. | |
| 9 | + * | |
| 10 | + * Replaces per-topic get_children()/WP_Query calls, which made this screen | |
| 11 | + * run several queries per topic on large forums. | |
| 12 | + * | |
| 13 | + * @param int[] $topic_ids Topic IDs. | |
| 14 | + * @return array{total: array<int,int>, pending: array<int,int[]>} | |
| 15 | + */ | |
| 16 | + function forumax_admin_reply_stats( array $topic_ids ) { | |
| 17 | + global $wpdb; | |
| 18 | + | |
| 19 | + $stats = [ | |
| 20 | + 'total' => [], | |
| 21 | + 'pending' => [], | |
| 22 | + ]; | |
| 23 | + $topic_ids = array_values( array_unique( array_filter( array_map( 'intval', $topic_ids ) ) ) ); | |
| 24 | + | |
| 25 | + foreach ( array_chunk( $topic_ids, 500 ) as $chunk ) { | |
| 26 | + $placeholders = implode( ',', array_fill( 0, count( $chunk ), '%d' ) ); | |
| 27 | + $args = array_merge( [ bbp_get_reply_post_type() ], $chunk ); | |
| 28 | + | |
| 29 | + // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.PreparedSQL.InterpolatedNotPrepared | |
| 30 | + $rows = $wpdb->get_results( $wpdb->prepare( "SELECT ID, post_parent, post_status FROM {$wpdb->posts} WHERE post_type = %s AND post_status IN ( 'publish', 'draft', 'pending' ) AND post_parent IN ( $placeholders ) ORDER BY post_date DESC", $args ) ); | |
| 31 | + | |
| 32 | + foreach ( $rows as $row ) { | |
| 33 | + $parent = (int) $row->post_parent; | |
| 34 | + $stats['total'][ $parent ] = ( $stats['total'][ $parent ] ?? 0 ) + 1; | |
| 35 | + | |
| 36 | + if ( 'pending' === $row->post_status ) { | |
| 37 | + $stats['pending'][ $parent ][] = (int) $row->ID; | |
| 38 | + } | |
| 39 | + } | |
| 40 | + } | |
| 41 | + | |
| 42 | + // Pending reply excerpts and each topic's freshness link are rendered, | |
| 43 | + // so load those posts together instead of one query each. | |
| 44 | + $prime_ids = empty( $stats['pending'] ) ? [] : array_merge( ...array_values( $stats['pending'] ) ); | |
| 45 | + foreach ( $topic_ids as $topic_id ) { | |
| 46 | + $prime_ids[] = (int) get_post_meta( $topic_id, '_bbp_last_active_id', true ); | |
| 47 | + } | |
| 48 | + $prime_ids = array_filter( $prime_ids ); | |
| 49 | + | |
| 50 | + if ( ! empty( $prime_ids ) ) { | |
| 51 | + _prime_post_caches( $prime_ids, false, true ); | |
| 52 | + } | |
| 53 | + | |
| 54 | + return $stats; | |
| 55 | + } | |
| 56 | +} | |
| 57 | + | |
| 6 | 58 | $parent_forums = []; |
| 7 | 59 | $fcount = wp_count_posts( bbp_get_forum_post_type() ); |
| 8 | 60 | $forum_count = (int) ( $fcount->publish + $fcount->hidden + $fcount->spam ); |
| 9 | 61 | ?> |
| @@ -57,15 +109,12 @@ | ||
| 57 | 109 | $count_solved = 0; |
| 58 | 110 | $count_unsolved = 0; |
| 59 | 111 | $count_trash = 0; |
| 60 | 112 | |
| 113 | + $reply_stats = forumax_admin_reply_stats( wp_list_pluck( $children->posts, 'ID' ) ); | |
| 114 | + | |
| 61 | 115 | while ( $children->have_posts() ) : $children->the_post(); |
| 62 | 116 | $topic_id = get_the_ID(); |
| 63 | - $replies = get_children( [ | |
| 64 | - 'post_parent' => $topic_id, | |
| 65 | - 'post_type' => bbp_get_reply_post_type(), | |
| 66 | - 'post_status' => [ 'publish', 'draft', 'pending' ], | |
| 67 | - ] ); | |
| 68 | 117 | |
| 69 | 118 | // Count open/closed topics. |
| 70 | 119 | if ( bbp_is_topic_closed( $topic_id ) ) { |
| 71 | 120 | $count_closed++; |
| @@ -78,9 +127,9 @@ | ||
| 78 | 127 | $count_hidden++; |
| 79 | 128 | } |
| 80 | 129 | |
| 81 | 130 | // Replies count. |
| 82 | - if ( 0 == count( $replies ) ) { | |
| 131 | + if ( empty( $reply_stats['total'][ $topic_id ] ) ) { | |
| 83 | 132 | $count_no_reply++; |
| 84 | 133 | } |
| 85 | 134 | |
| 86 | 135 | // Count solved. |
| @@ -92,24 +141,19 @@ | ||
| 92 | 141 | } |
| 93 | 142 | endwhile; |
| 94 | 143 | wp_reset_postdata(); |
| 95 | 144 | |
| 145 | + // Count trashed topics without loading them. | |
| 96 | 146 | $trash_topic = new WP_Query( [ |
| 97 | - 'post_parent' => $item, | |
| 98 | - 'post_type' => bbp_get_topic_post_type(), | |
| 99 | - 'posts_per_page' => -1, | |
| 100 | - 'post_status' => [ 'trash' ], | |
| 147 | + 'post_parent' => $item, | |
| 148 | + 'post_type' => bbp_get_topic_post_type(), | |
| 149 | + 'posts_per_page' => 1, | |
| 150 | + 'post_status' => [ 'trash' ], | |
| 151 | + 'fields' => 'ids', | |
| 152 | + 'update_post_meta_cache' => false, | |
| 153 | + 'update_post_term_cache' => false, | |
| 101 | 154 | ] ); |
| 102 | - | |
| 103 | - while ( $trash_topic->have_posts() ) : | |
| 104 | - $trash_topic->the_post(); | |
| 105 | - $trash_topic_id = get_the_ID(); | |
| 106 | - // Count trash. | |
| 107 | - if ( bbp_is_topic_trash( $trash_topic_id ) ) { | |
| 108 | - $count_trash++; | |
| 109 | - } | |
| 110 | - endwhile; | |
| 111 | - wp_reset_postdata(); | |
| 155 | + $count_trash = (int) $trash_topic->found_posts; | |
| 112 | 156 | ?> |
| 113 | 157 | <div class="easydocs-tab <?php echo esc_attr( $active ); ?>" id="tab-<?php echo esc_attr( $item ); ?>"> |
| 114 | 158 | |
| 115 | 159 | <!-- Tab filters. --> |