Likes.php 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. <?php
  2. namespace ColdTrick\ContentSubscriptions;
  3. class Likes {
  4. protected static $autosubscribe;
  5. /**
  6. * Listen to the creation of an annotation, if Like check auto subscribe
  7. *
  8. * @param string $event the name of the event
  9. * @param string $type the type of the event
  10. * @param \ElggAnnotation $annotation the created annotation
  11. *
  12. * @return void
  13. */
  14. public static function create($event, $type, $annotation) {
  15. if (!self::autoSubscribe()) {
  16. // auto subscribe isn't enabled
  17. return;
  18. }
  19. if (empty($annotation) || !($annotation instanceof \ElggAnnotation)) {
  20. // not an annotation
  21. return;
  22. }
  23. if ($annotation->name !== 'likes') {
  24. // not likes
  25. return;
  26. }
  27. $entity = $annotation->getEntity();
  28. if (empty($entity) || !($entity instanceof \ElggEntity)) {
  29. return;
  30. }
  31. $user = $annotation->getOwnerEntity();
  32. if (empty($user) || !($user instanceof \ElggUser)) {
  33. return;
  34. }
  35. if (!content_subscriptions_can_subscribe($entity, $user->getGUID())) {
  36. // subscribing isn't allowed for this entity type/subtype
  37. return;
  38. }
  39. // auto subscribe to this entity
  40. content_subscriptions_autosubscribe($entity->getGUID(), $user->getGUID());
  41. }
  42. /**
  43. * Is auto subscribe enabled for Likes
  44. *
  45. * @return bool
  46. */
  47. protected static function autoSubscribe() {
  48. if (isset(self::$autosubscribe)) {
  49. return self::$autosubscribe;
  50. }
  51. self::$autosubscribe = false;
  52. $setting = elgg_get_plugin_setting('likes_autosubscribe', 'content_subscriptions');
  53. if ($setting === 'yes') {
  54. self::$autosubscribe = true;
  55. }
  56. return self::$autosubscribe;
  57. }
  58. }