-
Notifications
You must be signed in to change notification settings - Fork 0
/
theme-inspector.php
215 lines (211 loc) · 6.67 KB
/
theme-inspector.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
<?php
/*
Plugin Name: Theme Inspector
Plugin URI: https://github.com/melissacabral/theme_inspector
Description: displays useful technical information on pages and posts to aid in developing Wordpress themes. Only visible to administrators. Use In Conjunction with the WP Template Hierarchy Document
Author: Melissa Cabral
Version: 2.1.0
Author URI: http://melissacabral.com/
*/
/**
* Display Theme Inspector by Melissa Cabral as Toolbar (Admin Bar) Item
* @since ver 2.0
*/
add_action( 'admin_bar_menu', 'mmc_toolbar_link', 999 );
function mmc_toolbar_link( $wp_admin_bar ) {
if (current_user_can('install_themes')) {
$html = mmc_generate_output();
$args = array(
'id' => 'theme-helper',
'title' => 'Theme Inspector',
'parent' => 'top-secondary',
'meta' => array(
'class' => 'theme-helper',
'html' => $html,
),
);
$wp_admin_bar->add_node( $args );
}
}
/**
* function to generate output HTML
* @since 1.0
*/
function mmc_generate_output(){
//make sure user is logged in
if (current_user_can('install_themes')) {
global $post;
//begin Table Output
ob_start();
?>
<!-- WordPress Theme Inspector by Melissa Cabral-->
<div id="theme-helper-toolbar">
<table>
<?php if(is_admin()){ ?>
<tr>
<td colspan="2"><a style="text-align:center" href="<?php echo home_url('/'); ?>">View your site to see Theme helper in action!</a></td>
</tr>
<?php }else{ ?>
<tr>
<th>Content Type:</th>
<td><?php echo adminhelper_content_type()?></td>
</tr>
<?php if(is_singular()){ ?>
<tr>
<th>Post ID:</th>
<td><?php echo adminhelper_post_id()?></td>
</tr>
<?php } ?>
<tr>
<th>True Condition(s):</th>
<td><?php echo adminhelper_true_conditions()?></td>
</tr>
<?php if( !is_404() && ! is_search() && get_post_type() ) { ?>
<tr>
<th>Post Type:</th>
<td><?php echo get_post_type(); ?></td>
</tr>
<?php } ?>
<?php
if( is_category() || is_tax() || is_tag() ){ ?>
<tr>
<th>Taxonomy:</th>
<td><?php
echo adminhelper_taxonomy()?>
</td>
</tr>
<?php } ?>
<?php
if (isset($post->ID) && is_page() && get_post_meta($post->ID,'_wp_page_template',true) != 'default') {?>
<tr>
<th>Custom Template:</th>
<td><?php echo get_post_meta($post->ID,'_wp_page_template',true) ?></td>
</tr>
<tr>
<th>Order:</th>
<td><?php echo $post->menu_order ?></td>
</tr>
<?php
} ?>
<tr class="file-loaded">
<th>File Loaded:</th>
<td><?php echo adminhelper_get_current_template() ?></td>
</tr>
<?php } //end not admin ?>
<tr class="credits">
<td colspan="2">Theme Inspector by <a href="https://github.com/melissacabral/theme_inspector">Melissa Cabral</a>.</td>
</tr>
<tr class="credits usewith">
<td colspan="2">Use with <a target="_blank" href="http://wptutsplus.s3.amazonaws.com/090_WPCheatSheets/WP_CheatSheet_TemplateMap.jpg">Hierarchy Diagram</a></td>
</tr>
</table>
</div><!-- End Theme Inspector by Melissa Cabral-->
<?php
return ob_get_clean();
} //end is user logged in
}
/**
* helper functions
*/
function adminhelper_taxonomy(){
global $post;
$queried_object = get_queried_object();
$term_id = $queried_object->term_id;
$tax_name = $queried_object->taxonomy;
$term = single_cat_title('', false );
$output = '';
$output .= $tax_name .' > ';
$output .= $term ;
$output .= ' (ID: '. $term_id .')';
return $output;
}
function adminhelper_content_type(){
global $post;
$output = '';
if (is_admin()) { $output .= "Admin Panel"; }
if (is_front_page()) { $output .= "Front Page"; }
if (is_home()) { $output .= "Home (blog)"; }
if (is_single()) { $output .= "Single Post "; }
if (is_page() && !is_front_page()) { $output .= "Page "; }
if (is_category()) { $output .= "Category "; }
if (is_tag()) { $output .= "Tag "; }
if (is_tax()) { $output .= "Taxonomy "; }
if (is_author()) { $output .= "Author "; }
if (is_archive()) { $output .= "Archive "; }
if (is_date()) { $output .= " - Date "; }
if (is_year()) { $output .= " (year) "; }
if (is_month()) { $output .= " (monthly) "; }
if (is_day()) { $output .= " (daily) "; }
if (is_time()) { $output .= " (time) "; }
if (is_search()) { $output .= "Search "; }
if (is_404()) { $output .= "404 "; }
if (is_paged()) { $output .= " (Paged) "; }
return $output;
}
function adminhelper_post_id(){
global $post;
if( isset($post) ){
$post_id = $post->ID;
}else{
$post_id = 'no post defined';
}
return $post_id;
}
function adminhelper_true_conditions(){
global $post;
$conditions = array();
$output = '';
$count = 0;
if (is_admin()) { $conditions[] = "is_admin()"; }
if (is_front_page()) { $conditions[] = "is_front_page()"; }
if (is_home()) { $conditions[] = "is_home()"; }
if (is_attachment() ){ $conditions[] = "is_attachment()"; }
if (is_single()) { $conditions[] = "is_single()"; }
if (is_page()) { $conditions[] = "is_page()"; }
if (is_singular()) { $conditions[] = "is_singular() "; }
if (is_category()) { $conditions[] = "is_category()"; }
if (is_tag()) { $conditions[] = "is_tag()"; }
if (is_tax()) { $conditions[] = "is_tax()"; }
if (is_author()) { $conditions[] = "is_author()"; }
if (is_post_type_archive()){ $conditions[] = "is_post_type_archive()"; }
if (is_date()) { $conditions[] = "is_date()"; }
if (is_year()) { $conditions[] = "is_year()"; }
if (is_month()) { $conditions[] = " is_month()"; }
if (is_day()) { $conditions[] = " is_day()"; }
if (is_time()) { $conditions[] = " is_time()"; }
if (is_archive()) { $conditions[] = " is_archive() "; }
if (is_search()) { $conditions[] = "is_search() "; }
if (is_404()) { $conditions[] = "is_404() "; }
if (is_paged()) { $conditions[] = "is_paged() "; }
foreach($conditions as $condition){
if($count == 0)
$output.= '<span class="first condition">'.$condition.'</span>';
else
$output.= '<span class="condition">, '.$condition.'</span>';
$count ++;
}
return $output;
}
function adminhelper_get_current_template( ) {
if( !isset( $GLOBALS['current_theme_template'] ) ){
return false;
}
return $GLOBALS['current_theme_template'];
}
//Added to Fetch the template file being used
add_filter( 'template_include', 'var_template_include', 1000 );
function var_template_include( $t ){
$GLOBALS['current_theme_template'] = basename($t);
return $t;
}
/**
* Enqueue the stylesheet
* @since ver 2.0
*/
add_action('wp_enqueue_scripts', 'adminhelper_enqueue_stylesheet');
add_action('admin_enqueue_scripts', 'adminhelper_enqueue_stylesheet');
function adminhelper_enqueue_stylesheet(){
$src = plugins_url( 'theme-inspector.css', __FILE__ );
wp_register_style( 'themehelper-style', $src, '', '', 'screen' );
wp_enqueue_style( 'themehelper-style' );
}