-
Notifications
You must be signed in to change notification settings - Fork 2
/
MentionsParse.php
104 lines (91 loc) · 2.02 KB
/
MentionsParse.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
101
102
103
104
<?php
if ( ! defined('e107_INIT')) {
exit;
}
require_once __DIR__ . '/Mentions.php';
class MentionsParse extends Mentions
{
/**
* Parses mentions in user submitted text
*
* @param string $text
* The text string to be parsed.
* @return string
* Parsed text string.
*/
protected function parseMentions($text)
{
$mText = '';
$pattern = '#(^|\w*@\s*[a-z0-9._]+)#mi';
$phrases = preg_split($pattern, $text, -1,
PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE);
foreach ($phrases as $phrase) {
$mention = $this->hasUserMentionIn($phrase);
if ($mention) {
$mText .= $this->createUserLinkFrom($mention);
} else {
$mText .= $phrase;
}
}
return $mText;
}
/**
* Checks input for user mention match and return that if found
*
* @param string $input
* String to be parsed for user mentions.
* @return bool
* Returns with boolean 'false' if no match and the matched string if match.
*/
protected function hasUserMentionIn($input)
{
$pattern = '#^(@[a-z0-9_.]*)$#i';
if (preg_match($pattern, $input, $matches)) {
return $matches[0];
}
return false;
}
/**
* Determines if the context of current text is in plugins preferred context
* @param string $context
* Context marker for current text
* @return bool
* true if yes, false if not
*/
protected function isInContextOf($context)
{
$ctxArray = $this->chosenContexts();
if ((array) $ctxArray !== $ctxArray && null === $ctxArray) {
return true;
}
foreach ($ctxArray as $ctxItem) {
if ($ctxItem === $context) {
return true;
}
}
return false;
}
/**
* Gets admin chosen text context preference as workable solution
*
* @return array|null
*/
protected function chosenContexts()
{
$contextPref = $this->prefs['mentions_contexts'];
switch ($contextPref) {
case 1:
return ['USER_BODY'];
break;
case 2:
return ['USER_BODY', 'OLDDEFAULT'];
break;
case 3:
return ['USER_BODY', 'BODY', 'OLDDEFAULT'];
break;
default:
return null;
break;
}
}
}