> */ public function all(): array { // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared, WordPress.DB.DirectDatabaseQuery $rows = (array) $this->db->get_results( "SELECT * FROM {$this->table} ORDER BY id DESC", ARRAY_A ); return array_map( array( self::class, 'hydrate' ), $rows ); } public function find( int $id ): ?array { // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared, WordPress.DB.DirectDatabaseQuery $row = $this->db->get_row( $this->db->prepare( "SELECT * FROM {$this->table} WHERE id = %d", $id ), ARRAY_A ); return $row ? self::hydrate( $row ) : null; } /** * An active coupon by code that still has uses left, as a domain object. * * @param string $code Code as typed. */ public function usable( string $code ): ?Coupon { $code = Coupon::normalize_code( $code ); if ( '' === $code ) { return null; } // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared, WordPress.DB.DirectDatabaseQuery $row = $this->db->get_row( $this->db->prepare( "SELECT * FROM {$this->table} WHERE code = %s AND active = 1 AND (max_uses = 0 OR uses < max_uses)", $code ), ARRAY_A ); if ( ! $row ) { return null; } try { return Coupon::from_array( (string) $row['code'], (array) self::json( $row['settings'] ) ); } catch ( InvalidArgumentException ) { return null; } } /** * Save a coupon. * * @param Coupon $coupon Coupon. * @param int $max_uses 0 for no limit. * @param bool $active On or off. * @param int $id ID when editing. */ public function save( Coupon $coupon, int $max_uses, bool $active, int $id = 0 ): int { $row = array( 'code' => $coupon->code, 'settings' => wp_json_encode( $coupon->to_array() ), 'max_uses' => max( 0, $max_uses ), 'active' => $active ? 1 : 0, ); if ( $id > 0 ) { $this->db->update( $this->table, $row, array( 'id' => $id ) ); return $id; } $row['created_at'] = self::now(); $this->db->insert( $this->table, $row ); return (int) $this->db->insert_id; } public function delete( int $id ): void { $this->db->delete( $this->table, array( 'id' => $id ) ); } public function code_taken( string $code, int $except = 0 ): bool { // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared, WordPress.DB.DirectDatabaseQuery return null !== $this->db->get_var( $this->db->prepare( "SELECT id FROM {$this->table} WHERE code = %s AND id <> %d", Coupon::normalize_code( $code ), $except ) ); } /** * Count a use if one is left. * * @param string $code Code. */ public function use( string $code ): bool { // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared, WordPress.DB.DirectDatabaseQuery return 1 === (int) $this->db->query( $this->db->prepare( "UPDATE {$this->table} SET uses = uses + 1 WHERE code = %s AND active = 1 AND (max_uses = 0 OR uses < max_uses)", Coupon::normalize_code( $code ) ) ); } /** * Give a use back, e.g. when a booking is cancelled. * * @param string $code Code. */ public function release( string $code ): void { // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared, WordPress.DB.DirectDatabaseQuery $this->db->query( $this->db->prepare( "UPDATE {$this->table} SET uses = uses - 1 WHERE code = %s AND uses > 0", Coupon::normalize_code( $code ) ) ); } /** * Cast a row. * * @param array $row Row. * @return array */ private static function hydrate( array $row ): array { return array( 'id' => (int) $row['id'], 'code' => (string) $row['code'], 'max_uses' => (int) $row['max_uses'], 'uses' => (int) $row['uses'], 'active' => (bool) $row['active'], ) + (array) self::json( $row['settings'] ); } }