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

497 lines 16.4 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 // Enqueue admin scripts and styles only on the plugin's main page
64 add_action( 'admin_enqueue_scripts', [ $this, 'admin_scripts' ] );
65
66 }
67
68 }
69
70 // Enqueue admin scripts and styles on plugin editor page
71 if ( 'plugin-editor.php' === $pagenow ) {
72 add_action( 'admin_enqueue_scripts', [ $this, 'plugin_editor_scripts' ] );
73 }
74
75 // Enqueue admin scripts and styles on theme editor page
76 if ( 'theme-editor.php' === $pagenow ) {
77 add_action( 'admin_enqueue_scripts', [ $this, 'theme_editor_scripts' ] );
78 }
79
80 // 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.
81
82 $default_value = array(
83 'status' => 'disabled',
84 'on' => date( 'Y-m-d H:i:s' ),
85 );
86
87 $logging_info = get_option( 'debug_log_manager', $default_value );
88 $logging_status = $logging_info['status'];
89
90 if ( ( $logging_status == 'enabled' ) && ! $this->is_dlm() ) {
91
92 // https://developer.wordpress.org/reference/hooks/admin_bar_menu/
93 add_action( 'admin_bar_menu', [ $this, 'admin_bar_icon' ] );
94
95 }
96
97 // Add dashboard widget
98
99 add_action( 'wp_dashboard_setup', [ $this, 'add_dashboard_widget' ] );
100
101 // Add inline CSS for the admin bar icon (menu item)
102 add_action( 'admin_enqueue_scripts', [ $this, 'admin_bar_icon_css' ] );
103 add_action( 'wp_enqueue_scripts', [ $this, 'admin_bar_icon_css' ] );
104
105 // Enqueue public scripts and styles
106 add_action( 'wp_enqueue_scripts', [ $this, 'public_scripts' ] );
107
108 // Register ajax calls
109 $this->debug_log = new DLM\Classes\Debug_Log;
110 $this->wp_config = new DLM\Classes\WP_Config_Transformer;
111 add_action( 'wp_ajax_toggle_debugging', [ $this->debug_log, 'toggle_debugging' ] );
112 add_action( 'wp_ajax_toggle_autorefresh', [ $this->debug_log, 'toggle_autorefresh' ] );
113 add_action( 'wp_ajax_get_latest_entries', [ $this->debug_log, 'get_latest_entries' ] );
114 add_action( 'wp_ajax_clear_log', [ $this->debug_log, 'clear_log' ] );
115 add_action( 'wp_ajax_disable_wp_file_editor', [ $this->debug_log, 'disable_wp_file_editor' ] );
116 add_action( 'wp_ajax_log_js_errors', [ $this->debug_log, 'log_js_errors' ] );
117 add_action( 'wp_ajax_nopriv_log_js_errors', [ $this->debug_log, 'log_js_errors' ] );
118
119 }
120
121 /**
122 * Check if current screen is this plugin's main page
123 *
124 * @since 1.0.0
125 */
126 public function is_dlm() {
127
128 $request_uri = sanitize_text_field( $_SERVER['REQUEST_URI'] ); // e.g. /wp-admin/index.php?page=page-slug
129
130 if ( strpos( $request_uri, 'tools.php?page=' . DLM_SLUG ) !== false ) {
131 return true; // Yes, this is the plugin's main page
132 } else {
133 return false; // Nope, this is NOT the plugin's page
134 }
135
136 }
137
138 /**
139 * Register admin menu
140 *
141 * @since 1.0.0
142 */
143 public function register_admin_menu() {
144
145 add_submenu_page(
146 'tools.php',
147 __( 'Debug Log Manager', 'debug-log-manager' ),
148 __( 'Debug Log Manager', 'debug-log-manager' ),
149 'manage_options',
150 'debug-log-manager',
151 [ $this, 'create_main_page' ]
152 );
153
154 }
155
156 /**
157 * Register action links
158 *
159 * @since 1.0.0
160 */
161 public function action_links( $links ) {
162
163 $settings_link = '<a href="tools.php?page='.DLM_SLUG.'">' . esc_html__( 'View Debug Log', 'debug-log-manager' ) . '</a>';
164
165 array_unshift($links, $settings_link);
166
167 return $links;
168
169 }
170
171 /**
172 * Change admin footer text
173 *
174 * @since 1.0.0
175 */
176 public function footer_text() {
177 ?>
178 <a href="https://wordpress.org/plugins/debug-log-manager/" target="_blank"><?php esc_html_e( 'Debug Log Manager', 'debug-log-manager' ); ?></a> (<a href="https://github.com/qriouslad/debug-log-manager" target="_blank">github</a>) <?php esc_html_e( 'is built using', 'debug-log-manager' ); ?> <a href="https://datatables.net/" target="_blank">DataTables.js</a>, <a href="https://github.com/AndrewHenderson/jSticky" target="_blank">jSticky</a> <?php esc_html_e( 'and', 'debug-log-manager' ); ?> <a href="https://github.com/kamranahmedse/jquery-toast-plugin" target="_blank">jQuery Toast</a>.
179 <?php
180 }
181
182 /**
183 * Add debug icon in the admin bar
184 *
185 * @since 1.6.0
186 */
187 public function admin_bar_icon( WP_Admin_Bar $wp_admin_bar ) {
188
189 // https://developer.wordpress.org/reference/classes/wp_admin_bar/add_menu/
190 // https://developer.wordpress.org/reference/classes/wp_admin_bar/add_node/ for more examples
191 $wp_admin_bar->add_menu( array(
192 'id' => DLM_SLUG,
193 'parent' => 'top-secondary',
194 'group' => null,
195 'title' => '<span class="dashicons dashicons-warning"></span>',
196 'href' => admin_url( 'tools.php?page=' . DLM_SLUG ),
197 'meta' => array(
198 'class' => 'dlm-admin-bar-icon',
199 'title' => esc_attr__( 'Error logging is enabled. Click to access the Debug Log Manager.', 'debug-log-manager' )
200 ),
201 ) );
202
203 }
204
205 /**
206 * Create the main admin page of the plugin
207 *
208 * @since 1.0.0
209 */
210 public function create_main_page() {
211
212 $log_file_path = get_option( 'debug_log_manager_file_path' );
213 $log_file_shortpath = str_replace( sanitize_text_field( $_SERVER['DOCUMENT_ROOT'] ), "", $log_file_path );
214 $file_size = size_format( (int) filesize( $log_file_path ) );
215
216 ?>
217
218 <div class="wrap dlm-main-page">
219 <div id="dlm-header" class="dlm-header">
220 <div class="dlm-header-left">
221 <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" target="_blank">bowo.io</a></small></h1>
222 </div>
223 <div class="dlm-header-right">
224 <a href="https://wordpress.org/plugins/debug-log-manager/" target="_blank" class="dlm-header-action"><span>&#8505;</span> <?php esc_html_e( 'Info', 'debug-log-manager' ); ?></a>
225 <a href="https://wordpress.org/plugins/debug-log-manager/#reviews" target="_blank" class="dlm-header-action"><span>�
226 </span> <?php esc_html_e( 'Review', 'debug-log-manager' ); ?></a>
227 <a href="https://wordpress.org/support/plugin/debug-log-manager/" target="_blank" class="dlm-header-action"> <?php esc_html_e( 'Feedback', 'debug-log-manager' ); ?></a>
228 <a href="https://paypal.me/qriouslad" target="_blank" class="dlm-header-action">&#10084; <?php esc_html_e( 'Donate', 'debug-log-manager' ); ?></a>
229 </div>
230 </div>
231 <div class="dlm-body">
232 <div class="dlm-log-management">
233 <div class="dlm-logging-status">
234 <div class="dlm-log-status-toggle">
235 <input type="checkbox" id="debug-log-checkbox" class="inset-3 debug-log-checkbox"><label for="debug-log-checkbox" class="green debug-log-switcher"></label>
236 </div>
237 <?php echo $this->debug_log->get_status(); ?>
238 </div>
239 <div class="dlm-autorefresh-status">
240 <div class="dlm-log-autorefresh-toggle">
241 <input type="checkbox" id="debug-autorefresh-checkbox" class="inset-3 debug-autorefresh-checkbox"><label for="debug-autorefresh-checkbox" class="green debug-autorefresh-switcher"></label>
242 </div>
243 <?php echo $this->debug_log->get_autorefresh_status(); ?>
244 </div>
245 </div>
246 <?php
247 $this->debug_log->get_entries_datatable();
248 ?>
249 </div>
250 <div class="dlm-footer">
251 <div id="dlm-log-file-location-section" class="dlm-footer-section">
252 <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>
253 <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>
254 </div>
255 <div id="dlm-disable-wp-file-editor-section" class="dlm-footer-section dlm-top-border" style="display:none;">
256 <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>
257 <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>
258 </div>
259 <?php
260 echo $this->wp_config->wpconfig_file( 'status' );
261 ?>
262 </div>
263 </div>
264
265 <?php
266
267 }
268
269 /**
270 * To stop other plugins' admin notices overlaying in the Debug Log Manager UI, remove them.
271 *
272 * @hooked admin_notices
273 *
274 * @since 1.8.7
275 */
276 public function suppress_admin_notices() {
277
278 global $plugin_page;
279
280 if ( DLM_SLUG === $plugin_page ) {
281 remove_all_actions( 'admin_notices' );
282 }
283
284 }
285
286 /**
287 * Suppress all generic notices on the plugin settings page
288 *
289 * @since 1.8.8
290 */
291 public function suppress_generic_notices() {
292
293 global $plugin_page;
294
295 // Suppress all notices
296
297 if ( DLM_SLUG === $plugin_page ) {
298
299 remove_all_actions( 'all_admin_notices' );
300
301 }
302
303 }
304
305 /**
306 * Add dashboard widget with latest errors
307 *
308 * @since 1.8.0
309 */
310 public function add_dashboard_widget() {
311
312 wp_add_dashboard_widget(
313 'debug_log_manager_widget', // widget ID
314 __( 'Debug Log | Latest Errors', 'debug-log-manager' ), // widget title
315 array( $this, 'get_dashboard_widget_entries' ) // callback #1 to display entries
316 // array( $this, 'dashboard_widget_settings' ) // callback #2 for configuration
317 );
318
319 }
320
321 /**
322 * Load latest errors for dashboard widget
323 *
324 * @since 1.8.0
325 */
326 public function get_dashboard_widget_entries() {
327
328 $this->debug_log->get_dashboard_widget_entries();
329
330 }
331
332 /**
333 * Enqueue admin scripts
334 *
335 * @since 1.0.0
336 */
337 public function admin_scripts() {
338
339 wp_enqueue_style( 'dlm-admin', DLM_URL . 'assets/css/admin.css', array(), DLM_VERSION );
340 wp_enqueue_style( 'dlm-datatables', DLM_URL . 'assets/css/datatables.min.css', array(), DLM_VERSION );
341 wp_enqueue_style( 'dlm-toast', DLM_URL . 'assets/css/jquery.toast.min.css', array(), DLM_VERSION );
342 wp_enqueue_script( 'dlm-app', DLM_URL . 'assets/js/admin.js', array(), DLM_VERSION, false );
343 wp_enqueue_script( 'dlm-jsticky', DLM_URL . 'assets/js/jquery.jsticky.mod.min.js', array( 'jquery' ), DLM_VERSION, false );
344 wp_enqueue_script( 'dlm-datatables', DLM_URL . 'assets/js/datatables.min.js', array( 'jquery' ), DLM_VERSION, false );
345 wp_enqueue_script( 'dlm-toast', DLM_URL . 'assets/js/jquery.toast.min.js', array( 'jquery' ), DLM_VERSION, false );
346
347 // Pass on data from PHP to JS
348
349 $default_value = array(
350 'status' => 'disabled',
351 'on' => date( 'Y-m-d H:i:s' ),
352 );
353
354 $log_info = get_option( 'debug_log_manager', $default_value );
355 $log_status = $log_info['status']; // WP_DEBUG log status: enabled / disabled
356
357 if ( false !== get_option( 'debug_log_manager_autorefresh' ) ) {
358 $autorefresh_status = get_option( 'debug_log_manager_autorefresh' );
359 } else {
360 $autorefresh_status = 'disabled';
361 update_option( 'debug_log_manager_autorefresh', $autorefresh_status, false );
362 }
363
364 wp_localize_script(
365 'dlm-app',
366 'dlmVars',
367 array(
368 'logStatus' => $log_status,
369 'autorefreshStatus' => $autorefresh_status,
370 'jsErrorLogging' => array(
371 'status' => '',
372 'url' => admin_url( 'admin-ajax.php' ),
373 'nonce' => wp_create_nonce( DLM_SLUG ),
374 'action' => 'log_js_errors',
375 ),
376 'toastMessage' => array(
377 'toggleDebugSuccess' => __( 'Error logging has been enabled and the latest entries have been loaded.', 'debug-log-manager' ),
378 'copySuccess' => __( 'Entries have been copied from an existing debug.log file.', 'debug-log-manager' ),
379 'logFileCleared' => __( 'Log file has been cleared.', 'debug-log-manager' ),
380 'editoDisabled' => __( 'WordPress plugin/theme editor has been disabled. ', 'debug-log-manager' ),
381 'paginationActive' => __( 'Pagination is active. Auto-refresh has been disabled.', 'debug-log-manager' ),
382 ),
383 'dataTable' => array(
384 'emptyTable' => __( 'No data available in table', 'debug-log-manager' ),
385 'info' => __( 'Showing _START_ to _END_ of _TOTAL_ entries', 'debug-log-manager' ),
386 'infoEmpty' => __( 'Showing 0 to 0 of 0 entries', 'debug-log-manager' ),
387 'infoFiltered' => __( '(filtered from _MAX_ total entries)', 'debug-log-manager' ),
388 'lengthMenu' => __( 'Show _MENU_ entries', 'debug-log-manager' ),
389 'search' => __( 'Search:', 'debug-log-manager' ),
390 'zeroRecords' => __( 'No matching records found', 'debug-log-manager' ),
391 'paginate' => array(
392 'first' => __( 'First', 'debug-log-manager' ),
393 'last' => __( 'Last', 'debug-log-manager' ),
394 'next' => __( 'Next', 'debug-log-manager' ),
395 'previous' => __( 'Previous', 'debug-log-manager' ),
396 ),
397 ),
398 )
399 );
400
401 }
402
403 /**
404 * Scripts for WP plugin editor page
405 *
406 * @since 2.0.0
407 */
408 public function plugin_editor_scripts() {
409
410 wp_enqueue_style( 'dlm-plugin-theme-editor', DLM_URL . 'assets/css/plugin-theme-editor.css', array(), DLM_VERSION );
411 wp_enqueue_script( 'dlm-plugin-editor', DLM_URL . 'assets/js/plugin-editor.js', array( 'jquery', 'wp-theme-plugin-editor' ), DLM_VERSION, false );
412
413 }
414
415 /**
416 * Scripts for WP theme editor page
417 *
418 * @since 2.0.0
419 */
420 public function theme_editor_scripts() {
421
422 wp_enqueue_style( 'dlm-plugin-theme-editor', DLM_URL . 'assets/css/plugin-theme-editor.css', array(), DLM_VERSION );
423 wp_enqueue_script( 'dlm-theme-editor', DLM_URL . 'assets/js/theme-editor.js', array( 'jquery', 'wp-theme-plugin-editor' ), DLM_VERSION, false );
424
425 }
426
427 /**
428 * Admin bar icon's inline css
429 *
430 * @since 1.6.0
431 */
432 public function admin_bar_icon_css() {
433
434 // https://developer.wordpress.org/reference/functions/wp_add_inline_style/
435 wp_add_inline_style( 'admin-bar', '
436
437 #wpadminbar .dlm-admin-bar-icon .dashicons {
438 font-family: dashicons;
439 font-size: 20px;
440 width: 20px;
441 height: 20px;
442 line-height: 32px;
443 }
444
445 #wpadminbar .quicklinks ul li.dlm-admin-bar-icon a {
446 background: green;
447 }
448
449 #wpadminbar:not(.mobile) .ab-top-menu>li:hover>.ab-item {
450 transition: .25s;
451 }
452
453 #wpadminbar:not(.mobile) .ab-top-menu>li.dlm-admin-bar-icon:hover>.ab-item,
454 #wpadminbar:not(.mobile) .ab-top-menu>li.dlm-admin-bar-icon>.ab-item:focus {
455 background: #006600;
456 color: #fff;
457 }
458
459 ' );
460
461 }
462
463 /**
464 * Enqueue public scripts
465 *
466 * @since 1.4.0
467 */
468 public function public_scripts() {
469
470 wp_enqueue_script( 'dlm-public', DLM_URL . 'assets/js/public.js', array( 'jquery' ), DLM_VERSION, false );
471
472 $default_value = array(
473 'status' => 'disabled',
474 'on' => date( 'Y-m-d H:i:s' ),
475 );
476
477 $log_info = get_option( 'debug_log_manager', $default_value );
478 $log_status = $log_info['status']; // WP_DEBUG log status: enabled / disabled
479
480 wp_localize_script(
481 'dlm-public',
482 'dlmVars',
483 array(
484 'logStatus' => $log_status,
485 'jsErrorLogging' => array(
486 'status' => '',
487 'url' => admin_url( 'admin-ajax.php' ),
488 'nonce' => wp_create_nonce( DLM_SLUG ),
489 'action' => 'log_js_errors',
490 ),
491 )
492 );
493 }
494
495 }
496
497 Debug_Log_Manager::get_instance();