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