Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add QM Panel to display info about js/css concatenation #5693

Open
wants to merge 7 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 49 additions & 0 deletions qm-plugins/qm-vip-concat/class-qm-collector-vip-concat.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php
/**
* Data collector class
*/
class QM_Collector_VIPConcat extends QM_Collector_Logger {

public $id = 'vip_concat';

public function set_up() {
parent::set_up();
foreach ( $this->get_levels() as $level ) {
add_action( "qm/{$level}", array( $this, $level ), 10, 2 );
}
}

/**
* @param mixed $message
* @param array<string, mixed> $context
* @phpstan-param LogMessage $message
* @return void
*/
public function vip_concat_info( $message, array $context = array() ) {
$this->store( 'vip_concat_info', $message, $context );
}
public function vip_concat_warn( $message, array $context = array() ) {
$this->store( 'vip_concat_warn', $message, $context );
}

/**
* @return array<int, string>
* @phpstan-return list<self::*>
*/
public function get_levels() {
return array(
'vip_concat_info',
'vip_concat_warn',
);
}

/**
* @return array<int, string>
* @phpstan-return list<self::*>
*/
public function get_warning_levels() {
return array(
'vip_concat_warn',
);
}
}
147 changes: 147 additions & 0 deletions qm-plugins/qm-vip-concat/class-qm-output-vip-concat.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
<?php
/**
* Output class
*
* Class QM_Output_VIPConcat
*/
class QM_Output_VIPConcat extends QM_Output_Html {

public function __construct( QM_Collector $collector ) {
parent::__construct( $collector );

add_filter( 'qm/output/menu_class', array( $this, 'admin_class' ) );
add_filter( 'qm/output/menus', array( $this, 'admin_menu' ), 101 );
Comment on lines +12 to +13
Copy link
Contributor

@rinatkhaziev rinatkhaziev Jul 24, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it possible to not display the panel altogether if there's no data (i.e. $data->logs is empty)?

}

/**
* Outputs data in the footer
*/
public function output() {
$data = $this->collector->get_data();

if ( empty( $data->logs ) ) {
$this->before_non_tabular_output();

$notice = sprintf(
/* translators: %s: Link to help article */
__( 'No data logged. <a href="%s">Read about the WordPress VIP Platform\'s file concatenation feature</a>.', 'query-monitor' ),
'https://docs.wpvip.com/vip-go-mu-plugins/file-concatenation-and-minification/'
);
echo $this->build_notice( $notice ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped --

$this->after_non_tabular_output();

return;
}

$levels = array();

foreach ( $this->collector->get_levels() as $level ) {
if ( $data->counts[ $level ] ) {
$levels[ $level ] = sprintf(
'%s (%d)',
ucfirst( $level ),
$data->counts[ $level ]
);
} else {
$levels[ $level ] = ucfirst( $level );
}
}
$this->before_tabular_output();

$level_args = array(
'all' => sprintf(
/* translators: %s: Total number of items in a list */
__( 'All (%d)', 'query-monitor' ),
count( $data->logs )
),
);

echo '<thead>';
echo '<tr>';
echo '<th scope="col" class="qm-filterable-column">';
echo $this->build_filter( 'type', $levels, __( 'Level', 'query-monitor' ), $level_args ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped --
echo '</th>';
echo '<th scope="col" class="qm-col-message">' . esc_html__( 'Message', 'query-monitor' ) . '</th>';
echo '</tr>';
echo '</thead>';
echo '<tbody>';

foreach ( $data->logs as $row ) {

$row_attr = array();
$row_attr['data-qm-type'] = $row['level'];

$attr = '';

foreach ( $row_attr as $a => $v ) {
rinatkhaziev marked this conversation as resolved.
Show resolved Hide resolved
$attr .= ' ' . $a . '="' . esc_attr( $v ) . '"';
}

$is_warning = in_array( $row['level'], $this->collector->get_warning_levels(), true );

if ( $is_warning ) {
$class = 'qm-warn';
} else {
$class = '';
}

echo '<tr' . $attr . ' class="' . esc_attr( $class ) . '">'; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped --
echo '<td class="qm-nowrap">';

if ( $is_warning ) {
// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
echo QueryMonitor::icon( 'warning' );
} else {
// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
echo QueryMonitor::icon( 'blank' );
}

echo esc_html( ucfirst( str_replace( 'vip_concat_', '', $row['level'] ) ) );
echo '</td>';
printf(
'<td><pre>%s</pre></td>',
esc_html( $row['message'] )
);

echo '</tr>';

}

echo '</tbody>';

$this->after_tabular_output();
}

/**
* Adds data to top admin bar
*
* @param array $title
*
* @return array
*/
public function admin_title( array $title ) {
return $title;
}

/**
* @param array $class
*
* @return array
*/
public function admin_class( array $class ) {
$class[] = 'qm-vip_concat';
return $class;
}

public function admin_menu( array $menu ) {

$menu[] = $this->menu( array(
'id' => 'qm-vip_concat',
'href' => '#qm-vip_concat',
'title' => __( 'VIP JS/CSS Concat', 'query-monitor' ),
));

return $menu;
}
}
32 changes: 32 additions & 0 deletions qm-plugins/qm-vip-concat/qm-vip-concat.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php
/**
* Plugin Name: Query Monitor: VIP Concat
* Description:
* Version: 0.1
* Author: trepmal
*/

add_action('plugins_loaded', function () {

/**
* Register collector, only if Query Monitor is enabled.
*/
if ( class_exists( 'QM_Collectors' ) && class_exists( 'QM_Collector_Logger' ) ) {
include_once 'class-qm-collector-vip-concat.php';

QM_Collectors::add( new QM_Collector_VIPConcat() );
}

/**
* Register output. The filter won't run if Query Monitor is not
* installed so we don't have to explicity check for it.
*/
add_filter( 'qm/outputter/html', function ( array $output ) {
include_once 'class-qm-output-vip-concat.php';
$collector = QM_Collectors::get( 'vip_concat' );
if ( $collector ) {
$output['vip_concat'] = new QM_Output_VIPConcat( $collector );
}
return $output;
}, 101 );
} );
3 changes: 3 additions & 0 deletions qm-plugins/qm-vip-concat/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Query Monitor: VIP Concat


3 changes: 3 additions & 0 deletions query-monitor.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,9 @@ function wpcom_vip_qm_require() {
if ( file_exists( __DIR__ . '/qm-plugins/qm-vip/qm-vip.php' ) ) {
require_once __DIR__ . '/qm-plugins/qm-vip/qm-vip.php';
}
if ( file_exists( __DIR__ . '/qm-plugins/qm-vip-concat/qm-vip-concat.php' ) ) {
require_once __DIR__ . '/qm-plugins/qm-vip-concat/qm-vip-concat.php';
}
if ( file_exists( __DIR__ . '/qm-plugins/qm-db-connections/qm-db-connections.php' ) ) {
require_once __DIR__ . '/qm-plugins/qm-db-connections/qm-db-connections.php';
}
Expand Down
Loading