| @@ -186,20 +186,41 @@ | ||
| 186 | 186 | $topic_ids = []; |
| 187 | 187 | $read_topics = $this->get_read_topics(); |
| 188 | 188 | if( ! empty( $read_topics ) && is_array( $read_topics ) ) { |
| 189 | 189 | $last_read_postid = $this->get_all_read( 'post' ); |
| 190 | + | |
| 191 | + // First pass: filter out topics already covered by all_read | |
| 192 | + $topics_to_check = []; | |
| 190 | 193 | foreach( $read_topics as $topicid => $postid ) { |
| 191 | 194 | if( $last_read_postid && (int) $postid <= $last_read_postid ) { |
| 192 | 195 | unset( $read_topics[ $topicid ] ); |
| 193 | 196 | } else { |
| 194 | - $current_last_postid = wpforo_topic( $topicid, 'last_post' ); | |
| 195 | - if( $current_last_postid ) { | |
| 196 | - if( (int) $current_last_postid <= (int) $postid ) { | |
| 197 | - $topic_ids[] = $topicid; | |
| 198 | - } | |
| 199 | - } else { | |
| 197 | + $topics_to_check[ $topicid ] = $postid; | |
| 198 | + } | |
| 199 | + } | |
| 200 | + | |
| 201 | + // Batch fetch last_post for all topics in a single query | |
| 202 | + $current_last_posts = []; | |
| 203 | + if( ! empty( $topics_to_check ) ) { | |
| 204 | + $ids_sql = implode( ',', array_map( 'intval', array_keys( $topics_to_check ) ) ); | |
| 205 | + $rows = WPF()->db->get_results( | |
| 206 | + "SELECT `topicid`, `last_post` FROM `" . WPF()->tables->topics . "` WHERE `topicid` IN (" . $ids_sql . ")", | |
| 207 | + ARRAY_A | |
| 208 | + ); | |
| 209 | + foreach( $rows as $row ) { | |
| 210 | + $current_last_posts[ (int) $row['topicid'] ] = (int) $row['last_post']; | |
| 211 | + } | |
| 212 | + } | |
| 213 | + | |
| 214 | + // Second pass: compare using batch-fetched data | |
| 215 | + foreach( $topics_to_check as $topicid => $postid ) { | |
| 216 | + $current_last_postid = isset( $current_last_posts[ $topicid ] ) ? $current_last_posts[ $topicid ] : 0; | |
| 217 | + if( $current_last_postid ) { | |
| 218 | + if( (int) $current_last_postid <= (int) $postid ) { | |
| 200 | 219 | $topic_ids[] = $topicid; |
| 201 | 220 | } |
| 221 | + } else { | |
| 222 | + $topic_ids[] = $topicid; | |
| 202 | 223 | } |
| 203 | 224 | } |
| 204 | 225 | } |
| 205 | 226 | if( $return == 'topicid' ) { |