PluginProbe
UsageDD / trunk
UsageDD vtrunk
usagedd / usagedd.php

usagedd.php in UsageDD trunk, at usagedd.php

69 lines 4.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /*
3 Plugin Name: UsageDD
4 Plugin URI: https://forum.dion-designs.com/f35/usagedd-support/
5 Version: 2.1
6 Author: Dion Designs
7 Description: Displays usage information to administrators
8 */
9
10 if (!defined('WPINC')) {
11 die('ERROR 000');
12 }
13
14 /*--------------------------------------
15 UsageDD
16 Copyright © 2023 by Dion Designs.
17 --------------------------------------*/
18
19 /*
20 You can change the CSS for the usage display.
21 DO NOT USE SINGLE-QUOTE ' CHARACTERS IN YOUR CSS!
22 You have been warned.
23 */
24
25 // CSS Starts after the next line
26 define('USAGEDD_CSS', '
27
28 .usage-dd {
29 display: block;
30 line-height: 24px;
31 background-color: #c4c4c4;
32 color: #000;
33 font-family: Helvetica,Arial,sans-serif;
34 font-size: 18px;
35 text-align: center;
36 }
37 .usagedd-bs {
38 margin: 0 45px;
39 }
40 .usagedd-ss {
41 margin: 0 4px;
42 border-left: 5px solid #999;
43 }
44 #usage_dd {
45 position: fixed;
46 left: 50%;
47 bottom: 0;
48 z-index: 9999998;
49 margin-left: -160px;
50 width: 320px;
51 height: 24px;
52 white-space: nowrap;
53 overflow: hidden;
54 background-color: rgba(160,160,160,0.5);
55 }
56
57 '); // CSS ends at the previous line. DO NOT CHANGE THIS LINE!
58
59 /*
60 If you change the following line from false to true, you will see a
61 usage line where a call to admin-ajax.php was made. Add up the
62 execution times and queries on all the usage lines, and you will
63 see why using admin-ajax.php should be avoided at all costs!
64 */
65 define('USAGEDD_AJAX_USAGE', false);
66
67 // DO NOT EDIT ANYTHING BEYOND THIS POINT!!!
68
69 class UsageDD { private $display = true; private $servertime, $assetdd, $css, $ajax, $db; private $precisn = 1; function __construct($css, $ajax) { global $wpdb; $this->css = $css; $this->ajax = $ajax; $this->db = $wpdb; $this->assetdd = class_exists('AssetDD'); add_action('init', array($this, 'hook_setup'), 9999); add_filter('wp_xmlrpc_server_class', array($this, 'no_usage_display')); add_filter('rest_jsonp_enabled', array($this, 'no_usage_display')); } function hook_setup() { if (!defined('NO_USAGEDD_DISPLAY') && current_user_can('update_core')) { if (defined('WP_ADMIN')) { add_action('admin_init', array($this, 'time_to_first_byte'), 9999); } else { add_action('wp_loaded', array($this, 'time_to_first_byte'), 9999); } if ($this->assetdd) { AssetDD::$css .= $this->css; } else { add_action('wp_head', array($this, 'add_css'), 9999); add_action('admin_head', array($this, 'add_css'), 9999); } } } function no_usage_display($val = false) { $this->display = false; return $val; } function time_to_first_byte() { global $timestart; if (!empty($_SERVER['REQUEST_TIME_FLOAT'])) { $timestart = $_SERVER['REQUEST_TIME_FLOAT']; } if (is_admin()) { global $pagenow; if ($pagenow == 'customize.php') { add_action('customize_controls_print_footer_scripts', array($this, 'setup_usage'), 9999); } else { add_action('admin_footer', array($this, 'setup_usage'), 9999); } $xcss = '.wrap .theme-overlay .theme-wrap,.wrap .theme-overlay .theme-backdrop,.wrap .wp-full-overlay-sidebar-content,#customize-preview iframe{bottom:24px}#customize-preview iframe{height:calc(100% - 24px)}'; if ($this->assetdd) { AssetDD::$css .= $xcss; } else { $this->css .= $xcss; } } else { add_action('wp_footer', array($this, 'setup_usage'), 9999); } $ttfb = microtime(true) - $timestart; if ($ttfb < 0.1) { $this->precisn = 3; } else if ($ttfb < 10.0) { $this->precisn = 2; } $this->servertime = strval(round($ttfb, $this->precisn)) . '<span class="usagedd-ss"></span>'; } function add_css() { echo '<style id="UsageDD" type="text/css">' . str_replace(array("\r","\n","\t"), '', $this->css) . "</style>\n"; } function setup_usage() { if ($this->assetdd) { add_action('assetdd_footer', array($this, 'display_usage'), 1); } else { remove_action('shutdown', 'wp_ob_end_flush_all', 1); add_action('shutdown', array($this, 'footer_output'), 1); ob_start(); } } function footer_output() { $html = str_ireplace('</BODY>', '</body>', strval(@ob_get_clean())); if (strpos($html, '</body>') !== false) { list($front, $html) = explode('</body>', $html); echo $front; $this->display_usage(); echo "\n</body>\n{$html}"; } else { echo $html; } wp_ob_end_flush_all(); } function display_usage() { global $timestart; if ($this->display && !defined('WP_INSTALLING') && (!defined('DOING_AJAX') || (defined('DOING_AJAX') && $this->ajax))) { $precision = 0; $memory_usage = memory_get_peak_usage() / 1048576; if ($memory_usage < 10) { $precision = 2; } else if ($memory_usage < 100) { $precision = 1; } $memory_usage = round($memory_usage, $precision); $time_usage = $this->servertime . round(microtime(true) - $timestart, $this->precisn); echo ((defined('DOING_AJAX')) ? '' : '<div id="usage_dd_spacer"></div>') . '<div class="usage-dd"' . ((defined('DOING_AJAX') && $this->ajax) ? '>' : ' id="usage_dd">') . "{$this->db->num_queries}Q<span class=\"usagedd-bs\">{$time_usage}</span>{$memory_usage}M</div>"; } } } add_action('plugins_loaded', function(){if(!defined('TOOLKITDD')){new UsageDD(USAGEDD_CSS, USAGEDD_AJAX_USAGE);}}, 1); register_activation_hook(__FILE__, function(){if(defined('TOOLKITDD')){die('Please enable UsageDD PRO in ToolkitDD.');}});