This commit is contained in:
2026-05-06 23:16:47 +02:00
parent ea77c8ea35
commit c4a485e530
23 changed files with 2141 additions and 1772 deletions

View File

@@ -18,14 +18,111 @@ if ( ! defined( 'ABSPATH' ) ) {
class Installer {
/**
* Run installation
* Run installation / upgrade.
*
* Wykonuje cleanup migration (raz, gdy upgrade z wersji < CURRENT)
* przed nadpisaniem yacht_booking_version w `set_version()`.
*/
public function install() {
$this->create_tables();
$this->create_options();
$this->migrate();
$this->set_version();
}
/**
* Cleanup migration — usuwa martwe dane po wycofaniu OAuth + per-yacht iCal (09-03).
*
* Idempotencja: porównuje zapisaną wersję z `YACHT_BOOKING_VERSION`. Jeśli zapisana
* jest >= bieżącej, cleanup nie uruchamia się (`set_version()` w install() nadpisuje).
*
* Cleanup kasuje:
* - meta jachtów: _yacht_gcal_id, _yacht_ical_import_url, _yacht_ical_token, _yacht_ical_last_import
* - opcje OAuth: yacht_booking_gcal_credentials, yacht_booking_gcal_tokens, yacht_booking_gcal_calendar_id, yacht_booking_gcal_sync_enabled
* - rezerwacje z _booking_source = 'ical_import' (per-yacht — nie odświeżane już)
* - stare crony OAuth + per-yacht iCal
*
* NIE kasuje:
* - _yacht_gcal_alias (aktywny dla globalnego importu)
* - rezerwacji 'ical_import_global' (aktywne) ani 'website' (rezerwacje klientów)
*/
private function migrate() {
$installed_version = get_option( 'yacht_booking_version', '' );
// Pierwsza instalacja (brak wpisu) — nic do migracji.
if ( '' === $installed_version ) {
return;
}
// Already on current or newer version — nothing to migrate.
if ( version_compare( $installed_version, YACHT_BOOKING_VERSION, '>=' ) ) {
return;
}
// Delete stale post meta across ALL posts.
$stale_meta_keys = array(
'_yacht_gcal_id',
'_yacht_ical_import_url',
'_yacht_ical_token',
'_yacht_ical_last_import',
);
foreach ( $stale_meta_keys as $meta_key ) {
delete_post_meta_by_key( $meta_key );
}
// Delete stale options (OAuth credentials, tokens, calendar id, sync flag).
$stale_options = array(
'yacht_booking_gcal_credentials',
'yacht_booking_gcal_tokens',
'yacht_booking_gcal_calendar_id',
'yacht_booking_gcal_sync_enabled',
'yacht_booking_gcal_token',
'yacht_booking_gcal_webhook_token',
);
foreach ( $stale_options as $option ) {
delete_option( $option );
}
// Delete bookings imported by old per-yacht iCal mechanism.
$stale_bookings = get_posts(
array(
'post_type' => 'yacht_booking',
'posts_per_page' => -1,
'fields' => 'ids',
'post_status' => 'any',
'meta_query' => array(
array(
'key' => '_booking_source',
'value' => 'ical_import',
),
),
)
);
if ( ! empty( $stale_bookings ) ) {
// Ensure Availability class is loaded (Installer is invoked from activation hook
// before main plugin bootstrap, so autoloader may not be active yet).
if ( ! class_exists( '\YachtBooking\Availability' ) ) {
require_once YACHT_BOOKING_PLUGIN_DIR . 'includes/class-availability.php';
}
foreach ( $stale_bookings as $booking_id ) {
\YachtBooking\Availability::clear_booking_availability( $booking_id );
wp_delete_post( $booking_id, true );
}
}
// Clear cron hooks for removed mechanisms.
$stale_cron_hooks = array(
'yacht_booking_ical_import', // per-yacht iCal import
'yacht_booking_pull_from_gcal', // OAuth pull
'yacht_booking_sync_to_gcal', // OAuth push
'yacht_booking_update_in_gcal', // OAuth update
'yacht_booking_delete_from_gcal', // OAuth delete
);
foreach ( $stale_cron_hooks as $hook ) {
wp_clear_scheduled_hook( $hook );
}
}
/**
* Create custom database tables
*/
@@ -67,7 +164,9 @@ class Installer {
'yacht_booking_currency_symbol' => 'zł',
'yacht_booking_terms_page_id' => 0,
'yacht_booking_enable_notifications' => 'yes',
'yacht_booking_gcal_sync_enabled' => 'no',
'yacht_booking_global_ical_import_url' => '',
'yacht_booking_global_ical_token' => '',
'yacht_booking_global_ical_last_import' => '',
);
foreach ( $options as $key => $value ) {

View File

@@ -83,18 +83,7 @@ class Yacht_Booking {
Admin::get_instance();
}
// Load Google Calendar integration
require_once YACHT_BOOKING_PLUGIN_DIR . 'integrations/google-calendar/class-oauth-handler.php';
require_once YACHT_BOOKING_PLUGIN_DIR . 'integrations/google-calendar/class-gcal-service.php';
require_once YACHT_BOOKING_PLUGIN_DIR . 'integrations/google-calendar/class-sync-controller.php';
// Initialize sync controller
\YachtBooking\Integrations\GoogleCalendar\Sync_Controller::get_instance();
// Register cron actions
\YachtBooking\Integrations\GoogleCalendar\Sync_Controller::register_cron_actions();
// Load iCal integration
// Load iCal integration (globalna sync z 09-02 — jedyny mechanizm sync z GCal)
require_once YACHT_BOOKING_PLUGIN_DIR . 'integrations/ical/class-ical-feed.php';
require_once YACHT_BOOKING_PLUGIN_DIR . 'integrations/ical/class-ical-import.php';

View File

@@ -92,16 +92,6 @@ class Yacht {
return (float) get_post_meta( $yacht_id, '_yacht_price_per_day', true );
}
/**
* Get yacht Google Calendar ID
*
* @param int $yacht_id Yacht post ID.
* @return string
*/
public static function get_gcal_id( $yacht_id ) {
return get_post_meta( $yacht_id, '_yacht_gcal_id', true );
}
/**
* Get yacht features
*
@@ -134,13 +124,23 @@ class Yacht {
}
/**
* Update yacht Google Calendar ID
* Get yacht alias for Google Calendar global sync.
*
* @param int $yacht_id Yacht post ID.
* @return string
*/
public static function get_gcal_alias( $yacht_id ) {
return (string) get_post_meta( $yacht_id, '_yacht_gcal_alias', true );
}
/**
* Update yacht alias for Google Calendar global sync.
*
* @param int $yacht_id Yacht post ID.
* @param string $gcal_id Google Calendar ID.
* @param string $alias Alias.
*/
public static function update_gcal_id( $yacht_id, $gcal_id ) {
update_post_meta( $yacht_id, '_yacht_gcal_id', sanitize_text_field( $gcal_id ) );
public static function update_gcal_alias( $yacht_id, $alias ) {
update_post_meta( $yacht_id, '_yacht_gcal_alias', sanitize_text_field( $alias ) );
}
/**