start.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. <?php
  2. /**
  3. * Likes plugin
  4. *
  5. */
  6. elgg_register_event_handler('init', 'system', 'likes_init');
  7. function likes_init() {
  8. elgg_extend_view('css/elgg', 'likes/css');
  9. elgg_extend_view('js/elgg', 'likes/js');
  10. // used to preload likes data before rendering river
  11. elgg_extend_view('page/components/list', 'likes/before_lists', 1);
  12. // registered with priority < 500 so other plugins can remove likes
  13. elgg_register_plugin_hook_handler('register', 'menu:river', 'likes_river_menu_setup', 400);
  14. elgg_register_plugin_hook_handler('register', 'menu:entity', 'likes_entity_menu_setup', 400);
  15. elgg_register_plugin_hook_handler('permissions_check', 'annotation', 'likes_permissions_check');
  16. $actions_base = elgg_get_plugins_path() . 'likes/actions/likes';
  17. elgg_register_action('likes/add', "$actions_base/add.php");
  18. elgg_register_action('likes/delete', "$actions_base/delete.php");
  19. elgg_register_ajax_view('likes/popup');
  20. }
  21. /**
  22. * Only allow annotation owner (or someone who can edit the owner, like an admin) to delete like
  23. *
  24. * @param string $hook "permissions_check"
  25. * @param string $type "annotation"
  26. * @param array $return Current value
  27. * @param array $params Hook parameters
  28. *
  29. * @return bool
  30. */
  31. function likes_permissions_check($hook, $type, $return, $params) {
  32. $annotation = elgg_extract('annotation', $params);
  33. if (!$annotation || $annotation->name !== 'likes') {
  34. return $return;
  35. }
  36. $owner = $annotation->getOwnerEntity();
  37. if (!$owner) {
  38. return $return;
  39. }
  40. return $owner->canEdit();
  41. }
  42. /**
  43. * Add likes to entity menu at end of the menu
  44. */
  45. function likes_entity_menu_setup($hook, $type, $return, $params) {
  46. if (elgg_in_context('widgets')) {
  47. return $return;
  48. }
  49. $entity = $params['entity'];
  50. /* @var ElggEntity $entity */
  51. if ($entity->canAnnotate(0, 'likes')) {
  52. $hasLiked = \Elgg\Likes\DataService::instance()->currentUserLikesEntity($entity->guid);
  53. // Always register both. That makes it super easy to toggle with javascript
  54. $return[] = ElggMenuItem::factory(array(
  55. 'name' => 'likes',
  56. 'href' => elgg_add_action_tokens_to_url("/action/likes/add?guid={$entity->guid}"),
  57. 'text' => elgg_view_icon('thumbs-up'),
  58. 'title' => elgg_echo('likes:likethis'),
  59. 'item_class' => $hasLiked ? 'hidden' : '',
  60. 'priority' => 1000,
  61. ));
  62. $return[] = ElggMenuItem::factory(array(
  63. 'name' => 'unlike',
  64. 'href' => elgg_add_action_tokens_to_url("/action/likes/delete?guid={$entity->guid}"),
  65. 'text' => elgg_view_icon('thumbs-up-alt'),
  66. 'title' => elgg_echo('likes:remove'),
  67. 'item_class' => $hasLiked ? '' : 'hidden',
  68. 'priority' => 1000,
  69. ));
  70. }
  71. // likes count
  72. $count = elgg_view('likes/count', array('entity' => $entity));
  73. if ($count) {
  74. $options = array(
  75. 'name' => 'likes_count',
  76. 'text' => $count,
  77. 'href' => false,
  78. 'priority' => 1001,
  79. );
  80. $return[] = ElggMenuItem::factory($options);
  81. }
  82. return $return;
  83. }
  84. /**
  85. * Add a like button to river actions
  86. */
  87. function likes_river_menu_setup($hook, $type, $return, $params) {
  88. if (!elgg_is_logged_in() || elgg_in_context('widgets')) {
  89. return;
  90. }
  91. $item = $params['item'];
  92. /* @var ElggRiverItem $item */
  93. // only like group creation #3958
  94. if ($item->type == "group" && $item->view != "river/group/create") {
  95. return;
  96. }
  97. // don't like users #4116
  98. if ($item->type == "user") {
  99. return;
  100. }
  101. if ($item->annotation_id != 0) {
  102. return;
  103. }
  104. $object = $item->getObjectEntity();
  105. if (!$object || !$object->canAnnotate(0, 'likes')) {
  106. return;
  107. }
  108. $hasLiked = \Elgg\Likes\DataService::instance()->currentUserLikesEntity($object->guid);
  109. // Always register both. That makes it super easy to toggle with javascript
  110. $return[] = ElggMenuItem::factory(array(
  111. 'name' => 'likes',
  112. 'href' => elgg_add_action_tokens_to_url("/action/likes/add?guid={$object->guid}"),
  113. 'text' => elgg_view_icon('thumbs-up'),
  114. 'title' => elgg_echo('likes:likethis'),
  115. 'item_class' => $hasLiked ? 'hidden' : '',
  116. 'priority' => 100,
  117. ));
  118. $return[] = ElggMenuItem::factory(array(
  119. 'name' => 'unlike',
  120. 'href' => elgg_add_action_tokens_to_url("/action/likes/delete?guid={$object->guid}"),
  121. 'text' => elgg_view_icon('thumbs-up-alt'),
  122. 'title' => elgg_echo('likes:remove'),
  123. 'item_class' => $hasLiked ? '' : 'hidden',
  124. 'priority' => 100,
  125. ));
  126. // likes count
  127. $count = elgg_view('likes/count', array('entity' => $object));
  128. if ($count) {
  129. $return[] = ElggMenuItem::factory(array(
  130. 'name' => 'likes_count',
  131. 'text' => $count,
  132. 'href' => false,
  133. 'priority' => 101,
  134. ));
  135. }
  136. return $return;
  137. }
  138. /**
  139. * Count how many people have liked an entity.
  140. *
  141. * @param ElggEntity $entity
  142. *
  143. * @return int Number of likes
  144. */
  145. function likes_count(ElggEntity $entity) {
  146. $type = $entity->getType();
  147. $params = array('entity' => $entity);
  148. $number = elgg_trigger_plugin_hook('likes:count', $type, $params, false);
  149. if ($number) {
  150. return $number;
  151. } else {
  152. return $entity->countAnnotations('likes');
  153. }
  154. }