Automattic\WooCommerce\EmailEditor\Engine\Renderer
Html2Text::convert
Converts HTML into plain text format
Method of the class: Html2Text{}
No Hooks.
Returns
String. The HTML converted to text.
Usage
$result = Html2Text::convert( $html, $options ): string;
- $html(string) (required)
- The input HTML.
- $options
- .
Default:array()
Html2Text::convert() Html2Text::convert code WC 10.7.0
public static function convert( string $html, $options = array() ): string {
if ( false === $options || true === $options ) {
// Using old style (< 1.0) of passing in options.
$options = array( 'ignore_errors' => $options );
}
$options = array_merge( static::default_options(), $options );
// Check all options are valid.
foreach ( array_keys( $options ) as $key ) {
if ( ! in_array( $key, array_keys( static::default_options() ), true ) ) {
// Log invalid option for debugging purposes without exposing in exception.
// phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_error_log -- Security: Logging sensitive data separately from user-facing exception messages.
error_log( 'Html2Text: Invalid option provided: ' . htmlspecialchars( (string) $key, ENT_QUOTES, 'UTF-8' ) . '. Valid options are: ' . htmlspecialchars( implode( ',', array_keys( static::default_options() ) ), ENT_QUOTES, 'UTF-8' ) );
// Throw generic error message to avoid exposing user input.
throw new \InvalidArgumentException( 'Invalid option provided for html2text conversion.' );
}
}
$is_office_document = self::is_office_document( $html );
if ( $is_office_document ) {
// Remove office namespace.
$html = str_replace( array( '<o:p>', '</o:p>' ), '', $html );
}
$html = self::fix_newlines( $html );
// Use mb_convert_encoding for legacy versions of php.
if ( PHP_MAJOR_VERSION * 10 + PHP_MINOR_VERSION < 81 && mb_detect_encoding( $html, 'UTF-8', true ) ) {
$converted = mb_convert_encoding( $html, 'HTML-ENTITIES', 'UTF-8' );
$html = false !== $converted ? $converted : $html;
}
// Ensure $html is always a string before passing to get_document.
if ( ! is_string( $html ) ) {
$html = (string) $html;
}
$doc = self::get_document( $html, $options );
$output = self::iterate_over_node( $doc, null, false, $is_office_document, $options );
// Process output for whitespace/newlines.
$output = self::process_whitespace_newlines( $output );
return $output;
}