123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213 |
- <?php
- elgg_register_event_handler('init', 'system', 'mentions_init');
- function mentions_init() {
- elgg_extend_view('css/elgg', 'css/mentions');
- elgg_require_js('mentions/autocomplete');
- elgg_extend_view('input/longtext', 'mentions/popup');
- elgg_extend_view('input/plaintext', 'mentions/popup');
- elgg_register_event_handler('pagesetup', 'system', 'mentions_get_views');
-
- elgg_register_event_handler('create', 'object', 'mentions_notification_handler');
- elgg_register_event_handler('create', 'annotation', 'mentions_notification_handler');
-
-
-
- elgg_extend_view('notifications/subscriptions/personal', 'mentions/notification_settings');
- elgg_register_plugin_hook_handler('action', 'notificationsettings/save', 'mentions_save_settings');
- }
- function mentions_get_regex() {
-
-
-
- return '/[\b]?@([\p{L}\p{M}_\.0-9]+)[\b]?/iu';
- }
- function mentions_get_views() {
-
- $views = array('output/longtext');
- $views = elgg_trigger_plugin_hook('get_views', 'mentions', null, $views);
- foreach ($views as $view) {
- elgg_register_plugin_hook_handler('view', $view, 'mentions_rewrite');
- }
- }
- function mentions_rewrite($hook, $entity_type, $returnvalue, $params) {
- $regexp = mentions_get_regex();
- $returnvalue = preg_replace_callback($regexp, 'mentions_preg_callback', $returnvalue);
- return $returnvalue;
- }
- function mentions_preg_callback($matches) {
- $user = get_user_by_username($matches[1]);
- $period = '';
- $icon = '';
-
- if (!$user && substr($matches[1], -1) == '.') {
- $user = get_user_by_username(rtrim($matches[1], '.'));
- $period = '.';
- }
- if ($user) {
- if (elgg_get_plugin_setting('fancy_links', 'mentions')) {
- $icon = elgg_view('output/img', array(
- 'src' => $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];
- }
- }
- function mentions_notification_handler($event, $event_type, $object) {
-
- if (elgg_instanceof($object, 'object', 'messages')) {
- return;
- }
- $type = $object->getType();
- $subtype = $object->getSubtype();
- $owner = $object->getOwnerEntity();
- $fields = array(
- 'title', 'description', 'value'
- );
-
- $notified_guids = array();
- foreach ($fields as $field) {
- $content = $object->$field;
-
- if (preg_match_all(mentions_get_regex(), $content, $matches)) {
-
- foreach ($matches[1] as $username) {
- $user = get_user_by_username($username);
-
- if (!$user && substr($username, -1) == '.') {
- $user = get_user_by_username(rtrim($username, '.'));
- }
- if (!$user) {
- continue;
- }
-
- 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();
-
-
- $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) {
-
-
- 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);
- }
- }
- }
- }
- }
- 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;
- }
|