-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdeveloper-discovery.php
465 lines (377 loc) · 14.1 KB
/
developer-discovery.php
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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
<?php
/*
Plugin Name: Developer Discovery
Plugin URI: http://themefoundation.com
Description: This plugin displays additional developer information on the front end.
Author: Alex Mansfield
Version: 0.1.0
Author URI: http://themefoundation.com
@see http://jquerymodal.com/
*/
class THMFDN_Developer_Discovery {
/**
* Class constructor
*
* Adds actions/filters to hooks.
*/
function __construct() {
add_action( 'wp_enqueue_scripts', array( $this, 'enqueue' ) );
add_action( 'wp_footer', array( $this, 'developer_discovery_div') );
add_action( 'thmfdn_particle_before', array( $this, 'thmfdn_discovery_particle_before') );
add_action( 'thmfdn_particle_after', array( $this, 'thmfdn_discovery_after') );
add_action( 'thmfdn_template_part_before', array( $this, 'thmfdn_discovery_template_part_before'), 10, 3 );
add_action( 'thmfdn_template_part_after', array( $this, 'thmfdn_discovery_after') );
add_action( 'thmfdn_extend_template_before', array( $this, 'thmfdn_discovery_extend_template_before'), 10, 1 );
add_action( 'thmfdn_comments_before', array( $this, 'thmfdn_discovery_comments_before') );
add_action( 'thmfdn_comments_after', array( $this, 'thmfdn_discovery_after') );
add_action( 'dynamic_sidebar_before', array( $this, 'sidebar_before') );
add_action( 'dynamic_sidebar_after', array( $this, 'thmfdn_discovery_after') );
}
/**
* Enqueues styles and scripts
*/
public function enqueue() {
wp_enqueue_style( 'jquery-modal', plugin_dir_url( __FILE__ ) . 'jquery.modal.css' );
wp_enqueue_style( 'developer-discovery', plugin_dir_url( __FILE__ ) . 'developer-discovery.css' );
wp_enqueue_script( 'jquery' );
wp_enqueue_script( 'jquery-modal', plugin_dir_url( __FILE__ ) . 'jquery.modal.js', array( 'jquery' ), false, true );
wp_enqueue_script( 'developer-discovery', plugin_dir_url( __FILE__ ) . 'developer-discovery.js', array( 'jquery' ), false, true );
}
/**
* Creates developer discover div
*
* The plugin populates this div with content as needed.
*/
public function developer_discovery_div() {
global $thmfdn_developer_discovery;
if ( empty( $thmfdn_developer_discovery ) ) {
$thmfdn_developer_discovery = array();
}
$details['path'] = $this->get_current_template();
$details['handle'] = basename( $details['path'] );
$split_path = explode( 'wp-content/', $details['path'] );
$details['table']['Path'] = end( $split_path );
array_unshift( $thmfdn_developer_discovery, $details );
$json_details = json_encode( $thmfdn_developer_discovery );
//echo wpautop( thmfdn_get_file_doc_block(thmfdn_get_current_template()));
?>
<div id="dd-data" data-discovery='<?php echo $json_details; ?>'></div>
<?php
}
/**
* Gets the path to the current template file, as determined by WordPress
*
* @see http://wordpress.stackexchange.com/questions/10537/get-name-of-the-current-template-file
*/
public function get_current_template() {
if( !empty( $GLOBALS['current_theme_template'] ) ) {
$template = $GLOBALS['current_theme_template'];
} else {
$template = false;
}
return $template;
}
/**
* Opens particle wrapper div
*/
public function thmfdn_discovery_particle_before( $id ) {
$details = $this->get_particle_details( $id );
$details_json = json_encode( $details );
$class = 'thmfdn-discovery dd-definition';
if ( !empty( $details['display'] ) && 'inline' == $details['display'] ) {
$class .= ' dd-inline';
}
echo '<div class="' . $class . '" data-discovery=\'' . $details_json . '\'>';
// echo '<a class="thmfdn-discovery-expand" href="#"></a>';
}
/**
* Opens template part wrapper div
*/
public function thmfdn_discovery_template_part_before( $slug, $name = '', $parent = false ) {
echo '<div class="thmfdn-discovery dd-definition" data-discovery=\'' . $this->get_template_part_details_json( $slug, $name, $parent ) . '\'>';
// echo '<a class="thmfdn-discovery-expand" href="#"></a>';
}
/**
* Opens sidebar wrapper div
*/
public function sidebar_before( $id ) {
global $wp_registered_sidebars;
// Gets sidebar details.
$details = array();
$details['title'] = $wp_registered_sidebars[$id]['name'];
// $details['description'] = 'This function calls a registered sidebar.';
// $details['path'] = locate_template( $template_name, false );
$details['table']['Name'] = $wp_registered_sidebars[$id]['name'];
$details['table']['Description'] = $wp_registered_sidebars[$id]['description'];
$details['table']['Type'] = 'WordPress function';
$details['table']['Docs'] = '<a href="https://developer.wordpress.org/reference/functions/dynamic_sidebar/">https://developer.wordpress.org/reference/functions/dynamic_sidebar/</a>';
// $path_array = explode( 'wp-content/', $details['path'] );
// $details['table']['Path'] = end( $path_array );
$details['handle'] = 'dynamic_sidebar()';
// echo '<pre>';
// print_r($wp_registered_sidebars[$id]);
// echo '</pre>';
// $details = $this->get_particle_details( $id );
$details_json = json_encode( $details );
$class = 'thmfdn-discovery dd-definition';
// if ( !empty( $details['display'] ) && 'inline' == $details['display'] ) {
// $class .= ' dd-inline';
// }
echo '<div class="' . $class . '" data-discovery=\'' . $details_json . '\'>';
// echo '<a class="thmfdn-discovery-expand" href="#"></a>';
}
public function thmfdn_discovery_extend_template_before( $template_name ) {
global $thmfdn_developer_discovery;
// Gets template details.
$details = array();
$details['title'] = 'Template File';
$details['path'] = locate_template( $template_name, false );
$details['table']['Type'] = 'Template';
$path_array = explode( 'wp-content/', $details['path'] );
$details['table']['Path'] = end( $path_array );
$details['handle'] = basename( $template_name );
$thmfdn_developer_discovery[] = $details;
}
/**
* Opens comments wrapper div
*/
public function thmfdn_discovery_comments_before() {
$discovery_details = array(
'path' => 'comments.php',
'handle' => 'comments.php',
'description' => 'This is the comments template. WordPress loads this template using the comments_template() function.',
'table' => array(
'Docs' => '<a href="https://developer.wordpress.org/reference/functions/comments_template/">https://developer.wordpress.org/reference/functions/comments_template/</a>',
),
);
echo '<div class="thmfdn-discovery dd-definition" data-discovery=\'' . json_encode( $discovery_details ) . '\'>';
// echo '<a class="thmfdn-discovery-expand" href="#"></a>';
}
/**
* Closes wrapper div
*/
public function thmfdn_discovery_after() {
echo '</div>';
}
/**
* Gets details about a particle
*/
public function get_particle_details( $id ) {
global $thmfdn_particles;
$defaults = array(
'id' => '',
'name' => '',
'description' => __( '', 'thmfdn_textdomain' ),
'reflection' => '',
'display' => '',
);
$particle = wp_parse_args( $thmfdn_particles->particles[$id]->particle, $defaults );
$particle['path'] = $particle['reflection']->getFileName();
$details['handle'] = basename( $particle['path'] );
$details['display'] = $particle['display'];
// Sets up args capture.
$arg_string = '';
$arg_first = true;
// Loops through args.
if ( !empty( $particle['args'] ) && is_array( $particle['args'] ) ) {
foreach ( $particle['args'] as $arg ) {
if ( $arg_first ) {
$arg_string .= '$' . $arg;
$arg_first = false;
} else {
$arg_string .= ', $' . $arg;
}
}
}
// Gets additional particle details
$details['table']['Type'] = 'Particle';
$details['table']['Title'] = $particle['name'];
$details['table']['Description'] = $particle['description'];
$path_array = explode( 'wp-content/', $particle['path'] );
$details['table']['Path'] = end( $path_array );
// $details['table']['Function'] = $particle['function'] . '( ' . $arg_string . ' )';
// $details['table']['Line #'] = $reflection->getStartLine();
return $details;
}
/**
* Gets details about a template part
*/
public function get_template_part_details_json( $slug, $name = '', $parent = false ) {
// Gets template part details.
$details = array();
$details['title'] = 'Template Part';
$details['table']['Type'] = 'Template Part';
$details['table']['Path'] = ( empty( $name ) ) ? $slug . '.php' : $slug . '-' . $name . '.php';
$details['table']['Slug'] = $slug;
$details['table']['Name'] = $name;
$details['table']['Parent'] = $parent;
$details['handle'] = basename( $details['table']['Path'] );
return json_encode( $details );
}
}
/**
* Initializes the plugin class if conditions are met
*/
function thmfdn_developer_discovery_init() {
if ( !is_admin() ) {
if ( current_user_can( 'manage_options' ) ) {
if ( 'on' == get_user_option( 'developer_discovery_status' ) ) {
$thmfdn_developer_discovery = new THMFDN_Developer_Discovery;
}
}
}
}
add_action( 'init', 'thmfdn_developer_discovery_init' );
/**
* Stores the path to the current template file globally for later use
*/
function thmfdn_store_current_template( $template ){
$GLOBALS['current_theme_template'] = $template;
return $template;
}
add_filter( 'template_include', 'thmfdn_store_current_template', 1000 );
/**
* Adds discovery toggle to WP admin bar
*/
function thmfdn_developer_discovery_toggle( $wp_admin_bar ) {
if ( !is_admin() ) {
global $wp;
$current_discovery_status = get_user_option( 'developer_discovery_status' );
if ( empty( $current_discovery_status ) || 'off' == $current_discovery_status ) {
$toggled_discovery_status = 'on';
$button_title = 'Turn Discovery On';
} else {
$toggled_discovery_status = 'off';
$button_title = 'Turn Discovery Off';
}
if ( $_SERVER['QUERY_STRING'] ) {
$prefix = '?';
$url = home_url( $wp->request ) . '/';
$query_args_array = array();
parse_str( $_SERVER['QUERY_STRING'], $query_args_array );
foreach ( $query_args_array as $key => $value ) {
if ( $key != 'developer-discovery-status' ) {
$url .= $prefix;
$url .= $key . '=' . $value;
$prefix = '&';
}
}
$url .= $prefix . 'developer-discovery-status=' . $toggled_discovery_status;
} else {
$url .= home_url( $wp->request ) . '?developer-discovery-status=' . $toggled_discovery_status;
}
$args = array(
'id' => 'dd-toggle',
'title' => $button_title,
'href' => $url,
'parent' => 'top-secondary',
'meta' => array(
'class' => 'dd-display-toggle'
)
);
$wp_admin_bar->add_node($args);
}
}
add_action( 'admin_bar_menu', 'thmfdn_developer_discovery_toggle', 50 );
/**
* Updates saved discovery toggle state when changed by user
*/
function thmfdn_developer_discovery_update_toggle_state() {
if ( !empty( $_GET['developer-discovery-status'] ) ) {
if ( 'on' == $_GET['developer-discovery-status'] ) {
update_user_option( get_current_user_id(), 'developer_discovery_status', 'on' );
}
if ( 'off' == $_GET['developer-discovery-status'] ) {
update_user_option( get_current_user_id(), 'developer_discovery_status', 'off' );
}
}
}
add_action( 'init', 'thmfdn_developer_discovery_update_toggle_state', 1);
// $thmfdn_developer_discovery_count = 0;
// function thmfdn_discovery_template_part_bottom() {
// if ( current_user_can( 'manage_options' ) ) {
// echo '</div>';
// }
// }
// add_action( 'thmfdn_template_part_bottom', 'thmfdn_discovery_template_part_bottom' );
// function thmfdn_get_get_particle_details_json( $id ) {
// global $thmfdn_particles;
// $details = array();
// // Get file path;
// $reflection = new ReflectionFunction($thmfdn_particles->particles[$id]['function']);
// $full_path = $reflection->getFileName();
// $split_path = explode( 'wp-content/', $full_path );
// $useful_path = end( $split_path );
// $arg_string = '';
// $arg_first = true;
// foreach ( $thmfdn_particles->particles[$id]['args'] as $arg ) {
// if ( $arg_first ) {
// $arg_string .= '$' . $arg;
// $arg_first = false;
// } else {
// $arg_string .= ', $' . $arg;
// }
// }
// // echo '&&' . $thmfdn_particles->particles[$id]['title'] . '&&';
// $details['title'] = $thmfdn_particles->particles[$id]['title'];
// $details['description'] = $thmfdn_particles->particles[$id]['description'];
// $details['path'] = $useful_path;
// $json_details = json_encode( $details );
// // echo '&&' . $json_details . '&&';
// return $json_details;
// }
// function thmfdn_get_discovery_particle_details( $id ) {
// global $thmfdn_particles;
// // print_r(debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS));
// // $e = new \Exception;
// // var_dump($e->getTraceAsString());
// //http://php.net/manual/en/exception.gettrace.php
// // $bt = debug_backtrace();
// // print_r($bt);
// // $caller = array_shift($bt);
// // echo $bt[0]['file'];
// // wp-content/
// $reflection = new ReflectionFunction($thmfdn_particles->particles[$id]['function']);
// $full_path = $reflection->getFileName();
// $split_path = explode( 'wp-content/', $full_path );
// $useful_path = end( $split_path );
// $arg_string = '';
// $arg_first = true;
// foreach ( $thmfdn_particles->particles[$id]['args'] as $arg ) {
// if ( $arg_first ) {
// $arg_string .= '$' . $arg;
// $arg_first = false;
// } else {
// $arg_string .= ', $' . $arg;
// }
// }
// // echo '<pre>';
// // print_r($thmfdn_particles->particles[$id]);
// // print_r(debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS));
// // $e = new \Exception;
// // var_dump($e->getTraceAsString());
// //http://php.net/manual/en/exception.gettrace.php
// // $bt = debug_backtrace();
// // print_r($bt);
// // $caller = array_shift($bt);
// // echo $bt[0]['file'];
// // wp-content/
// // $reflection = new ReflectionFunction($thmfdn_particles->particles[$id]['function']);
// // echo $reflection->getFileName() . ':' . $reflection->getStartLine();
// // echo '</pre>';
// }
/**
* Gets the first docblock in a file
*
* @see http://stackoverflow.com/questions/11504541/get-comments-in-a-php-file
*/
// function thmfdn_get_file_doc_block( $file_path )
// {
// $docComments = array_filter(
// token_get_all( file_get_contents( $file_path ) ), function($entry) {
// return $entry[0] == T_DOC_COMMENT;
// }
// );
// $fileDocComment = array_shift( $docComments );
// return $fileDocComment[1];
// }