Last active
March 1, 2018 22:38
-
-
Save trepmal/a5651526cf01a67f8d6a4a3ff2a12879 to your computer and use it in GitHub Desktop.
Intended for use alongside Debug Bar tools. Do not use in Production.
This file contains 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: Debug Bar for REST API | |
* Description: Enable Debug Bar for REST API endpoints (add ?debug). Not for production. | |
* Author: Kailey "trepmal" Lampert | |
*/ | |
namespace trepmal\rest_debug; | |
/** | |
* load plugins, template if ?debug | |
* disable theme-y css/js, known cruft | |
*/ | |
function rest_api_init( $wp_rest_server ) { | |
if ( ! is_rest_debug() ) { | |
return; | |
} | |
// probably a bad shortcut, helps with authentication-required endpoints | |
// as well as Debug Bar Console | |
wp_signon(); | |
do_action( 'template_redirect' ); | |
foreach( [ 'style_loader_src', 'script_loader_src' ] as $hook ) { | |
add_filter( $hook, function( $src, $handle ) { | |
if ( false !== strpos( $src, '/themes/' ) ) { | |
return false; | |
} | |
return $src; | |
}, 10, 2 ); | |
} | |
remove_action( 'wp_head', 'print_emoji_detection_script', 7 ); | |
remove_action( 'wp_print_styles', 'print_emoji_styles' ); | |
} | |
add_action( 'rest_api_init', __NAMESPACE__ . '\rest_api_init' ); | |
/** | |
* output json in html | |
*/ | |
function rest_pre_echo_response( $result, $wp_rest_server, $request ) { | |
if ( ! is_rest_debug() ) { | |
return $result; | |
} | |
$result = wp_json_encode( $result, JSON_PRETTY_PRINT ); | |
header('Content-type: text/html'); | |
?> | |
<!doctype html> | |
<html <?php language_attributes(); ?>> | |
<head> | |
<meta charset="<?php bloginfo( 'charset' ); ?>"> | |
<?php wp_head(); ?> | |
</head> | |
<body> | |
<pre><?php echo esc_html( $result ); ?></pre> | |
<?php wp_footer(); ?> | |
</body> | |
</html> | |
<?php | |
die(); | |
} | |
add_filter( 'rest_pre_echo_response', __NAMESPACE__ . '\rest_pre_echo_response', 10, 3 ); | |
/** | |
* helper for debugging arg | |
*/ | |
function is_rest_debug() { | |
return isset( $_GET['debug'] ); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
to do: make the theme/front-end be not terrible. We only want enough css and markup to render the debug bar and its goodies.