PluginProbe
Debug Log Manager – Conveniently Monitor and Inspect Errors / 2.1.4
Debug Log Manager – Conveniently Monitor and Inspect Errors v2.1.4
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.1.4, at bootstrap.php

508 lines 16.2 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 // https://developer.wordpress.org/reference/classes/wp_admin_bar/add_menu/
202 // https://developer.wordpress.org/reference/classes/wp_admin_bar/add_node/ for more examples
203 $wp_admin_bar->add_menu( array(
204 'id' => DLM_SLUG,
205 'parent' => 'top-secondary',
206 'group' => null,
207 'title' => '<span class="dashicons dashicons-warning"></span>',
208 'href' => admin_url( 'tools.php?page=' . DLM_SLUG ),
209 'meta' => array(
210 'class' => 'dlm-admin-bar-icon',
211 'title' => esc_attr__( 'Error logging is enabled. Click to access the Debug Log Manager.', 'debug-log-manager' )
212 ),
213 ) );
214
215 }
216
217 /**
218 * Create the main admin page of the plugin
219 *
220 * @since 1.0.0
221 */
222 public function create_main_page() {
223
224 $log_file_path = get_option( 'debug_log_manager_file_path' );
225 $log_file_shortpath = str_replace( sanitize_text_field( $_SERVER['DOCUMENT_ROOT'] ), "", $log_file_path );
226 $file_size = size_format( (int) filesize( $log_file_path ) );
227
228 ?>
229
230 <div class="wrap dlm-main-page">
231 <div id="dlm-header" class="dlm-header">
232 <div class="dlm-header-left">
233 <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>
234 </div>
235 <div class="dlm-header-right">
236 <a href="https://bowo.io/review-dlm" target="_blank" class="dlm-header-action"><span>�
237 </span> <?php esc_html_e( 'Review', 'debug-log-manager' ); ?></a>
238 <a href="https://bowo.io/feedback-dlm" target="_blank" class="dlm-header-action"> <?php esc_html_e( 'Feedback', 'debug-log-manager' ); ?></a>
239 <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>
240 </div>
241 </div>
242 <div class="dlm-body">
243 <div class="dlm-log-management">
244 <div class="dlm-logging-status">
245 <div class="dlm-log-status-toggle">
246 <input type="checkbox" id="debug-log-checkbox" class="inset-3 debug-log-checkbox"><label for="debug-log-checkbox" class="green debug-log-switcher"></label>
247 </div>
248 <?php echo $this->debug_log->get_status(); ?>
249 </div>
250 <div class="dlm-autorefresh-status">
251 <div class="dlm-log-autorefresh-toggle">
252 <input type="checkbox" id="debug-autorefresh-checkbox" class="inset-3 debug-autorefresh-checkbox"><label for="debug-autorefresh-checkbox" class="green debug-autorefresh-switcher"></label>
253 </div>
254 <?php echo $this->debug_log->get_autorefresh_status(); ?>
255 </div>
256 </div>
257 <?php
258 $this->debug_log->get_entries_datatable();
259 ?>
260 </div>
261 <div class="dlm-footer">
262 <div id="dlm-log-file-location-section" class="dlm-footer-section">
263 <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>
264 <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>
265 </div>
266 <div id="dlm-disable-wp-file-editor-section" class="dlm-footer-section dlm-top-border" style="display:none;">
267 <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>
268 <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>
269 </div>
270 <?php
271 echo $this->wp_config->wpconfig_file( 'status' );
272 ?>
273 </div>
274 </div>
275
276 <?php
277
278 }
279
280 /**
281 * To stop other plugins' admin notices overlaying in the Debug Log Manager UI, remove them.
282 *
283 * @hooked admin_notices
284 *
285 * @since 1.8.7
286 */
287 public function suppress_admin_notices() {
288
289 global $plugin_page;
290
291 if ( DLM_SLUG === $plugin_page ) {
292 remove_all_actions( 'admin_notices' );
293 }
294
295 }
296
297 /**
298 * Suppress all generic notices on the plugin settings page
299 *
300 * @since 1.8.8
301 */
302 public function suppress_generic_notices() {
303
304 global $plugin_page;
305
306 // Suppress all notices
307
308 if ( DLM_SLUG === $plugin_page ) {
309
310 remove_all_actions( 'all_admin_notices' );
311
312 }
313
314 }
315
316 /**
317 * Add dashboard widget with latest errors
318 *
319 * @since 1.8.0
320 */
321 public function add_dashboard_widget() {
322
323 wp_add_dashboard_widget(
324 'debug_log_manager_widget', // widget ID
325 __( 'Debug Log | Latest Errors', 'debug-log-manager' ), // widget title
326 array( $this, 'get_dashboard_widget_entries' ) // callback #1 to display entries
327 // array( $this, 'dashboard_widget_settings' ) // callback #2 for configuration
328 );
329
330 }
331
332 /**
333 * Load latest errors for dashboard widget
334 *
335 * @since 1.8.0
336 */
337 public function get_dashboard_widget_entries() {
338
339 $this->debug_log->get_dashboard_widget_entries();
340
341 }
342
343 /**
344 * Enqueue admin scripts
345 *
346 * @since 1.0.0
347 */
348 public function admin_scripts() {
349
350 wp_enqueue_style( 'dlm-admin', DLM_URL . 'assets/css/admin.css', array(), DLM_VERSION );
351 wp_enqueue_style( 'dlm-datatables', DLM_URL . 'assets/css/datatables.min.css', array(), DLM_VERSION );
352 wp_enqueue_style( 'dlm-toast', DLM_URL . 'assets/css/jquery.toast.min.css', array(), DLM_VERSION );
353 wp_enqueue_script( 'dlm-app', DLM_URL . 'assets/js/admin.js', array(), DLM_VERSION, false );
354 wp_enqueue_script( 'dlm-jsticky', DLM_URL . 'assets/js/jquery.jsticky.mod.min.js', array( 'jquery' ), DLM_VERSION, false );
355 wp_enqueue_script( 'dlm-datatables', DLM_URL . 'assets/js/datatables.min.js', array( 'jquery' ), DLM_VERSION, false );
356 wp_enqueue_script( 'dlm-toast', DLM_URL . 'assets/js/jquery.toast.min.js', array( 'jquery' ), DLM_VERSION, false );
357
358 // Pass on data from PHP to JS
359
360 $default_value = array(
361 'status' => 'disabled',
362 'on' => date( 'Y-m-d H:i:s' ),
363 );
364
365 $log_info = get_option( 'debug_log_manager', $default_value );
366 $log_status = $log_info['status']; // WP_DEBUG log status: enabled / disabled
367
368 if ( false !== get_option( 'debug_log_manager_autorefresh' ) ) {
369 $autorefresh_status = get_option( 'debug_log_manager_autorefresh' );
370 } else {
371 $autorefresh_status = 'disabled';
372 update_option( 'debug_log_manager_autorefresh', $autorefresh_status, false );
373 }
374
375 wp_localize_script(
376 'dlm-app',
377 'dlmVars',
378 array(
379 'logStatus' => $log_status,
380 'autorefreshStatus' => $autorefresh_status,
381 'jsErrorLogging' => array(
382 'status' => '',
383 'url' => admin_url( 'admin-ajax.php' ),
384 'nonce' => wp_create_nonce( DLM_SLUG ),
385 'action' => 'log_js_errors',
386 ),
387 'toastMessage' => array(
388 'toggleDebugSuccess' => __( 'Error logging has been enabled and the latest entries have been loaded.', 'debug-log-manager' ),
389 'copySuccess' => __( 'Entries have been copied from an existing debug.log file.', 'debug-log-manager' ),
390 'logFileCleared' => __( 'Log file has been cleared.', 'debug-log-manager' ),
391 'editoDisabled' => __( 'WordPress plugin/theme editor has been disabled. ', 'debug-log-manager' ),
392 'paginationActive' => __( 'Pagination is active. Auto-refresh has been disabled.', 'debug-log-manager' ),
393 ),
394 'dataTable' => array(
395 'emptyTable' => __( 'No data available in table', 'debug-log-manager' ),
396 'info' => __( 'Showing _START_ to _END_ of _TOTAL_ entries', 'debug-log-manager' ),
397 'infoEmpty' => __( 'Showing 0 to 0 of 0 entries', 'debug-log-manager' ),
398 'infoFiltered' => __( '(filtered from _MAX_ total entries)', 'debug-log-manager' ),
399 'lengthMenu' => __( 'Show _MENU_ entries', 'debug-log-manager' ),
400 'search' => __( 'Search:', 'debug-log-manager' ),
401 'zeroRecords' => __( 'No matching records found', 'debug-log-manager' ),
402 'paginate' => array(
403 'first' => __( 'First', 'debug-log-manager' ),
404 'last' => __( 'Last', 'debug-log-manager' ),
405 'next' => __( 'Next', 'debug-log-manager' ),
406 'previous' => __( 'Previous', 'debug-log-manager' ),
407 ),
408 ),
409 )
410 );
411
412 }
413
414 /**
415 * Scripts for WP plugin editor page
416 *
417 * @since 2.0.0
418 */
419 public function plugin_editor_scripts() {
420
421 wp_enqueue_style( 'dlm-plugin-theme-editor', DLM_URL . 'assets/css/plugin-theme-editor.css', array(), DLM_VERSION );
422 wp_enqueue_script( 'dlm-plugin-editor', DLM_URL . 'assets/js/plugin-editor.js', array( 'jquery', 'wp-theme-plugin-editor' ), DLM_VERSION, false );
423
424 }
425
426 /**
427 * Scripts for WP theme editor page
428 *
429 * @since 2.0.0
430 */
431 public function theme_editor_scripts() {
432
433 wp_enqueue_style( 'dlm-plugin-theme-editor', DLM_URL . 'assets/css/plugin-theme-editor.css', array(), DLM_VERSION );
434 wp_enqueue_script( 'dlm-theme-editor', DLM_URL . 'assets/js/theme-editor.js', array( 'jquery', 'wp-theme-plugin-editor' ), DLM_VERSION, false );
435
436 }
437
438 /**
439 * Admin bar icon's inline css
440 *
441 * @since 1.6.0
442 */
443 public function admin_bar_icon_css() {
444
445 // https://developer.wordpress.org/reference/functions/wp_add_inline_style/
446 wp_add_inline_style( 'admin-bar', '
447
448 #wpadminbar .dlm-admin-bar-icon .dashicons {
449 font-family: dashicons;
450 font-size: 20px;
451 width: 20px;
452 height: 20px;
453 line-height: 32px;
454 }
455
456 #wpadminbar .quicklinks ul li.dlm-admin-bar-icon a {
457 background: green;
458 }
459
460 #wpadminbar:not(.mobile) .ab-top-menu>li:hover>.ab-item {
461 transition: .25s;
462 }
463
464 #wpadminbar:not(.mobile) .ab-top-menu>li.dlm-admin-bar-icon:hover>.ab-item,
465 #wpadminbar:not(.mobile) .ab-top-menu>li.dlm-admin-bar-icon>.ab-item:focus {
466 background: #006600;
467 color: #fff;
468 }
469
470 ' );
471
472 }
473
474 /**
475 * Enqueue public scripts
476 *
477 * @since 1.4.0
478 */
479 public function public_scripts() {
480
481 wp_enqueue_script( 'dlm-public', DLM_URL . 'assets/js/public.js', array( 'jquery' ), DLM_VERSION, false );
482
483 $default_value = array(
484 'status' => 'disabled',
485 'on' => date( 'Y-m-d H:i:s' ),
486 );
487
488 $log_info = get_option( 'debug_log_manager', $default_value );
489 $log_status = $log_info['status']; // WP_DEBUG log status: enabled / disabled
490
491 wp_localize_script(
492 'dlm-public',
493 'dlmVars',
494 array(
495 'logStatus' => $log_status,
496 'jsErrorLogging' => array(
497 'status' => '',
498 'url' => admin_url( 'admin-ajax.php' ),
499 'nonce' => wp_create_nonce( DLM_SLUG ),
500 'action' => 'log_js_errors',
501 ),
502 )
503 );
504 }
505
506 }
507
508 Debug_Log_Manager::get_instance();