/core/user.php

https://bitbucket.org/cyberhobo/easy-subscribe · PHP · 211 lines · 112 code · 25 blank · 74 comment · 11 complexity · 66b177d2e2369fd45f7d597bce55f157 MD5 · raw file

  1. <?php
  2. /**
  3. * User-centric behavior
  4. *
  5. * Static methods are generally hooks.
  6. *
  7. * Instantiation augments a WP_User.
  8. */
  9. class ES_User {
  10. const SUBSCRIBED_TERM_META_KEY = 'es_subscribed_term';
  11. /** @var int user ID */
  12. private $_id;
  13. /** @var WP_User user object */
  14. private $_user;
  15. /**
  16. * Create an EasySubscribe user.
  17. *
  18. * @param int|WP_User $user_id_or_object
  19. */
  20. public function __construct( $user_id_or_object ) {
  21. if ( is_a( $user_id_or_object, 'WP_User' ) ) {
  22. $this->_user = $user_id_or_object;
  23. $this->_id = $this->_user->ID;
  24. } else {
  25. $this->_id = intval( $user_id_or_object );
  26. }
  27. }
  28. /**
  29. * When a user is deleted, remove their subscriptions.
  30. *
  31. * Hooks deleted_user.
  32. * @param $id
  33. */
  34. public static function action_deleted_user( $id ) {
  35. $es_user = new ES_User( $id );
  36. $subscribed_posts = $es_user->get_subscribed_posts();
  37. foreach ( $subscribed_posts as $post ) {
  38. $es_user->unsubscribe( 'post', $post );
  39. }
  40. $subscribed_author_ids = $es_user->get_subscribed_author_ids( $id );
  41. foreach ( $subscribed_author_ids as $author_id ) {
  42. $es_user->unsubscribe( 'user', $author_id );
  43. }
  44. }
  45. /**
  46. * Echo the term form for a user.
  47. * @param WP_User $user
  48. */
  49. public static function echo_term_form( $user ) {
  50. $taxonomies = EasySubscribe::$options->get( 'enable_taxonomies' );
  51. if ( empty( $taxonomies ) )
  52. return;
  53. $es_user = new ES_User( $user );
  54. $subscribed_term_signatures = $es_user->get_subscribed_term_signatures();
  55. $template = EasySubscribe::locate_template( 'profile-term-form.php' );
  56. $template_data = compact( 'taxonomies', 'user', 'subscribed_term_signatures' );
  57. EasySubscribe::render_template( $template, $template_data );
  58. }
  59. /**
  60. * When a profile is saved, also save changes to term subscriptions.
  61. *
  62. * Hooks edit_user_profile_updated and profile_options_updated.
  63. * @param $user_id
  64. */
  65. public static function process_term_form( $user_id ) {
  66. $taxonomies = EasySubscribe::$options->get( 'enable_taxonomies' );
  67. if ( empty( $taxonomies ) )
  68. return;
  69. $es_user = new ES_User( $user_id );
  70. $posted_sigs = isset( $_POST[self::SUBSCRIBED_TERM_META_KEY] ) ? $_POST[self::SUBSCRIBED_TERM_META_KEY] : array();
  71. $subscribed_sigs = $es_user->get_subscribed_term_signatures();
  72. $additions = array_diff( $posted_sigs, $subscribed_sigs );
  73. foreach( $additions as $added_sig ) {
  74. add_user_meta( $user_id, self::SUBSCRIBED_TERM_META_KEY, $added_sig );
  75. }
  76. $subtractions = array_diff( $subscribed_sigs, $posted_sigs );
  77. foreach( $subtractions as $removed_sig ) {
  78. delete_user_meta( $user_id, self::SUBSCRIBED_TERM_META_KEY, $removed_sig );
  79. }
  80. }
  81. /**
  82. * Get the underlying user.
  83. * @return null|WP_User
  84. */
  85. public function get_user() {
  86. if ( !isset( $this->_user ) )
  87. $this->_user = get_userdata( $this->_id );
  88. return $this->_user;
  89. }
  90. /**
  91. * Ensure that the user is subscribed to an object.
  92. *
  93. * Does nothing if the user is already subscribed.
  94. *
  95. * @param $type
  96. * @param $object
  97. * @return bool
  98. */
  99. public function ensure_subscribed( $type, $object ) {
  100. $result = false;
  101. $object_id = is_object( $object ) ? $object->ID : intval( $object );
  102. if ( !$object_id )
  103. return $result;
  104. $subscriber_ids = EasySubscribe::get_subscriber_ids( $type, $object_id );
  105. if ( !in_array( $this->_id, $subscriber_ids ) ) {
  106. array_push( $subscriber_ids, $this->_id );
  107. $result = update_metadata( $type, $object_id, EasySubscribe::SUBSCRIBED_META_KEY, $subscriber_ids );
  108. if ( $result )
  109. do_action( 'es_subscribed', $type, $this->_id, $object_id );
  110. }
  111. return $result;
  112. }
  113. /**
  114. * Unsubscribe a user from an object.
  115. *
  116. * @param string $type post or user
  117. * @param object|int $object object or ID to unsubscribe from.
  118. * @return bool True if the user has been unsubscribed.
  119. */
  120. public function unsubscribe( $type, $object ) {
  121. $object_id = is_object( $object ) ? $object->ID : intval( $object );
  122. $success = true;
  123. $subscriber_ids = EasySubscribe::get_subscriber_ids( $type, $object_id );
  124. if ( in_array( $this->_id, $subscriber_ids ) ) {
  125. $subscriber_ids = array_diff( $subscriber_ids, array( $this->_id ) );
  126. $success = update_metadata( $type, $object_id, EasySubscribe::SUBSCRIBED_META_KEY, $subscriber_ids );
  127. if ( $success )
  128. do_action( 'es_unsubscribed', $type, $this->_id, $object_id );
  129. }
  130. return $success;
  131. }
  132. /**
  133. * Determine whether a user is subscribed to an object.
  134. *
  135. * @param string $type object or user
  136. * @param int|object $object
  137. * @return bool True if the user is subscribed to the object.
  138. */
  139. public function is_subscribed( $type, $object ) {
  140. $object_id = is_object( $object ) ? $object->ID : intval( $object );
  141. if ( !$object_id )
  142. return false;
  143. $subscriber_ids = EasySubscribe::get_subscriber_ids( $type, $object_id );
  144. return in_array( $this->_id, $subscriber_ids );
  145. }
  146. /**
  147. * Get the posts the user is subscribed to.
  148. *
  149. * @return array posts
  150. */
  151. public function get_subscribed_posts() {
  152. // Here we decide how to deal with querying serialized data
  153. // Choosing to depend on PHP's serial format
  154. $posts = get_posts( array(
  155. 'post_type' => 'any',
  156. 'posts_per_page' => -1,
  157. 'meta_query' => array(
  158. array(
  159. 'key' => EasySubscribe::SUBSCRIBED_META_KEY,
  160. 'value' => 'i:' . $this->_id . ';',
  161. 'compare' => 'LIKE',
  162. )
  163. )
  164. ) );
  165. return $posts;
  166. }
  167. /**
  168. * Get the post IDs the user is subscribed to.
  169. *
  170. * @return array author IDs
  171. */
  172. public function get_subscribed_author_ids() {
  173. global $wpdb;
  174. // Here we decide how to deal with querying serialized data
  175. // Choosing to depend on PHP's serial format
  176. $sql = "SELECT user_id FROM {$wpdb->usermeta} WHERE meta_key='" . EasySubscribe::SUBSCRIBED_META_KEY .
  177. "' and meta_value LIKE '%i:" . $this->_id . ";%'";
  178. return $wpdb->get_col( $sql );
  179. }
  180. /**
  181. * @return array Signatures are taxonomy+term_id.
  182. */
  183. public function get_subscribed_term_signatures() {
  184. return get_user_meta( $this->_id, self::SUBSCRIBED_TERM_META_KEY );
  185. }
  186. }