| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentCart\Api; |
| 4 |
|
| 5 |
use FluentCart\Framework\Support\Arr; |
| 6 |
|
| 7 |
|
| 8 |
/** |
| 9 |
* todo - need to consult with heera bhai regarding this approach |
| 10 |
* |
| 11 |
*/ |
| 12 |
class Helper |
| 13 |
{ |
| 14 |
|
| 15 |
public static function getPermittedRoles() |
| 16 |
{ |
| 17 |
if (!current_user_can('manage_options')) { |
| 18 |
return array( |
| 19 |
'capability' => array(), |
| 20 |
'roles' => array() |
| 21 |
); |
| 22 |
} |
| 23 |
|
| 24 |
if (!function_exists('get_editable_roles')) { |
| 25 |
require_once ABSPATH . 'wp-admin/includes/user.php'; |
| 26 |
} |
| 27 |
$roles = \get_editable_roles(); |
| 28 |
|
| 29 |
$formatted = array(); |
| 30 |
foreach ($roles as $key => $role) { |
| 31 |
if ($key == 'administrator') { |
| 32 |
continue; |
| 33 |
} |
| 34 |
if ($key != 'subscriber') { |
| 35 |
$formatted[] = array( |
| 36 |
'name' => $role['name'], |
| 37 |
'key' => $key |
| 38 |
); |
| 39 |
} |
| 40 |
} |
| 41 |
|
| 42 |
$capability = fluent_cart_get_option('_fluent_cart_admin_permissions'); |
| 43 |
|
| 44 |
if (is_string($capability)) { |
| 45 |
$capability = []; |
| 46 |
} |
| 47 |
|
| 48 |
return array( |
| 49 |
'capability' => $capability, |
| 50 |
'roles' => $formatted |
| 51 |
); |
| 52 |
} |
| 53 |
|
| 54 |
|
| 55 |
public static function savePermissions($capability) |
| 56 |
{ |
| 57 |
if (current_user_can('manage_options')) { |
| 58 |
fluent_cart_update_option('_fluent_cart_admin_permissions', $capability); |
| 59 |
} else { |
| 60 |
throw new \Exception(esc_html__('Sorry, You can not update permissions. Only administrators can update permissions', 'fluent-cart')); |
| 61 |
} |
| 62 |
|
| 63 |
return array( |
| 64 |
'message' => esc_html__('Successfully updated the role(s).', 'fluent-cart') |
| 65 |
); |
| 66 |
} |
| 67 |
|
| 68 |
/** |
| 69 |
* |
| 70 |
* @param $val |
| 71 |
* @param $stack |
| 72 |
* @param $def |
| 73 |
* @return mixed|string |
| 74 |
*/ |
| 75 |
public static function getValWithinEnum($val, $stack, $def = '') |
| 76 |
{ |
| 77 |
|
| 78 |
return in_array($val, $stack) ? $val : $def; |
| 79 |
} |
| 80 |
|
| 81 |
/** |
| 82 |
* sanitize by fields |
| 83 |
* where $data should be an array like [key => value] |
| 84 |
* $fields will be like [key=>array( 'type'=> '', 'value'=> '')] |
| 85 |
*/ |
| 86 |
public static function sanitize($data, $fields) |
| 87 |
{ |
| 88 |
$sanitizers = [ |
| 89 |
'email' => function ($value) { |
| 90 |
if(empty($value)) { |
| 91 |
return ''; |
| 92 |
} |
| 93 |
|
| 94 |
return sanitize_email($value); |
| 95 |
}, |
| 96 |
'url' => 'esc_url_raw', |
| 97 |
'number' => 'floatval', |
| 98 |
'textarea' => 'sanitize_textarea_field', |
| 99 |
'text' => 'sanitize_text_field', |
| 100 |
'html_attr' => 'wp_kses_post', |
| 101 |
'provider' => 'sanitize_text_field', |
| 102 |
'validate' => 'sanitize_text_field', |
| 103 |
]; |
| 104 |
|
| 105 |
foreach ($fields as $key => $field) { |
| 106 |
if (!isset($data[$key])) { |
| 107 |
continue; |
| 108 |
} |
| 109 |
$type = $field['type'] ?? 'text'; |
| 110 |
// Skip non-user input types |
| 111 |
if (in_array($type, ['notice', 'tab'])) { |
| 112 |
continue; |
| 113 |
} |
| 114 |
if ($type === 'password') { |
| 115 |
continue; |
| 116 |
} |
| 117 |
$value = $data[$key]; |
| 118 |
if (is_array($value)) { |
| 119 |
$data[$key] = self::deepSanitize($value); |
| 120 |
} elseif (isset($sanitizers[$type]) && is_callable($sanitizers[$type])) { |
| 121 |
$data[$key] = call_user_func($sanitizers[$type], $value); |
| 122 |
} else { |
| 123 |
// Fallback to text sanitizer |
| 124 |
$data[$key] = sanitize_text_field($value); |
| 125 |
} |
| 126 |
} |
| 127 |
|
| 128 |
return $data; |
| 129 |
} |
| 130 |
|
| 131 |
protected static function deepSanitize($value) |
| 132 |
{ |
| 133 |
if (is_array($value)) { |
| 134 |
foreach ($value as $k => $v) { |
| 135 |
$value[$k] = self::deepSanitize($v); |
| 136 |
} |
| 137 |
return $value; |
| 138 |
} |
| 139 |
|
| 140 |
return sanitize_text_field($value); |
| 141 |
} |
| 142 |
|
| 143 |
|
| 144 |
public static function sanitizeTextAll($data = []) |
| 145 |
{ |
| 146 |
$sanitizedData = []; |
| 147 |
foreach ($data as $key => $value) { |
| 148 |
if (is_array($value)) { |
| 149 |
$sanitizedData[$key] = static::sanitizeTextAll($value); |
| 150 |
} else { |
| 151 |
$sanitizedData[$key] = sanitize_text_field($value); |
| 152 |
} |
| 153 |
} |
| 154 |
return $sanitizedData; |
| 155 |
} |
| 156 |
|
| 157 |
|
| 158 |
/** |
| 159 |
* to get dynamic select search by any query params for settings page |
| 160 |
*/ |
| 161 |
public static function getSearchOptions($data) |
| 162 |
{ |
| 163 |
$key = Arr::get($data, 'search_for', ''); |
| 164 |
$searchBy = Arr::get($data, 'search_by', ''); |
| 165 |
return apply_filters('fluent_cart/get_dynamic_search_' . $key, [], [ |
| 166 |
'searchBy' => $searchBy |
| 167 |
]); |
| 168 |
} |
| 169 |
|
| 170 |
/** |
| 171 |
* |
| 172 |
* |
| 173 |
* @param array $val |
| 174 |
* @param array $stack |
| 175 |
* @param array|string $def |
| 176 |
* @return array|string[] |
| 177 |
*/ |
| 178 |
// public static function getArrValWithinEnum(array $val, array $stack, $def = ''): array |
| 179 |
// { |
| 180 |
// $ret = []; |
| 181 |
// |
| 182 |
// if (empty($val)) { |
| 183 |
// |
| 184 |
// return []; |
| 185 |
// } |
| 186 |
// |
| 187 |
// foreach ($val as $item) { |
| 188 |
// if (in_array($item, $stack)) { |
| 189 |
// $ret[] = $item; |
| 190 |
// } |
| 191 |
// } |
| 192 |
// |
| 193 |
// if (empty($ret)) { |
| 194 |
// |
| 195 |
// if (is_array($def)) { |
| 196 |
// |
| 197 |
// return $def; |
| 198 |
// } |
| 199 |
// |
| 200 |
// return [$def]; |
| 201 |
// } |
| 202 |
// |
| 203 |
// return $ret; |
| 204 |
// } |
| 205 |
|
| 206 |
|
| 207 |
} |
| 208 |
|