From e50647ecda43e7708e92175afd1507991f1bd855 Mon Sep 17 00:00:00 2001 From: Harald Eilertsen Date: Sun, 22 Jan 2023 16:17:01 +0100 Subject: Change ical export handler to a function. A class with one static function does not need to be a class in any case. Also renames the function to be a bit more descriptive. --- includes/class-giglogadmin-icalexport.php | 93 --------------------------- includes/giglogadmin-export-ical-handler.php | 96 ++++++++++++++++++++++++++++ 2 files changed, 96 insertions(+), 93 deletions(-) delete mode 100644 includes/class-giglogadmin-icalexport.php create mode 100644 includes/giglogadmin-export-ical-handler.php diff --git a/includes/class-giglogadmin-icalexport.php b/includes/class-giglogadmin-icalexport.php deleted file mode 100644 index d12db13..0000000 --- a/includes/class-giglogadmin-icalexport.php +++ /dev/null @@ -1,93 +0,0 @@ - - * SPDX-FileCopyrightText: 2022 Harald Eilertsen - * - * SPDX-License-Identifier: AGPL-3.0-or-later - */ - -use Kigkonsult\Icalcreator\Vcalendar; - -if ( ! class_exists( 'GiglogAdmin_IcalExport' ) ) { - /** - * Class to handle ICAL export of concert data. - */ - class Giglogadmin_IcalExport { - - public static function export_ical() { - $evid = $_GET['evid']; - - $concert = GiglogAdmin_Concert::get( $evid ); - $cfullname = $concert->cname() . ' live at ' . $concert->venue()->name() . ', ' . $concert->venue()->city(); - $cshortname = substr( $cfullname, 0, 20 ); - $fdate = strtotime( $concert->cdate() ); - $newformat = date( 'Ymd', $fdate ); - - $filename = sanitize_file_name( - "Concert-{$concert->venue()->name()}-{$newformat}-" - . substr( $concert->cname(), 0, 30 ) - ); - - // create a new calendar - $vcalendar = Vcalendar::factory( array( Vcalendar::UNIQUE_ID => 'kigkonsult.se' ) ) - // with calendaring info - ->setMethod( Vcalendar::PUBLISH ) - ->setXprop( - Vcalendar::X_WR_CALNAME, - 'Calendar Sample' - ) - ->setXprop( - Vcalendar::X_WR_CALDESC, - 'Concert ' . $cfullname . '' - ) - ->setXprop( - Vcalendar::X_WR_RELCALID, - '3E26604A-50F4-4449-8B3E-E4F4932D05B5' - ) - ->setXprop( - Vcalendar::X_WR_TIMEZONE, - 'Europe/Oslo' - ); - - // create a new event - $event1 = $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( - new DateTime( - $newformat . 'T190000', - new DateTimezone( 'Europe/Oslo' ) - ) - ) - ->setDuration( 'PT4H' ); - $vcalendarString = - // apply appropriate Vtimezone with Standard/DayLight components - $vcalendar->vtimezonePopulate() - // and create the (string) calendar - ->createCalendar(); - - header( 'Content-Type: text/calendar' ); - header( 'content-disposition: attachment;filename=' . $filename . '.ics' ); - echo $vcalendarString; - die(); - } - } - - /** @psalm-suppress HookNotFound */ - add_action( 'wp_ajax_nopriv_giglog_export_ical', array( 'GiglogAdmin_IcalExport', 'export_ical' ) ); - - /** @psalm-suppress HookNotFound */ - add_action( 'wp_ajax_giglog_export_ical', array( 'GiglogAdmin_IcalExport', 'export_ical' ) ); -} diff --git a/includes/giglogadmin-export-ical-handler.php b/includes/giglogadmin-export-ical-handler.php new file mode 100644 index 0000000..94309fc --- /dev/null +++ b/includes/giglogadmin-export-ical-handler.php @@ -0,0 +1,96 @@ + + * SPDX-FileCopyrightText: 2022 Harald Eilertsen + * + * 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 ); + $fdate = strtotime( $concert->cdate() ); + $newformat = date( 'Ymd', $fdate ); + + $filename = sanitize_file_name( + "Concert-{$concert->venue()->name()}-{$newformat}-" + . substr( $concert->cname(), 0, 30 ) + ); + + // create a new calendar + $vcalendar = Vcalendar::factory( array( Vcalendar::UNIQUE_ID => 'kigkonsult.se' ) ) + // with calendaring info + ->setMethod( Vcalendar::PUBLISH ) + ->setXprop( + Vcalendar::X_WR_CALNAME, + 'Calendar Sample' + ) + ->setXprop( + Vcalendar::X_WR_CALDESC, + 'Concert ' . $cfullname . '' + ) + ->setXprop( + Vcalendar::X_WR_RELCALID, + '3E26604A-50F4-4449-8B3E-E4F4932D05B5' + ) + ->setXprop( + Vcalendar::X_WR_TIMEZONE, + 'Europe/Oslo' + ); + + // create a new event + $event1 = $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( + new DateTime( + $newformat . 'T190000', + new DateTimezone( 'Europe/Oslo' ) + ) + ) + ->setDuration( 'PT4H' ); + $output = + // apply appropriate Vtimezone with Standard/DayLight components + $vcalendar->vtimezonePopulate() + // and create the (string) calendar + ->createCalendar(); + + header( 'Content-Type: text/calendar' ); + header( 'content-disposition: attachment;filename=' . $filename . '.ics' ); + echo $output; + die(); + } + + add_action( 'wp_ajax_nopriv_giglog_export_ical', 'giglogadmin_export_ical_handler' ); + add_action( 'wp_ajax_giglog_export_ical', 'giglogadmin_export_ical_handler' ); +} + -- cgit v1.2.3