is_dlm() ) { // Update footer text add_filter( 'admin_footer_text', [ $this, 'footer_text' ] ); // Replace WP version text in footer add_filter( 'update_footer', [ $this, 'footer_version_text' ], 20 ); // Enqueue admin scripts and styles only on the plugin's main page add_action( 'admin_enqueue_scripts', [ $this, 'admin_scripts' ] ); } } // Enqueue admin scripts and styles on plugin editor page if ( 'plugin-editor.php' === $pagenow ) { add_action( 'admin_enqueue_scripts', [ $this, 'plugin_editor_scripts' ] ); } // Enqueue admin scripts and styles on theme editor page if ( 'theme-editor.php' === $pagenow ) { add_action( 'admin_enqueue_scripts', [ $this, 'theme_editor_scripts' ] ); } // 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. $default_value = array( 'status' => 'disabled', 'on' => date( 'Y-m-d H:i:s' ), ); $logging_info = get_option( 'debug_log_manager', $default_value ); $logging_status = $logging_info['status']; if ( ( $logging_status == 'enabled' ) && ! $this->is_dlm() ) { // https://developer.wordpress.org/reference/hooks/admin_bar_menu/ add_action( 'admin_bar_menu', [ $this, 'admin_bar_icon' ], 100 ); } // Add dashboard widget add_action( 'wp_dashboard_setup', [ $this, 'add_dashboard_widget' ] ); // Add inline CSS for the admin bar icon (menu item) add_action( 'admin_enqueue_scripts', [ $this, 'admin_bar_icon_css' ] ); add_action( 'wp_enqueue_scripts', [ $this, 'admin_bar_icon_css' ] ); // Enqueue public scripts and styles add_action( 'wp_enqueue_scripts', [ $this, 'public_scripts' ] ); // Schedule log trim cron for existing installs upgraded without re-activation. if ( ! wp_next_scheduled( 'dlm_trim_debug_log' ) ) { wp_schedule_event( time(), 'hourly', 'dlm_trim_debug_log' ); } $this->log_trimmer = new DLM\Classes\Log_Trimmer; add_action( 'dlm_trim_debug_log', [ $this->log_trimmer, 'maybe_trim_log' ] ); // Register ajax calls $this->debug_log = new DLM\Classes\Debug_Log; $this->wp_config = new DLM\Classes\WP_Config_Transformer; add_action( 'wp_ajax_toggle_debugging', [ $this->debug_log, 'toggle_debugging' ] ); add_action( 'wp_ajax_toggle_autorefresh', [ $this->debug_log, 'toggle_autorefresh' ] ); add_action( 'wp_ajax_get_latest_entries', [ $this->debug_log, 'get_latest_entries' ] ); add_action( 'wp_ajax_clear_log', [ $this->debug_log, 'clear_log' ] ); add_action( 'wp_ajax_disable_wp_file_editor', [ $this->debug_log, 'disable_wp_file_editor' ] ); add_action( 'wp_ajax_toggle_js_error_logging', [ $this->debug_log, 'toggle_js_error_logging' ] ); add_action( 'wp_ajax_toggle_script_debug_modification_status', [ $this->debug_log, 'toggle_script_debug_modification_status' ] ); add_action( 'wp_ajax_toggle_process_non_utc_timezones_status', [ $this->debug_log, 'toggle_process_non_utc_timezones_status' ] ); add_action( 'wp_ajax_log_js_errors', [ $this->debug_log, 'log_js_errors' ] ); add_action( 'wp_ajax_nopriv_log_js_errors', [ $this->debug_log, 'log_js_errors' ] ); } /** * Check if current screen is this plugin's main page * * @since 1.0.0 */ public function is_dlm() { $request_uri = sanitize_text_field( $_SERVER['REQUEST_URI'] ); // e.g. /wp-admin/index.php?page=page-slug if ( strpos( $request_uri, 'tools.php?page=' . DLM__SLUG ) !== false ) { return true; // Yes, this is the plugin's main page } else { return false; // Nope, this is NOT the plugin's page } } /** * Register admin menu * * @since 1.0.0 */ public function register_admin_menu() { add_submenu_page( 'tools.php', __( 'Debug Log Manager', 'debug-log-manager' ), __( 'Debug Log Manager', 'debug-log-manager' ), 'manage_options', 'debug-log-manager', [ $this, 'create_main_page' ] ); } /** * Register action links * * @since 1.0.0 */ public function action_links( $links ) { $settings_link = '' . esc_html__( 'View Debug Log', 'debug-log-manager' ) . ''; array_unshift($links, $settings_link); return $links; } /** * Change admin footer text * * @since 1.0.0 */ public function footer_text() { ?> is on github WordPress Newsboard: The latest from 100+ sources'; } /** * Add debug icon in the admin bar * * @since 1.6.0 */ public function admin_bar_icon( WP_Admin_Bar $wp_admin_bar ) { $current_user = wp_get_current_user(); $current_user_roles = array_values( $current_user->roles ); // indexed array if ( in_array( 'administrator', $current_user_roles ) ) { // https://developer.wordpress.org/reference/classes/wp_admin_bar/add_menu/ // https://developer.wordpress.org/reference/classes/wp_admin_bar/add_node/ for more examples $wp_admin_bar->add_menu( array( 'id' => DLM__SLUG, 'parent' => 'top-secondary', 'group' => null, 'title' => '', 'href' => admin_url( 'tools.php?page=' . DLM__SLUG ), 'meta' => array( 'class' => 'dlm-admin-bar-icon', 'title' => esc_attr__( 'Error logging is enabled. Click to access the Debug Log Manager.', 'debug-log-manager' ) ), ) ); } } /** * Create the main admin page of the plugin * * @since 1.0.0 */ public function create_main_page() { $log_file_path = get_option( 'debug_log_manager_file_path' ); $log_file_shortpath = str_replace( sanitize_text_field( $_SERVER['DOCUMENT_ROOT'] ), "", $log_file_path ); $file_size = size_format( (int) filesize( $log_file_path ) ); ?>
roles ); if ( in_array( 'administrator', $roles ) ) { wp_add_dashboard_widget( 'debug_log_manager_widget', // widget ID __( 'Debug Log | Latest Errors', 'debug-log-manager' ), // widget title array( $this, 'get_dashboard_widget_entries' ) // callback #1 to display entries // array( $this, 'dashboard_widget_settings' ) // callback #2 for configuration ); } } /** * Load latest errors for dashboard widget * * @since 1.8.0 */ public function get_dashboard_widget_entries() { $this->debug_log->get_dashboard_widget_entries(); } /** * Enqueue admin scripts * * @since 1.0.0 */ public function admin_scripts() { wp_enqueue_style( 'dlm-admin', DLM__URL . 'assets/css/admin.css', array(), DLM__VERSION ); wp_enqueue_style( 'dlm-datatables', DLM__URL . 'assets/css/datatables.min.css', array(), DLM__VERSION ); wp_enqueue_style( 'dlm-toast', DLM__URL . 'assets/css/jquery.toast.min.css', array(), DLM__VERSION ); wp_enqueue_script( 'dlm-app', DLM__URL . 'assets/js/admin.js', array(), DLM__VERSION, false ); wp_enqueue_script( 'dlm-jsticky', DLM__URL . 'assets/js/jquery.jsticky.mod.min.js', array( 'jquery' ), DLM__VERSION, false ); wp_enqueue_script( 'dlm-datatables', DLM__URL . 'assets/js/datatables.min.js', array( 'jquery' ), DLM__VERSION, false ); wp_enqueue_script( 'dlm-toast', DLM__URL . 'assets/js/jquery.toast.min.js', array( 'jquery' ), DLM__VERSION, false ); // Pass on data from PHP to JS $default_value = array( 'status' => 'disabled', 'on' => date( 'Y-m-d H:i:s' ), ); $log_info = get_option( 'debug_log_manager', $default_value ); $log_status = $log_info['status']; // WP_DEBUG log status: enabled / disabled if ( false !== get_option( 'debug_log_manager_autorefresh' ) ) { $autorefresh_status = get_option( 'debug_log_manager_autorefresh' ); } else { $autorefresh_status = 'disabled'; update_option( 'debug_log_manager_autorefresh', $autorefresh_status, false ); } $js_error_logging_status = get_option( 'debug_log_manager_js_error_logging', 'enabled' ); $modify_script_debug_status = get_option( 'debug_log_manager_modify_script_debug', 'enabled' ); $process_non_utc_timezones_status = get_option( 'debug_log_manager_process_non_utc_timezones', 'enabled' ); $title_of_column_to_filter = __( 'Type', 'debug-log-manager' ); $nonce = wp_create_nonce( 'dlm-app' . get_current_user_id() ); wp_localize_script( 'dlm-app', 'dlmVars', array( 'logStatus' => $log_status, 'autorefreshStatus' => $autorefresh_status, 'jsErrorLoggingStatus' => $js_error_logging_status, 'modifyScriptDebugStatus' => $modify_script_debug_status, 'processNonUtcTimezonesStatus' => $process_non_utc_timezones_status, 'titleOfColumnToFilter' => $title_of_column_to_filter, 'nonce' => $nonce, 'jsErrorLogging' => array( 'status' => '', 'url' => admin_url( 'admin-ajax.php' ), 'nonce' => wp_create_nonce( DLM__SLUG ), 'action' => 'log_js_errors', ), 'toastMessage' => array( 'toggleDebugSuccess' => __( 'Error logging has been enabled and the latest entries have been loaded.', 'debug-log-manager' ), 'copySuccess' => __( 'Entries have been copied from an existing debug.log file.', 'debug-log-manager' ), 'logFileCleared' => __( 'Log file has been cleared.', 'debug-log-manager' ), 'editoDisabled' => __( 'WordPress plugin/theme editor has been disabled. ', 'debug-log-manager' ), 'paginationActive' => __( 'Pagination is active. Auto-refresh has been disabled.', 'debug-log-manager' ), ), 'dataTable' => array( 'emptyTable' => __( 'No data available in table', 'debug-log-manager' ), 'info' => __( 'Showing _START_ to _END_ of _TOTAL_ entries', 'debug-log-manager' ), 'infoEmpty' => __( 'Showing 0 to 0 of 0 entries', 'debug-log-manager' ), 'infoFiltered' => __( '(filtered from _MAX_ total entries)', 'debug-log-manager' ), 'lengthMenu' => __( 'Show _MENU_ entries', 'debug-log-manager' ), 'search' => __( 'Search:', 'debug-log-manager' ), 'zeroRecords' => __( 'No matching records found', 'debug-log-manager' ), 'paginate' => array( 'first' => __( 'First', 'debug-log-manager' ), 'last' => __( 'Last', 'debug-log-manager' ), 'next' => __( 'Next', 'debug-log-manager' ), 'previous' => __( 'Previous', 'debug-log-manager' ), ), ), ) ); } /** * Scripts for WP plugin editor page * * @since 2.0.0 */ public function plugin_editor_scripts() { wp_enqueue_style( 'dlm-plugin-theme-editor', DLM__URL . 'assets/css/plugin-theme-editor.css', array(), DLM__VERSION ); wp_enqueue_script( 'dlm-plugin-editor', DLM__URL . 'assets/js/plugin-editor.js', array( 'jquery', 'wp-theme-plugin-editor' ), DLM__VERSION, false ); } /** * Scripts for WP theme editor page * * @since 2.0.0 */ public function theme_editor_scripts() { wp_enqueue_style( 'dlm-plugin-theme-editor', DLM__URL . 'assets/css/plugin-theme-editor.css', array(), DLM__VERSION ); wp_enqueue_script( 'dlm-theme-editor', DLM__URL . 'assets/js/theme-editor.js', array( 'jquery', 'wp-theme-plugin-editor' ), DLM__VERSION, false ); } /** * Admin bar icon's inline css * * @since 1.6.0 */ public function admin_bar_icon_css() { // https://developer.wordpress.org/reference/functions/wp_add_inline_style/ wp_add_inline_style( 'admin-bar', ' #wpadminbar .dlm-admin-bar-icon .dashicons { font-family: dashicons; font-size: 20px; width: 20px; height: 20px; line-height: 32px; } #wpadminbar .quicklinks ul li.dlm-admin-bar-icon a { background: green; } #wpadminbar:not(.mobile) .ab-top-menu>li:hover>.ab-item { transition: .25s; } #wpadminbar:not(.mobile) .ab-top-menu>li.dlm-admin-bar-icon:hover>.ab-item, #wpadminbar:not(.mobile) .ab-top-menu>li.dlm-admin-bar-icon>.ab-item:focus { background: #006600; color: #fff; } ' ); } /** * Enqueue public scripts * * @since 1.4.0 */ public function public_scripts() { $log_info_default_value = array( 'status' => 'disabled', 'on' => date( 'Y-m-d H:i:s' ), ); $log_info = get_option( 'debug_log_manager', $log_info_default_value ); $log_status = $log_info['status']; // WP_DEBUG log status: enabled / disabled $js_error_logging_status = get_option( 'debug_log_manager_js_error_logging', 'enabled' ); // enabled | disabled if ( 'enabled' == $log_status && 'enabled' == $js_error_logging_status ) { wp_enqueue_script( 'dlm-public', DLM__URL . 'assets/js/public.js', array( 'jquery' ), DLM__VERSION, false ); wp_localize_script( 'dlm-public', 'dlmVars', array( 'logStatus' => $log_status, 'jsErrorLogging' => array( 'status' => '', 'url' => admin_url( 'admin-ajax.php' ), 'nonce' => wp_create_nonce( DLM__SLUG ), 'action' => 'log_js_errors', ), ) ); } } } Debug_Log_Manager::get_instance();