From ac92e3c9ca2004361839e66b6dc7a2dad13e0b3f Mon Sep 17 00:00:00 2001 From: Jonnyauk Date: Wed, 22 Jun 2016 15:20:33 +0100 Subject: [PATCH] New function wfx_allowed_simple_tags() Returns array of limited HTML tags to be used with kses or similar. - You shouldn't use wp_kses() much - it can be a-little intensive! - However, sometimes we need it to clean user input to only allow certain tags so there is no funny business! --- functions.php | 27 ++++++- wf-includes/wf-data-management.php | 124 ++++++++++++++++++++++++++++- wf-includes/wf-engine.php | 3 +- 3 files changed, 149 insertions(+), 5 deletions(-) diff --git a/functions.php b/functions.php index 8d5ae6e..1107d86 100644 --- a/functions.php +++ b/functions.php @@ -447,19 +447,42 @@ /** * Returns array of common HTML tags to be used with kses or similar. - * Use filter 'wflux_allowed_tags' to mainpulate allowed tags + * You shouldn't use wp_kses() much - it can be a-little intensive! + * However, sometimes we need it to clean user input to only allow certain tags so there is no funny business! + * + * Filters available: + * wflux_allowed_tags - Array containing allowed tags * * @since 1.1 * @version 1.1 * * @param none - * @return [array] Allowed tags array + * @return [array] Allowed tags */ if ( !function_exists( 'wfx_allowed_tags' ) ) : function wfx_allowed_tags() { global $wfx_data_manage; return $wfx_data_manage->allowed_tags(); } endif; +/** + * Returns array of limited HTML tags to be used with kses or similar. + * You shouldn't use wp_kses() much - it can be a-little intensive! + * However, sometimes we need it to clean user input to only allow certain tags so there is no funny business! + * + * @since 2.3 + * @version 2.3 + * + * @param [string] $type Required - Type of tags to return text/simple/headings [text] + * - text => Sutable for wrapping inside your own block level elements - a, br, span, b, strong and i + * - simple => Similar to 'text' param, much more limited, no links or text styling tags = span, br + * - headings => Just headings, nothing else = h1, h2, h3, h4, h5, h6 + * @return [array] Allowed tags + */ +if ( !function_exists( 'wfx_allowed_simple_tags' ) ) : function wfx_allowed_simple_tags($input) { + global $wfx_data_manage; return $wfx_data_manage->allowed_simple_tags($input); +} endif; + + /** * Strips white space and other cruft in html type output * diff --git a/wf-includes/wf-data-management.php b/wf-includes/wf-data-management.php index 005742a..a502e2c 100644 --- a/wf-includes/wf-data-management.php +++ b/wf-includes/wf-data-management.php @@ -5,14 +5,20 @@ */ class wflux_data_manage { + /** * Returns array of common HTML tags to be used with kses or similar. - * Use filter 'wflux_allowed_tags' to mainpulate allowed tags + * You shouldn't use wp_kses() much - it can be a-little intensive! + * However, sometimes we need it to clean user input to only allow certain tags so there is no funny business! + * + * Filters available: + * wflux_allowed_tags - Array containing allowed tags * * @since 1.1 * @version 1.1 * - * @return [array] Allowed tags array + * @param none + * @return [array] Allowed tags */ function wf_allowed_tags(){ @@ -409,6 +415,120 @@ function wf_allowed_tags(){ } + /** + * Returns array of limited HTML tags to be used with kses or similar. + * You shouldn't use wp_kses() much - it can be a-little intensive! + * However, sometimes we need it to clean user input to only allow certain tags so there is no funny business! + * + * @since 2.3 + * @version 2.3 + * + * @param [string] $type Required - Type of tags to return text/simple/headings [text] + * - text => Sutable for wrapping inside your own block level elements - a, br, span, b, strong and i + * - simple => Similar to 'text' param, much more limited, no links or text styling tags = span, br + * - headings => Just headings, nothing else = h1, h2, h3, h4, h5, h6 + * @return [array] Allowed tags + */ + function wf_allowed_simple_tags( $input='text' ) { + + // Default is first in array + $types = array( + 'text', + 'notags', + 'simple', + 'headings' + ); + + $input = ( !isset($input) ) ? $types[0] : $input; + + $type = ( !in_array($input, $types) ) ? $types[0] : $input; + + switch ( $type ) { + + case 'simple': + + $output = array ( + 'span' => array( + 'class'=>array(), + 'id'=>array() + ), + 'br' => array() + ); + + break; + + case 'headings': + + $output = array ( + 'h1' => array( + 'align' => true, + 'class' => true, + 'id' => true, + 'style' => true + ), + 'h2' => array ( + 'align' => true, + 'class' => true, + 'id' => true, + 'style' => true + ), + 'h3' => array ( + 'align' => true, + 'class' => true, + 'id' => true, + 'style' => true + ), + 'h4' => array ( + 'align' => true, + 'class' => true, + 'id' => true, + 'style' => true + ), + 'h5' => array ( + 'align' => true, + 'class' => true, + 'id' => true, + 'style' => true + ), + 'h6' => array ( + 'align' => true, + 'class' => true, + 'id' => true, + 'style' => true + ) + ); + + break; + + default: + + // text + $output = array( + 'a' => array( + 'href'=> array(), + 'title'=> array(), + 'class'=>array(), + 'id'=>array() + ), + 'span' => array( + 'class'=>array(), + 'id'=>array() + ), + 'br' => array(), + 'b' => array(), + 'strong' => array(), + 'i' => array() + ); + + break; + + } + + return $output; + + } + + /** * Strips white space and other cruft in html type output * diff --git a/wf-includes/wf-engine.php b/wf-includes/wf-engine.php index f8da515..8e9662c 100644 --- a/wf-includes/wf-engine.php +++ b/wf-includes/wf-engine.php @@ -122,13 +122,14 @@ function wflux_capacitor_data_manage() { /** * @since 1.1 -* @updated 2.2 +* @updated 2.3 * Creates all Wonderflux core data handling/cleaning functions */ class wflux_data_manage_all { public $wflux_data_manage_do; function __construct(){ $this->wflux_data_manage_do = new wflux_data_manage; } function allowed_tags(){ return $this->wflux_data_manage_do->wf_allowed_tags(); } + function allowed_simple_tags($input){ return $this->wflux_data_manage_do->wf_allowed_simple_tags($input); } function strip_whitespace($input){ return $this->wflux_data_manage_do->wf_strip_whitespace($input); } function valid_url($input){ return $this->wflux_data_manage_do->wf_valid_url($input); } function valid_hex_colour($input){ return $this->wflux_data_manage_do->wf_valid_hex_colour($input); }