Passed
Push — master ( 28ff52...4eff68 )
by Paul
02:06
created

Controller::enqueueAssets()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 0
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
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
	 * @param array $scripts
43
	 * @return array
44
	 * @action gform_noconflict_scripts
45
	 */
46
	public function filterNoconflictScripts( $scripts )
47
	{
48
		$scripts[] = Application::ID;
49
		return $scripts;
50
	}
51
52
	/**
53
	 * @param array $styles
54
	 * @return array
55
	 * @action gform_noconflict_styles
56
	 */
57
	public function filterNoconflictStyles( $styles )
58
	{
59
		$styles[] = Application::ID;
60
		$styles[] = Application::ID.'-syntax';
61
		return $styles;
62
	}
63
64
	/**
65
	 * @return void
66
	 * @filter all
67
	 */
68
	public function initProfiler()
69
	{
70
		if( func_get_arg(0) != Application::DEBUG )return;
71
		$this->app->profiler->trace( func_get_arg(1) );
72
	}
73
74
	/**
75
	 * @return void
76
	 * @action plugins_loaded
77
	 */
78
	public function registerLanguages()
79
	{
80
		load_plugin_textdomain( Application::ID, false,
81
			plugin_basename( $this->app->path() ).Application::LANG
82
		);
83
	}
84
85
	/**
86
	 * @return void
87
	 * @action admin_footer
88
	 * @action wp_footer
89
	 */
90
	public function renderBar()
91
	{
92
		apply_filters( 'debug', 'Profiler Stopped' );
93
		$this->app->render( 'debug-bar', array(
94
			'blackbar' => $this->app,
95
			'errors' => $this->getErrors(),
96
			'errorsLabel' => $this->getErrorsLabel(),
97
			'profiler' => $this->app->profiler,
98
			'profilerLabel' => $this->getProfilerLabel(),
99
			'queries' => $this->getQueries(),
100
			'queriesLabel' => $this->getQueriesLabel(),
101
			'templates' => $this->getTemplates(),
102
		));
103
	}
104
105
	/**
106
	 * @param int $time
107
	 * @param int $decimals
108
	 * @return string
109
	 */
110
	protected function convertToMiliseconds( $time, $decimals = 2 )
111
	{
112
		return number_format( $time * 1000, $decimals );
113
	}
114
115
	/**
116
	 * @return array
117
	 */
118
	protected function getErrors()
119
	{
120
		$errors = array();
121
		foreach( $this->app->errors as $error ) {
122
			if( $error['errno'] == E_WARNING ) {
123
				$error['name'] = '<span class="glbb-error">'.$error['name'].'</span>';
124
			}
125
			if( $error['count'] > 1 ) {
126
				$error['name'] .= ' ('.$error['count'].')';
127
			}
128
			$errors[] = array(
129
				'name' => $error['name'],
130
				'message' => sprintf( __( '%s on line %s in file %s', 'blackbar' ),
131
					$error['message'],
132
					$error['line'],
133
					$error['file']
134
				),
135
			);
136
		}
137
		return $errors;
138
	}
139
140
	/**
141
	 * @return string
142
	 */
143
	protected function getErrorsLabel()
144
	{
145
		$warningCount = 0;
146
		foreach( $this->app->errors as $error ) {
147
			if( $error['errno'] == E_WARNING ) {
148
				$warningCount++;
149
			}
150
		}
151
		$errorCount = count( $this->app->errors );
152
		$warnings = $warningCount > 0
153
			? sprintf( ', %d!', $warningCount )
154
			: '';
155
		return sprintf( __( 'Errors (%s)', 'blackbar' ), $errorCount.$warnings );
156
	}
157
158
	/**
159
	 * @return array
160
	 */
161
	protected function getIncludedFiles()
162
	{
163
		$files = array_values( array_filter( get_included_files(), function( $file ) {
164
			$bool = strpos( $file, '/themes/' ) !== false
165
				&& strpos( $file, '/functions.php' ) === false;
166
			return (bool)apply_filters( 'blackbar/templates/file', $bool, $file );
167
		}));
168
		return array_map( function( $key, $value ) {
169
			$value = str_replace( trailingslashit( WP_CONTENT_DIR ), '', $value );
170
			return sprintf( '[%s] => %s', $key, $value );
171
		}, array_keys( $files ), $files );
172
	}
173
174
	/**
175
	 * @return string
176
	 */
177
	protected function getProfilerLabel()
178
	{
179
		$profilerTime = $this->convertToMiliseconds( $this->app->profiler->getTotalTime(), 0 );
180
		return sprintf( __( 'Profiler (%s ms)', 'blackbar' ), $profilerTime );
181
	}
182
183
	/**
184
	 * @return array
185
	 */
186
	protected function getQueries()
187
	{
188
		global $wpdb;
189
		$queries = array();
190
		$search = array(
191
			'AND', 'FROM', 'GROUP BY', 'INNER JOIN', 'LIMIT', 'ON DUPLICATE KEY UPDATE',
192
			'ORDER BY', 'SET', 'WHERE',
193
		);
194
		$replace = array_map( function( $value ) {
195
			return PHP_EOL.$value;
196
		}, $search );
197
		foreach( $wpdb->queries as $query ) {
198
			$miliseconds = number_format( round( $query[1] * 1000, 4 ), 4 );
199
			$sql = preg_replace( '/\s\s+/', ' ', trim( $query[0] ));
200
			$sql = str_replace( PHP_EOL, ' ', $sql );
201
			$sql = str_replace( $search, $replace, $sql );
202
			$queries[] = array(
203
				'ms' => $miliseconds,
204
				'sql' => $sql,
205
			);
206
		}
207
		return $queries;
208
	}
209
210
	/**
211
	 * @return string
212
	 */
213
	protected function getQueriesLabel()
214
	{
215
		if( !SAVEQUERIES ) {
216
			return __( 'SQL', 'blackbar' );
217
		}
218
		global $wpdb;
219
		$queryTime = 0;
220
		foreach( $wpdb->queries as $query ) {
221
			$queryTime += $query[1];
222
		}
223
		$queriesCount = '<span class="glbb-queries-count">'.count( $wpdb->queries ).'</span>';
224
		$queriesTime = '<span class="glbb-queries-time">'.$this->convertToMiliseconds( $queryTime ).'</span>';
225
		return sprintf( __( 'SQL (%s queries in %s ms)', 'blackbar' ), $queriesCount, $queriesTime );
226
	}
227
228
	/**
229
	 * @return void|string
230
	 */
231
	protected function getTemplates()
232
	{
233
		if( is_admin() )return;
234
		if( class_exists( '\GeminiLabs\Castor\Facades\Development' )) {
235
			ob_start();
236
			\GeminiLabs\Castor\Facades\Development::printTemplatePaths();
237
			return ob_get_clean();
238
		}
239
		return '<pre>'.implode( PHP_EOL, $this->getIncludedFiles() ).'</pre>';
240
	}
241
}
242