PluginProbe
Debug Log Manager – Conveniently Monitor and Inspect Errors / 2.3.2
Debug Log Manager – Conveniently Monitor and Inspect Errors v2.3.2
2.5.2 2.5.1 trunk 1.0.0 1.0.1 1.1.0 1.2.0 1.3.0 1.3.1 1.3.3 1.4.0 1.5.0 1.5.1 1.5.2 1.5.3 1.6.0 1.6.1 1.6.2 1.6.3 1.6.4 1.7.0 1.8.0 1.8.2 1.8.3 1.8.4 All 49 releases
debug-log-manager / bootstrap.php

bootstrap.php in Debug Log Manager – Conveniently Monitor and Inspect Errors 2.3.2, at bootstrap.php

526 lines 16.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 // We're using the singleton design pattern
4 // https://code.tutsplus.com/articles/design-patterns-in-wordpress-the-singleton-pattern--wp-31621
5 // https://carlalexander.ca/singletons-in-wordpress/
6 // https://torquemag.io/2016/11/singletons-wordpress-good-evil/
7
8 /**
9 * Main class of the plugin used to add functionalities
10 *
11 * @since 1.0.0
12 */
13 class Debug_Log_Manager {
14
15 // Refers to a single instance of this class
16 private static $instance = null;
17
18 // For the debug log object
19 private $debug_log;
20
21 // For the wp-config object
22 private $wp_config;
23
24 /**
25 * Creates or returns a single instance of this class
26 *
27 * @return Debug_Log_Manager a single instance of this class.
28 */
29 public static function get_instance() {
30
31 if ( null == self::$instance ) {
32 self::$instance = new self;
33 }
34
35 return self::$instance;
36
37 }
38
39 /**
40 * Initialize plugin functionalities
41 */
42 private function __construct() {
43
44 global $pagenow;
45
46 // Register admin menu and subsequently the main admin page
47 add_action( 'admin_menu', [ $this, 'register_admin_menu' ] );
48
49 // Do not display any admin notices while viewing logs.
50 add_action( 'admin_notices', [ $this, 'suppress_admin_notices' ], 0 );
51 add_action( 'all_admin_notices', [ $this, 'suppress_generic_notices' ], 0 );
52
53 // Add action links
54 add_filter( 'plugin_action_links_'.DLM_SLUG.'/'.DLM_SLUG.'.php', [ $this, 'action_links' ] );
55
56 if ( is_admin() ) {
57
58 if ( $this->is_dlm() ) {
59
60 // Update footer text
61 add_filter( 'admin_footer_text', [ $this, 'footer_text' ] );
62
63 // Replace WP version text in footer
64 add_filter( 'update_footer', [ $this, 'footer_version_text' ], 20 );
65
66
67 // Enqueue admin scripts and styles only on the plugin's main page
68 add_action( 'admin_enqueue_scripts', [ $this, 'admin_scripts' ] );
69
70 }
71
72 }
73
74 // Enqueue admin scripts and styles on plugin editor page
75 if ( 'plugin-editor.php' === $pagenow ) {
76 add_action( 'admin_enqueue_scripts', [ $this, 'plugin_editor_scripts' ] );
77 }
78
79 // Enqueue admin scripts and styles on theme editor page
80 if ( 'theme-editor.php' === $pagenow ) {
81 add_action( 'admin_enqueue_scripts', [ $this, 'theme_editor_scripts' ] );
82 }
83
84 // Add admin bar icon if error logging is enabled and admin URL is not the plugin's main page. It will show on on the front end too (when logged-in), as we're also logging JavaScript errors.
85
86 $default_value = array(
87 'status' => 'disabled',
88 'on' => date( 'Y-m-d H:i:s' ),
89 );
90
91 $logging_info = get_option( 'debug_log_manager', $default_value );
92 $logging_status = $logging_info['status'];
93
94 if ( ( $logging_status == 'enabled' ) && ! $this->is_dlm() ) {
95
96 // https://developer.wordpress.org/reference/hooks/admin_bar_menu/
97 add_action( 'admin_bar_menu', [ $this, 'admin_bar_icon' ] );
98
99 }
100
101 // Add dashboard widget
102 add_action( 'wp_dashboard_setup', [ $this, 'add_dashboard_widget' ] );
103
104 // Add inline CSS for the admin bar icon (menu item)
105 add_action( 'admin_enqueue_scripts', [ $this, 'admin_bar_icon_css' ] );
106 add_action( 'wp_enqueue_scripts', [ $this, 'admin_bar_icon_css' ] );
107
108 // Enqueue public scripts and styles
109 add_action( 'wp_enqueue_scripts', [ $this, 'public_scripts' ] );
110
111 // Register ajax calls
112 $this->debug_log = new DLM\Classes\Debug_Log;
113 $this->wp_config = new DLM\Classes\WP_Config_Transformer;
114 add_action( 'wp_ajax_toggle_debugging', [ $this->debug_log, 'toggle_debugging' ] );
115 add_action( 'wp_ajax_toggle_autorefresh', [ $this->debug_log, 'toggle_autorefresh' ] );
116 add_action( 'wp_ajax_get_latest_entries', [ $this->debug_log, 'get_latest_entries' ] );
117 add_action( 'wp_ajax_clear_log', [ $this->debug_log, 'clear_log' ] );
118 add_action( 'wp_ajax_disable_wp_file_editor', [ $this->debug_log, 'disable_wp_file_editor' ] );
119 add_action( 'wp_ajax_log_js_errors', [ $this->debug_log, 'log_js_errors' ] );
120 add_action( 'wp_ajax_nopriv_log_js_errors', [ $this->debug_log, 'log_js_errors' ] );
121
122 }
123
124 /**
125 * Check if current screen is this plugin's main page
126 *
127 * @since 1.0.0
128 */
129 public function is_dlm() {
130
131 $request_uri = sanitize_text_field( $_SERVER['REQUEST_URI'] ); // e.g. /wp-admin/index.php?page=page-slug
132
133 if ( strpos( $request_uri, 'tools.php?page=' . DLM_SLUG ) !== false ) {
134 return true; // Yes, this is the plugin's main page
135 } else {
136 return false; // Nope, this is NOT the plugin's page
137 }
138
139 }
140
141 /**
142 * Register admin menu
143 *
144 * @since 1.0.0
145 */
146 public function register_admin_menu() {
147
148 add_submenu_page(
149 'tools.php',
150 __( 'Debug Log Manager', 'debug-log-manager' ),
151 __( 'Debug Log Manager', 'debug-log-manager' ),
152 'manage_options',
153 'debug-log-manager',
154 [ $this, 'create_main_page' ]
155 );
156
157 }
158
159 /**
160 * Register action links
161 *
162 * @since 1.0.0
163 */
164 public function action_links( $links ) {
165
166 $settings_link = '<a href="tools.php?page='.DLM_SLUG.'">' . esc_html__( 'View Debug Log', 'debug-log-manager' ) . '</a>';
167
168 array_unshift($links, $settings_link);
169
170 return $links;
171
172 }
173
174 /**
175 * Change admin footer text
176 *
177 * @since 1.0.0
178 */
179 public function footer_text() {
180 ?>
181 <a href="https://bowo.io/dotorg-dlm" target="_blank"><?php esc_html_e( 'Debug Log Manager', 'debug-log-manager' ); ?></a> is on <a href="https://bowo.io/github-dlm" target="_blank">github</a>
182 <?php
183 }
184
185 /**
186 * Replace WP version number text in footer
187 *
188 * @since 2.1.4
189 */
190 public function footer_version_text() {
191 return 'Also by Bowo &#8594; <a href="https://bowo.io/wpn-dlm" target="_blank">WordPress Newsboard</a>: The latest from 100+ sources';
192 }
193
194 /**
195 * Add debug icon in the admin bar
196 *
197 * @since 1.6.0
198 */
199 public function admin_bar_icon( WP_Admin_Bar $wp_admin_bar ) {
200
201 $current_user = wp_get_current_user();
202 $current_user_roles = array_values( $current_user->roles ); // indexed array
203
204 if ( in_array( 'administrator', $current_user_roles ) ) {
205
206 // https://developer.wordpress.org/reference/classes/wp_admin_bar/add_menu/
207 // https://developer.wordpress.org/reference/classes/wp_admin_bar/add_node/ for more examples
208 $wp_admin_bar->add_menu( array(
209 'id' => DLM_SLUG,
210 'parent' => 'top-secondary',
211 'group' => null,
212 'title' => '<span class="dashicons dashicons-warning"></span>',
213 'href' => admin_url( 'tools.php?page=' . DLM_SLUG ),
214 'meta' => array(
215 'class' => 'dlm-admin-bar-icon',
216 'title' => esc_attr__( 'Error logging is enabled. Click to access the Debug Log Manager.', 'debug-log-manager' )
217 ),
218 ) );
219
220 }
221
222 }
223
224 /**
225 * Create the main admin page of the plugin
226 *
227 * @since 1.0.0
228 */
229 public function create_main_page() {
230
231 $log_file_path = get_option( 'debug_log_manager_file_path' );
232 $log_file_shortpath = str_replace( sanitize_text_field( $_SERVER['DOCUMENT_ROOT'] ), "", $log_file_path );
233 $file_size = size_format( (int) filesize( $log_file_path ) );
234
235 ?>
236
237 <div class="wrap dlm-main-page">
238 <div id="dlm-header" class="dlm-header">
239 <div class="dlm-header-left">
240 <h1 class="dlm-heading"><?php esc_html_e( 'Debug Log Manager', 'debug-log-manager' ); ?> <small><?php esc_html_e( 'by', 'debug-log-manager' ); ?> <a href="https://bowo.io/bowoio-dlm" target="_blank">Bowo</a></small></h1>
241 </div>
242 <div class="dlm-header-right">
243 <a href="https://bowo.io/review-dlm" target="_blank" class="dlm-header-action"><span>�
244 </span> <?php esc_html_e( 'Review', 'debug-log-manager' ); ?></a>
245 <a href="https://bowo.io/feedback-dlm" target="_blank" class="dlm-header-action"> <?php esc_html_e( 'Feedback', 'debug-log-manager' ); ?></a>
246 <a href="https://bowo.io/sponsor-dlm" target="_blank" class="button button-primary plugin-sponsor">&#10084; <?php esc_html_e( 'Sponsor', 'debug-log-manager' ); ?></a>
247 </div>
248 </div>
249 <div class="dlm-body">
250 <div class="dlm-log-management">
251 <div class="dlm-logging-status">
252 <div class="dlm-log-status-toggle">
253 <input type="checkbox" id="debug-log-checkbox" class="inset-3 debug-log-checkbox"><label for="debug-log-checkbox" class="green debug-log-switcher"></label>
254 </div>
255 <?php echo $this->debug_log->get_status(); ?>
256 </div>
257 <div class="dlm-autorefresh-status">
258 <div class="dlm-log-autorefresh-toggle">
259 <input type="checkbox" id="debug-autorefresh-checkbox" class="inset-3 debug-autorefresh-checkbox"><label for="debug-autorefresh-checkbox" class="green debug-autorefresh-switcher"></label>
260 </div>
261 <?php echo $this->debug_log->get_autorefresh_status(); ?>
262 </div>
263 </div>
264 <?php
265 $this->debug_log->get_entries_datatable();
266 ?>
267 </div>
268 <div class="dlm-footer">
269 <div id="dlm-log-file-location-section" class="dlm-footer-section">
270 <div class="dlm-log-file-location"><strong><?php esc_html_e( 'Log file', 'debug-log-manager' ); ?></strong>: <?php echo esc_html( $log_file_shortpath ); ?> (<span id="dlm-log-file-size"><?php echo esc_html( $file_size ); ?></span>)</div>
271 <button id="dlm-log-clear" class="button button-small button-secondary dlm-footer-button dlm-log-clear"><?php esc_html_e( 'Clear Log', 'debug-log-manager' ); ?></button>
272 </div>
273 <div id="dlm-disable-wp-file-editor-section" class="dlm-footer-section dlm-top-border" style="display:none;">
274 <div><?php esc_html_e( 'Once error logging is enabled, the core\'s plugin/theme editor stays enabled even if error logging has been disabled later on. This allows for viewing the files where errors occurred even when logging has been disabled. You can optionally disable the editor here once you\'re done debugging.', 'debug-log-manager' ); ?></div>
275 <button id="dlm-disable-wp-file-editor" class="button button-small button-secondary dlm-footer-button dlm-disable-wp-file-editor"><?php esc_html_e( 'Disable Editor', 'debug-log-manager' ); ?></button>
276 </div>
277 <?php
278 echo $this->wp_config->wpconfig_file( 'status' );
279 ?>
280 </div>
281 </div>
282
283 <?php
284
285 }
286
287 /**
288 * To stop other plugins' admin notices overlaying in the Debug Log Manager UI, remove them.
289 *
290 * @hooked admin_notices
291 *
292 * @since 1.8.7
293 */
294 public function suppress_admin_notices() {
295
296 global $plugin_page;
297
298 if ( DLM_SLUG === $plugin_page ) {
299 remove_all_actions( 'admin_notices' );
300 }
301
302 }
303
304 /**
305 * Suppress all generic notices on the plugin settings page
306 *
307 * @since 1.8.8
308 */
309 public function suppress_generic_notices() {
310
311 global $plugin_page;
312
313 // Suppress all notices
314
315 if ( DLM_SLUG === $plugin_page ) {
316
317 remove_all_actions( 'all_admin_notices' );
318
319 }
320
321 }
322
323 /**
324 * Add dashboard widget with latest errors
325 *
326 * @since 1.8.0
327 */
328 public function add_dashboard_widget() {
329
330 $user = wp_get_current_user();
331 $roles = array_values( $user->roles );
332
333 if ( in_array( 'administrator', $roles ) ) {
334 wp_add_dashboard_widget(
335 'debug_log_manager_widget', // widget ID
336 __( 'Debug Log | Latest Errors', 'debug-log-manager' ), // widget title
337 array( $this, 'get_dashboard_widget_entries' ) // callback #1 to display entries
338 // array( $this, 'dashboard_widget_settings' ) // callback #2 for configuration
339 );
340 }
341
342 }
343
344 /**
345 * Load latest errors for dashboard widget
346 *
347 * @since 1.8.0
348 */
349 public function get_dashboard_widget_entries() {
350
351 $this->debug_log->get_dashboard_widget_entries();
352
353 }
354
355 /**
356 * Enqueue admin scripts
357 *
358 * @since 1.0.0
359 */
360 public function admin_scripts() {
361
362 wp_enqueue_style( 'dlm-admin', DLM_URL . 'assets/css/admin.css', array(), DLM_VERSION );
363 wp_enqueue_style( 'dlm-datatables', DLM_URL . 'assets/css/datatables.min.css', array(), DLM_VERSION );
364 wp_enqueue_style( 'dlm-toast', DLM_URL . 'assets/css/jquery.toast.min.css', array(), DLM_VERSION );
365 wp_enqueue_script( 'dlm-app', DLM_URL . 'assets/js/admin.js', array(), DLM_VERSION, false );
366 wp_enqueue_script( 'dlm-jsticky', DLM_URL . 'assets/js/jquery.jsticky.mod.min.js', array( 'jquery' ), DLM_VERSION, false );
367 wp_enqueue_script( 'dlm-datatables', DLM_URL . 'assets/js/datatables.min.js', array( 'jquery' ), DLM_VERSION, false );
368 wp_enqueue_script( 'dlm-toast', DLM_URL . 'assets/js/jquery.toast.min.js', array( 'jquery' ), DLM_VERSION, false );
369
370 // Pass on data from PHP to JS
371
372 $default_value = array(
373 'status' => 'disabled',
374 'on' => date( 'Y-m-d H:i:s' ),
375 );
376
377 $log_info = get_option( 'debug_log_manager', $default_value );
378 $log_status = $log_info['status']; // WP_DEBUG log status: enabled / disabled
379
380 if ( false !== get_option( 'debug_log_manager_autorefresh' ) ) {
381 $autorefresh_status = get_option( 'debug_log_manager_autorefresh' );
382 } else {
383 $autorefresh_status = 'disabled';
384 update_option( 'debug_log_manager_autorefresh', $autorefresh_status, false );
385 }
386
387 $nonce = wp_create_nonce( 'dlm-app' . get_current_user_id() );
388
389 wp_localize_script(
390 'dlm-app',
391 'dlmVars',
392 array(
393 'logStatus' => $log_status,
394 'autorefreshStatus' => $autorefresh_status,
395 'nonce' => $nonce,
396 'jsErrorLogging' => array(
397 'status' => '',
398 'url' => admin_url( 'admin-ajax.php' ),
399 'nonce' => wp_create_nonce( DLM_SLUG ),
400 'action' => 'log_js_errors',
401 ),
402 'toastMessage' => array(
403 'toggleDebugSuccess' => __( 'Error logging has been enabled and the latest entries have been loaded.', 'debug-log-manager' ),
404 'copySuccess' => __( 'Entries have been copied from an existing debug.log file.', 'debug-log-manager' ),
405 'logFileCleared' => __( 'Log file has been cleared.', 'debug-log-manager' ),
406 'editoDisabled' => __( 'WordPress plugin/theme editor has been disabled. ', 'debug-log-manager' ),
407 'paginationActive' => __( 'Pagination is active. Auto-refresh has been disabled.', 'debug-log-manager' ),
408 ),
409 'dataTable' => array(
410 'emptyTable' => __( 'No data available in table', 'debug-log-manager' ),
411 'info' => __( 'Showing _START_ to _END_ of _TOTAL_ entries', 'debug-log-manager' ),
412 'infoEmpty' => __( 'Showing 0 to 0 of 0 entries', 'debug-log-manager' ),
413 'infoFiltered' => __( '(filtered from _MAX_ total entries)', 'debug-log-manager' ),
414 'lengthMenu' => __( 'Show _MENU_ entries', 'debug-log-manager' ),
415 'search' => __( 'Search:', 'debug-log-manager' ),
416 'zeroRecords' => __( 'No matching records found', 'debug-log-manager' ),
417 'paginate' => array(
418 'first' => __( 'First', 'debug-log-manager' ),
419 'last' => __( 'Last', 'debug-log-manager' ),
420 'next' => __( 'Next', 'debug-log-manager' ),
421 'previous' => __( 'Previous', 'debug-log-manager' ),
422 ),
423 ),
424 )
425 );
426
427 }
428
429 /**
430 * Scripts for WP plugin editor page
431 *
432 * @since 2.0.0
433 */
434 public function plugin_editor_scripts() {
435
436 wp_enqueue_style( 'dlm-plugin-theme-editor', DLM_URL . 'assets/css/plugin-theme-editor.css', array(), DLM_VERSION );
437 wp_enqueue_script( 'dlm-plugin-editor', DLM_URL . 'assets/js/plugin-editor.js', array( 'jquery', 'wp-theme-plugin-editor' ), DLM_VERSION, false );
438
439 }
440
441 /**
442 * Scripts for WP theme editor page
443 *
444 * @since 2.0.0
445 */
446 public function theme_editor_scripts() {
447
448 wp_enqueue_style( 'dlm-plugin-theme-editor', DLM_URL . 'assets/css/plugin-theme-editor.css', array(), DLM_VERSION );
449 wp_enqueue_script( 'dlm-theme-editor', DLM_URL . 'assets/js/theme-editor.js', array( 'jquery', 'wp-theme-plugin-editor' ), DLM_VERSION, false );
450
451 }
452
453 /**
454 * Admin bar icon's inline css
455 *
456 * @since 1.6.0
457 */
458 public function admin_bar_icon_css() {
459
460 // https://developer.wordpress.org/reference/functions/wp_add_inline_style/
461 wp_add_inline_style( 'admin-bar', '
462
463 #wpadminbar .dlm-admin-bar-icon .dashicons {
464 font-family: dashicons;
465 font-size: 20px;
466 width: 20px;
467 height: 20px;
468 line-height: 32px;
469 }
470
471 #wpadminbar .quicklinks ul li.dlm-admin-bar-icon a {
472 background: green;
473 }
474
475 #wpadminbar:not(.mobile) .ab-top-menu>li:hover>.ab-item {
476 transition: .25s;
477 }
478
479 #wpadminbar:not(.mobile) .ab-top-menu>li.dlm-admin-bar-icon:hover>.ab-item,
480 #wpadminbar:not(.mobile) .ab-top-menu>li.dlm-admin-bar-icon>.ab-item:focus {
481 background: #006600;
482 color: #fff;
483 }
484
485 ' );
486
487 }
488
489 /**
490 * Enqueue public scripts
491 *
492 * @since 1.4.0
493 */
494 public function public_scripts() {
495
496 $options = get_option( 'debug_log_manager', array() );
497 if ( $options['status'] == 'enabled' ) {
498 wp_enqueue_script( 'dlm-public', DLM_URL . 'assets/js/public.js', array( 'jquery' ), DLM_VERSION, false );
499 }
500
501 $default_value = array(
502 'status' => 'disabled',
503 'on' => date( 'Y-m-d H:i:s' ),
504 );
505
506 $log_info = get_option( 'debug_log_manager', $default_value );
507 $log_status = $log_info['status']; // WP_DEBUG log status: enabled / disabled
508
509 wp_localize_script(
510 'dlm-public',
511 'dlmVars',
512 array(
513 'logStatus' => $log_status,
514 'jsErrorLogging' => array(
515 'status' => '',
516 'url' => admin_url( 'admin-ajax.php' ),
517 'nonce' => wp_create_nonce( DLM_SLUG ),
518 'action' => 'log_js_errors',
519 ),
520 )
521 );
522 }
523
524 }
525
526 Debug_Log_Manager::get_instance();