-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathktwp-debuggery-toolkit.php
More file actions
232 lines (189 loc) · 7.8 KB
/
ktwp-debuggery-toolkit.php
File metadata and controls
232 lines (189 loc) · 7.8 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
<?php
/*
* Plugin Name: Kupietools Debuggery Toolkit
* Plugin URI: https://michaelkupietz.com/
* Description: My custom debugging helper functions.
* Version: 1
* Requires at least: 5.2
* Requires PHP: 7.2
* Author: Michael Kupietz
* Author URI: https://michaelkupietz.com/
* License: GPL v2 or later
* License URI: https://www.gnu.org/licenses/gpl-2.0.html
* Update URI: https://michaelkupietz.com/my-plugin/kupietools-debuggery-toolkit/
* Text Domain: mk-plugin
* Domain Path: /languages
*/
/**
* Your code goes below.
*/
// Add menu page if it doesn't exist
add_action('admin_menu', function () {
global $menu;
$exists = false;
if ($menu) {
foreach($menu as $item) {
if (isset($item[0]) && $item[0] === 'KupieTools') {
$exists = true;
break;
}
}
}
if (!$exists) {
add_menu_page(
'KupieTools Settings',
'KupieTools',
'manage_options',
'kupietools',
function() {
echo '<div class="wrap"><h1>KupieTools</h1>';
do_action('kupietools_sections');
echo '</div>';
},
'dashicons-admin-tools'
);
}
});
// Add the template logger section to the KTools page
add_action('kupietools_sections', function() {
?>
<details class="card" style="max-width: 800px; padding: 20px; margin-top: 20px;"><summary style="font-weight:bold">Template Logger Settings</summary>
<form action="options.php" method="post">
<?php
settings_fields('kupietools');
do_settings_sections('kupietools');
submit_button();
?>
</form>
</details>
<?php
});
// Rest of your settings registration code stays the same
function template_logger_settings_init() {
register_setting('kupietools', 'template_logger_enabled');
add_settings_section(
'template_logger_section',
'Template Logger Settings',
'template_logger_section_callback',
'kupietools'
); /*Parameters
$id stringrequired Slug-name to identify the section. Used in the 'id' attribute of tags.
$title stringrequired Formatted title of the section. Shown as the heading for the section.
$callback callablerequired Function that echos out any content at the top of the section (between heading and fields).
$page stringrequired The slug-name of the settings page on which to show the section. Built-in pages include 'general', 'reading', 'writing', 'discussion', 'media', etc. Create your own using add_options_page() ;
$args arrayoptional Arguments used to create the settings section.
before_section string HTML content to prepend to the section’s HTML output. Receives the section’s class name as %s.
after_section string HTML content to append to the section’s HTML output.
section_class string The class name to use for the section.
Default:array()*/
add_settings_field(
'template_logger_enabled',
'Enable Template Logging',
'template_logger_field_callback',
'kupietools',
'template_logger_section'
); /* Parameters
$id stringrequired Slug-name to identify the field. Used in the 'id' attribute of tags.
$title stringrequired Formatted title of the field. Shown as the label for the field during output.
$callback allablerequired Function that fills the field with the desired form inputs. The function should echo its output.
$page stringrequired The slug-name of the settings page on which to show the section (general, reading, writing, …).
$section stringoptional The slug-name of the section of the settings page in which to show the box. Default 'default'. Default:'default'
$args arrayoptional Extra arguments that get passed to the callback function.
label_for string When supplied, the setting title will be wrapped in a <label> element, its for attribute populated with this value.
class string CSS Class to be added to the <tr> element when the field is output.
Default:array()
More Information:
You MUST register any options used by this function with register_setting() or they won’t be saved and updated automatically.
The callback function needs to output the appropriate html input and fill it with the old value, the saving will be done behind the scenes.
The html input field’s name attribute must match $option_name in register_setting(), and value can be filled using get_option().
This function can also be used to add extra settings fields to the default WP settings pages like media or general. You can add them to an existing section, or use add_settings_section() to create a new section to add the fields to.
*/
}
add_action('admin_init', 'template_logger_settings_init');
// Section callback = output info between section header and fields
function template_logger_section_callback() {
echo '<p>Control template part logging functionality.</p>';
}
// Field callback - html for the field
function template_logger_field_callback() {
$enabled = get_option('template_logger_enabled');
echo '<input type="checkbox" name="template_logger_enabled" value="1" ' . checked(1, $enabled, false) . '/>';
}
/* this was to add it to the general settings screen
function template_logger_settings_init() {
register_setting('general', 'template_logger_enabled');
add_settings_section(
'template_logger_section',
'Template Logger Settings',
'template_logger_section_callback',
'general'
);
add_settings_field(
'template_logger_enabled',
'Enable Template Logging',
'template_logger_field_callback',
'general',
'template_logger_section'
);
}
add_action('admin_init', 'template_logger_settings_init');
// Section callback
function template_logger_section_callback() {
echo '<p>Control template part logging functionality.</p>';
}
// Field callback
function template_logger_field_callback() {
$enabled = get_option('template_logger_enabled');
echo '<input type="checkbox" name="template_logger_enabled" value="1" ' . checked(1, $enabled, false) . '/>';
} */
// Modified logging functions that check the setting
function log_template_parts($template_name, $template_path, $located, $args) {
if (!get_option('template_logger_enabled')) {
return $located;
}
global $wp_query;
$wp_query->template_parts_log = array();
$wp_query->template_parts_log[] = array(
'name' => "start " . $template_name,
'path' => $template_path,
'location' => $located,
'args' => $args,
);
return $located;
}
function end_log_template_parts($template_name, $template_path, $located, $args) {
if (!get_option('template_logger_enabled')) {
return $located;
}
global $wp_query;
$wp_query->template_parts_log = array();
$wp_query->template_parts_log[] = array(
'name' => "end " . $template_name,
'path' => $template_path,
'location' => $located,
'args' => $args,
);
return $located;
}
function display_logged_template_parts() {
if (!get_option('template_logger_enabled')) {
return;
}
global $wp_query;
if (!empty($wp_query->template_parts_log)) {
foreach ($wp_query->template_parts_log as $template_data) {
echo "<!-- Template Part: {$template_data['name']} -->\n";
echo "\n";
}
}
}
// Keep your existing hooks
add_filter('get_template_part', 'log_template_parts', 1, 4);
add_filter('get_template_part', 'display_logged_template_parts', 2, 4);
//replace bugfu
if (!function_exists('BugFu_log')) {
function BugFu_log($comment) {
if (class_exists('BugFu',false)) { BugFu::log($comment);}
}
}
?>