functions.php 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  1. <?php
  2. /**
  3. * All helper functions form this plugin are bundled here
  4. */
  5. /**
  6. * Check if the user has a subscription with the content
  7. *
  8. * @param int $entity_guid the content entity to check
  9. * @param int $user_guid the user to check (defaults to current user)
  10. * @param bool $return_subscription return the subscription settings
  11. *
  12. * @return bool|array
  13. */
  14. function content_subscriptions_check_subscription($entity_guid, $user_guid = 0, $return_subscription = false) {
  15. $entity_guid = sanitise_int($entity_guid, false);
  16. $user_guid = sanitise_int($user_guid, false);
  17. if (empty($user_guid)) {
  18. $user_guid = elgg_get_logged_in_user_guid();
  19. }
  20. if (empty($entity_guid) || empty($user_guid)) {
  21. return false;
  22. }
  23. // check if we didn't block subscription
  24. if (content_subscriptions_check_block_subscription($entity_guid, $user_guid)) {
  25. return false;
  26. }
  27. // special case for discussions
  28. $ia = elgg_set_ignore_access(true);
  29. $entity = get_entity($entity_guid);
  30. if (elgg_instanceof($entity, 'object', 'groupforumtopic')) {
  31. $group_sub = content_subscriptions_check_notification_settings($entity->getContainerEntity(), $user_guid, $return_subscription);
  32. if ($group_sub) {
  33. elgg_set_ignore_access($ia);
  34. return $group_sub;
  35. }
  36. }
  37. elgg_set_ignore_access($ia);
  38. // check entity subscription
  39. $subs = elgg_get_subscriptions_for_container($entity_guid);
  40. if (empty($subs)) {
  41. return false;
  42. }
  43. if (!isset($subs[$user_guid])) {
  44. return false;
  45. }
  46. if ($return_subscription) {
  47. return $subs[$user_guid];
  48. }
  49. return true;
  50. }
  51. /**
  52. * Subscribe a user to the updates of an entity
  53. *
  54. * @param int $entity_guid the content entity to subscribe to
  55. * @param int $user_guid the user to subscribe (defaults to current user)
  56. *
  57. * @return bool
  58. */
  59. function content_subscriptions_subscribe($entity_guid, $user_guid = 0) {
  60. $entity_guid = sanitise_int($entity_guid, false);
  61. $user_guid = sanitise_int($user_guid, false);
  62. if (empty($user_guid)) {
  63. $user_guid = elgg_get_logged_in_user_guid();
  64. }
  65. // remove autosubscription block
  66. remove_entity_relationship($user_guid, CONTENT_SUBSCRIPTIONS_BLOCK, $entity_guid);
  67. $notification_services = _elgg_services()->notifications->getMethods();
  68. if (empty($notification_services)) {
  69. return false;
  70. }
  71. foreach ($notification_services as $service) {
  72. elgg_add_subscription($user_guid, $service, $entity_guid);
  73. }
  74. return true;
  75. }
  76. /**
  77. * Automaticly subscribe to the updates of an entity if the user didn't block this
  78. *
  79. * @param int $entity_guid the content entity to subscribe to
  80. * @param int $user_guid the user to subscribe (defaults to current user)
  81. *
  82. * @return bool
  83. */
  84. function content_subscriptions_autosubscribe($entity_guid, $user_guid = 0) {
  85. $result = false;
  86. $entity_guid = sanitise_int($entity_guid, false);
  87. $user_guid = sanitise_int($user_guid, false);
  88. if (empty($user_guid)) {
  89. $user_guid = elgg_get_logged_in_user_guid();
  90. }
  91. // check if the user blocked the subscription
  92. if (!content_subscriptions_check_block_subscription($entity_guid, $user_guid)) {
  93. $entity = get_entity($entity_guid);
  94. // check if this is not the content owner
  95. if ($entity->getOwnerGUID() != $user_guid) {
  96. // no, so subscribe
  97. $result = content_subscriptions_subscribe($entity_guid, $user_guid);
  98. }
  99. }
  100. return $result;
  101. }
  102. /**
  103. * Unsubscribe a user from updates and set a flag so auto updates don't recreate the updates
  104. *
  105. * @param int $entity_guid the content entity to unsubscribe from
  106. * @param int $user_guid the user to unsubscribe (defaults to current user)
  107. *
  108. * @return bool
  109. */
  110. function content_subscriptions_unsubscribe($entity_guid, $user_guid = 0) {
  111. $entity_guid = sanitise_int($entity_guid, false);
  112. $user_guid = sanitise_int($user_guid, false);
  113. if (empty($user_guid)) {
  114. $user_guid = elgg_get_logged_in_user_guid();
  115. }
  116. if (empty($entity_guid) || empty($user_guid)) {
  117. return false;
  118. }
  119. // check if we have a subscription
  120. $sub = content_subscriptions_check_subscription($entity_guid, $user_guid, true);
  121. // make sure we can't autosubscribe
  122. add_entity_relationship($user_guid, CONTENT_SUBSCRIPTIONS_BLOCK, $entity_guid);
  123. // quick return if no subscriptions
  124. if (empty($sub)) {
  125. return true;
  126. }
  127. // remove subscriptions
  128. foreach ($sub as $service) {
  129. elgg_remove_subscription($user_guid, $service, $entity_guid);
  130. }
  131. return true;
  132. }
  133. /**
  134. * Check if the user gets notifications from the group, based on notification settings
  135. *
  136. * @param ElggEntity $container the container to check (only act on ElggGroups)
  137. * @param int $user_guid the user to check (defaults to current user)
  138. * @param bool $return_subscription return the subscription settings
  139. *
  140. * @return bool
  141. */
  142. function content_subscriptions_check_notification_settings(ElggEntity $container, $user_guid = 0, $return_subscription = false) {
  143. static $user_cache;
  144. $user_guid = sanitise_int($user_guid, false);
  145. if (empty($user_guid)) {
  146. $user_guid = elgg_get_logged_in_user_guid();
  147. }
  148. // only check groups
  149. if (($container instanceof ElggGroup) && !empty($user_guid)) {
  150. if (!isset($user_cache[$container->getGUID()])) {
  151. $user_cache[$container->getGUID()] = elgg_get_subscriptions_for_container($container->getGUID());
  152. }
  153. if ($return_subscription) {
  154. return $user_cache[$container->getGUID()][$user_guid];
  155. } else {
  156. return isset($user_cache[$container->getGUID()][$user_guid]);
  157. }
  158. }
  159. return false;
  160. }
  161. /**
  162. * Checks if a user can subscribe to a content item
  163. *
  164. * @param ElggEntity $entity the entity to check
  165. * @param int $user_guid the user to check (default: current user)
  166. *
  167. * @return bool
  168. */
  169. function content_subscriptions_can_subscribe(ElggEntity $entity, $user_guid = 0) {
  170. $user_guid = sanitise_int($user_guid, false);
  171. if (empty($user_guid)) {
  172. $user_guid = elgg_get_logged_in_user_guid();
  173. }
  174. if (empty($user_guid) || !($entity instanceof ElggEntity)) {
  175. return false;
  176. }
  177. if ($entity->getOwnerGUID() === $user_guid) {
  178. // owner cant subscribe to own content
  179. return false;
  180. }
  181. $supported_entity_types = content_subscriptions_get_supported_entity_types();
  182. if (empty($supported_entity_types)) {
  183. return false;
  184. }
  185. $type = $entity->getType();
  186. if (!isset($supported_entity_types[$type])) {
  187. return false;
  188. }
  189. $subtype = $entity->getSubtype();
  190. if (!empty($subtype)) {
  191. return in_array($subtype, $supported_entity_types[$type]);
  192. }
  193. return true;
  194. }
  195. /**
  196. * Get an array of the supported entity types/subtypes for subscriptions
  197. *
  198. * @return array
  199. */
  200. function content_subscriptions_get_supported_entity_types() {
  201. $result = [
  202. 'object' => [
  203. 'groupforumtopic',
  204. 'blog',
  205. 'file',
  206. 'page_top',
  207. 'page',
  208. 'bookmark'
  209. ],
  210. ];
  211. $params = [
  212. 'defaults' => $result,
  213. ];
  214. return elgg_trigger_plugin_hook('entity_types', 'content_subscriptions', $params, $result);
  215. }
  216. /**
  217. * Get the subscription methods of the user
  218. *
  219. * @param int $user_guid the user_guid to check (default: current user)
  220. *
  221. * @return array
  222. */
  223. function content_subscriptions_get_notification_settings($user_guid = 0) {
  224. static $user_cache;
  225. $user_guid = sanitise_int($user_guid, false);
  226. if (empty($user_guid)) {
  227. $user_guid = elgg_get_logged_in_user_guid();
  228. }
  229. if (empty($user_guid)) {
  230. return [];
  231. }
  232. if (!isset($user_cache)) {
  233. $user_cache = [];
  234. }
  235. if (!isset($user_cache[$user_guid])) {
  236. $user_cache[$user_guid] = [];
  237. $checked = false;
  238. if (elgg_is_active_plugin('notifications')) {
  239. $saved = elgg_get_plugin_user_setting('notification_settings_saved', $user_guid, 'content_subscriptions');
  240. if (!empty($saved)) {
  241. $checked = true;
  242. $settings = elgg_get_plugin_user_setting('notification_settings', $user_guid, 'content_subscriptions');
  243. if (!empty($settings)) {
  244. $user_cache[$user_guid] = string_to_tag_array($settings);
  245. }
  246. }
  247. }
  248. if (!$checked) {
  249. // default elgg settings
  250. $settings = get_user_notification_settings($user_guid);
  251. if (!empty($settings)) {
  252. $settings = (array) $settings;
  253. foreach ($settings as $method => $value) {
  254. if (!empty($value)) {
  255. $user_cache[$user_guid][] = $method;
  256. }
  257. }
  258. }
  259. }
  260. }
  261. return $user_cache[$user_guid];
  262. }
  263. /**
  264. * Check if a user has a block relationship with an entity
  265. *
  266. * @param int $entity_guid the entity to check
  267. * @param int $user_guid the user to check for (default: current user)
  268. *
  269. * @return bool
  270. */
  271. function content_subscriptions_check_block_subscription($entity_guid, $user_guid = 0) {
  272. static $user_cache;
  273. $entity_guid = sanitise_int($entity_guid, false);
  274. $user_guid = sanitise_int($user_guid, false);
  275. if (empty($user_guid)) {
  276. $user_guid = elgg_get_logged_in_user_guid();
  277. }
  278. if (empty($entity_guid) || empty($user_guid)) {
  279. return false;
  280. }
  281. if (!isset($user_cache)) {
  282. $user_cache = [];
  283. }
  284. if (!isset($user_cache[$user_guid])) {
  285. $user_cache[$user_guid] = [];
  286. $relationships = get_entity_relationships($user_guid);
  287. if (!empty($relationships)) {
  288. foreach ($relationships as $relationship) {
  289. if ($relationship->relationship === CONTENT_SUBSCRIPTIONS_BLOCK) {
  290. $user_cache[$user_guid][] = (int) $relationship->guid_two;
  291. }
  292. }
  293. }
  294. }
  295. return in_array($entity_guid, $user_cache[$user_guid]);
  296. }