Elgg  Version 3.0
comments.php
Go to the documentation of this file.
1 <?php
11 
18 function _elgg_comments_init() {
19  elgg_register_entity_type('object', 'comment');
20 
21  elgg_register_plugin_hook_handler('container_permissions_check', 'object', '_elgg_comments_container_permissions_override');
22  elgg_register_plugin_hook_handler('permissions_check', 'object', '_elgg_comments_permissions_override');
23  elgg_register_plugin_hook_handler('email', 'system', '_elgg_comments_notification_email_subject');
24 
25  elgg_register_plugin_hook_handler('register', 'menu:social', '_elgg_comments_social_menu_setup');
26 
27  elgg_register_event_handler('update:after', 'all', '_elgg_comments_access_sync', 600);
28 
29  elgg_register_ajax_view('core/ajax/edit_comment');
30  elgg_register_ajax_view('page/elements/comments');
31  elgg_register_ajax_view('river/elements/responses');
32 
33  elgg_register_plugin_hook_handler('likes:is_likable', 'object:comment', 'Elgg\Values::getTrue');
34 
35  elgg_register_notification_event('object', 'comment', ['create']);
36  elgg_register_plugin_hook_handler('get', 'subscriptions', '_elgg_comments_add_content_owner_to_subscriptions');
37  elgg_register_plugin_hook_handler('prepare', 'notification:create:object:comment', '_elgg_comments_prepare_content_owner_notification');
38  elgg_register_plugin_hook_handler('prepare', 'notification:create:object:comment', '_elgg_comments_prepare_notification');
39 }
40 
49  $params = [
50  'entity' => $container,
51  ];
52  return (bool) elgg_trigger_plugin_hook('config', 'comments_latest_first', $params, true);
53 }
54 
63  $params = [
64  'entity' => $container,
65  ];
66  return (int) elgg_trigger_plugin_hook('config', 'comments_per_page', $params, 25);
67 }
68 
79  $fail = function () {
80  register_error(elgg_echo('generic_comment:notfound'));
82  };
83 
85  if (!$comment) {
86  // try fallback if given
87  $fallback = get_entity($fallback_guid);
88  if (!$fallback) {
89  $fail();
90  }
91 
92  register_error(elgg_echo('generic_comment:notfound_fallback'));
93  forward($fallback->getURL());
94  }
95 
96  if (!$comment instanceof ElggComment) {
97  $fail();
98  }
99 
100  $container = $comment->getContainerEntity();
101  if (!$container) {
102  $fail();
103  }
104 
105  $operator = elgg_comments_are_latest_first($container) ? '>' : '<';
106 
107  // this won't work with threaded comments, but core doesn't support that yet
108  $condition = function(QueryBuilder $qb, $main_alias) use ($comment, $operator) {
109  return $qb->compare("{$main_alias}.guid", $operator, $comment->guid, ELGG_VALUE_GUID);
110  };
112  'type' => 'object',
113  'subtype' => 'comment',
114  'container_guid' => $container->guid,
115  'count' => true,
116  'wheres' => [$condition],
117  ]);
118  $limit = (int) get_input('limit');
119  if (!$limit) {
121  }
122  $offset = floor($count / $limit) * $limit;
123  if (!$offset) {
124  $offset = null;
125  }
126 
128  'offset' => $offset,
129  ]);
130 
131  // make sure there's only one fragment (#)
132  $parts = parse_url($url);
133  $parts['fragment'] = "elgg-object-{$comment->guid}";
134  $url = elgg_http_build_url($parts, false);
135 
136  forward($url);
137 }
138 
155  // is someone trying to comment, if so override permissions check
156  if (elgg_extract('subtype', $params) === 'comment') {
157  return true;
158  }
159 }
160 
173  $entity = elgg_extract('entity', $params);
174  $user = elgg_extract('user', $params);
175 
176  if ($entity instanceof ElggComment && $user instanceof ElggUser) {
177  return $entity->getOwnerGUID() === $user->getGUID();
178  }
179 }
180 
197 function _elgg_comments_notification_email_subject($hook, $type, $returnvalue, $params) {
198  if (!is_array($returnvalue) || !is_array($returnvalue['params'])) {
199  // another hook handler returned a non-array, let's not override it
200  return;
201  }
202 
203  if (empty($returnvalue['params']['notification'])) {
204  return;
205  }
206 
208  $notification = $returnvalue['params']['notification'];
209 
210  if ($notification instanceof Elgg\Notifications\Notification) {
211  $object = elgg_extract('object', $notification->params);
212 
213  if ($object instanceof ElggComment) {
214  $container = $object->getContainerEntity();
215 
216  $returnvalue['subject'] = 'Re: ' . $container->getDisplayName();
217  }
218  }
219 
220  return $returnvalue;
221 }
222 
234  if (!($entity instanceof \ElggEntity)) {
235  return true;
236  }
237 
238  // need to override access in case comments ended up with ACCESS_PRIVATE
239  // and to ensure write permissions
240  $ia = elgg_set_ignore_access(true);
241  $options = [
242  'type' => 'object',
243  'subtype' => 'comment',
244  'container_guid' => $entity->getGUID(),
245  'wheres' => [function(\Elgg\Database\QueryBuilder $qb, $main_alias) use ($entity) {
246  return $qb->compare("{$main_alias}.access_id", '!=', $entity->access_id, ELGG_VALUE_INTEGER);
247  }],
248  'limit' => 0,
249  ];
250 
251  $batch = new \ElggBatch('elgg_get_entities', $options, null, 25, false);
252  foreach ($batch as $comment) {
253  // Update comment access_id
254  $comment->access_id = $entity->access_id;
255  $comment->save();
256  }
257 
259 
260  return true;
261 }
262 
276 
277  $event = elgg_extract('event', $params);
278  if (!$event instanceof \Elgg\Notifications\SubscriptionNotificationEvent) {
279  return;
280  }
281 
282  if ($event->getAction() !== 'create') {
283  return;
284  }
285 
286  $object = $event->getObject();
287  if (!$object instanceof ElggComment) {
288  return;
289  }
290 
291  $content_owner = $object->getContainerEntity()->getOwnerEntity();
292  if (!$content_owner instanceof ElggUser) {
293  return;
294  }
295 
296  $notification_settings = $content_owner->getNotificationSettings();
297  if (empty($notification_settings)) {
298  return;
299  }
300 
301  $returnvalue[$content_owner->guid] = [];
302  foreach ($notification_settings as $method => $enabled) {
303  if (empty($enabled)) {
304  continue;
305  }
306 
307  $returnvalue[$content_owner->guid][] = $method;
308  }
309 
310  return $returnvalue;
311 }
312 
326 
327  $comment = elgg_extract('object', $params);
328  if (!$comment instanceof ElggComment) {
329  return;
330  }
331 
332  /* @var $content \ElggEntity */
333  $content = $comment->getContainerEntity();
334  $recipient = elgg_extract('recipient', $params);
335  if ($content->owner_guid !== $recipient->guid) {
336  // not the content owner
337  return;
338  }
339 
340  $language = elgg_extract('language', $params);
341  /* @var $commenter \ElggUser */
342  $commenter = $comment->getOwnerEntity();
343 
344  $returnvalue->subject = elgg_echo('generic_comment:notification:owner:subject', [], $language);
345  $returnvalue->summary = elgg_echo('generic_comment:notification:owner:summary', [], $language);
346  $returnvalue->body = elgg_echo('generic_comment:notification:owner:body', [
347  $content->getDisplayName(),
348  $commenter->getDisplayName(),
349  $comment->description,
350  $comment->getURL(),
351  $commenter->getDisplayName(),
352  $commenter->getURL(),
353  ], $language);
354 
355  return $returnvalue;
356 }
357 
370 function _elgg_comments_prepare_notification($hook, $type, $returnvalue, $params) {
371 
372  $comment = elgg_extract('object', $params);
373  if (!$comment instanceof ElggComment) {
374  return;
375  }
376 
377  /* @var $content \ElggEntity */
378  $content = $comment->getContainerEntity();
379  $recipient = elgg_extract('recipient', $params);
380  if ($content->getOwnerGUID() === $recipient->getGUID()) {
381  // the content owner, this is handled in other hook
382  return;
383  }
384 
385  $language = elgg_extract('language', $params);
386  /* @var $commenter \ElggUser */
387  $commenter = $comment->getOwnerEntity();
388 
389  $returnvalue->subject = elgg_echo('generic_comment:notification:user:subject', [$content->getDisplayName()], $language);
390  $returnvalue->summary = elgg_echo('generic_comment:notification:user:summary', [$content->getDisplayName()], $language);
391  $returnvalue->body = elgg_echo('generic_comment:notification:user:body', [
392  $content->getDisplayName(),
393  $commenter->getDisplayName(),
394  $comment->description,
395  $comment->getURL(),
396  $commenter->getDisplayName(),
397  $commenter->getURL(),
398  ], $language);
399 
400  $returnvalue->url = $comment->getURL();
401 
402  return $returnvalue;
403 }
404 
415 function _elgg_comments_social_menu_setup(\Elgg\Hook $hook) {
416  $entity = $hook->getEntityParam();
417  if (!$entity) {
418  return;
419  }
420 
421  $return = $hook->getValue();
422 
423  $comment_count = $entity->countComments();
424  $can_comment = $entity->canComment();
425  if ($can_comment || $comment_count) {
426  $text = $can_comment ? elgg_echo('comment:this') : elgg_echo('comments');
427 
428  $options = [
429  'name' => 'comment',
430  'icon' => 'speech-bubble',
431  'badge' => $comment_count ?: null,
432  'text' => $text,
433  'title' => $text,
434  'href' => $entity->getURL() . '#comments',
435  ];
436 
437  $item = $hook->getParam('item');
438  if ($item && $can_comment) {
439  $options['href'] = "#comments-add-{$entity->guid}-{$item->id}";
440  $options['rel'] = 'toggle';
441  }
442 
443  $return[] = \ElggMenuItem::factory($options);
444  }
445 
446  return $return;
447 }
448 
463  return $value;
464 }
465 
469 return function(\Elgg\EventsService $events, \Elgg\HooksRegistrationService $hooks) {
470  $events->registerHandler('init', 'system', '_elgg_comments_init');
471  $hooks->registerHandler('unit_test', 'system', '_elgg_comments_test');
472 };
elgg_http_add_url_query_elements($url, array $elements)
Sets elements in a URL&#39;s query string.
Definition: elgglib.php:942
if(!$item instanceof ElggRiverItem) $object
Definition: responses.php:23
elgg_comments_per_page(ElggEntity $container=null)
How many comments appear per page.
Definition: comments.php:62
_elgg_comments_container_permissions_override($hook, $type, $return, $params)
Allow users to comment on entities not owned by them.
Definition: comments.php:154
$params
Saves global plugin settings.
Definition: save.php:13
if(!$items) $item
Definition: delete.php:13
_elgg_comments_social_menu_setup(\Elgg\Hook $hook)
Adds comment menu items to entity menu.
Definition: comments.php:415
const ELGG_VALUE_INTEGER
Value types.
Definition: constants.php:138
elgg_comments_are_latest_first(ElggEntity $container=null)
Are comments displayed with latest first?
Definition: comments.php:48
const ELGG_VALUE_GUID
Definition: constants.php:140
Events service.
$commenter
Definition: comment.php:18
if(!$count) $offset
Definition: pagination.php:26
Database abstraction query builder.
elgg parse_url
Parse a URL into its parts.
Definition: elgglib.js:442
_elgg_comments_permissions_override($hook, $type, $return, $params)
By default, only authors can edit their comments.
Definition: comments.php:172
elgg forward
Meant to mimic the php forward() function by simply redirecting the user to another page...
Definition: elgglib.js:417
$type
Definition: delete.php:21
elgg_register_plugin_hook_handler($hook, $type, $callback, $priority=500)
Definition: elgglib.php:634
$comment
Definition: save.php:43
$comment_guid
Definition: save.php:10
elgg_echo($message_key, array $args=[], $language="")
Given a message key, returns an appropriately translated full-text string.
Definition: languages.php:21
$fail
Definition: register.php:43
_elgg_comments_test($hook, $type, $value, $params)
Runs unit tests for .
Definition: comments.php:461
$text
Definition: default.php:28
static factory($options)
Create an ElggMenuItem from an associative array.
$limit
Definition: comments.php:24
_elgg_comments_access_sync($event, $type, $entity)
Update comment access to match that of the container.
Definition: comments.php:233
Configuration exception.
Base class for events and hooks.
$fallback_guid
Definition: view.php:9
$language
Definition: useradd.php:17
$container
Definition: delete.php:23
elgg_get_entities(array $options=[])
Fetches/counts entities or performs a calculation on their properties.
Definition: entities.php:545
elgg_set_ignore_access($ignore=true)
Set if Elgg&#39;s access system should be ignored.
Definition: access.php:52
get_input($variable, $default=null, $filter_result=true)
Get some input from variables passed submitted through GET or POST.
Definition: input.php:27
_elgg_comments_add_content_owner_to_subscriptions($hook, $type, $returnvalue, $params)
Add the owner of the content being commented on to the subscribers.
Definition: comments.php:275
$enabled
Sample cli installer script.
$user
Definition: ban.php:7
_elgg_comments_init()
Comments initialization function.
Definition: comments.php:18
compare($x, $comparison, $y=null, $type=null, $case_sensitive=null)
Build value comparison clause.
$url
Definition: default.php:33
elgg_trigger_plugin_hook($hook, $type, $params=null, $returnvalue=null)
Definition: elgglib.php:720
if(!$limit) $options
Definition: comments.php:34
elgg_register_ajax_view($view)
Register a view to be available for ajax calls.
Definition: views.php:133
_elgg_comment_redirect($comment_guid, $fallback_guid)
Redirect to the comment in context of the containing page.
Definition: comments.php:78
elgg_extract($key, $array, $default=null, $strict=true)
Checks for $array[$key] and returns its value if it exists, else returns $default.
Definition: elgglib.php:1131
elgg_register_event_handler($event, $object_type, $callback, $priority=500)
Definition: elgglib.php:449
_elgg_comments_prepare_notification($hook, $type, $returnvalue, $params)
Set the notification message for interested users.
Definition: comments.php:370
$value
Definition: debugging.php:7
const REFERER
Definition: constants.php:52
elgg_register_notification_event($object_type, $object_subtype, array $actions=[])
Register a notification event.
elgg register_error
Wrapper function for system_messages.
Definition: elgglib.js:391
elgg_http_build_url(array $parts, $html_encode=true)
Builds a URL from the a parts array like one returned by parse_url().
Definition: elgglib.php:859
if(elgg_in_context('widget')) $count
Definition: pagination.php:21
class
Definition: placeholder.php:21
$entity
Definition: comments.php:15
if(!empty($item->annotation_id)||!$object instanceof ElggEntity||$object instanceof ElggComment) $comment_count
Definition: responses.php:30
_elgg_comments_prepare_content_owner_notification($hook, $type, $returnvalue, $params)
Set the notification message for the owner of the content being commented on.
Definition: comments.php:325
$notification_settings
elgg_register_entity_type($type, $subtype=null)
Registers an entity type and subtype as a public-facing entity that should be shown in search and by ...
Definition: entities.php:657
get_entity($guid)
Loads and returns an entity object from a guid.
Definition: entities.php:87
$content
Definition: comments.php:66