update
This commit is contained in:
@@ -32,18 +32,18 @@ class ICal_Feed {
|
||||
}
|
||||
|
||||
/**
|
||||
* Add rewrite rule for ical feed
|
||||
* Add rewrite rule for global ical feed
|
||||
*/
|
||||
public static function add_rewrite_rules() {
|
||||
add_rewrite_rule(
|
||||
'^yacht-ical/([0-9]+)/([a-zA-Z0-9]+)\.ics$',
|
||||
'index.php?yacht_ical_id=$matches[1]&yacht_ical_token=$matches[2]',
|
||||
'^yacht-ical-global/([a-zA-Z0-9]+)\.ics$',
|
||||
'index.php?yacht_ical_global=1&yacht_ical_token=$matches[1]',
|
||||
'top'
|
||||
);
|
||||
|
||||
// Flush rewrite rules if our rule is not registered yet.
|
||||
$rules = get_option( 'rewrite_rules' );
|
||||
if ( is_array( $rules ) && ! isset( $rules['^yacht-ical/([0-9]+)/([a-zA-Z0-9]+)\.ics$'] ) ) {
|
||||
if ( is_array( $rules ) && ! isset( $rules['^yacht-ical-global/([a-zA-Z0-9]+)\.ics$'] ) ) {
|
||||
flush_rewrite_rules( false );
|
||||
}
|
||||
}
|
||||
@@ -55,94 +55,82 @@ class ICal_Feed {
|
||||
* @return array
|
||||
*/
|
||||
public static function add_query_vars( $vars ) {
|
||||
$vars[] = 'yacht_ical_id';
|
||||
$vars[] = 'yacht_ical_token';
|
||||
$vars[] = 'yacht_ical_global';
|
||||
return $vars;
|
||||
}
|
||||
|
||||
/**
|
||||
* Handle feed request
|
||||
* Handle feed request — globalny feed iCal (jeden plik dla całej floty).
|
||||
*/
|
||||
public static function handle_feed_request() {
|
||||
$yacht_id = (int) get_query_var( 'yacht_ical_id', 0 );
|
||||
$token = get_query_var( 'yacht_ical_token', '' );
|
||||
$is_global = (int) get_query_var( 'yacht_ical_global', 0 );
|
||||
$token = get_query_var( 'yacht_ical_token', '' );
|
||||
|
||||
if ( ! $yacht_id || ! $token ) {
|
||||
if ( ! $is_global || ! $token ) {
|
||||
return;
|
||||
}
|
||||
|
||||
$yacht = get_post( $yacht_id );
|
||||
if ( ! $yacht || 'yacht' !== $yacht->post_type ) {
|
||||
status_header( 404 );
|
||||
exit;
|
||||
}
|
||||
|
||||
$stored_token = self::get_feed_token( $yacht_id );
|
||||
$stored_token = self::get_global_feed_token();
|
||||
if ( ! $stored_token || ! hash_equals( $stored_token, $token ) ) {
|
||||
status_header( 403 );
|
||||
exit;
|
||||
}
|
||||
|
||||
self::output_ics( $yacht );
|
||||
self::output_global_ics();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get or create feed token for a yacht
|
||||
* Get or create global feed token (one shared token for all-yachts feed).
|
||||
*
|
||||
* @param int $yacht_id Yacht ID.
|
||||
* @return string
|
||||
*/
|
||||
public static function get_feed_token( $yacht_id ) {
|
||||
$token = get_post_meta( $yacht_id, '_yacht_ical_token', true );
|
||||
public static function get_global_feed_token() {
|
||||
$token = get_option( 'yacht_booking_global_ical_token', '' );
|
||||
|
||||
if ( empty( $token ) ) {
|
||||
$token = wp_generate_password( 24, false );
|
||||
update_post_meta( $yacht_id, '_yacht_ical_token', $token );
|
||||
update_option( 'yacht_booking_global_ical_token', $token );
|
||||
}
|
||||
|
||||
return $token;
|
||||
}
|
||||
|
||||
/**
|
||||
* Regenerate feed token
|
||||
* Regenerate global feed token (invalidates the previous URL).
|
||||
*
|
||||
* @param int $yacht_id Yacht ID.
|
||||
* @return string
|
||||
*/
|
||||
public static function regenerate_token( $yacht_id ) {
|
||||
public static function regenerate_global_token() {
|
||||
$token = wp_generate_password( 24, false );
|
||||
update_post_meta( $yacht_id, '_yacht_ical_token', $token );
|
||||
update_option( 'yacht_booking_global_ical_token', $token );
|
||||
return $token;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get feed URL for a yacht
|
||||
* Get global feed URL (all yachts in one .ics).
|
||||
*
|
||||
* @param int $yacht_id Yacht ID.
|
||||
* @return string
|
||||
*/
|
||||
public static function get_feed_url( $yacht_id ) {
|
||||
$token = self::get_feed_token( $yacht_id );
|
||||
return home_url( sprintf( '/yacht-ical/%d/%s.ics', $yacht_id, $token ) );
|
||||
public static function get_global_feed_url() {
|
||||
$token = self::get_global_feed_token();
|
||||
return home_url( sprintf( '/yacht-ical-global/%s.ics', $token ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* Output .ics file
|
||||
* Output global .ics feed — wszystkie jachty w jednym pliku.
|
||||
*
|
||||
* @param \WP_Post $yacht Yacht post.
|
||||
* Każdy event ma SUMMARY w formacie "{nazwa_jachtu} - {klient}".
|
||||
* Eventy z _booking_source = 'ical_import_global' są pomijane (anti-loop —
|
||||
* nie wysyłamy z powrotem do Google tego, co stamtąd przyszło).
|
||||
*/
|
||||
private static function output_ics( $yacht ) {
|
||||
private static function output_global_ics() {
|
||||
$bookings = get_posts(
|
||||
array(
|
||||
'post_type' => 'yacht_booking',
|
||||
'posts_per_page' => -1,
|
||||
'post_status' => 'publish',
|
||||
'meta_query' => array(
|
||||
'relation' => 'AND',
|
||||
array(
|
||||
'key' => '_booking_yacht_id',
|
||||
'value' => $yacht->ID,
|
||||
),
|
||||
array(
|
||||
'key' => '_booking_status',
|
||||
'value' => 'cancelled',
|
||||
@@ -156,7 +144,7 @@ class ICal_Feed {
|
||||
$domain = wp_parse_url( home_url(), PHP_URL_HOST );
|
||||
|
||||
header( 'Content-Type: text/calendar; charset=utf-8' );
|
||||
header( 'Content-Disposition: inline; filename="' . sanitize_file_name( $yacht->post_title ) . '.ics"' );
|
||||
header( 'Content-Disposition: inline; filename="yachts-all.ics"' );
|
||||
header( 'Cache-Control: no-cache, must-revalidate' );
|
||||
|
||||
$lines = array();
|
||||
@@ -165,10 +153,32 @@ class ICal_Feed {
|
||||
$lines[] = 'PRODID:-//YachtBooking//NONSGML v1.0//PL';
|
||||
$lines[] = 'CALSCALE:GREGORIAN';
|
||||
$lines[] = 'METHOD:PUBLISH';
|
||||
$lines[] = 'X-WR-CALNAME:' . self::escape_ical( $yacht->post_title . ' - ' . $site_name );
|
||||
$lines[] = 'X-WR-CALNAME:' . self::escape_ical(
|
||||
sprintf(
|
||||
/* translators: %s: site name */
|
||||
__( 'Wszystkie jachty - %s', 'yacht-booking' ),
|
||||
$site_name
|
||||
)
|
||||
);
|
||||
$lines[] = 'X-WR-TIMEZONE:Europe/Warsaw';
|
||||
|
||||
foreach ( $bookings as $booking ) {
|
||||
// Anti-loop: pomiń eventy które zostały zaimportowane z globalnego GCal.
|
||||
$source = get_post_meta( $booking->ID, '_booking_source', true );
|
||||
if ( ICal_Import::GLOBAL_IMPORT_SOURCE === $source ) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$yacht_id = (int) Booking::get_yacht_id( $booking->ID );
|
||||
if ( ! $yacht_id ) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$yacht = get_post( $yacht_id );
|
||||
if ( ! $yacht ) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$start = Booking::get_start_date( $booking->ID );
|
||||
$end = Booking::get_end_date( $booking->ID );
|
||||
$status = Booking::get_status( $booking->ID );
|
||||
@@ -178,10 +188,11 @@ class ICal_Feed {
|
||||
continue;
|
||||
}
|
||||
|
||||
// iCal DTEND for all-day events is exclusive
|
||||
// iCal DTEND for all-day events is exclusive.
|
||||
$end_exclusive = gmdate( 'Ymd', strtotime( $end . ' +1 day' ) );
|
||||
$created = get_the_date( 'Ymd\THis\Z', $booking );
|
||||
|
||||
// Prefiks nazwy jachtu — kluczowy dla późniejszego importu po prefiksie.
|
||||
$summary = sprintf( '%s - %s', $yacht->post_title, $name );
|
||||
if ( 'pending' === $status ) {
|
||||
$summary = '[' . __( 'Oczekująca', 'yacht-booking' ) . '] ' . $summary;
|
||||
|
||||
Reference in New Issue
Block a user