| 1 |
<?php |
| 2 |
/** |
| 3 |
* @package FirePlugins Framework |
| 4 |
* @version 1.0.27 |
| 5 |
* |
| 6 |
* @author FirePlugins <[email protected]> |
| 7 |
* @link https://www.fireplugins.com |
| 8 |
* @copyright Copyright © 2022 FirePlugins All Rights Reserved |
| 9 |
* @license GNU GPLv3 <http://www.gnu.org/licenses/gpl.html> or later |
| 10 |
*/ |
| 11 |
|
| 12 |
namespace FPFramework\Helpers; |
| 13 |
|
| 14 |
if (!defined('ABSPATH')) |
| 15 |
{ |
| 16 |
exit; // Exit if accessed directly. |
| 17 |
} |
| 18 |
|
| 19 |
class FieldsHelper |
| 20 |
{ |
| 21 |
/** |
| 22 |
* Transforms an array(key, value) into html attributes |
| 23 |
* |
| 24 |
* @param array $atts |
| 25 |
* |
| 26 |
* @return string |
| 27 |
*/ |
| 28 |
public static function getHTMLAttributes($atts) |
| 29 |
{ |
| 30 |
if (!$atts) |
| 31 |
{ |
| 32 |
return ''; |
| 33 |
} |
| 34 |
|
| 35 |
$atts = (array) $atts; |
| 36 |
|
| 37 |
if (!is_array($atts)) |
| 38 |
{ |
| 39 |
return ''; |
| 40 |
} |
| 41 |
|
| 42 |
$html = implode( |
| 43 |
'', |
| 44 |
array_map( |
| 45 |
function ($k, $v) |
| 46 |
{ |
| 47 |
if ($v == '' || (empty($v) && $v != 0)) |
| 48 |
{ |
| 49 |
return ''; |
| 50 |
} |
| 51 |
|
| 52 |
$v = is_array($v) ? htmlspecialchars(json_encode($v)) : $v; |
| 53 |
|
| 54 |
return $k . '="' . esc_attr($v) . '" '; |
| 55 |
}, |
| 56 |
array_keys($atts), |
| 57 |
$atts |
| 58 |
) |
| 59 |
); |
| 60 |
|
| 61 |
return !empty($html) ? ' ' . $html : ''; |
| 62 |
} |
| 63 |
|
| 64 |
/** |
| 65 |
* Searches for a name in an array and returns its value or a default value. |
| 66 |
* Can find value of keys : [name][name2][name3] in array |
| 67 |
* |
| 68 |
* @param string $name |
| 69 |
* @param string $default |
| 70 |
* @param array $values |
| 71 |
* |
| 72 |
* @return string |
| 73 |
*/ |
| 74 |
public static function findFieldValueInArray($name, $default, $values) |
| 75 |
{ |
| 76 |
if (!is_string($name)) |
| 77 |
{ |
| 78 |
return null; |
| 79 |
} |
| 80 |
|
| 81 |
if (!is_array($values) || empty($values)) |
| 82 |
{ |
| 83 |
return null; |
| 84 |
} |
| 85 |
|
| 86 |
$values = is_object($values) ? (array) $values : $values; |
| 87 |
|
| 88 |
// field name: [name][name2][name3] |
| 89 |
if (substr_count($name, '[') > 1) |
| 90 |
{ |
| 91 |
// split name |
| 92 |
$splited_name = explode('[', $name); |
| 93 |
unset($splited_name[0]); |
| 94 |
|
| 95 |
// find value |
| 96 |
$tmp_value = $values; |
| 97 |
|
| 98 |
foreach ($splited_name as $key => $_name) |
| 99 |
{ |
| 100 |
$tmp_value = is_object($tmp_value) ? (array) $tmp_value : $tmp_value; |
| 101 |
|
| 102 |
$_name = str_replace(']', '', $_name); |
| 103 |
if (!isset($tmp_value[$_name])) |
| 104 |
{ |
| 105 |
// key does not exist, return default |
| 106 |
$tmp_value = $default; |
| 107 |
break; |
| 108 |
} |
| 109 |
else |
| 110 |
{ |
| 111 |
if (empty($tmp_value[$_name]) && $tmp_value[$_name] !== '0') |
| 112 |
{ |
| 113 |
$tmp_value = $default; |
| 114 |
break; |
| 115 |
} |
| 116 |
} |
| 117 |
|
| 118 |
$tmp_value = $tmp_value[$_name]; |
| 119 |
} |
| 120 |
|
| 121 |
// return value |
| 122 |
return $tmp_value; |
| 123 |
} |
| 124 |
else |
| 125 |
{ |
| 126 |
// if the name contains the name prefix, remove it |
| 127 |
$name = strstr($name, '[') ? strstr($name, '[') : $name; |
| 128 |
// remove all brackets so we can search the data with a clean name |
| 129 |
$name = str_replace(['[', ']'], '', $name); |
| 130 |
|
| 131 |
return isset($values[$name]) && (!empty($values[$name]) || $values[$name] == '0') ? $values[$name] : $default; |
| 132 |
} |
| 133 |
} |
| 134 |
} |