Elgg  Version 2.3
comments.php
Go to the documentation of this file.
1 <?php
16 function _elgg_comments_init() {
17  elgg_register_entity_type('object', 'comment');
18  elgg_register_plugin_hook_handler('register', 'menu:entity', '_elgg_comment_setup_entity_menu', 900);
19  elgg_register_plugin_hook_handler('entity:url', 'object', '_elgg_comment_url_handler');
20  elgg_register_plugin_hook_handler('container_permissions_check', 'object', '_elgg_comments_container_permissions_override');
21  elgg_register_plugin_hook_handler('permissions_check', 'object', '_elgg_comments_permissions_override');
22  elgg_register_plugin_hook_handler('email', 'system', '_elgg_comments_notification_email_subject');
23 
24  elgg_register_event_handler('update:after', 'all', '_elgg_comments_access_sync', 600);
25 
26  elgg_register_page_handler('comment', '_elgg_comments_page_handler');
27 
28  elgg_register_ajax_view('core/ajax/edit_comment');
29 
30  elgg_register_plugin_hook_handler('likes:is_likable', 'object:comment', 'Elgg\Values::getTrue');
31 }
32 
40 function _elgg_comments_page_handler($segments) {
41 
42  $page = elgg_extract(0, $segments);
43  switch ($page) {
44 
45  case 'edit':
46  echo elgg_view_resource('comments/edit', [
47  'guid' => elgg_extract(1, $segments),
48  ]);
49  return true;
50  break;
51 
52  case 'view':
53  _elgg_comment_redirect(elgg_extract(1, $segments), elgg_extract(2, $segments));
54  break;
55 
56  default:
57  return false;
58  break;
59  }
60 }
61 
71 function _elgg_comment_redirect($comment_guid, $fallback_guid) {
72  $fail = function () {
73  register_error(elgg_echo('generic_comment:notfound'));
75  };
76 
78  if (!$comment) {
79  // try fallback if given
80  $fallback = get_entity($fallback_guid);
81  if (!$fallback) {
82  $fail();
83  }
84 
85  register_error(elgg_echo('generic_comment:notfound_fallback'));
86  forward($fallback->getURL());
87  }
88 
89  if (!elgg_instanceof($comment, 'object', 'comment')) {
90  $fail();
91  }
92 
93  $container = $comment->getContainerEntity();
94  if (!$container) {
95  $fail();
96  }
97 
98  // this won't work with threaded comments, but core doesn't support that yet
100  'type' => 'object',
101  'subtype' => 'comment',
102  'container_guid' => $container->guid,
103  'count' => true,
104  'wheres' => ["e.guid < " . (int)$comment->guid],
105  ]);
106  $limit = (int)get_input('limit');
107  if (!$limit) {
108  $limit = elgg_trigger_plugin_hook('config', 'comments_per_page', [], 25);
109  }
110  $offset = floor($count / $limit) * $limit;
111  if (!$offset) {
112  $offset = null;
113  }
114 
116  'offset' => $offset,
117  ]);
118 
119  // make sure there's only one fragment (#)
120  $parts = parse_url($url);
121  $parts['fragment'] = "elgg-object-{$comment->guid}";
122  $url = elgg_http_build_url($parts, false);
123 
124  forward($url);
125 }
126 
139  if (elgg_in_context('widgets')) {
140  return $return;
141  }
142 
143  $entity = $params['entity'];
144  if (!elgg_instanceof($entity, 'object', 'comment')) {
145  return $return;
146  }
147 
148  // Remove edit link and access level from the menu
149  foreach ($return as $key => $item) {
150  if ($item->getName() === 'access') {
151  unset($return[$key]);
152  }
153  }
154 
155  return $return;
156 }
157 
172  $entity = $params['entity'];
173  /* @var \ElggObject $entity */
174 
175  if (!elgg_instanceof($entity, 'object', 'comment') || !$entity->getOwnerEntity()) {
176  // not a comment or has no owner
177 
178  // @todo handle anonymous comments
179  return $return;
180  }
181 
182  $container = $entity->getContainerEntity();
183  if (!$container) {
184  return $return;
185  }
186 
187  return "comment/view/{$entity->guid}/{$container->guid}";
188 }
189 
206 
207  // is someone trying to comment, if so override permissions check
208  if ($params['subtype'] === 'comment') {
209  return true;
210  }
211 
212  return $return;
213 }
214 
227  $entity = $params['entity'];
228  $user = $params['user'];
229 
230  if (elgg_instanceof($entity, 'object', 'comment') && $user) {
231  return $entity->getOwnerGUID() == $user->getGUID();
232  }
233 
234  return $return;
235 }
236 
253 function _elgg_comments_notification_email_subject($hook, $type, $returnvalue, $params) {
254  if (!is_array($returnvalue) || !is_array($returnvalue['params'])) {
255  // another hook handler returned a non-array, let's not override it
256  return;
257  }
258 
259  if (empty($returnvalue['params']['notification'])) {
260  return;
261  }
262 
264  $notification = $returnvalue['params']['notification'];
265 
266  if ($notification instanceof Elgg\Notifications\Notification) {
267  $object = elgg_extract('object', $notification->params);
268 
269  if ($object instanceof ElggComment) {
270  $container = $object->getContainerEntity();
271 
272  $returnvalue['subject'] = 'Re: ' . $container->getDisplayName();
273  }
274  }
275 
276  return $returnvalue;
277 }
278 
290  if (!($entity instanceof \ElggEntity)) {
291  return true;
292  }
293 
294  // need to override access in case comments ended up with ACCESS_PRIVATE
295  // and to ensure write permissions
296  $ia = elgg_set_ignore_access(true);
297  $options = array(
298  'type' => 'object',
299  'subtype' => 'comment',
300  'container_guid' => $entity->getGUID(),
301  'wheres' => array(
302  "e.access_id != {$entity->access_id}"
303  ),
304  'limit' => 0,
305  );
306 
307  $batch = new \ElggBatch('elgg_get_entities', $options, null, 25, false);
308  foreach ($batch as $comment) {
309  // Update comment access_id
310  $comment->access_id = $entity->access_id;
311  $comment->save();
312  }
313 
315 
316  return true;
317 }
318 
331  global $CONFIG;
332  $value[] = "{$CONFIG->path}engine/tests/ElggCommentTest.php";
333  return $value;
334 }
335 
336 return function(\Elgg\EventsService $events, \Elgg\HooksRegistrationService $hooks) {
337  $events->registerHandler('init', 'system', '_elgg_comments_init');
338  $hooks->registerHandler('unit_test', 'system', '_elgg_comments_test');
339 };
elgg_http_add_url_query_elements($url, array $elements)
Sets elements in a URL&#39;s query string.
Definition: elgglib.php:1199
$object
These two snippets demonstrates triggering an event and how to register for that event.
Definition: trigger.php:7
_elgg_comments_container_permissions_override($hook, $type, $return, $params)
Allow users to comment on entities not owned by them.
Definition: comments.php:205
_elgg_comment_url_handler($hook, $type, $return, $params)
Format and return the URL for a comment.
Definition: comments.php:171
if(!$items) $item
Definition: delete.php:17
elgg_echo($message_key, $args=array(), $language="")
Given a message key, returns an appropriately translated full-text string.
Definition: languages.php:21
elgg_view_resource($name, array $vars=[])
Render a resource view.
Definition: views.php:510
$value
Definition: longtext.php:42
$return
Definition: opendd.php:15
if(!$count) $offset
Definition: pagination.php:26
elgg parse_url
Parse a URL into its parts.
Definition: elgglib.js:450
_elgg_comments_permissions_override($hook, $type, $return, $params)
By default, only authors can edit their comments.
Definition: comments.php:226
elgg forward
Meant to mimic the php forward() function by simply redirecting the user to another page...
Definition: elgglib.js:425
elgg_register_plugin_hook_handler($hook, $type, $callback, $priority=500)
Definition: elgglib.php:740
$comment
Definition: delete.php:10
$url
Definition: exceptions.php:24
$options
Elgg admin footer.
Definition: footer.php:6
_elgg_comments_test($hook, $type, $value, $params)
Runs unit tests for .
Definition: comments.php:330
$params
Definition: login.php:72
$limit
Definition: comments.php:17
_elgg_comments_access_sync($event, $type, $entity)
Update comment access to match that of the container.
Definition: comments.php:289
elgg_instanceof($entity, $type=null, $subtype=null, $class=null)
Checks if $entity is an and optionally for type and subtype.
Definition: entities.php:736
Save menu items.
$key
Definition: summary.php:34
$container
Definition: delete.php:29
elgg_set_ignore_access($ignore=true)
Set if Elgg&#39;s access system should be ignored.
Definition: access.php:43
get_input($variable, $default=null, $filter_result=true)
Get some input from variables passed submitted through GET or POST.
Definition: input.php:27
const REFERER
Definition: elgglib.php:2123
global $CONFIG
$user
Definition: ban.php:13
_elgg_comments_init()
Comments initialization function.
Definition: comments.php:16
elgg_in_context($context)
Check if this context exists anywhere in the stack.
Definition: pageowner.php:241
elgg echo
Translates a string.
Definition: languages.js:48
elgg_get_entities(array $options=array())
Returns an array of entities with optional filtering.
Definition: entities.php:326
elgg_trigger_plugin_hook($hook, $type, $params=null, $returnvalue=null)
Definition: elgglib.php:826
elgg_register_page_handler($identifier, $function)
Registers a page handler for a particular identifier.
Definition: pagehandler.php:34
_elgg_comment_setup_entity_menu($hook, $type, $return, $params)
Setup the menu shown with a comment.
Definition: comments.php:138
elgg global
Pointer to the global context.
Definition: elgglib.js:12
elgg_register_ajax_view($view)
Register a view to be available for ajax calls.
Definition: views.php:221
_elgg_comment_redirect($comment_guid, $fallback_guid)
Redirect to the comment in context of the containing page.
Definition: comments.php:71
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:1375
$comment_guid
Definition: delete.php:9
elgg_register_event_handler($event, $object_type, $callback, $priority=500)
Definition: elgglib.php:550
elgg register_error
Wrapper function for system_messages.
Definition: elgglib.js:399
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:1116
$entity
Definition: delete.php:7
if(elgg_in_context('widget')) $count
Definition: pagination.php:21
_elgg_comments_page_handler($segments)
Page handler for generic comments manipulation.
Definition: comments.php:40
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:526
get_entity($guid)
Loads and returns an entity object from a guid.
Definition: entities.php:204
if(!$display_name) $type
Definition: delete.php:27