$item bookable_id, session_id, staff_id, location_id, resources, * start, end, block_start, block_end, quantity. * @param int $expires Unix time it lapses. */ public function create( string $token, array $item, int $expires ): int { $this->db->insert( $this->table, array( 'token' => $token, 'bookable_id' => (int) $item['bookable_id'], 'session_id' => (int) ( $item['session_id'] ?? 0 ), 'staff_id' => (int) ( $item['staff_id'] ?? 0 ), 'location_id' => (int) ( $item['location_id'] ?? 0 ), 'resources' => implode( ',', array_map( 'intval', (array) ( $item['resources'] ?? array() ) ) ), 'start_at' => self::dt( (int) $item['start'] ), 'end_at' => self::dt( (int) $item['end'] ), 'block_start' => self::dt( (int) ( $item['block_start'] ?? $item['start'] ) ), 'block_end' => self::dt( (int) ( $item['block_end'] ?? $item['end'] ) ), 'quantity' => max( 1, (int) ( $item['quantity'] ?? 1 ) ), 'expires_at' => self::dt( $expires ), ) ); return (int) $this->db->insert_id; } /** * A hold's rows that have not lapsed. * * @param string $token Token. * @return array> */ public function find( string $token ): array { // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared, WordPress.DB.DirectDatabaseQuery $rows = (array) $this->db->get_results( $this->db->prepare( "SELECT * FROM {$this->table} WHERE token = %s AND expires_at > %s ORDER BY id", $token, self::now() ), ARRAY_A ); return array_map( array( self::class, 'hydrate' ), $rows ); } public function release( string $token ): void { $this->db->delete( $this->table, array( 'token' => $token ) ); } public function purge(): int { // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared, WordPress.DB.DirectDatabaseQuery return (int) $this->db->query( $this->db->prepare( "DELETE FROM {$this->table} WHERE expires_at <= %s", self::now() ) ); } /** * Busy entries from live holds by staff, bookable or resource. * * @param string $by staff, bookable or resource. * @param int[] $ids IDs. * @param int $from Start. * @param int $to End. * @param string $except Hold token to leave out (the customer's own). * @return array>> */ public function busy( string $by, array $ids, int $from, int $to, string $except = '' ): array { $ids = array_values( array_filter( array_map( 'intval', $ids ) ) ); if ( array() === $ids ) { return array(); } $args = array( self::now(), self::dt( $to ), self::dt( $from ), $except ); if ( 'resource' === $by ) { $parts = array(); foreach ( $ids as $id ) { $parts[] = 'FIND_IN_SET(%d, resources)'; $args[] = $id; } $match = '(' . implode( ' OR ', $parts ) . ')'; } else { $column = 'staff' === $by ? 'staff_id' : 'bookable_id'; $match = "{$column} IN (" . self::in( $ids ) . ')'; $args = array_merge( $args, $ids ); } // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared, WordPress.DB.PreparedSQLPlaceholders.UnfinishedPrepare, WordPress.DB.DirectDatabaseQuery $rows = (array) $this->db->get_results( $this->db->prepare( "SELECT * FROM {$this->table} WHERE expires_at > %s AND block_start < %s AND block_end > %s AND token <> %s AND {$match}", $args ), ARRAY_A ); $out = array_fill_keys( $ids, array() ); foreach ( $rows as $row ) { $hold = self::hydrate( $row ); $entry = array( 'start' => $hold['block_start'], 'end' => $hold['block_end'], 'qty' => $hold['quantity'], 'kind' => 'hold', 'bookable_id' => $hold['bookable_id'], 'slot_start' => $hold['start'], 'session_id' => $hold['session_id'], ); $keys = 'resource' === $by ? $hold['resources'] : array( 'staff' === $by ? $hold['staff_id'] : $hold['bookable_id'] ); foreach ( $keys as $key ) { if ( isset( $out[ $key ] ) ) { $out[ $key ][] = $entry; } } } return $out; } /** * Places held in sessions. * * @param int[] $session_ids Sessions. * @param string $except Token to leave out. * @return array */ public function held_in_sessions( array $session_ids, string $except = '' ): array { $session_ids = array_values( array_filter( array_map( 'intval', $session_ids ) ) ); if ( array() === $session_ids ) { return array(); } $args = array_merge( array( self::now(), $except ), $session_ids ); // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared, WordPress.DB.PreparedSQLPlaceholders.UnfinishedPrepare, WordPress.DB.DirectDatabaseQuery $rows = (array) $this->db->get_results( $this->db->prepare( "SELECT session_id, SUM(quantity) AS n FROM {$this->table} WHERE expires_at > %s AND token <> %s AND session_id IN (" . self::in( $session_ids ) . ') GROUP BY session_id', $args ), ARRAY_A ); return array_column( array_map( static fn( array $r ): array => array( (int) $r['session_id'], (int) $r['n'] ), $rows ), 1, 0 ); } /** * Cast a row. * * @param array $row Row. * @return array */ private static function hydrate( array $row ): array { return array( 'id' => (int) $row['id'], 'token' => (string) $row['token'], 'bookable_id' => (int) $row['bookable_id'], 'session_id' => (int) $row['session_id'], 'staff_id' => (int) $row['staff_id'], 'location_id' => (int) $row['location_id'], 'resources' => '' === $row['resources'] ? array() : array_map( 'intval', explode( ',', (string) $row['resources'] ) ), 'start' => self::ts( $row['start_at'] ), 'end' => self::ts( $row['end_at'] ), 'block_start' => self::ts( $row['block_start'] ), 'block_end' => self::ts( $row['block_end'] ), 'quantity' => (int) $row['quantity'], 'expires_at' => self::ts( $row['expires_at'] ), ); } }