false, // Display debug information? 'debug_ips' => '', // List of IP addresses that can see debug information, when debug is enabled. 'debug_to_browser' => false, // Display debug info in the browser? 'debug_to_file' => false, // Save debug info to a file. 3.1 In the settings form, display the inputs: $this->add_debug_settings_to_form( $form ); 3.2 In the settings form, save your settings. $this->save_debug_settings_from_form( $form ); @since 2014-05-01 08:56:20 **/ trait debug { /** @brief Adds the debug settings to a form. @since 2014-05-01 08:57:39 **/ public function add_debug_settings_to_form( $form ) { // We need this so that the options use the correct namespace. $instance = self::instance(); $fs = $form->fieldset( 'fs_debug' ); $fs->legend->label_( 'Debugging' ); // You are currently NOT in debug mode. $not = $this->_( 'not' ); $fs->markup( 'debug_info' ) ->p_( "According to the settings below, you are currently%s in debug mode. Don't forget to reload this page after saving the settings.", $this->debugging() ? '' : " $not" ); $debug = $fs->checkbox( 'debug' ) ->description_( 'Show debugging information in various places.' ) ->label_( 'Enable debugging' ) ->checked( $instance->get_site_option( 'debug', false ) ); $fs->checkbox( 'debug_to_browser' ) ->description_( 'Show the debugging information in the browser.' ) ->label_( 'Show debug in the browser' ) ->checked( $instance->get_site_option( 'debug_to_browser', false ) ); $debug_to_file = $fs->checkbox( 'debug_to_file' ) ->label_( 'Save debug to file' ) ->checked( $instance->get_site_option( 'debug_to_file', false ) ); // We need to set the description unescaped due to the link. $filename = $this->get_debug_filename(); $basename = basename( $filename ); $filename = sprintf( '%s', $this->paths( 'url' ) . '/' . $basename, $basename ); $description = $this->_( 'The debug data will be saved to the file %s. This link is distributable.', $filename ); $debug_to_file->description ->get_label() ->content = $description; $fs->checkbox( 'delete_debug_file' ) ->description_( 'Delete the contents of the debug file now after saving the settings.' ) ->label_( 'Delete debug file' ) ->checked( false ); $fs->textarea( 'debug_ips' ) ->description_( 'Only show debugging info to specific IP addresses. Use spaces between IPs. You can also specify part of an IP address. Your address is %s', $_SERVER[ 'REMOTE_ADDR' ] ) ->label_( 'Debug IPs' ) ->rows( 5, 16 ) ->trim() ->value( $instance->get_site_option( 'debug_ips', '' ) ); } /** @brief Output a string if in debug mode. @since 20140220 */ public function debug( $string ) { if ( ! $this->debugging() ) return; // Convert the non-string arguments into lovely code blocks. $args = func_get_args(); foreach( $args as $index => $arg ) { $export = false; $export |= is_array( $arg ); $export |= is_object( $arg ); if ( $export ) $args[ $index ] = sprintf( '
%s', htmlspecialchars( var_export( $arg, true ) ) );
}
// Put all of the arguments into one string.
$text = call_user_func_array( 'sprintf', $args );
if ( $text == '' )
$text = $string;
// We want the name of the class.
$class_name = get_called_class();
// But without the namespace
$class_name = preg_replace( '/.*\\\/', '', $class_name );
// Date class: string
$text = sprintf( '%s.%s %s: %s