From a12086e6813f4a526ccbafbb32713c963258479c Mon Sep 17 00:00:00 2001 From: Harald Eilertsen Date: Tue, 7 Mar 2023 22:36:21 +0100 Subject: Add links + pagination to concerts table shortcode. --- includes/class-giglogadmin-concert.php | 26 +++++++ includes/giglogadmin-shortcodes.php | 20 ++++- includes/templates/giglogadmin-concerts-table.php | 95 +++++++++++++---------- 3 files changed, 98 insertions(+), 43 deletions(-) diff --git a/includes/class-giglogadmin-concert.php b/includes/class-giglogadmin-concert.php index a470617..c6223cb 100644 --- a/includes/class-giglogadmin-concert.php +++ b/includes/class-giglogadmin-concert.php @@ -211,6 +211,8 @@ if ( ! class_exists( 'GiglogAdmin_Concert' ) ) { $where = array(); $offset = 0; $limit = 15; + $page = null; + foreach ( $filter as $key => $value ) { switch ( $key ) { case 'name': @@ -230,6 +232,10 @@ if ( ! class_exists( 'GiglogAdmin_Concert' ) ) { array_push( $where, $keymap[ $key ] . $wpdb->prepare( ' like %s', esc_like( $value ) ) ); break; + case 'page': + $page = intval( $value ); + break; + case 'offset': $offset = intval( $value ); break; @@ -244,6 +250,10 @@ if ( ! class_exists( 'GiglogAdmin_Concert' ) ) { $query .= ' AND ' . implode( ' and ', $where ); } + if ( $page !== null ) { + $offset = ( $page - 1 ) * $limit; + } + $query .= ' ORDER BY wpgconcert_date'; $query .= " LIMIT {$offset},{$limit}"; @@ -289,6 +299,22 @@ if ( ! class_exists( 'GiglogAdmin_Concert' ) ) { return $count ? $count : 0; } + /** + * Return number of pages with the limit of concerts/page + * specified in the filter. + * + * @param array $filter The filter to use for the query. + * @return number of pages. + */ + public static function count_pages( array $filter = array() ) : int { + if ( ! isset( $filter['limit'] ) ) { + $filter['limit'] = 15; + } + + $num_concerts = self::count( $filter ); + return ( intval( $num_concerts / $filter['limit'] ) ) + 1; + } + /** * Save concert to database. */ diff --git a/includes/giglogadmin-shortcodes.php b/includes/giglogadmin-shortcodes.php index 5a3d70d..1fba82e 100644 --- a/includes/giglogadmin-shortcodes.php +++ b/includes/giglogadmin-shortcodes.php @@ -22,15 +22,27 @@ if ( ! function_exists( 'giglogadmin_shortcode_public' ) ) { * This shortcode does not have any attributes. */ function giglogadmin_shortcode_public() : string { - $filters = array( - 'offset' => 0, - 'limit' => 15, - ); + $filters = array(); if ( isset( $_GET['city'] ) && ! empty( $_GET['city'] ) ) { $filters['city'] = sanitize_text_field( wp_unslash( $_GET['city'] ) ); } + $pages = GiglogAdmin_Concert::count_pages( $filters ); + $page = 1; + + if ( isset( $_GET['pg'] ) && ! empty( $_GET['pg'] ) ) { + $page = intval( wp_unslash( $_GET['pg'] ) ); + + if ( $page < 1 || $page > $pages ) { + // Just silently redirect to first page if we're outside + // the acceptable range. + wp_redirect( get_page_link() ); + } + } + + $filters['page'] = $page; + $concerts = GiglogAdmin_Concert::find_concerts( $filters ); ob_start(); diff --git a/includes/templates/giglogadmin-concerts-table.php b/includes/templates/giglogadmin-concerts-table.php index f38db3d..3f394c9 100644 --- a/includes/templates/giglogadmin-concerts-table.php +++ b/includes/templates/giglogadmin-concerts-table.php @@ -7,6 +7,9 @@ ?>