@@ -6,10 +6,13 @@ |
||
| 6 | 6 | <tr> |
| 7 | 7 | <td><?= __('No entries found.', 'blackbar'); ?></td> |
| 8 | 8 | </tr> |
| 9 | - <?php else : ?> |
|
| 9 | + <?php else { |
|
| 10 | + : ?> |
|
| 10 | 11 | <?php foreach ($entries as $entry) : ?> |
| 11 | 12 | <tr> |
| 12 | - <td class="glbb-small"><?= $entry['name']; ?></td> |
|
| 13 | + <td class="glbb-small"><?= $entry['name']; |
|
| 14 | +} |
|
| 15 | +?></td> |
|
| 13 | 16 | <td><pre><?= $entry['message']; ?></pre></td> |
| 14 | 17 | </tr> |
| 15 | 18 | <?php endforeach; ?> |
@@ -6,14 +6,17 @@ |
||
| 6 | 6 | <tr> |
| 7 | 7 | <td><?= __('No entries found.', 'blackbar'); ?></td> |
| 8 | 8 | </tr> |
| 9 | - <?php else : ?> |
|
| 9 | + <?php else { |
|
| 10 | + : ?> |
|
| 10 | 11 | <?php foreach ($actions->getMeasure() as $hook => $flow) : ?> |
| 11 | 12 | <tr> |
| 12 | 13 | <td class="glbb-smalls"><?php |
| 13 | 14 | // glsr_log($flow); |
| 14 | 15 | ?></td> |
| 15 | 16 | </tr> |
| 16 | - <?php endforeach; ?> |
|
| 17 | + <?php endforeach; |
|
| 18 | +} |
|
| 19 | +?> |
|
| 17 | 20 | <?php endif; ?> |
| 18 | 21 | </tbody> |
| 19 | 22 | </table> |
@@ -8,9 +8,12 @@ |
||
| 8 | 8 | </tr> |
| 9 | 9 | </tbody> |
| 10 | 10 | </table> |
| 11 | -<?php else : ?> |
|
| 11 | +<?php else { |
|
| 12 | + : ?> |
|
| 12 | 13 | <form method="get" class="glbb-queries-filter"> |
| 13 | - <input type="text" name="glbb_query_filter" id="glbb_query_filter" placeholder="<?= __('Find queries containing', 'blackbar'); ?>"> |
|
| 14 | + <input type="text" name="glbb_query_filter" id="glbb_query_filter" placeholder="<?= __('Find queries containing', 'blackbar'); |
|
| 15 | +} |
|
| 16 | +?>"> |
|
| 14 | 17 | <input type="text" name="glbb_query_min_time" id="glbb_query_min_time" placeholder="<?= __('Minimum Execution Time', 'blackbar'); ?>"> |
| 15 | 18 | </form> |
| 16 | 19 | <table class="glbb-queries-table"> |
@@ -6,10 +6,13 @@ |
||
| 6 | 6 | <tr> |
| 7 | 7 | <td><?= __('No entries found.', 'blackbar'); ?></td> |
| 8 | 8 | </tr> |
| 9 | - <?php else : ?> |
|
| 9 | + <?php else { |
|
| 10 | + : ?> |
|
| 10 | 11 | <?php foreach ($profiler->getMeasure() as $timer) : ?> |
| 11 | 12 | <tr> |
| 12 | - <td><?= $profiler->getNameString($timer); ?></td> |
|
| 13 | + <td><?= $profiler->getNameString($timer); |
|
| 14 | +} |
|
| 15 | +?></td> |
|
| 13 | 16 | <td class="glbb-medium"><?= $profiler->getTimeString($timer); ?></td> |
| 14 | 17 | <td class="glbb-medium"><?= $profiler->getMemoryString($timer); ?></td> |
| 15 | 18 | </tr> |
@@ -7,8 +7,8 @@ discard block |
||
| 7 | 7 | * @see https://docs.gravityforms.com/gform_noconflict_scripts/ |
| 8 | 8 | */ |
| 9 | 9 | add_filter('gform_noconflict_scripts', function (array $scripts) { |
| 10 | - $scripts[] = 'blackbar'; |
|
| 11 | - return $scripts; |
|
| 10 | + $scripts[] = 'blackbar'; |
|
| 11 | + return $scripts; |
|
| 12 | 12 | }); |
| 13 | 13 | |
| 14 | 14 | /* |
@@ -16,7 +16,7 @@ discard block |
||
| 16 | 16 | * @see https://docs.gravityforms.com/gform_noconflict_styles/ |
| 17 | 17 | */ |
| 18 | 18 | add_filter('gform_noconflict_styles', function (array $styles) { |
| 19 | - $styles[] = 'blackbar'; |
|
| 20 | - $styles[] = 'blackbar-syntax'; |
|
| 21 | - return $styles; |
|
| 19 | + $styles[] = 'blackbar'; |
|
| 20 | + $styles[] = 'blackbar-syntax'; |
|
| 21 | + return $styles; |
|
| 22 | 22 | }); |
@@ -6,7 +6,8 @@ discard block |
||
| 6 | 6 | * @return array |
| 7 | 7 | * @see https://docs.gravityforms.com/gform_noconflict_scripts/ |
| 8 | 8 | */ |
| 9 | -add_filter('gform_noconflict_scripts', function (array $scripts) { |
|
| 9 | +add_filter('gform_noconflict_scripts', function (array $scripts) |
|
| 10 | +{ |
|
| 10 | 11 | $scripts[] = 'blackbar'; |
| 11 | 12 | return $scripts; |
| 12 | 13 | }); |
@@ -15,7 +16,8 @@ discard block |
||
| 15 | 16 | * @return array |
| 16 | 17 | * @see https://docs.gravityforms.com/gform_noconflict_styles/ |
| 17 | 18 | */ |
| 18 | -add_filter('gform_noconflict_styles', function (array $styles) { |
|
| 19 | +add_filter('gform_noconflict_styles', function (array $styles) |
|
| 20 | +{ |
|
| 19 | 21 | $styles[] = 'blackbar'; |
| 20 | 22 | $styles[] = 'blackbar-syntax'; |
| 21 | 23 | return $styles; |
@@ -20,13 +20,13 @@ |
||
| 20 | 20 | defined('ABSPATH') || exit; |
| 21 | 21 | |
| 22 | 22 | if (!class_exists('GL_Plugin_Check_v6')) { |
| 23 | - require_once __DIR__.'/activate.php'; |
|
| 23 | + require_once __DIR__.'/activate.php'; |
|
| 24 | 24 | } |
| 25 | 25 | if ((new GL_Plugin_Check_v6(__FILE__))->canProceed()) { |
| 26 | - require_once __DIR__.'/autoload.php'; |
|
| 27 | - require_once __DIR__.'/compatibility.php'; |
|
| 28 | - if (!defined('SAVEQUERIES')) { |
|
| 29 | - define('SAVEQUERIES', 1); |
|
| 30 | - } |
|
| 31 | - (new GeminiLabs\BlackBar\Application())->init(); |
|
| 26 | + require_once __DIR__.'/autoload.php'; |
|
| 27 | + require_once __DIR__.'/compatibility.php'; |
|
| 28 | + if (!defined('SAVEQUERIES')) { |
|
| 29 | + define('SAVEQUERIES', 1); |
|
| 30 | + } |
|
| 31 | + (new GeminiLabs\BlackBar\Application())->init(); |
|
| 32 | 32 | } |
@@ -3,20 +3,20 @@ |
||
| 3 | 3 | defined('WPINC') || die; |
| 4 | 4 | |
| 5 | 5 | spl_autoload_register(function ($className) { |
| 6 | - $namespaces = [ |
|
| 7 | - 'GeminiLabs\\BlackBar\\' => __DIR__.'/plugin/', |
|
| 8 | - 'GeminiLabs\\BlackBar\\Tests\\' => __DIR__.'/tests/', |
|
| 9 | - ]; |
|
| 10 | - foreach ($namespaces as $prefix => $baseDir) { |
|
| 11 | - $len = strlen($prefix); |
|
| 12 | - if (0 !== strncmp($prefix, $className, $len)) { |
|
| 13 | - continue; |
|
| 14 | - } |
|
| 15 | - $file = $baseDir.str_replace('\\', '/', substr($className, $len)).'.php'; |
|
| 16 | - if (!file_exists($file)) { |
|
| 17 | - continue; |
|
| 18 | - } |
|
| 19 | - require $file; |
|
| 20 | - break; |
|
| 21 | - } |
|
| 6 | + $namespaces = [ |
|
| 7 | + 'GeminiLabs\\BlackBar\\' => __DIR__.'/plugin/', |
|
| 8 | + 'GeminiLabs\\BlackBar\\Tests\\' => __DIR__.'/tests/', |
|
| 9 | + ]; |
|
| 10 | + foreach ($namespaces as $prefix => $baseDir) { |
|
| 11 | + $len = strlen($prefix); |
|
| 12 | + if (0 !== strncmp($prefix, $className, $len)) { |
|
| 13 | + continue; |
|
| 14 | + } |
|
| 15 | + $file = $baseDir.str_replace('\\', '/', substr($className, $len)).'.php'; |
|
| 16 | + if (!file_exists($file)) { |
|
| 17 | + continue; |
|
| 18 | + } |
|
| 19 | + require $file; |
|
| 20 | + break; |
|
| 21 | + } |
|
| 22 | 22 | }); |
@@ -2,7 +2,8 @@ |
||
| 2 | 2 | |
| 3 | 3 | defined('WPINC') || die; |
| 4 | 4 | |
| 5 | -spl_autoload_register(function ($className) { |
|
| 5 | +spl_autoload_register(function ($className) |
|
| 6 | +{ |
|
| 6 | 7 | $namespaces = [ |
| 7 | 8 | 'GeminiLabs\\BlackBar\\' => __DIR__.'/plugin/', |
| 8 | 9 | 'GeminiLabs\\BlackBar\\Tests\\' => __DIR__.'/tests/', |
@@ -6,70 +6,70 @@ |
||
| 6 | 6 | |
| 7 | 7 | class Console |
| 8 | 8 | { |
| 9 | - const ERROR_CODES = array( |
|
| 10 | - E_ERROR => 'Error', // 1 |
|
| 11 | - E_WARNING => 'Warning', // 2 |
|
| 12 | - E_NOTICE => 'Notice', // 8 |
|
| 13 | - E_STRICT => 'Strict', // 2048 |
|
| 14 | - E_DEPRECATED => 'Deprecated', // 8192 |
|
| 15 | - ); |
|
| 9 | + const ERROR_CODES = array( |
|
| 10 | + E_ERROR => 'Error', // 1 |
|
| 11 | + E_WARNING => 'Warning', // 2 |
|
| 12 | + E_NOTICE => 'Notice', // 8 |
|
| 13 | + E_STRICT => 'Strict', // 2048 |
|
| 14 | + E_DEPRECATED => 'Deprecated', // 8192 |
|
| 15 | + ); |
|
| 16 | 16 | |
| 17 | - const MAPPED_ERROR_CODES = array( |
|
| 18 | - 'debug' => 0, |
|
| 19 | - 'info' => 0, |
|
| 20 | - 'notice' => 0, |
|
| 21 | - 'warning' => E_NOTICE, // 8 |
|
| 22 | - 'error' => E_WARNING, // 2 |
|
| 23 | - 'critical' => E_WARNING, // 2 |
|
| 24 | - 'alert' => E_WARNING, // 2 |
|
| 25 | - 'emergency' => E_WARNING, // 2 |
|
| 26 | - ); |
|
| 17 | + const MAPPED_ERROR_CODES = array( |
|
| 18 | + 'debug' => 0, |
|
| 19 | + 'info' => 0, |
|
| 20 | + 'notice' => 0, |
|
| 21 | + 'warning' => E_NOTICE, // 8 |
|
| 22 | + 'error' => E_WARNING, // 2 |
|
| 23 | + 'critical' => E_WARNING, // 2 |
|
| 24 | + 'alert' => E_WARNING, // 2 |
|
| 25 | + 'emergency' => E_WARNING, // 2 |
|
| 26 | + ); |
|
| 27 | 27 | |
| 28 | - public $entries = array(); |
|
| 28 | + public $entries = array(); |
|
| 29 | 29 | |
| 30 | - /** |
|
| 31 | - * @param string $message |
|
| 32 | - * @return static |
|
| 33 | - */ |
|
| 34 | - public function store($message, $errno = 0, $location = '') |
|
| 35 | - { |
|
| 36 | - $errname = 'Debug'; |
|
| 37 | - if (array_key_exists($errno, static::MAPPED_ERROR_CODES)) { |
|
| 38 | - $errname = ucfirst($errno); |
|
| 39 | - $errno = static::MAPPED_ERROR_CODES[$errno]; |
|
| 40 | - } elseif (array_key_exists($errno, static::ERROR_CODES)) { |
|
| 41 | - $errname = static::ERROR_CODES[$errno]; |
|
| 42 | - } |
|
| 43 | - $this->entries[] = array( |
|
| 44 | - 'errno' => $errno, |
|
| 45 | - 'message' => $location.$this->normalizeValue($message), |
|
| 46 | - 'name' => sprintf('<span class="glbb-info glbb-%s">%s</span>', strtolower($errname), $errname), |
|
| 47 | - ); |
|
| 48 | - return $this; |
|
| 49 | - } |
|
| 30 | + /** |
|
| 31 | + * @param string $message |
|
| 32 | + * @return static |
|
| 33 | + */ |
|
| 34 | + public function store($message, $errno = 0, $location = '') |
|
| 35 | + { |
|
| 36 | + $errname = 'Debug'; |
|
| 37 | + if (array_key_exists($errno, static::MAPPED_ERROR_CODES)) { |
|
| 38 | + $errname = ucfirst($errno); |
|
| 39 | + $errno = static::MAPPED_ERROR_CODES[$errno]; |
|
| 40 | + } elseif (array_key_exists($errno, static::ERROR_CODES)) { |
|
| 41 | + $errname = static::ERROR_CODES[$errno]; |
|
| 42 | + } |
|
| 43 | + $this->entries[] = array( |
|
| 44 | + 'errno' => $errno, |
|
| 45 | + 'message' => $location.$this->normalizeValue($message), |
|
| 46 | + 'name' => sprintf('<span class="glbb-info glbb-%s">%s</span>', strtolower($errname), $errname), |
|
| 47 | + ); |
|
| 48 | + return $this; |
|
| 49 | + } |
|
| 50 | 50 | |
| 51 | - /** |
|
| 52 | - * @param mixed $value |
|
| 53 | - * @return bool |
|
| 54 | - */ |
|
| 55 | - protected function isObjectOrArray($value) |
|
| 56 | - { |
|
| 57 | - return is_object($value) || is_array($value); |
|
| 58 | - } |
|
| 51 | + /** |
|
| 52 | + * @param mixed $value |
|
| 53 | + * @return bool |
|
| 54 | + */ |
|
| 55 | + protected function isObjectOrArray($value) |
|
| 56 | + { |
|
| 57 | + return is_object($value) || is_array($value); |
|
| 58 | + } |
|
| 59 | 59 | |
| 60 | - /** |
|
| 61 | - * @param mixed $value |
|
| 62 | - * @return string |
|
| 63 | - */ |
|
| 64 | - protected function normalizeValue($value) |
|
| 65 | - { |
|
| 66 | - if ($value instanceof DateTime) { |
|
| 67 | - $value = $value->format('Y-m-d H:i:s'); |
|
| 68 | - } elseif ($this->isObjectOrArray($value)) { |
|
| 69 | - $value = print_r(json_decode(json_encode($value)), true); |
|
| 70 | - } elseif (is_resource($value)) { |
|
| 71 | - $value = (string) $value; |
|
| 72 | - } |
|
| 73 | - return esc_html($value); |
|
| 74 | - } |
|
| 60 | + /** |
|
| 61 | + * @param mixed $value |
|
| 62 | + * @return string |
|
| 63 | + */ |
|
| 64 | + protected function normalizeValue($value) |
|
| 65 | + { |
|
| 66 | + if ($value instanceof DateTime) { |
|
| 67 | + $value = $value->format('Y-m-d H:i:s'); |
|
| 68 | + } elseif ($this->isObjectOrArray($value)) { |
|
| 69 | + $value = print_r(json_decode(json_encode($value)), true); |
|
| 70 | + } elseif (is_resource($value)) { |
|
| 71 | + $value = (string) $value; |
|
| 72 | + } |
|
| 73 | + return esc_html($value); |
|
| 74 | + } |
|
| 75 | 75 | } |
@@ -37,7 +37,8 @@ discard block |
||
| 37 | 37 | if (array_key_exists($errno, static::MAPPED_ERROR_CODES)) { |
| 38 | 38 | $errname = ucfirst($errno); |
| 39 | 39 | $errno = static::MAPPED_ERROR_CODES[$errno]; |
| 40 | - } elseif (array_key_exists($errno, static::ERROR_CODES)) { |
|
| 40 | + } |
|
| 41 | + elseif (array_key_exists($errno, static::ERROR_CODES)) { |
|
| 41 | 42 | $errname = static::ERROR_CODES[$errno]; |
| 42 | 43 | } |
| 43 | 44 | $this->entries[] = array( |
@@ -65,9 +66,11 @@ discard block |
||
| 65 | 66 | { |
| 66 | 67 | if ($value instanceof DateTime) { |
| 67 | 68 | $value = $value->format('Y-m-d H:i:s'); |
| 68 | - } elseif ($this->isObjectOrArray($value)) { |
|
| 69 | + } |
|
| 70 | + elseif ($this->isObjectOrArray($value)) { |
|
| 69 | 71 | $value = print_r(json_decode(json_encode($value)), true); |
| 70 | - } elseif (is_resource($value)) { |
|
| 72 | + } |
|
| 73 | + elseif (is_resource($value)) { |
|
| 71 | 74 | $value = (string) $value; |
| 72 | 75 | } |
| 73 | 76 | return esc_html($value); |
@@ -4,161 +4,161 @@ |
||
| 4 | 4 | |
| 5 | 5 | class SlowActions |
| 6 | 6 | { |
| 7 | - /** |
|
| 8 | - * @var array |
|
| 9 | - */ |
|
| 10 | - protected $flow; |
|
| 11 | - |
|
| 12 | - /** |
|
| 13 | - * This is the time that WordPress takes to execute the all hook. |
|
| 14 | - * @var int |
|
| 15 | - */ |
|
| 16 | - protected $noise = 0; |
|
| 17 | - |
|
| 18 | - /** |
|
| 19 | - * @var int |
|
| 20 | - */ |
|
| 21 | - protected $start = null; |
|
| 22 | - |
|
| 23 | - /** |
|
| 24 | - * @var int |
|
| 25 | - */ |
|
| 26 | - protected $stop = null; |
|
| 27 | - |
|
| 28 | - /** |
|
| 29 | - * @var int |
|
| 30 | - */ |
|
| 31 | - protected $totalActions = 0; |
|
| 32 | - |
|
| 33 | - /** |
|
| 34 | - * @var int |
|
| 35 | - */ |
|
| 36 | - protected $totalTime = 0; |
|
| 37 | - |
|
| 38 | - public function __construct() |
|
| 39 | - { |
|
| 40 | - $this->flow = []; |
|
| 41 | - $this->start = microtime(true); |
|
| 42 | - } |
|
| 43 | - |
|
| 44 | - /** |
|
| 45 | - * @return array |
|
| 46 | - */ |
|
| 47 | - public function getTotalTimeForHook(array $data) |
|
| 48 | - { |
|
| 49 | - $total = 0; |
|
| 50 | - foreach ($data['time'] as $time) { |
|
| 51 | - $total += ($time['stop'] - $time['start']) * 1000; |
|
| 52 | - } |
|
| 53 | - return $total; |
|
| 54 | - } |
|
| 55 | - |
|
| 56 | - /** |
|
| 57 | - * @return array |
|
| 58 | - */ |
|
| 59 | - public function addCallbacksForAction($action) |
|
| 60 | - { |
|
| 61 | - global $wp_filter; |
|
| 62 | - if (!array_key_exists($action, $this->flow)) { |
|
| 63 | - return; |
|
| 64 | - } |
|
| 65 | - $this->flow[$action]['callbacks_count'] = 0; |
|
| 66 | - foreach ($wp_filter[$action] as $priority => $callbacks) { |
|
| 67 | - if (!array_key_exists($priority, $this->flow[$action]['callbacks'])) { |
|
| 68 | - $this->flow[$action]['callbacks'][$priority] = []; |
|
| 69 | - } |
|
| 70 | - foreach ($callbacks as $callback) { |
|
| 71 | - if (is_array($callback['function']) && 2 == count($callback['function'])) { |
|
| 72 | - list($object, $method) = $callback['function']; |
|
| 73 | - if (is_object($object)) { |
|
| 74 | - $object = get_class($object); |
|
| 75 | - } |
|
| 76 | - $this->flow[$action]['callbacks'][$priority][] = sprintf('%s::%s', $object, $method); |
|
| 77 | - } elseif (is_object($callback['function'])) { |
|
| 78 | - $this->flow[$action]['callbacks'][$priority][] = get_class($callback['function']); |
|
| 79 | - } else { |
|
| 80 | - $this->flow[$action]['callbacks'][$priority][] = $callback['function']; |
|
| 81 | - } |
|
| 82 | - ++$this->flow[$action]['callbacks_count']; |
|
| 83 | - } |
|
| 84 | - } |
|
| 85 | - } |
|
| 86 | - |
|
| 87 | - /** |
|
| 88 | - * @return array |
|
| 89 | - */ |
|
| 90 | - public function getMeasure() |
|
| 91 | - { |
|
| 92 | - foreach ($this->flow as $action => $data) { |
|
| 93 | - $total = $this->getTotalTimeForHook($data); |
|
| 94 | - $this->flow[$action]['total'] = $total; |
|
| 95 | - $this->totalTime += $total; |
|
| 96 | - $this->totalActions += $data['count']; |
|
| 97 | - $this->addCallbacksForAction($action); |
|
| 98 | - } |
|
| 99 | - uasort($this->flow, [$this, 'sortByTime']); |
|
| 100 | - return $this->flow; |
|
| 101 | - } |
|
| 102 | - |
|
| 103 | - /** |
|
| 104 | - * @return int |
|
| 105 | - */ |
|
| 106 | - public function getTotalActions() |
|
| 107 | - { |
|
| 108 | - return $this->totalActions; |
|
| 109 | - } |
|
| 110 | - |
|
| 111 | - /** |
|
| 112 | - * @return int Microseconds |
|
| 113 | - */ |
|
| 114 | - public function getTotalTime() |
|
| 115 | - { |
|
| 116 | - return $this->totalTime; |
|
| 117 | - // $totalNoise = (count($this->timers) - 1) * $this->noise; |
|
| 118 | - // return $this->stop - $this->start - $totalNoise; |
|
| 119 | - } |
|
| 120 | - |
|
| 121 | - /** |
|
| 122 | - * @param string $name |
|
| 123 | - * @return void |
|
| 124 | - */ |
|
| 125 | - public function startTimer() |
|
| 126 | - { |
|
| 127 | - if (!isset($this->flow[current_filter()])) { |
|
| 128 | - $this->flow[current_filter()] = [ |
|
| 129 | - 'callbacks' => [], |
|
| 130 | - 'count' => 0, |
|
| 131 | - 'stack' => [], |
|
| 132 | - 'time' => [], |
|
| 133 | - ]; |
|
| 134 | - add_action(current_filter(), [$this, 'stopTimer'], 9000); |
|
| 135 | - } |
|
| 136 | - $count = ++$this->flow[current_filter()]['count']; |
|
| 137 | - array_push($this->flow[current_filter()]['stack'], ['start' => microtime(true)]); |
|
| 138 | - } |
|
| 139 | - |
|
| 140 | - /** |
|
| 141 | - * @param mixed $possibleFilter |
|
| 142 | - * @return mixed |
|
| 143 | - */ |
|
| 144 | - public function stopTimer($possibleFilter = null) |
|
| 145 | - { |
|
| 146 | - $time = array_pop($this->flow[current_filter()]['stack']); |
|
| 147 | - $time['stop'] = microtime(true); |
|
| 148 | - array_push($this->flow[current_filter()]['time'], $time); |
|
| 149 | - return $possibleFilter; |
|
| 150 | - } |
|
| 151 | - |
|
| 152 | - /** |
|
| 153 | - * @param array $a |
|
| 154 | - * @param array $b |
|
| 155 | - * @return int |
|
| 156 | - */ |
|
| 157 | - protected function sortByTime($a, $b) |
|
| 158 | - { |
|
| 159 | - if ($a['total'] == $b['total']) { |
|
| 160 | - return 0; |
|
| 161 | - } |
|
| 162 | - return ($a['total'] > $b['total']) ? -1 : 1; |
|
| 163 | - } |
|
| 7 | + /** |
|
| 8 | + * @var array |
|
| 9 | + */ |
|
| 10 | + protected $flow; |
|
| 11 | + |
|
| 12 | + /** |
|
| 13 | + * This is the time that WordPress takes to execute the all hook. |
|
| 14 | + * @var int |
|
| 15 | + */ |
|
| 16 | + protected $noise = 0; |
|
| 17 | + |
|
| 18 | + /** |
|
| 19 | + * @var int |
|
| 20 | + */ |
|
| 21 | + protected $start = null; |
|
| 22 | + |
|
| 23 | + /** |
|
| 24 | + * @var int |
|
| 25 | + */ |
|
| 26 | + protected $stop = null; |
|
| 27 | + |
|
| 28 | + /** |
|
| 29 | + * @var int |
|
| 30 | + */ |
|
| 31 | + protected $totalActions = 0; |
|
| 32 | + |
|
| 33 | + /** |
|
| 34 | + * @var int |
|
| 35 | + */ |
|
| 36 | + protected $totalTime = 0; |
|
| 37 | + |
|
| 38 | + public function __construct() |
|
| 39 | + { |
|
| 40 | + $this->flow = []; |
|
| 41 | + $this->start = microtime(true); |
|
| 42 | + } |
|
| 43 | + |
|
| 44 | + /** |
|
| 45 | + * @return array |
|
| 46 | + */ |
|
| 47 | + public function getTotalTimeForHook(array $data) |
|
| 48 | + { |
|
| 49 | + $total = 0; |
|
| 50 | + foreach ($data['time'] as $time) { |
|
| 51 | + $total += ($time['stop'] - $time['start']) * 1000; |
|
| 52 | + } |
|
| 53 | + return $total; |
|
| 54 | + } |
|
| 55 | + |
|
| 56 | + /** |
|
| 57 | + * @return array |
|
| 58 | + */ |
|
| 59 | + public function addCallbacksForAction($action) |
|
| 60 | + { |
|
| 61 | + global $wp_filter; |
|
| 62 | + if (!array_key_exists($action, $this->flow)) { |
|
| 63 | + return; |
|
| 64 | + } |
|
| 65 | + $this->flow[$action]['callbacks_count'] = 0; |
|
| 66 | + foreach ($wp_filter[$action] as $priority => $callbacks) { |
|
| 67 | + if (!array_key_exists($priority, $this->flow[$action]['callbacks'])) { |
|
| 68 | + $this->flow[$action]['callbacks'][$priority] = []; |
|
| 69 | + } |
|
| 70 | + foreach ($callbacks as $callback) { |
|
| 71 | + if (is_array($callback['function']) && 2 == count($callback['function'])) { |
|
| 72 | + list($object, $method) = $callback['function']; |
|
| 73 | + if (is_object($object)) { |
|
| 74 | + $object = get_class($object); |
|
| 75 | + } |
|
| 76 | + $this->flow[$action]['callbacks'][$priority][] = sprintf('%s::%s', $object, $method); |
|
| 77 | + } elseif (is_object($callback['function'])) { |
|
| 78 | + $this->flow[$action]['callbacks'][$priority][] = get_class($callback['function']); |
|
| 79 | + } else { |
|
| 80 | + $this->flow[$action]['callbacks'][$priority][] = $callback['function']; |
|
| 81 | + } |
|
| 82 | + ++$this->flow[$action]['callbacks_count']; |
|
| 83 | + } |
|
| 84 | + } |
|
| 85 | + } |
|
| 86 | + |
|
| 87 | + /** |
|
| 88 | + * @return array |
|
| 89 | + */ |
|
| 90 | + public function getMeasure() |
|
| 91 | + { |
|
| 92 | + foreach ($this->flow as $action => $data) { |
|
| 93 | + $total = $this->getTotalTimeForHook($data); |
|
| 94 | + $this->flow[$action]['total'] = $total; |
|
| 95 | + $this->totalTime += $total; |
|
| 96 | + $this->totalActions += $data['count']; |
|
| 97 | + $this->addCallbacksForAction($action); |
|
| 98 | + } |
|
| 99 | + uasort($this->flow, [$this, 'sortByTime']); |
|
| 100 | + return $this->flow; |
|
| 101 | + } |
|
| 102 | + |
|
| 103 | + /** |
|
| 104 | + * @return int |
|
| 105 | + */ |
|
| 106 | + public function getTotalActions() |
|
| 107 | + { |
|
| 108 | + return $this->totalActions; |
|
| 109 | + } |
|
| 110 | + |
|
| 111 | + /** |
|
| 112 | + * @return int Microseconds |
|
| 113 | + */ |
|
| 114 | + public function getTotalTime() |
|
| 115 | + { |
|
| 116 | + return $this->totalTime; |
|
| 117 | + // $totalNoise = (count($this->timers) - 1) * $this->noise; |
|
| 118 | + // return $this->stop - $this->start - $totalNoise; |
|
| 119 | + } |
|
| 120 | + |
|
| 121 | + /** |
|
| 122 | + * @param string $name |
|
| 123 | + * @return void |
|
| 124 | + */ |
|
| 125 | + public function startTimer() |
|
| 126 | + { |
|
| 127 | + if (!isset($this->flow[current_filter()])) { |
|
| 128 | + $this->flow[current_filter()] = [ |
|
| 129 | + 'callbacks' => [], |
|
| 130 | + 'count' => 0, |
|
| 131 | + 'stack' => [], |
|
| 132 | + 'time' => [], |
|
| 133 | + ]; |
|
| 134 | + add_action(current_filter(), [$this, 'stopTimer'], 9000); |
|
| 135 | + } |
|
| 136 | + $count = ++$this->flow[current_filter()]['count']; |
|
| 137 | + array_push($this->flow[current_filter()]['stack'], ['start' => microtime(true)]); |
|
| 138 | + } |
|
| 139 | + |
|
| 140 | + /** |
|
| 141 | + * @param mixed $possibleFilter |
|
| 142 | + * @return mixed |
|
| 143 | + */ |
|
| 144 | + public function stopTimer($possibleFilter = null) |
|
| 145 | + { |
|
| 146 | + $time = array_pop($this->flow[current_filter()]['stack']); |
|
| 147 | + $time['stop'] = microtime(true); |
|
| 148 | + array_push($this->flow[current_filter()]['time'], $time); |
|
| 149 | + return $possibleFilter; |
|
| 150 | + } |
|
| 151 | + |
|
| 152 | + /** |
|
| 153 | + * @param array $a |
|
| 154 | + * @param array $b |
|
| 155 | + * @return int |
|
| 156 | + */ |
|
| 157 | + protected function sortByTime($a, $b) |
|
| 158 | + { |
|
| 159 | + if ($a['total'] == $b['total']) { |
|
| 160 | + return 0; |
|
| 161 | + } |
|
| 162 | + return ($a['total'] > $b['total']) ? -1 : 1; |
|
| 163 | + } |
|
| 164 | 164 | } |
@@ -74,9 +74,11 @@ |
||
| 74 | 74 | $object = get_class($object); |
| 75 | 75 | } |
| 76 | 76 | $this->flow[$action]['callbacks'][$priority][] = sprintf('%s::%s', $object, $method); |
| 77 | - } elseif (is_object($callback['function'])) { |
|
| 77 | + } |
|
| 78 | + elseif (is_object($callback['function'])) { |
|
| 78 | 79 | $this->flow[$action]['callbacks'][$priority][] = get_class($callback['function']); |
| 79 | - } else { |
|
| 80 | + } |
|
| 81 | + else { |
|
| 80 | 82 | $this->flow[$action]['callbacks'][$priority][] = $callback['function']; |
| 81 | 83 | } |
| 82 | 84 | ++$this->flow[$action]['callbacks_count']; |