get_var( $wpdb->prepare( 'SELECT status FROM %i WHERE id = %d', Schema::table( 'sessions' ), $session_id ) ) ) { return 0; } // A replayed token must not add a second row for the same request. if ( (int) $wpdb->get_var( $wpdb->prepare( 'SELECT id FROM %i WHERE session_id = %d AND url = %s AND pass = %d LIMIT 1', Schema::table( 'requests' ), $session_id, $url, $pass ) ) > 0 ) { return 0; } $json = wp_json_encode( $data ); if ( ! is_string( $json ) ) { $json = '{}'; } if ( strlen( $json ) > self::MAX_BYTES ) { $json = wp_json_encode( self::trim( $data ) ); if ( ! is_string( $json ) ) { $json = '{}'; } } $wpdb->insert( Schema::table( 'requests' ), array( 'session_id' => $session_id, 'url' => $url, 'pass' => $pass, 'total_ms' => round( (float) ( $summary['total_ms'] ?? 0 ), 2 ), 'peak_mem_kb' => (int) ( $summary['peak_mem_kb'] ?? 0 ), 'query_count' => (int) ( $summary['query_count'] ?? 0 ), 'data' => $json, 'created_at' => current_time( 'mysql', true ), ), array( '%d', '%s', '%d', '%f', '%d', '%d', '%s', '%s' ) ); return (int) $wpdb->insert_id; } /** * Reduce a record to fit the size cap. * * @param array $data Data. */ public static function trim( array $data ): array { foreach ( (array) ( $data['owners'] ?? array() ) as $k => $o ) { if ( isset( $o['hooks'] ) ) { $data['owners'][ $k ]['hooks'] = array_slice( (array) $o['hooks'], 0, 10 ); } if ( isset( $o['queries']['slowest'] ) ) { $data['owners'][ $k ]['queries']['slowest'] = array_slice( (array) $o['queries']['slowest'], 0, 3 ); } if ( isset( $o['assets'] ) ) { $data['owners'][ $k ]['assets'] = array_slice( (array) $o['assets'], 0, 20 ); } } return $data; } }