get_results( $wpdb->prepare( "SELECT option_name, option_value FROM {$wpdb->options} WHERE option_name IN ({$placeholders})", $names ), ARRAY_A ); // phpcs:enable // Issued passes, and also bookings whose pass could not be created (create_error, // no share_url), so the admin list can say why. $found = []; foreach ((array) $rows as $row) { $value = maybe_unserialize($row['option_value']); if (is_array($value) && (!empty($value['share_url']) || !empty($value['create_error'])) && isset($by_option[$row['option_name']])) { $found[$by_option[$row['option_name']]] = $value; } } return $found; } /** * @param string $logo_url The logo the pass was created with. Kept because expiring the * pass has to send it again (see ATW_Bookly_Api_Client::expire_pass). */ public static function save($booking_key, array $result, $logo_url = '', $fingerprint = '') { $value = [ 'card_id' => isset($result['card_id']) ? $result['card_id'] : '', 'share_url' => $result['share_url'], 'logo_url' => (string) $logo_url, 'fingerprint' => (string) $fingerprint, 'created' => current_time('mysql'), ]; // autoload 'no' — there could be one row per booking, and none of them are // needed on a normal page load. if (!add_option(self::key($booking_key), $value, '', 'no')) { update_option(self::key($booking_key), $value, 'no'); } return $value; } /** * Records that the pass has been expired, so a booking whose status is toggled back * and forth does not call the API again on every change. */ public static function mark_expired($booking_key) { $stored = self::get($booking_key); if (!$stored) { return null; } $stored['expired'] = current_time('mysql'); update_option(self::key($booking_key), $stored, 'no'); return $stored; } /** * Records why expiring the pass failed, for the admin list. The status sync retries * passes carrying one of these on its next run. */ public static function record_expire_error($booking_key, $message) { $stored = self::get($booking_key); if (!$stored) { return null; } $stored['expire_error'] = [ 'message' => (string) $message, 'at' => current_time('mysql'), ]; update_option(self::key($booking_key), $stored, 'no'); return $stored; } /** * How many passes have been issued for a provider, for the Pass design banner. One * COUNT over the options table; cached for a minute so a busy admin screen does not * repeat it. */ public static function count($provider = 'bookly') { global $wpdb; $cache_key = 'atw_bookly_pass_count_' . sanitize_key($provider); $cached = wp_cache_get($cache_key, 'atw_bookly'); if ($cached !== false) { return (int) $cached; } $like = $wpdb->esc_like('atw_bookly_pass_' . sanitize_key($provider) . '_') . '%'; // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery -- counting this plugin's own per-booking options; there is no options API for a prefix count. Cached above. $count = (int) $wpdb->get_var($wpdb->prepare("SELECT COUNT(*) FROM {$wpdb->options} WHERE option_name LIKE %s", $like)); wp_cache_set($cache_key, $count, 'atw_bookly', MINUTE_IN_SECONDS); return $count; } /** * Records why creating a booking's pass failed (no credits, API down). Stored without a * share_url, so get() still reports "no pass" and the next attempt is not blocked; a * later successful save() replaces it. */ public static function record_create_error($booking_key, $message) { if (self::get($booking_key)) { return null; } $value = [ 'create_error' => [ 'message' => (string) $message, 'at' => current_time('mysql'), ], ]; if (!add_option(self::key($booking_key), $value, '', 'no')) { update_option(self::key($booking_key), $value, 'no'); } return $value; } /** * A short hash of what a pass says, kept with the pass so a change in Bookly that does * not alter the pass (a note, a payment) sends no update. */ public static function fingerprint(array $payload) { return substr(md5((string) wp_json_encode($payload)), 0, 16); } /** Records a successful update (a rescheduled booking) and clears an earlier failure. */ public static function mark_updated($booking_key, $logo_url = '', $fingerprint = '') { $stored = self::get($booking_key); if (!$stored) { return null; } $stored['updated'] = current_time('mysql'); if ((string) $fingerprint !== '') { $stored['fingerprint'] = (string) $fingerprint; } if ((string) $logo_url !== '') { $stored['logo_url'] = (string) $logo_url; } unset($stored['update_error']); update_option(self::key($booking_key), $stored, 'no'); return $stored; } /** Records why updating the pass failed, for the admin list. */ public static function record_update_error($booking_key, $message) { $stored = self::get($booking_key); if (!$stored) { return null; } $stored['update_error'] = [ 'message' => (string) $message, 'at' => current_time('mysql'), ]; update_option(self::key($booking_key), $stored, 'no'); return $stored; } public static function is_expired($booking_key) { $stored = self::get($booking_key); return $stored && !empty($stored['expired']); } /** * Every pass for a provider that is still live (has a pass, not expired), keyed by * booking id, for the status sync: it checks these for deleted bookings and retries * failed expiries and updates. Capped, newest bookings first. * * @return array booking id => stored record */ public static function live($provider = 'bookly', $limit = 5000) { global $wpdb; $prefix = 'atw_bookly_pass_' . sanitize_key($provider) . '_'; $like = $wpdb->esc_like($prefix) . '%'; // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- reading this plugin's own per-booking options by prefix; there is no options API for that. Runs from the status sync only. $rows = $wpdb->get_results($wpdb->prepare("SELECT option_name, option_value FROM {$wpdb->options} WHERE option_name LIKE %s ORDER BY option_id DESC LIMIT %d", $like, absint($limit)), ARRAY_A); $live = []; foreach ((array) $rows as $row) { $id = absint(substr((string) $row['option_name'], strlen($prefix))); $value = maybe_unserialize($row['option_value']); if ($id && is_array($value) && !empty($value['share_url']) && !empty($value['card_id']) && empty($value['expired'])) { $live[$id] = $value; } } return $live; } }