summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorHarald Eilertsen <haraldei@anduin.net>2023-03-07 22:36:21 +0100
committerHarald Eilertsen <haraldei@anduin.net>2023-03-07 22:36:21 +0100
commita12086e6813f4a526ccbafbb32713c963258479c (patch)
treee680add56ee4b9335c45cddded1025c8fcc0099a
parent1c6be6b8a5724f5a4162168241062abded4635e7 (diff)
downloadgigologadmin-a12086e6813f4a526ccbafbb32713c963258479c.tar.gz
gigologadmin-a12086e6813f4a526ccbafbb32713c963258479c.tar.bz2
gigologadmin-a12086e6813f4a526ccbafbb32713c963258479c.zip
Add links + pagination to concerts table shortcode.
-rw-r--r--includes/class-giglogadmin-concert.php26
-rw-r--r--includes/giglogadmin-shortcodes.php20
-rw-r--r--includes/templates/giglogadmin-concerts-table.php95
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}";
@@ -290,6 +300,22 @@ if ( ! class_exists( 'GiglogAdmin_Concert' ) ) {
}
/**
+ * Return number of pages with the limit of concerts/page
+ * specified in the filter.
+ *
+ * @param array<string, mixed> $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.
*/
public function save() : void {
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 @@
?>
<style>
+.giglogadmin-concerts-filter {
+ margin-bottom: 0.5em;
+}
.giglogadmin-concert {
margin-bottom: 1em;
}
@@ -43,45 +46,59 @@
<a href="<?php echo esc_url( site_url( get_page_uri() ) ); ?>">Clear filters</a>
</form>
</div>
-<?php foreach ( $concerts as $concert ) { ?>
- <div class="giglogadmin-concert">
- <div class="giglogadmin-concert-title-row">
- <time datetime="<?php echo esc_attr( $concert->cdate()->format( 'c' ) ); ?>">
- <?php echo esc_html( $concert->cdate()->format( 'd.m.Y' ) ); ?>
- </time>
- &nbsp;—&nbsp;
- <span class="giglogadmin-concert-title">
- <?php echo esc_html( $concert->cname() ); ?>
- </span>
- &nbsp;—&nbsp;
- <span class="giglogadmin-concert-city">
- <?php echo esc_html( $concert->venue()->city() ); ?>
- </span>
- </div>
- <div class="giglogadmin-concert-meta-row">
- <span class="giglogadmin-concert-venue">
- <?php echo esc_html( $concert->venue()->name() ); ?>
- </span>
- </div>
- <div class="giglogadmin-concert-links">
- <span class="giglogadmin-event-link">
- <a href="<?php echo esc_url( $concert->eventlink() ); ?>">
- Event
- </a>
- </span>
- &nbsp;|&nbsp;
- <span class="giglogadmin-concert-ticket-link">
- <a href="<?php echo esc_url( $concert->tickets() ); ?>">
- Tickets
- </a>
- </span>
- &nbsp;|&nbsp;
- <span class="giglogadmin-concert-ical-link">
- <a href="">
- Add to calendar
- </a>
- </span>
+ <div class="giglogadmin-concerts-table-list">
+ <?php foreach ( $concerts as $concert ) { ?>
+ <div class="giglogadmin-concert">
+ <div class="giglogadmin-concert-title-row">
+ <time datetime="<?php echo esc_attr( $concert->cdate()->format( 'c' ) ); ?>">
+ <?php echo esc_html( $concert->cdate()->format( 'd.m.Y' ) ); ?>
+ </time>
+ &nbsp;—&nbsp;
+ <span class="giglogadmin-concert-title">
+ <?php echo esc_html( $concert->cname() ); ?>
+ </span>
+ &nbsp;—&nbsp;
+ <span class="giglogadmin-concert-city">
+ <?php echo esc_html( $concert->venue()->city() ); ?>
+ </span>
+ </div>
+ <div class="giglogadmin-concert-meta-row">
+ <span class="giglogadmin-concert-venue">
+ <?php echo esc_html( $concert->venue()->name() ); ?>
+ </span>
+ </div>
+ <div class="giglogadmin-concert-links">
+ <span class="giglogadmin-event-link">
+ <a href="<?php echo esc_url( $concert->eventlink() ); ?>">
+ Event
+ </a>
+ </span>
+ &nbsp;|&nbsp;
+ <span class="giglogadmin-concert-ticket-link">
+ <a href="<?php echo esc_url( $concert->tickets() ); ?>">
+ Tickets
+ </a>
+ </span>
+ &nbsp;|&nbsp;
+ <span class="giglogadmin-concert-ical-link">
+ <a href="">
+ Add to calendar
+ </a>
+ </span>
+ </div>
</div>
+ <?php } ?>
+ </div>
+ <div class="giglogadmin-concerts-table-pagination">
+ <?php
+ echo paginate_links(
+ array(
+ 'base' => site_url( get_page_uri() ) . '%_%',
+ 'total' => $pages,
+ 'current' => $page,
+ 'format' => '?pg=%#%',
+ )
+ );
+ ?>
</div>
-<?php } ?>
</div>