Advertisement
Guest User

Untitled

a guest
Jun 26th, 2017
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.07 KB | None | 0 0
  1. <?php
  2. /**
  3. * LifterLMS User Data Abstract
  4. *
  5. * @since 3.9.0
  6. * @version 3.9.0
  7. */
  8.  
  9. if ( ! defined( 'ABSPATH' ) ) { exit; }
  10.  
  11. abstract class LLMS_Abstract_User_Data {
  12.  
  13. /**
  14. * Student's WordPress User ID
  15. * @var int
  16. */
  17. private $id;
  18.  
  19. /**
  20. * User postmeta key prefix
  21. * @var string
  22. */
  23. private $meta_prefix = 'llms_';
  24.  
  25. /**
  26. * Instance of the WP_User
  27. * @var obj
  28. */
  29. private $user;
  30.  
  31. /**
  32. * Constructor
  33. *
  34. * If no user id provided, will attempt to use the current user id
  35. * @param mixed $user WP_User ID, instance of WP_User, or instance of any student class extending this class
  36. * @return void
  37. * @since 2.2.3
  38. * @version 3.9.0
  39. */
  40. public function __construct( $user = null ) {
  41.  
  42. $user = $this->get_user_id( $user );
  43. if ( false !== $user ) {
  44. $this->id = $user;
  45. $this->user = get_user_by( 'ID', $user );
  46. }
  47.  
  48. }
  49.  
  50. /**
  51. * Magic Getter for User Data
  52. * Mapped directly to the WP_User class
  53. * @param string $key key of the property to get a value for
  54. * @return mixed
  55. * @since 3.0.0
  56. * @version 3.9.0
  57. */
  58. public function __get( $key ) {
  59.  
  60. // array of items we should *not* add the $this->meta_prefix to
  61. $unprefixed = apply_filters( 'llms_student_unprefixed_metas', array(
  62. 'description',
  63. 'display_name',
  64. 'first_name',
  65. 'last_name',
  66. 'nickname',
  67. 'user_login',
  68. 'user_nicename',
  69. 'user_email',
  70. 'user_registered',
  71. ), $this );
  72.  
  73. // add the meta prefix to things that aren't in the above array
  74. // only if the meta prefix isn't already there
  75. // this means that the following will output the same data
  76. // $this->get( 'llms_billing_address_1')
  77. // $this->get( 'billing_address_1')
  78. if ( false === strpos( $key, $this->meta_prefix ) && ! in_array( $key, $unprefixed ) ) {
  79. $key = $this->meta_prefix . $key;
  80. }
  81.  
  82. if ( ! $this->exists() ) {
  83. return '';
  84. }
  85.  
  86. return apply_filters( 'llms_get_student_meta_' . $key, $this->user->get( $key ), $this );
  87.  
  88. }
  89.  
  90. /**
  91. * Determine if the user exists
  92. * @return boolean
  93. * @since 3.9.0
  94. * @version 3.9.0
  95. */
  96. public function exists() {
  97. return ( $this->user && $this->user->exists() );
  98. }
  99.  
  100. /**
  101. * Allows direct access to WP_User object for retrieving user data from the user or usermeta tables
  102. * @since 3.0.0
  103. * @version 3.0.0
  104. * @param string $key key of the property to get a value for
  105. * @return mixed
  106. */
  107. public function get( $key ) {
  108. return $this->$key;
  109. }
  110.  
  111. /**
  112. * Retrieve the user id
  113. * @return int
  114. * @since 3.9.0
  115. * @version 3.9.0
  116. */
  117. public function get_id() {
  118. return $this->id;
  119. }
  120.  
  121. /**
  122. * Allow extending classes to access the main student class
  123. * @return LLMS_Student|false
  124. * @since 3.9.0
  125. * @version 3.9.0
  126. */
  127. protected function get_student() {
  128. return llms_get_student( $this->get_id() );
  129. }
  130.  
  131. /**
  132. * Retrieve the instance of the WP User for the student
  133. * @return WP_User
  134. * @since 3.9.0
  135. * @version 3.9.0
  136. */
  137. public function get_user() {
  138. return $this->user;
  139. }
  140.  
  141. /**
  142. * Retrieve the User ID based on object
  143. * @param mixed $user WP_User ID, instance of WP_User, or instance of any student class extending this class
  144. * @return mixed int if a user id can be found, otherwise fale
  145. * @since 3.9.0
  146. * @version 3.9.0
  147. */
  148. protected function get_user_id( $user ) {
  149.  
  150. if ( ! $user && get_current_user_id() ) {
  151. return get_current_user_id();
  152. } elseif ( is_numeric( $user ) ) {
  153. return $user;
  154. } elseif ( is_a( $user, 'WP_User' ) && isset( $user->ID ) ) {
  155. return $user->ID;
  156. } elseif ( $user instanceof LLMS_Abstract_User_Data ) {
  157. return $user->get_id();
  158. }
  159.  
  160. return false;
  161.  
  162. }
  163.  
  164. /**
  165. * Update a meta property for the user
  166. * @param string $key meta key
  167. * @param mixed $value meta value
  168. * @param boolean $prefix include the meta prefix when setting
  169. * passing false will allow 3rd parties to update fields with a custom prefix
  170. * @since 3.2.0
  171. * @version 3.2.0
  172. */
  173. public function set( $key, $value, $prefix = true ) {
  174. $key = $prefix ? $this->meta_prefix . $key : $key;
  175. update_user_meta( $this->get_id(), $key, $value );
  176. }
  177.  
  178. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement