| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* JCH Optimize - Performs several front-end optimizations for fast downloads |
| 5 |
* |
| 6 |
* @package jchoptimize/wordpress-platform |
| 7 |
* @author Samuel Marshall <samuel@jch-optimize.net> |
| 8 |
* @copyright Copyright (c) 2020 Samuel Marshall / JCH Optimize |
| 9 |
* @license GNU/GPLv3, or later. See LICENSE file |
| 10 |
* |
| 11 |
* If LICENSE file missing, see <http://www.gnu.org/licenses/>. |
| 12 |
*/ |
| 13 |
|
| 14 |
namespace JchOptimize\WordPress\Platform; |
| 15 |
|
| 16 |
defined('_WP_EXEC') or die('Restricted access'); |
| 17 |
|
| 18 |
use JchOptimize\Core\Helper; |
| 19 |
use JchOptimize\Core\Platform\ProfilerInterface; |
| 20 |
|
| 21 |
use function __; |
| 22 |
use function function_exists; |
| 23 |
use function is_super_admin; |
| 24 |
use function number_format; |
| 25 |
use function number_format_i18n; |
| 26 |
use function timer_stop; |
| 27 |
|
| 28 |
class Profiler implements ProfilerInterface |
| 29 |
{ |
| 30 |
/** |
| 31 |
* |
| 32 |
* @param string $html |
| 33 |
* @param bool $isAmpPage |
| 34 |
*/ |
| 35 |
public function attachProfiler(&$html, $isAmpPage = false): void |
| 36 |
{ |
| 37 |
if (! is_super_admin() || $isAmpPage) { |
| 38 |
return; |
| 39 |
} |
| 40 |
|
| 41 |
$items = Profiler::mark(true); |
| 42 |
|
| 43 |
$node = self::getAdminBarNodeBegin() . $items . self::getAdminBarNodeEnd(); |
| 44 |
$cdata = Helper::isXhtml($html) ? '/*<![CDATA[*/' : ''; |
| 45 |
|
| 46 |
$script = <<<HTML |
| 47 |
<script>{$cdata} |
| 48 |
const li = document.getElementById('wp-admin-bar-jch-optimize-profiler') |
| 49 |
if (li !== null){ |
| 50 |
li.classList.add('menupop') |
| 51 |
li.innerHTML = '{$node}' |
| 52 |
} |
| 53 |
{$cdata}</script> |
| 54 |
HTML; |
| 55 |
|
| 56 |
$html = str_replace('</body>', $script . '</body>', $html); |
| 57 |
} |
| 58 |
|
| 59 |
/** |
| 60 |
* |
| 61 |
* @staticvar string $item |
| 62 |
* |
| 63 |
* @param string|true $text |
| 64 |
* |
| 65 |
* @return null|string |
| 66 |
*/ |
| 67 |
public function mark($text): ?string |
| 68 |
{ |
| 69 |
static $item = ''; |
| 70 |
|
| 71 |
if ($text === true) { |
| 72 |
return $item; |
| 73 |
} |
| 74 |
|
| 75 |
static $prevTime = 0.0; |
| 76 |
if ($prevTime === 0.0) { |
| 77 |
$prevTime = microtime(true); |
| 78 |
} |
| 79 |
$currentTime = microtime(true); |
| 80 |
$timeInterval = ($currentTime - $prevTime) * 1000; |
| 81 |
$prevTime = $currentTime; |
| 82 |
|
| 83 |
$pageLoadTime = (float)timer_stop(); |
| 84 |
|
| 85 |
$timeIntervalFormatted = (function_exists('number_format_i18n')) |
| 86 |
? number_format_i18n($timeInterval, 3) |
| 87 |
: number_format($timeInterval, 3); |
| 88 |
|
| 89 |
$item .= self::addAdminBarItem($pageLoadTime . ' (+' . $timeIntervalFormatted . 'ms) - ' . $text); |
| 90 |
|
| 91 |
return null; |
| 92 |
} |
| 93 |
|
| 94 |
/** |
| 95 |
* |
| 96 |
* @param string $item |
| 97 |
* |
| 98 |
* @return string |
| 99 |
*/ |
| 100 |
protected static function addAdminBarItem(string $item): string |
| 101 |
{ |
| 102 |
static $counter = 0; |
| 103 |
$counter++; |
| 104 |
|
| 105 |
return <<<HTML |
| 106 |
<li id="wp-admin-bar-jch-optimize-profiler-item{$counter}"><div class="ab-item ab-empty-item"><a class="ab-item">{$item}</a></div></li> |
| 107 |
HTML; |
| 108 |
} |
| 109 |
|
| 110 |
/** |
| 111 |
* |
| 112 |
* @return string |
| 113 |
*/ |
| 114 |
protected static function getAdminBarNodeBegin(): string |
| 115 |
{ |
| 116 |
$profiler = __('Profiler', 'jch-optimize'); |
| 117 |
|
| 118 |
return <<<HTML |
| 119 |
<div class="ab-item ab-empty-item" aria-haspopup="true"><span class="wp-admin-bar-arrow" aria-hidden="true"></span><span class="dashicons dashicons-dashboard jch-ms-icon"></span><span>{$profiler}</span></div><div class="ab-sub-wrapper"><ul id="wp-admin-bar-jch-optimize-profiler-default" class="ab-submenu" style="overflow:auto;max-height: 600px;"> |
| 120 |
HTML; |
| 121 |
} |
| 122 |
|
| 123 |
/** |
| 124 |
* |
| 125 |
* @return string |
| 126 |
*/ |
| 127 |
protected static function getAdminBarNodeEnd(): string |
| 128 |
{ |
| 129 |
return '</ul></div>'; |
| 130 |
} |
| 131 |
|
| 132 |
/** |
| 133 |
* |
| 134 |
* @param string $text |
| 135 |
* @param bool $mark |
| 136 |
*/ |
| 137 |
public function start($text, $mark = false): void |
| 138 |
{ |
| 139 |
if ($mark) { |
| 140 |
self::mark('before' . $text); |
| 141 |
} |
| 142 |
} |
| 143 |
|
| 144 |
/** |
| 145 |
* |
| 146 |
* @param string $text |
| 147 |
* @param bool $mark |
| 148 |
*/ |
| 149 |
public function stop($text, $mark = false): void |
| 150 |
{ |
| 151 |
if ($mark) { |
| 152 |
self::mark('after' . $text); |
| 153 |
} |
| 154 |
} |
| 155 |
} |
| 156 |
|