public
Last active

Draft of "Theme Errors API"

  • Download Gist
wp-error-api.php
PHP
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
<?php
/**
* Wordpress "Theme Errors API"
*
* @package Wordpress
* @subpackage Theme Errors API
* @since 3.4
* @uses WP_Error
*/
 
/**
* Adds theme specific messages to the global theme WP_Error object.
*
* Takes the theme name as $code for the WP_Error object.
* Merges old $data and new $data arrays @uses wp_parse_args().
*
* @param (string) $message
* @param (mixed) $data_key
* @param (mixed) $data_value
* @return void
*/
function add_theme_error( $message, $data_key = '', $data_value = '' )
{
global $wp_theme_error, $wp_theme_error_code;
 
if ( ! isset( $wp_theme_error_code ) )
{
$theme_data = get_theme_data( get_stylesheet_uri() );
$name = str_replace( ' ', '', strtolower( $theme_data['Name'] ) );
$wp_theme_error_code = preg_replace( "/[^a-zA-Z0-9\s]/", '', $name );
}
 
if ( ! is_wp_error( $wp_theme_error ) || ! $wp_theme_error )
{
$data[ $data_key ] = $data_value;
$wp_theme_error = new WP_Error( $wp_theme_error_code, $message, $data );
return $wp_theme_error;
}
 
// merge old and new data
$old_data = $wp_theme_error->get_error_data( $wp_theme_error_code );
$new_data[ $data_key ] = $data_value;
$data = wp_parse_args( $new_data, $old_data );
 
return $wp_theme_error->add( $wp_theme_error_code, $message, $data );
}
 
 
/**
* Prints the error messages added to the global theme specific WP_Error object
*
* Only displays for users that have 'manage_options' capability,
* needs WP_DEBUG & WP_DEBUG_DISPLAY constants set to true.
* Doesn't output anything if there's no error object present.
*
* Adds the output to the 'shutdown' hook to render after the theme viewport is output.
*
* @return
*/
function print_theme_errors()
{
global $wp_theme_error, $wp_theme_error_code;
 
if ( ! current_user_can( 'manage_options' ) || ! is_wp_error( $wp_theme_error ) )
return;
 
# >>>> don't know what to do here
$messages = $wp_theme_error->get_error_messages( $wp_theme_error_code );
$data = $wp_theme_error->get_error_data( $wp_theme_error_code );
$errors = array_merge( $errors, $data );
$output = '';
foreach ( $errors as $error )
$output .= "{$error}\n";
# <<<< don't know what to do here
 
if ( defined( 'WP_DEBUG' ) && WP_DEBUG && defined( 'WP_DEBUG_DISPLAY' ) && WP_DEBUG_DISPLAY )
return var_dump( $output );
return;
}
add_action( 'shutdown', 'print_theme_errors', 9999 );

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.