User.php (3631B)
1 <?php 2 /** 3 * Helper class for interacting with the user 4 */ 5 6 namespace Extendify\ExtendifySdk; 7 8 /** 9 * Helper class for interacting with the user 10 */ 11 class User 12 { 13 14 /** 15 * User unique, anonymous identifier 16 * 17 * @var string 18 */ 19 public $uuid = ''; 20 21 /** 22 * A WP user 23 * 24 * @var \WP_User 25 */ 26 protected $user = null; 27 28 /** 29 * The DB key for scoping 30 * 31 * @var string 32 */ 33 protected $key = 'extendifysdk_'; 34 35 /** 36 * The class instance. 37 * 38 * @var $instance 39 */ 40 protected static $instance = null; 41 42 /** 43 * Set up the user 44 * 45 * @param WP_User $user - A WP User object. 46 * @return void 47 */ 48 public function __construct($user) 49 { 50 $this->user = $user; 51 } 52 53 /** 54 * Return the user ID 55 * 56 * @return void 57 */ 58 private function setupUuid() 59 { 60 $uuid = \get_user_meta($this->user->ID, $this->key . 'uuid', true); 61 if (!$uuid) { 62 $id = \wp_hash(\wp_json_encode($this->user)); 63 \update_user_meta($this->user->ID, $this->key . 'uuid', $id); 64 } 65 66 $this->uuid = $uuid; 67 } 68 69 /** 70 * Returns data about the user 71 * Use it like User::data('ID') to get the user id 72 * 73 * @param string $arguments - Right now a string of arguments, like ID. 74 * @return mixed - Data about the user. 75 */ 76 private function dataHandler($arguments) 77 { 78 // Right now assume a single argument, but could expand to multiple. 79 if (isset($this->user->$arguments)) { 80 return $this->user->$arguments; 81 } 82 83 return \get_user_meta($this->user->ID, $this->key . $arguments, true); 84 } 85 86 /** 87 * Returns the application state for he current user 88 * Use it like User::data('ID') to get the user id 89 * 90 * @return string - JSON representation of the current state 91 */ 92 private function stateHandler() 93 { 94 $state = \get_user_meta($this->user->ID, $this->key . 'user_data'); 95 96 // Add some state boilerplate code for the first load. 97 if (!isset($state[0])) { 98 $state[0] = '{}'; 99 } 100 101 $userData = json_decode($state[0], true); 102 if (!isset($userData['version'])) { 103 $userData['version'] = 0; 104 } 105 106 // Get the current default number of imports allowed. 107 if (!isset($userData['state']['allowedImports'])) { 108 $currentImports = Http::get('/max-free-imports'); 109 $userData['state']['allowedImports'] = is_numeric($currentImports) && $currentImports > 0 ? $currentImports : 3; 110 } 111 112 $userData['state']['uuid'] = self::data('uuid'); 113 $userData['state']['canInstallPlugins'] = \current_user_can('install_plugins'); 114 $userData['state']['canActivatePlugins'] = \current_user_can('activate_plugins'); 115 116 return \wp_json_encode($userData); 117 } 118 119 /** 120 * Allows to dynamically setup the user with uuid 121 * Use it like User::data('ID') to get the user id 122 * 123 * @param string $name - The name of the method to call. 124 * @param array $arguments - The arguments to pass in. 125 * 126 * @return mixed 127 */ 128 public static function __callStatic($name, array $arguments) 129 { 130 $name = "{$name}Handler"; 131 if (is_null(self::$instance)) { 132 require_once ABSPATH . 'wp-includes/pluggable.php'; 133 self::$instance = new static(\wp_get_current_user()); 134 $r = self::$instance; 135 $r->setupUuid(); 136 } 137 138 $r = self::$instance; 139 return $r->$name(...$arguments); 140 } 141 }