| 1 |
<?php |
| 2 |
/** |
| 3 |
* @package FirePlugins Framework |
| 4 |
* @version 1.2.3 |
| 5 |
* |
| 6 |
* @author FirePlugins <[email protected]> |
| 7 |
* @link https://www.fireplugins.com |
| 8 |
* @copyright Copyright © 2026 FirePlugins All Rights Reserved |
| 9 |
* @license GNU GPLv3 <http://www.gnu.org/licenses/gpl.html> or later |
| 10 |
*/ |
| 11 |
|
| 12 |
namespace FPFramework\Base; |
| 13 |
|
| 14 |
if (!defined('ABSPATH')) |
| 15 |
{ |
| 16 |
exit; // Exit if accessed directly. |
| 17 |
} |
| 18 |
|
| 19 |
class User |
| 20 |
{ |
| 21 |
/** |
| 22 |
* Per-request memoized result of get() for the current active user (no field/value given), |
| 23 |
* keyed by the logged-in user ID (0 for guests) so it stays correct if the current user |
| 24 |
* changes within the same PHP process (e.g. wp_set_current_user() in tests/WP-CLI/cron). |
| 25 |
* |
| 26 |
* @var array |
| 27 |
*/ |
| 28 |
private static $currentUserCache = []; |
| 29 |
|
| 30 |
/** |
| 31 |
* Return the user object |
| 32 |
* |
| 33 |
* @param mixed $value The user identifier |
| 34 |
* @param mixed $field The field to search for (id | ID | slug | email | login) |
| 35 |
* |
| 36 |
* @return mixed object on success, null on failure |
| 37 |
*/ |
| 38 |
public static function get($value = null, $field = 'ID') |
| 39 |
{ |
| 40 |
// Return current active user |
| 41 |
if (empty($field) || empty($value)) |
| 42 |
{ |
| 43 |
$current_user_id = get_current_user_id(); |
| 44 |
|
| 45 |
if (array_key_exists($current_user_id, self::$currentUserCache)) |
| 46 |
{ |
| 47 |
return self::$currentUserCache[$current_user_id]; |
| 48 |
} |
| 49 |
|
| 50 |
if (!is_user_logged_in()) |
| 51 |
{ |
| 52 |
return (self::$currentUserCache[$current_user_id] = new \WP_User); |
| 53 |
} |
| 54 |
|
| 55 |
$field = 'ID'; |
| 56 |
$value = $current_user_id; |
| 57 |
|
| 58 |
if (!self::exists($value)) |
| 59 |
{ |
| 60 |
return (self::$currentUserCache[$current_user_id] = null); |
| 61 |
} |
| 62 |
|
| 63 |
return (self::$currentUserCache[$current_user_id] = get_user_by($field, $value)); |
| 64 |
} |
| 65 |
|
| 66 |
if (!self::exists($value)) |
| 67 |
{ |
| 68 |
return; |
| 69 |
} |
| 70 |
|
| 71 |
return get_user_by($field, $value); |
| 72 |
} |
| 73 |
|
| 74 |
/** |
| 75 |
* Checks whether the user does exist in the database |
| 76 |
* |
| 77 |
* @param integer $value The user identifier |
| 78 |
* |
| 79 |
* @return bool |
| 80 |
*/ |
| 81 |
public static function exists($value) |
| 82 |
{ |
| 83 |
$hash = 'fpf_user' . $value; |
| 84 |
|
| 85 |
// check cache |
| 86 |
if ($user = wp_cache_get($hash)) |
| 87 |
{ |
| 88 |
return $user; |
| 89 |
} |
| 90 |
|
| 91 |
$user = get_user_by('ID', $value); |
| 92 |
|
| 93 |
// set cache |
| 94 |
wp_cache_set($hash, $user); |
| 95 |
|
| 96 |
return $user; |
| 97 |
} |
| 98 |
|
| 99 |
/** |
| 100 |
* Get the IP address of the user |
| 101 |
* |
| 102 |
* @return string |
| 103 |
*/ |
| 104 |
public static function getIP() |
| 105 |
{ |
| 106 |
// Whether ip is from the share internet |
| 107 |
if (!empty($_SERVER['HTTP_CLIENT_IP'])) |
| 108 |
{ |
| 109 |
return sanitize_text_field(wp_unslash($_SERVER['HTTP_CLIENT_IP'])); |
| 110 |
} |
| 111 |
|
| 112 |
//whether ip is from the proxy |
| 113 |
if (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) |
| 114 |
{ |
| 115 |
return sanitize_text_field(wp_unslash($_SERVER['HTTP_X_FORWARDED_FOR'])); |
| 116 |
} |
| 117 |
|
| 118 |
return isset($_SERVER['REMOTE_ADDR']) ? sanitize_text_field(wp_unslash($_SERVER['REMOTE_ADDR'])) : ''; |
| 119 |
} |
| 120 |
} |