Created
October 28, 2011 18:28
-
-
Save franz-josef-kaiser/1322989 to your computer and use it in GitHub Desktop.
Draft of "Theme Errors API"
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 | |
/** | |
* 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 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment