Html_Processing_Helper
in package
Helper class for HTML processing and manipulation.
Table of Contents
- clean_css_classes() : string
- Clean CSS classes by removing background and border related classes.
- extract_url_from_text() : string
- Extract the first HTTP/HTTPS URL from a text string.
- get_caption_css_properties() : array<string|int, mixed>
- Get list of safe CSS properties for caption typography (excludes background properties).
- get_safe_css_properties() : array<string|int, mixed>
- Get list of safe CSS properties for typography and basic styling.
- remove_wrapper_handled_classes() : void
- Remove from an element the class names whose styles the renderer applies to the wrapping table cell.
- sanitize_caption_html() : string
- Sanitize caption HTML to allow only specific tags and attributes.
- sanitize_color() : string
- Sanitize color value to ensure it's a valid color format.
- sanitize_css_value() : string
- Sanitize CSS value to prevent injection attacks.
- sanitize_dimension_value() : string
- Sanitize dimension value to ensure it's a valid CSS dimension.
- sanitize_image_html() : string
- Sanitize image HTML while preserving necessary attributes for email rendering.
- validate_caption_attribute() : void
- Validate and sanitize specific caption attributes for security.
- validate_container_attributes() : bool
- Validate HTML container attributes for security before content extraction.
- get_allowed_caption_html() : array<string, array<string, bool>>
- Elements and attributes allowed in captions, in wp_kses() format.
- is_wrapper_handled_class() : bool
- Whether a single class name applies a background or border that the wrapping table cell already renders.
- normalize_rel_attribute() : string
- Normalize rel attribute by lowercasing, deduplicating tokens, and ensuring required tokens.
- sanitize_image_styles() : string
- Sanitize inline styles for image elements - only allow safe properties for email rendering.
Methods
clean_css_classes()
Clean CSS classes by removing background and border related classes.
public
static clean_css_classes(string $classes) : string
Parameters
- $classes : string
-
CSS classes to clean.
Return values
string — Cleaned CSS classes.extract_url_from_text()
Extract the first HTTP/HTTPS URL from a text string.
public
static extract_url_from_text(string $text) : string
Parameters
- $text : string
-
Text to search for URLs.
Return values
string — Extracted URL or empty string if not found.get_caption_css_properties()
Get list of safe CSS properties for caption typography (excludes background properties).
public
static get_caption_css_properties() : array<string|int, mixed>
Return values
array<string|int, mixed> — Array of safe CSS property names for captions.get_safe_css_properties()
Get list of safe CSS properties for typography and basic styling.
public
static get_safe_css_properties() : array<string|int, mixed>
Return values
array<string|int, mixed> — Array of safe CSS property names.remove_wrapper_handled_classes()
Remove from an element the class names whose styles the renderer applies to the wrapping table cell.
public
static remove_wrapper_handled_classes(WP_HTML_Tag_Processor $html) : void
Background and border classes are resolved by the CSS inliner wherever they appear. The wrapping cell keeps the block's original class list and receives the same styles inline, so leaving these classes on the inner element paints them a second time. For an opaque color that is invisible, but a translucent palette color composites over itself and renders as a visibly darker band inside the cell's padding.
Parameters
- $html : WP_HTML_Tag_Processor
-
Tag processor positioned on the element to clean.
Return values
void —sanitize_caption_html()
Sanitize caption HTML to allow only specific tags and attributes.
public
static sanitize_caption_html(string $caption_html) : string
Parameters
- $caption_html : string
-
Raw caption HTML.
Return values
string — Sanitized caption HTML.sanitize_color()
Sanitize color value to ensure it's a valid color format.
public
static sanitize_color(string $color) : string
Supports hex colors, rgb/rgba, hsl/hsla, named colors, and CSS variables.
Parameters
- $color : string
-
The color value to sanitize.
Return values
string — Sanitized color value or safe default if invalid.sanitize_css_value()
Sanitize CSS value to prevent injection attacks.
public
static sanitize_css_value(string $value) : string
Parameters
- $value : string
-
CSS value to sanitize.
Return values
string — Sanitized CSS value or empty string if invalid.sanitize_dimension_value()
Sanitize dimension value to ensure it's a valid CSS dimension.
public
static sanitize_dimension_value(mixed $value) : string
Supports numeric values (converted to px) and standard CSS units.
Parameters
- $value : mixed
-
The dimension value to sanitize.
Return values
string — Sanitized dimension value or empty string if invalid.sanitize_image_html()
Sanitize image HTML while preserving necessary attributes for email rendering.
public
static sanitize_image_html(string $image_html) : string
Parameters
- $image_html : string
-
Raw image HTML.
Return values
string — Sanitized image HTML.validate_caption_attribute()
Validate and sanitize specific caption attributes for security.
public
static validate_caption_attribute(WP_HTML_Tag_Processor $html, string $attr_name) : void
Parameters
- $html : WP_HTML_Tag_Processor
-
HTML tag processor.
- $attr_name : string
-
Attribute name to validate.
Return values
void —validate_container_attributes()
Validate HTML container attributes for security before content extraction.
public
static validate_container_attributes(string $container_html) : bool
This method checks if a container element (like figcaption, span) has safe attributes.
Parameters
- $container_html : string
-
Full container HTML (e.g.,
content ).
Return values
bool — True if container attributes are safe, false otherwise.get_allowed_caption_html()
Elements and attributes allowed in captions, in wp_kses() format.
private
static get_allowed_caption_html() : array<string, array<string, bool>>
Attribute values are narrowed further by validate_caption_attribute().
Return values
array<string, array<string, bool>> — Allowed HTML for wp_kses().is_wrapper_handled_class()
Whether a single class name applies a background or border that the wrapping table cell already renders.
private
static is_wrapper_handled_class(string $class_name) : bool
Parameters
- $class_name : string
-
Class name to test.
Return values
bool — True when the class should not stay on the inner element.normalize_rel_attribute()
Normalize rel attribute by lowercasing, deduplicating tokens, and ensuring required tokens.
private
static normalize_rel_attribute(string|null $rel_value[, bool $require_security_tokens = false ]) : string
Parameters
- $rel_value : string|null
-
Current rel attribute value.
- $require_security_tokens : bool = false
-
Whether to require noopener and noreferrer tokens.
Return values
string — Normalized rel attribute value.sanitize_image_styles()
Sanitize inline styles for image elements - only allow safe properties for email rendering.
private
static sanitize_image_styles(string $style_value) : string
Parameters
- $style_value : string
-
Raw style value.
