| 1 |
<?php // phpcs:disable WordPress.Files.FileName.InvalidClassFileName, Generic.Files.OneObjectStructurePerFile.MultipleFound |
| 2 |
|
| 3 |
if ( ! class_exists( 'QM_Collector' ) ) { |
| 4 |
return; |
| 5 |
} |
| 6 |
|
| 7 |
/** |
| 8 |
* Data transfer object for SQLite query data. |
| 9 |
*/ |
| 10 |
class SQLite_QM_Data extends QM_Data { |
| 11 |
/** |
| 12 |
* SQLite queries indexed by normalized MySQL SQL text. |
| 13 |
* |
| 14 |
* @var array<string, list<string>> |
| 15 |
*/ |
| 16 |
public $queries = array(); |
| 17 |
} |
| 18 |
|
| 19 |
/** |
| 20 |
* Collector for SQLite query data. |
| 21 |
* |
| 22 |
* Extracts SQLite queries from $wpdb->queries and stores them |
| 23 |
* indexed by SQL text for the QM 4.0+ JS integration. |
| 24 |
*/ |
| 25 |
class SQLite_QM_Collector extends QM_Collector { |
| 26 |
/** @var string */ |
| 27 |
public $id = 'sqlite'; |
| 28 |
|
| 29 |
public function get_storage(): QM_Data { |
| 30 |
return new SQLite_QM_Data(); |
| 31 |
} |
| 32 |
|
| 33 |
public function process(): void { |
| 34 |
global $wpdb; |
| 35 |
|
| 36 |
if ( empty( $wpdb->queries ) ) { |
| 37 |
return; |
| 38 |
} |
| 39 |
|
| 40 |
// Index by SQL rather than row position — robust to filtering, sorting, etc. |
| 41 |
$mapped = array(); |
| 42 |
foreach ( $wpdb->queries as $query ) { |
| 43 |
// Query Monitor skips queries with 'wp_admin_bar' in the stack. |
| 44 |
if ( false !== strpos( $query[2] ?? '', 'wp_admin_bar' ) ) { |
| 45 |
continue; |
| 46 |
} |
| 47 |
if ( ! empty( $query['sqlite_queries'] ) ) { |
| 48 |
$sql = trim( preg_replace( '/\s+/', ' ', $query[0] ) ); |
| 49 |
$mapped[ $sql ] = array_column( $query['sqlite_queries'], 'sql' ); |
| 50 |
} |
| 51 |
} |
| 52 |
$this->data->queries = $mapped; |
| 53 |
} |
| 54 |
} |
| 55 |
|
| 56 |
/** |
| 57 |
* HTML outputter for SQLite query data. |
| 58 |
* |
| 59 |
* With $client_side_rendered = true, QM auto-serializes the collector data |
| 60 |
* into "window.QueryMonitorData.data.sqlite". This outputter's only job is to |
| 61 |
* emit the inline JS module that reads that data and injects SQLite query |
| 62 |
* details into QM 4.0's shadow DOM DB queries panel. |
| 63 |
*/ |
| 64 |
class SQLite_QM_Output_Html extends QM_Output_Html { |
| 65 |
/** @var bool */ |
| 66 |
public static $client_side_rendered = true; |
| 67 |
|
| 68 |
public function name(): string { |
| 69 |
return 'SQLite'; |
| 70 |
} |
| 71 |
|
| 72 |
public function output(): void { |
| 73 |
if ( empty( $this->get_collector()->get_data()->queries ) ) { |
| 74 |
return; |
| 75 |
} |
| 76 |
|
| 77 |
$js_path = __DIR__ . '/query-monitor-sqlite.js'; |
| 78 |
if ( is_readable( $js_path ) ) { |
| 79 |
echo '<script type="module">'; |
| 80 |
include $js_path; |
| 81 |
echo '</script>'; |
| 82 |
} |
| 83 |
} |
| 84 |
} |
| 85 |
|
| 86 |
/** |
| 87 |
* Register the SQLite collector. |
| 88 |
*/ |
| 89 |
function register_sqlite_qm_collector( array $collectors ): array { |
| 90 |
$collectors['sqlite'] = new SQLite_QM_Collector(); |
| 91 |
return $collectors; |
| 92 |
} |
| 93 |
|
| 94 |
add_filter( 'qm/collectors', 'register_sqlite_qm_collector', 20 ); |
| 95 |
|
| 96 |
/** |
| 97 |
* Register the SQLite HTML outputter. |
| 98 |
*/ |
| 99 |
function register_sqlite_qm_output_html( array $output, QM_Collectors $collectors ): array { |
| 100 |
$collector = QM_Collectors::get( 'sqlite' ); |
| 101 |
if ( $collector ) { |
| 102 |
$output['sqlite'] = new SQLite_QM_Output_Html( $collector ); |
| 103 |
} |
| 104 |
return $output; |
| 105 |
} |
| 106 |
|
| 107 |
add_filter( 'qm/outputter/html', 'register_sqlite_qm_output_html', 30, 2 ); |
| 108 |
|