Constants
3 weeks ago
Contracts
3 weeks ago
Hooks
3 weeks ago
Models
3 weeks ago
BaseHook.php
3 weeks ago
HookServiceProvider.php
3 weeks ago
Menu.php
3 weeks ago
User.php
3 weeks ago
UserMeta.php
3 weeks ago
UserMeta.php
150 lines
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * Class to handle the user meta Provides static methods for getting, setting, updating, and deleting user meta. |
| 5 | * |
| 6 | * @package Framework |
| 7 | * @subpackage Wordpress |
| 8 | * @since 1.0.0 |
| 9 | */ |
| 10 | namespace Kirki\Framework\Wordpress; |
| 11 | |
| 12 | \defined('ABSPATH') || exit; |
| 13 | use function Kirki\Framework\with_prefix; |
| 14 | use function Kirki\Framework\without_prefix; |
| 15 | class UserMeta |
| 16 | { |
| 17 | /** |
| 18 | * Retrieve user meta by ID and key. |
| 19 | * |
| 20 | * @param int $user_id The user id. |
| 21 | * @param string $key The key. |
| 22 | * @param bool $single The single. |
| 23 | * @param bool $with_prefix The with prefix. |
| 24 | * |
| 25 | * @return mixed |
| 26 | * |
| 27 | * @since 1.0.0 |
| 28 | */ |
| 29 | public static function get(int $user_id, string $key = '', bool $single = \true, bool $with_prefix = \true) |
| 30 | { |
| 31 | if ($key === '') { |
| 32 | $key = $with_prefix ? with_prefix($key) : $key; |
| 33 | return get_user_meta($user_id, $key, $single); |
| 34 | } |
| 35 | $metadata = get_user_meta($user_id, '', $single); |
| 36 | $updated_metadata = []; |
| 37 | foreach ($metadata as $name => $value) { |
| 38 | $name = $with_prefix ? without_prefix($name) : $name; |
| 39 | $updated_metadata[$name] = $single ? $value[0] : $value; |
| 40 | } |
| 41 | return $updated_metadata; |
| 42 | } |
| 43 | /** |
| 44 | * Get all user meta by ID. |
| 45 | * |
| 46 | * @param int $user_id The user id. |
| 47 | * @param mixed $single The single. |
| 48 | * @param bool $with_prefix The with prefix. |
| 49 | * |
| 50 | * @return array |
| 51 | * |
| 52 | * @since 1.0.0 |
| 53 | */ |
| 54 | public static function get_all(int $user_id, $single = \true, bool $with_prefix = \true) |
| 55 | { |
| 56 | return static::get($user_id, '', $single, $with_prefix); |
| 57 | } |
| 58 | /** |
| 59 | * Add the value of a specific user meta. |
| 60 | * |
| 61 | * @param int $user_id The user id. |
| 62 | * @param string $key The key. |
| 63 | * @param mixed $value The value. |
| 64 | * @param mixed $unique The unique. |
| 65 | * @param bool $with_prefix The with prefix. |
| 66 | * |
| 67 | * @return bool|int |
| 68 | * |
| 69 | * @since 1.0.0 |
| 70 | */ |
| 71 | public static function add(int $user_id, string $key, $value, $unique = \false, bool $with_prefix = \true) |
| 72 | { |
| 73 | $key = $with_prefix ? with_prefix($key) : $key; |
| 74 | return add_user_meta($user_id, $key, $value, $unique); |
| 75 | } |
| 76 | /** |
| 77 | * Add multiple user meta at once. |
| 78 | * |
| 79 | * @param int $user_id The user id. |
| 80 | * @param array $meta_input The meta input. |
| 81 | * @param bool $with_prefix The with prefix. |
| 82 | * |
| 83 | * @return bool |
| 84 | * |
| 85 | * @since 1.0.0 |
| 86 | */ |
| 87 | public static function add_many(int $user_id, array $meta_input, bool $with_prefix = \true) |
| 88 | { |
| 89 | foreach ($meta_input as $key => $value) { |
| 90 | static::add($user_id, $key, $value, \false, $with_prefix); |
| 91 | } |
| 92 | return \true; |
| 93 | } |
| 94 | /** |
| 95 | * Update the value of a specific user meta. |
| 96 | * |
| 97 | * @param int $user_id The user id. |
| 98 | * @param string $key The key. |
| 99 | * @param mixed $value The value. |
| 100 | * @param mixed $prev_value The prev value. |
| 101 | * @param bool $with_prefix The with prefix. |
| 102 | * |
| 103 | * @return bool|int |
| 104 | * |
| 105 | * @since 1.0.0 |
| 106 | */ |
| 107 | public static function update(int $user_id, string $key, $value, $prev_value = '', bool $with_prefix = \true) |
| 108 | { |
| 109 | $key = $with_prefix ? with_prefix($key) : $key; |
| 110 | return update_user_meta($user_id, $key, $value, $prev_value); |
| 111 | } |
| 112 | /** |
| 113 | * Update multiple user meta at once. |
| 114 | * |
| 115 | * @param int $user_id The user id. |
| 116 | * @param array $meta_input The meta input. |
| 117 | * @param bool $with_prefix The with prefix. |
| 118 | * |
| 119 | * @return bool |
| 120 | * |
| 121 | * @since 1.0.0 |
| 122 | */ |
| 123 | public static function update_many(int $user_id, array $meta_input, bool $with_prefix = \true) |
| 124 | { |
| 125 | foreach ($meta_input as $key => $value) { |
| 126 | static::update($user_id, $key, $value, '', $with_prefix); |
| 127 | } |
| 128 | return \true; |
| 129 | } |
| 130 | /** |
| 131 | * Delete a specific user meta. |
| 132 | * |
| 133 | * @param int $user_id The user id. |
| 134 | * @param string $key The key. |
| 135 | * @param mixed $value Metadata value. If provided, |
| 136 | * rows will only be removed that match the value. |
| 137 | * Must be serializable if non-scalar. Default empty. |
| 138 | * @param bool $with_prefix The with prefix. |
| 139 | * |
| 140 | * @return bool |
| 141 | * |
| 142 | * @since 1.0.0 |
| 143 | */ |
| 144 | public static function delete(int $user_id, string $key, $value = '', bool $with_prefix = \true) |
| 145 | { |
| 146 | $key = $with_prefix ? with_prefix($key) : $key; |
| 147 | return delete_user_meta($user_id, $key, $value); |
| 148 | } |
| 149 | } |
| 150 |