prefix . 'dpsupport_messages'; } public function create( $ticket_id, array $data ) { global $wpdb; $ticket_id = absint( $ticket_id ); $user = wp_get_current_user(); $body = isset( $data['body'] ) ? wp_kses_post( $data['body'] ) : ''; if ( ! $ticket_id ) { return new WP_Error( 'dpsupport_invalid_ticket', __( 'Ungültige Ticket-ID.', '24laser-support-tickets' ) ); } if ( '' === trim( wp_strip_all_tags( $body ) ) ) { return new WP_Error( 'dpsupport_empty_message', __( 'Die Nachricht darf nicht leer sein.', '24laser-support-tickets' ) ); } $author_type = isset( $data['author_type'] ) && in_array( $data['author_type'], array( 'staff', 'customer', 'system' ), true ) ? $data['author_type'] : 'staff'; $author_user_id = ! empty( $data['author_user_id'] ) ? absint( $data['author_user_id'] ) : absint( get_current_user_id() ); $author_name = isset( $data['author_name'] ) ? sanitize_text_field( $data['author_name'] ) : ( $user->exists() ? sanitize_text_field( $user->display_name ) : '' ); $inserted = $wpdb->insert( $this->table(), array( 'ticket_id' => $ticket_id, 'author_type' => $author_type, 'author_user_id' => $author_user_id, 'author_name' => $author_name, 'body' => $body, 'is_internal' => ! empty( $data['is_internal'] ) ? 1 : 0, 'created_at' => current_time( 'mysql' ), ), array( '%d', '%s', '%d', '%s', '%s', '%d', '%s' ) ); if ( false === $inserted ) { $message = __( 'Nachricht konnte nicht gespeichert werden.', '24laser-support-tickets' ); if ( current_user_can( 'dpsupport_manage_support' ) && ! empty( $wpdb->last_error ) ) { $message .= ' ' . __( 'Datenbankfehler:', '24laser-support-tickets' ) . ' ' . sanitize_text_field( $wpdb->last_error ); } return new WP_Error( 'dpsupport_message_create_failed', $message ); } $message_id = absint( $wpdb->insert_id ); if ( ! $message_id ) { return new WP_Error( 'dpsupport_message_missing_id', __( 'Die Nachricht wurde geschrieben, aber WordPress hat keine Nachrichten-ID zurückgegeben.', '24laser-support-tickets' ) ); } $stored = $this->get( $message_id ); if ( ! $stored || (int) $stored->ticket_id !== $ticket_id ) { return new WP_Error( 'dpsupport_message_verify_failed', __( 'Die gespeicherte Nachricht konnte nicht wieder aus der Datenbank gelesen werden.', '24laser-support-tickets' ) ); } return $message_id; } public function get( $message_id ) { global $wpdb; return $wpdb->get_row( $wpdb->prepare( 'SELECT * FROM %i WHERE id = %d LIMIT 1', $this->table(), absint( $message_id ) ) ); } public function get_public_for_ticket( $ticket_id ) { global $wpdb; $ticket_id = absint( $ticket_id ); if ( ! $ticket_id ) { return array(); } $results = $wpdb->get_results( $wpdb->prepare( 'SELECT * FROM %i WHERE ticket_id = %d AND is_internal = 0 ORDER BY id ASC', $this->table(), $ticket_id ) ); return is_array( $results ) ? $results : array(); } public function get_latest_customer_message_id( $ticket_id ) { global $wpdb; $ticket_id = absint( $ticket_id ); if ( ! $ticket_id ) { return 0; } return absint( $wpdb->get_var( $wpdb->prepare( 'SELECT MAX(id) FROM %i WHERE ticket_id = %d AND author_type = \'customer\' AND is_internal = 0', $this->table(), $ticket_id ) ) ); } public function get_for_ticket( $ticket_id ) { global $wpdb; $ticket_id = absint( $ticket_id ); if ( ! $ticket_id ) { return array(); } $results = $wpdb->get_results( $wpdb->prepare( 'SELECT * FROM %i WHERE ticket_id = %d ORDER BY id ASC', $this->table(), $ticket_id ) ); return is_array( $results ) ? $results : array(); } } // phpcs:enable WordPress.DB.DirectDatabaseQuery.DirectQuery,WordPress.DB.DirectDatabaseQuery.NoCaching