-
Notifications
You must be signed in to change notification settings - Fork 100
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
trepmal
wants to merge
7
commits into
develop
Choose a base branch
from
add/qm-vip-concat
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
48523ae
Add QM Panel to display info about js/css concatenation
trepmal b98fbb6
phpcs fixes
trepmal 66aef06
check class exists
trepmal 3a47727
Merge branch 'develop' into add/qm-vip-concat
trepmal a14fdb9
Update qm-plugins/qm-vip-concat/class-qm-output-vip-concat.php
trepmal 1221ae8
Merge branch 'develop' into add/qm-vip-concat
rinatkhaziev 238171a
Merge branch 'develop' into add/qm-vip-concat
rinatkhaziev File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
49 changes: 49 additions & 0 deletions
49
qm-plugins/qm-vip-concat/class-qm-collector-vip-concat.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
147
qm-plugins/qm-vip-concat/class-qm-output-vip-concat.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 ); | ||
} | ||
|
||
/** | ||
* 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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 ); | ||
} ); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
# Query Monitor: VIP Concat | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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)?