query( "UPDATE {$res} SET table_ids = table_id WHERE table_ids = '' AND table_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->query( "UPDATE {$res} SET gateway = 'stripe' WHERE gateway = '' AND stripe_intent <> ''" ); // 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. Guests::backfill(); do_action( 'tableengine_migrate', TABLEENGINE_DB_VERSION ); } public static function table( $name ) { global $wpdb; return $wpdb->prefix . 'tableengine_' . $name; } /** * Expose meta tables to the WordPress metadata API (get_metadata( 'tableengine_reservation', … )). */ public static function register_meta_tables() { global $wpdb; $wpdb->tableengine_reservationmeta = self::table( 'reservationmeta' ); $wpdb->tableengine_guestmeta = self::table( 'guestmeta' ); } /** * Every table the plugin owns (used by uninstall). */ public static function tables() { return apply_filters( 'tableengine_tables', array( 'floorplans', 'reservations', 'reservationmeta', 'guests', 'guestmeta', 'events' ) ); } /** * Remove data on uninstall, only when the owner opted in. */ public static function uninstall() { global $wpdb; // Every scheduled job the plugin (or a premium module) added, whatever its arguments. foreach ( (array) _get_cron_array() as $events ) { foreach ( array_keys( (array) $events ) as $hook ) { if ( 0 === strpos( (string) $hook, 'tableengine_' ) ) { wp_unschedule_hook( $hook ); } } } $settings = get_option( 'tableengine_settings', array() ); if ( empty( $settings['delete_on_uninstall'] ) ) { return; } foreach ( self::tables() as $name ) { $wpdb->query( 'DROP TABLE IF EXISTS ' . self::table( $name ) ); // 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. } // Gift vouchers (premium) are posts. foreach ( $wpdb->get_col( $wpdb->prepare( "SELECT ID FROM {$wpdb->posts} WHERE post_type = %s", 'tableengine_voucher' ) ) as $id ) { // phpcs:ignore WordPress.DB wp_delete_post( (int) $id, true ); } remove_role( 'tableengine_host' ); remove_role( 'tableengine_manager' ); // Options, transients and user meta all share the tableengine_ prefix. $like = $wpdb->esc_like( 'tableengine_' ) . '%'; $wpdb->query( $wpdb->prepare( "DELETE FROM {$wpdb->options} WHERE option_name LIKE %s OR option_name LIKE %s OR option_name LIKE %s", $like, $wpdb->esc_like( '_transient_tableengine_' ) . '%', $wpdb->esc_like( '_transient_timeout_tableengine_' ) . '%' ) ); // phpcs:ignore WordPress.DB $wpdb->query( $wpdb->prepare( "DELETE FROM {$wpdb->usermeta} WHERE meta_key LIKE %s", $like ) ); // phpcs:ignore WordPress.DB wp_cache_flush(); do_action( 'tableengine_uninstalled' ); } public static function create_tables() { global $wpdb; require_once ABSPATH . 'wp-admin/includes/upgrade.php'; $charset = $wpdb->get_charset_collate(); $plans = self::table( 'floorplans' ); $res = self::table( 'reservations' ); $resmeta = self::table( 'reservationmeta' ); $guests = self::table( 'guests' ); $gmeta = self::table( 'guestmeta' ); $events = self::table( 'events' ); dbDelta( "CREATE TABLE {$plans} ( id bigint(20) unsigned NOT NULL AUTO_INCREMENT, name varchar(190) NOT NULL DEFAULT '', data longtext NOT NULL, is_active tinyint(1) NOT NULL DEFAULT 1, sort_order int(11) NOT NULL DEFAULT 0, location_id bigint(20) unsigned NOT NULL DEFAULT 0, created_at datetime NOT NULL, updated_at datetime NOT NULL, PRIMARY KEY (id), KEY is_active (is_active), KEY location_id (location_id) ) {$charset};" ); dbDelta( "CREATE TABLE {$res} ( id bigint(20) unsigned NOT NULL AUTO_INCREMENT, reference varchar(20) NOT NULL DEFAULT '', token varchar(64) NOT NULL DEFAULT '', floorplan_id bigint(20) unsigned NOT NULL DEFAULT 0, table_id varchar(64) NOT NULL DEFAULT '', table_ids varchar(255) NOT NULL DEFAULT '', table_label varchar(190) NOT NULL DEFAULT '', location_id bigint(20) unsigned NOT NULL DEFAULT 0, shift_id bigint(20) unsigned NOT NULL DEFAULT 0, guest_id bigint(20) unsigned NOT NULL DEFAULT 0, party_size smallint(5) unsigned NOT NULL DEFAULT 1, start_at datetime NOT NULL, end_at datetime NOT NULL, status varchar(32) NOT NULL DEFAULT 'pending', name varchar(190) NOT NULL DEFAULT '', email varchar(190) NOT NULL DEFAULT '', phone varchar(64) NOT NULL DEFAULT '', occasion varchar(120) NOT NULL DEFAULT '', notes text NULL, internal_notes text NULL, amount int(11) NOT NULL DEFAULT 0, currency varchar(3) NOT NULL DEFAULT '', payment_status varchar(32) NOT NULL DEFAULT 'none', gateway varchar(32) NOT NULL DEFAULT '', stripe_intent varchar(190) NOT NULL DEFAULT '', hold_expires_gmt datetime NULL, reminder_sent tinyint(1) NOT NULL DEFAULT 0, marketing_optin tinyint(1) NOT NULL DEFAULT 0, source varchar(32) NOT NULL DEFAULT 'web', ip varchar(64) NOT NULL DEFAULT '', created_gmt datetime NOT NULL, updated_gmt datetime NOT NULL, PRIMARY KEY (id), UNIQUE KEY reference (reference), KEY start_at (start_at), KEY status (status), KEY table_lookup (floorplan_id, table_id, start_at), KEY stripe_intent (stripe_intent), KEY email (email), KEY guest_id (guest_id), KEY location_id (location_id) ) {$charset};" ); // Column name must be {$type}_id for the WordPress metadata API. dbDelta( "CREATE TABLE {$resmeta} ( meta_id bigint(20) unsigned NOT NULL AUTO_INCREMENT, tableengine_reservation_id bigint(20) unsigned NOT NULL DEFAULT 0, meta_key varchar(255) DEFAULT NULL, meta_value longtext NULL, PRIMARY KEY (meta_id), KEY tableengine_reservation_id (tableengine_reservation_id), KEY meta_key (meta_key(191)) ) {$charset};" ); dbDelta( "CREATE TABLE {$guests} ( id bigint(20) unsigned NOT NULL AUTO_INCREMENT, user_id bigint(20) unsigned NOT NULL DEFAULT 0, name varchar(190) NOT NULL DEFAULT '', email varchar(190) NOT NULL DEFAULT '', phone varchar(64) NOT NULL DEFAULT '', bookings int(11) unsigned NOT NULL DEFAULT 0, visits int(11) unsigned NOT NULL DEFAULT 0, covers int(11) unsigned NOT NULL DEFAULT 0, cancellations int(11) unsigned NOT NULL DEFAULT 0, no_shows int(11) unsigned NOT NULL DEFAULT 0, spent bigint(20) NOT NULL DEFAULT 0, first_visit datetime NULL, last_visit datetime NULL, marketing_optin tinyint(1) NOT NULL DEFAULT 0, tags varchar(255) NOT NULL DEFAULT '', notes text NULL, created_gmt datetime NOT NULL, updated_gmt datetime NOT NULL, PRIMARY KEY (id), KEY email (email), KEY phone (phone), KEY user_id (user_id) ) {$charset};" ); dbDelta( "CREATE TABLE {$gmeta} ( meta_id bigint(20) unsigned NOT NULL AUTO_INCREMENT, tableengine_guest_id bigint(20) unsigned NOT NULL DEFAULT 0, meta_key varchar(255) DEFAULT NULL, meta_value longtext NULL, PRIMARY KEY (meta_id), KEY tableengine_guest_id (tableengine_guest_id), KEY meta_key (meta_key(191)) ) {$charset};" ); dbDelta( "CREATE TABLE {$events} ( id bigint(20) unsigned NOT NULL AUTO_INCREMENT, event_id varchar(190) NOT NULL DEFAULT '', gateway varchar(32) NOT NULL DEFAULT 'stripe', type varchar(120) NOT NULL DEFAULT '', reservation_id bigint(20) unsigned NOT NULL DEFAULT 0, livemode tinyint(1) NOT NULL DEFAULT 0, status varchar(32) NOT NULL DEFAULT 'processed', message text NULL, created_gmt datetime NOT NULL, PRIMARY KEY (id), UNIQUE KEY event_id (event_id), KEY reservation_id (reservation_id) ) {$charset};" ); } /** * A tasteful starter floor plan so owners see something immediately. * All coordinates are normalised: 1.0 == full plan width. */ public static function sample_plan() { $el = array(); $id = 0; $t = function ( $shape, $x, $y, $w, $h, $label, $min, $max, $section, $r = 0 ) use ( &$id ) { $id++; return array( 'id' => 'tb_' . $id . wp_generate_password( 4, false, false ), 'type' => 'table', 'shape' => $shape, 'x' => $x, 'y' => $y, 'w' => $w, 'h' => $h, 'r' => $r, 'label' => $label, 'seatsMin' => $min, 'seatsMax' => $max, 'section' => $section, 'features' => '', 'bookable' => true, 'priority' => 0, ); }; $d = function ( $kind, $x, $y, $w, $h, $label = '', $r = 0 ) use ( &$id ) { $id++; return array( 'id' => 'el_' . $id . wp_generate_password( 4, false, false ), 'type' => 'decor', 'kind' => $kind, 'x' => $x, 'y' => $y, 'w' => $w, 'h' => $h, 'r' => $r, 'label' => $label, ); }; // Plan is 1.0 wide x 0.625 tall (aspect 1.6). $el[] = $d( 'area', 0.17, 0.3125, 0.30, 0.585, 'Window' ); $el[] = $d( 'window', 0.012, 0.3125, 0.012, 0.5, '' ); $el[] = $d( 'bar', 0.86, 0.17, 0.22, 0.09, 'Bar' ); $el[] = $d( 'kitchen', 0.86, 0.5, 0.22, 0.2, 'Kitchen' ); $el[] = $d( 'door', 0.5, 0.612, 0.09, 0.02, 'Entrance' ); $el[] = $d( 'plant', 0.38, 0.06, 0.035, 0.035 ); $el[] = $d( 'plant', 0.66, 0.06, 0.035, 0.035 ); $el[] = $t( 'round', 0.1, 0.12, 0.07, 0.07, 'T1', 1, 2, 'Window' ); $el[] = $t( 'round', 0.1, 0.31, 0.07, 0.07, 'T2', 1, 2, 'Window' ); $el[] = $t( 'round', 0.1, 0.5, 0.07, 0.07, 'T3', 1, 2, 'Window' ); $el[] = $t( 'rect', 0.24, 0.16, 0.09, 0.09, 'T4', 2, 4, 'Window' ); $el[] = $t( 'rect', 0.24, 0.42, 0.09, 0.09, 'T5', 2, 4, 'Window' ); $el[] = $t( 'rect', 0.43, 0.2, 0.16, 0.08, 'T6', 4, 6, 'Main' ); $el[] = $t( 'round', 0.43, 0.43, 0.11, 0.11, 'T7', 4, 6, 'Main' ); $el[] = $t( 'rect', 0.62, 0.2, 0.09, 0.09, 'T8', 2, 4, 'Main' ); $el[] = $t( 'oval', 0.63, 0.43, 0.15, 0.09, 'T9', 6, 8, 'Main' ); $el[] = $t( 'booth', 0.86, 0.33, 0.16, 0.07, 'B1', 2, 4, 'Bar' ); return array( 'name' => __( 'Main Dining Room', 'tableengine' ), 'is_active' => 1, 'data' => array( 'aspect' => 1.6, 'floor' => '', 'grid' => 0.0125, 'elements' => $el, ), ); } }