| @@ -53,10 +53,12 @@ | ||
| 53 | 53 | |
| 54 | 54 | $where = ''; |
| 55 | 55 | $search_str = ''; |
| 56 | 56 | |
| 57 | - $count_only = ! empty( $args['count_only'] ) ? true : false; | |
| 58 | - $not_count = ! empty( $args['not_count'] ) ? true : false; | |
| 57 | + $count_only = ! empty( $args['count_only'] ) ? true : false; | |
| 58 | + $not_count = ! empty( $args['not_count'] ) ? true : false; | |
| 59 | + $optimize_has_more = ! empty( $args['optimize_has_more'] ) ? true : false; | |
| 60 | + | |
| 59 | 61 | if ( ! empty( $args['search'] ) ) { |
| 60 | 62 | $search_str = MainWP_DB::instance()->escape( $args['search'] ); |
| 61 | 63 | // for searching. |
| 62 | 64 | if ( ! empty( $search_str ) ) { |
| @@ -212,9 +214,10 @@ | ||
| 212 | 214 | $start = absint( $args['start'] ); |
| 213 | 215 | $per_page = absint( $args['records_per_page'] ); |
| 214 | 216 | |
| 215 | 217 | if ( $per_page > 0 ) { |
| 216 | - $limits = "LIMIT {$start}, {$per_page}"; | |
| 218 | + $per_page_more = $optimize_has_more ? $per_page + 1 : $per_page; // Fetch one extra record to determine whether to show the "More" button. | |
| 219 | + $limits = "LIMIT {$start}, {$per_page_more}"; | |
| 217 | 220 | } |
| 218 | 221 | |
| 219 | 222 | // Show the recent records first by default. |
| 220 | 223 | $order = 'DESC'; |
| @@ -394,8 +397,16 @@ | ||
| 394 | 397 | } |
| 395 | 398 | |
| 396 | 399 | $items = $wpdb->get_results( $query ); // phpcs:ignore -- ok. |
| 397 | 400 | |
| 401 | + $count_har_more = 0; | |
| 402 | + if ( $optimize_has_more ) { | |
| 403 | + $count_har_more = $items && is_array( $items ) ? count( $items ) : 0; // to support "More" button. | |
| 404 | + if ( $count_har_more > $per_page ) { | |
| 405 | + array_pop( $items ); // Do not show the last item. | |
| 406 | + } | |
| 407 | + } | |
| 408 | + | |
| 398 | 409 | if ( ( ( $optimize_get_dt && $optimize_get_meta ) || $with_logs_meta ) && $items ) { |
| 399 | 410 | |
| 400 | 411 | $ids = array_map( 'absint', wp_list_pluck( $items, 'log_id' ) ); |
| 401 | 412 | |
| @@ -481,8 +492,9 @@ | ||
| 481 | 492 | */ |
| 482 | 493 | $results = array( |
| 483 | 494 | 'items' => $items, |
| 484 | 495 | 'sites_opts' => $sites_opts, |
| 496 | + 'extra_data' => array(), | |
| 485 | 497 | ); |
| 486 | 498 | |
| 487 | 499 | if ( ! $not_count ) { |
| 488 | 500 | $cached_count = wp_cache_get( $cache_key, 'mainwp_logs' ); |
| @@ -493,8 +505,14 @@ | ||
| 493 | 505 | wp_cache_set( $cache_key, $count, 'mainwp_logs', HOUR_IN_SECONDS ); |
| 494 | 506 | $results['count'] = $count; |
| 495 | 507 | } |
| 496 | 508 | } |
| 509 | + | |
| 510 | + if ( $optimize_has_more ) { | |
| 511 | + $results['count'] = $start + $count_har_more; // to fix offset issue for next loading. | |
| 512 | + $results['extra_data']['has_more'] = $count_har_more > $per_page ? true : false; | |
| 513 | + } | |
| 514 | + | |
| 497 | 515 | return $results; |
| 498 | 516 | } |
| 499 | 517 | |
| 500 | 518 | /** |