Skip to content

Commit

Permalink
Fix whole day events (imported and manual edited)
Browse files Browse the repository at this point in the history
  • Loading branch information
albig committed Sep 2, 2024
1 parent fcb54b7 commit 2728806
Show file tree
Hide file tree
Showing 4 changed files with 169 additions and 34 deletions.
21 changes: 0 additions & 21 deletions assets/custom-jquery-date-time-picker.js

This file was deleted.

120 changes: 120 additions & 0 deletions assets/js/custom-jquery-date-time-picker.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
/* eslint-disable no-undef */

jQuery( function ( $ ) {
$.datetimepicker.setLocale( 'de' );

const isWholeDay = $( 'input[name="_sunflower_event_whole_day"]' ).is(
':checked'
);

if ( isWholeDay === true ) {
$(
'input.datetimepicker[name="_sunflower_event_from"]'
).datetimepicker( {
timepicker: false,
dayOfWeekStart: 1,
format: 'd.m.Y',
} );
$(
'input.datetimepicker[name="_sunflower_event_until"]'
).datetimepicker( {
timepicker: false,
dayOfWeekStart: 1,
format: 'd.m.Y',
onShow() {
this.setOptions( {
value: $(
'input.datetimepicker[name="_sunflower_event_until"]'
).val()
? $(
'input.datetimepicker[name="_sunflower_event_until"]'
).val()
: $(
'input.datetimepicker[name="_sunflower_event_from"]'
).val(),
} );
},
} );
} else {
$(
'input.datetimepicker[name="_sunflower_event_from"]'
).datetimepicker( {
timepicker: true,
dayOfWeekStart: 1,
format: 'd.m.Y H:i',
} );
$(
'input.datetimepicker[name="_sunflower_event_until"]'
).datetimepicker( {
timepicker: true,
dayOfWeekStart: 1,
format: 'd.m.Y H:i',
onShow() {
this.setOptions( {
value: $(
'input.datetimepicker[name="_sunflower_event_until"]'
).val()
? $(
'input.datetimepicker[name="_sunflower_event_until"]'
).val()
: $(
'input.datetimepicker[name="_sunflower_event_from"]'
).val(),
} );
},
} );
}

$( 'input[name="_sunflower_event_whole_day"]' ).change( function () {
if ( this.checked ) {
$(
'input.datetimepicker[name="_sunflower_event_from"]'
).datetimepicker( {
timepicker: false,
dayOfWeekStart: 1,
format: 'd.m.Y',
} );
$(
'input.datetimepicker[name="_sunflower_event_until"]'
).datetimepicker( {
timepicker: false,
dayOfWeekStart: 1,
format: 'd.m.Y',
} );

const from = $(
'input.datetimepicker[name="_sunflower_event_from"]'
)
.val()
.substring( 0, 10 );
const until = $(
'input.datetimepicker[name="_sunflower_event_until"]'
)
.val()
.substring( 0, 10 );

$( 'input.datetimepicker[name="_sunflower_event_from"]' ).val(
from
);
$( 'input.datetimepicker[name="_sunflower_event_until"]' ).val(
until
);
} else {
$(
'input.datetimepicker[name="_sunflower_event_from"]'
).datetimepicker( {
timepicker: true,
dayOfWeekStart: 1,
format: 'd.m.Y H:i',
} );
$(
'input.datetimepicker[name="_sunflower_event_until"]'
).datetimepicker( {
timepicker: true,
dayOfWeekStart: 1,
format: 'd.m.Y H:i',
} );
}
} );
} );
/* eslint-enable no-undef */
49 changes: 38 additions & 11 deletions functions/events.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ function sunflower_add_event_meta_boxes() {
add_action( 'admin_init', 'sunflower_add_event_meta_boxes' );

/**
* Save event meta data
* Save event meta data to database.
*/
function sunflower_save_event_meta_boxes() {
global $post;
Expand Down Expand Up @@ -131,8 +131,16 @@ function sunflower_save_event_meta_boxes() {

$into_transients = array( '_sunflower_event_lat', '_sunflower_event_lon', '_sunflower_event_zoom' );

$is_all_day = $_POST['_sunflower_event_whole_day'] ?? '';

foreach ( $sunflower_event_fields as $id => $config ) {
$value = ( 'datetimepicker' === $config[1] ) ? sunflower_german_date2int_date( $_POST[ $id ] ) : $_POST[ $id ];

// In case of all day events the events end on midnight, next day. So we have to add one day on save.
if ( '_sunflower_event_until' === $id && 'checked' === $is_all_day ) {
$value = gmdate( 'Y-m-d', strtotime( sunflower_german_date2int_date( $_POST[ $id ] ) ) + 86400 );
} else {
$value = ( 'datetimepicker' === $config[1] ) ? sunflower_german_date2int_date( $_POST[ $id ] ) : $_POST[ $id ];
}

update_post_meta( $post->ID, $id, sanitize_text_field( $value ) );
if ( ! in_array( $id, $into_transients, true ) ) {
Expand All @@ -150,7 +158,7 @@ function sunflower_save_event_meta_boxes() {
add_action( 'save_post', 'sunflower_save_event_meta_boxes', 10, 2 );

/**
* Transform date from German format to YYYY-MM-DD HH:mm
* Transform date from German format "DD.MM.YYYY HH:mm" to "YYYY-MM-DD HH:mm"
*
* @param string $german_date The date string in German format.
*/
Expand All @@ -159,24 +167,36 @@ function sunflower_german_date2int_date( $german_date ) {
return '';
}

[$day, $month, $year, $hours, $minutes] = preg_split( '/[^0-9]/', (string) $german_date );
return sprintf( '%s-%s-%s %s:%s', $year, $month, $day, $hours, $minutes );
$date_array = preg_split( '/[^0-9]/', (string) $german_date );

if ( count( $date_array ) === 5 ) {
return sprintf( '%s-%s-%s %s:%s', $date_array[2], $date_array[1], $date_array[0], $date_array[3], $date_array[4] );
} else {
return sprintf( '%s-%s-%s', $date_array[2], $date_array[1], $date_array[0] );
}
}

/**
* Transform date format from "YYYY-MM-DD HH:mm" into DD.MM.YYYY HH:mm
* Transform date format from "YYYY-MM-DD HH:mm" into German format "DD.MM.YYYY HH:mm"
*
* @param int $int_date The date in format "YYYY-MM-DD HH:mm".
* @param int $int_date The date in format international format.
*/
function sunflower_int_date2german_date( $int_date ) {
if ( ! $int_date ) {
return '';
}

[$year, $month, $day, $hours, $minutes] = preg_split( '/[^0-9]/', (string) $int_date );
return sprintf( '%s.%s.%s %s:%s', $day, $month, $year, $hours, $minutes );
$date_array = preg_split( '/[^0-9]/', (string) $int_date );

if ( count( $date_array ) === 5 ) {
return sprintf( '%s.%s.%s %s:%s', $date_array[2], $date_array[1], $date_array[0], $date_array[3], $date_array[4] );
} else {
return sprintf( '%s.%s.%s', $date_array[2], $date_array[1], $date_array[0] );
}
}



/**
* Render event meta box
*/
Expand All @@ -203,8 +223,15 @@ function sunflower_event_meta_box() {
return;
}

$is_all_day = $custom['_sunflower_event_whole_day'][0] ?? '';

foreach ( $sunflower_event_fields as $id => $config ) {
$value = $custom[ $id ][0] ?? false;

// In case of all day events the events end on midnight, next day. For the input, we show last day of event.
if ( '_sunflower_event_until' === $id && 'checked' === $is_all_day ) {
$value = gmdate( 'Y.m.d', strtotime( $value ) - 86400 );
}
sunflower_event_field( $id, $config, $value );
}

Expand Down Expand Up @@ -259,7 +286,7 @@ function sunflower_event_field( $id, $config, $value ) {

match ( $sunflower_type ) {
'checkbox' => printf(
'<div><span><input class="%4$s" type="checkbox" name="%1$s" id="%1$s" %3$s value="checked"></span><label for="%1$s">%2$s</label></div>',
'<div><span><input class="%4$s" type="checkbox" name="%1$s" id="%1$s" %3$s value="checked"></span><label for="%1$s">%2$s</label></div>',
esc_attr( $id ),
esc_attr( $sunflower_label ),
esc_attr( $value ),
Expand Down Expand Up @@ -294,7 +321,7 @@ function sunflower_load_event_admin_scripts() {

wp_enqueue_script(
'sunflower-datetimepicker-custom',
get_template_directory_uri() . '/assets/custom-jquery-date-time-picker.js',
get_template_directory_uri() . '/assets/js/custom-jquery-date-time-picker.js',
array( 'sunflower-datetimepicker' ),
'1.0.0',
true
Expand Down
13 changes: 11 additions & 2 deletions functions/icalimport.php
Original file line number Diff line number Diff line change
Expand Up @@ -122,8 +122,17 @@ function sunflower_icalimport( $url = false, $auto_categories = false ) {
$ids_from_remote[] = $id;

// Write start and end time to event post metadata.
update_post_meta( $id, '_sunflower_event_from', $event->DTSTART->getDateTime( $timezone_fix )->setTimezone( $timezone )->format( 'Y-m-d H:i' ) ); // phpcs:ignore
update_post_meta( $id, '_sunflower_event_until', $event->DTEND?->getDateTime( $timezone_fix )->setTimezone( $timezone )->format( 'Y-m-d H:i' ) ); // phpcs:ignore
if ( $event->DTSTART instanceof \Sabre\VObject\Property\ICalendar\Date || // phpcs:ignore
( $event->DTSTART->getDateTime()->format( 'H:i' ) == '00:00' && $event->DTEND->getDateTime()->format( 'H:i' ) == '00:00' ) // phpcs:ignore
) {
update_post_meta( $id, '_sunflower_event_whole_day', 'checked' );
update_post_meta( $id, '_sunflower_event_from', $event->DTSTART->getDateTime()->format( 'Y-m-d' ) ); // phpcs:ignore
update_post_meta( $id, '_sunflower_event_until', $event->DTEND?->getDateTime()->format( 'Y-m-d' ) ); // phpcs:ignore
} else {
update_post_meta( $id, '_sunflower_event_from', $event->DTSTART->getDateTime( $timezone_fix )->setTimezone( $timezone )->format( 'Y-m-d H:i' ) ); // phpcs:ignore
update_post_meta( $id, '_sunflower_event_until', $event->DTEND?->getDateTime( $timezone_fix )->setTimezone( $timezone )->format( 'Y-m-d H:i' ) ); // phpcs:ignore
}

update_post_meta( $id, '_sunflower_event_uid', $uid );

if ( isset( $event->LOCATION ) ) { // phpcs:ignore
Expand Down

0 comments on commit 2728806

Please sign in to comment.