Skip to content

Commit

Permalink
Fix pagination
Browse files Browse the repository at this point in the history
Props @ejrobinson-uw. Fixes #615
  • Loading branch information
joedolson committed Sep 13, 2024
1 parent 2ae62f8 commit 40b3385
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 15 deletions.
30 changes: 17 additions & 13 deletions src/my-calendar-event-manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -679,19 +679,20 @@ function mc_list_events() {
$filter = "'$filter'";
}
$join = '';
// Set up standard filter limits - normal database fields.
if ( '' === $limit && '' !== $filter ) {
$limit = "WHERE $restrict = $filter";
} elseif ( '' !== $limit && '' !== $filter ) {
$limit .= " AND $restrict = $filter";
}
// Set up filter format for categories.
if ( 'event_category' === $restrict ) {
$cat_limit = mc_select_category( $filter );
$join = ( isset( $cat_limit[0] ) ) ? $cat_limit[0] : '';
$select_category = ( isset( $cat_limit[1] ) ) ? $cat_limit[1] : '';
$limit .= ' ' . $select_category;
}
// Set up standard filter limits - normal database fields.
if ( '' === $limit && '' !== $filter ) {
$limit = "WHERE $restrict = $filter";
} elseif ( '' !== $limit && '' !== $filter && 'event_category' !== $restrict ) {
$limit .= " AND $restrict = $filter";
}

