PluginProbe
UpStream: a Project Management Plugin for WordPress / 1.39.1
UpStream: a Project Management Plugin for WordPress v1.39.1
trunk 1.39.0 1.39.1 1.39.2 1.39.3 2.0.7 2.1.0
upstream / includes / class-up-debug.php

class-up-debug.php in UpStream: a Project Management Plugin for WordPress 1.39.1, at includes/class-up-debug.php

303 lines 9.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 // Exit if accessed directly
4 if ( ! defined('ABSPATH')) {
5 return;
6 }
7
8
9 /**
10 * UpStream_Debug Class
11 *
12 * @since 1.0.0
13 */
14 class UpStream_Debug
15 {
16 const FILE = 'debug-upstream.log';
17
18 const PAGE_SLUG = 'upstream_debug_log';
19
20 const ACTION_DELETE_LOG = 'delete_log';
21
22 const ADMIN_PAGE_URL_FRAGMENT = 'admin.php?page=';
23
24 protected static $path;
25
26 protected static $initialized = false;
27
28 protected static $messages = [];
29
30 /**
31 * Write the given message into the log file.
32 *
33 * @param $message
34 */
35 public static function write($message, $trace = null)
36 {
37 if ( ! static::is_enabled()) {
38 return;
39 }
40
41 if ( ! static::$initialized) {
42 static::init();
43 }
44
45 // Make sure we have a string to write.
46 if ( ! is_string($message)) {
47 $message = print_r($message, true);
48 }
49
50 // Add the timestamp to the message.
51 $message = sprintf('[%s] %s', date('Y-m-d H:i:s T O'), $message) . "\n";
52
53 if ($trace) {
54 $tt = print_r($trace,true);
55 $message .= $tt . "\n";
56 }
57
58 error_log($message, 3, static::$path);
59 }
60
61 /**
62 * Returns true if debug is enabled in the UpStream settings.
63 *
64 * @return bool
65 */
66 public static function is_enabled()
67 {
68 $option = get_option('upstream_general');
69
70 if (!$option) return false;
71
72 $key = 'debug';
73
74 return array_key_exists($key, $option)
75 && ! empty($option[$key])
76 && (int)$option[$key][0] === 1;
77 }
78
79 /**
80 * Get things going
81 *
82 * @since 1.0.0
83 */
84 public static function init()
85 {
86 if ( ! static::is_enabled()) {
87 return;
88 }
89
90 static::$path = str_replace('//', '/', WP_CONTENT_DIR . '/' . static::FILE);
91
92 // Admin bar.
93 add_action('admin_bar_menu', [__CLASS__, 'admin_bar_menu'], 99);
94
95 // Admin menu.
96 add_action('admin_menu', [__CLASS__, 'admin_menu']);
97
98 static::$initialized = true;
99 }
100
101 public static function admin_bar_menu()
102 {
103 global $wp_admin_bar;
104
105 $args = [
106 'id' => 'upstream_debug',
107 'title' => __('UpStream Debug Log', 'upstream'),
108 'href' => admin_url(static::ADMIN_PAGE_URL_FRAGMENT . static::PAGE_SLUG),
109 ];
110
111 $wp_admin_bar->add_menu($args);
112 }
113
114 public static function admin_menu()
115 {
116 // Admin menu.
117 add_submenu_page(
118 admin_url(static::ADMIN_PAGE_URL_FRAGMENT . static::PAGE_SLUG),
119 __('Debug Log'),
120 __('Debug Log'),
121 'activate_plugins',
122 'upstream_debug_log',
123 [__CLASS__, 'view_log_page']
124 );
125 }
126
127 public static function view_log_page()
128 {
129 static::handle_actions();
130
131 global $wp_version;
132
133 $is_log_found = file_exists(static::$path);
134
135 // Get all the plugins and versions
136 $plugins = get_plugins();
137 $pluginsData = [];
138 foreach ($plugins as $plugin => $data) {
139 $pluginsData[$plugin] = (is_plugin_active($plugin) ? 'ACTIVATED' : 'deactivated') . ' [' . $data['Version'] . ']';
140 }
141
142 $user_info = get_userdata(wp_get_current_user()->ID);
143
144 $debug_data = [
145 'php' => [
146 'version' => PHP_VERSION,
147 'os' => PHP_OS,
148 'date_default_timezone_get' => date_default_timezone_get(),
149 'date(e)' => date('e'),
150 'date(T)' => date('T'),
151 'browser_str' => $_SERVER['HTTP_USER_AGENT'],
152 ],
153 'wordpress' => [
154 'version' => $wp_version,
155 'date_format' => get_option('date_format'),
156 'time_format' => get_option('time_format'),
157 'timezone_string' => get_option('timezone_string'),
158 'gmt_offset' => get_option('gmt_offset'),
159 'plugins' => $pluginsData,
160 ],
161 'user' => [
162 'uid' => $user_info->ID,
163 'roles' => $user_info->roles,
164 'username' => $user_info->user_email,
165 'capabilities' => $user_info->allcaps,
166 ],
167 'theme' => [
168 'template' => get_template(),
169 'template_dir' => get_template_directory(),
170 ]
171 ];
172
173 $context = [
174 'label' => [
175 'title' => __('UpStream Debug Log', 'upstream'),
176 'file_info' => __('File info', 'upstream'),
177 'path' => __('Path', 'upstream'),
178 'log_content' => __('Log content', 'upstream'),
179 'size' => __('Size', 'upstream'),
180 'creation_time' => __('Created on', 'upstream'),
181 'modification_time' => __('Modified on', 'upstream'),
182 'delete_file' => __('Delete file', 'upstream'),
183 'debug_data' => __('Debug data', 'upstream'),
184 'log_file' => __('Log File', 'upstream'),
185 ],
186 'message' => [
187 'log_not_found' => __('Log file not found.', 'upstream'),
188 'contact_support_tip' => __(
189 'If you see any error or look for information regarding UpStream, please don\'t hesitate to contact the support team. E-mail us:',
190 'upstream'
191 ),
192 'click_to_delete' => __(
193 'Click to delete the log file. Be careful, this operation can not be undone. ',
194 'upstream'
195 ),
196 ],
197 'contact_email' => 'help@upstreamplugin.com',
198 'link_delete' => admin_url(
199 sprintf(
200 'admin.php?page=%s&action=%s&_wpnonce=%s',
201 static::PAGE_SLUG,
202 static::ACTION_DELETE_LOG,
203 wp_create_nonce(static::ACTION_DELETE_LOG)
204 )
205 ),
206 'is_log_found' => $is_log_found,
207 'file' => [
208 'path' => static::$path,
209 'size' => $is_log_found ? round(filesize(static::$path) / 1024, 2) : 0,
210 'modification_time' => $is_log_found ? date('Y-m-d H:i:s T O', filemtime(static::$path)) : '',
211 'content' => $is_log_found ? file_get_contents(static::$path) : '',
212 ],
213 'debug_data' => print_r($debug_data, true),
214 'messages' => static::$messages,
215 ];
216
217 ?>
218
219
220 <h1><?php esc_html_e($context['label']['title']) ?></h1>
221
222 <div id="upstream-debug-data">
223 <h2><?php esc_html_e($context['label']['debug_data']) ?></h2>
224 <textarea style="width:100%; height:400px"><?php echo esc_textarea($context['debug_data']) ?></textarea>
225 </div>
226
227 <hr>
228
229 <div id="upstream-debug-log">
230 <h2><?php esc_html_e($context['label']['log_file']) ?></h2>
231
232 <p><?php esc_html_e($context['message']['click_to_delete']) ?></p>
233 <a class="button button-danger" href="<?php esc_attr_e($context['link_delete']) ?>"><?php esc_html_e($context['label']['delete_file']) ?></a>
234
235 <h3><?php esc_html_e($context['label']['log_content']) ?></h3>
236 <textarea style="width:100%; height:400px" id="upstream-debug-log"><?php echo esc_textarea($context['file']['content']) ?></textarea>
237 </div>
238
239
240 <?php
241
242 }
243
244 protected static function handle_actions()
245 {
246 // Are we on the correct page?
247 $pageParam = 'page';
248 if ( ! array_key_exists($pageParam, $_GET) || sanitize_text_field($_GET[$pageParam]) !== static::PAGE_SLUG) {
249 return;
250 }
251
252 // Do we have an action?
253 $actionParam = 'action';
254 if ( ! array_key_exists($actionParam, $_GET) || empty($_GET[$actionParam])) {
255 return;
256 }
257
258 $action = preg_replace('/[^a-z0-9_\-]/i', '', sanitize_text_field($_GET[$actionParam]));
259
260 // Do we have a nonce?
261 $wpOnceParam = '_wpnonce';
262 if ( ! array_key_exists($wpOnceParam, $_GET) || empty($_GET[$wpOnceParam])) {
263 static::$messages[] = __('Action nonce not found.', 'upstream');
264
265 return;
266 }
267
268 // Check the nonce.
269 if ( ! wp_verify_nonce($_GET[$wpOnceParam], $action)) {
270 static::$messages[] = __('Invalid action nonce.', 'upstream');
271
272 return;
273 }
274
275 if ($action === static::ACTION_DELETE_LOG && file_exists(static::$path)) {
276 unlink(static::$path);
277 }
278
279 wp_redirect(admin_url(static::ADMIN_PAGE_URL_FRAGMENT . static::PAGE_SLUG));
280 }
281 }
282
283 function up_debug($s = null) {
284 if (!$s) {
285 $fname = "";
286 $bt = debug_backtrace();
287 for ($i = 1; $i < 2 && $i < count($bt); $i++) {
288 $fname = $bt[$i]['function'];
289 }
290 UpStream_Debug::write("Entering function: " . $fname);
291 }
292 else if (is_string($s)) {
293 UpStream_Debug::write($s, debug_backtrace());
294 }
295 else if (is_object($s)) {
296 if ($s instanceof \Exception) {
297 UpStream_Debug::write($s->getMessage(), $s->getTrace());
298 }
299 else {
300 UpStream_Debug::write(print_r($s, true), debug_backtrace());
301 }
302 }
303 }