get_var( "SELECT COUNT(*) FROM {$table}" ); // 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. } /** * @return array[] */ /** @var array Per-request cache keyed by active flag. */ private static $cache = array(); public static function all( $active_only = false ) { $ck = $active_only ? 'active' : 'all'; global $wpdb; $table = Install::table( 'floorplans' ); $where = $active_only ? 'WHERE is_active = 1' : ''; if ( ! isset( self::$cache[ $ck ] ) ) { $rows = $wpdb->get_results( "SELECT * FROM {$table} {$where} ORDER BY sort_order ASC, id ASC", 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. self::$cache[ $ck ] = array_map( array( __CLASS__, 'hydrate' ), $rows ? $rows : array() ); } /** Narrow the rooms in play, e.g. to the current location. */ return array_values( (array) apply_filters( 'tableengine_floorplans', self::$cache[ $ck ], $active_only ) ); } public static function get( $id ) { global $wpdb; $table = Install::table( 'floorplans' ); $row = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$table} WHERE id = %d", $id ), 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. return $row ? self::hydrate( $row ) : null; } private static function hydrate( $row ) { $data = json_decode( $row['data'], true ); return array( 'id' => (int) $row['id'], 'name' => $row['name'], 'is_active' => (int) $row['is_active'], 'sort_order' => (int) $row['sort_order'], 'location_id' => isset( $row['location_id'] ) ? (int) $row['location_id'] : 0, 'data' => self::sanitize_data( is_array( $data ) ? $data : array() ), 'updated_at' => $row['updated_at'], ); } /** * Insert or update a plan. Returns the saved plan. */ public static function save( array $plan ) { global $wpdb; $table = Install::table( 'floorplans' ); $now = current_time( 'mysql', true ); $row = array( 'name' => sanitize_text_field( isset( $plan['name'] ) ? $plan['name'] : __( 'Untitled room', 'tableengine' ) ), 'data' => wp_json_encode( self::sanitize_data( isset( $plan['data'] ) && is_array( $plan['data'] ) ? $plan['data'] : array() ) ), 'is_active' => empty( $plan['is_active'] ) ? 0 : 1, 'sort_order' => isset( $plan['sort_order'] ) ? (int) $plan['sort_order'] : 0, 'updated_at' => $now, ); if ( isset( $plan['location_id'] ) ) { $row['location_id'] = absint( $plan['location_id'] ); } /** Adjust the saved columns (e.g. assign a new room to the current location). */ $row = apply_filters( 'tableengine_floorplan_row', $row, $plan, ! empty( $plan['id'] ) && self::get( (int) $plan['id'] ) ); self::$cache = array(); if ( ! empty( $plan['id'] ) && self::get( (int) $plan['id'] ) ) { $wpdb->update( $table, $row, array( 'id' => (int) $plan['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. $id = (int) $plan['id']; } else { $row['created_at'] = $now; $wpdb->insert( $table, $row ); // 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. $id = (int) $wpdb->insert_id; } return self::get( $id ); } public static function delete( $id ) { global $wpdb; self::$cache = array(); return (bool) $wpdb->delete( Install::table( 'floorplans' ), array( '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. } private static function num( $v, $min, $max, $default = 0 ) { if ( ! is_numeric( $v ) ) { return $default; } return round( max( $min, min( $max, (float) $v ) ), 5 ); } /** * Strictly validate plan JSON. */ public static function sanitize_data( array $data ) { $aspect = self::num( isset( $data['aspect'] ) ? $data['aspect'] : 1.6, 0.4, 4, 1.6 ); $height = 1 / $aspect; $out = array( 'aspect' => $aspect, 'floor' => isset( $data['floor'] ) && sanitize_hex_color( $data['floor'] ) ? sanitize_hex_color( $data['floor'] ) : '', 'grid' => self::num( isset( $data['grid'] ) ? $data['grid'] : 0.0125, 0.0025, 0.1, 0.0125 ), 'elements' => array(), ); $elements = isset( $data['elements'] ) && is_array( $data['elements'] ) ? $data['elements'] : array(); $seen = array(); $shapes = self::table_shapes(); $kinds = self::decor_kinds(); foreach ( array_slice( $elements, 0, 500 ) as $el ) { if ( ! is_array( $el ) || empty( $el['type'] ) ) { continue; } $id = isset( $el['id'] ) ? preg_replace( '/[^A-Za-z0-9_\-]/', '', (string) $el['id'] ) : ''; if ( '' === $id || isset( $seen[ $id ] ) ) { $id = 'el_' . wp_generate_password( 8, false, false ); } $seen[ $id ] = true; $base = array( 'id' => substr( $id, 0, 60 ), 'x' => self::num( isset( $el['x'] ) ? $el['x'] : 0.5, -0.2, 1.2, 0.5 ), 'y' => self::num( isset( $el['y'] ) ? $el['y'] : $height / 2, -0.2, $height + 0.2, $height / 2 ), 'w' => self::num( isset( $el['w'] ) ? $el['w'] : 0.08, 0.004, 1.5, 0.08 ), 'h' => self::num( isset( $el['h'] ) ? $el['h'] : 0.08, 0.004, 1.5, 0.08 ), 'r' => self::num( isset( $el['r'] ) ? $el['r'] : 0, -360, 360, 0 ), 'label' => isset( $el['label'] ) ? mb_substr( sanitize_text_field( $el['label'] ), 0, 40 ) : '', ); if ( 'table' === $el['type'] ) { $min = isset( $el['seatsMin'] ) ? max( 1, min( 100, (int) $el['seatsMin'] ) ) : 1; $max = isset( $el['seatsMax'] ) ? max( 1, min( 100, (int) $el['seatsMax'] ) ) : 2; if ( $min > $max ) { $min = $max; } $clean = $base + array( 'type' => 'table', 'shape' => isset( $el['shape'] ) && in_array( $el['shape'], $shapes, true ) ? $el['shape'] : 'rect', 'seatsMin' => $min, 'seatsMax' => $max, 'section' => isset( $el['section'] ) ? mb_substr( sanitize_text_field( $el['section'] ), 0, 40 ) : '', 'features' => isset( $el['features'] ) ? mb_substr( sanitize_text_field( $el['features'] ), 0, 120 ) : '', 'bookable' => ! isset( $el['bookable'] ) || ! empty( $el['bookable'] ), 'priority' => isset( $el['priority'] ) ? max( -99, min( 99, (int) $el['priority'] ) ) : 0, ); } elseif ( 'decor' === $el['type'] ) { $kind = isset( $el['kind'] ) && in_array( $el['kind'], $kinds, true ) ? $el['kind'] : 'label'; $color = isset( $el['color'] ) && sanitize_hex_color( $el['color'] ) ? sanitize_hex_color( $el['color'] ) : ''; $clean = $base + array( 'type' => 'decor', 'kind' => $kind, 'color' => $color, ); } else { $clean = null; } /** * Sanitise an element. Unknown keys are dropped unless an extension keeps them here * (e.g. table photos); return null to drop the element. */ $clean = apply_filters( 'tableengine_sanitize_element', $clean, $el ); if ( is_array( $clean ) ) { $out['elements'][] = $clean; } } /** Sanitise plan-level data, e.g. to keep a background image or table combinations. */ return apply_filters( 'tableengine_sanitize_plan_data', $out, $data ); } /** * Locate a table definition. */ public static function find_table( $plan_id, $table_id ) { $plan = self::get( $plan_id ); if ( ! $plan ) { return null; } $found = null; foreach ( $plan['data']['elements'] as $el ) { if ( 'table' === $el['type'] && $el['id'] === $table_id ) { $el['plan_id'] = $plan['id']; $el['plan_name'] = $plan['name']; $found = $el; break; } } return apply_filters( 'tableengine_find_table', $found, $plan, $table_id ); } /** * All bookable tables across active plans. * * @return array[] */ public static function all_tables( $active_only = true ) { $tables = array(); foreach ( self::all( $active_only ) as $plan ) { foreach ( $plan['data']['elements'] as $el ) { if ( 'table' === $el['type'] ) { $el['plan_id'] = $plan['id']; $el['plan_name'] = $plan['name']; $tables[] = $el; } } } return $tables; } }