PluginProbe
SQLite Database Integration / 2.2.21
SQLite Database Integration v2.2.21
3.0.2 3.0.1 trunk 2.1.13 2.1.14 2.1.15 2.1.16 2.2.0 2.2.1 2.2.10 2.2.11 2.2.12 2.2.13 2.2.14 2.2.15 2.2.16 2.2.17 2.2.18 2.2.19 2.2.2 2.2.20 2.2.21 2.2.22 2.2.23 2.2.3 All 32 releases
sqlite-database-integration / integrations / query-monitor / plugin.php

plugin.php in SQLite Database Integration 2.2.21, at integrations/query-monitor/plugin.php

90 lines 3.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php // phpcs:disable WordPress.Files.FileName.InvalidClassFileName
2
3 if ( ! class_exists( 'QM_Output_Html_DB_Queries' ) || ! class_exists( 'QM_Collectors' ) ) {
4 return;
5 }
6
7 /**
8 * Override Query Monitor's "QM_Output_Html_DB_Queries" to inject SQLite info.
9 */
10 class SQLite_QM_Output_Html_DB_Queries extends QM_Output_Html_DB_Queries {
11 /**
12 * Override the parent method to inject SQLite info.
13 *
14 * Currently, Query Monitor doesn't provide a way to customize the rendered
15 * query HTML. To overcome this limitation, we capture the HTML generated by
16 * the parent method and modify it to inject the SQLite query data.
17 *
18 * @param array<string, mixed> $row The row data.
19 * @param array<int, string> $cols The column names.
20 * @return void
21 */
22 protected function output_query_row( array $row, array $cols ) {
23 // Capture the query row HTML.
24 ob_start();
25 parent::output_query_row( $row, $cols );
26 $data = ob_get_length() > 0 ? ob_get_clean() : '';
27
28 // Get the corresponding SQLite queries.
29 global $wpdb;
30 static $query_index = 0;
31 $sqlite_queries = $wpdb->queries[ $query_index ]['sqlite_queries'] ?? array();
32 $sqlite_query_count = count( $sqlite_queries );
33 $query_index += 1;
34
35 // Build the SQLite info HTML.
36 $sqlite_info = sprintf(
37 '<div class="qm-info" style="margin: 15px 0 8px;">Executed %d SQLite %s:</div>',
38 $sqlite_query_count,
39 1 === $sqlite_query_count ? 'Query' : 'Queries'
40 );
41 $sqlite_info .= '<ol>';
42 foreach ( $sqlite_queries as $query ) {
43 $sqlite_info .= '<li class="qm-sqlite-query" style="list-style: decimal !important; margin-left: 20px !important;">';
44 $sqlite_info .= '<code>' . str_replace( '<br>', '', self::format_sql( $query['sql'] ) ) . '</code>';
45 $sqlite_info .= '</li>';
46 }
47 $sqlite_info .= '</ol>';
48
49 // Inject toggle button and SQLite info into the query row HTML.
50 $toggle_button = '<button class="qm-toggle sqlite-toggle" data-on="+" data-off="-" aria-expanded="false" aria-label="Toggle SQLite queries" title="Toggle SQLite queries"><span aria-hidden="true">+</span></button>';
51 $toggle_content = sprintf( '<div class="qm-toggled" style="display: none;">%s</div>', $sqlite_info );
52
53 $data = str_replace( 'qm-row-sql', 'qm-row-sql qm-has-toggle', $data );
54 $data = preg_replace(
55 '/(<td class="qm-row-sql.*?">)(.*?)(<\/td>)/s',
56 implode(
57 array(
58 '$1',
59 str_replace( '$', '\\$', $toggle_button ),
60 '$2',
61 str_replace( '$', '\\$', $toggle_content ),
62 '$3',
63 )
64 ),
65 $data
66 );
67 echo $data;
68 }
69 }
70
71 // Remove the default Query Monitor class and replace it with the custom one.
72 remove_filter( 'qm/outputter/html', 'register_qm_output_html_db_queries', 20 );
73
74 /**
75 * Register the custom HTML output class.
76 *
77 * @param array<string, QM_Output> $output
78 * @param QM_Collectors $collectors
79 * @return array<string, QM_Output>
80 */
81 function register_sqlite_qm_output_html_db_queries( array $output, $collectors ) {
82 $collector = QM_Collectors::get( 'db_queries' );
83 if ( $collector ) {
84 $output['db_queries'] = new SQLite_QM_Output_Html_DB_Queries( $collector );
85 }
86 return $output;
87 }
88
89 add_filter( 'qm/outputter/html', 'register_sqlite_qm_output_html_db_queries', 20, 2 );
90