/var/www/html_de/wp-content/plugins/query-monitor/output/html/db_dupes.php


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
<?php declare(strict_types 1);
/**
 * Duplicate database query output for HTML pages.
 *
 * @package query-monitor
 */

if ( ! defined'ABSPATH' ) ) {
    exit;
}

class 
QM_Output_Html_DB_Dupes extends QM_Output_Html {

    
/**
     * Collector instance.
     *
     * @var QM_Collector_DB_Dupes Collector.
     */
    
protected $collector;

    public function 
__constructQM_Collector $collector ) {
        
parent::__construct$collector );
        
add_filter'qm/output/menus', array( $this'admin_menu' ), 45 );
        
add_filter'qm/output/panel_menus', array( $this'panel_menu' ), 25 );
    }

    
/**
     * @return string
     */
    
public function name() {
        return 
__'Duplicate Queries''query-monitor' );
    }

    
/**
     * @return void
     */
    
public function output() {
        
/** @var QM_Data_DB_Dupes $data */
        
$data $this->collector->get_data();

        if ( empty( 
$data->dupes ) ) {
            return;
        }

        
$this->before_tabular_output();

        echo 
'<thead>';

        echo 
'<tr>';
        echo 
'<th scope="col">' esc_html__'Query''query-monitor' ) . '</th>';
        echo 
'<th scope="col" class="qm-num">' esc_html__'Count''query-monitor' ) . '</th>';
        echo 
'<th scope="col" class="qm-num">' esc_html__'Time''query-monitor' ) . '</th>';
        echo 
'<th scope="col">' esc_html__'Callers''query-monitor' ) . '</th>';
        if ( ! empty( 
$data->dupe_components ) ) {
            echo 
'<th scope="col">' esc_html__'Components''query-monitor' ) . '</th>';
        }
        echo 
'<th scope="col">' esc_html__'Potential Troublemakers''query-monitor' ) . '</th>';
        echo 
'</tr>';

        echo 
'</thead>';

        echo 
'<tbody>';

        
/* translators: %s: Number of calls to a PHP function */
        
$call_text _n_noop'%s call''%s calls''query-monitor' );

        foreach ( 
$data->dupes as $sql => $queries ) {

            
// This should probably happen in the collector's processor
            
$type QM_Util::get_query_type$sql );
            
$sql_out self::format_sql$sql );
            
$time $data->dupe_times$sql ];

            if ( 
'SELECT' !== $type ) {
                
$sql_out "<span class='qm-nonselectsql'>{$sql_out}</span>";
            }

            echo 
'<tr>';
            echo 
'<td class="qm-row-sql qm-ltr qm-wrap">';
            echo 
$sql_out// WPCS: XSS ok;
            
echo '</td>';
            echo 
'<td class="qm-num">';
            echo 
esc_htmlnumber_format_i18ncount$queries ), ) );
            echo 
'</td>';
            echo 
'<td class="qm-num">';
            echo 
esc_htmlnumber_format_i18n$time) );
            echo 
'</td>';
            echo 
'<td class="qm-row-caller qm-nowrap qm-ltr">';
            foreach ( 
$data->dupe_callers$sql ] as $caller => $calls ) {
                echo 
self::build_filter_trigger'db_queries''caller'$caller'<code>' esc_html$caller ) . '</code>' ); // WPCS: XSS ok;
                
printf(
                    
'<br><span class="qm-info qm-supplemental">%s</span><br>',
                    
esc_htmlsprintf(
                        
translate_nooped_plural$call_text$calls'query-monitor' ),
                        
number_format_i18n$calls )
                    ) )
                );
            }
            echo 
'</td>';
            if ( isset( 
$data->dupe_components$sql ] ) ) {
                echo 
'<td class="qm-row-component qm-nowrap">';
                foreach ( 
$data->dupe_components$sql ] as $component => $calls ) {
                    
printf(
                        
'%s<br><span class="qm-info qm-supplemental">%s</span><br>',
                        
esc_html$component ),
                        
esc_htmlsprintf(
                            
translate_nooped_plural$call_text$calls'query-monitor' ),
                            
number_format_i18n$calls )
                        ) )
                    );
                }
                echo 
'</td>';
            }
            echo 
'<td class="qm-row-caller qm-nowrap qm-ltr">';
            foreach ( 
$data->dupe_sources$sql ] as $source => $calls ) {
                
printf(
                    
'<code>%s</code><br><span class="qm-info qm-supplemental">%s</span><br>',
                    
esc_html$source ),
                    
esc_htmlsprintf(
                        
translate_nooped_plural$call_text$calls'query-monitor' ),
                        
number_format_i18n$calls )
                    ) )
                );
            }
            echo 
'</td>';
            echo 
'</tr>';
        }
        echo 
'</tbody>';

        
$this->after_tabular_output();
    }

    
/**
     * @param array<string, mixed[]> $menu
     * @return array<string, mixed[]>
     */
    
public function admin_menu( array $menu ) {
        
/** @var QM_Collector_DB_Dupes|null $dbq */
        
$dbq QM_Collectors::get'db_dupes' );

        if ( 
$dbq ) {
            
/** @var QM_Data_DB_Queries $dbq_data */
            
$dbq_data $dbq->get_data();
            if ( ! empty( 
$dbq_data->dupes ) ) {
                
$count 0;

                foreach ( 
$dbq_data->dupes as $dupe ) {
                    
$count += count$dupe );
                }

                
$menu$this->collector->id() ] = $this->menu( array(
                    
'title' => esc_htmlsprintf(
                        
/* translators: %s: Number of duplicate database queries */
                        
__'Duplicate Queries (%s)''query-monitor' ),
                        
number_format_i18n$count )
                    ) ),
                ) );
            }
        }
        return 
$menu;

    }

    
/**
     * @param array<string, mixed[]> $menu
     * @return array<string, mixed[]>
     */
    
public function panel_menu( array $menu ) {
        
$id $this->collector->id();
        if ( isset( 
$menu$id ] ) ) {
            
$menu['qm-db_queries']['children'][] = $menu$id ];
            unset( 
$menu$id ] );
        }

        return 
$menu;
    }

}

/**
 * @param array<string, QM_Output> $output
 * @param QM_Collectors $collectors
 * @return array<string, QM_Output>
 */
function register_qm_output_html_db_dupes( array $outputQM_Collectors $collectors ) {
    
$collector QM_Collectors::get'db_dupes' );
    if ( 
$collector ) {
        
$output['db_dupes'] = new QM_Output_Html_DB_Dupes$collector );
    }
    return 
$output;
}

add_filter'qm/outputter/html''register_qm_output_html_db_dupes'45);