self::INTERVAL, 'display' => __('Every ten minutes (AddToWallet for Bookly)', 'addtowallet-for-bookly'), ]; return $schedules; } public static function on_activate() { // Start from now: nothing changed before the plugin existed can have a pass. if (get_option(self::OPTION_LAST_SYNC) === false) { add_option(self::OPTION_LAST_SYNC, time(), '', 'no'); } self::ensure_scheduled(); } public static function on_deactivate() { wp_clear_scheduled_hook(self::CRON_HOOK); } public static function ensure_scheduled() { if (!wp_next_scheduled(self::CRON_HOOK)) { wp_schedule_event(time() + self::INTERVAL, self::CRON_SCHEDULE, self::CRON_HOOK); } } /** Queues a sync at the end of a Bookly admin-ajax request that can change a booking. */ public static function watch_ajax() { if (!wp_doing_ajax() || self::$queued) { return; } // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- only reads which Bookly action this request is, to decide whether to sync afterwards; Bookly checks its own nonce. $action = isset($_REQUEST['action']) && is_string($_REQUEST['action']) ? sanitize_key(wp_unslash($_REQUEST['action'])) : ''; if (strpos($action, 'bookly_') !== 0 || preg_match(self::READ_ONLY, $action)) { return; } self::$queued = true; $deletes = strpos($action, 'delete') !== false; // Bookly answers with wp_send_json(), which exits; shutdown still runs after that. add_action('shutdown', function () use ($deletes) { self::sync(); if ($deletes) { self::sweep(); } }); } /** WP-Cron: changed bookings, then every live pass. */ public static function cron() { self::sync(); self::sweep(); } /** Which statuses the site has opted into expiring on. Empty means the feature is off. */ private static function expire_statuses() { $settings = ATW_Bookly_Settings::get(); return array_values(array_intersect((array) $settings['expire_statuses'], ATW_Bookly_Settings::EXPIRABLE_STATUSES)); } private static function provider() { $provider = ATW_Bookly_Providers::get(self::PROVIDER); return $provider && $provider->is_available() ? $provider : null; } /** * Everything that changed since the last run. Safe to call as often as wanted: it is * idempotent, and a booking with no pass costs one array lookup. */ public static function sync() { $provider = self::provider(); if (!$provider) { return; } $now = time(); $last = (int) get_option(self::OPTION_LAST_SYNC, 0); if (!$last) { $last = $now - self::INTERVAL; } // Bookly's stamps are site time, which is what wp_date() prints. $since = wp_date('Y-m-d H:i:s', $last - self::OVERLAP); $ids = $provider->bookings_changed_since($since); if ($ids) { $keys = array_map(function ($id) { return self::PROVIDER . ':' . $id; }, $ids); $stored = ATW_Bookly_Pass_Store::get_many($keys); foreach ($ids as $id) { if (isset($stored[self::PROVIDER . ':' . $id])) { self::sync_one($id, true); } } } update_option(self::OPTION_LAST_SYNC, $now, 'no'); } /** * Every live pass: expired if its booking has been deleted, retried if an earlier * expiry or update failed. */ public static function sweep() { $provider = self::provider(); if (!$provider) { return; } $live = ATW_Bookly_Pass_Store::live(self::PROVIDER); if (!$live) { return; } foreach ($provider->missing_bookings(array_keys($live)) as $id) { self::sync_one($id, false); unset($live[$id]); } foreach ($live as $id => $stored) { if (!empty($stored['expire_error']) || !empty($stored['update_error'])) { self::sync_one($id, true); } } } /** * Brings one booking's pass in line with the booking as Bookly now has it. * * @param int $booking_id * @param bool $changed True when Bookly says the booking changed, so a pass that * predates fingerprints (one imported from 1.x) is updated too. */ public static function sync_one($booking_id, $changed) { $booking_id = absint($booking_id); $key = self::PROVIDER . ':' . $booking_id; if (!$booking_id) { return; } $stored = ATW_Bookly_Pass_Store::get($key); if (!$stored || !empty($stored['expired']) || empty($stored['card_id'])) { return; } $provider = self::provider(); if (!$provider) { return; } $settings = ATW_Bookly_Settings::get(); $expire = self::expire_statuses(); $booking = $provider->get_booking($booking_id); if (!$booking) { // Deleted in Bookly: nobody is coming, the same as a cancellation. if (in_array('cancelled', $expire, true)) { self::expire($key, $booking_id, 'deleted', $stored, $settings); } return; } if (in_array($booking['status'], $expire, true)) { self::expire($key, $booking_id, $booking['status'], $stored, $settings); return; } if (in_array($booking['status'], ATW_Bookly_Settings::EXPIRABLE_STATUSES, true)) { // Not happening, but this site chose not to expire passes for it. return; } $payload = ATW_Bookly_Pass_Builder::build($booking, $settings); $fingerprint = ATW_Bookly_Pass_Store::fingerprint($payload); $known = isset($stored['fingerprint']) ? (string) $stored['fingerprint'] : ''; if ($known === $fingerprint || ($known === '' && !$changed)) { return; } self::update($key, $booking_id, $stored, $payload, $fingerprint, $settings); } private static function update($key, $booking_id, array $stored, array $payload, $fingerprint, array $settings) { $result = ATW_Bookly_Api_Client::update_pass($stored['card_id'], $payload, $settings); if (is_wp_error($result)) { ATW_Bookly_Pass_Store::record_update_error($key, $result->get_error_message()); self::log('update failed for booking ' . $booking_id . ': ' . $result->get_error_message()); return; } ATW_Bookly_Pass_Store::mark_updated($key, isset($payload['logoUrl']) ? $payload['logoUrl'] : '', $fingerprint); self::log('updated pass for booking ' . $booking_id); /** * Fires after a booking's pass has been updated (rescheduled, or its details changed). * * @param int $booking_id * @param array $payload The payload the pass was updated with. */ do_action('atw_bookly_pass_updated', $booking_id, $payload); } /** * The logo is resent with the expiry: a stateType-only edit makes the API substitute * an unreachable placeholder logo and fail (see ATW_Bookly_Api_Client::expire_pass). */ private static function expire($key, $booking_id, $status, array $stored, array $settings) { // Passes from 1.x were saved without their logo; they fall back to today's setting. $logo = !empty($stored['logo_url']) ? $stored['logo_url'] : ATW_Bookly_Pass_Builder::logo($settings); $result = ATW_Bookly_Api_Client::expire_pass($stored['card_id'], $settings, $logo); if (is_wp_error($result)) { // Kept on the pass record for the admin list; the next cron sweep retries it. ATW_Bookly_Pass_Store::record_expire_error($key, $result->get_error_message()); self::log('expire failed for booking ' . $booking_id . ': ' . $result->get_error_message()); return; } ATW_Bookly_Pass_Store::mark_expired($key); self::log('expired pass for booking ' . $booking_id . ' (' . $status . ')'); /** * Fires after a booking's pass has been expired. * * @param int $booking_id * @param string $status The booking's status, or 'deleted'. * @param array $stored The stored pass record. */ do_action('atw_bookly_pass_expired', $booking_id, $status, $stored); } private static function log($message) { $settings = ATW_Bookly_Settings::get(); if (empty($settings['debug'])) { return; } // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_error_log -- opt-in debug logging, off by default. error_log('[addtowallet-for-bookly] ' . $message); } }