suppress_errors( true ); $ok = $wpdb->insert( Schema::receipts_table(), array( 'reference' => $data['reference'], 'token_salt' => $data['token_salt'], 'source' => $data['source'], 'form_id' => $data['form_id'], 'form_title' => $data['form_title'], 'status_key' => $data['status_key'], 'email' => $data['email'], 'user_id' => (int) $data['user_id'], 'snapshot' => $data['snapshot'], 'snapshot_hash' => $data['snapshot_hash'], 'files_count' => (int) $data['files_count'], 'created_at' => $data['created_at'], 'updated_at' => $data['created_at'], 'purge_after' => $data['purge_after'], ), array( '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%d', '%s', '%s', '%d', '%s', '%s', '%s' ) ); $wpdb->suppress_errors( $suppress ); return $ok ? (int) $wpdb->insert_id : 0; } public static function find( int $id ): ?array { global $wpdb; $table = Schema::receipts_table(); $row = $wpdb->get_row( $wpdb->prepare( 'SELECT * FROM %i WHERE id = %d', $table, $id ), ARRAY_A ); return $row ? $row : null; } public static function find_by_reference( string $reference ): ?array { global $wpdb; $table = Schema::receipts_table(); $row = $wpdb->get_row( $wpdb->prepare( 'SELECT * FROM %i WHERE reference = %s', $table, strtoupper( $reference ) ), ARRAY_A ); return $row ? $row : null; } public static function find_by_email( string $email, int $limit = 100, int $offset = 0 ): array { global $wpdb; $table = Schema::receipts_table(); return (array) $wpdb->get_results( $wpdb->prepare( 'SELECT * FROM %i WHERE email = %s ORDER BY id ASC LIMIT %d OFFSET %d', $table, $email, $limit, $offset ), ARRAY_A ); } public static function update_status( int $id, string $status_key ): bool { global $wpdb; return false !== $wpdb->update( Schema::receipts_table(), array( 'status_key' => $status_key, 'updated_at' => current_time( 'mysql', true ), ), array( 'id' => $id ), array( '%s', '%s' ), array( '%d' ) ); } public static function touch( int $id ): void { global $wpdb; $wpdb->update( Schema::receipts_table(), array( 'updated_at' => current_time( 'mysql', true ) ), array( 'id' => $id ), array( '%s' ), array( '%d' ) ); } public static function delete( int $id ): void { global $wpdb; $wpdb->delete( Schema::events_table(), array( 'receipt_id' => $id ), array( '%d' ) ); $wpdb->delete( Schema::receipts_table(), array( 'id' => $id ), array( '%d' ) ); } /** * @return int[] IDs past their retention date. */ public static function expired_ids( int $limit = 200 ): array { global $wpdb; $table = Schema::receipts_table(); return array_map( 'intval', (array) $wpdb->get_col( $wpdb->prepare( 'SELECT id FROM %i WHERE purge_after IS NOT NULL AND purge_after < %s LIMIT %d', $table, current_time( 'mysql', true ), $limit ) ) ); } /** * Paged query for the admin list. * * @return array{items: array, total: int} */ public static function query( array $args ): array { global $wpdb; $table = Schema::receipts_table(); $args = array_merge( array( 'status' => '', 'statuses' => null, 'source' => '', 'form_id' => '', 'search' => '', 'per_page' => 20, 'page' => 1, 'orderby' => 'created_at', 'order' => 'DESC', ), $args ); $where = array( '1=1' ); $params = array(); if ( '' !== $args['status'] ) { $where[] = 'status_key = %s'; $params[] = $args['status']; } elseif ( is_array( $args['statuses'] ) ) { // Any of several statuses (e.g. the "Open" view). An empty list matches nothing. $keys = array_values( array_map( 'strval', $args['statuses'] ) ); if ( $keys ) { $where[] = 'status_key IN (' . implode( ', ', array_fill( 0, count( $keys ), '%s' ) ) . ')'; $params = array_merge( $params, $keys ); } else { $where[] = '1=0'; } } if ( '' !== $args['source'] ) { $where[] = 'source = %s'; $params[] = $args['source']; } if ( '' !== $args['form_id'] ) { $where[] = 'form_id = %s'; $params[] = $args['form_id']; } if ( '' !== $args['search'] ) { $like = '%' . $wpdb->esc_like( $args['search'] ) . '%'; $where[] = '(reference LIKE %s OR email LIKE %s)'; $params[] = $like; $params[] = $like; } $orderby = in_array( $args['orderby'], array( 'created_at', 'updated_at', 'reference', 'status_key' ), true ) ? $args['orderby'] : 'created_at'; $order = 'ASC' === strtoupper( $args['order'] ) ? 'ASC' : 'DESC'; $limit = max( 1, (int) $args['per_page'] ); $offset = ( max( 1, (int) $args['page'] ) - 1 ) * $limit; // $where holds only fixed SQL fragments with placeholders; every value is in $params. // $orderby is passed as an identifier (%i) and $order is one of two literals checked above. $where_sql = implode( ' AND ', $where ); $count_sql = 'SELECT COUNT(*) FROM %i WHERE ' . $where_sql; $items_sql = 'SELECT * FROM %i WHERE ' . $where_sql . ' ORDER BY %i ' . $order . ', id ' . $order . ' LIMIT %d OFFSET %d'; // phpcs:disable WordPress.DB.PreparedSQL.NotPrepared, PluginCheck.Security.DirectDB.UnescapedDBParameter -- built from fixed fragments, see above. $total = (int) $wpdb->get_var( $wpdb->prepare( $count_sql, array_merge( array( $table ), $params ) ) ); $items = (array) $wpdb->get_results( $wpdb->prepare( $items_sql, array_merge( array( $table ), $params, array( $orderby, $limit, $offset ) ) ), ARRAY_A ); // phpcs:enable WordPress.DB.PreparedSQL.NotPrepared, PluginCheck.Security.DirectDB.UnescapedDBParameter return array( 'items' => $items, 'total' => $total, ); } /** * @return array status_key => count */ public static function status_counts(): array { global $wpdb; $table = Schema::receipts_table(); $rows = (array) $wpdb->get_results( $wpdb->prepare( 'SELECT status_key, COUNT(*) AS c FROM %i GROUP BY status_key', $table ), ARRAY_A ); $out = array(); foreach ( $rows as $row ) { $out[ $row['status_key'] ] = (int) $row['c']; } return $out; } }