update
This commit is contained in:
@@ -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 ) {
|
||||
|
||||
Reference in New Issue
Block a user