Initial commit

This commit is contained in:
Felix Förtsch
2020-10-20 14:39:50 +02:00
commit 648ded8896
1225 changed files with 216511 additions and 0 deletions

View File

@@ -0,0 +1,99 @@
<?php
/**
* Data collector class
*/
class QM_Collector_IncludedFiles extends QM_Collector {
public $id = 'included_files';
public function name() {
return __( 'Included files', 'query-monitor' );
}
public function process() {
$included_files = get_included_files();
$opcache_enabled = function_exists('opcache_get_status');
$included_files_opcode_stats = $opcache_enabled ? opcache_get_status( true ) : null;
$included_files_component = array();
$this->data['total_filesize'] = 0;
$this->data['total_opcache_filesize'] = 0;
//Calculate stats
$this->data['stats'] = array();
$this->data['stats_raw'] = array();
foreach($included_files as $included_file)
{
$component = QM_Util::get_file_component($included_file)->name;
$included_files_component[] = $component;
//Create component array if it does not exist
if(!isset($this->data['stats'][$component]))
$this->data['stats'][$component] = array();
//Create size array if it does not exist
if(!isset($this->data['stats'][$component]['size']))
$this->data['stats'][$component]['size'] = 0;
//Create number of files array if it does not exist
if(!isset($this->data['stats'][$component]['number_of_files']))
$this->data['stats'][$component]['number_of_files'] = 0;
//Create opcode array if it does not exist
if($opcache_enabled) {
if(!isset($this->data['stats'][$component]['opcache_size']))
$this->data['stats'][$component]['opcache_size'] = 0;
}
//Record total filesize, opcode size and number of files
$filesize = filesize($included_file);
$this->data['stats'][$component]['size'] += $filesize;
$this->data['total_filesize'] += $filesize;
$this->data['stats'][$component]['number_of_files']++;
if($opcache_enabled) {
$this->data['stats'][$component]['opcache_size'] += $included_files_opcode_stats['scripts'][$included_file]['memory_consumption'];
$this->data['total_opcache_filesize'] += $included_files_opcode_stats['scripts'][$included_file]['memory_consumption'];
}
}
//Order data array by number of files
$counts = array();
foreach ($this->data['stats'] as $component => $values) {
$counts[$component] = $values['size'];
}
array_multisort($counts, SORT_DESC, $this->data['stats']);
//Convert file sizes to KB
foreach($this->data['stats'] as $component => $values) {
$this->data['stats'][$component]['size_kb'] = $this->format_bytes_to_kb($this->data['stats'][$component]['size'], 2);
if($opcache_enabled)
$this->data['stats'][$component]['opcache_size_kb'] = $this->format_bytes_to_kb($this->data['stats'][$component]['opcache_size'], 2);
}
//Convert totals
$this->data['total_filesize_kb'] = $this->format_bytes_to_kb($this->data['total_filesize'], 2);
$this->data['total_opcache_filesize_kb'] = $this->format_bytes_to_kb($this->data['total_opcache_filesize'], 2);
//Misc stats
$this->data['included_files_component'] = $included_files_component;
$this->data['included_files_number'] = sizeof($included_files);
$this->data['included_files'] = $included_files;
}
/**
* Adapted from:
* http://stackoverflow.com/a/2510459
*/
private function format_bytes_to_kb($bytes, $precision = 2) {
$bytes = max($bytes, 0);
$bytes /= pow(1000, 1);
return round($bytes, $precision);
}
}

View File

