-
Notifications
You must be signed in to change notification settings - Fork 5
/
plugin.php
266 lines (213 loc) · 7.91 KB
/
plugin.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
<?php
/**
* Plugin Name: DustPress Debugger
* Plugin URI: https://github.com/devgeniem/dustpress-debugger
* Description: Provides handy ajaxified debugger tool for DustPress based themes.
* Version: 2.0.0
* Author: Geniem Oy / Miika Arponen & Ville Siltala
* Author URI: http://www.geniem.com
*/
namespace DustPress;
/**
* DustPressDebugger
*/
class Debugger
{
const SCRIPT_HANDLE = 'dustpress_debugger';
private static $hash;
private static $data = [];
/**
* Add hooks if the user has correct capabilities.
*/
public static function init() {
if (
(
is_user_logged_in() &&
current_user_can( 'manage_options' )
) ||
(
defined( 'DUSTPRESS_DEBUGGER_ALWAYS_ON' ) &&
\DUSTPRESS_DEBUGGER_ALWAYS_ON === true
) ) {
// Register user option hooks
add_action('show_user_profile', array(__CLASS__, 'profile_option'));
add_action('edit_user_profile', array(__CLASS__, 'profile_option'));
add_action('personal_options_update', array(__CLASS__, 'save_profile_option'));
add_action('edit_user_profile_update', array(__CLASS__, 'save_profile_option'));
if (
get_the_author_meta( 'dustpress_debugger', get_current_user_id() ) ||
(
defined( 'DUSTPRESS_DEBUGGER_ALWAYS_ON' ) &&
\DUSTPRESS_DEBUGGER_ALWAYS_ON === true
)
) {
// Register debugger ajax hook
add_action('wp_ajax_dustpress_debugger', array(__CLASS__, 'get_debugger_data'));
add_action('wp_ajax_nopriv_dustpress_debugger', array(__CLASS__, 'get_debugger_data'));
add_filter('dustpress/data/after_render', array(__CLASS__, 'performance'), 100, 2);
add_filter('dustpress/data/after_render', array(__CLASS__, 'debugger'), 101, 2);
add_filter('dustpress/data/after_render', array(__CLASS__, 'templates'), 99, 2);
// Register DustPress core helper hooks
add_filter('dustpress/menu/data', array(__CLASS__, 'gather_menu_helper_data'));
add_filter('dustpress/template', array(__CLASS__, 'main_model'), 100, 1);
add_action('dustpress/model_list', array(__CLASS__, 'models'), 100, 1);
// Prevent DustPress for caching the rendered output so that this plugin works
add_filter('dustpress/cache/rendered', '__return_false', (PHP_INT_MAX - 1000));
add_filter('dustpress/cache/partials', '__return_false', (PHP_INT_MAX - 1000));
\add_action( 'wp_head', [ __CLASS__, 'wp_head'] );
}
}
}
public static function use_debugger() {
if (
(
is_user_logged_in() &&
current_user_can( 'manage_options' ) &&
get_the_author_meta( 'dustpress_debugger', get_current_user_id() )
) ||
(
defined( 'DUSTPRESS_DEBUGGER_ALWAYS_ON' ) &&
\DUSTPRESS_DEBUGGER_ALWAYS_ON === true
) ) {
return true;
} else {
return false;
}
}
/**
* Sets the hash for the data to the DOM to get.
* @param object $data DustPress render data
* @return object
*/
public static function wp_head() {
self::$hash = md5( $_SERVER['REQUEST_URI'] . microtime() );
$data_array = [
'ajaxurl' => \admin_url('admin-ajax.php'),
'hash' => self::$hash
];
\wp_register_script( static::SCRIPT_HANDLE, '' );
\wp_enqueue_script( static::SCRIPT_HANDLE );
\wp_localize_script( static::SCRIPT_HANDLE, 'dustpress_debugger', $data_array );
}
/**
* Add data for js.
*
* @param string $hash The current data hash.
*/
public static function debugger($data, $main)
{
if ($main) {
$debugger_data = array_merge((array) $data, (array) self::$data);
$debugger_data = apply_filters('dustpress/debugger/data', $debugger_data);
set_transient('dustpress_debugger_' . self::$hash, $debugger_data, 5 * 60);
}
}
/**
* Function for the AJAX call to get the debugger data from the transient.
*/
public static function get_debugger_data()
{
if (defined('DOING_AJAX')) {
$hash = filter_input(INPUT_POST, 'hash');
$data = get_transient('dustpress_debugger_' . $hash);
if ($data) {
$status = 'success';
} else {
$status = 'error';
}
// The response data
$response = [
'status' => $status,
'data' => $data
];
$output = wp_json_encode($response);
wp_send_json($output);
}
}
public static function gather_menu_helper_data($data)
{
self::set_debugger_data('Menu', $data);
return $data;
}
/**
* Gathers debug data from other sources than DustPress core.
*/
public static function set_debugger_data($key, $data)
{
if (empty($key)) {
die('You did not set a key for your debugging data collection.');
} else {
$debug_data_block_name = dustpress()->get_setting( 'debug_data_block_name' ) ?? 'Debug';
if (!isset(self::$data['Debugs'])) {
self::$data['Debugs'] = [];
}
if (!isset(self::$data['Debugs'][$debug_data_block_name])) {
self::$data['Debugs'][$debug_data_block_name] = [];
}
if (!isset(self::$data['Debugs'][$debug_data_block_name][$key])) {
self::$data['Debugs'][$debug_data_block_name][$key] = [];
}
self::$data['Debugs'][$debug_data_block_name][$key][] = $data;
}
}
public static function profile_option($user)
{
$current_status = get_the_author_meta('dustpress_debugger', $user->ID);
?>
<h3>DustPress Debugger</h3>
<table class="form-table">
<tr>
<th><label for="dustpress_debugger">DustPress Debugger enabled</label></th>
<td>
<input type="checkbox" name="dustpress_debugger" value="1" id="dustpress_debugger" <?php if ($current_status) : ?> checked="checked" <?php endif; ?> />
</td>
</tr>
</table>
<?php
}
public static function save_profile_option($user_id)
{
if (!current_user_can("manage_options")) {
return false;
}
$user_value = (int) filter_input(INPUT_POST, 'dustpress_debugger', FILTER_SANITIZE_NUMBER_INT);
update_user_meta($user_id, 'dustpress_debugger', $user_value);
}
public static function main_model($model)
{
if (!isset(self::$data['Debugs'])) {
self::$data['Debugs'] = [];
}
self::$data['Debugs']['Main model'] = $model;
return $model;
}
public static function templates($data, $main)
{
if ($main) {
if (!isset(self::$data['Debugs'])) {
self::$data['Debugs'] = [];
}
self::$data['Debugs']['Templates'] = array_keys((array) dustpress()->dust->templates);
}
return $data;
}
public static function performance($data, $main)
{
if ($main) {
self::$data['Performance'] = dustpress()->get_performance_data();
}
return $data;
}
public static function models($models)
{
if (!isset(self::$data['Debugs'])) {
self::$data['Debugs'] = [];
}
self::$data['Debugs']['Submodels'] = $models;
}
public static function get_data($key)
{
return self::$data[$key] ?? null;
}
}
add_action('init', __NAMESPACE__ . '\\Debugger::init');