// Define default limits if none otherwise set.
if ( '' === $limit ) {
$limit .= ( 'event_flagged' !== $restrict ) ? ' WHERE event_flagged = 0' : '';
Expand All @@ -709,15 +710,17 @@ function mc_list_events() {
$limit .= ( 'archived' !== $restrict ) ? ' AND e.event_status = 1' : ' AND e.event_status = 0';
// Toggle query type depending on whether we're limiting categories, which requires a join.
if ( 'event_category' !== $sortbyvalue ) {
$events = $wpdb->get_results( $wpdb->prepare( 'SELECT e.event_id FROM ' . my_calendar_table() . " AS e $join $limit ORDER BY $sortbyvalue " . 'LIMIT %d, %d', $query['query'], $query['items_per_page'] ) ); // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared,WordPress.DB.PreparedSQL.NotPrepared
$events = $wpdb->get_results( $wpdb->prepare( 'SELECT e.event_id FROM ' . my_calendar_table() . " AS e $join $limit ORDER BY $sortbyvalue " . 'LIMIT %d, %d', $query['query'], $query['items_per_page'] ) ); // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared,WordPress.DB.PreparedSQL.NotPrepared
$found_rows = $wpdb->get_col( 'SELECT COUNT(*) FROM ' . my_calendar_table() . " AS e $join $limit ORDER BY $sortbyvalue" ); // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared
} else {
$limit = str_replace( array( 'WHERE ' ), '', $limit );
$limit = ( strpos( $limit, 'AND' ) === 0 ) ? $limit : 'AND ' . $limit;
$events = $wpdb->get_results( $wpdb->prepare( 'SELECT DISTINCT e.event_id FROM ' . my_calendar_table() . ' AS e ' . $join . ' JOIN ' . my_calendar_categories_table() . " AS c WHERE e.event_category = c.category_id $limit ORDER BY c.category_name $sortbydirection " . 'LIMIT %d, %d', $query['query'], $query['items_per_page'] ) ); // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared,WordPress.DB.PreparedSQL.NotPrepared
$limit = str_replace( array( 'WHERE ' ), '', $limit );
$limit = ( strpos( $limit, 'AND' ) === 0 ) ? $limit : 'AND ' . $limit;
$events = $wpdb->get_results( $wpdb->prepare( 'SELECT DISTINCT e.event_id FROM ' . my_calendar_table() . ' AS e ' . $join . ' JOIN ' . my_calendar_categories_table() . " AS c WHERE e.event_category = c.category_id $limit ORDER BY c.category_name $sortbydirection " . 'LIMIT %d, %d', $query['query'], $query['items_per_page'] ) ); // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared,WordPress.DB.PreparedSQL.NotPrepared
$found_rows = $wpdb->get_col( 'SELECT COUNT(*) FROM ' . my_calendar_table() . ' AS e ' . $join . ' JOIN ' . my_calendar_categories_table() . " AS c WHERE e.event_category = c.category_id $limit ORDER BY c.category_name $sortbydirection" ); // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared

}
$found_rows = $wpdb->get_col( 'SELECT FOUND_ROWS();' );
$items = $found_rows[0];
$num_pages = ceil( $items / $query['items_per_page'] );
$items = $found_rows[0];
$num_pages = ceil( $items / $query['items_per_page'] );
if ( $num_pages > 1 ) {
$page_links = paginate_links(
array(
Expand Down Expand Up @@ -768,6 +771,7 @@ function mc_list_events() {
<tr>
<?php
$admin_url = admin_url( "admin.php?page=my-calendar-manage&order=$sortbydirection&paged=" . $query['current'] );
$admin_url = isset( $_GET['limit'] ) ? add_query_arg( 'limit', sanitize_text_field( $_GET['limit'] ), $admin_url ) : $admin_url;
$url = add_query_arg( 'sort', '1', $admin_url );
$col_head = mc_table_header( __( 'ID', 'my-calendar' ), $sortbydirection, $sortby, '1', $url );
$url = add_query_arg( 'sort', '2', $admin_url );
Expand Down
3 changes: 2 additions & 1 deletion src/my-calendar-group-manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -797,7 +797,7 @@ function mc_list_groups() {
}
$query_limit = ( ( $current - 1 ) * $items_per_page );
$events = $wpdb->get_results( $wpdb->prepare( 'SELECT * FROM ' . my_calendar_table() . " $limit ORDER BY $sortbyvalue $sortbydirection LIMIT %d, %d", $query_limit, $items_per_page ) ); // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared,WordPress.DB.PreparedSQL.NotPrepared
$found_rows = $wpdb->get_col( 'SELECT FOUND_ROWS();' );
$found_rows = $wpdb->get_col( 'SELECT COUNT(*) FROM ' . my_calendar_table() ); // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared
$items = $found_rows[0];
?>
<div class='inside'>
Expand Down Expand Up @@ -848,6 +848,7 @@ function mc_list_groups() {
<tr>
<?php
$admin_url = admin_url( "admin.php?page=my-calendar-manage&groups=true&order=$sort&paged=$current" );
$admin_url = isset( $_GET['limit'] ) ? add_query_arg( 'limit', sanitize_text_field( $_GET['limit'] ), $admin_url ) : $admin_url;
$url = add_query_arg( 'sort', '1', $admin_url );
$col_head = mc_table_header( __( 'ID', 'my-calendar' ), $sort, $sortby, '1', $url );
$url = add_query_arg( 'sort', '8', $admin_url );
Expand Down
2 changes: 1 addition & 1 deletion src/my-calendar-location-manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,7 @@ function mc_manage_locations() {

$query_limit = ( ( $current - 1 ) * $items_per_page );
$locations = $wpdb->get_results( $wpdb->prepare( 'SELECT location_id FROM ' . my_calendar_locations_table() . " $search ORDER BY $orderby $query_order LIMIT %d, %d", $query_limit, $items_per_page ) ); // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared,WordPress.DB.PreparedSQL.NotPrepared
$found_rows = $wpdb->get_col( 'SELECT FOUND_ROWS();' );
$found_rows = $wpdb->get_col( 'SELECT COUNT(*) FROM ' . my_calendar_locations_table() . " $search ORDER BY $orderby $query_order" ); // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared,WordPress.DB.PreparedSQL.NotPrepared
$items = $found_rows[0];
$pagination = '';

Expand Down

0 comments on commit 40b3385

Please sign in to comment.