@@ -0,0 +1,177 @@
<?php
/**
* Output class
*
* Class QM_Output_IncludedFiles
*/
class QM_Output_IncludedFiles extends QM_Output_Html {
public function __construct( QM_Collector $collector ) {
parent::__construct( $collector );
add_filter( 'qm/output/menus', array( $this, 'admin_menu' ), 101 );
add_filter( 'qm/output/title', array( $this, 'admin_title' ), 101 );
add_filter( 'qm/output/menu_class', array( $this, 'admin_class' ) );
}
/**
* Outputs data in the footer
*/
public function output() {
$data = $this->collector->get_data();
$opcache_enabled = function_exists('opcache_get_status');
?>
<!-- Print total stats for included files -->
<div class="qm" id="<?php echo esc_attr($this->collector->id())?>">
<table cellspacing="0">
<thead>
<tr>
<th scope="col">
<?php echo __('Total number of included files','query-monitor'); ?>
</th>
<th scope="col">
<?php echo __('Total file size','query-monitor'); ?>
</th>
<th scope="col">
<?php echo __('Total OPCache memory consumption','query-monitor'); ?>
</th>
</tr>
</thead>
<tbody>
<tr>
<td class="qm-ltr">
<?php echo $data['included_files_number']; ?>
</td>
<td class="qm-ltr">
<?php echo $data['total_filesize_kb']; ?> KB
</td>
<td class="qm-nowrap">
<?php
if($opcache_enabled)
echo $data['total_opcache_filesize_kb'] . " KB";
else
echo "N/A";
?>
</td>
</tr>
</tbody>
</table>
</div>
<!-- Print stats for included files -->
<div class="qm" id="<?php echo esc_attr($this->collector->id())?>-aggregated">
<table cellspacing="0">
<thead>
<tr>
<th scope="col">
<?php echo __('Included files by component','query-monitor'); ?>
</th>
<th scope="col">
<?php echo __('Number of included files','query-monitor'); ?>
</th>
<th scope="col">
<?php echo __('Total file size','query-monitor'); ?>
</th>
<th scope="col">
<?php echo __('Zend OPCache memory consumption','query-monitor'); ?>
</th>
</tr>
</thead>
<tbody>
<?php foreach($data['stats'] as $component => $component_stats) : ?>
<tr>
<td class="qm-ltr">
<?php echo esc_html($component); ?>
</td>
<td class="qm-ltr">
<?php echo esc_html($component_stats['number_of_files']); ?>
</td>
<td class="qm-nowrap">
<?php echo $component_stats['size_kb'] ?> KB
</td>
<td class="qm-nowrap">
<?php
if($opcache_enabled)
echo $component_stats['opcache_size_kb'] . " KB";
else
echo "N/A";
?>
</td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
</div>
<!-- Print detailed included files info -->
<div class="qm" id="<?php echo esc_attr($this->collector->id())?>-full">
<table cellspacing="0">
<thead>
<tr>
<th scope="col">
<?php echo __('Included files','query-monitor'); ?>
</th>
<th scope="col">
<?php echo __('Component','query-monitor'); ?>
</th>
</tr>
</thead>
<tbody>
<?php foreach($data['included_files'] as $key => $file) : ?>
<tr>
<td class="qm-ltr">
<?php echo esc_html($file); ?>
</td>
<td class="qm-nowrap">
<?php echo $data['included_files_component'][$key]; ?>
</td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
</div>
<?php
}
/**
* Adds data to top admin bar
*
* @param array $title
*
* @return array
*/
public function admin_title( array $title ) {
$data = $this->collector->get_data();
$title[] = sprintf(
_x( '%s<small>F</small>', 'number of included files', 'query-monitor' ),
$data['included_files_number']
);
return $title;
}
/**
* @param array $class
*
* @return array
*/
public function admin_class( array $class ) {
$class[] = 'qm-included_files';
return $class;
}
public function admin_menu( array $menu ) {
$data = $this->collector->get_data();
$menu[] = $this->menu( array(
'id' => 'qm-included_files',
'href' => '#qm-included_files',
'title' => sprintf( __( 'Included files (%s)', 'query-monitor' ), $data['included_files_number'] )
));
return $menu;
}
}

View File

@@ -0,0 +1,10 @@
{
"name": "khromov/query-monitor-included-files",
"description": "Shows number of included files in admin bar and in the footer of each page load. Requires Query Monitor.",
"keywords": ["wordpress", "debug"],
"license": "GPL2",
"type": "wordpress-plugin",
"require": {
"composer/installers": "~1.0"
}
}

View File

@@ -0,0 +1,31 @@
<?php
/**
* Plugin Name: Query Monitor: Included files
* Description: Shows number of included files in Query Monitor
* Version: 1.3
* Author: khromov
* GitHub Plugin URI: khromov/wp-query-monitor-included-files
*/
add_action('plugins_loaded', function() {
/**
* Register collector, only if Query Monitor is enabled.
*/
if(class_exists('QM_Collectors')) {
include 'classes/QM_Collector_IncludedFiles.class.php';
QM_Collectors::add( new QM_Collector_IncludedFiles() );
}
/**
* 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, QM_Collectors $collectors) {
include 'classes/QM_Output_IncludedFiles.class.php';
if ( $collector = QM_Collectors::get( 'included_files' ) ) {
$output['included_files'] = new QM_Output_IncludedFiles( $collector );
}
return $output;
}, 101, 2 );
});

View File

@@ -0,0 +1,50 @@
=== Query Monitor: Included files ===
Contributors: khromov
Tags: debugging, query monitor
Requires at least: 3.5
Tested up to: 4.2
Stable tag: 1.3
License: GPL2
Shows number of included files in admin bar and in the footer of each page load. Requires Query Monitor.
== Description ==
This plugin lets you see the number of included files for each page load in the admin bar. You can also see the full list of included files and the order in which they were included.
**Usage**
*Basic usage*
Install this plugin, install Query Monitor. Check the admin bar.
== Requirements ==
* PHP 5.3 or higher
== Translations ==
* None
== Installation ==
1. Upload the `query-monitor-included-files` folder to `/wp-content/plugins/`
2. Activate the plugin (Query Monitor: Included files) through the 'Plugins' menu in WordPress
3. Use the functionality via the admin bar
== Screenshots ==
1. None yet
== Changelog ==
= 1.3 =
* Added aggregated totals
* Added OPCache memory consumption per file/totals
= 1.2 =
* Added support for showing total number of files included by component
* Added support for showing total filesize of included files by component
= 1.1 =
* Now shows which component loaded each file (Core, plugins, theme)
* File names are now escaped before output
= 1.0 =
* Initial release