-
Notifications
You must be signed in to change notification settings - Fork 0
/
mixcloud-embed-core.php
558 lines (461 loc) · 18.3 KB
/
mixcloud-embed-core.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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
<?php
/**
*
* @author : Domenico Biancardi
* @email : domenico.biancardi@gmail.com
* Created : 29/03/13 - {16.00}
*/
/**
* Get the playlist from a single cloudcast
*/
class MixcloudPlaylist
{
/**
* @var
*/
private $url;
/**
* @var wpdb
*/
private $wpdb;
/**
* @var WP_Error
*/
private $errors;
/**
* @var string
*/
private $table_name;
function __construct($url)
{
global $wpdb;
$this->wpdb = $wpdb;
$this->errors = new WP_Error();
$this->table_name = $this->wpdb->prefix . "mixcloudPlaylist";
$this->url = $url;
}
function __toString()
{
// if the playlist are saved to db, i load it from db
$playlist = $this->wpdb->get_row("SELECT ID, url, playlist FROM " . $this->table_name . " WHERE url = '" . $this->url . "'");
if ($this->wpdb->num_rows > 0) {
$playlist = unserialize($playlist->playlist);
} else {
$playlist = array();
$code = implode("", file($this->url));
if ($code == "") $this->errors->add('no_content', __('The url $url are not valid!'));
preg_match_all("/section-row-track(.+)/", $code, $results);
for ($i = 0; $i < sizeof($results[0]); $i++) {
preg_match("/class=\"tracklisttrackname mx-link\">(.+)<\/a>/U", $results[0][$i], $match);
$title = $match[1];
preg_match("/class=\"tracklistartistname mx-link\">(.+)<\/a>/U", $results[0][$i], $match);
$artist = $match[1];
if ($title != "" || $artist != "") {
$playlist[] = array("title" => $title, "artist" => $artist);
}
}
$this->wpdb->show_errors();
// save to db the playlist for this url
$this->wpdb->insert($this->table_name, array("url" => $this->url, "playlist" => serialize($playlist)), array("%s", "%s"));
}
$code = "<h3>Playlist</h3><ul class='mixcloud-embed-playlist'>";
for ($i = 0; $i < count($playlist); $i++) {
$code .= "<li><span class='mixcloud-embed-position'>" . ($i + 1) . "</span>";
$code .= "<span class='mixcloud-embed-artist'>" . $playlist[$i]["artist"] . "</span>";
$code .= "<span class='mixcloud-embed-title'>" . $playlist[$i]["title"] . "</span></li>";
}
$code .= "</ul>";
return $code;
}
}
/**
* Abstract object of mixcloud embed
*/
abstract class AbstractMixcloudObject
{
/**
* @var
*/
protected $width;
/**
* @var
*/
protected $height;
/**
* @var array
*/
protected $params;
abstract public function getUuid();
/**
* @param $options
* @param $url
*/
function __construct($options, $url)
{
$this->_options = $options;
$this->width = $this->_options["width"];
$this->height = $this->_options["height"];
}
/**
* Make an array from options
*/
function arrayOptions(){
$arrayOpts = array();
$arrayOpts["embed_uuid"] = $this->getUuid();
$arrayOpts["replace"] = 0;
if (!$this -> _options["cover"]) $arrayOpts["hide_cover"] = 1;
if (!$this -> _options["artwork"]) $arrayOpts["hide_artwork"] = 1;
$arrayOpts["embed_type"] = "widget_standard";
if (!$this -> _options["tracklist"]) $arrayOpts["hide_tracklist"] = 1;
$arrayOpts["stylecolor"] = $this->_options['color'];
$arrayOpts["mini"] = $this->_options['mini'];
$arrayOpts["light"] = $this->_options['light'];
$str = "";
foreach($arrayOpts as $k => $v){
$str .= "&$k=$v";
}
return $str;
}
}
class MixcloudEmbedObject extends AbstractMixcloudObject
{
protected $movie;
function __construct($options, $url)
{
parent::__construct($options, $url);
$this->movie = "//www.mixcloud.com/media/swf/player/mixcloudLoader.swf?feed=".urlencode($url) . $this -> arrayOptions();
$this->params["allowFullScreen"] = "true";
$this->params["wmode"] = "opaque";
$this->params["allowscriptaccess"] = "always";
}
public function getUuid()
{
return "8d600ec2-4de4-4196-804f-5d839aac495d";
}
function __toString()
{
$object = new FlashObject($this->movie, $this->width, $this->height, 'Mixcloud Mixes Object', 'mixcloud-embed');
$object->setParams($this->params);
return $object->get();
}
}
class MixcloudEmbedHtml5 extends AbstractMixcloudObject
{
function __construct($options, $url)
{
parent::__construct($options, $url);
$this->movie = "//www.mixcloud.com/widget/iframe/?feed=".urlencode($url) . $this -> arrayOptions();
/**
*
* <iframe width='600' height='180' src='//www.mixcloud.com/widget/iframe/?feed=http%3A%2F%2Fwww.mixcloud.com%2FBJT%2Fbjt-workout-session-1%2F&embed_uuid=25198838-bedd-46c8-81b8-b0e0246e4816&replace=0&hide_cover=1&hide_tracklist=1&hide_artwork=1&stylecolor=#fffff&mini=1&light=1&embed_type=widget_standard' frameborder='0'></iframe>
* <iframe width="660" height="180" src="//www.mixcloud.com/widget/iframe/?feed=http%3A%2F%2Fwww.mixcloud.com%2FBJT%2Fbjt-workout-session-1%2F&embed_uuid=25198838-bedd-46c8-81b8-b0e0246e4816&replace=0&hide_cover=1&hide_artwork=1&embed_type=widget_standard&hide_tracklist=1" frameborder="0"></iframe><div style="clear: both; height: 3px; width: 652px;"></div><p style="display: block; font-size: 11px; font-family: 'Open Sans', Helvetica, Arial, sans-serif; margin: 0px; padding: 3px 4px; color: rgb(153, 153, 153); width: 652px;"><a href="http://www.mixcloud.com/BJT/bjt-workout-session-1/?utm_source=widget&amp;utm_medium=web&amp;utm_campaign=base_links&amp;utm_term=resource_link" target="_blank" style="color:#808080; font-weight:bold;">BJT - WORKOUT SESSION #1</a><span> by </span><a href="http://www.mixcloud.com/BJT/?utm_source=widget&amp;utm_medium=web&amp;utm_campaign=base_links&amp;utm_term=profile_link" target="_blank" style="color:#808080; font-weight:bold;">Bjt</a><span> on </span><a href="http://www.mixcloud.com/?utm_source=widget&utm_medium=web&utm_campaign=base_links&utm_term=homepage_link" target="_blank" style="color:#808080; font-weight:bold;"> Mixcloud</a></p><div style="clear: both; height: 3px; width: 652px;"></div>
*/
}
public function getUuid()
{
return "25198838-bedd-46c8-81b8-b0e0246e4816";
}
function __toString()
{
return "<iframe width='" . $this->width . "' height='" . $this->height . "' src='" . $this->movie . "' frameborder='0'></iframe>";
}
}
class MixcloudMultiEmbedObject extends MixcloudEmbedObject
{
public function getUuid()
{
return "24fa4730-87be-4a1a-a3cf-b83cc4b21651";
}
}
class MixcloudMultiEmbedHtml5 extends MixcloudEmbedHtml5
{
public function getUuid()
{
return "9e28450c-d230-4d75-8fa8-9f2841cb0165";
}
}
class MixcloudProfile
{
private $url;
private $options;
function __construct($options, $url)
{
$this->url = $url;
if (count(explode("/", $this->url) ) > 5) throw new Exception("Error in a profile url, check the right url");
$this->options = $options;
}
public function __toString()
{
$explode = explode("/", $this->url);
$profile = $explode[0] . "//" . $explode[2] . "/" . $explode[3] . "/";
$code = '<a class="mixcloud-follow-widget" href="' . $profile . '" data-h="' . $this->options["height"] . '" data-w="' . $this->options["width"] . '" data-faces="on">Follow ' . $explode[3] . ' on Mixcloud</a><script type="text/javascript" src="http://widget.mixcloud.com/media/js/follow_embed.js"></script>';
return $code;
}
}
class MixcloudEmbedCore
{
private $table_name;
private $wpdb;
private $_options;
private $_ui;
function MixcloudEmbedCore()
{
//constructor
global $wpdb;
$this->wpdb = $wpdb;
$this->errors = new WP_Error();
$this->table_name = $this->wpdb->prefix . "mixcloudPlaylist";
$this->LoadOptions();
}
/**
* Enables the Mixcloud Embed and registers the WordPress hooks
*
* @since 1.5
* @access public
* @static
*/
static function Enable()
{
if (!isset($GLOBALS["me_instance"])) {
$GLOBALS["me_instance"] = new MixcloudEmbedCore();
}
}
/**
* Returns the instance of the Sitemap Generator
*
* @since 1.5
* @access public
* @return MixcloudEmbedCore The instance or null if not available.
*/
static function &GetInstance()
{
if (isset($GLOBALS["me_instance"])) {
return $GLOBALS["me_instance"];
} else return null;
}
/**
* Returns a link pointing back to the plugin page in WordPress
*
* @since 1.5
* @return string The full url
*/
function GetBackLink()
{
global $wp_version;
//admin_url was added in WP 2.6.0
if (function_exists("admin_url")) $url = admin_url("options-general.php?page=" . MixcloudEmbed::GetBaseName());
else $url = $_SERVER['PHP_SELF'] . "?page=" . MixcloudEmbed::GetBaseName();
//Some browser cache the page... great! So lets add some no caching params depending on the WP and plugin version
$url .= '&me_wpv=' . $wp_version . '&me_pv=' . MixcloudEmbed::GetVersion();
return $url;
}
/**
* Shows the option page of the plugin. Before 3.1.1, this function was basically the UI, afterwards the UI was outsourced to another class
*
* @see MixcloudEmbedUI
* @since 1.5
* @return bool
*/
function HtmlShowOptionsPage()
{
if (!empty($_POST['mixcloud-embed_update'])) { //Pressed Button: Update Config
// @TODO verificare il funzionamento
// check_admin_referer('mixcloud-embed');
$this->_options = $_POST;
$message = "";
if ($this->SaveOptions()) $message .= __('Configuration updated', 'mixcloud-embed') . "<br />";
else $message .= __('Error while saving options', 'mixcloud-embed') . "<br />";
}
$ui = $this->GetUI();
if ($ui) {
$ui->HtmlShowOptionsPage();
return true;
}
return false;
}
/**
* Includes the user interface class and intializes it
*
* @since 1.5
* @see MixcloudEmbedUI
* @return MixcloudEmbedUI
*/
function GetUI()
{
if ($this->_ui === null) {
$className = 'MixcloudEmbedUI';
$fileName = 'mixcloud-embed-ui.php';
if (!class_exists($className)) {
$path = trailingslashit(dirname(__FILE__));
if (!file_exists($path . $fileName)) return false;
require_once($path . $fileName);
}
$this->_ui = new $className($this);
}
return $this->_ui;
}
/**
* [mixcloud height="int value" width="int value" iframe="boolean value"]
* The following function creates a "[mixcloud]" shortcode that supports two attributes: ["height" and "width"].
* Both attributes are optional and will take on default options [height="300" width="300" iframe="true"] if they are not provided.
* This shortcode handler function accepts two arguments:
* $atts, an associative array of attributes
* $content, the enclosed content (if the shortcode is used in its enclosing form)
*/
function CreateShortcode($options, $content = null)
{
// read if there are a default value or a customizated value
$options = array(
'height' => (isset($options["height"]) != "") ? $options["height"] : $this->getOption("player_height"),
'width' => (isset($options["width"]) != "") ? $options["width"] : $this->getOption("player_width"),
'color' => (isset($options["color"]) != "") ? $options["color"] : $this->getOption("player_color"),
'iframe' => (isset($options["iframe"]) != "") ? $options["iframe"] : $this->getOption("player_iframe"),
'playlist' => (isset($options["playlist"]) != "") ? $options["playlist"] : $this->getOption("player_playlist"),
'cover' => (isset($options["cover"]) != "") ? $options["cover"] : $this->getOption("player_cover"),
'artwork' => (isset($options["artwork"]) != "") ? $options["artwork"] : $this->getOption("player_artwork"),
'tracklist' => (isset($options["tracklist"]) != "") ? $options["tracklist"] : $this->getOption("player_tracklist"),
'mini' => (isset($options["mini"]) != "") ? $options["mini"] : $this->getOption("player_mini"),
'light' => (isset($options["light"]) != "") ? $options["light"] : $this->getOption("player_light"),
'profile' => (isset($options["profile"]) != "") ? $options["profile"] : $this->getOption("widget_profile"),
);
// clear a width or height value
$options["width"] = str_replace("px", "", $options["width"]);
$options["height"] = str_replace("px", "", $options["height"]);
// the content are required
if ($content == "") {
$this->errors->add('no_url', __('The url to mixcloud stream are required!'));
}
if ($options["profile"] == "true") {
$object = $this->makeProfileWidget($options, $content);
} else {
$object = $this->makeEmbedWidget($options, $content);
}
//print_R(debug_backtrace());
return $object;
}
/**
* @param $options
* @param $content string
* @return MixcloudEmbedObject|MixcloudMultiEmbedObject
*/
private function makeEmbedWidget($options, $content)
{
$multiEmbed = false;
/**
* @var MixcloudPlaylist
*/
$playlistCode = null;
// se ho il content che contiene un url alla playlists allora sto includendo una playlist
if (strpos($content, "/playlists/") !== false) {
$multiEmbed = true;
if ($options["iframe"] == "true") {
$object = new MixcloudMultiEmbedHtml5($options, $content);
} else {
$object = new MixcloudMultiEmbedObject($options, $content);
}
} else {
if ($options["iframe"] == "true") {
$object = new MixcloudEmbedHtml5($options, $content);
} else {
$object = new MixcloudEmbedObject($options, $content);
}
}
if ($options["playlist"] == "true" && !$multiEmbed) {
// get a playlist information
$playlistCode = new MixcloudPlaylist($content);
}
if (sizeof($this->errors->get_error_messages()) > 0) {
$code = "<b>##############<br/>Cannot generate a Mixcloud Embed because: <ul>";
for ($i = 0; $i < sizeof($this->errors->get_error_messages()); $i++) {
$code .= "<li>" . $this->errors->get_error_message("no_url") . "</li>";
}
$code .= "</ul>##############</b>";
}
if ($playlistCode != null)
return $object->__toString() . " " . $playlistCode->__toString();
return $object->__toString();
}
private function makeProfileWidget($atts, $content)
{
return new MixcloudProfile($atts, $content);
}
/**
* Sets up the default configuration
*
* @since 1.5
* @access private
*/
private function InitOptions()
{
$this->_options = array();
$this->_options['mixcloud-embed_player_height'] = '200';
$this->_options['mixcloud-embed_player_width'] = '100%';
$this->_options['mixcloud-embed_player_iframe'] = true;
$this->_options['mixcloud-embed_player_color'] = '#fffff';
$this->_options['mixcloud-embed_player_playlist'] = false;
$this->_options['mixcloud-embed_player_mini'] = false;
$this->_options['mixcloud-embed_player_light'] = false;
$this->_options['mixcloud-embed_widget_profile'] = "";
}
/**
* Loads the configuration from the database
*
* @since 1.5
* @access private
*/
private function LoadOptions()
{
$this->InitOptions();
//First init default values, then overwrite it with stored values so we can add default
//values with an update which get stored by the next edit.
$storedoptions = get_option("mixcloud-embed-settings");
if ($storedoptions && is_array($storedoptions)) {
foreach ($storedoptions AS $k => $v) {
$this->_options[$k] = $v;
}
} else update_option("mixcloud-embed-settings", $this->_options); //First time use, store default values
}
/**
* Returns the option value for the given key
*
* @since 1.5
* @access public
* @param $key string The Configuration Key
* @return mixed The value
*/
public function GetOption($key)
{
$key = "mixcloud-embed_" . $key;
if (array_key_exists($key, $this->_options)) {
return $this->_options[$key];
} else return null;
}
/**
* Sets an option to a new value
*
* @since 1.5
* @access public
* @param $key string The configuration key
* @param $value mixed The new object
*/
public function SetOption($key, $value)
{
if (strstr($key, "mixcloud-embed_") !== 0) $key = "mixcloud-embed_" . $key;
$this->_options[$key] = $value;
}
/**
* Saves the options back to the database
*
* @since 1.5
* @access private
* @return bool true on success
*/
private function SaveOptions()
{
$oldvalue = get_option("mixcloud-embed-settings");
if ($oldvalue == $this->_options) {
return true;
} else return update_option("mixcloud-embed-settings", $this->_options);
}
}
?>