-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathoui_embed.php
211 lines (187 loc) · 6.33 KB
/
oui_embed.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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
<?php
# --- BEGIN PLUGIN CODE ---
use Embed\Embed;
if (class_exists('\Textpattern\Tag\Registry')) {
// Register Textpattern tags for TXP 4.6+.
Txp::get('\Textpattern\Tag\Registry')
->register('oui_embed')
->register('oui_embed_info');
}
if (txpinterface === 'admin') {
add_privs('prefs.oui_embed', '1');
add_privs('plugin_prefs.oui_embed', '1');
register_callback('oui_embed_welcome', 'plugin_lifecycle.oui_embed');
register_callback('oui_embed_install', 'prefs', null, 1);
}
function oui_embed_welcome($evt, $stp)
{
switch ($stp) {
case 'installed':
case 'enabled':
oui_embed_install();
break;
case 'deleted':
if (function_exists('remove_pref')) {
// 4.6 API
remove_pref(null, 'oui_embed');
} else {
safe_delete('txp_prefs', "event='oui_embed'");
}
safe_delete('txp_lang', "name LIKE 'oui\_embed%'");
break;
}
}
/**
* Set prefs through:
*
* PREF_PLUGIN for 4.6+
* PREF_ADVANCED for 4.5
*/
function oui_embed_install()
{
$prefList = array(
'oui_embed_providers_oembed_parameters',
'oui_embed_providers_oembed_embedlykey',
'oui_embed_providers_oembed_iframelykey',
'oui_embed_providers_facebook_key',
'oui_embed_providers_google_key',
'oui_embed_providers_soundcloud_key',
);
$position = 10;
foreach ($prefList as $pref => $options) {
if (get_pref($pref, null) === null) {
set_pref(
$pref,
'',
'oui_embed',
defined('PREF_PLUGIN') ? PREF_PLUGIN : PREF_ADVANCED,
'text_input',
$position
);
}
$position = ++;
}
}
function oui_embed($atts, $thing === null)
{
global $prefs, $embed;
extract(lAtts(array(
'url' => '',
'info' => 'code',
'label' => '',
'labeltag' => '',
'wraptag' => '',
'class' => '',
'responsive' => '',
'cache_time' => '0',
'hash_key' => '194820'
), $atts));
// Prepare cache variables
$keybase = md5($url.$info.$label.$labeltag.$wraptag.$class.$responsive.$thing);
$hash = str_split($hash_key);
$cachekey = '';
foreach ($hash as $hashskip) {
$cachekey .= $keybase[$hashskip];
}
$cachedate = get_pref('cacheset');
$cachefile = find_temp_dir().DS.'oui_embed_data_'.$cachekey;
$cacheexists = file_exists($cachefile) ? true : false;
$needcache = (($cache_time > 0) && ((!$cacheexists) || (time() - $cachedate) > $cache_time)) ? true : false;
$readcache = (($cache_time > 0) && ($cacheexists)) ? true : false;
// Cache_time is not set, or a new cache file is needed; throw a new request
if ($needcache || $cache_time == 0) {
$config = [
'providers' => [
'oembed' => [
'parameters' => [get_pref('oui_embed_providers_oembed_parameters')],
'embedlyKey' => get_pref('oui_embed_providers_oembed_embedlykey'),
'iframelyKey' => get_pref('oui_embed_providers_oembed_iframelykey')
],
'html' => [
'maxImages' => get_pref('oui_embed_providers_html_maximages')
],
'facebook' => [
'key' => get_pref('oui_embed_providers_facebook_key')
],
'google' => [
'key' => get_pref('oui_embed_providers_google_key')
],
'soundcloud' => [
'key' => get_pref('oui_embed_providers_soundcloud_key')
]
]
];
$embed = Embed::create($url, $config);
// Container tag use
if ($thing === null) {
$data = $embed->$info;
if (is_array($data)) {
implode('', $data);
}
if (($info === 'code') && $responsive) {
// Add padding-top if responsive attribute is set
$ratio = number_format($embed->aspectRatio, 2, '.', '').'%';
$out = (($label) ? doLabel($label, $labeltag) : '').\n
.'<div class="oui_embed '.$class.'" style="padding-top:'.$ratio.'">'.\n
.' '.$data.\n
.'</div>'.\n;
} else {
$out = (($label) ? doLabel($label, $labeltag) : '').\n
.(($wraptag) ? doWrap($data, $wraptag, $class) : $data);
};
// Single tag use
} else {
$data = parse($thing);
$out = (($label) ? doLabel($label, $labeltag) : '').\n
.(($wraptag) ? doTag($data, $wraptag, $class) : $data);
}
}
// Cache file is needed
if ($needcache) {
// Remove old cache files
$oldcaches = glob($cachefile);
if (!empty($oldcaches)) {
foreach ($oldcaches as $todel) {
unlink($todel);
}
}
// Time stamp and write the new cache files and return
set_pref('cacheset', time(), 'oui_embed', PREF_HIDDEN, 'text_input');
$cache = fopen($cachefile, 'w+');
fwrite($cache, $out);
fclose($cache);
}
// Cache is on and file is found, get it!
if ($readcache) {
$cache_out = file_get_contents($cachefile);
return $cache_out;
// No cache file :(
} else {
return $out;
}
}
function oui_embed_info($atts)
{
global $embed;
extract(lAtts(array(
'type' => '',
'label' => '',
'labeltag' => '',
'wraptag' => '',
'class' => '',
'responsive' => ''
), $atts));
$data = $embed->$info;
if (($info === 'code') && $responsive) {
// Add padding-top if responsive attribute is set
$ratio = number_format($embed->aspectRatio, 2, '.', '').'%';
$out = (($label) ? doLabel($label, $labeltag) : '').\n
.'<div class="oui_embed '.$class.'" style="padding-top:'.$ratio.'">'.\n
.' '.$data.\n
.'</div>'.\n;
} else {
$out = (($label) ? doLabel($label, $labeltag) : '').\n
.(($wraptag) ? doTag($data, $wraptag, $class) : $data);
};
return $out;
}