*/ public function get_output() { $output = array(); /** @var QM_Data_DB_Queries $data */ $data = $this->collector->get_data(); if ( empty( $data->wpdb ) ) { return $output; } $output['wpdb'] = $this->output_queries( $data->wpdb ); if ( ! empty( $data->errors ) ) { $output['errors'] = array( 'total' => count( $data->errors ), 'errors' => $data->errors, ); } if ( ! empty( $data->dupes ) ) { $dupes = $data->dupes; // Filter out SQL queries that do not have dupes $dupes = array_filter( $dupes, array( $this->collector, 'filter_dupe_items' ) ); // Ignore dupes from `WP_Query->set_found_posts()` unset( $dupes['SELECT FOUND_ROWS()'] ); $output['dupes'] = array( 'total' => count( $dupes ), 'queries' => $dupes, ); } return $output; } /** * @param stdClass $db * @return array * @phpstan-return array{ * total: int, * time: float, * queries: mixed[], * }|array{} */ protected function output_queries( stdClass $db ) { $this->query_row = 0; $output = array(); if ( empty( $db->rows ) ) { return $output; } foreach ( $db->rows as $row ) { $output[] = $this->output_query_row( $row ); } return array( 'total' => $db->total_qs, 'time' => round( $db->total_time, 4 ), 'queries' => $output, ); } /** * @param array $row * @return array */ protected function output_query_row( array $row ) { $output = array(); $output['i'] = ++$this->query_row; $output['sql'] = $row['sql']; $output['time'] = round( $row['ltime'], 4 ); if ( isset( $row['trace'] ) ) { $stack = array(); $filtered_trace = $row['trace']->get_filtered_trace(); foreach ( $filtered_trace as $item ) { $stack[] = $item['display']; } } else { $stack = $row['stack']; } $output['stack'] = $stack; $output['result'] = $row['result']; return $output; } } /** * @param array $output * @param QM_Collectors $collectors * @return array */ function register_qm_output_raw_db_queries( array $output, QM_Collectors $collectors ) { $collector = QM_Collectors::get( 'db_queries' ); if ( $collector ) { $output['db_queries'] = new QM_Output_Raw_DB_Queries( $collector ); } return $output; } add_filter( 'qm/outputter/raw', 'register_qm_output_raw_db_queries', 20, 2 );