) are copied, so a booking * that already has a pass keeps it (no second credit on a reload) and can still be * expired when it is cancelled. The originals are left alone. * * And while the 1.x plugin is still active, its button and its expiry sync are switched * off, so customers see one set of buttons and one pass, not two, and an admin notice asks * for it to be deactivated. Every name this plugin defines differs from the 1.x plugin's, * so having both active never fatals. */ if (!defined('ABSPATH')) { exit; } class ATW_Bookly_Legacy { /** 1.x option names, newest first. */ const OPTIONS = ['bookly_atw_settings']; /** The 1.x per-booking pass records, and the 1.x expiry cron event. */ const OLD_PASS_PREFIX = 'bookly_atw_pass_bookly_'; const OLD_CRON_HOOK = 'bookly_atw_sync_statuses'; /** Settings keys 1.x had that mean the same thing here. */ const KEYS = ['api_key', 'logo_url', 'hero_image', 'background_color', 'button_label', 'allowed_statuses', 'expire_statuses', 'barcode_type', 'debug']; public static function init() { // On init, not plugins_loaded: the defaults it stores are translated strings, and // WordPress 6.7+ warns when a text domain loads before init. add_action('init', [__CLASS__, 'maybe_import'], 1); // After the 1.x plugin's own plugins_loaded callback (priority 10) has hooked in. add_action('plugins_loaded', [__CLASS__, 'quiet_old_plugin'], 20); add_action('admin_notices', [__CLASS__, 'notice']); } /** True while the 1.x plugin is loaded. */ public static function old_plugin_active() { return defined('BOOKLY_ATW_VERSION') && class_exists('Bookly_ATW_Assets', false); } /** * Copies 1.x settings and pass records, once. Runs only while this plugin has no * settings of its own, so it can never overwrite anything set here. */ public static function maybe_import() { if (get_option(ATW_Bookly_Settings::OPTION) !== false) { return false; } $legacy = null; foreach (self::OPTIONS as $name) { $value = get_option($name); if (is_array($value) && $value) { $legacy = $value; break; } } if (!$legacy) { return false; } $settings = ATW_Bookly_Settings::defaults(); foreach (self::KEYS as $key) { if (array_key_exists($key, $legacy)) { $settings[$key] = $legacy[$key]; } } $settings['imported_from'] = '1.x'; add_option(ATW_Bookly_Settings::OPTION, $settings); self::import_passes(); return true; } /** * One INSERT … SELECT rather than a loop, so a site with thousands of bookings is not * held up. INSERT IGNORE: a record this plugin already has is never replaced. */ private static function import_passes() { global $wpdb; $old = $wpdb->esc_like(self::OLD_PASS_PREFIX) . '%'; // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- one-time copy of this plugin family's own per-booking options; there is no options API for a bulk copy. $wpdb->query( $wpdb->prepare( "INSERT IGNORE INTO {$wpdb->options} (option_name, option_value, autoload) SELECT CONCAT(%s, SUBSTRING(option_name, %d)), option_value, 'no' FROM {$wpdb->options} WHERE option_name LIKE %s", 'atw_bookly_pass_bookly_', strlen(self::OLD_PASS_PREFIX) + 1, $old ) ); wp_cache_delete('alloptions', 'options'); } /** * With both plugins active, only this one acts: the 1.x front-end script is not * enqueued (one set of buttons, one credit) and its expiry sync is unhooked and * unscheduled (its passes were imported, so this plugin expires them, with the logo * resent). */ public static function quiet_old_plugin() { if (!self::old_plugin_active()) { return; } remove_action('wp_enqueue_scripts', ['Bookly_ATW_Assets', 'enqueue'], 20); remove_action(self::OLD_CRON_HOOK, ['Bookly_ATW_Status_Listener', 'sync']); remove_action('init', ['Bookly_ATW_Status_Listener', 'ensure_scheduled']); add_action('init', [__CLASS__, 'unschedule_old_sync']); } public static function unschedule_old_sync() { if (wp_next_scheduled(self::OLD_CRON_HOOK)) { wp_clear_scheduled_hook(self::OLD_CRON_HOOK); } } public static function notice() { if (!self::old_plugin_active() || !current_user_can('activate_plugins')) { return; } $screen = function_exists('get_current_screen') ? get_current_screen() : null; if ($screen && !in_array($screen->id, ['plugins', 'dashboard', 'toplevel_page_' . ATW_Bookly_Settings::PAGE], true)) { return; } echo '

'; echo esc_html__('The older AddToWallet for Bookly 1.x plugin (folder bookly-addtowallet) is still active. Its settings and passes have been copied into this version, which now creates and expires the passes, so the older plugin does nothing. Please deactivate and delete it.', 'addtowallet-for-bookly'); echo ' ' . esc_html__('Go to Plugins', 'addtowallet-for-bookly') . ''; echo '

'; } }