| 1120 | function wp_comments_per_page() { |
| 1121 | $per_page = (int) get_query_var( 'comments_per_page' ); |
| 1122 | if ( 0 === $per_page ) { |
| 1123 | $per_page = (int) get_option( 'comments_per_page' ); |
| 1124 | } |
| 1125 | return $per_page; |
| 1126 | } |
| 1127 | |
| 1128 | function wp_comment_pages_count( $total, $top_level ) { |
| 1129 | if ( ! get_option( 'page_comments' ) ) { |
| 1130 | return 1; |
| 1131 | } |
| 1132 | |
| 1133 | $per_page = wp_comments_per_page(); |
| 1134 | if ( 0 === $per_page ) { |
| 1135 | return 1; |
| 1136 | } |
| 1137 | |
| 1138 | $threaded = get_option( 'thread_comments' ); |
| 1139 | |
| 1140 | if ( $threaded ) { |
| 1141 | $count = ceil( $top_level / $per_page ); |
| 1142 | } else { |
| 1143 | $count = ceil( $total / $per_page ); |
| 1144 | } |
| 1145 | |
| 1146 | return $count; |
| 1147 | } |
| 1148 | |
| 1149 | function wp_threaded_comment_ids( $comment_ids, $comment_args ) { |
| 1150 | $threaded_ids = array(); |
| 1151 | $args = $comment_args; |
| 1152 | unset( $args['parent'] ); |
| 1153 | |
| 1154 | do { |
| 1155 | $threaded_ids = array_merge( $threaded_ids, $comment_ids ); |
| 1156 | $args['parent__in'] = $comment_ids; |
| 1157 | $comment_ids = get_comments( $args ); |
| 1158 | } while ( count( $comment_ids ) ); |
| 1159 | |
| 1160 | return $threaded_ids; |
| 1161 | } |
| 1162 | |
| 1244 | $top_level = 0; |
| 1245 | $threaded = get_option( 'thread_comments' ); |
| 1246 | if ( $threaded ) { |
| 1247 | $comment_args['parent'] = 0; |
| 1248 | // array of all top-level comment IDs for the post |
| 1249 | $comment_ids = get_comments( $comment_args ); |
| 1250 | $top_level = count( $comment_ids ); |
| 1251 | } else { |
| 1252 | // array of all comment IDs for the post |
| 1253 | $comment_ids = get_comments( $comment_args ); |
| 1254 | } |
| 1255 | |
| 1256 | $pages = wp_comment_pages_count( $post->comment_count, $top_level ); |
| 1257 | $wp_query->comment_pages_count = $pages; |
| 1258 | $wp_query->comment_pages_page = 1; |
| 1259 | |
| 1260 | if ( $pages > 1 ) { |
| 1261 | $per_page = wp_comments_per_page(); |
| 1262 | $page = (int) get_query_var( 'cpage' ); |
| 1263 | if ( 0 === $page ) { |
| 1264 | $page = 1; |
| 1265 | } |
| 1266 | $wp_query->comment_pages_page = $page; |
| 1267 | |
| 1268 | $comment_ids = array_slice( $comment_ids, ( $page - 1 ) * $per_page, $per_page ); |
| 1269 | if ( $threaded ) { |
| 1270 | $comment_ids = wp_threaded_comment_ids( $comment_ids, $comment_args ); |
| 1271 | } |
| 1272 | |
| 1273 | $comment_args['comment__in'] = $comment_ids; |
| 1274 | $comment_args['orderby'] = 'comment__in'; |
| 1275 | unset( $comment_args['order'] ); |
| 1276 | } |
| 1277 | |
| 1278 | unset( $comment_args['parent'], $comment_args['fields'] ); |