Elgg  Version 1.11
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');
25 
26  elgg_register_page_handler('comment', '_elgg_comments_page_handler');
27 
28  elgg_register_ajax_view('core/ajax/edit_comment');
29 }
30 
38 function _elgg_comments_page_handler($page) {
39 
40  switch ($page[0]) {
41 
42  case 'edit':
44 
45  if (empty($page[1])) {
46  register_error(elgg_echo('generic_comment:notfound'));
48  }
49  $comment = get_entity($page[1]);
50  if (!($comment instanceof \ElggComment) || !$comment->canEdit()) {
51  register_error(elgg_echo('generic_comment:notfound'));
53  }
54 
55  $target = $comment->getContainerEntity();
56  if (!($target instanceof \ElggEntity)) {
57  register_error(elgg_echo('generic_comment:notfound'));
59  }
60 
61  $title = elgg_echo('generic_comments:edit');
62  elgg_push_breadcrumb($target->getDisplayName(), $target->getURL());
64 
65  $params = array(
66  'entity' => $target,
67  'comment' => $comment,
68  'is_edit_page' => true,
69  );
70  $content = elgg_view_form('comment/save', null, $params);
71 
72  $params = array(
73  'content' => $content,
74  'title' => $title,
75  'filter' => '',
76  );
77  $body = elgg_view_layout('content', $params);
79 
80  return true;
81  break;
82 
83  case 'view':
84  _elgg_comment_redirect(elgg_extract(1, $page), elgg_extract(2, $page));
85  break;
86 
87  default:
88  return false;
89  break;
90  }
91 }
92 
102 function _elgg_comment_redirect($comment_guid, $fallback_guid) {
103  $fail = function () {
104  register_error(elgg_echo('generic_comment:notfound'));
105  forward(REFERER);
106  };
107 
109  if (!$comment) {
110  // try fallback if given
111  $fallback = get_entity($fallback_guid);
112  if (!$fallback) {
113  $fail();
114  }
115 
116  register_error(elgg_echo('generic_comment:notfound_fallback'));
117  forward($fallback->getURL());
118  }
119 
120  if (!elgg_instanceof($comment, 'object', 'comment')) {
121  $fail();
122  }
123 
124  $container = $comment->getContainerEntity();
125  if (!$container) {
126  $fail();
127  }
128 
129  // this won't work with threaded comments, but core doesn't support that yet
131  'type' => 'object',
132  'subtype' => 'comment',
133  'container_guid' => $container->guid,
134  'count' => true,
135  'wheres' => ["e.guid < " . (int)$comment->guid],
136  ]);
137  $limit = (int)get_input('limit');
138  if (!$limit) {
139  $limit = elgg_trigger_plugin_hook('config', 'comments_per_page', [], 25);
140  }
141  $offset = floor($count / $limit) * $limit;
142  if (!$offset) {
143  $offset = null;
144  }
145 
147  'offset' => $offset,
148  ]) . "#elgg-object-{$comment->guid}";
149 
150  forward($url);
151 }
152 
165  if (elgg_in_context('widgets')) {
166  return $return;
167  }
168 
169  $entity = $params['entity'];
170  if (!elgg_instanceof($entity, 'object', 'comment')) {
171  return $return;
172  }
173 
174  // Remove edit link and access level from the menu
175  foreach ($return as $key => $item) {
176  if ($item->getName() === 'access') {
177  unset($return[$key]);
178  }
179  }
180 
181  return $return;
182 }
183 
198  $entity = $params['entity'];
199  /* @var \ElggObject $entity */
200 
201  if (!elgg_instanceof($entity, 'object', 'comment') || !$entity->getOwnerEntity()) {
202  // not a comment or has no owner
203 
204  // @todo handle anonymous comments
205  return $return;
206  }
207 
208  $container = $entity->getContainerEntity();
209  if (!$container) {
210  return $return;
211  }
212 
213  return "comment/view/{$entity->guid}/{$container->guid}";
214 }
215 
232 
233  // is someone trying to comment, if so override permissions check
234  if ($params['subtype'] === 'comment') {
235  return true;
236  }
237 
238  return $return;
239 }
240 
252  $entity = $params['entity'];
253  $user = $params['user'];
254 
255  if (elgg_instanceof($entity, 'object', 'comment') && $user) {
256  return $entity->getOwnerGUID() == $user->getGUID();
257  }
258 
259  return $return;
260 }
261 
277 function _elgg_comments_notification_email_subject($hook, $type, $returnvalue, $params) {
278  if (!is_array($returnvalue)) {
279  // another hook handler returned a non-array, let's not override it
280  return;
281  }
282 
284  $notification = elgg_extract('notification', $returnvalue['params']);
285 
286  if ($notification instanceof Elgg\Notifications\Notification) {
287  $object = elgg_extract('object', $notification->params);
288 
289  if ($object instanceof ElggComment) {
290  $container = $object->getContainerEntity();
291 
292  $returnvalue['subject'] = 'Re: ' . $container->getDisplayName();
293  }
294  }
295 
296  return $returnvalue;
297 }
298 
310  if (!($entity instanceof \ElggEntity)) {
311  return true;
312  }
313 
314  // need to override access in case comments ended up with ACCESS_PRIVATE
315  // and to ensure write permissions
316  $ia = elgg_set_ignore_access(true);
317  $options = array(
318  'type' => 'object',
319  'subtype' => 'comment',
320  'container_guid' => $entity->getGUID(),
321  'wheres' => array(
322  "e.access_id != {$entity->access_id}"
323  ),
324  'limit' => 0,
325  );
326 
327  $batch = new \ElggBatch('elgg_get_entities', $options, null, 25, false);
328  foreach ($batch as $comment) {
329  // Update comment access_id
330  $comment->access_id = $entity->access_id;
331  $comment->save();
332  }
333 
335 
336  return true;
337 }
338 
351  global $CONFIG;
352  $value[] = "{$CONFIG->path}engine/tests/ElggCommentTest.php";
353  return $value;
354 }
355 
356 return function(\Elgg\EventsService $events, \Elgg\HooksRegistrationService $hooks) {
357  $events->registerHandler('init', 'system', '_elgg_comments_init');
358  $hooks->registerHandler('unit_test', 'system', '_elgg_comments_test');
359 };
elgg_http_add_url_query_elements($url, array $elements)
Sets elements in a URL&#39;s query string.
Definition: elgglib.php:1107
elgg_push_breadcrumb($title, $link=null)
Adds a breadcrumb to the breadcrumbs stack.
Definition: navigation.php:244
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_container_permissions_override($hook, $type, $return, $params)
Allow users to comment on entities not owned by them.
Definition: comments.php:231
_elgg_comment_url_handler($hook, $type, $return, $params)
Format and return the URL for a comment.
Definition: comments.php:197
$object
Definition: upgrade.php:12
$value
Definition: longtext.php:26
$ia
Definition: upgrade.php:26
$return
Definition: opendd.php:15
if(!$count) $offset
Definition: pagination.php:25
_elgg_comments_page_handler($page)
Page handler for generic comments manipulation.
Definition: comments.php:38
_elgg_comments_permissions_override($hook, $type, $return, $params)
By default, only authors can edit their comments.
Definition: comments.php:251
elgg_extract($key, array $array, $default=null, $strict=true)
Checks for $array[$key] and returns its value if it exists, else returns $default.
Definition: elgglib.php:1246
elgg forward
Meant to mimic the php forward() function by simply redirecting the user to another page...
Definition: elgglib.js:419
elgg_register_plugin_hook_handler($hook, $type, $callback, $priority=500)
Definition: elgglib.php:703
$comment
Definition: delete.php:10
$url
Definition: exceptions.php:24
$title
Definition: save.php:24
_elgg_comments_test($hook, $type, $value, $params)
Runs unit tests for .
Definition: comments.php:350
$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:309
elgg_gatekeeper()
Used at the top of a page to mark it as logged in users only.
Definition: pagehandler.php:58
elgg_instanceof($entity, $type=null, $subtype=null, $class=null)
Checks if $entity is an and optionally for type and subtype.
Definition: entities.php:922
Save menu items.
if($entity) $container
Definition: access.php:62
elgg_echo($message_key, $args=array(), $language="")
Given a message key, returns an appropriately translated full-text string.
Definition: languages.php:21
$key
Definition: summary.php:34
elgg_set_ignore_access($ignore=true)
Set if Elgg&#39;s access system should be ignored.
Definition: access.php:43
const REFERER
Definition: elgglib.php:1995
$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:250
elgg echo
Translates a string.
Definition: languages.js:43
elgg_get_entities(array $options=array())
Returns an array of entities with optional filtering.
Definition: entities.php:490
elgg_trigger_plugin_hook($hook, $type, $params=null, $returnvalue=null)
Definition: elgglib.php:775
elgg_register_page_handler($identifier, $function)
Registers a page handler for a particular identifier.
Definition: pagehandler.php:34
$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:164
elgg global
Pointer to the global context.
Definition: elgglib.js:12
$type
Definition: add.php:8
_elgg_comment_redirect($comment_guid, $fallback_guid)
Redirect to the comment in context of the containing page.
Definition: comments.php:102
$comment_guid
Definition: delete.php:9
elgg_view_layout($layout_name, $vars=array())
Displays a layout with optional parameters.
Definition: views.php:622
elgg_register_event_handler($event, $object_type, $callback, $priority=500)
Definition: elgglib.php:519
elgg register_error
Wrapper function for system_messages.
Definition: elgglib.js:383
elgg_register_ajax_view($view)
Register a view to be available for ajax calls.
Definition: views.php:208
$content
Set robots.txt action.
Definition: set_robots.php:6
if(elgg_in_context('widget')) $count
Definition: pagination.php:20
elgg_view_page($title, $body, $page_shell= 'default', $vars=array())
Assembles and outputs a full page.
Definition: views.php:437
$target
Definition: responses.php:19
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:711
$entity
Definition: delete.php:10
elgg_view_form($action, $form_vars=array(), $body_vars=array())
Definition: views.php:1313
get_entity($guid)
Loads and returns an entity object from a guid.
Definition: entities.php:382