-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMW_EXT_URL.class.php
153 lines (141 loc) · 4.09 KB
/
MW_EXT_URL.class.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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
<?php
namespace MediaWiki\Extension\PkgStore;
use MWException;
use OutputPage, Parser, Skin;
/**
* Class MW_EXT_URL
*/
class MW_EXT_URL
{
/**
* * Clear URL.
*
* @param $string
*
* @return string
*/
private static function clearURL($string): string
{
return rawurlencode(trim($string));
}
/**
* Register tag function.
*
* @param Parser $parser
*
* @return void
* @throws MWException
*/
public static function onParserFirstCallInit(Parser $parser): void
{
$parser->setFunctionHook('url', [__CLASS__, 'onRenderTag']);
}
/**
* Render `URL` function.
*
* @param Parser $parser
* @param string $type
* @param string $content
* @param string $title
*
* @return string|null
*/
public static function onRenderTag(Parser $parser, string $type = '', string $content = '', string $title = ''): ?string
{
// Argument: type.
$getType = MW_EXT_Kernel::outClear($type ?? '' ?: '');
// Argument: content.
$getContent = MW_EXT_Kernel::outClear($content ?? '' ?: '');
// Argument: title.
$getTitle = MW_EXT_Kernel::outClear($title ?? '' ?: '');
// Build URL.
switch ($getType) {
case 'address':
$outContent = $getContent;
$outTitle = $getTitle ? $getTitle : $outContent;
$outScheme = 'https://';
$outIcon = 'fas fa-map-marker-alt';
$outClass = 'address';
$outURL = 'google.ru/maps/place/' . self::clearURL($outContent);
break;
case 'email':
$outContent = $getContent;
$outTitle = $getTitle ? $getTitle : $getContent;
$outScheme = 'mailto:';
$outIcon = 'fas fa-envelope';
$outClass = 'email';
$outURL = $outContent;
break;
case 'tel':
$outContent = preg_replace('#[^0-9\+]#', '', $getContent);
$outTitle = $getTitle ? $getTitle : $getContent;
$outScheme = 'tel:';
$outIcon = 'fas fa-phone';
$outClass = 'tel';
$outURL = $outContent;
break;
case 'fax':
$outContent = preg_replace('#[^0-9\+]#', '', $getContent);
$outTitle = $getTitle ? $getTitle : $getContent;
$outScheme = 'fax:';
$outIcon = 'fas fa-fax';
$outClass = 'fax';
$outURL = $outContent;
break;
case 'whatsapp':
$outContent = preg_replace('#[^0-9]#', '', $getContent);
$outTitle = $getTitle ? $getTitle : $outContent;
$outScheme = 'https://';
$outIcon = 'fab fa-whatsapp';
$outClass = 'whatsapp';
$outURL = 'api.whatsapp.com/send?phone=' . $outContent;
break;
case 'tg':
$outContent = $getContent;
$outTitle = $getTitle ? $getTitle : $outContent;
$outScheme = 'tg://';
$outIcon = 'fab fa-telegram';
$outClass = 'tg';
$outURL = 'resolve?domain=' . $outContent;
break;
case 'viber':
$outContent = $getContent;
$outTitle = $getTitle ? $getTitle : $outContent;
$outScheme = 'viber://';
$outIcon = 'fab fa-viber';
$outClass = 'viber';
$outURL = 'public?id=' . $outContent;
break;
case 'skype':
$outContent = $getContent;
$outTitle = $getTitle ? $getTitle : $outContent;
$outScheme = 'skype:';
$outIcon = 'fab fa-skype';
$outClass = 'skype';
$outURL = $outContent;
break;
default:
$parser->addTrackingCategory('mw-url-error-category');
return null;
}
// Out HTML.
$outHTML = '<a class="mw-url mw-url-' . $outClass . '" href="' . $outScheme . $outURL . '" target="_blank" rel="nofollow">';
$outHTML .= '<i class="fa-icon ' . $outIcon . '"></i>';
$outHTML .= '<span>' . $outTitle . '</span>';
$outHTML .= '</a>';
// Out parser.
return $parser->insertStripItem($outHTML, $parser->getStripState());
}
/**
* Load resource function.
*
* @param OutputPage $out
* @param Skin $skin
*
* @return void
*/
public static function onBeforePageDisplay(OutputPage $out, Skin $skin): void
{
$out->addModuleStyles(['ext.mw.url.styles']);
}
}