summaryrefslogtreecommitdiffstats
path: root/includes/giglogadmin-shortcodes.php
blob: 1fba82eddc16980ce2faaaf392c822e6ffedcf18 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
<?php
/**
 * Shortcodes for GiglogAdmin.
 *
 * @package giglogadmin
 *
 * SPDX-FileCopyrightText: 2022 Andrea Chirulescu <andrea.chirulescu@gmail.com>
 * SPDX-FileCopyrightText: 2022 Harald Eilertsen <haraldei@anduin.net>
 *
 * SPDX-License-Identifier: AGPL-3.0-or-later
 */

declare(strict_types=1);

if ( ! function_exists( 'giglogadmin_shortcode_public' ) ) {

    /**
     * Shortcode that displays the public concert list.
     *
     * Usage: `[getconcerts]`
     *
     * This shortcode does not have any attributes.
     */
    function giglogadmin_shortcode_public() : string {
        $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();
        include __DIR__ . '/templates/giglogadmin-concerts-table.php';

        return ob_get_clean();
    }

    add_shortcode( 'getconcerts', 'giglogadmin_shortcode_public' );
}