| 1 |
<?php |
| 2 |
/** |
| 3 |
* @package FireBox |
| 4 |
* @version 2.1.14 Free |
| 5 |
* |
| 6 |
* @author FirePlugins <info@fireplugins.com> |
| 7 |
* @link https://www.fireplugins.com |
| 8 |
* @copyright Copyright © 2024 FirePlugins All Rights Reserved |
| 9 |
* @license GNU GPLv3 <http://www.gnu.org/licenses/gpl.html> or later |
| 10 |
*/ |
| 11 |
|
| 12 |
namespace FireBox\Core\Helpers\Form; |
| 13 |
|
| 14 |
if (!defined('ABSPATH')) |
| 15 |
{ |
| 16 |
exit; // Exit if accessed directly. |
| 17 |
} |
| 18 |
|
| 19 |
class Field |
| 20 |
{ |
| 21 |
/** |
| 22 |
* Get Field Name given a field block. |
| 23 |
* |
| 24 |
* @param array $block |
| 25 |
* |
| 26 |
* @return string |
| 27 |
*/ |
| 28 |
public static function getFieldName($block) |
| 29 |
{ |
| 30 |
if (isset($block['attrs']['fieldName'])) |
| 31 |
{ |
| 32 |
return $block['attrs']['fieldName']; |
| 33 |
} |
| 34 |
|
| 35 |
return str_replace('firebox/', '', $block['blockName']); |
| 36 |
} |
| 37 |
|
| 38 |
/** |
| 39 |
* Get the field label. |
| 40 |
* |
| 41 |
* @param array $block |
| 42 |
* |
| 43 |
* @return string |
| 44 |
*/ |
| 45 |
public static function getFieldLabel($block) |
| 46 |
{ |
| 47 |
$label = isset($block['attrs']['fieldLabel']) ? $block['attrs']['fieldLabel'] : ''; |
| 48 |
|
| 49 |
if (empty($label)) |
| 50 |
{ |
| 51 |
$type = self::getFieldType($block['blockName']); |
| 52 |
|
| 53 |
$label = sprintf(firebox()->_('FB_X_FIELD'), ucfirst($type)); |
| 54 |
} |
| 55 |
|
| 56 |
return $label; |
| 57 |
} |
| 58 |
|
| 59 |
/** |
| 60 |
* Returns the field tpye given a field block name. |
| 61 |
* |
| 62 |
* @param string $blockName |
| 63 |
* |
| 64 |
* @return string |
| 65 |
*/ |
| 66 |
public static function getFieldType($blockName) |
| 67 |
{ |
| 68 |
return str_replace('firebox/', '', $blockName); |
| 69 |
} |
| 70 |
|
| 71 |
/** |
| 72 |
* Returns the field class instance. |
| 73 |
* |
| 74 |
* @param array $params |
| 75 |
* |
| 76 |
* @return /FireBox/Core/Form/Fields/Field |
| 77 |
*/ |
| 78 |
public static function getFieldClass($params) |
| 79 |
{ |
| 80 |
$type = isset($params['type']) ? $params['type'] : ''; |
| 81 |
if (!$type) |
| 82 |
{ |
| 83 |
return; |
| 84 |
} |
| 85 |
|
| 86 |
$class = '\FireBox\Core\Form\Fields\Fields\\' . ucfirst($type); |
| 87 |
if (!class_exists($class)) |
| 88 |
{ |
| 89 |
return; |
| 90 |
} |
| 91 |
|
| 92 |
return new $class($params); |
| 93 |
} |
| 94 |
|
| 95 |
/** |
| 96 |
* Convert all applicable characters to HTML entities |
| 97 |
* |
| 98 |
* @param string $input The input string. |
| 99 |
* |
| 100 |
* @return string |
| 101 |
*/ |
| 102 |
public static function escape($input) |
| 103 |
{ |
| 104 |
if (!is_string($input)) |
| 105 |
{ |
| 106 |
return $input; |
| 107 |
} |
| 108 |
|
| 109 |
// Convert all HTML tags to HTML entities. |
| 110 |
$input = htmlspecialchars($input, ENT_NOQUOTES, 'UTF-8'); |
| 111 |
|
| 112 |
// We do not need any Smart Tag replacements take place here, so we need to escape curly brackets too. |
| 113 |
$input = str_replace(['{', '}'], ['{', '}'], $input); |
| 114 |
|
| 115 |
// Respect newline characters, by converting them to <br> tags. |
| 116 |
$input = nl2br($input); |
| 117 |
|
| 118 |
return $input; |
| 119 |
} |
| 120 |
} |