WordPress.org

Plugin Directory

Changeset 432272


Ignore:
Timestamp:
09/02/11 13:06:13 (3 years ago)
Author:
tott
Message:
  • added filters to control settings debugbarextender_default_settings, debugbarextender_default_checkpoint_actions
  • added wrapper functions dbgx_checkpoint( $note = ) and dbgx_trace_var( $value, $var_name = false ) for easier access to debugging functions.
  • added new setting option to allow control of some settings via $_GET parameters in the urls. Allows dbgx_use_error_log, dbgx_track_default_vars, dbgx_add_default_checkpoints to be added as query strings in order to enable respective features. Combinations are possible - for example: /wp-admin/options-general.php?page=debug-bar-extender&dbgx_use_error_log=1&dbgx_add_default_checkpoints=1&dbgx_track_default_vars=1
  • added new setting option to enable a admin bar menu with shortcuts to this urls
Location:
debug-bar-extender/trunk
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • debug-bar-extender/trunk/debug-bar-extender.php

    r425345 r432272  
    55 Description: A minimalistic profiler / debugging class that hooks into the debug bar and can be implemented easily 
    66 Author: Thorsten Ott, Automattic 
    7  Version: 0.3 
    8  Author URI: http://hitchhacker.com 
     7 Version: 0.5 
     8 Author URI: http://hitchhackerguide.com 
    99 */ 
    1010  
     
    1818 * <?php if (have_posts()) : ?> 
    1919 * 
    20  * <?php Debug_Bar_Extender::instance()->start('loop start'); ?> 
     20 * <?php dbgx_checkpoint('loop start'); ?> 
    2121 * 
    2222 * <?php while (have_posts()) : the_post(); ?> 
    2323 *  
    24  * <?php Debug_Bar_Extender::instance()->trace_var( $post ); ?> 
    25  * <?php Debug_Bar_Extender::instance()->checkpoint('loop1'); ?> 
     24 * <?php dbgx_trace_var( $post ); ?> 
     25 * <?php dbgx_checkpoint('loop1'); ?> 
    2626 * 
    2727 * <div <?php post_class() ?> id="post-<?php the_ID(); ?>"> 
     
    3636 * </div> 
    3737 * 
    38  * <?php Debug_Bar_Extender::instance()->checkpoint('loop2'); ?> 
     38 * <?php dbgx_checkpoint('loop2'); ?> 
    3939 * 
    4040 * <?php endwhile; ?> 
    4141 * 
    42  * <?php Debug_Bar_Extender::instance()->end('loop end'); ?> 
     42 * <?php dbgx_checkpoint('loop end'); ?> 
    4343 * 
    4444 * <div class="navigation"> 
     
    4848 * <?php else : ?> 
    4949 * ... 
    50  * 
    51  * You can have as many Debug_Bar_Extender::instance()->checkpoint() calls as you want but must have only 1 Debug_Bar_Extender::instance()->start() and Debug_Bar_Extender::instance()->end() call 
    5250 * 
    5351 */ 
     
    8482            'savequeries_debugmode_enable'  => 1, 
    8583            'use_error_log'                 => 0, 
     84            'allow_url_settings'            => 0, 
     85            'enable_admin_bar_menu'         => 0, 
    8686        ) ); 
    8787         
    8888        $this->settings_texts = (array) apply_filters( 'debugbarextender_settings_texts', array( 
    89             'enable_profiler'               => array( 'label' => 'Enable profiler', 'desc' => 'You can measure runtimes between checkpoints via <code>Debug_Bar_Extender::instance()->start( $note="" )</code>, <code>Debug_Bar_Extender::instance()->checkpoint( $note="" )</code>,  <code>Debug_Bar_Extender::instance()->end( $note="" )</code>.' ), 
    90             'enable_variable_lookup'        => array( 'label' => 'Enable variable lookup', 'desc' => 'You can screen variables via <code>Debug_Bar_Extender::instance()->trace_var( $value, $var_name = false )</code>. Omitting the <code>$var_name</code> will let the script look it up. Please be aware that sizes are approximate based on <code>strlen(serialize())</code>' ), 
     89            'enable_profiler'               => array( 'label' => 'Enable profiler', 'desc' => 'You can measure runtimes between checkpoints via <code>Debug_Bar_Extender::instance()->start( $note="" )</code>, <code>Debug_Bar_Extender::instance()->checkpoint( $note="" )</code>,  <code>Debug_Bar_Extender::instance()->end( $note="" )</code> or by using the wrapper function <code>dbgx_checkpoint( $note="" )</code>.' ), 
     90            'enable_variable_lookup'        => array( 'label' => 'Enable variable lookup', 'desc' => 'You can screen variables via <code>Debug_Bar_Extender::instance()->trace_var( $value, $var_name = false )</code> or via <code>dbgx_trace_var( $value, $var_name = false )</code>. Omitting the <code>$var_name</code> will let the script look it up. Please be aware that sizes are approximate based on <code>strlen(serialize())</code>' ), 
    9191            'track_default_vars'            => array( 'label' => 'Track default variables', 'desc' => 'Track various useful variables useful for the regular debugging.' ), 
    9292            'add_default_checkpoints'       => array( 'label' => 'Add default checkpoints', 'desc' => 'Enabling this option will add various default checkpoints to be used with the profiler.' ), 
    9393            'savequeries_debugmode_enable'  => array( 'label' => 'Try setting debug mode', 'desc' => 'Try setting <code>WP_DEBUG</code> and <code>SAVEQUERIES</code> variables to enable additional debug menus. This should be generally done via <code>wp-config.php</code>, but this setting is worth a shot.' ), 
    9494            'use_error_log'                 => array( 'label' => 'Use <code>error_log()</code> reporting', 'desc' => 'Log the results using <code>error_log()</code> as well. This helps when debugging sessions which are not producing any admin bar output.' ), 
     95            'allow_url_settings'            => array( 'label' => 'Allow setting alteration via query parameters', 'desc' => 'Allow <code>dbgx_use_error_log</code>, <code>dbgx_track_default_vars</code>, <code>dbgx_add_default_checkpoints</code> to be added as query strings in order to enable respective features. Combinations are possible - for example: ' . add_query_arg( array( 'dbgx_use_error_log' => 1, 'dbgx_add_default_checkpoints' => 1, 'dbgx_track_default_vars' => 1 ) ) ), 
     96            'enable_admin_bar_menu'         => array( 'label' => 'Enable admin bar menu', 'desc' => 'Adds a menu to your admin bar to allow access to the url query parameters to control debug bar behaviour, only useful with allow_url_settings enabled' ), 
    9597        ) ); 
    9698         
     
    110112        } 
    111113         
    112         if ( 1 == Debug_Bar_Extender::instance()->settings['use_error_log'] ) { 
     114        if ( 1 == Debug_Bar_Extender::instance()->settings['use_error_log'] || ( 1 == Debug_Bar_Extender::instance()->settings['allow_url_settings'] && isset( $_GET['dbgx_use_error_log'] ) && 1 == $_GET['dbgx_use_error_log'] ) ) { 
    113115            Debug_Bar_Extender::instance()->use_error_log = true; 
    114116        } 
     
    116118        if ( Debug_Bar_Extender::instance()->use_error_log ) 
    117119            error_log( 'Starting debug session for ' . $_SERVER['REQUEST_URI'] ); 
    118              
     120         
    119121        Debug_Bar_Extender::instance()->prepare_debug_menu(); 
    120122    } 
     
    146148             
    147149         
     150        add_action( 'admin_bar_menu', array( &$this, 'debug_action_admin_bar_menu' ), 101 ); 
    148151        add_action( 'init', array( &$this, 'add_design_parts' ) ); 
    149152         
    150         if ( 1 == $this->settings['track_default_vars'] ) { 
     153        if ( 1 == $this->settings['track_default_vars'] || ( 1 == Debug_Bar_Extender::instance()->settings['allow_url_settings'] && isset( $_GET['dbgx_track_default_vars'] ) && 1 == $_GET['dbgx_track_default_vars'] ) ) { 
    151154            add_action( 'parse_request', array( &$this, 'trace_var_request' ), 1000 ); 
    152155            add_action( 'after_setup_theme', array( &$this, 'trace_var_template' ), 1000 ); 
    153156            add_action( 'posts_request', array( &$this, 'trace_var_posts_request' ), 1000 ); 
    154             add_action( 'posts_results', array( &$this, 'trace_var_found_post_ids' ), 1000 ); 
     157            //add_action( 'posts_results', array( &$this, 'trace_var_found_post_ids' ), 1000 ); 
    155158            add_action( 'pre_get_posts', array( &$this, 'trace_var_wp_query' ), 1000 ); 
    156159            add_action( 'wp_redirect', array( &$this, 'trace_var_redirect' ), 1000, 2 ); 
     
    158161        } 
    159162         
    160         if ( 1 == $this->settings['add_default_checkpoints'] ) { 
    161             Debug_Bar_Extender::instance()->start( 'start' ); 
     163        if ( 1 == $this->settings['add_default_checkpoints'] || ( 1 == Debug_Bar_Extender::instance()->settings['allow_url_settings'] && isset( $_GET['dbgx_add_default_checkpoints'] ) && 1 == $_GET['dbgx_add_default_checkpoints'] ) ) { 
    162164             
    163             $checkpoint_actions = array(  
     165            $checkpoint_actions = apply_filters( 'debugbarextender_default_checkpoint_actions', array(  
    164166                                            'widgets_init', 
    165167                                            'register_sidebar', 
     
    215217                                            'admin_print_footer_scripts', 
    216218                                            'admin_footer-edit.php', 
    217                                              
    218                                     ); 
     219                                    ) ); 
    219220 
    220221            foreach( $checkpoint_actions as $action_hook ) { 
    221                 add_action( $action_hook, create_function( '$in=NULL', 'Debug_Bar_Extender::instance()->checkpoint("' . $action_hook . ' action");if ( $in ) return $in;' ) ); 
     222                add_action( $action_hook, create_function( '$in=NULL', 'dbgx_checkpoint("' . $action_hook . ' action");if ( $in ) return $in;' ) ); 
    222223            } 
    223224        } 
    224225    } 
    225226 
     227    /* 
     228     * Receive start time 
     229     */ 
     230    public function get_start_time() { 
     231        return $this->start_time; 
     232    } 
     233     
     234    /*  
     235     * Receive end time 
     236     */ 
     237    public function get_time() { 
     238        return $this->current_time; 
     239    } 
     240     
    226241    /*  
    227242     * Enqueue Styles and Scripts 
     
    279294        $mem_usage = $this->guess_variable_size( $value ); 
    280295        $trace = debug_backtrace(); 
    281  
     296        if ( !empty( $trace[1]['function'] ) && 'dbgx_trace_var' == $trace[1]['function'] ) // usage of dbgx_trace_var() shortcut function 
     297            array_shift( $trace ); 
     298             
    282299        $result = array( 
    283300            'file' => $trace[0]['file'], 
     
    453470        } 
    454471 
    455         $note = '<span class="debug-note">Note: you can measure runtimes between checkpoints via <code>Debug_Bar_Extender::instance()->start( $note="" )</code>, <code>Debug_Bar_Extender::instance()->checkpoint( $note="" )</code>,  <code>Debug_Bar_Extender::instance()->end( $note="" )</code></span>'; 
     472        $note = '<span class="debug-note">Note: you can measure runtimes between checkpoints via <code>Debug_Bar_Extender::instance()->start( $note="" )</code>, <code>Debug_Bar_Extender::instance()->checkpoint( $note="" )</code>,  <code>Debug_Bar_Extender::instance()->end( $note="" )</code> or using the simple shortcut function <code>dbgx_checkpoint( $note="" )</code></span>'; 
    456473        $out = $query_time . $out . $note; 
    457474        return $out; 
     
    548565 
    549566        if ( 'variable' == $scope ) 
    550             $note = '<span class="debug-note">Note: you can screen additional variables via <code>Debug_Bar_Extender::instance()->trace_var( $value, $var_name = false )</code>. Omitting the <code>$var_name</code> will let the script look it up. Please be aware that sizes are approximate based on <code>strlen(serialize())</code></span>'; 
     567            $note = '<span class="debug-note">Note: you can screen additional variables via <code>Debug_Bar_Extender::instance()->trace_var( $value, $var_name = false )</code> or simply by using <code>dbgx_trace_var( $value, $var_name = false )</code>. Omitting the <code>$var_name</code> will let the script look it up. Please be aware that sizes are approximate based on <code>strlen(serialize())</code></span>'; 
    551568        elseif ( 'memcache' == $scope ) 
    552569            $note = '<span class="debug-note">Note: you can screen additional keys via <code>Debug_Bar_Extender::instance()->trace_memcache( $key, $group = "default" )</code>. This lookups are gathering the values from all datacenters and will show one value if all results are the same and multiple if the results in the datacenters differ. As sandboxes don\'t update remote memcache servers make sure to prime the caches by a live page load prior to sandboxing a site.</span>'; 
     
    556573    private function log() { 
    557574        $trace = debug_backtrace(); 
     575        if ( !empty( $trace[2]['function'] ) && 'dbgx_checkpoint' == $trace[2]['function'] )    // usage of dbgx_checkpoint() shortcut function 
     576            array_shift( $trace ); 
     577             
    558578        $caller = $trace[1]; // it always goes through start, checkpoint or end 
    559579        $result = array( 
     
    632652    } 
    633653     
    634     function settings_page() { ?> 
     654    public function settings_page() { 
     655        if ( !current_user_can( 'manage_options' ) ) { 
     656            global $current_user; 
     657            $msg = "I'm sorry, " . $current_user->display_name . " I'm afraid I can't do that."; 
     658            echo '<div class="wrap">' . $msg . '</div>'; 
     659            return false; 
     660        } 
     661    ?> 
    635662    <div class="wrap"> 
    636663    <?php if ( function_exists('screen_icon') ) screen_icon(); ?> 
     
    678705    <?php 
    679706    } 
     707     
     708    public function debug_action_admin_bar_menu() { 
     709        global $wp_admin_bar; 
     710        if ( 0 == Debug_Bar_Extender::instance()->settings['allow_url_settings'] || 0 == Debug_Bar_Extender::instance()->settings['enable_admin_bar_menu'] ) 
     711            return; 
     712             
     713        $wp_admin_bar->add_menu( array( 
     714            'id' => 'dbg', 
     715            'title' => 'DbgBExt', 
     716            'href' => false, 
     717        ) ); 
     718        $wp_admin_bar->add_menu( array( 
     719            'parent' => 'dbg', 
     720            'title' => 'default vars', 
     721            'href' => add_query_arg( array( 'dbgx_use_error_log' => 0, 'dbgx_add_default_checkpoints' => 0, 'dbgx_track_default_vars' => 1 ) ), 
     722        ) ); 
     723        $wp_admin_bar->add_menu( array( 
     724            'parent' => 'dbg', 
     725            'title' => 'default checkpoints', 
     726            'href' => add_query_arg( array( 'dbgx_use_error_log' => 0, 'dbgx_add_default_checkpoints' => 1, 'dbgx_track_default_vars' => 0 ) ), 
     727        ) ); 
     728        $wp_admin_bar->add_menu( array( 
     729            'parent' => 'dbg', 
     730            'title' => 'default checkpoints+vars', 
     731            'href' => add_query_arg( array( 'dbgx_use_error_log' => 0, 'dbgx_add_default_checkpoints' => 1, 'dbgx_track_default_vars' => 1 ) ), 
     732        ) ); 
     733        $wp_admin_bar->add_menu( array( 
     734            'parent' => 'dbg', 
     735            'title' => 'send to error_log', 
     736            'href' => add_query_arg( array( 'dbgx_use_error_log' => 1, 'dbgx_add_default_checkpoints' => 0, 'dbgx_track_default_vars' => 0 ) ), 
     737        ) ); 
     738        $wp_admin_bar->add_menu( array( 
     739            'parent' => 'dbg', 
     740            'title' => 'default vars -> log', 
     741            'href' => add_query_arg( array( 'dbgx_use_error_log' => 1, 'dbgx_add_default_checkpoints' => 0, 'dbgx_track_default_vars' => 1 ) ), 
     742        ) ); 
     743        $wp_admin_bar->add_menu( array( 
     744            'parent' => 'dbg', 
     745            'title' => 'default checkpoints -> log', 
     746            'href' => add_query_arg( array( 'dbgx_use_error_log' => 1, 'dbgx_add_default_checkpoints' => 1, 'dbgx_track_default_vars' => 0 ) ), 
     747        ) ); 
     748        $wp_admin_bar->add_menu( array( 
     749            'parent' => 'dbg', 
     750            'title' => 'default checkpoints+vars -> log', 
     751            'href' => add_query_arg( array( 'dbgx_use_error_log' => 1, 'dbgx_add_default_checkpoints' => 1, 'dbgx_track_default_vars' => 1 ) ), 
     752        ) ); 
     753    } 
     754 
    680755 
    681756} 
    682757 
    683758Debug_Bar_Extender::init(); 
     759 
     760if ( !function_exists( 'dbgx_checkpoint' ) ) { 
     761    function dbgx_checkpoint( $note = '' ) { 
     762        if ( 0 == Debug_Bar_Extender::instance()->get_start_time() ) { 
     763            Debug_Bar_Extender::instance()->start( $note ); 
     764        } else { 
     765            Debug_Bar_Extender::instance()->checkpoint( $note ); 
     766        } 
     767    } 
     768} 
     769 
     770if ( !function_exists( 'dbgx_trace_var' ) ) { 
     771    function dbgx_trace_var( $value, $var_name = false ) { 
     772        Debug_Bar_Extender::instance()->trace_var( $value, $var_name ); 
     773    } 
     774} 
  • debug-bar-extender/trunk/readme.txt

    r425345 r432272  
    6161` 
    6262 
     63== Advanced usage ==  
     64 
     65Looking at the source of the debug-bar-extender.php you will notice that there are various filters to control the default settings. This is useful when you are working on a multisite install and want to use the debug bar without having to adjust the settings every time. Here's an example of how settings enforced by a php file in mu-plugins/ could look like 
     66 
     67`add_filter( 'debugbarextender_default_settings', 'my_debug_bar_extender_settings' ); 
     68// enforce debug bar settings 
     69function my_debug_bar_extender_settings( $default_settings ) { 
     70    $default_settings = array( 
     71            'enable_profiler'               => 1, 
     72            'enable_variable_lookup'        => 1, 
     73            'track_default_vars'            => 0, 
     74            'add_default_checkpoints'       => 1, 
     75            'savequeries_debugmode_enable'  => 1, 
     76            'use_error_log'                 => 0, 
     77            'allow_url_settings'            => 1, 
     78            'enable_admin_bar_menu'         => 1, 
     79    ); 
     80    return $default_settings; 
     81} 
     82 
     83add_filter( 'debugbarextender_default_checkpoint_actions', 'my_debug_bar_extender_checkpoints' ); 
     84// initiate some default checkpoints 
     85function my_debug_bar_extender_checkpoints( $default_checkpoints ) { 
     86    $default_checkpoints = array( 
     87                    'muplugins_loaded', 
     88                    'wp_head', 
     89                    'wp_footer', 
     90                    'loop_start', 
     91                    'loop_end', 
     92                    'shutdown', 
     93    ); 
     94    return $default_checkpoints; 
     95} 
     96` 
     97 
    6398== Wishlist == 
    6499 
     
    73108 
    74109== ChangeLog == 
     110 
     111= Version 0.5 = 
     112 
     113* added filters to control settings `debugbarextender_default_settings`, `debugbarextender_default_checkpoint_actions` 
     114* added wrapper functions `dbgx_checkpoint( $note = '' )` and `dbgx_trace_var( $value, $var_name = false )` for easier access to debugging functions. 
     115* added new setting option to allow control of some settings via $_GET parameters in the urls. Allows dbgx_use_error_log, dbgx_track_default_vars, dbgx_add_default_checkpoints to be added as query strings in order to enable respective features. Combinations are possible - for example: /wp-admin/options-general.php?page=debug-bar-extender&dbgx_use_error_log=1&dbgx_add_default_checkpoints=1&dbgx_track_default_vars=1  
     116* added new setting option to enable a admin bar menu with shortcuts to this urls 
    75117 
    76118= Version 0.4 = 
Note: See TracChangeset for help on using the changeset viewer.