insert( $wpdb->prefix . 'weship_acs_sync_logs', array( 'order_id' => $order_id ? absint( $order_id ) : null, 'event' => sanitize_key( $event ), 'direction' => 'inbound' === $direction ? 'inbound' : 'outbound', 'url' => substr( esc_url_raw( (string) $url ), 0, 500 ), 'status_code' => substr( sanitize_text_field( (string) $status_code ), 0, 16 ), 'message' => wp_strip_all_tags( (string) $message ), 'request' => (string) $request, 'response' => (string) $response, 'created_at' => current_time( 'mysql' ), ), array( '%d', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s' ) ); // Also mirror to the WooCommerce logger for support debugging. if ( function_exists( 'wc_get_logger' ) ) { wc_get_logger()->info( sprintf( '[%s/%s] order:%d code:%s url:%s %s', $event, $direction, $order_id, $status_code, $url, $message ), array( 'source' => 'weship-acs-sync-for-woocommerce' ) ); } return (int) $wpdb->insert_id; } /** * Overwrite a log row's message after the fact. * * Used when the real outcome only becomes known after the row was * already logged with a generic HTTP-level message — e.g. a CCP order * rejection, which ACS reports as an HTTP 200 with the rejection buried * in the response body, so the row is initially logged as "OK". * * @param int $log_id Log row ID, as returned by log(). * @param string $message New message. */ public function update_message( $log_id, $message ) { if ( ! $log_id ) { return; } global $wpdb; // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery,WordPress.DB.DirectDatabaseQuery.NoCaching $wpdb->update( $wpdb->prefix . 'weship_acs_sync_logs', array( 'message' => wp_strip_all_tags( (string) $message ) ), array( 'id' => absint( $log_id ) ), array( '%s' ), array( '%d' ) ); } /** * Columns the Logs tab is allowed to filter/sort by, mapped to the * actual DB column (orderby keys are also used as-is for filters). * * @return array */ public static function sortable_columns() { return array( 'date' => 'created_at', 'event' => 'event', 'direction' => 'direction', 'order' => 'order_id', 'status' => 'status_code', ); } /** * Get a page of log rows, optionally filtered and sorted. * * @param int $page Zero-based page number. * @param int $size Rows per page. * @param array $filters Optional filters: event, direction, order_id, status. * @param string $orderby One of self::sortable_columns() keys. * @param string $order 'ASC' or 'DESC'. * @return array{items:array,total:int,total_pages:int} */ public function get_recent( $page = 0, $size = 50, $filters = array(), $orderby = 'date', $order = 'DESC' ) { global $wpdb; $table = $wpdb->prefix . 'weship_acs_sync_logs'; $columns = self::sortable_columns(); $where = array( '1=1' ); $args = array(); if ( isset( $filters['event'] ) && '' !== $filters['event'] ) { $where[] = 'event = %s'; $args[] = $filters['event']; } if ( isset( $filters['direction'] ) && '' !== $filters['direction'] ) { $where[] = 'direction = %s'; $args[] = $filters['direction']; } if ( ! empty( $filters['order_id'] ) ) { $where[] = 'order_id = %d'; $args[] = absint( $filters['order_id'] ); } if ( isset( $filters['status'] ) && '' !== $filters['status'] ) { $where[] = 'status_code = %s'; $args[] = $filters['status']; } $where_sql = implode( ' AND ', $where ); // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared $count_sql = "SELECT COUNT(*) FROM {$table} WHERE {$where_sql}"; // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared $total = $args ? (int) $wpdb->get_var( $wpdb->prepare( $count_sql, $args ) ) // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared : (int) $wpdb->get_var( $count_sql ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery,WordPress.DB.PreparedSQL.NotPrepared $sort_column = isset( $columns[ $orderby ] ) ? $columns[ $orderby ] : 'created_at'; $sort_dir = 'ASC' === strtoupper( $order ) ? 'ASC' : 'DESC'; $page = max( 0, (int) $page ); $size = max( 1, (int) $size ); $offset = $page * $size; $sql = "SELECT * FROM {$table} WHERE {$where_sql} ORDER BY {$sort_column} {$sort_dir} LIMIT %d OFFSET %d"; // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared $query_args = array_merge( $args, array( $size, $offset ) ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery,WordPress.DB.DirectDatabaseQuery.NoCaching,WordPress.DB.PreparedSQL.NotPrepared $rows = $wpdb->get_results( $wpdb->prepare( $sql, $query_args ) ); return array( 'items' => $rows, 'total' => $total, 'total_pages' => (int) ceil( $total / $size ), ); } /** * Distinct event names actually present in the logs table, used to * populate the Logs tab's Event filter without hardcoding the list. * * @return array */ public function get_distinct_events() { global $wpdb; $table = $wpdb->prefix . 'weship_acs_sync_logs'; // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery,WordPress.DB.DirectDatabaseQuery.NoCaching return $wpdb->get_col( "SELECT DISTINCT event FROM {$table} ORDER BY event ASC" ); } /** * Get log rows for a set of order IDs (used by the Orders page "View Logs" modal). * * @param array $order_ids Order IDs. * @param string $status_code Optional status code filter. * @return array */ public function get_by_order_ids( $order_ids, $status_code = '' ) { global $wpdb; $order_ids = array_filter( array_map( 'absint', (array) $order_ids ) ); if ( empty( $order_ids ) ) { return array(); } $table = $wpdb->prefix . 'weship_acs_sync_logs'; $placeholders = implode( ',', array_fill( 0, count( $order_ids ), '%d' ) ); $sql = "SELECT * FROM {$table} WHERE order_id IN ({$placeholders})"; // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared $args = $order_ids; if ( '' !== $status_code ) { $sql .= ' AND status_code = %s'; $args[] = $status_code; } $sql .= ' ORDER BY id DESC'; // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery,WordPress.DB.DirectDatabaseQuery.NoCaching,WordPress.DB.PreparedSQL.NotPrepared return $wpdb->get_results( $wpdb->prepare( $sql, $args ) ); } }