Elgg  Version master
Mentions.php
Go to the documentation of this file.
1 <?php
2 
4 
7 
14 
18  public function getSubscriptions(): array {
19  $entity = $this->getEventEntity();
20  if (!$entity instanceof \ElggEntity || $entity->access_id === ACCESS_PRIVATE) {
21  // no actions taken on private content
22  return [];
23  }
24 
25  $mentions = $this->getMentions();
26  if (empty($mentions)) {
27  return [];
28  }
29 
30  // store the usernames as notified
31  $notified = (array) $entity->_mentioned_usernames;
32  $entity->_mentioned_usernames = array_merge($notified, $mentions);
33 
34  // transform usernames to users to find notification preferences
35  $result = [];
37  'type' => 'user',
38  'limit' => false,
39  'batch' => true,
40  'metadata_name_value_pairs' => [
41  [
42  'name' => 'username',
43  'value' => $mentions,
44  ],
45  ],
46  ]);
47  /* @var $user \ElggUser */
48  foreach ($users as $user) {
49  $preference = $user->getNotificationSettings('mentions', true);
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 function addMuteLink(): bool {
97  return false;
98  }
99 
105  protected function getMetadataFields(): array {
106  return ['description'];
107  }
108 
114  protected function getMentions(): array {
115  $entity = $this->getEventEntity();
116  if (!$entity instanceof \ElggEntity) {
117  return [];
118  }
119 
120  $result = [];
121  $metadata_fields = $this->getMetadataFields();
122 
123  foreach ($metadata_fields as $field) {
124  $value = $entity->$field;
125  if (empty($value)) {
126  continue;
127  }
128 
129  if (!is_array($value)) {
130  $value = [$value];
131  }
132 
133  foreach ($value as $text) {
134  if (empty($text) || !is_string($text)) {
135  continue;
136  }
137 
138  $matches = [];
139  preg_match_all(HtmlFormatter::MENTION_REGEX, $text, $matches);
140 
141  $text_mentions = (array) elgg_extract(3, $matches);
142  // remove empty/duplicate values
143  $text_mentions = array_values(array_unique(array_filter($text_mentions)));
144  // remove trailing punctuation (.) from the username
145  $text_mentions = array_map(function($mention) {
146  return rtrim($mention, '.');
147  }, $text_mentions);
148 
149  // prepare hook so other plugins can extend the mentions found in the text
150  $params = [
151  'entity' => $entity,
152  'field' => $field,
153  'text' => $text,
154  ];
155  $text_mentions = (array) _elgg_services()->events->triggerResults('usernames', 'mentions', $params, $text_mentions);
156 
157  $result = array_merge($result, $text_mentions);
158  }
159  }
160 
161  $result = array_values(array_unique($result));
162 
163  return $this->filterMentions($result);
164  }
165 
174  protected function filterMentions(array $mentions): array {
175  $entity = $this->getEventEntity();
176  if (!$entity instanceof \ElggEntity) {
177  return $mentions;
178  }
179 
180  $already_notified = (array) $entity->_mentioned_usernames;
181 
182  return array_diff($mentions, $already_notified);
183  }
184 }
$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/clear'=>['access'=> 'admin'], 'admin/site/cache/invalidate'=>['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', 'controller'=> \Elgg\Diagnostics\DownloadController::class,], '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:76
$user
Definition: ban.php:7
if(empty($user_guids)) $users
Definition: ban.php:12
$text
Definition: button.php:26
Mentions notification handler.
Definition: Mentions.php:13
getMetadataFields()
Get the metadata fields to check for mentions.
Definition: Mentions.php:105
getNotificationSubject(\ElggUser $recipient, string $method)
{Get subject for the notification.Plugins can define a subtype specific subject simply by providing a...
Definition: Mentions.php:66
getSubscriptions()
{Returns subscriptions for the event.array}
Definition: Mentions.php:18
filterMentions(array $mentions)
Filter mentions.
Definition: Mentions.php:174
addMuteLink()
{Add a mute link in the email notification.bool}
Definition: Mentions.php:96
getMentions()
Get usernames which are mentioned in the configured metadata fields.
Definition: Mentions.php:114
getNotificationBody(\ElggUser $recipient, string $method)
{Get body for the notification.Plugin can define a subtype specific body simply by providing a transl...
Definition: Mentions.php:78
A notification event handler which isn't configurable by a user.
getEventEntity()
Get the entity from the notification event.
Various helper methods 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:343
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:246
$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
elgg_get_entities(array $options=[])
Fetches/counts entities or performs a calculation on their properties.
Definition: entities.php:507