| 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 |
/** |
| 22 |
* Creates or returns a single instance of this class |
| 23 |
* |
| 24 |
* @return Debug_Log_Manager a single instance of this class. |
| 25 |
*/ |
| 26 |
public static function get_instance() { |
| 27 |
|
| 28 |
if ( null == self::$instance ) { |
| 29 |
self::$instance = new self; |
| 30 |
} |
| 31 |
|
| 32 |
return self::$instance; |
| 33 |
|
| 34 |
} |
| 35 |
|
| 36 |
/** |
| 37 |
* Initialize plugin functionalities |
| 38 |
*/ |
| 39 |
private function __construct() { |
| 40 |
|
| 41 |
// Register admin menu and subsequently the main admin page |
| 42 |
add_action( 'admin_menu', [ $this, 'register_admin_menu' ] ); |
| 43 |
|
| 44 |
// Add action links |
| 45 |
add_filter( 'plugin_action_links_'.DLM_SLUG.'/'.DLM_SLUG.'.php', [ $this, 'action_links' ] ); |
| 46 |
|
| 47 |
if ( is_admin() && $this->is_dlm() ) { |
| 48 |
|
| 49 |
// Update footer text |
| 50 |
add_filter( 'admin_footer_text', [ $this, 'footer_text' ] ); |
| 51 |
|
| 52 |
// Enqueue admin scripts and styles only on the plugin's main page |
| 53 |
add_action( 'admin_enqueue_scripts', [ $this, 'admin_scripts' ] ); |
| 54 |
|
| 55 |
} |
| 56 |
|
| 57 |
// Enqueue public scripts and styles |
| 58 |
add_action( 'wp_enqueue_scripts', [ $this, 'public_scripts' ] ); |
| 59 |
|
| 60 |
// Register ajax calls |
| 61 |
$this->debug_log = new DLM\Classes\Debug_Log; |
| 62 |
add_action( 'wp_ajax_toggle_debugging', [ $this->debug_log, 'toggle_debugging' ] ); |
| 63 |
add_action( 'wp_ajax_toggle_autorefresh', [ $this->debug_log, 'toggle_autorefresh' ] ); |
| 64 |
add_action( 'wp_ajax_get_latest_entries', [ $this->debug_log, 'get_latest_entries' ] ); |
| 65 |
add_action( 'wp_ajax_clear_log', [ $this->debug_log, 'clear_log' ] ); |
| 66 |
add_action( 'wp_ajax_log_js_errors', [ $this->debug_log, 'log_js_errors' ] ); |
| 67 |
add_action( 'wp_ajax_nopriv_log_js_errors', [ $this->debug_log, 'log_js_errors' ] ); |
| 68 |
|
| 69 |
} |
| 70 |
|
| 71 |
/** |
| 72 |
* Check if current screen is this plugin's main page |
| 73 |
* |
| 74 |
* @since 1.0.0 |
| 75 |
*/ |
| 76 |
public function is_dlm() { |
| 77 |
|
| 78 |
$request_uri = sanitize_text_field( $_SERVER['REQUEST_URI'] ); // e.g. /wp-admin/index.php?page=page-slug |
| 79 |
|
| 80 |
if ( strpos( $request_uri, 'tools.php?page=' . DLM_SLUG ) !== false ) { |
| 81 |
return true; // Yes, this is the plugin's main page |
| 82 |
} else { |
| 83 |
return false; // Nope, this is NOT the plugin's page |
| 84 |
} |
| 85 |
|
| 86 |
} |
| 87 |
|
| 88 |
/** |
| 89 |
* Register admin menu |
| 90 |
* |
| 91 |
* @since 1.0.0 |
| 92 |
*/ |
| 93 |
public function register_admin_menu() { |
| 94 |
|
| 95 |
add_submenu_page( |
| 96 |
'tools.php', |
| 97 |
'Debug Log Manager', |
| 98 |
'Debug Log Manager', |
| 99 |
'manage_options', |
| 100 |
'debug-log-manager', |
| 101 |
[ $this, 'create_main_page' ] |
| 102 |
); |
| 103 |
|
| 104 |
} |
| 105 |
|
| 106 |
/** |
| 107 |
* Register action links |
| 108 |
* |
| 109 |
* @since 1.0.0 |
| 110 |
*/ |
| 111 |
public function action_links( $links ) { |
| 112 |
|
| 113 |
$settings_link = '<a href="tools.php?page='.DLM_SLUG.'">View Debug Log</a>'; |
| 114 |
|
| 115 |
array_unshift($links, $settings_link); |
| 116 |
|
| 117 |
return $links; |
| 118 |
|
| 119 |
} |
| 120 |
|
| 121 |
/** |
| 122 |
* Change admin footer text |
| 123 |
* |
| 124 |
* @since 1.0.0 |
| 125 |
*/ |
| 126 |
public function footer_text() { |
| 127 |
?> |
| 128 |
<a href="https://wordpress.org/plugins/debug-log-manager/" target="_blank">Debug Log Manager</a> (<a href="https://github.com/qriouslad/debug-log-manager" target="_blank">github</a>) is built using <a href="https://datatables.net/" target="_blank">DataTables.js</a>, <a href="https://github.com/AndrewHenderson/jSticky" target="_blank">jSticky</a> and <a href="https://github.com/kamranahmedse/jquery-toast-plugin" target="_blank">jQuery Toast</a>. |
| 129 |
<?php |
| 130 |
} |
| 131 |
|
| 132 |
/** |
| 133 |
* Create the main admin page of the plugin |
| 134 |
* |
| 135 |
* @since 1.0.0 |
| 136 |
*/ |
| 137 |
public function create_main_page() { |
| 138 |
|
| 139 |
$log_file_path = get_option( 'debug_log_manager_file_path' ); |
| 140 |
$log_file_shortpath = str_replace( sanitize_text_field( $_SERVER['DOCUMENT_ROOT'] ), "", $log_file_path ); |
| 141 |
$file_size = size_format( (int) filesize( $log_file_path ) ); |
| 142 |
|
| 143 |
?> |
| 144 |
|
| 145 |
<div class="wrap dlm-main-page"> |
| 146 |
<div id="dlm-header" class="dlm-header"> |
| 147 |
<div class="dlm-header-left"> |
| 148 |
<h1 class="dlm-heading">Debug Log Manager <small>by <a href="https://bowo.io" target="_blank">bowo.io</a></small></h1> |
| 149 |
</div> |
| 150 |
<div class="dlm-header-right"> |
| 151 |
<a href="https://wordpress.org/plugins/debug-log-manager/" target="_blank" class="dlm-header-action"><span>ℹ</span> Info</a> |
| 152 |
<a href="https://wordpress.org/plugins/debug-log-manager/#reviews" target="_blank" class="dlm-header-action"><span>� |
| 153 |
</span> Review</a> |
| 154 |
<a href="https://wordpress.org/support/plugin/debug-log-manager/" target="_blank" class="dlm-header-action">✚ Feedback</a> |
| 155 |
<a href="https://paypal.me/qriouslad" target="_blank" class="dlm-header-action">❤ Donate</a> |
| 156 |
</div> |
| 157 |
</div> |
| 158 |
<div class="dlm-body"> |
| 159 |
<div class="dlm-log-management"> |
| 160 |
<div class="dlm-logging-status"> |
| 161 |
<div class="dlm-log-status-toggle"> |
| 162 |
<input type="checkbox" id="debug-log-checkbox" class="inset-3 debug-log-checkbox"><label for="debug-log-checkbox" class="green debug-log-switcher"></label> |
| 163 |
</div> |
| 164 |
<?php echo $this->debug_log->get_status(); ?> |
| 165 |
</div> |
| 166 |
<div class="dlm-autorefresh-status"> |
| 167 |
<div class="dlm-log-autorefresh-toggle"> |
| 168 |
<input type="checkbox" id="debug-autorefresh-checkbox" class="inset-3 debug-autorefresh-checkbox"><label for="debug-autorefresh-checkbox" class="green debug-autorefresh-switcher"></label> |
| 169 |
</div> |
| 170 |
<?php echo $this->debug_log->get_autorefresh_status(); ?> |
| 171 |
</div> |
| 172 |
</div> |
| 173 |
<?php |
| 174 |
$this->debug_log->get_entries_datatable(); |
| 175 |
?> |
| 176 |
</div> |
| 177 |
<div class="dlm-footer"> |
| 178 |
<div class="dlm-log-file"><strong>Log file</strong>: <?php echo esc_html( $log_file_shortpath ); ?> (<span id="dlm-log-file-size"><?php echo esc_html( $file_size ); ?></span>)</div> |
| 179 |
<button id="dlm-log-clear" class="button button-small button-secondary dlm-log-clear">Clear Log</button> |
| 180 |
</div> |
| 181 |
</div> |
| 182 |
|
| 183 |
<?php |
| 184 |
|
| 185 |
} |
| 186 |
|
| 187 |
/** |
| 188 |
* Enqueue admin scripts |
| 189 |
* |
| 190 |
* @since 1.0.0 |
| 191 |
*/ |
| 192 |
public function admin_scripts() { |
| 193 |
|
| 194 |
wp_enqueue_style( 'dlm-admin', DLM_URL . 'assets/css/admin.css', array(), DLM_VERSION ); |
| 195 |
wp_enqueue_style( 'dlm-datatables', DLM_URL . 'assets/css/datatables.min.css', array(), DLM_VERSION ); |
| 196 |
wp_enqueue_style( 'dlm-toast', DLM_URL . 'assets/css/jquery.toast.min.css', array(), DLM_VERSION ); |
| 197 |
wp_enqueue_script( 'dlm-app', DLM_URL . 'assets/js/app.js', array(), DLM_VERSION, false ); |
| 198 |
wp_enqueue_script( 'dlm-jsticky', DLM_URL . 'assets/js/jquery.jsticky.min.js', array( 'jquery' ), DLM_VERSION, false ); |
| 199 |
wp_enqueue_script( 'dlm-datatables', DLM_URL . 'assets/js/datatables.min.js', array( 'jquery' ), DLM_VERSION, false ); |
| 200 |
wp_enqueue_script( 'dlm-toast', DLM_URL . 'assets/js/jquery.toast.min.js', array( 'jquery' ), DLM_VERSION, false ); |
| 201 |
|
| 202 |
// Pass on data from PHP to JS |
| 203 |
|
| 204 |
$log_info = get_option( 'debug_log_manager' ); |
| 205 |
$log_status = $log_info['status']; // WP_DEBUG log status: enabled / disabled |
| 206 |
|
| 207 |
if ( false !== get_option( 'debug_log_manager_autorefresh' ) ) { |
| 208 |
$autorefresh_status = get_option( 'debug_log_manager_autorefresh' ); |
| 209 |
} else { |
| 210 |
$autorefresh_status = 'disabled'; |
| 211 |
update_option( 'debug_log_manager_autorefresh', $autorefresh_status, false ); |
| 212 |
} |
| 213 |
|
| 214 |
wp_localize_script( |
| 215 |
'dlm-app', |
| 216 |
'dlmVars', |
| 217 |
array( |
| 218 |
'logStatus' => $log_status, |
| 219 |
'autorefreshStatus' => $autorefresh_status, |
| 220 |
'jsErrorLogging' => array( |
| 221 |
'status' => '', |
| 222 |
'url' => admin_url( 'admin-ajax.php' ), |
| 223 |
'nonce' => wp_create_nonce( DLM_SLUG ), |
| 224 |
'action' => 'log_js_errors', |
| 225 |
), |
| 226 |
) |
| 227 |
); |
| 228 |
|
| 229 |
} |
| 230 |
|
| 231 |
/** |
| 232 |
* Enqueue public scripts |
| 233 |
* |
| 234 |
* @since 1.4.0 |
| 235 |
*/ |
| 236 |
public function public_scripts() { |
| 237 |
|
| 238 |
wp_enqueue_script( 'dlm-public', DLM_URL . 'assets/js/public.js', array( 'jquery' ), DLM_VERSION, false ); |
| 239 |
|
| 240 |
$log_info = get_option( 'debug_log_manager' ); |
| 241 |
$log_status = $log_info['status']; // WP_DEBUG log status: enabled / disabled |
| 242 |
|
| 243 |
wp_localize_script( |
| 244 |
'dlm-public', |
| 245 |
'dlmVars', |
| 246 |
array( |
| 247 |
'logStatus' => $log_status, |
| 248 |
'jsErrorLogging' => array( |
| 249 |
'status' => '', |
| 250 |
'url' => admin_url( 'admin-ajax.php' ), |
| 251 |
'nonce' => wp_create_nonce( DLM_SLUG ), |
| 252 |
'action' => 'log_js_errors', |
| 253 |
), |
| 254 |
) |
| 255 |
); |
| 256 |
} |
| 257 |
|
| 258 |
} |
| 259 |
|
| 260 |
Debug_Log_Manager::get_instance(); |