__( 'TableEngine reservations', 'tableengine' ), 'callback' => array( __CLASS__, 'export' ), ); return $exporters; } public function register_eraser( $erasers ) { $erasers['tableengine'] = array( 'eraser_friendly_name' => __( 'TableEngine reservations', 'tableengine' ), 'callback' => array( __CLASS__, 'erase' ), ); return $erasers; } public function policy_text() { if ( ! function_exists( 'wp_add_privacy_policy_content' ) ) { return; } $text = '
' . __( 'When you book a table we store your name, email address, phone number, party size, date and time, any requests or occasion you enter, and your IP address. We use them to manage your booking, send confirmations and reminders, and recognise you on future visits. Deposits are processed by our payment provider; we do not store card details.', 'tableengine' ) . '
' . '' . __( 'Staff may add notes to your guest record. You can ask for a copy of your data or for it to be erased; past bookings are then anonymised.', 'tableengine' ) . '
'; wp_add_privacy_policy_content( 'TableEngine', wp_kses_post( apply_filters( 'tableengine_privacy_policy_text', $text ) ) ); } /** Reservation rows for an email address (case-insensitive), oldest first. */ public static function reservations_for( $email, $limit = 0, $offset = 0 ) { global $wpdb; $t = Install::table( 'reservations' ); $email = strtolower( trim( (string) $email ) ); if ( ! is_email( $email ) ) { return array(); } $sql = $wpdb->prepare( "SELECT * FROM {$t} WHERE LOWER(email) = %s ORDER BY start_at ASC, id ASC", $email ); // phpcs:ignore WordPress.DB, PluginCheck.Security.DirectDB -- Plugin's own custom table: the name is $wpdb->prefix plus a fixed suffix and values are prepared, so there is no core API or cache to use. if ( $limit ) { $sql .= $wpdb->prepare( ' LIMIT %d OFFSET %d', $limit, $offset ); } return (array) $wpdb->get_results( $sql, ARRAY_A ); // phpcs:ignore WordPress.DB, PluginCheck.Security.DirectDB -- Plugin's own custom table: the name is $wpdb->prefix plus a fixed suffix and values are prepared, so there is no core API or cache to use. } public static function export( $email, $page = 1 ) { $per = 50; $page = max( 1, (int) $page ); $items = array(); if ( 1 === $page ) { $g = Guests::find( $email ); if ( $g ) { $items[] = array( 'group_id' => 'tableengine-guest', 'group_label' => __( 'Restaurant guest record', 'tableengine' ), 'item_id' => 'tableengine-guest-' . $g['id'], 'data' => self::pairs( array( __( 'Name', 'tableengine' ) => $g['name'], __( 'Email', 'tableengine' ) => $g['email'], __( 'Phone', 'tableengine' ) => $g['phone'], __( 'Visits', 'tableengine' ) => $g['visits'], __( 'No-shows', 'tableengine' ) => $g['no_shows'], __( 'Tags', 'tableengine' ) => $g['tags'], __( 'Staff notes', 'tableengine' ) => $g['notes'], __( 'Marketing consent', 'tableengine' ) => $g['marketing_optin'] ? __( 'Yes', 'tableengine' ) : __( 'No', 'tableengine' ), ) ), ); } } $rows = self::reservations_for( $email, $per, ( $page - 1 ) * $per ); foreach ( $rows as $r ) { $data = array( __( 'Reference', 'tableengine' ) => $r['reference'], __( 'Date', 'tableengine' ) => Reservations::format_date( $r['start_at'] ) . ' ' . Reservations::format_time( $r['start_at'] ), __( 'Guests', 'tableengine' ) => $r['party_size'], __( 'Status', 'tableengine' ) => Reservations::statuses()[ $r['status'] ] ?? $r['status'], __( 'Name', 'tableengine' ) => $r['name'], __( 'Email', 'tableengine' ) => $r['email'], __( 'Phone', 'tableengine' ) => $r['phone'], __( 'Table', 'tableengine' ) => $r['table_label'], __( 'Occasion', 'tableengine' ) => $r['occasion'], __( 'Requests', 'tableengine' ) => $r['notes'], __( 'Deposit', 'tableengine' ) => $r['amount'] ? Reservations::format_money( $r['amount'], $r['currency'] ) : '', __( 'IP address', 'tableengine' ) => $r['ip'], ); $items[] = array( 'group_id' => 'tableengine-reservations', 'group_label' => __( 'Restaurant reservations', 'tableengine' ), 'item_id' => 'tableengine-reservation-' . $r['id'], /** Add personal data extensions keep for a reservation (label => value). */ 'data' => self::pairs( (array) apply_filters( 'tableengine_privacy_export_reservation', $data, $r ) ), ); } return array( 'data' => $items, 'done' => count( $rows ) < $per, ); } private static function pairs( array $map ) { $out = array(); foreach ( $map as $name => $value ) { if ( '' !== (string) $value ) { $out[] = array( 'name' => $name, 'value' => (string) $value ); } } return $out; } /** Upcoming bookings that still need the guest's details. */ public static function is_upcoming( array $row ) { return in_array( $row['status'], array( 'pending_payment', 'pending', 'confirmed', 'seated' ), true ) && strtotime( get_gmt_from_date( $row['end_at'] ) ) > time(); } /** * Remove personal data from a reservation, keeping the booking for totals. * Deletes its extra data (meta) too. */ public static function anonymise_reservation( $id ) { global $wpdb; $row = Reservations::get( $id ); if ( ! $row ) { return false; } $wpdb->update( // phpcs:ignore WordPress.DB, PluginCheck.Security.DirectDB -- Plugin's own custom table: the name is $wpdb->prefix plus a fixed suffix and values are prepared, so there is no core API or cache to use. Install::table( 'reservations' ), array( 'name' => self::ANON_NAME, 'email' => '', 'phone' => '', 'notes' => '', 'internal_notes' => '', 'occasion' => '', 'ip' => '', 'marketing_optin' => 0, 'guest_id' => 0, 'updated_gmt' => current_time( 'mysql', true ), ), array( 'id' => (int) $id ) ); $wpdb->delete( Install::table( 'reservationmeta' ), array( 'tableengine_reservation_id' => (int) $id ) ); // phpcs:ignore WordPress.DB, PluginCheck.Security.DirectDB -- Plugin's own custom table: the name is $wpdb->prefix plus a fixed suffix and values are prepared, so there is no core API or cache to use. wp_cache_delete( (int) $id, 'tableengine_reservation_meta' ); do_action( 'tableengine_reservation_anonymised', $row ); return true; } /** Delete a guest record and its meta. */ public static function delete_guest( $guest_id ) { global $wpdb; $guest_id = (int) $guest_id; if ( ! $guest_id || ! Guests::get( $guest_id ) ) { return false; } $wpdb->delete( Install::table( 'guests' ), array( 'id' => $guest_id ) ); // phpcs:ignore WordPress.DB, PluginCheck.Security.DirectDB -- Plugin's own custom table: the name is $wpdb->prefix plus a fixed suffix and values are prepared, so there is no core API or cache to use. $wpdb->delete( Install::table( 'guestmeta' ), array( 'tableengine_guest_id' => $guest_id ) ); // phpcs:ignore WordPress.DB, PluginCheck.Security.DirectDB -- Plugin's own custom table: the name is $wpdb->prefix plus a fixed suffix and values are prepared, so there is no core API or cache to use. wp_cache_delete( $guest_id, 'tableengine_guest_meta' ); do_action( 'tableengine_guest_deleted', $guest_id ); return true; } /** WordPress eraser callback. */ public static function erase( $email, $page = 1 ) { $removed = false; $retained = false; $messages = array(); $keep_ids = array(); foreach ( self::reservations_for( $email ) as $r ) { if ( self::is_upcoming( $r ) ) { $retained = true; $keep_ids[] = (int) $r['guest_id']; continue; } $removed = self::anonymise_reservation( $r['id'] ) || $removed; } if ( $retained ) { $messages[] = __( 'Upcoming restaurant bookings were kept so the restaurant can still contact the guest; erase again after the visit.', 'tableengine' ); } $g = Guests::find( $email ); if ( $g ) { if ( in_array( (int) $g['id'], $keep_ids, true ) ) { $retained = true; } else { $removed = self::delete_guest( $g['id'] ) || $removed; } } return array( 'items_removed' => $removed, 'items_retained' => $retained, 'messages' => $messages, 'done' => true, ); } }