summaryrefslogblamecommitdiffstats
path: root/includes/giglogadmin-export-ical-handler.php
blob: c230638396a54a8e7ff834d188608c99eecb0819 (plain) (tree)































                                                                                                                    

                                       
                                                                                       



                                                
                                                                                                
                                             


                                                                          

                             











                                                                                       
                                             
                                    


                                                                                   






                                                                         






                                                                                         
<?php
/**
 * Class to handle ICAL export of concert data.
 *
 * @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
 */

use Kigkonsult\Icalcreator\Vcalendar;

if ( ! function_exists( 'giglogadmin_export_ical_handler' ) ) {
    /**
     * Function to handle ICAL export of concert data.
     */
    function giglogadmin_export_ical_handler() {
        if ( ! isset( $_GET['evid'] ) || ! is_numeric( $_GET['evid'] ) ) {
            wp_die( 'Invalid request, invalid or missing event id.' );
        }

        $evid = intval( $_GET['evid'] );

        $concert = GiglogAdmin_Concert::get( $evid );
        if ( ! $concert ) {
            wp_die( 'Invalid request, unknown event id.' );
        }

        $cfullname = $concert->cname() . ' live at ' . $concert->venue()->name() . ', ' . $concert->venue()->city();
        $cshortname = substr( $cfullname, 0, 20 );

        $filename = sanitize_file_name(
            "Concert-{$concert->venue()->name()}-{$concert->cdate()->format( 'Ymd' )}-"
            . substr( $concert->cname(), 0, 30 )
        );

        // create a new calendar
        $vcalendar = Vcalendar::factory( array( Vcalendar::UNIQUE_ID => 'eternal-terror.com' ) )
            ->setMethod( Vcalendar::PUBLISH )
            ->setXprop( Vcalendar::X_WR_CALNAME, 'Eternal Terror Giglog' )
            ->setXprop( Vcalendar::X_WR_CALDESC, "Concert {$cfullname}" )
            ->setXprop( Vcalendar::X_WR_TIMEZONE, 'Europe/Oslo' );

        // create a new event
        $vcalendar
            ->newVevent()
            ->setTransp( Vcalendar::OPAQUE )
            ->setClass( Vcalendar::P_BLIC )
            ->setSequence( 1 )
            // describe the event
            ->setSummary( $cfullname )
            ->setDescription( $cfullname )
            ->setComment( $cfullname )
            // place the event
            ->setLocation( "{$concert->venue()->name()}, {$concert->venue()->city()}" )
            // set the time
            ->setDtstart( $concert->cdate() )
            ->setDuration( 'PT4H' );

        header( 'Content-Type: text/calendar' );
        header( 'content-disposition: attachment;filename=' . $filename . '.ics' );

        // This output is generated by the ics class, and should be
        // correctly escaped for output as ICS data.
        //
        // phpcs:disable WordPress.Security.EscapeOutput.OutputNotEscaped
        echo $vcalendar->vtimezonePopulate()->createCalendar();

        die();
    }

    add_action( 'wp_ajax_nopriv_giglog_export_ical', 'giglogadmin_export_ical_handler' );
    add_action( 'wp_ajax_giglog_export_ical', 'giglogadmin_export_ical_handler' );
}