-
Notifications
You must be signed in to change notification settings - Fork 2
/
Mentions.php
100 lines (82 loc) · 2.07 KB
/
Mentions.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
<?php
if ( ! defined('e107_INIT')) {
exit;
}
class Mentions
{
protected $prefs;
protected $parse;
/**
* Mentions constructor.
*/
public function __construct()
{
$this->prefs = e107::getPlugPref('mentions');
$this->parse = e107::getParser();
}
/**
* Converts valid user mention to user profile-link
*
* @param string $mention
* User mention string
*
* @return string
* User mention profile-link or string prepended with '@'
*/
protected function createUserLinkFrom($mention)
{
$data = $this->getUserData($mention);
if ($data['user_name'] === $this->stripAtFrom($mention)) {
$userData =
['id' => $data['user_id'], 'name' => $data['user_name']];
$link = e107::getUrl()->create('user/profile/view', $userData);
return '<a href="' . $link . '">' . $mention . '</a>';
}
return $mention;
}
/**
* Get user data from database
*
* @param string $mention
* String prepended with '@' which the parsing logic captured.
*
* @return array
* User details from 'user' table - user_id, user_name, user_email
*/
protected function getUserData($mention)
{
$username = e107::getParser()->toDB($this->stripAtFrom($mention));
$row = e107::getDb()->retrieve("user", "user_name, user_id, user_email",
"user_name = '" . $username . "' ");
return $row;
}
/**
* Strips '@' sign from mention string
*
* @param string $mention
* String prepended with '@'.
* @return string
* String striped clean of '@'
*/
protected function stripAtFrom($mention)
{
return ltrim($mention, '@');
}
/**
* Does Debug logging by writing a log file to the plugin directory
*
* @param string|array $content
* The data to be logged - can be passed as string or array.
* @param string $logname
* The name of log that need to be written to file-system.
*/
protected function log($content, $logname = 'mentions')
{
$path = e_PLUGIN . 'mentions/' . $logname . '.txt';
if (is_array($content)) {
$content = var_export($content, true);
}
file_put_contents($path, $content . "\n", FILE_APPEND);
unset($path, $content);
}
}