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 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) (limited to 'includes/class-giglogadmin-concert.php') 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. */ -- cgit v1.2.3