Нет описания

User.php 5.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. <?php
  2. /*******************************************************************************
  3. * Copyright (c) 2019, Code Atlantic LLC
  4. ******************************************************************************/
  5. if ( ! defined( 'ABSPATH' ) ) {
  6. exit;
  7. }
  8. /**
  9. * Core class used to implement the custom WP_User object.
  10. *
  11. * @property string $nickname
  12. * @property string $description
  13. * @property string $user_description
  14. * @property string $first_name
  15. * @property string $user_firstname
  16. * @property string $last_name
  17. * @property string $user_lastname
  18. * @property string $user_login
  19. * @property string $user_pass
  20. * @property string $user_nicename
  21. * @property string $user_email
  22. * @property string $user_url
  23. * @property string $user_registered
  24. * @property string $user_activation_key
  25. * @property string $user_status
  26. * @property int $user_level
  27. * @property string $display_name
  28. * @property string $spam
  29. * @property string $deleted
  30. * @property string $locale
  31. */
  32. abstract class PUM_Abstract_Model_User {
  33. /**
  34. * The current model version.
  35. *
  36. * Used for compatibility testing.
  37. * 1 - v1.0.0
  38. *
  39. * @var int
  40. */
  41. public $model_version = 1;
  42. /**
  43. * The version of the data currently stored for the current item.
  44. *
  45. * 1 - v1.0.0
  46. *
  47. * @var int
  48. */
  49. public $data_version;
  50. /**
  51. * The user's ID.
  52. *
  53. * @var int
  54. */
  55. public $ID = 0;
  56. /**
  57. * @var \WP_User
  58. */
  59. public $user;
  60. /**
  61. * @var array An array of keys that can be accessed via the $this->user (WP_User) object.
  62. */
  63. public $core_data_keys = array(
  64. 'nickname',
  65. 'description',
  66. 'user_description',
  67. 'first_name',
  68. 'user_firstname',
  69. 'last_name',
  70. 'user_lastname',
  71. 'user_login',
  72. 'user_pass',
  73. 'user_nicename',
  74. 'user_email',
  75. 'user_url',
  76. 'user_registered',
  77. 'user_activation_key',
  78. 'user_status',
  79. 'user_level',
  80. 'display_name',
  81. 'spam',
  82. 'deleted',
  83. 'locale',
  84. 'data',
  85. 'ID',
  86. 'caps',
  87. 'cap_key',
  88. 'roles',
  89. 'allcaps',
  90. 'filter',
  91. );
  92. /**
  93. * The required permission|user_role|capability|user_level of the user.
  94. */
  95. protected $required_permission = '';
  96. /**
  97. * Get things going
  98. *
  99. * @param WP_User|int $user
  100. */
  101. public function __construct( $user ) {
  102. if ( ! is_a( $user, 'WP_User' ) ) {
  103. $user = new WP_User( $user );
  104. }
  105. $this->setup( $user );
  106. }
  107. /**
  108. * Given the user data, let's set the variables
  109. *
  110. * @param WP_User $user The User Object
  111. */
  112. protected function setup( $user ) {
  113. if ( ! is_a( $user, 'WP_User' ) || ( $this->required_permission && ! $user->has_cap( $this->required_permission ) ) ) {
  114. return;
  115. }
  116. if ( ! isset( $user->data->ID ) ) {
  117. $user->data->ID = 0;
  118. }
  119. $this->user = $user;
  120. // Set $this->ID based on the users ID.
  121. $this->ID = $user->ID;
  122. }
  123. /**
  124. * @param $key
  125. *
  126. * @return bool
  127. */
  128. public function __isset( $key ) {
  129. if ( in_array( $key, $this->core_data_keys ) ) {
  130. return isset( $this->user->$key );
  131. }
  132. }
  133. /**
  134. * @param $key
  135. */
  136. public function __unset( $key ) {
  137. if ( in_array( $key, $this->core_data_keys ) ) {
  138. unset( $this->user->$key );
  139. }
  140. }
  141. /**
  142. * Magic __get function to dispatch a call to retrieve a private property
  143. *
  144. * @param $key
  145. *
  146. * @return mixed|WP_Error
  147. */
  148. public function __get( $key ) {
  149. if ( in_array( $key, $this->core_data_keys ) ) {
  150. return $this->user->$key;
  151. } elseif ( method_exists( $this, 'get_' . $key ) ) {
  152. return call_user_func( array( $this, 'get_' . $key ) );
  153. } else {
  154. $meta = get_user_meta( $this->ID, $key, true );
  155. if ( $meta ) {
  156. return $meta;
  157. }
  158. return new WP_Error( 'user-invalid-property', sprintf( __( 'Can\'t get property %s' ), $key ) );
  159. }
  160. }
  161. /**
  162. * @param $name
  163. * @param $arguments
  164. *
  165. * @return mixed
  166. */
  167. public function __call( $name, $arguments ) {
  168. if ( method_exists( $this->user, $name ) ) {
  169. return call_user_func_array( array( $this->user, $name ), $arguments );
  170. }
  171. }
  172. /**
  173. * Get per site or global user options.
  174. *
  175. * @param $key
  176. *
  177. * @return mixed
  178. */
  179. public function get_option( $key ) {
  180. return get_user_option( $key, $this->ID );
  181. }
  182. /**
  183. * Used to set per site or global user options.
  184. *
  185. * @param $key
  186. * @param $value
  187. * @param bool $global
  188. *
  189. * @return bool|int
  190. */
  191. public function update_option( $key, $value, $global = false ) {
  192. return update_user_option( $this->ID, $key, $value, $global );
  193. }
  194. /**
  195. * Used to delete per site or global user options.
  196. *
  197. * @param $key
  198. * @param bool $global
  199. *
  200. * @return bool
  201. */
  202. public function delete_option( $key, $global = false ) {
  203. return delete_user_option( $this->ID, $key, $global );
  204. }
  205. /**
  206. * Get user meta.
  207. *
  208. * @param $key
  209. * @param bool $single
  210. *
  211. * @return mixed
  212. */
  213. public function get_meta( $key, $single = true ) {
  214. return get_user_meta( $this->ID, $key, $single );
  215. }
  216. /**
  217. * Add user meta.
  218. *
  219. * @param $key
  220. * @param $value
  221. *
  222. * @return bool|int
  223. */
  224. public function add_meta( $key, $value, $unique = false ) {
  225. return add_user_meta( $this->ID, $key, $value, $unique );
  226. }
  227. /**
  228. * Update user meta.
  229. *
  230. * @param $key
  231. * @param $value
  232. *
  233. * @return bool|int
  234. */
  235. public function update_meta( $key, $value ) {
  236. return update_user_meta( $this->ID, $key, $value );
  237. }
  238. /**
  239. * Delete user meta.
  240. *
  241. * @param $key
  242. * @param $value
  243. *
  244. * @return bool|int
  245. */
  246. public function delete_meta( $key, $value = "" ) {
  247. return delete_user_meta( $this->ID, $key, $value );
  248. }
  249. /**
  250. * @param int $size
  251. *
  252. * @return false|string
  253. */
  254. public function get_avatar( $size = 35 ) {
  255. return get_avatar( $this->ID, $size );
  256. }
  257. /**
  258. * Convert object to array.
  259. *
  260. * @return array Object as array.
  261. */
  262. public function to_array() {
  263. $user = $this->user->to_array();
  264. foreach ( get_object_vars( $this ) as $k => $v ) {
  265. $user[ $k ] = $v;
  266. }
  267. return $user;
  268. }
  269. }