Console.php
5.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
<?php
namespace SearchWP\Debug\Console;
use SearchWP\Settings;
use SearchWP\Utils;
/**
* Console class manages SearchWP Debugging Console functionality.
*
* @since 4.2.9
*/
class Console {
/**
* Init.
*
* @since 4.2.9
*/
public static function init() {
if ( self::is_enabled() ) {
self::hooks();
}
}
/**
* Hooks.
*
* @since 4.2.9
*/
public static function hooks() {
add_action( 'wp_before_admin_bar_render', [ __CLASS__, 'add_admin_bar_menu' ] );
add_action( 'wp_enqueue_scripts', [ __CLASS__, 'assets' ] );
add_action( 'wp_footer', [ __CLASS__, 'render' ], 999 );
}
/**
* Check if current user can access the Console.
*
* @since 4.2.9
*/
public static function is_enabled() {
if ( ! apply_filters( 'searchwp\debug', Settings::get( 'debug', 'boolean' ) ) ) {
return false;
}
if ( ! apply_filters( 'searchwp\debug\console', true ) ) {
return false;
}
if ( ! current_user_can( Settings::get_capability() ) ) {
return false;
}
return true;
}
/**
* Add Debug to the SearchWP admin bar menu.
*
* @since 4.2.9
*/
public static function add_admin_bar_menu() {
global $wp_admin_bar;
if ( is_admin() ) {
return;
}
if ( ! is_admin_bar_showing() || ! apply_filters( 'searchwp\admin_bar', current_user_can( Settings::get_capability() ) ) ) {
return;
}
$wp_admin_bar->add_menu(
[
'id' => Utils::$slug . '-debug',
'title' => __( 'SearchWP Debug', 'searchwp' ),
'href' => '#debug',
]
);
}
/**
* Register assets.
*
* @since 4.2.9
*/
public static function assets() {
$handle = Utils::$slug . '_debug_console';
wp_enqueue_style( $handle, SEARCHWP_PLUGIN_URL . 'assets/styles/debug-console.css', [], SEARCHWP_VERSION );
wp_enqueue_script( $handle, SEARCHWP_PLUGIN_URL . 'assets/js/debug-console.js', [ 'jquery' ], SEARCHWP_VERSION );
}
/**
* Render the console.
*
* @since 4.2.9
*/
public static function render() {
?>
<div id="searchwp-debug-console-main">
<?php self::print_header_html(); ?>
<?php self::print_panels_html(); ?>
</div>
<?php
}
/**
* Print console header markup.
*
* @since 4.2.9
*/
private static function print_header_html() {
?>
<div id="searchwp-console-header">
<h1 id="searchwp-console-header-title">
<?php esc_html_e( 'SearchWP Console', 'searchwp' ); ?>
</h1>
<button class="searchwp-console-header-button searchwp-console-close" aria-label="<?php esc_html_e( 'Close Console', 'searchwp' ); ?>">
<span class="searchwp-console-icon" aria-hidden="true">
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="20" height="20" viewBox="0 0 20 20">
<path d="M14.95 6.46l-3.54 3.54 3.54 3.54-1.41 1.41-3.54-3.53-3.53 3.53-1.42-1.42 3.53-3.53-3.53-3.53 1.42-1.42 3.53 3.53 3.54-3.53z"></path>
</svg>
</span>
</button>
</div>
<?php
}
/**
* Print console panels markup.
*
* @since 4.2.9
*/
private static function print_panels_html() {
$panels = Panels::get_panels();
$nav_html = '';
$content_html = '';
foreach ( $panels as $panel ) {
$nav_html .= self::get_panel_nav_html( $panel );
$content_html .= self::get_panel_content_html( $panel );
}
?>
<div id="searchwp-console-panels">
<nav id="searchwp-console-panels-nav">
<ul role="tablist">
<?php echo wp_kses_post( $nav_html ); ?>
</ul>
</nav>
<div id="searchwp-console-panels-content">
<?php echo wp_kses_post( $content_html ); ?>
</div>
</div>
<?php
}
/**
* Get console panel nav markup.
*
* @param array $panel Panel data.
*
* @since 4.2.9
*/
private static function get_panel_nav_html( $panel ) {
ob_start();
?>
<li role="presentation">
<button role="tab" data-swp-href="#swp-<?php echo esc_attr( $panel['id'] ); ?>" data-swp-slug="<?php echo esc_attr( empty( $panel['slug'] ) ? $panel['id'] : $panel['slug'] ); ?>">
<?php echo esc_html( $panel['title'] ); ?>
</button>
<?php if ( isset( $panel['sub_panels'] ) ) : ?>
<ul role="presentation">
<?php foreach ( $panel['sub_panels'] as $sub_panel ) : ?>
<li role="presentation">
<button role="tab" data-swp-href="#swp-<?php echo esc_attr( $sub_panel['id'] ); ?>" data-swp-slug="<?php echo esc_attr( empty( $sub_panel['slug'] ) ? $sub_panel['id'] : $sub_panel['slug'] ); ?>">
<?php echo esc_html( $sub_panel['title'] ); ?>
</button>
</li>
<?php endforeach; ?>
</ul>
<?php endif; ?>
</li>
<?php
return ob_get_clean();
}
/**
* Get console panel content markup.
*
* @param array $panel Panel data.
*
* @since 4.2.9
*/
private static function get_panel_content_html( $panel ) {
ob_start();
?>
<div class="searchwp-panel-content" id="swp-<?php echo esc_attr( $panel['id'] ); ?>" role="tabpanel" tabindex="-1">
<section>
<pre><?php echo wp_kses_post( $panel['content'] ); ?></pre>
</section>
</div>
<?php
if ( isset( $panel['sub_panels'] ) ) {
foreach ( $panel['sub_panels'] as $sub_panel ) {
?>
<div class="searchwp-panel-content" id="swp-<?php echo esc_attr( $sub_panel['id'] ); ?>" role="tabpanel" tabindex="-1">
<section>
<pre><?php echo wp_kses_post( $sub_panel['content'] ); ?></pre>
</section>
</div>
<?php
}
}
return ob_get_clean();
}
}