← Back
Editing: debug.php
<?php /** * Uncanny_Automator\Admin_Tools_Debug_Debug * * @since 4.5 */ namespace Uncanny_Automator; if ( ! defined( 'ABSPATH' ) ) { exit; } /** * */ class Admin_Tools_Debug_Debug { /** * */ const SETTINGS_GROUP_NAME = 'uncanny_automator_settings_debug'; /** * */ public function __construct() { // Define the tab $this->create_tab(); // Define dynamic logs tab. $this->create_dynamic_logs_tab(); // Register the debug option. add_action( 'admin_init', array( $this, 'register_settings' ) ); } /** * @return void */ public function register_settings() { // The hidden Warnings and notices switch is excluded so WordPress leaves its value alone. register_setting( self::SETTINGS_GROUP_NAME, 'automator_settings_debug_enabled', array( 'sanitize_callback' => array( $this, 'save_debug_enabled' ), ) ); } /** * Saves the Logs switch where the plugin reads it. * * @since 8.1.1 * * @param mixed $value The posted value; null when the switch is off. * * @return int 1 or 0. */ public function save_debug_enabled( $value ) { return $this->save_switch( 'automator_settings_debug_enabled', $value ); } /** * Writes a switch to uap_options as well as letting WordPress write wp_options. * * The form saves through options.php, which writes wp_options only, and automator_get_option() * reads the uap_options row first. The options migration copied both switches into uap_options, * so without this write a migrated site's switch never changes. It runs as the sanitize callback * because WordPress calls that on every save, even when the wp_options value is unchanged. * * @since 8.1.1 * * @param string $option The option name. * @param mixed $value The posted value. * * @return int 1 or 0. */ private function save_switch( $option, $value ) { $value = absint( $value ); automator_update_option( $option, $value ); return $value; } /** * @return void */ private function create_dynamic_logs_tab() { $log_files = $this->get_log_file_names(); if ( ! empty( $log_files ) ) { foreach ( $log_files as $file ) { add_filter( 'automator_admin_tools_debug_tabs', function ( $tabs ) use ( $file ) { $tab_id = sanitize_file_name( $file ); $ext = pathinfo( $tab_id, PATHINFO_EXTENSION ); $allowed_file_types = array( AUTOMATOR_LOGS_EXT ); if ( in_array( $ext, $allowed_file_types, true ) ) { $tabs[ $tab_id ] = (object) array( 'name' => str_replace( array( '.' . AUTOMATOR_LOGS_EXT, ), '', $tab_id ), 'function' => array( $this, 'tab_log_output' ), 'preload' => false, ); } return $tabs; }, 20, 1 ); } } } /** * Adds the tab using the automator_settings_tab filter */ private function create_tab() { // Add the tab using the filter add_filter( 'automator_admin_tools_debug_tabs', function ( $tabs ) { $tabs['debug'] = (object) array( 'name' => esc_html__( 'Settings', 'uncanny-automator' ), 'function' => array( $this, 'tab_output' ), 'preload' => false, ); return $tabs; }, 10, 1 ); } /** * Outputs the content of the "General" tab */ public function tab_output() { // Load the view include Utilities::automator_get_view( 'admin-tools/tab/debug/debug.php' ); } /** * @return void */ public function tab_log_output() { include Utilities::automator_get_view( 'admin-tools/tab/debug/log-viewer.php' ); } /** * @return array|false|string[] */ public function get_log_file_names() { if ( ! defined( 'UA_DEBUG_LOGS_DIR' ) || ! is_dir( UA_DEBUG_LOGS_DIR ) ) { return false; } require_once trailingslashit( ABSPATH ) . 'wp-admin/includes/file.php'; return array_map( function ( $file ) { return basename( $file ); }, list_files( UA_DEBUG_LOGS_DIR, 1, array() ) ); } /** * @return false|string */ public function get_requested_log() { $requested_log = sanitize_file_name( automator_filter_input( 'debug' ) ); if ( empty( $requested_log ) ) { return false; } $dir = trailingslashit( UA_DEBUG_LOGS_DIR ); $log_file = $dir . $requested_log; $ext = pathinfo( $log_file, PATHINFO_EXTENSION ); if ( is_file( $log_file ) && in_array( $ext, array( AUTOMATOR_LOGS_EXT ), true ) ) { return file_get_contents( $log_file, false, null, 0, 10000 ); } return false; } } new Admin_Tools_Debug_Debug();
Save File
Cancel