$user->getIconURL('topbar'), 'class' => 'pas mentions-user-icon' )); $replace = elgg_view('output/url', array( 'href' => $user->getURL(), 'text' => $icon . $user->name, 'class' => 'mentions-user-link' )); } else { $replace = elgg_view('output/url', array( 'href' => $user->getURL(), 'text' => $user->name, )); } return $replace .= $period; } else { return $matches[0]; } } /** * Catch all create events and scan for @username tags to notify user. * * @param string $event The event name * @param string $event_type The event type * @param ElggData $object The object that was created * @return void */ function mentions_notification_handler($event, $event_type, $object) { // excludes messages - otherwise an endless loop of notifications occur! if (elgg_instanceof($object, 'object', 'messages')) { return; } $type = $object->getType(); $subtype = $object->getSubtype(); $owner = $object->getOwnerEntity(); $fields = array( 'title', 'description', 'value' ); // store the guids of notified users so they only get one notification per creation event $notified_guids = array(); foreach ($fields as $field) { $content = $object->$field; // it's ok in this case if 0 matches == FALSE if (preg_match_all(mentions_get_regex(), $content, $matches)) { // match against the 2nd index since the first is everything foreach ($matches[1] as $username) { $user = get_user_by_username($username); // check for trailing punctuation caught by the regex if (!$user && substr($username, -1) == '.') { $user = get_user_by_username(rtrim($username, '.')); } if (!$user) { continue; } // user must have access to view object/annotation if ($type == 'annotation') { $annotated_entity = $object->getEntity(); if (!$annotated_entity || !has_access_to_entity($annotated_entity, $user)) { continue; } } else { if (!has_access_to_entity($object, $user)) { continue; } } if (!in_array($user->getGUID(), $notified_guids)) { $notified_guids[] = $user->getGUID(); // if they haven't set the notification status default to sending. // Private settings are stored as strings so we check against "0" $notification_setting = elgg_get_plugin_user_setting('notify', $user->getGUID(), 'mentions'); if ($notification_setting === "0") { continue; } $link = $object->getURL(); $type_key = "mentions:notification_types:$type:$subtype"; $type_str = elgg_echo($type_key); if ($type_str == $type_key) { // plugins can add to the list of mention objects by defining // the language string 'mentions:notification_types::' continue; } $subject = elgg_echo('mentions:notification:subject', array($owner->name, $type_str)); $body = elgg_echo('mentions:notification:body', array( $owner->name, $type_str, $link, )); $params = array( 'object' => $object, 'action' => 'mention', ); notify_user($user->getGUID(), $owner->getGUID(), $subject, $body, $params); } } } } } /** * Save mentions-specific info from the notification form * * @param type $hook * @param type $type * @param type $value * @param type $params */ function mentions_save_settings($hook, $type, $value, $params) { $notify = (bool) get_input('mentions_notify'); $user = get_entity(get_input('guid')); if (!elgg_set_plugin_user_setting('notify', $notify, $user->getGUID(), 'mentions')) { register_error(elgg_echo('mentions:settings:failed')); } return; }