Skip to content

Commit

Permalink
chore: move stackable_allow_wp_kses_allowed_html to separate file (#3217
Browse files Browse the repository at this point in the history
)
  • Loading branch information
Arukuen committed Jul 11, 2024
1 parent 86c8781 commit b72b1ba
Show file tree
Hide file tree
Showing 3 changed files with 174 additions and 164 deletions.
1 change: 1 addition & 0 deletions plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,7 @@ function is_frontend() {
require_once( plugin_dir_path( __FILE__ ) . 'src/pro.php' );
require_once( plugin_dir_path( __FILE__ ) . 'src/jetpack.php' );
require_once( plugin_dir_path( __FILE__ ) . 'src/multisite.php' );
require_once( plugin_dir_path( __FILE__ ) . 'src/kses.php' );
require_once( plugin_dir_path( __FILE__ ) . 'src/dynamic-breakpoints.php' );
require_once( plugin_dir_path( __FILE__ ) . 'src/design-library/init.php' );
require_once( plugin_dir_path( __FILE__ ) . 'src/global-settings.php' );
Expand Down
173 changes: 173 additions & 0 deletions src/kses.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,173 @@
<?php
/**
* Stackable KSES function.
*/

// Exit if accessed directly.
if ( ! defined( 'ABSPATH' ) ) {
exit;
}

if ( ! function_exists( 'stackable_allow_wp_kses_allowed_html' ) ) {

/**
* Fix block saving for Non-Super-Admins (no unfiltered_html capability).
* For Non-Super-Admins, some styles & HTML tags/attributes are removed upon saving,
* this allows Stackable HTML tags & attributes from being saved.
*
* For every Stackable block, add the HTML tags and attributes used here.
*
* @see The list of tags & attributes currently allowed: https://core.trac.wordpress.org/browser/tags/5.2/src/wp-includes/kses.php#L61
* @see https://github.com/gambitph/Stackable/issues/184
*
* @param array $tags Allowed HTML tags & attributes.
* @param string $context The context wherein the HTML is being filtered.
*
* @return array Modified HTML tags & attributes.
*/
function stackable_allow_wp_kses_allowed_html( $tags, $context ) {
$tags['style'] = array(
'id' => true,
'class' => true,
'type' => true,
);

// Used by Separators & Icons.
$tags['svg'] = array(
'viewbox' => true,
'filter' => true,
'enablebackground' => true,
'xmlns' => true,
'class' => true,
'preserveaspectratio' => true,
'aria-hidden' => true,
'data-*' => true,
'role' => true,
'height' => true,
'width' => true,
'style' => true,
);
$tags['path'] = array(
'd' => true,
);
$tags['circle'] = array(
'cx' => true,
'cy' => true,
'r' => true,
);
$tags['polygon'] = array(
'points' => true,
);
$tags['polyline'] = array(
'points' => true,
);
$tags['rect'] = array(
'x' => true,
'y' => true,
'width' => true,
'height' => true,
'rx' => true,
'ry' => true,
);
$tags['line'] = array(
'x1' => true,
'x2' => true,
'y1' => true,
'y2' => true,
);
$tags['clippath'] = array();
$tags['filter'] = array();
$tags['g'] = array();
$tags['text'] = array(
'font-size' => true,
'font-family' => true,
'font-weight' => true,
'font-*' => true,
);
$tags['fegaussianblur'] = array(
'in' => true,
'stddeviation' => true,
);
$tags['fecomponenttransfer'] = array();
$tags['fefunca'] = array(
'type' => true,
'slope' => true,
);
$tags['femerge'] = array();
$tags['femergenode'] = array(
'in' => true,
);
// SVG gradients.
$tags['defs'] = array();
$tags['stop'] = array(
'offset' => true,
'style' => true,
'stop-color' => true,
'stop-opacity' => true,
);
$tags['lineargradient'] = array( // Intentionally made this lowercase since tags do not recognize caps.
'id' => true,
'x1' => true,
'x2' => true,
'y1' => true,
'y2' => true,
'gradientunits' => true,
'gradienttransform' => true,
);

// Used by posts block.
$tags['time'] = array(
'datetime' => true,
);

_stackable_svg_attributes( $tags, 'path' );
_stackable_svg_attributes( $tags, 'circle' );
_stackable_svg_attributes( $tags, 'polygon' );
_stackable_svg_attributes( $tags, 'polyline' );
_stackable_svg_attributes( $tags, 'line' );
_stackable_svg_attributes( $tags, 'rect' );
_stackable_svg_attributes( $tags, 'g' );
_stackable_svg_attributes( $tags, 'clippath' );
_stackable_svg_attributes( $tags, 'filter' );
_stackable_svg_attributes( $tags, 'text' );

_stackable_common_attributes( $tags, 'div' );
_stackable_common_attributes( $tags, 'h1' );
_stackable_common_attributes( $tags, 'h2' );
_stackable_common_attributes( $tags, 'h3' );
_stackable_common_attributes( $tags, 'h4' );
_stackable_common_attributes( $tags, 'h5' );
_stackable_common_attributes( $tags, 'h6' );
_stackable_common_attributes( $tags, 'svg' );

return $tags;
}

function _stackable_svg_attributes( &$tags, $tag ) {
$tags[ $tag ]['id'] = true;
$tags[ $tag ]['class'] = true;
$tags[ $tag ]['style'] = true;
$tags[ $tag ]['fill'] = true;
$tags[ $tag ]['fill-rule'] = true;
$tags[ $tag ]['fill-opacity'] = true;
$tags[ $tag ]['fill-*'] = true;
$tags[ $tag ]['clip-path'] = true;
$tags[ $tag ]['transform'] = true;
$tags[ $tag ]['stroke'] = true;
$tags[ $tag ]['stroke-width'] = true;
$tags[ $tag ]['stroke-linejoin'] = true;
$tags[ $tag ]['stroke-miterlimit'] = true;
$tags[ $tag ]['stroke-*'] = true;
$tags[ $tag ]['opacity'] = true;
}

function _stackable_common_attributes( &$tags, $tag ) {
$tags[ $tag ]['aria-hidden'] = true; // Used by Separators & Icons
$tags[ $tag ]['aria-expanded'] = true; // Used by Expand block.
$tags[ $tag ]['aria-level'] = true; // Used by Accordion block.
$tags[ $tag ]['role'] = true; // Used by Accordion block.
$tags[ $tag ]['tabindex'] = true; // Used by Accordion block.
}

add_filter( 'wp_kses_allowed_html', 'stackable_allow_wp_kses_allowed_html', 10, 2 );
}
164 changes: 0 additions & 164 deletions src/multisite.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,170 +43,6 @@ function stackable_allow_safe_style_css( $styles ) {
add_filter( 'safe_style_css', 'stackable_allow_safe_style_css' );
}

if ( ! function_exists( 'stackable_allow_wp_kses_allowed_html' ) ) {

/**
* Fix block saving for Non-Super-Admins (no unfiltered_html capability).
* For Non-Super-Admins, some styles & HTML tags/attributes are removed upon saving,
* this allows Stackable HTML tags & attributes from being saved.
*
* For every Stackable block, add the HTML tags and attributes used here.
*
* @see The list of tags & attributes currently allowed: https://core.trac.wordpress.org/browser/tags/5.2/src/wp-includes/kses.php#L61
* @see https://github.com/gambitph/Stackable/issues/184
*
* @param array $tags Allowed HTML tags & attributes.
* @param string $context The context wherein the HTML is being filtered.
*
* @return array Modified HTML tags & attributes.
*/
function stackable_allow_wp_kses_allowed_html( $tags, $context ) {
$tags['style'] = array(
'id' => true,
'class' => true,
'type' => true,
);

// Used by Separators & Icons.
$tags['svg'] = array(
'viewbox' => true,
'filter' => true,
'enablebackground' => true,
'xmlns' => true,
'class' => true,
'preserveaspectratio' => true,
'aria-hidden' => true,
'data-*' => true,
'role' => true,
'height' => true,
'width' => true,
'style' => true,
);
$tags['path'] = array(
'd' => true,
);
$tags['circle'] = array(
'cx' => true,
'cy' => true,
'r' => true,
);
$tags['polygon'] = array(
'points' => true,
);
$tags['polyline'] = array(
'points' => true,
);
$tags['rect'] = array(
'x' => true,
'y' => true,
'width' => true,
'height' => true,
'rx' => true,
'ry' => true,
);
$tags['line'] = array(
'x1' => true,
'x2' => true,
'y1' => true,
'y2' => true,
);
$tags['clippath'] = array();
$tags['filter'] = array();
$tags['g'] = array();
$tags['text'] = array(
'font-size' => true,
'font-family' => true,
'font-weight' => true,
'font-*' => true,
);
$tags['fegaussianblur'] = array(
'in' => true,
'stddeviation' => true,
);
$tags['fecomponenttransfer'] = array();
$tags['fefunca'] = array(
'type' => true,
'slope' => true,
);
$tags['femerge'] = array();
$tags['femergenode'] = array(
'in' => true,
);
// SVG gradients.
$tags['defs'] = array();
$tags['stop'] = array(
'offset' => true,
'style' => true,
'stop-color' => true,
'stop-opacity' => true,
);
$tags['lineargradient'] = array( // Intentionally made this lowercase since tags do not recognize caps.
'id' => true,
'x1' => true,
'x2' => true,
'y1' => true,
'y2' => true,
'gradientunits' => true,
'gradienttransform' => true,
);

// Used by posts block.
$tags['time'] = array(
'datetime' => true,
);

_stackable_svg_attributes( $tags, 'path' );
_stackable_svg_attributes( $tags, 'circle' );
_stackable_svg_attributes( $tags, 'polygon' );
_stackable_svg_attributes( $tags, 'polyline' );
_stackable_svg_attributes( $tags, 'line' );
_stackable_svg_attributes( $tags, 'rect' );
_stackable_svg_attributes( $tags, 'g' );
_stackable_svg_attributes( $tags, 'clippath' );
_stackable_svg_attributes( $tags, 'filter' );
_stackable_svg_attributes( $tags, 'text' );

_stackable_common_attributes( $tags, 'div' );
_stackable_common_attributes( $tags, 'h1' );
_stackable_common_attributes( $tags, 'h2' );
_stackable_common_attributes( $tags, 'h3' );
_stackable_common_attributes( $tags, 'h4' );
_stackable_common_attributes( $tags, 'h5' );
_stackable_common_attributes( $tags, 'h6' );
_stackable_common_attributes( $tags, 'svg' );

return $tags;
}

function _stackable_svg_attributes( &$tags, $tag ) {
$tags[ $tag ]['id'] = true;
$tags[ $tag ]['class'] = true;
$tags[ $tag ]['style'] = true;
$tags[ $tag ]['fill'] = true;
$tags[ $tag ]['fill-rule'] = true;
$tags[ $tag ]['fill-opacity'] = true;
$tags[ $tag ]['fill-*'] = true;
$tags[ $tag ]['clip-path'] = true;
$tags[ $tag ]['transform'] = true;
$tags[ $tag ]['stroke'] = true;
$tags[ $tag ]['stroke-width'] = true;
$tags[ $tag ]['stroke-linejoin'] = true;
$tags[ $tag ]['stroke-miterlimit'] = true;
$tags[ $tag ]['stroke-*'] = true;
$tags[ $tag ]['opacity'] = true;
}

function _stackable_common_attributes( &$tags, $tag ) {
$tags[ $tag ]['aria-hidden'] = true; // Used by Separators & Icons
$tags[ $tag ]['aria-expanded'] = true; // Used by Expand block.
$tags[ $tag ]['aria-level'] = true; // Used by Accordion block.
$tags[ $tag ]['role'] = true; // Used by Accordion block.
$tags[ $tag ]['tabindex'] = true; // Used by Accordion block.
}

add_filter( 'wp_kses_allowed_html', 'stackable_allow_wp_kses_allowed_html', 10, 2 );
}

if ( is_multisite() && ! function_exists( 'stackable_fix_gt_style_errors' ) ) {

/**
Expand Down

0 comments on commit b72b1ba

Please sign in to comment.