Advertisement
Guest User

wpdb-profiling.php

a guest
Nov 17th, 2011
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 14.42 KB | None | 0 0
  1. <?php
  2. /*
  3. Plugin Name: WPDB Profiling
  4. Plugin URI: http://www.tierra-innovation.com/blog/2009/07/01/wpdb-profiling-1-1-released/
  5. Description: Render database profiling below the wp_footer() variable.  Upload `wpdb-profiling` to the `/wp-content/plugins/` directory, activate the plugin, and enable / disable features from the wp-admin plugin screen.
  6. Author: Tierra Innovation
  7. Version: 1.3.3
  8. Author URI: http://www.tierra-innovation.com
  9. */
  10.  
  11. /*
  12.  * This is a modified version (under the MIT License) of a plugin
  13.  * originally developed by Tierra Innovation for WNET.org.
  14.  *
  15.  * This plugin is currently available for use in all personal
  16.  * or commercial projects under both MIT and GPL licenses. This
  17.  * means that you can choose the license that best suits your
  18.  * project, and use it accordingly.
  19.  *
  20.  * MIT License: http://www.tierra-innovation.com/license/MIT-LICENSE.txt
  21.  * GPL2 License: http://www.tierra-innovation.com/license/GPL-LICENSE.txt
  22.  */
  23.  
  24. add_action('wp_head', 'wpdb_profiling_head',1);
  25. add_action('admin_head', 'wpdb_profiling_head',1);
  26. add_action('wp_footer', 'wpdb_profiling', 1000);
  27. add_action('admin_footer', 'wpdb_profiling', 1000);
  28.  
  29.  
  30. if ( !defined('SAVEQUERIES') )
  31.     define('SAVEQUERIES', true);
  32.  
  33. function wpdb_profiling_head() {
  34.     if(@file_exists(TEMPLATEPATH.'/wpdb-profiling.css')) {
  35.         echo '<link rel="stylesheet" href="'.get_stylesheet_directory_uri().'/wpdb-profiling.css" type="text/css" />'."\n";
  36.     } else {
  37.         echo '<link rel="stylesheet" type="text/css" href="' . get_option('siteurl') . '/wp-content/plugins/wpdb-profiling/wpdb-profiling.css" />'."\n";
  38.     }
  39. }
  40.  
  41. function wpdb_profiling() {
  42.  
  43.     global $wpdb;
  44.  
  45.     $permission_1 = explode(",", get_option('profiling_user_permission_1'));
  46.     $permission_2 = explode(",", get_option('profiling_user_permission_2'));
  47.     trace( $permission_1 );
  48.     trace( $permission_2 );
  49.  
  50.     $display_1 = false;
  51.     foreach ($permission_1 as $permission)
  52.     {
  53.         if (current_user_can($permission))
  54.             $display_1 = true;
  55.  
  56.         if ($display_1 == true)
  57.             break;
  58.     }
  59.  
  60.     $display_2 = false;
  61.     foreach ($permission_2 as $permission)
  62.     {
  63.         if (current_user_can($permission))
  64.             $display_2 = true;
  65.  
  66.         if ($display_2 == true)
  67.             break;
  68.     }
  69.  
  70.     if (
  71.             get_option('always_show_profiling') == "yes"
  72.             && $display_1 == true
  73.             ||
  74.             (isset( $_GET['show_queries'] ) && ( $_GET['show_queries'] == "yes" || $_GET['show_queries'] == "true" ) )
  75.             && get_option('allow_get_request') == "yes"
  76.             && $display_2 == true
  77.         ) {
  78.  
  79.         $rows = array();
  80.         $total_time = 0;
  81.         $total_time_by_function = array();
  82.  
  83.         foreach ($wpdb->queries as $query) {
  84.             if ( !isset($total_time_by_function[$query[2]]) ) {
  85.                 $total_time_by_function[$query[2]] = 0;
  86.             }
  87.             $total_time_by_function[$query[2]] += $query[1];
  88.             $total_time += $query[1];
  89.             $query_time = number_format($query[1], 7, '.', '');
  90.             $rows[] = array($query[1], "
  91.                 <tr>
  92.                     <td>$query[0]</td>\n
  93.                     <td>$query_time</td>\n
  94.                     <td>$query[2]</td>
  95.                 </tr>\n
  96.             ");
  97.         }
  98.  
  99.         usort($rows, "wpdb_sort_output");
  100.         arsort($total_time_by_function);
  101.  
  102.         echo "<div id='wpdb-profiling'>\n";
  103.  
  104.         if ( defined('WP_CACHE') ) {
  105.             $style = "green";
  106.             $cached = "<strong>Enabled!</strong>";
  107.         }
  108.         else {
  109.             $style = "red";
  110.             $cached = "<strong>Disabled!</strong>  Install a caching tool (<a href='http://wordpress.org/extend/plugins/batcache/' title='Batcache' target='_blank'>Batcache</a>) or (<a href='http://wordpress.org/extend/plugins/wp-super-cache/' title='WP_Super_Cache' target='_blank'>WP_Super_Cache</a>) for DB Optimization.";
  111.         }
  112.  
  113.         echo "<table border='2'>\n";
  114.         echo "<tr>\n";
  115.         echo "
  116.             <th width='50%'>Item</th>\n
  117.             <th width='50%'>Status</th>\n
  118.         ";
  119.         echo "</tr>\n";
  120.         echo "<tr>\n";
  121.         echo "
  122.             <td><strong>WP_Cache Status:</strong></td>\n
  123.             <td class='$style'>$cached</td>\n
  124.         ";
  125.         echo "</tr>\n";
  126.         echo "</table>\n";
  127.  
  128.         echo "<table border='2'>\n";
  129.         echo "<tr>\n";
  130.         echo "
  131.             <th>SQL</th>\n
  132.             <th>Execution Time</th>\n
  133.             <th>Function Call</th>\n
  134.         ";
  135.         echo "</tr>\n";
  136.  
  137.         foreach ($rows as $row)
  138.             echo $row[1];
  139.  
  140.         echo "<tr>\n";
  141.         echo "  <td colspan='3'><strong>Total Time:</strong> " . count($wpdb->queries) . " database queries run in " . $total_time . " seconds.</td>";
  142.         echo "</tr>\n";
  143.  
  144.         echo "</table>\n";
  145.  
  146.         if ( !empty($total_time_by_function) ) {
  147.  
  148.         echo "<table border='2'>\n";
  149.         echo "<tr>\n";
  150.         echo "
  151.             <th>Grouped Functions</th>\n
  152.             <th>Execution Time</th>\n
  153.         ";
  154.         echo "</tr>\n";
  155.  
  156.         foreach ($total_time_by_function as $function => $time) {
  157.             echo "<tr>\n";
  158.             echo "<td>$function</td>";
  159.             echo "<td>" . number_format($time, 7, '.', '') . "</td>";
  160.             echo "</tr>\n";
  161.         }
  162.  
  163.         echo "<tr>\n";
  164.         echo "
  165.             <td><strong>Total Time</strong></td>\n
  166.             <td><strong>" . array_sum($total_time_by_function) . "</td></strong>
  167.         ";
  168.         echo "</tr>\n";
  169.  
  170.         echo "</table>\n";
  171.  
  172.         echo "</div>\n";
  173.  
  174.         }
  175.     }
  176. }
  177.  
  178. function wpdb_sort_output($a, $b) {
  179.     return $a[0] == $b[0] ? 0 : (($a[0] > $b[0]) ? -1 : 1);
  180. }
  181.  
  182. function set_wpdb_profiling_options() {
  183.     $always_show_profiling      = get_option('always_show_profiling');
  184.     $allow_get_request      = get_option('allow_get_request');
  185.  
  186.     $profiling_user_permission_1    = get_option('profiling_user_permission_1');
  187.     $profiling_user_permission_2    = get_option('profiling_user_permission_2');
  188.  
  189.     $override_disable_auto_save = get_option('override_disable_auto_save');
  190.     $override_disable_revisioning   = get_option('override_disable_revisioning');
  191.  
  192.     if ($always_show_profiling !== "no")
  193.         add_option('always_show_profiling','yes');
  194.  
  195.     if ($allow_get_request !== "yes")
  196.         add_option('allow_get_request','no');
  197.  
  198.     if ($profiling_user_permission_1 == '')
  199.         add_option('profiling_user_permission_1','administrator');
  200.  
  201.     if ($profiling_user_permission_2 == '')
  202.         add_option('profiling_user_permission_2','administrator');
  203.  
  204.     if ($override_disable_auto_save !== "no")
  205.         add_option('override_disable_auto_save','yes');
  206.  
  207.     if ($override_disable_revisioning !== "yes")
  208.         add_option('override_disable_revisioning','no');
  209.  
  210. }
  211.  
  212. function unset_wpdb_profiling_options() {
  213.     delete_option('always_show_profiling');
  214.     delete_option('allow_get_request');
  215.     delete_option('profiling_user_permission_1');
  216.     delete_option('profiling_user_permission_2');
  217.     delete_option('override_disable_auto_save');
  218.     delete_option('override_disable_revisioning');
  219. }
  220.  
  221. function admin_profiling_options() {
  222.  
  223.     if (isset( $_REQUEST['submit'] ) && $_REQUEST['submit'])
  224.         update_profiling_options();
  225.  
  226.     print_profiling_form();
  227. }
  228.  
  229. function update_profiling_options() {
  230.  
  231.     $ok = false;
  232.  
  233.     if ($_REQUEST['always_show_profiling'])
  234.     {
  235.         update_option('always_show_profiling',$_REQUEST['always_show_profiling']);
  236.         $ok = true;
  237.     }
  238.  
  239.     if ($_REQUEST['allow_get_request'])
  240.     {
  241.         update_option('allow_get_request',$_REQUEST['allow_get_request']);
  242.         $ok = true;
  243.     }
  244.  
  245.     if ($_REQUEST['profiling_user_permission_1'])
  246.     {
  247.         update_option('profiling_user_permission_1',$_REQUEST['profiling_user_permission_1']);
  248.         $ok = true;
  249.     }
  250.  
  251.     if ($_REQUEST['profiling_user_permission_2'])
  252.     {
  253.         update_option('profiling_user_permission_2',$_REQUEST['profiling_user_permission_2']);
  254.         $ok = true;
  255.     }
  256.  
  257.     if ($_REQUEST['override_disable_auto_save'])
  258.     {
  259.         update_option('override_disable_auto_save',$_REQUEST['override_disable_auto_save']);
  260.         $ok = true;
  261.     }
  262.  
  263.     if ($_REQUEST['override_disable_revisioning'])
  264.     {
  265.         update_option('override_disable_revisioning',$_REQUEST['override_disable_revisioning']);
  266.         $ok = true;
  267.     }
  268.  
  269.     if ($ok)
  270.     {
  271.         echo '<div id="message" class="updated fade">
  272.             <p>Options saved.</p>
  273.         </div>';
  274.     }
  275.     else
  276.     {
  277.         echo '<div id="message" class="error fade">
  278.             <p>Failed to save options.</p>
  279.         </div>';
  280.     }
  281.  
  282. }
  283.  
  284. function print_profiling_form() {
  285.  
  286.     $true_selected = '';
  287.     $false_selected = '';
  288.  
  289.     $perm_1_1_selected = '';
  290.     $perm_1_2_selected = '';
  291.     $perm_1_3_selected = '';
  292.     $perm_1_4_selected = '';
  293.     $perm_2_1_selected = '';
  294.     $perm_2_2_selected = '';
  295.     $perm_2_3_selected = '';
  296.     $perm_2_4_selected = '';
  297.     $get_true_selected = '';
  298.     $get_false_selected = '';
  299.     $true_selected = '';
  300.     $false_selected = '';
  301.     $true_autosave_selected = '';
  302.     $false_autosave_selected = '';
  303.     $true_revision_selected = '';
  304.     $false_revision_selected = '';
  305.  
  306.     if (get_option('always_show_profiling') == "yes")
  307.         $true_selected = ' selected="selected"';
  308.     else
  309.         $false_selected = ' selected="selected"';
  310.  
  311.     if (get_option('allow_get_request') == "yes")
  312.         $get_true_selected = ' selected="selected"';
  313.     else
  314.         $get_false_selected = ' selected="selected"';
  315.  
  316.     if (get_option('profiling_user_permission_1') == "contributor")
  317.         $perm_1_1_selected = ' selected="selected"';
  318.     elseif (get_option('profiling_user_permission_1') == "author")
  319.         $perm_1_2_selected = ' selected="selected"';
  320.     elseif (get_option('profiling_user_permission_1') == "editor")
  321.         $perm_1_3_selected = ' selected="selected"';
  322.     elseif (get_option('profiling_user_permission_1') == "administrator")
  323.         $perm_1_4_selected = ' selected="selected"';
  324.  
  325.     if (get_option('profiling_user_permission_2') == "contributor")
  326.         $perm_2_1_selected = ' selected="selected"';
  327.     elseif (get_option('profiling_user_permission_2') == "author")
  328.         $perm_2_2_selected = ' selected="selected"';
  329.     elseif (get_option('profiling_user_permission_2') == "editor")
  330.         $perm_2_3_selected = ' selected="selected"';
  331.     elseif (get_option('profiling_user_permission_2') == "administrator")
  332.         $perm_2_4_selected = ' selected="selected"';
  333.  
  334.     if (get_option('override_disable_auto_save') == "yes")
  335.         $true_autosave_selected = ' selected="selected"';
  336.     else
  337.         $false_autosave_selected = ' selected="selected"';
  338.  
  339.     if (get_option('override_disable_revisioning') == "yes")
  340.         $true_revision_selected = ' selected="selected"';
  341.     else
  342.         $false_revision_selected = ' selected="selected"';
  343.  
  344.     print '
  345.  
  346.     <div class="wrap">
  347.  
  348.         <div id="icon-options-general" class="icon32"><img src="http://tierra-innovation.com/wordpress-cms/logos/src/wpdb-profiling/1.3.2/default.gif" alt="" title="" /><br /></div>
  349.  
  350.         <h2>WPDB Profiling &amp; Optimization</h2>
  351.  
  352.     ';
  353.  
  354.     if ( defined('WP_CACHE') ) {
  355.         $style = "green";
  356.         $cached = "<strong>Good!</strong> WP_CACHE is defined.";
  357.     }
  358.     else {
  359.         $style = "red";
  360.         $cached = "<strong>Uh Oh!</strong>  You'll need to enable and install a caching tool (<a href='http://wordpress.org/extend/plugins/batcache/' title='Batcache' target='_blank'>Batcache</a>) or (<a href='http://wordpress.org/extend/plugins/wp-super-cache/' title='WP_Super_Cache' target='_blank'>WP_Super_Cache</a>) for DB Optimization.";
  361.     }
  362.  
  363.     if ( defined('SAVEQUERIES') ) {
  364.         $stylesaved = "green";
  365.         $saved = "<strong>Good!</strong> SAVEQUERIES is defined in wp-config.php";
  366.     }
  367.     else {
  368.         $stylesaved = "red";
  369.         $saved = "<strong>Uh Oh!</strong>  Please add `define('SAVEQUERIES', true);` to your wp-config.php file.";
  370.     }
  371.  
  372.     print "
  373.  
  374.         <h3>Plugin Check</h3>
  375.  
  376.         <ul>
  377.             <li><span style='color: $style'>$cached</span><br />
  378.             <span style='color: $stylesaved'>$saved</span></li>
  379.         </ul>
  380.    
  381.     ";
  382.  
  383.     print "
  384.  
  385.             <style type='text/css'>
  386.                 select.wpdb { width: 120px; }
  387.             </style>
  388.  
  389.             <form method='post'>
  390.  
  391.                 <h3>Database Profiling</h3>
  392.  
  393.                 <ul>
  394.                     <li>
  395.                         <select class='wpdb' name='always_show_profiling'>
  396.                             <option value='yes' $true_selected>Always</option>
  397.                             <option value='no' $false_selected>Never</option>
  398.                         </select>
  399.  
  400.                         <label for='always_show_profiling'>automatically show profiling when logged in as </label>
  401.  
  402.                         <select class='wpdb' name='profiling_user_permission_1'>
  403.                             <option value='administrator' $perm_1_4_selected>Administrator</option>
  404.                             <option value='editor' $perm_1_3_selected>Editor</option>
  405.                             <option value='author' $perm_1_2_selected>Author</option>
  406.                             <option value='contributor' $perm_1_1_selected>Contributor</option>
  407.                         </select>
  408.                     </li>
  409.  
  410.                     <li>
  411.                         <select class='wpdb' name='allow_get_request'>
  412.                             <option value='yes' $get_true_selected>Enable</option>
  413.                             <option value='no' $get_false_selected>Disabled</option>
  414.                         </select>
  415.  
  416.                         <label for='allow_get_request'>the `?show_queries=yes` parameter in URL for user level</label>
  417.  
  418.                         <select class='wpdb' name='profiling_user_permission_2'>
  419.                             <option value='administrator' $perm_2_4_selected>Administrator</option>
  420.                             <option value='editor' $perm_2_3_selected>Editor</option>
  421.                             <option value='author' $perm_2_2_selected>Author</option>
  422.                             <option value='contributor' $perm_2_1_selected>Contributor</option>
  423.                         </select>
  424.                     </li>
  425.                 </ul>
  426.  
  427.                 <p><strong>Note:</strong> Select the lowest user permission level that should have access to profiling. The level you select and all higher levels will have access.</p>
  428.  
  429.                 <h3>Database Optimization</h3>
  430.  
  431.                 <ul>
  432.                     <li>
  433.                         <select class='wpdb' name='override_disable_auto_save'>
  434.                             <option value='yes' $true_autosave_selected>Yes</option>
  435.                             <option value='no' $false_autosave_selected>No</option>
  436.                         </select>
  437.  
  438.                         <label for='override_disable_auto_save'>Disable Auto Saving</label>
  439.                     </li>
  440.                     <li>
  441.                         <select class='wpdb' name='override_disable_revisioning'>
  442.                             <option value='yes' $true_revision_selected>Yes</option>
  443.                             <option value='no' $false_revision_selected>No</option>
  444.                         </select>
  445.  
  446.                         <label for='override_disable_revisioning'>Disable Post Revisions</label>
  447.                     </li>
  448.                 </ul>
  449.  
  450.                 <p><input type='submit' name='submit' class='button-primary' value='Save Options' /></p>
  451.  
  452.             </form>
  453.  
  454.         </div>
  455.  
  456.     ";
  457.  
  458. }
  459.  
  460. if (get_option('always_show_profiling') == "yes") {
  461.  
  462.     add_action('wp_head', 'wpdb_profiling_head',1);
  463.     add_action('admin_head', 'wpdb_profiling_head',1);
  464.     add_action('wp_footer', 'wpdb_profiling', 1000);
  465.     add_action('admin_footer', 'wpdb_profiling', 1000);
  466.  
  467. }
  468.  
  469. if ( get_option('override_disable_auto_save') == "yes" ) {
  470.  
  471.     function wpdb_disable_autosave() {
  472.         wp_deregister_script('autosave');
  473.     }
  474.  
  475.     add_action( 'wp_print_scripts', 'wpdb_disable_autosave' );
  476.  
  477. }
  478.  
  479. if ( get_option('override_disable_revisioning') == "yes" ) {
  480.  
  481.     define('WP_POST_REVISIONS', false);
  482.  
  483. }
  484.  
  485. function modify_menu() {
  486.     add_options_page(
  487.         'WPDB Profiling', // page title
  488.         'WPDB Profiling', // sub-menu title
  489.         'manage_options', // access/capa
  490.         'wpdb-profiling.php', // file
  491.         'admin_profiling_options' // function
  492.     );
  493. }
  494.  
  495. add_action('admin_menu', 'modify_menu');
  496.  
  497. register_activation_hook(__FILE__,'set_wpdb_profiling_options');
  498. register_deactivation_hook(__FILE__,'unset_wpdb_profiling_options');
  499. ?>
  500.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement