| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* The Sanitizer class |
| 5 |
*/ |
| 6 |
|
| 7 |
namespace Extendify\Shared\Services; |
| 8 |
|
| 9 |
defined('ABSPATH') || die('No direct access.'); |
| 10 |
|
| 11 |
/** |
| 12 |
* Class for sanitizing various data types. |
| 13 |
*/ |
| 14 |
|
| 15 |
class Sanitizer |
| 16 |
{ |
| 17 |
/** |
| 18 |
* This function will sanitize a value. |
| 19 |
* |
| 20 |
* @param mixed $data - The data we need to sanitize. |
| 21 |
* @return array|string |
| 22 |
*/ |
| 23 |
public static function sanitizeUnknown($data) |
| 24 |
{ |
| 25 |
return is_array($data) ? self::sanitizeArray($data) : self::sanitizeText($data); |
| 26 |
} |
| 27 |
/** |
| 28 |
* This function will sanitize a multidimensional array, |
| 29 |
* if the value is number it will be returned as is. |
| 30 |
* |
| 31 |
* @param array $array - The array we need to sanitize. |
| 32 |
* @return array |
| 33 |
*/ |
| 34 |
public static function sanitizeArray($array) |
| 35 |
{ |
| 36 |
return array_map(function ($value) { |
| 37 |
if (is_array($value)) { |
| 38 |
return self::sanitizeArray($value); |
| 39 |
} elseif (is_int($value) || is_float($value)) { |
| 40 |
return $value; |
| 41 |
} else { |
| 42 |
return \sanitize_textarea_field($value); |
| 43 |
} |
| 44 |
}, $array); |
| 45 |
} |
| 46 |
|
| 47 |
/** |
| 48 |
* This function will sanitize the user selections. |
| 49 |
* |
| 50 |
* @param array $array - The array we need to sanitize. |
| 51 |
* @return array |
| 52 |
*/ |
| 53 |
public static function sanitizeUserSelections($array) |
| 54 |
{ |
| 55 |
return array_map(function ($value) { |
| 56 |
if (is_array($value)) { |
| 57 |
return self::sanitizeUserSelections($value); |
| 58 |
} |
| 59 |
|
| 60 |
if (is_int($value) || is_float($value)) { |
| 61 |
return $value; |
| 62 |
} |
| 63 |
|
| 64 |
return self::sanitizePostContent($value); |
| 65 |
}, $array); |
| 66 |
} |
| 67 |
|
| 68 |
/** |
| 69 |
* This function will sanitize a text field. |
| 70 |
* |
| 71 |
* @param string $text - The string we need to sanitize. |
| 72 |
* @return string |
| 73 |
*/ |
| 74 |
public static function sanitizeText($text) |
| 75 |
{ |
| 76 |
return \sanitize_text_field($text); |
| 77 |
} |
| 78 |
|
| 79 |
/** |
| 80 |
* This function will sanitize a text field that may contain |
| 81 |
* the <strong>, <em> and <del> HTML tags, allowing the tags in |
| 82 |
* the final result. |
| 83 |
* |
| 84 |
* @param string $text - The string we need to sanitize. |
| 85 |
* @return string |
| 86 |
*/ |
| 87 |
public static function sanitizeTextWithFormattingTags($text) |
| 88 |
{ |
| 89 |
$allowedTags = [ |
| 90 |
'strong' => [], |
| 91 |
'em' => [], |
| 92 |
'del' => [], |
| 93 |
]; |
| 94 |
|
| 95 |
return \wp_kses($text, $allowedTags); |
| 96 |
} |
| 97 |
|
| 98 |
/** |
| 99 |
* This function will sanitize a textarea field. |
| 100 |
* |
| 101 |
* @param string $text - The strings we need to sanitize. |
| 102 |
* @return string |
| 103 |
*/ |
| 104 |
public static function sanitizeTextarea($text) |
| 105 |
{ |
| 106 |
return \sanitize_textarea_field($text); |
| 107 |
} |
| 108 |
|
| 109 |
/** |
| 110 |
* This function will sanitize a gutenberg block. |
| 111 |
* |
| 112 |
* @param string $blockString - The strings we need to sanitize. |
| 113 |
* @return string |
| 114 |
*/ |
| 115 |
public static function sanitizeBlocks($blockString) |
| 116 |
{ |
| 117 |
$parsed = parse_blocks($blockString); |
| 118 |
return \serialize_blocks($parsed); |
| 119 |
} |
| 120 |
|
| 121 |
/** |
| 122 |
* This function will sanitize the post content. |
| 123 |
* |
| 124 |
* @param string $content - The post content we need to sanitize. |
| 125 |
* @return string |
| 126 |
*/ |
| 127 |
public static function sanitizePostContent($content) |
| 128 |
{ |
| 129 |
return \wp_kses_post($content ? $content : ''); |
| 130 |
} |
| 131 |
} |
| 132 |
|