-
Notifications
You must be signed in to change notification settings - Fork 0
/
reveal-template.php
578 lines (512 loc) · 18 KB
/
reveal-template.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
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
<?php
/**
* Plugin Name: Reveal Template
* Version: 3.7
* Plugin URI: https://coffee2code.com/wp-plugins/reveal-template/
* Author: Scott Reilly
* Author URI: https://coffee2code.com/
* License: GPLv2 or later
* License URI: https://www.gnu.org/licenses/gpl-2.0.html
* Text Domain: reveal-template
* Description: Reveal the theme template file used to render the displayed page, via the footer, widget, shortcode, and/or template tag.
*
* Compatible with WordPress 4.9+ through 5.8+.
*
* =>> Read the accompanying readme.txt file for instructions and documentation.
* =>> Also, visit the plugin's homepage for additional information and updates.
* =>> Or visit: https://wordpress.org/plugins/reveal-template/
*
* @package Reveal_Template
* @author Scott Reilly
* @version 3.7
*/
/*
Copyright (c) 2008-2021 by Scott Reilly (aka coffee2code)
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
defined( 'ABSPATH' ) or die();
if ( ! class_exists( 'c2c_RevealTemplate' ) ) :
require_once( dirname( __FILE__ ) . DIRECTORY_SEPARATOR . 'c2c-plugin.php' );
require_once( dirname( __FILE__ ) . DIRECTORY_SEPARATOR . 'reveal-template.widget.php' );
final class c2c_RevealTemplate extends c2c_Plugin_064 {
/**
* Name of plugin's setting.
*
* @since 3.4
* @var string
*/
const SETTING_NAME = 'c2c_reveal_template';
/**
* The one true instance.
*
* @var c2c_RevealTemplate
* @since 3.0
*/
private static $instance;
/**
* The default template path type.
*
* @var string
* @since 3.0
*/
private $default_template_path_type = 'theme-relative';
/**
* The shortcode name.
*
* @var string
* @since 3.0
*/
private $shortcode = 'revealtemplate';
/**
* The template being used.
*
* @var string
*/
private $template = '';
/**
* The memoized template path types array.
*
* @var array
* @since 3.0
*/
private static $template_path_types;
/**
* Get singleton instance.
*
* @since 3.0
*/
public static function get_instance() {
if ( ! isset( self::$instance ) ) {
self::$instance = new self();
}
return self::$instance;
}
/**
* Constructor.
*/
protected function __construct() {
parent::__construct( '3.7', 'reveal-template', 'c2c', __FILE__, array( 'settings_page' => 'themes' ) );
register_activation_hook( __FILE__, array( __CLASS__, 'activation' ) );
return self::$instance = $this;
}
/**
* Handles activation tasks, such as registering the uninstall hook.
*
* @since 2.1
*/
public static function activation() {
register_uninstall_hook( __FILE__, array( __CLASS__, 'uninstall' ) );
}
/**
* Handles uninstallation tasks, such as deleting plugin options.
*
* @since 2.1
*/
public static function uninstall() {
delete_option( self::SETTING_NAME );
}
/**
* Returns translated strings used by c2c_Plugin parent class.
*
* @since 3.6
*
* @param string $string Optional. The string whose translation should be
* returned, or an empty string to return all strings.
* Default ''.
* @return string|string[] The translated string, or if a string was provided
* but a translation was not found then the original
* string, or an array of all strings if $string is ''.
*/
public function get_c2c_string( $string = '' ) {
$strings = array(
'A value is required for: "%s"'
/* translators: %s: Label for setting. */
=> __( 'A value is required for: "%s"', 'reveal-template' ),
'Click for more help on this plugin'
=> __( 'Click for more help on this plugin', 'reveal-template' ),
' (especially check out the "Other Notes" tab, if present)'
=> __( ' (especially check out the "Other Notes" tab, if present)', 'reveal-template' ),
'Coffee fuels my coding.'
=> __( 'Coffee fuels my coding.', 'reveal-template' ),
'Did you find this plugin useful?'
=> __( 'Did you find this plugin useful?', 'reveal-template' ),
'Donate'
=> __( 'Donate', 'reveal-template' ),
'Expected integer value for: %s'
=> __( 'Expected integer value for: %s', 'reveal-template' ),
'Invalid file specified for C2C_Plugin: %s'
/* translators: %s: Path to the plugin file. */
=> __( 'Invalid file specified for C2C_Plugin: %s', 'reveal-template' ),
'More information about %1$s %2$s'
/* translators: 1: plugin name 2: plugin version */
=> __( 'More information about %1$s %2$s', 'reveal-template' ),
'More Help'
=> __( 'More Help', 'reveal-template' ),
'More Plugin Help'
=> __( 'More Plugin Help', 'reveal-template' ),
'Please consider a donation'
=> __( 'Please consider a donation', 'reveal-template' ),
'Reset Settings'
=> __( 'Reset Settings', 'reveal-template' ),
'Save Changes'
=> __( 'Save Changes', 'reveal-template' ),
'See the "Help" link to the top-right of the page for more help.'
=> __( 'See the "Help" link to the top-right of the page for more help.', 'reveal-template' ),
'Settings'
=> __( 'Settings', 'reveal-template' ),
'Settings reset.'
=> __( 'Settings reset.', 'reveal-template' ),
'Something went wrong.'
=> __( 'Something went wrong.', 'reveal-template' ),
'The plugin author homepage.'
=> __( 'The plugin author homepage.', 'reveal-template' ),
"The plugin configuration option '%s' must be supplied."
/* translators: %s: The setting configuration key name. */
=>__( "The plugin configuration option '%s' must be supplied.", 'reveal-template' ),
'This plugin brought to you by %s.'
/* translators: %s: Link to plugin author's homepage. */
=> __( 'This plugin brought to you by %s.', 'reveal-template' ),
);
if ( ! $string ) {
return array_values( $strings );
}
return ! empty( $strings[ $string ] ) ? $strings[ $string ] : $string;
}
/**
* Initializes the plugin's configuration and localizable text variables.
*/
public function load_config() {
$this->name = __( 'Reveal Template', 'reveal-template' );
$this->menu_name = $this->name;
$this->config = array(
'display_in_footer' => array(
'input' => 'checkbox',
'default' => true,
'label' => __( 'Reveal in footer?', 'reveal-template' ),
'help' => __( 'To be precise, this displays where <code><?php wp_footer(); ?></code> is called. Refer to the documentation for additional ways to display the template (e.g. widget, shortcode, template tag).', 'reveal-template' ),
),
'display_in_admin_bar' => array(
'input' => 'checkbox',
'default' => true,
'label' => __( 'Reveal in admin bar?', 'reveal-template' ),
'help' => __( 'Adds a "Reveal Template" admin bar entry that only appears on the front-end of the site.', 'reveal-template' ),
),
'format' => array(
'input' => 'long_text',
'default' => __( '<p>Rendered template: %template%</p>', 'reveal-template' ),
'label' => __( 'Output format', 'reveal-template' ),
'required' => true,
'help' => __( 'Only used for the footer display. Use %template% to indicate where the template name should go.', 'reveal-template' ),
),
'template_path' => array(
'input' => 'select',
'datatype' => 'hash',
'default' => $this->get_default_template_path_type(),
'label' => __( 'Template path', 'reveal-template' ),
'options' => self::get_template_path_types(),
'help' => __( 'How much of the template path do you want reported? Applies directly to admin bar and footer displays, and is the default for the template tag usage (though can be overridden via an argument to <code>c2c_reveal_template()</code>)', 'reveal-template' ),
),
);
}
/**
* Override the plugin framework's register_filters() to actually register
* actions against filters.
*
* @link https://developer.wordpress.org/reference/functions/get_query_template/
*/
public function register_filters() {
$options = $this->get_options();
$templates = array(
'404',
'archive',
'attachment',
'author',
'category',
'date',
'embed',
'frontpage',
'home',
'index',
'page',
'paged',
'privacypolicy',
'search',
'single',
'singular',
'tag',
'taxonomy',
);
foreach ( $templates as $template ) {
add_filter( $template . '_template', array( $this, 'template_handler' ) );
}
if ( $options['display_in_footer'] ) {
add_action( 'wp_footer', array( $this, 'reveal_in_footer' ) );
}
if ( $this->can_show_in_admin_bar() ) {
add_action( 'wp_before_admin_bar_render', array( $this, 'output_admin_bar_styles' ) );
add_action( 'admin_bar_menu', array( $this, 'add_to_admin_bar' ), 100 );
}
add_shortcode( $this->shortcode, array( $this, 'shortcode' ) );
}
/**
* Outputs the text above the setting form.
*
* @param string $localized_heading_text Optional. Localized page heading text.
*/
public function options_page_description( $localized_heading_text = '' ) {
$options = $this->get_options();
parent::options_page_description( __( 'Reveal Template Settings', 'reveal-template' ) );
?>
<p>
<?php _e( 'Reveal the theme template used to render the displayed page. By default this appears in the admin bar and the site\'s footer, but only for logged in users with the "update_themes" capability (such as an admin).', 'reveal-template' ); ?>
</p>
<p>
<?php printf(
__( 'Also note that the plugin provides a "Reveal Template" <a href="%s">widget</a> that can be used to reveal the current template.', 'reveal-template' ),
esc_url( admin_url( 'widgets.php' ) )
); ?>
</p>
<p>
<?php printf(
__( 'Please refer to this plugin\'s <a href="%s" title="readme">readme.txt</a> file for documentation and examples.', 'reveal-template' ),
esc_url( $this->readme_url() )
); ?>
</p>
<?php
}
/**
* Stores the name of the template being rendered.
*
* @param string $template The template name.
* @return string The unmodified template name.
*/
public function template_handler( $template ) {
$this->template = $template;
return $template;
}
/**
* Returns types of, and descriptions for, the valid template path types.
*
* @since 3.0
*
* @return array Keys are the template path types, values are the translated descriptions
*/
public function get_template_path_types() {
if ( ! self::$template_path_types ) {
self::$template_path_types = array(
'absolute' => __( 'Absolute path, e.g. /usr/local/www/yoursite/wp-content/themes/yourtheme/single.php', 'reveal-template' ),
'relative' => __( 'Relative path, e.g. wp-content/themes/yourtheme/single.php', 'reveal-template' ),
'theme-relative' => __( 'Path relative to themes directory, e.g. yourtheme/single.php', 'reveal-template' ),
'filename' => __( 'Filename, e.g. single.php', 'reveal-template' ),
);
}
return self::$template_path_types;
}
/**
* Gets the default template path type.
*
* @since 3.0
*
* @return string
*/
public function get_default_template_path_type() {
return $this->default_template_path_type;
}
/**
* Determines if the current user can be shown the template name/path.
*
* @since 3.0
*/
public function reveal_to_current_user() {
return current_user_can( 'update_themes' );
}
/**
* Handles the shortcode.
*
* @since 3.0
*
* @param array $atts The shortcode attributes parsed into an array.
* @param string $content The content between opening and closing shortcode tags.
* @return string
*/
public function shortcode( $atts, $content = null ) {
$defaults = array(
'type' => $this->get_default_template_path_type(),
'admin' => '1',
);
$a = shortcode_atts( $defaults, $atts );
// Validate attributes
if ( ! in_array( $a['type'], array_keys( $this->get_template_path_types() ) ) ) {
$a['type'] = $this->get_default_template_path_type();
}
$args = array(
'admin_only' => ( '0' === $a['admin'] ? false : true ),
'echo' => false,
'return' => false,
);
return $this->reveal( $a['type'], $args );
}
/**
* Invokes the reveal intended to be shown in the site's footer.
*
* @since 3.0
*/
public function reveal_in_footer() {
$options = $this->get_options();
return $this->reveal( $options['template_path'], array( 'format_from_settings' => true ) );
}
/**
* Determines if the admin bar entry should be shown.
*
* @since 3.5
*
* @return bool True if the admin bar entry can be shown, else false.
*/
public function can_show_in_admin_bar() {
// Don't show if in admin or is admin bar isn't even showing.
if ( is_admin() || ! is_admin_bar_showing() ) {
return false;
}
$options = $this->get_options();
// Is feature enabled via settings and user is permitted to see it?
return $options['display_in_admin_bar'] && $this->reveal_to_current_user();
}
/**
* Outputs CSS for the admin bar menu item.
*
* @since 3.5
*/
public function output_admin_bar_styles() {
echo '<style>#wpadminbar #wp-admin-bar-reveal-template .ab-icon::before { content: "\f100"; top: 2px; } </style>' . "\n";
}
/**
* Adds an admin menu bar node that reveals the template.
*
* @since 3.5
*
* @param WP_Admin_Bar $wp_admin_bar WP_Admin_Bar instance.
*/
public function add_to_admin_bar( $wp_admin_bar ) {
$wp_admin_bar->add_node(
array(
'id' => 'reveal-template',
'parent' => 'top-secondary',
'title' => '<span class="ab-icon"></span><span class="ab-label">' . __( 'Reveal Template', 'reveal-template' ) . '</span>',
'meta' => array(
'tabindex' => 0,
),
)
);
$options = $this->get_options();
$path = $this->reveal( $options['template_path'], array( 'echo' => false, 'format_from_settings' => false ) );
$wp_admin_bar->add_node(
array(
'parent' => 'reveal-template',
'id' => 'revealed-template',
'title' => $path,
)
);
}
/**
* Formats for output the template path info for the currently rendered template.
*
* @param string $template_path_type The style of the template's path for return. Accepts: 'absolute', 'relative',
* 'theme-relative', 'filename'
* @param array $args {
* Optional. Additional arguments.
*
* @type bool $admin_only Only show for an admin? Default is true.
* @type bool $echo Echo the output? Default is true.
* @type string $format Output string format. Uses '%template%' as placeholder for template path.
* @type bool $format_from_settings Use format string specified via the plugin's settings page? Default false.
* @type bool $return Return the value regardless of the admin_only value and check? Default true.
* }
* @return string The path info for the currently rendered template, unless $args['return'] is false AND user
* wouldn't be shown the output
*/
public function reveal( $template_path_type, $args = array() ) {
$template = $this->template;
if ( ! $template ) {
return '';
}
$defaults = array(
'admin_only' => true,
'echo' => true,
'format' => '',
'format_from_settings' => false,
'return' => true,
);
$args = wp_parse_args( $args, $defaults );
$return = $args['return'];
// Format the template path string based on type.
switch ( $template_path_type ) {
case 'absolute':
// Do nothing; already have the absolute path
break;
case 'filename':
$template = basename( $template );
break;
case 'relative':
$template = str_replace( ABSPATH, '', $template );
break;
case 'theme-relative':
default:
$template = basename( dirname( $template ) ) . '/' . basename( $template );
break;
}
// Check if current user can be shown the template.
$is_allowed = ( false === $args['admin_only'] || $this->reveal_to_current_user() );
if ( $return || $is_allowed ) {
// Format the template path string according to format string.
if ( $args['format_from_settings'] ) {
$options = $this->get_options();
$format = $options['format'];
} else {
$format = $args['format'];
}
$display = $format ? str_replace( '%template%', $template, $format ) : $template;
if ( $is_allowed && $display && $args['echo'] ) {
echo $display;
}
return $display;
}
return '';
}
} // end c2c_RevealTemplate
add_action( 'plugins_loaded', array( 'c2c_RevealTemplate', 'get_instance' ) );
//
// TEMPLATE FUNCTION
//
/**
* Formats the template path info for the currently rendered template for output.
*
* If $template_path_type argument is not specified, then the default value
* configured via the plugin's settings page will be used.
*
* @since 2.0
*
* @param bool $echo Optional. Echo the template info? Default true
* @param string $template_path_type Optional. The style of the template's path for return. Accepts: 'absolute',
* 'relative', 'theme-relative', 'filename'
* @param array $args Optional. Additional configuration. See c2c_RevealTemplate::reveal() for
* documentation.
* @return string The path info for the currently rendered template
*/
if ( ! function_exists( 'c2c_reveal_template' ) ) :
function c2c_reveal_template( $echo = true, $template_path_type = '', $args = array() ) {
// See (and possibly override) 'echo' value in $args with value passed as the $echo argument.
$args['echo'] = $echo;
return c2c_RevealTemplate::get_instance()->reveal( $template_path_type, $args );
}
endif;
endif; // end if !class_exists()