Elgg  Version 6.2
MentionsEventHandler.php
Go to the documentation of this file.
1 <?php
2 
3 namespace Elgg\Notifications;
4 
6 
13 
17  public function getSubscriptions(): array {
18  $entity = $this->getEventEntity();
19  if (!$entity instanceof \ElggEntity || $entity->access_id === ACCESS_PRIVATE) {
20  // no actions taken on private content
21  return [];
22  }
23 
24  $mentions = $this->getMentions();
25  if (empty($mentions)) {
26  return [];
27  }
28 
29  // store the usernames as notified
30  $notified = (array) $entity->_mentioned_usernames;
31  $entity->_mentioned_usernames = array_merge($notified, $mentions);
32 
33  // transform usernames to users to find notification preferences
34  $result = [];
36  'type' => 'user',
37  'limit' => false,
38  'batch' => true,
39  'metadata_name_value_pairs' => [
40  [
41  'name' => 'username',
42  'value' => $mentions,
43  ],
44  ],
45  ]);
46  /* @var $user \ElggUser */
47  foreach ($users as $user) {
48  $preference = $user->getNotificationSettings('mentions');
49  $preference = array_keys(array_filter($preference));
50  if (empty($preference)) {
51  continue;
52  }
53 
54  $result[$user->guid] = $preference;
55  }
56 
57  // remove the actor from the subscribers
58  unset($result[$this->event->getActorGUID()]);
59 
60  return $result;
61  }
62 
66  protected function getNotificationSubject(\ElggUser $recipient, string $method): string {
67  $lan_key = 'notification:mentions:subject';
68  if (elgg_language_key_exists("notification:{$this->event->getDescription()}:subject")) {
69  $lan_key = "notification:{$this->event->getDescription()}:subject";
70  }
71 
72  return elgg_echo($lan_key, [$this->getEventActor()?->getDisplayName()]);
73  }
74 
78  protected function getNotificationBody(\ElggUser $recipient, string $method): string {
79  $entity = $this->getEventEntity();
80 
81  $lan_key = 'notification:mentions:body';
82  if (elgg_language_key_exists("notification:{$this->event->getDescription()}:body")) {
83  $lan_key = "notification:{$this->event->getDescription()}:body";
84  }
85 
86  return elgg_echo($lan_key, [
87  $this->getEventActor()?->getDisplayName(),
88  $entity?->getDisplayName(),
89  $entity?->getURL(),
90  ]);
91  }
92 
96  protected static function isConfigurableForUser(\ElggUser $user): bool {
97  return false;
98  }
99 
103  protected static function isConfigurableForGroup(\ElggGroup $group): bool {
104  return false;
105  }
106 
110  protected function addMuteLink(): bool {
111  return false;
112  }
113 
119  protected function getMetadataFields(): array {
120  return ['description'];
121  }
122 
128  protected function getMentions(): array {
129  $entity = $this->getEventEntity();
130  if (!$entity instanceof \ElggEntity) {
131  return [];
132  }
133 
134  $result = [];
135  $metadata_fields = $this->getMetadataFields();
136 
137  foreach ($metadata_fields as $field) {
138  $value = $entity->$field;
139  if (empty($value)) {
140  continue;
141  }
142 
143  if (!is_array($value)) {
144  $value = [$value];
145  }
146 
147  foreach ($value as $text) {
148  if (empty($text) || !is_string($text)) {
149  continue;
150  }
151 
152  $matches = [];
153  preg_match_all(HtmlFormatter::MENTION_REGEX, $text, $matches);
154 
155  $text_mentions = (array) elgg_extract(3, $matches);
156  // remove empty/duplicate values
157  $text_mentions = array_values(array_unique(array_filter($text_mentions)));
158  // remove trailing punctuation (.) from the username
159  $text_mentions = array_map(function($mention) {
160  return rtrim($mention, '.');
161  }, $text_mentions);
162 
163  // prepare hook so other plugins can extend the mentions found in the text
164  $params = [
165  'entity' => $entity,
166  'field' => $field,
167  'text' => $text,
168  ];
169  $text_mentions = (array) _elgg_services()->events->triggerResults('usernames', 'mentions', $params, $text_mentions);
170 
171  $result = array_merge($result, $text_mentions);
172  }
173  }
174 
175  $result = array_values(array_unique($result));
176 
177  return $this->filterMentions($result);
178  }
179 
188  protected function filterMentions(array $mentions): array {
189  $entity = $this->getEventEntity();
190  if (!$entity instanceof \ElggEntity) {
191  return $mentions;
192  }
193 
194  $already_notified = (array) $entity->_mentioned_usernames;
195 
196  return array_diff($mentions, $already_notified);
197  }
198 }
$entity
Definition: reset.php:8
if(! $annotation->delete()) $lan_key
Definition: delete.php:33
$recipient
Definition: mute.php:8
$params
Saves global plugin settings.
Definition: save.php:13
return[ 'admin/delete_admin_notices'=>['access'=> 'admin'], 'admin/menu/save'=>['access'=> 'admin'], 'admin/plugins/activate'=>['access'=> 'admin'], 'admin/plugins/activate_all'=>['access'=> 'admin'], 'admin/plugins/deactivate'=>['access'=> 'admin'], 'admin/plugins/deactivate_all'=>['access'=> 'admin'], 'admin/plugins/set_priority'=>['access'=> 'admin'], 'admin/security/security_txt'=>['access'=> 'admin'], 'admin/security/settings'=>['access'=> 'admin'], 'admin/security/regenerate_site_secret'=>['access'=> 'admin'], 'admin/site/cache/invalidate'=>['access'=> 'admin'], 'admin/site/flush_cache'=>['access'=> 'admin'], 'admin/site/icons'=>['access'=> 'admin'], 'admin/site/set_maintenance_mode'=>['access'=> 'admin'], 'admin/site/set_robots'=>['access'=> 'admin'], 'admin/site/theme'=>['access'=> 'admin'], 'admin/site/unlock_upgrade'=>['access'=> 'admin'], 'admin/site/settings'=>['access'=> 'admin'], 'admin/upgrade'=>['access'=> 'admin'], 'admin/upgrade/reset'=>['access'=> 'admin'], 'admin/user/ban'=>['access'=> 'admin'], 'admin/user/bulk/ban'=>['access'=> 'admin'], 'admin/user/bulk/delete'=>['access'=> 'admin'], 'admin/user/bulk/unban'=>['access'=> 'admin'], 'admin/user/bulk/validate'=>['access'=> 'admin'], 'admin/user/change_email'=>['access'=> 'admin'], 'admin/user/delete'=>['access'=> 'admin'], 'admin/user/login_as'=>['access'=> 'admin'], 'admin/user/logout_as'=>[], 'admin/user/makeadmin'=>['access'=> 'admin'], 'admin/user/resetpassword'=>['access'=> 'admin'], 'admin/user/removeadmin'=>['access'=> 'admin'], 'admin/user/unban'=>['access'=> 'admin'], 'admin/user/validate'=>['access'=> 'admin'], 'annotation/delete'=>[], 'avatar/upload'=>[], 'comment/save'=>[], 'diagnostics/download'=>['access'=> 'admin'], 'entity/chooserestoredestination'=>[], 'entity/delete'=>[], 'entity/mute'=>[], 'entity/restore'=>[], 'entity/subscribe'=>[], 'entity/trash'=>[], 'entity/unmute'=>[], 'entity/unsubscribe'=>[], 'login'=>['access'=> 'logged_out'], 'logout'=>[], 'notifications/mute'=>['access'=> 'public'], 'plugins/settings/remove'=>['access'=> 'admin'], 'plugins/settings/save'=>['access'=> 'admin'], 'plugins/usersettings/save'=>[], 'register'=>['access'=> 'logged_out', 'middleware'=>[\Elgg\Router\Middleware\RegistrationAllowedGatekeeper::class,],], 'river/delete'=>[], 'settings/notifications'=>[], 'settings/notifications/subscriptions'=>[], 'user/changepassword'=>['access'=> 'public'], 'user/requestnewpassword'=>['access'=> 'public'], 'useradd'=>['access'=> 'admin'], 'usersettings/save'=>[], 'widgets/add'=>[], 'widgets/delete'=>[], 'widgets/move'=>[], 'widgets/save'=>[],]
Definition: actions.php:73
$user
Definition: ban.php:7
if(empty($user_guids)) $users
Definition: ban.php:12
$text
Definition: button.php:33
addMuteLink()
{Add a mute link in the email notification.bool}
filterMentions(array $mentions)
Filter mentions.
getMentions()
Get usernames which are mentioned in the configured metadata fields.
getNotificationBody(\ElggUser $recipient, string $method)
{Get body for the notification.Plugin can define a subtype specific body simply by providing a transl...
getMetadataFields()
Get the metadata fields to check for mentions.
getSubscriptions()
{Returns subscriptions for the event.array}
getNotificationSubject(\ElggUser $recipient, string $method)
{Get subject for the notification.Plugins can define a subtype specific subject simply by providing a...
static isConfigurableForUser(\ElggUser $user)
{Can this event be configured for a specific user.the user to check forbool 4.1}
static isConfigurableForGroup(\ElggGroup $group)
{Can this event be configured for a specific group.For example this can be based on a group tools opt...
Notification Event Handler handles preparation of a notification.
getEventEntity()
Get the entity from the notification event.
Various helper method for formatting and sanitizing output.
const ACCESS_PRIVATE
Definition: constants.php:10
if(elgg_extract('input_type', $vars)) if(elgg_extract('required', $vars)) if(elgg_extract('disabled', $vars)) $field
Definition: field.php:42
_elgg_services()
Get the global service provider.
Definition: elgglib.php:353
elgg_extract($key, $array, $default=null, bool $strict=true)
Checks for $array[$key] and returns its value if it exists, else returns $default.
Definition: elgglib.php:256
elgg_get_entities(array $options=[])
Fetches/counts entities or performs a calculation on their properties.
Definition: entities.php:507
$value
Definition: generic.php:51
elgg_language_key_exists(string $key, string $language='en')
Check if a given language key exists.
Definition: languages.php:44
elgg_echo(string $message_key, array $args=[], string $language='')
Elgg language module Functions to manage language and translations.
Definition: languages.php:17
if(!elgg_get_config('trash_enabled')) $group
Definition: group.php:13