-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
show-current-template.php
223 lines (187 loc) · 7.06 KB
/
show-current-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
<?php
/**
Plugin Name: Show Current Template
Plugin URI: https://wp.tekapo.com/
Description: Show the current template file name in the tool bar. <a href="https://wp.tekapo.com/is-my-plugin-useful/">Is this useful for you?</a>
Author: JOTAKI Taisuke
Version: 0.5.2
Requires at least: 5.9
Requires PHP: 7.4
Author URI: https://tekapo.com/
Text Domain: show-current-template
Domain Path: /languages/
License:
Released under the GPL license
http://www.gnu.org/copyleft/gpl.html
Copyright 2023 (email : [email protected])
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 St, Fifth Floor, Boston, MA 02110-1301 USA
* */
define( 'WPSCT_VERSION', '0.5.2' );
load_plugin_textdomain( 'show-current-template', false, dirname( plugin_basename( __FILE__ ) ) . '/languages/' );
new Show_Template_File_Name();
class Show_Template_File_Name {
public $debug_info = array();
public function __construct() {
add_action( 'wp_footer', array( $this, 'get_included_files_at_footr' ) );
add_action( 'admin_bar_menu', array( &$this, 'show_template_file_name_on_top' ), 9999 );
add_action( 'wp_enqueue_scripts', array( &$this, 'add_current_template_stylesheet' ), 9999 );
add_action( 'wp_enqueue_scripts', array( &$this, 'add_current_template_js' ), 9999 );
}
public function show_template_file_name_on_top( $wp_admin_bar ) {
if ( is_admin() || ! is_super_admin() ) {
return;
}
global $template;
$template_relative_path = str_replace( ABSPATH . 'wp-content/', '', $template );
if ( wp_is_block_theme() ) {
$template_file_name = __( '!!Block Theme!!', 'show-current-template' );
$site_editor_url = admin_url( 'site-editor.php' );
$block_theme_notice = sprintf(
/* translators: The placeholder is a URL. */
__(
"<p>The theme you're currently using is a block theme. You can modify its templates through the <a href='%s'>Site Editor</a> on your admin page.</p>
<p>Generally, it's advisable not to alter the template files of block themes directly.</p>
<p>Please refer to the <a href='https://developer.wordpress.org/block-editor/getting-started/full-site-editing/'>Full Site Editing</a> page for further information.</p>",
'show-current-template'
),
$site_editor_url
);
} else {
$template_file_name = '<span class="show-template-name">' . basename( $template ) . '</span>';
$menu_title = __( 'Template relative path:', 'show-current-template' )
. '<span class="show-template-name"> ' . $template_relative_path . '</span>';
}
$current_theme = wp_get_theme();
$current_theme_name = $current_theme->Name;
$parent_theme_name = '';
if ( is_child_theme() ) {
$child_theme_name = __( 'Theme name: ', 'show-current-template' )
. $current_theme_name;
$parent_theme_name = $current_theme->parent()->Name;
$parent_theme_name = ' (' . $parent_theme_name
. __( "'s child", 'show-current-template' ) . ')';
$parent_or_child = $child_theme_name . $parent_theme_name;
} else {
$parent_or_child = __( 'Theme name: ', 'show-current-template' )
. $current_theme_name . ' (' . __( 'NOT a child theme', 'show-current-template' ) . ')';
}
if ( ! wp_is_block_theme() ) {
$included_files = get_included_files();
$included_files_list = '';
sort( $included_files );
foreach ( $included_files as $filename ) {
if ( strstr( $filename, 'themes' ) ) {
$filepath = strstr( $filename, 'themes' );
if ( $template_relative_path === $filepath ) {
$included_files_list .= '';
} else {
$included_files_list .= '<li>' . "$filepath" . '</li>';
}
}
}
$admin_bar_dropdown_menu = __( 'Also, below template files are included:', 'show-current-template' )
. '<br /><ul id="included-files-list">'
. $included_files_list
. '</ul>';
}
global $wp_admin_bar;
$args = array(
'id' => 'show_template_file_name_on_top',
'title' => __( 'Template: ', 'show-current-template' ) . $template_file_name,
);
$wp_admin_bar->add_node( $args );
if ( wp_is_block_theme() ) {
$wp_admin_bar->add_menu(
array(
'parent' => 'show_template_file_name_on_top',
'id' => 'template_relative_path',
'title' => $block_theme_notice,
)
);
} else {
$wp_admin_bar->add_menu(
array(
'parent' => 'show_template_file_name_on_top',
'id' => 'template_relative_path',
'title' => $menu_title,
)
);
$wp_admin_bar->add_menu(
array(
'parent' => 'show_template_file_name_on_top',
'id' => 'is_child_theme',
'title' => $parent_or_child,
)
);
$wp_admin_bar->add_menu(
array(
'parent' => 'show_template_file_name_on_top',
'id' => 'included_files_path',
'title' => $admin_bar_dropdown_menu,
)
);
}
}
public function get_included_files_at_footr() {
if ( is_admin() || ! is_super_admin() || wp_is_block_theme() ) {
return;
}
$included_files = get_included_files();
global $template;
$template_relative_path = str_replace( ABSPATH . 'wp-content/', '', $template );
sort( $included_files );
$included_files_list = '';
foreach ( $included_files as $filename ) {
if ( strstr( $filename, 'themes' . DIRECTORY_SEPARATOR ) ) {
$filepath = strstr( $filename, 'themes' );
if ( $template_relative_path === $filepath ) {
$included_files_list .= '';
} else {
$included_files_list .= '<li>' . "$filepath" . '</li>';
}
}
}
$included_files_format = '<ol id="included-files-fie-on-wp-footer">'
. '%s'
. '</ol>';
$included_files_html = sprintf( $included_files_format, $included_files_list );
echo wp_kses_post( $included_files_html );
}
public function add_current_template_stylesheet() {
if ( is_admin() || ! is_super_admin() ) {
return;
}
$wp_version = get_bloginfo( 'version' );
if ( $wp_version >= '3.8' ) {
$is_older_than_3_8 = '';
} else {
$is_older_than_3_8 = '-old';
}
$stylesheet_path = plugins_url( 'css/style' . $is_older_than_3_8 . '.css', __FILE__ );
wp_register_style( 'current-template-style', $stylesheet_path, array(), WPSCT_VERSION );
wp_enqueue_style( 'current-template-style' );
}
public function add_current_template_js() {
if ( is_admin() || ! is_super_admin() || ! is_admin_bar_showing() || wp_is_block_theme() ) {
return;
}
$wp_version = get_bloginfo( 'version' );
if ( $wp_version >= '5.4' ) {
$js_path = plugins_url( 'assets/js/replace.js', __FILE__ );
wp_register_script( 'current-template-js', $js_path, array( 'jquery' ), WPSCT_VERSION, true );
wp_enqueue_script( 'current-template-js' );
} else {
return;
}
}
}