| 1 |
<?php |
| 2 |
|
| 3 |
namespace GeminiLabs\BlackBar; |
| 4 |
|
| 5 |
use GeminiLabs\BlackBar\Application; |
| 6 |
|
| 7 |
class Controller |
| 8 |
{ |
| 9 |
/** |
| 10 |
* @var Application |
| 11 |
*/ |
| 12 |
protected $app; |
| 13 |
|
| 14 |
public function __construct( Application $app ) |
| 15 |
{ |
| 16 |
$this->app = $app; |
| 17 |
} |
| 18 |
|
| 19 |
/** |
| 20 |
* @return void |
| 21 |
* @action admin_enqueue_scripts |
| 22 |
* @action wp_enqueue_scripts |
| 23 |
*/ |
| 24 |
public function enqueueAssets() |
| 25 |
{ |
| 26 |
wp_enqueue_script( Application::ID, $this->app->url( 'assets/main.js' )); |
| 27 |
wp_enqueue_style( Application::ID, $this->app->url( 'assets/main.css' ), array( 'dashicons' )); |
| 28 |
wp_enqueue_style( Application::ID.'-syntax', $this->app->url( 'assets/syntax.css' )); |
| 29 |
} |
| 30 |
|
| 31 |
/** |
| 32 |
* @param string $classes |
| 33 |
* @return string |
| 34 |
* @action admin_body_class |
| 35 |
*/ |
| 36 |
public function filterBodyClasses( $classes ) |
| 37 |
{ |
| 38 |
return trim( $classes.' '.Application::ID ); |
| 39 |
} |
| 40 |
|
| 41 |
/** |
| 42 |
* @return void |
| 43 |
* @filter all |
| 44 |
*/ |
| 45 |
public function initConsole() |
| 46 |
{ |
| 47 |
if( func_get_arg(0) != Application::CONSOLE_HOOK )return; |
| 48 |
$backtrace = debug_backtrace( DEBUG_BACKTRACE_IGNORE_ARGS, 4 ); |
| 49 |
$entry = array_pop( $backtrace ); |
| 50 |
$location = ''; |
| 51 |
if( array_key_exists( 'file', $entry )) { |
| 52 |
$path = explode( ABSPATH, $entry['file'] ); |
| 53 |
$location = sprintf( '[%s:%s] ', array_pop( $path ), $entry['line'] ); |
| 54 |
} |
| 55 |
$this->app->console->store( func_get_arg(1), $location ); |
| 56 |
} |
| 57 |
|
| 58 |
/** |
| 59 |
* @return void |
| 60 |
* @filter all |
| 61 |
*/ |
| 62 |
public function initProfiler() |
| 63 |
{ |
| 64 |
if( func_get_arg(0) != Application::PROFILER_HOOK )return; |
| 65 |
$this->app->profiler->trace( func_get_arg(1) ); |
| 66 |
} |
| 67 |
|
| 68 |
/** |
| 69 |
* @return void |
| 70 |
* @action plugins_loaded |
| 71 |
*/ |
| 72 |
public function registerLanguages() |
| 73 |
{ |
| 74 |
load_plugin_textdomain( Application::ID, false, |
| 75 |
plugin_basename( $this->app->path() ).Application::LANG |
| 76 |
); |
| 77 |
} |
| 78 |
|
| 79 |
/** |
| 80 |
* @return void |
| 81 |
* @action admin_footer |
| 82 |
* @action wp_footer |
| 83 |
*/ |
| 84 |
public function renderBar() |
| 85 |
{ |
| 86 |
apply_filters( 'debug', 'Profiler Stopped' ); |
| 87 |
$this->app->render( 'debug-bar', array( |
| 88 |
'blackbar' => $this->app, |
| 89 |
'consoleEntries' => $this->getConsoleEntries(), |
| 90 |
'consoleLabel' => $this->getConsoleLabel(), |
| 91 |
'profiler' => $this->app->profiler, |
| 92 |
'profilerLabel' => $this->getProfilerLabel(), |
| 93 |
'queries' => $this->getQueries(), |
| 94 |
'queriesLabel' => $this->getQueriesLabel(), |
| 95 |
'templates' => $this->getTemplates(), |
| 96 |
)); |
| 97 |
} |
| 98 |
|
| 99 |
/** |
| 100 |
* @param int $time |
| 101 |
* @param int $decimals |
| 102 |
* @return string |
| 103 |
*/ |
| 104 |
protected function convertToMiliseconds( $time, $decimals = 2 ) |
| 105 |
{ |
| 106 |
return number_format( $time * 1000, $decimals ); |
| 107 |
} |
| 108 |
|
| 109 |
/** |
| 110 |
* @return array |
| 111 |
*/ |
| 112 |
protected function getConsoleEntries() |
| 113 |
{ |
| 114 |
return array_merge( $this->getErrors(), $this->app->console->entries ); |
| 115 |
} |
| 116 |
|
| 117 |
/** |
| 118 |
* @return string |
| 119 |
*/ |
| 120 |
protected function getConsoleLabel() |
| 121 |
{ |
| 122 |
$class = ''; |
| 123 |
$entries = $this->getConsoleEntries(); |
| 124 |
$errorCount = 0; |
| 125 |
foreach( $entries as $entry ) { |
| 126 |
if( in_array( $entry['errno'], [E_NOTICE, E_STRICT, E_DEPRECATED] )) { |
| 127 |
$class = 'glbb-warning'; |
| 128 |
} |
| 129 |
if( in_array( $entry['errno'], [E_WARNING] )) { |
| 130 |
$errorCount++; |
| 131 |
} |
| 132 |
} |
| 133 |
$entryCount = count( $entries ); |
| 134 |
if( $errorCount > 0 ) { |
| 135 |
$class = 'glbb-error'; |
| 136 |
$entryCount .= sprintf( ', %d!', $errorCount ); |
| 137 |
} |
| 138 |
$label = sprintf( __( 'Console (%s)', 'blackbar' ), $entryCount ); |
| 139 |
return '<span class="'.$class.'">'.$label.'</span>'; |
| 140 |
} |
| 141 |
|
| 142 |
/** |
| 143 |
* @return array |
| 144 |
*/ |
| 145 |
protected function getErrors() |
| 146 |
{ |
| 147 |
$errors = array(); |
| 148 |
foreach( $this->app->errors as $error ) { |
| 149 |
$class = 'glbb-info'; |
| 150 |
if( in_array( $error['errno'], [E_NOTICE, E_STRICT, E_DEPRECATED] )) { |
| 151 |
$class = 'glbb-warning'; |
| 152 |
} |
| 153 |
if( $error['errno'] == E_WARNING ) { |
| 154 |
$class = 'glbb-error'; |
| 155 |
} |
| 156 |
if( $error['count'] > 1 ) { |
| 157 |
$error['name'] .= ' ('.$error['count'].')'; |
| 158 |
} |
| 159 |
$errors[] = array( |
| 160 |
'errno' => $error['errno'], |
| 161 |
'name' => '<span class="'.$class.'">'.$error['name'].'</span>', |
| 162 |
'message' => sprintf( __( '%s on line %s in file %s', 'blackbar' ), |
| 163 |
$error['message'], |
| 164 |
$error['line'], |
| 165 |
$error['file'] |
| 166 |
), |
| 167 |
); |
| 168 |
} |
| 169 |
return $errors; |
| 170 |
} |
| 171 |
|
| 172 |
/** |
| 173 |
* @return array |
| 174 |
*/ |
| 175 |
protected function getIncludedFiles() |
| 176 |
{ |
| 177 |
$files = array_values( array_filter( get_included_files(), function( $file ) { |
| 178 |
$bool = strpos( $file, '/themes/' ) !== false |
| 179 |
&& strpos( $file, '/functions.php' ) === false; |
| 180 |
return (bool)apply_filters( 'blackbar/templates/file', $bool, $file ); |
| 181 |
})); |
| 182 |
return array_map( function( $key, $value ) { |
| 183 |
$value = str_replace( trailingslashit( WP_CONTENT_DIR ), '', $value ); |
| 184 |
return sprintf( '[%s] => %s', $key, $value ); |
| 185 |
}, array_keys( $files ), $files ); |
| 186 |
} |
| 187 |
|
| 188 |
/** |
| 189 |
* @return string |
| 190 |
*/ |
| 191 |
protected function getProfilerLabel() |
| 192 |
{ |
| 193 |
$profilerTime = $this->convertToMiliseconds( $this->app->profiler->getTotalTime(), 0 ); |
| 194 |
return sprintf( __( 'Profiler (%s ms)', 'blackbar' ), $profilerTime ); |
| 195 |
} |
| 196 |
|
| 197 |
/** |
| 198 |
* @return array |
| 199 |
*/ |
| 200 |
protected function getQueries() |
| 201 |
{ |
| 202 |
global $wpdb; |
| 203 |
$queries = array(); |
| 204 |
$search = array( |
| 205 |
'AND', 'FROM', 'GROUP BY', 'INNER JOIN', 'LIMIT', 'ON DUPLICATE KEY UPDATE', |
| 206 |
'ORDER BY', 'SET', 'WHERE', |
| 207 |
); |
| 208 |
$replace = array_map( function( $value ) { |
| 209 |
return PHP_EOL.$value; |
| 210 |
}, $search ); |
| 211 |
foreach( $wpdb->queries as $query ) { |
| 212 |
$miliseconds = number_format( round( $query[1] * 1000, 4 ), 4 ); |
| 213 |
$sql = preg_replace( '/\s\s+/', ' ', trim( $query[0] )); |
| 214 |
$sql = str_replace( PHP_EOL, ' ', $sql ); |
| 215 |
$sql = str_replace( $search, $replace, $sql ); |
| 216 |
$queries[] = array( |
| 217 |
'ms' => $miliseconds, |
| 218 |
'sql' => $sql, |
| 219 |
); |
| 220 |
} |
| 221 |
return $queries; |
| 222 |
} |
| 223 |
|
| 224 |
/** |
| 225 |
* @return string |
| 226 |
*/ |
| 227 |
protected function getQueriesLabel() |
| 228 |
{ |
| 229 |
if( !SAVEQUERIES ) { |
| 230 |
return __( 'SQL', 'blackbar' ); |
| 231 |
} |
| 232 |
global $wpdb; |
| 233 |
$queryTime = 0; |
| 234 |
foreach( $wpdb->queries as $query ) { |
| 235 |
$queryTime += $query[1]; |
| 236 |
} |
| 237 |
$queriesCount = '<span class="glbb-queries-count">'.count( $wpdb->queries ).'</span>'; |
| 238 |
$queriesTime = '<span class="glbb-queries-time">'.$this->convertToMiliseconds( $queryTime ).'</span>'; |
| 239 |
return sprintf( __( 'SQL (%s queries | %s ms)', 'blackbar' ), $queriesCount, $queriesTime ); |
| 240 |
} |
| 241 |
|
| 242 |
/** |
| 243 |
* @return void|string |
| 244 |
*/ |
| 245 |
protected function getTemplates() |
| 246 |
{ |
| 247 |
if( is_admin() )return; |
| 248 |
if( class_exists( '\GeminiLabs\Castor\Facades\Development' )) { |
| 249 |
ob_start(); |
| 250 |
\GeminiLabs\Castor\Facades\Development::printTemplatePaths(); |
| 251 |
return ob_get_clean(); |
| 252 |
} |
| 253 |
return '<pre>'.implode( PHP_EOL, $this->getIncludedFiles() ).'</pre>'; |
| 254 |
} |
| 255 |
} |
| 256 |
|