Elgg  Version 2.2
 All Classes Namespaces Files Functions Variables Pages
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  ]) . "#elgg-object-{$comment->guid}";
118 
119  forward($url);
120 }
121 
134  if (elgg_in_context('widgets')) {
135  return $return;
136  }
137 
138  $entity = $params['entity'];
139  if (!elgg_instanceof($entity, 'object', 'comment')) {
140  return $return;
141  }
142 
143  // Remove edit link and access level from the menu
144  foreach ($return as $key => $item) {
145  if ($item->getName() === 'access') {
146  unset($return[$key]);
147  }
148  }
149 
150  return $return;
151 }
152 
167  $entity = $params['entity'];
168  /* @var \ElggObject $entity */
169 
170  if (!elgg_instanceof($entity, 'object', 'comment') || !$entity->getOwnerEntity()) {
171  // not a comment or has no owner
172 
173  // @todo handle anonymous comments
174  return $return;
175  }
176 
177  $container = $entity->getContainerEntity();
178  if (!$container) {
179  return $return;
180  }
181 
182  return "comment/view/{$entity->guid}/{$container->guid}";
183 }
184 
201 
202  // is someone trying to comment, if so override permissions check
203  if ($params['subtype'] === 'comment') {
204  return true;
205  }
206 
207  return $return;
208 }
209 
222  $entity = $params['entity'];
223  $user = $params['user'];
224 
225  if (elgg_instanceof($entity, 'object', 'comment') && $user) {
226  return $entity->getOwnerGUID() == $user->getGUID();
227  }
228 
229  return $return;
230 }
231 
248 function _elgg_comments_notification_email_subject($hook, $type, $returnvalue, $params) {
249  if (!is_array($returnvalue) || !is_array($returnvalue['params'])) {
250  // another hook handler returned a non-array, let's not override it
251  return;
252  }
253 
254  if (empty($returnvalue['params']['notification'])) {
255  return;
256  }
257 
259  $notification = $returnvalue['params']['notification'];
260 
261  if ($notification instanceof Elgg\Notifications\Notification) {
262  $object = elgg_extract('object', $notification->params);
263 
264  if ($object instanceof ElggComment) {
265  $container = $object->getContainerEntity();
266 
267  $returnvalue['subject'] = 'Re: ' . $container->getDisplayName();
268  }
269  }
270 
271  return $returnvalue;
272 }
273 
285  if (!($entity instanceof \ElggEntity)) {
286  return true;
287  }
288 
289  // need to override access in case comments ended up with ACCESS_PRIVATE
290  // and to ensure write permissions
291  $ia = elgg_set_ignore_access(true);
292  $options = array(
293  'type' => 'object',
294  'subtype' => 'comment',
295  'container_guid' => $entity->getGUID(),
296  'wheres' => array(
297  "e.access_id != {$entity->access_id}"
298  ),
299  'limit' => 0,
300  );
301 
302  $batch = new \ElggBatch('elgg_get_entities', $options, null, 25, false);
303  foreach ($batch as $comment) {
304  // Update comment access_id
305  $comment->access_id = $entity->access_id;
306  $comment->save();
307  }
308 
310 
311  return true;
312 }
313 
326  global $CONFIG;
327  $value[] = "{$CONFIG->path}engine/tests/ElggCommentTest.php";
328  return $value;
329 }
330 
331 return function(\Elgg\EventsService $events, \Elgg\HooksRegistrationService $hooks) {
332  $events->registerHandler('init', 'system', '_elgg_comments_init');
333  $hooks->registerHandler('unit_test', 'system', '_elgg_comments_test');
334 };
elgg_http_add_url_query_elements($url, array $elements)
Sets elements in a URL's query string.
Definition: elgglib.php:1171
$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:200
_elgg_comment_url_handler($hook, $type, $return, $params)
Format and return the URL for a comment.
Definition: comments.php:166
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:500
$value
Definition: longtext.php:26
$return
Definition: opendd.php:15
if(!$count) $offset
Definition: pagination.php:26
_elgg_comments_permissions_override($hook, $type, $return, $params)
By default, only authors can edit their comments.
Definition: comments.php:221
elgg_register_plugin_hook_handler($hook, $type, $callback, $priority=500)
Definition: elgglib.php:715
$comment
Definition: delete.php:10
$url
Definition: exceptions.php:24
register_error($error)
Display an error on next page load.
Definition: elgglib.php:452
_elgg_comments_test($hook, $type, $value, $params)
Runs unit tests for .
Definition: comments.php:325
$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:284
elgg_instanceof($entity, $type=null, $subtype=null, $class=null)
Checks if $entity is an and optionally for type and subtype.
Definition: entities.php:716
$key
Definition: summary.php:34
$container
Definition: delete.php:29
elgg_set_ignore_access($ignore=true)
Set if Elgg'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:2029
$item
Definition: item.php:12
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_get_entities(array $options=array())
Returns an array of entities with optional filtering.
Definition: entities.php:316
elgg_trigger_plugin_hook($hook, $type, $params=null, $returnvalue=null)
Definition: elgglib.php:801
$options
RSS comments view.
Definition: comments.php:8
_elgg_comment_setup_entity_menu($hook, $type, $return, $params)
Setup the menu shown with a comment.
Definition: comments.php:133
elgg_register_ajax_view($view)
Register a view to be available for ajax calls.
Definition: views.php:220
_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:1310
$comment_guid
Definition: delete.php:9
elgg_register_event_handler($event, $object_type, $callback, $priority=500)
Definition: elgglib.php:538
forward($location="", $reason= 'system')
Forward to $location.
Definition: elgglib.php:93
$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:506
get_entity($guid)
Loads and returns an entity object from a guid.
Definition: entities.php:204
if(!$display_name) $type
Definition: delete.php:27