Forked from norcross/norcross-debug-functions.php
Created
September 30, 2017 16:15
-
-
Save melaniedunford/0f9e80af396786046df0caca683ff4c4 to your computer and use it in GitHub Desktop.
my list of debugging functions to keep in an MU file
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
Plugin Name: Norcross Debug Functions | |
Plugin URI: https://gist.github.com/norcross/7864205/ | |
Description: A set of functions I use on all sites while building | |
Author: Andrew Norcross | |
Version: 0.0.1 | |
Requires at least: 3.0 | |
Author URI: http://andrewnorcross.com | |
*/ | |
/** | |
* Bypass the email sent for a successful auto-update. | |
* | |
* @param bool $send Whether or not to send the email. | |
* @param string $type The type of email to send. Can be one of 'success', 'fail', 'critical'. | |
* @param object $core_update The update offer that was attempted. | |
* @param mixed $result The result for the core update. Can be WP_Error. | |
* | |
* @return bool The result to send the email. | |
*/ | |
function rkv_bypass_auto_update_email( $send, $type, $core_update, $result ) { | |
return ! empty( $type ) && 'success' === $type ? false : true; | |
} | |
add_filter( 'auto_core_update_send_email', 'rkv_bypass_auto_update_email', 10, 4 ); | |
/** | |
* Add a small bit of CSS to the Query Monitor output. | |
* | |
* @return void | |
*/ | |
function rkv_add_qm_css() { | |
echo '<style>#qm { position: relative; z-index: 1000; }</style>' . "\n"; | |
} | |
add_action( 'wp_head', 'rkv_add_qm_css' ); | |
add_action( 'admin_head', 'rkv_add_qm_css' ); | |
/** | |
* Keeps a user always logged in. | |
* NOTE: Don't use this on a production site, ever! | |
* | |
* @return void | |
*/ | |
function rkv_auto_login() { | |
// Bail if the user is logged in. | |
if ( is_user_logged_in() ) { | |
return; | |
} | |
// Bail on ajax calls. | |
if ( defined( 'DOING_AJAX' ) && DOING_AJAX ) { | |
return; | |
} | |
// Don't load on a specific query string. | |
if ( isset( $_GET['test-logout'] ) ) { | |
return; | |
} | |
// Get our userdata (change the ID to match). | |
$user = get_userdata( 1 ); | |
// Bail if I don't have userdata. | |
if ( empty( $user ) || ! is_object( $user ) ) { | |
return; | |
} | |
// Set the current user. | |
wp_set_current_user( $user->ID, $user->user_login ); | |
// Set the auth cookie. | |
wp_set_auth_cookie( $user->ID ); | |
// Now actually log in. | |
do_action( 'wp_login', $user->user_login ); | |
} | |
add_action( 'init', 'rkv_auto_login' ); | |
add_action( 'admin_init', 'rkv_auto_login' ); | |
/** | |
* Display array results in a readable fashion. | |
* | |
* @param mixed $display The output we want to display. | |
* @param boolean $die Whether or not to die as soon as output is generated. | |
* @param boolean $return Whether to return the output or show it. | |
* | |
* @return mixed Our printed (or returned) output. | |
*/ | |
function preprint( $display, $die = false, $return = false ) { | |
// Set an empty. | |
$code = ''; | |
// Add some CSS to make it a bit more readable. | |
$style = 'background-color: #fff; color: #000; font-size: 16px; line-height: 22px; padding: 5px; white-space: pre-wrap; white-space: -moz-pre-wrap; white-space: -pre-wrap; white-space: -o-pre-wrap; word-wrap: break-word;'; | |
// Filter the style. | |
$style = apply_filters( 'rkv_preprint_style', $style ); | |
// Generate the actual output. | |
$code .= '<pre style="' . $style . '">'; | |
$code .= print_r( $display, 1 ); | |
$code .= '</pre>'; | |
// Return if requested. | |
if ( $return ) { | |
return $code; | |
} | |
// Print if requested (the default). | |
if ( ! $return ) { | |
print $code; | |
} | |
// Die if you want to die. | |
if ( $die ) { | |
die(); | |
} | |
} | |
/** | |
* Grab the global WP_Query object and dump it. | |
* | |
* @param boolean $die Whether to die after generation. | |
* | |
* @return void | |
*/ | |
function fullquery( $die = true ) { | |
// Call the global WP_Query object. | |
global $wp_query; | |
// Output it. | |
preprint( $wp_query, $die ); | |
} | |
/** | |
* Debugging Convenience function to show all the "filters" currently attached to a hook. | |
* | |
* @param string $hook The hook to grab filters for. | |
* @param boolean $die Whether to die after generation. | |
* | |
* @return mixed Our printed output. | |
*/ | |
function print_filters_for( $hook = '', $die = false ) { | |
// Call the global wp_filter object. | |
global $wp_filter; | |
// Bail if no hook was provided or doesn't exist in the wp_filter object. | |
if ( empty( $hook ) || ! isset( $wp_filter[ $hook ] ) ) { | |
return; | |
} | |
// Output the information. | |
preprint( $wp_filter[ $hook ], $die ); | |
} | |
/** | |
* Suppress errors generated by specified WordPress plugins. | |
* | |
* Include in the auto_prepend_file php.ini directive to ignore globally. Update | |
* the directory separator as required. | |
* | |
* @see http://plugins.trac.wordpress.org/browser/ostrichcize/tags/0.1/ostrichcize.php#L146 | |
* | |
* @param string $errno The error number. | |
* @param string $errstr The error message. | |
* @param string $errfile Path to the file that caused the error. | |
* @param int $errline Line number of the error. | |
* | |
* @return bool True to success error reporting; false to use default error handler. | |
*/ | |
function blazersix_wpdev_error_handler( $errno, $errstr, $errfile, $errline ) { | |
// Set an array of plugin paths. | |
$paths = array( | |
'plugins\\wordpress-importer\\' | |
); | |
// Bail without paths. | |
if ( empty( $paths ) ) { | |
return false; | |
} | |
// Loop through our set plugin paths. | |
foreach ( $paths as $path ) { | |
// If we have the plugin path included in the error message, return true. | |
if ( false !== strpos( $errstr, $path ) ) { | |
return true; | |
} | |
// If we have the plugin path included in the file that caused it, return true. | |
if ( false !== strpos( $errfile, $path ) ) { | |
return true; | |
} | |
} | |
// The path was not found, so report the error. | |
return false; | |
} | |
set_error_handler( 'blazersix_wpdev_error_handler' ); | |
/** | |
* Parse a JSON file into more readable text. | |
* | |
* @param string $text The JSON text we want to parse. | |
* | |
* @return string $text The JSON text we want to parse. | |
*/ | |
function rkv_json_parse( $text = '' ) { | |
return str_replace( array( '\n', '\t' ), '', $text ); | |
} | |
/** | |
* Set up some quick links for the admin bar. | |
* | |
* @param WP_Admin_Bar $wp_admin_bar The global WP_Admin_Bar object. | |
* | |
* @return void. | |
*/ | |
function rkv_admin_bar_static( WP_Admin_Bar $wp_admin_bar ) { | |
// Bail if current user doesnt have cap. | |
if ( ! current_user_can( 'manage_options' ) ) { | |
return; | |
} | |
// Remove customizer. | |
$wp_admin_bar->remove_node( 'customize' ); | |
// Add a parent item. | |
$wp_admin_bar->add_node( | |
array( | |
'id' => 'norcross-dev-links', | |
'title' => 'Dev Links', | |
) | |
); | |
// Add the GitHub profile link. | |
$wp_admin_bar->add_node( | |
array( | |
'id' => 'github-profile', | |
'title' => 'GitHub Profile', | |
'href' => 'https://github.com/norcross/', | |
'position' => 0, | |
'parent' => 'norcross-dev-links', | |
'meta' => array( | |
'title' => 'GitHub Profile', | |
'target' => '_blank', | |
), | |
) | |
); | |
// Add the WP profile link. | |
$wp_admin_bar->add_node( | |
array( | |
'id' => 'wp-profile', | |
'title' => 'WordPress Profile', | |
'href' => 'https://profiles.wordpress.org/norcross', | |
'position' => 0, | |
'parent' => 'norcross-dev-links', | |
'meta' => array( | |
'title' => 'WordPress Profile', | |
'target' => '_blank', | |
), | |
) | |
); | |
} | |
add_action( 'admin_bar_menu', 'rkv_admin_bar_static', 9999 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment