-
-
Save tomjn/d63acb2be62e777d6110 to your computer and use it in GitHub Desktop.
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 | |
$code = ''; | |
if ( isset( $instance[ 'code' ] ) ) { | |
$code = $instance[ 'code' ]; | |
// catch PHP tags | |
if ( strpos( $code, '<?' ) !== false ) { | |
echo '<div class="error"><p>Embed Code error: You can\'t include PHP code here, it will not work, and will be shown on the frontend. Please remove the PHP code and contact a developer. PHP code is surrounded by <?php and ?></p></div>'; | |
} | |
$dom = new \DOMDocument(); | |
libxml_use_internal_errors( true ); | |
$dom->loadHtml( $code ); | |
$errors = libxml_get_errors(); | |
foreach ( $errors as $error ) { | |
$return = '<strong>Issue found in the embed code:</strong><br>'; | |
switch ( $error->level ) { | |
case LIBXML_ERR_WARNING: | |
$return .= "Warning $error->code: "; | |
break; | |
case LIBXML_ERR_ERROR: | |
$return .= "Error $error->code: "; | |
break; | |
case LIBXML_ERR_FATAL: | |
$return .= "Fatal Error $error->code: "; | |
break; | |
} | |
$return .= trim( $error->message ) . | |
"<br> Line: $error->line" . | |
", Column: $error->column"; | |
echo '<div class="error"><p>'.wp_kses_post( $return ).'</p></div>'; | |
} | |
libxml_clear_errors(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment