Files
2026-05-07 14:57:59 +02:00

188 lines
5.6 KiB
PHP

<?php
/**
* Plugin Installer Class
*
* @package YachtBooking
*/
namespace YachtBooking;
// Exit if accessed directly
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
/**
* Installer class - handles plugin installation and database setup
*/
class Installer {
/**
* 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
*/
private function create_tables() {
global $wpdb;
$charset_collate = $wpdb->get_charset_collate();
$table_name = $wpdb->prefix . 'yacht_availability';
$sql = "CREATE TABLE $table_name (
id bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT,
yacht_id bigint(20) UNSIGNED NOT NULL,
date date NOT NULL,
status varchar(20) NOT NULL DEFAULT 'available',
booking_id bigint(20) UNSIGNED NULL,
created_at datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
updated_at datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY (id),
UNIQUE KEY yacht_date (yacht_id, date),
KEY yacht_id (yacht_id),
KEY date (date),
KEY status (status),
KEY booking_id (booking_id)
) $charset_collate;";
require_once ABSPATH . 'wp-admin/includes/upgrade.php';
dbDelta( $sql );
}
/**
* Create default plugin options
*/
private function create_options() {
$options = array(
'yacht_booking_default_status' => 'pending',
'yacht_booking_email_from_name' => get_bloginfo( 'name' ),
'yacht_booking_email_from' => get_bloginfo( 'admin_email' ),
'yacht_booking_date_format' => 'Y-m-d',
'yacht_booking_currency_symbol' => 'zł',
'yacht_booking_terms_page_id' => 0,
'yacht_booking_enable_notifications' => 'yes',
'yacht_booking_global_ical_import_url' => '',
'yacht_booking_global_ical_token' => '',
'yacht_booking_global_ical_last_import' => '',
'yacht_booking_ical_sync_mode' => 'per_yacht',
);
foreach ( $options as $key => $value ) {
if ( false === get_option( $key ) ) {
add_option( $key, $value );
}
}
}
/**
* Set plugin version
*/
private function set_version() {
update_option( 'yacht_booking_version', YACHT_BOOKING_VERSION );
update_option( 'yacht_booking_installed_at', current_time( 'mysql' ) );
}
}