safecss_filter_attr

Inline CSS filter.

Description

(string) safecss_filter_attr( (string) $css, (string) $deprecated = '' ); 

Returns (string)

Filtered string of CSS rules.

Parameters (2)

0. $css (string)
A string of CSS rules.
1. $deprecated — Optional. (string) => ''
The deprecated.

Usage

  1. if ( !function_exists( 'safecss_filter_attr' ) ) { 
  2. require_once ABSPATH . WPINC . '/kses.php'; 
  3.  
  4. // A string of CSS rules. 
  5. $css = ''; 
  6.  
  7. // The deprecated. 
  8. $deprecated = ''; 
  9.  
  10. // NOTICE! Understand what this does before running. 
  11. $result = safecss_filter_attr($css, $deprecated); 
  12.  

Defined (1)

The function is defined in the following location(s).

/wp-includes/kses.php  
  1. function safecss_filter_attr( $css, $deprecated = '' ) { 
  2. if ( !empty( $deprecated ) ) 
  3. _deprecated_argument( __FUNCTION__, '2.8.1' ); // Never implemented 
  4.  
  5. $css = wp_kses_no_null($css); 
  6. $css = str_replace(array("\n", "\r", "\t"), '', $css); 
  7.  
  8. if ( preg_match( '%[\\\\(&=}]|/\*%', $css ) ) // remove any inline css containing \ ( & } = or comments 
  9. return ''; 
  10.  
  11. $css_array = explode( ';', trim( $css ) ); 
  12.  
  13. /** 
  14. * Filters list of allowed CSS attributes. 
  15. * @since 2.8.1 
  16. * @since 4.4.0 Added support for `min-height`, `max-height`, `min-width`, and `max-width`. 
  17. * @since 4.6.0 Added support for `list-style-type`. 
  18. * @param array $attr List of allowed CSS attributes. 
  19. */ 
  20. $allowed_attr = apply_filters( 'safe_style_css', array( 
  21. 'background',  
  22. 'background-color',  
  23.  
  24. 'border',  
  25. 'border-width',  
  26. 'border-color',  
  27. 'border-style',  
  28. 'border-right',  
  29. 'border-right-color',  
  30. 'border-right-style',  
  31. 'border-right-width',  
  32. 'border-bottom',  
  33. 'border-bottom-color',  
  34. 'border-bottom-style',  
  35. 'border-bottom-width',  
  36. 'border-left',  
  37. 'border-left-color',  
  38. 'border-left-style',  
  39. 'border-left-width',  
  40. 'border-top',  
  41. 'border-top-color',  
  42. 'border-top-style',  
  43. 'border-top-width',  
  44.  
  45. 'border-spacing',  
  46. 'border-collapse',  
  47. 'caption-side',  
  48.  
  49. 'color',  
  50. 'font',  
  51. 'font-family',  
  52. 'font-size',  
  53. 'font-style',  
  54. 'font-variant',  
  55. 'font-weight',  
  56. 'letter-spacing',  
  57. 'line-height',  
  58. 'text-decoration',  
  59. 'text-indent',  
  60. 'text-align',  
  61.  
  62. 'height',  
  63. 'min-height',  
  64. 'max-height',  
  65.  
  66. 'width',  
  67. 'min-width',  
  68. 'max-width',  
  69.  
  70. 'margin',  
  71. 'margin-right',  
  72. 'margin-bottom',  
  73. 'margin-left',  
  74. 'margin-top',  
  75.  
  76. 'padding',  
  77. 'padding-right',  
  78. 'padding-bottom',  
  79. 'padding-left',  
  80. 'padding-top',  
  81.  
  82. 'clear',  
  83. 'cursor',  
  84. 'direction',  
  85. 'float',  
  86. 'overflow',  
  87. 'vertical-align',  
  88. 'list-style-type',  
  89. ) ); 
  90.  
  91. if ( empty($allowed_attr) ) 
  92. return $css; 
  93.  
  94. $css = ''; 
  95. foreach ( $css_array as $css_item ) { 
  96. if ( $css_item == '' ) 
  97. continue; 
  98. $css_item = trim( $css_item ); 
  99. $found = false; 
  100. if ( strpos( $css_item, ':' ) === false ) { 
  101. $found = true; 
  102. } else { 
  103. $parts = explode( ':', $css_item ); 
  104. if ( in_array( trim( $parts[0] ), $allowed_attr ) ) 
  105. $found = true; 
  106. if ( $found ) { 
  107. if( $css != '' ) 
  108. $css .= ';'; 
  109. $css .= $css_item; 
  110.  
  111. return $css;