Skip to content

Commit b811666

Browse files
committed
modify the KSES filter to allow Font Awesome HTML
1 parent 68611f2 commit b811666

File tree

2 files changed

+156
-0
lines changed

2 files changed

+156
-0
lines changed
Lines changed: 153 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,153 @@
1+
<?php
2+
3+
namespace FortAwesome;
4+
5+
if ( ! defined( 'ABSPATH' ) ) {
6+
exit; // Exit if accessed directly.
7+
}
8+
9+
/**
10+
* Allow the <svg> element and its children and attributes.
11+
*/
12+
function allow_font_awesome_html( $allowed_html ) {
13+
add_filter('wp_kses_allowed_html', function ($tags, $context) use ($allowed_html) {
14+
foreach ( $allowed_html as $tag => $attributes ) {
15+
if (isset($tags[$tag]) && is_array($tags[$tag])) {
16+
// Merge with existing attributes
17+
$tags[$tag] = array_merge($tags[$tag], $attributes);
18+
} else {
19+
$tags[$tag] = $attributes;
20+
}
21+
}
22+
23+
return $tags;
24+
}, 10, 2 );
25+
}
26+
27+
function allowed_html() {
28+
/**
29+
* This is based on an analysis of the code in `@fortawesome/fontawesome-svg-core` that is responsible
30+
* for building the SVG elements. It may need to be updated if that code changes.
31+
*/
32+
return add_lowercase_attributes( array(
33+
'div' => array(
34+
'class' => true,
35+
'style' => true,
36+
),
37+
'svg' => array(
38+
'xmlns' => true,
39+
'viewBox' => true,
40+
'width' => true,
41+
'height' => true,
42+
'class' => true,
43+
'color' => true,
44+
'role' => true,
45+
'aria-hidden' => true,
46+
'aria-label' => true,
47+
'aria-labelledby' => true,
48+
'data-prefix' => true,
49+
'data-icon' => true,
50+
'data-fa-i2svg' => true,
51+
'data-fa-pseudo-element' => true,
52+
'focusable' => true,
53+
'style' => true,
54+
'transform-origin' => true,
55+
),
56+
'path' => array(
57+
'fill' => true,
58+
'opacity' => true,
59+
'd' => true,
60+
'class' => true,
61+
'transform' => true,
62+
),
63+
'span' => array(
64+
'class' => true,
65+
'style' => true,
66+
'aria-label' => true,
67+
'data-fa-i2svg' => true,
68+
),
69+
'g' => array(
70+
'class' => true,
71+
'transform' => true,
72+
),
73+
'symbol' => array(
74+
'id' => true,
75+
'viewBox' => true,
76+
'class' => true,
77+
'role' => true,
78+
'aria-hidden' => true,
79+
'aria-label' => true,
80+
'aria-labelledby' => true,
81+
'data-prefix' => true,
82+
'data-icon' => true,
83+
),
84+
'rect' => array(
85+
'x' => true,
86+
'y' => true,
87+
'width' => true,
88+
'height' => true,
89+
'fill' => true,
90+
'clip-path' => true,
91+
'mask' => true,
92+
),
93+
'circle' => array(
94+
'cx' => true,
95+
'cy' => true,
96+
'r' => true,
97+
'fill' => true,
98+
),
99+
'mask' => array(
100+
'x' => true,
101+
'y' => true,
102+
'width' => true,
103+
'height' => true,
104+
'id' => true,
105+
'maskUnits' => true,
106+
'maskContentUnits' => true,
107+
),
108+
'defs' => array(),
109+
'clipPath' => array(
110+
'id' => true,
111+
),
112+
'animate' => array(
113+
'attributeType' => true,
114+
'repeatCount' => true,
115+
'dur' => true,
116+
'attributeName' => true,
117+
'values' => true,
118+
),
119+
) );
120+
}
121+
122+
/**
123+
* Add lowercase versions of all attributes to the allowed HTML.
124+
*
125+
* This is necessary because some attribute names like `viewBox` will not be
126+
* matched by the KSES filter, which seems to lowercase attributes when filtering.
127+
*
128+
* So this will add `viewbox` as an allowed attribute alongside `viewBox`,
129+
* and will do the same for all other mixed-case attributes.
130+
*
131+
* @param array $allowed_html The original allowed HTML tags and attributes.
132+
* @return array The modified allowed HTML tags and attributes with lowercase attributes added.
133+
*/
134+
function add_lowercase_attributes( $allowed_html ) {
135+
foreach ( $allowed_html as $tag => $attributes ) {
136+
$additional_attributes = array();
137+
138+
if ( is_array( $attributes ) ) {
139+
foreach ( $attributes as $name => $value ) {
140+
$lowercase_name = strtolower( $name );
141+
if ( $lowercase_name !== $name ) {
142+
$additional_attributes[ $lowercase_name ] = $value;
143+
}
144+
}
145+
}
146+
147+
if ( ! empty( $additional_attributes ) ) {
148+
$allowed_html[ $tag ] = array_merge( $attributes, $additional_attributes );
149+
}
150+
}
151+
152+
return $allowed_html;
153+
}

block-editor/font-awesome-icon-block-init.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
}
88

99
require_once trailingslashit( FONTAWESOME_DIR_PATH ) . 'includes/class-fontawesome-svg-styles-manager.php';
10+
require_once trailingslashit( FONTAWESOME_DIR_PATH ) . 'block-editor/font-awesome-allowed-html.php';
1011

1112
/**
1213
* We need to register the block-editor script explicitly, instead of
@@ -108,4 +109,6 @@ function block_init() {
108109
}
109110

110111
register_block_type( __DIR__ . '/build' );
112+
113+
allow_font_awesome_html( allowed_html() );
111114
}

0 commit comments

Comments
 (0)