-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUstream-live-status-widget.php
507 lines (405 loc) · 18.5 KB
/
Ustream-live-status-widget.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
<?php
/*
Plugin Name: Ustream Live Status Widget
Plugin URI: http://URI_Of_Page_Describing_Plugin_and_Updates
Description: A widget to show your live status from ustream and a video preview.
Version: 1.1
Author: lion2486
Author URI: http://codescar.eu
*/
class UstreamLiveStatusWidget extends WP_Widget {
private $instance;
static $textDomain = "UstreamLiveStatus";
// constructor
public function UstreamLiveStatusWidget() {
parent::WP_Widget('UstreamLiveStatusWidget', $name = __('Ustreal Live Status Widget', self::$textDomain ),
array( 'description' => __( 'A widget to see the status of your live streaming in Ustream and embeded video player.', self::$textDomain ) )
);
}
public function init(){
register_widget( "UstreamLiveStatusWidget" );
add_action( 'wp_ajax_UstreamAjax', array( $this, 'getAjax' ) );
add_action( 'wp_ajax_nopriv_UstreamAjax', array( $this, 'getAjax' ) );
}
private function renderFormInput( $fieldName, $fieldLabel, $inputValue = "", $inputType = "text", $inputExtraAttr = "" ){
?>
<p>
<label for="<?php echo $this->get_field_id( $fieldName ); ?>">
<?php _e( $fieldLabel, self::$textDomain ); ?>
</label>
<input class="widefat"
id="<?php echo $this->get_field_id( $fieldName ); ?>"
name="<?php echo $this->get_field_name( $fieldName ); ?>"
type="<?php echo $inputType; ?>"
<?php echo $inputExtraAttr ? $inputExtraAttr : ""; ?>
value="<?php echo $inputValue; ?>" />
</p>
<?php
}
// widget form creation
public function form($instance) {
// Check values
if( $instance ) {
$instance['UID'] = esc_attr($instance['UID']);
$instance['CacheInterval'] = esc_attr($instance['CacheInterval']); //in seconds
$instance['dev_key'] = esc_attr($instance['dev_key']);
$instance['JsInitialTimeout'] = esc_attr($instance['JsInitialTimeout']);
$instance['JsMAXtimeout'] = esc_attr($instance['JsMAXtimeout']);
$instance['OnlineImg'] = esc_attr($instance['OnlineImg']);
$instance['OnlineMsg'] = esc_attr($instance['OnlineMsg']);
$instance['OnlineSlug'] = esc_attr($instance['OnlineSlug']);
$instance['OfflineImg'] = esc_attr($instance['OfflineImg']);
$instance['OfflineMsg'] = esc_attr($instance['OfflineMsg']);
$instance['OfflineSlug'] = esc_attr($instance['OfflineSlug']);
$instance['VideoPreview'] = esc_attr($instance['VideoPreview']);
$instance['VideoHeight'] = esc_attr($instance['VideoHeight']); //in pixels
$instance['VideoWidth'] = esc_attr($instance['VideoWidth']); //in pixels
$instance['FullVideoPage'] = esc_attr($instance['FullVideoPage']);
$instance['FullVideoWidth'] = esc_attr($instance['FullVideoWidth']);
$instance['FullVideoHeight'] = esc_attr($instance['FullVideoHeight']);
$instance['WidgetTitle'] = esc_attr($instance['WidgetTitle']);
$instance['WidgetText'] = esc_attr($instance['WidgetText']);
$instance['AutoPlay'] = esc_attr($instance['AutoPlay']);
$instance['PlayerAutoResize'] = esc_attr($instance['PlayerAutoResize']);
} else {
$instance = array();
$instance['UID'] = "";
$instance['CacheInterval'] = 60; //in seconds
$instance['dev_key'] = "";
$instance['JsInitialTimeout'] = 5000;
$instance['JsMAXtimeout'] = 60000;
$instance['OnlineImg'] = plugin_dir_url( __FILE__ ) . "online.png";
$instance['OnlineMsg'] = __( 'Live Streaming is Available', self::$textDomain);
$instance['OnlineSlug'] = __( 'Online', self::$textDomain);
$instance['OfflineImg'] = plugin_dir_url( __FILE__ ) . "offline.png";
$instance['OfflineMsg'] = __( 'Live Streaming is Unavailable', self::$textDomain);
$instance['OfflineSlug'] = __( 'Offline', self::$textDomain);
$instance['VideoPreview'] = true;
$instance['VideoHeight'] = "130"; //in pixels
$instance['VideoWidth'] = "220"; //in pixels
$instance['FullVideoPage'] = true;
$instance['FullVideoWidth'] = "480";
$instance['FullVideoHeight'] = "302";
$instance['WidgetTitle'] = "Your title here";
$instance['WidgetText'] = "Your text here";
$instance['AutoPlay'] = true;
$instance['PlayerAutoResize'] = true;
}
$this->renderFormInput( "WidgetTitle", "Widget Title", $instance['WidgetTitle']);
$this->renderFormInput( "WidgetText", "Widget Text", $instance['WidgetText']);
echo "<hr/>";
$this->renderFormInput( "UID", "Ustream UID", $instance['UID']);
$this->renderFormInput( "dev_key", "Ustream dev key", $instance['dev_key']);
echo "<hr/>";
$this->renderFormInput( "CacheInterval", "Status Cache Interval (in sec)", $instance['CacheInterval'], "number", "min='0' max='10000'");
$this->renderFormInput( "JsInitialTimeout", "Initial refresh Interval(in ms)", $instance['JsInitialTimeout'], "number", "min='1000' max='10000'");
$this->renderFormInput( "JsMAXtimeout", "Max refresh Interval(in ms)", $instance['JsMAXtimeout'], "number", "min='10000' max='1000000'");
echo "<hr/>";
$this->renderFormInput( "OnlineImg", "Online Image", $instance['OnlineImg']);
$this->renderFormInput( "OnlineMsg", "Online Message", $instance['OnlineMsg']);
$this->renderFormInput( "OnlineSlug", "Online Slug", $instance['OnlineSlug']);
echo "<hr/>";
$this->renderFormInput( "OfflineImg", "Offline Image", $instance['OfflineImg']);
$this->renderFormInput( "OfflineMsg", "Offline Message", $instance['OfflineMsg']);
$this->renderFormInput( "OfflineSlug", "Offline SLug", $instance['OfflineSlug']);
echo "<hr/>";
$this->renderFormInput( "VideoPreview", "Video Preview in Widget", "true", "checkbox", checked( $instance['VideoPreview'], 'true', false ) );
$this->renderFormInput( "VideoHeight", "Video Preview Height(in px)", $instance['VideoHeight'], "number", "min=\"40\" max=\"1080\"");
$this->renderFormInput( "VideoWidth", "Video Preview Width(in px)", $instance['VideoWidth'], "number", "min=\"40\" max=\"1080\"");
echo "<hr/>";
$this->renderFormInput( "FullVideoPage", "Full Page Video Player", "true", "checkbox", checked( $instance['FullVideoPage'], 'true', false ) );
$this->renderFormInput( "FullVideoHeight", "Full Video Player Height(in px)", $instance['FullVideoHeight'], "number", "min=\"40\" max=\"1080\"");
$this->renderFormInput( "FullVideoWidth", "Full Video Player Width(in px)", $instance['FullVideoWidth'], "number", "min=\"40\" max=\"1080\"");
echo "<hr/>";
$this->renderFormInput( "AutoPlay", "Video Auto Play", "true", "checkbox", checked( $instance['AutoPlay'] , 'true', false ) );
$this->renderFormInput( "PlayerAutoResize", "Auto Resize Player", "true", "checkbox", checked( $instance['PlayerAutoResize'] , 'true', false ) );
}
// update widget
public function update($new_instance, $old_instance) {
$instance = $old_instance;
// Fields
$instance['UID'] = strip_tags($new_instance['UID']);
$instance['CacheInterval'] = strip_tags($new_instance['CacheInterval']); //in seconds
$instance['dev_key'] = strip_tags($new_instance['dev_key']);
$instance['JsInitialTimeout'] = strip_tags($new_instance['JsInitialTimeout']);
$instance['JsMAXtimeout'] = strip_tags($new_instance['JsMAXtimeout']);
$instance['OnlineImg'] = strip_tags($new_instance['OnlineImg']);
$instance['OnlineMsg'] = strip_tags($new_instance['OnlineMsg']);
$instance['OnlineSlug'] = strip_tags($new_instance['OnlineSlug']);
$instance['OfflineImg'] = strip_tags($new_instance['OfflineImg']);
$instance['OfflineMsg'] = strip_tags($new_instance['OfflineMsg']);
$instance['OfflineSlug'] = strip_tags($new_instance['OfflineSlug']);
$instance['VideoPreview'] = strip_tags($new_instance['VideoPreview']);
$instance['VideoHeight'] = strip_tags($new_instance['VideoHeight']); //in pixels
$instance['VideoWidth'] = strip_tags($new_instance['VideoWidth']); //in pixels
$instance['FullVideoPage'] = strip_tags($new_instance['FullVideoPage']);
$instance['FullVideoWidth'] = strip_tags($new_instance['FullVideoWidth']);
$instance['FullVideoHeight'] = strip_tags($new_instance['FullVideoHeight']);
$instance['WidgetTitle'] = strip_tags($new_instance['WidgetTitle']);
$instance['WidgetText'] = strip_tags($new_instance['WidgetText']);
$instance['AutoPlay'] = strip_tags($new_instance['AutoPlay']);
$instance['PlayerAutoResize'] = strip_tags($new_instance['PlayerAutoResize']);
return $instance;
}
public function footer_script( ){
$ajax_url = admin_url('admin-ajax.php');
?>
<script type="text/javascript">
var timeout = <?php echo $this->instance['JsInitialTimeout']; ?>;
var MAXtimeout = <?php echo $this->instance['JsMAXtimeout']; ?>;
var UID = <?php echo $this->instance['UID']; ?>;
<?php if( $this->instance['FullVideoPage'] ) : ?>
function video_pop(){
jQuery.ajax({
url: '<?php echo $ajax_url; ?>',
type: 'POST',
data: {
action: 'UstreamAjax',
WID: '<?php echo $this->number; ?>',
popOut: '1'
},
success: function (data) {
var popOut = window.open( "", "<?php echo $this->instance['WidgetTitle']; ?>",
"width=<?php echo $this->instance['FullVideoWidth']+50; ?>,height=<?php echo $this->instance['FullVideoHeight']+130; ?>" );
popOut.document.write(data);
}
});
}
<?php endif; ?>
jQuery().ready(function(){
checkLiveVideoStatus();
});
var online = false;
function checkLiveVideoStatus(){
jQuery.ajax({
url: '<?php echo $ajax_url; ?>',
type: 'POST',
data: {
action : 'UstreamAjax',
WID: '<?php echo $this->number; ?>',
get_status: '1'
},
success: function(data){
if(data != "false"){
//Status is active!
if(!online) {
jQuery("#LiveButton img").attr("src", "<?php echo $this->instance['OnlineImg']; ?>");
jQuery("#LiveButton img").attr("alt", "<?php echo $this->instance['OnlineMsg']; ?>");
jQuery("#LiveButton img").attr("title", "<?php echo $this->instance['OnlineMsg']; ?>");
jQuery("#LiveButton span").text("<?php $this->instance['OnlineSlug']; ?>");
jQuery("#LiveButton").attr("title", "<?php echo $this->instance['OnlineMsg']; ?>");
UID = data;
<?php if( $this->instance['VideoPreview'] ) : ?>
jQuery.ajax({
url: '<?php echo $ajax_url; ?>',
type: 'POST',
data: {
action: 'UstreamAjax',
WID: '<?php echo $this->number; ?>',
getPlayer: '1'
},
success: function (data) {
jQuery("#VideoPreview").html(data);
jQuery("#VideoPreview").show("slow");
}
});
<?php endif; ?>
}
online = true;
}else {
if (online) {
jQuery("#LiveButton img").attr("src", "<?php echo $this->instance['OfflineImg']; ?>");
jQuery("#LiveButton img").attr("alt", "<?php echo $this->instance['OfflineMsg']; ?>");
jQuery("#LiveButton img").attr("title", "<?php echo $this->instance['OfflineMsg']; ?>");
jQuery("#LiveButton span").text("<?php echo $this->instance['OfflineSlug']; ?>");
jQuery("#LiveButton").attr("title", "<?php echo $this->instance['OfflineMsg']; ?>");
<?php if( $this->instance['VideoPreview'] ) : ?>
jQuery("#VideoPreview").hide("slow");
<?php endif; ?>
}
online = false;
}
if(timeout < MAXtimeout)
timeout += 300;
setTimeout(checkLiveVideoStatus, timeout);
}
});
}
</script>
<?php
}
// display widget
public function widget($args, $instance) {
$this->instance = $instance;
add_action('wp_footer', array( $this, 'footer_script' ) );
extract( $args );
// these are the widget options
$title = apply_filters('widget_title', $instance['WidgetTitle']);
echo $before_widget;
// Display the widget
echo '<div class="widget-text wp_widget_plugin_box UstreamLiveStatus">';
// Check if title is set
if ( $title ) {
echo $before_title . $title . $after_title;
}
if( $instance['VideoPreview'] ) : ?>
<div style="display:none;" id="VideoPreview"></div>
<?php
endif;
if( $instance['WidgetText'] ) {
echo '<p class="wp_widget_plugin_text">'.$instance['WidgetText'].'</p>';
}
?>
<button <?php if( $instance['FullVideoPage'] ) { echo 'onclick="video_pop();"'; } ?> id="LiveButton" style="width: 140px; display: inherit; height: 35px; margin: 4px auto 4px auto;" title="<?php echo $instance['OfflineSlug']; ?>">
<img src="<?php echo plugin_dir_url( __FILE__ ); ?>offline.png" style="height: 26px; vertical-align: middle;" alt="<?php echo $instance['OfflineMsg']; ?>" title="<?php echo $instance['OfflineMsg']; ?>" /><span><?php echo $instance['OfflineSlug']; ?></span>
</button>
</div>
<?php
echo $after_widget;
}
private static function is_broadcast_live( $UID, $dev_key, $CacheInterval = 60 ){
$call_url = "http://api.ustream.tv/php/channel/$UID/getValueOf/status?key=$dev_key";
$CacheFile = "StatusFile".$UID.".cache";
if(file_exists($CacheFile) && (time() - filemtime($CacheFile) < $CacheInterval )){
//we can use local cache file
$resultsArray = unserialize(file_get_contents($CacheFile));
}else{
// Get and config the curl session object
$session = curl_init($call_url);
curl_setopt($session, CURLOPT_HEADER, false);
curl_setopt($session, CURLOPT_RETURNTRANSFER, true);
//execute the request and close
$response = curl_exec($session);
curl_close($session);
// this line works because we requested $format='php' and not some other output format
$resultsArray = unserialize($response);
@unlink($CacheFile);
file_put_contents($CacheFile, $response, LOCK_EX);
}
// this is your data returned; you could do something more useful here than just echo it
//print_r( $resultsArray);
return ($resultsArray['results'] == "live") ? $UID : false;
}
private static function get_player($UID, $WIDTH = 480, $HEIGHT = 302, $AUTOPLAY = "false", $AUTORESIZE = true){
$player_object = '
<iframe
src="http://www.ustream.tv/embed/'. $UID .'?html5ui&autoplay='. $AUTOPLAY .'"
style="border: 0; width: 100%; height: calc( 100vh - 144px );"
webkitallowfullscreen
allowfullscreen
frameborder="no"
width="'. $WIDTH .'"
height="'. $HEIGHT .'">
</iframe>
';
/* Old player code
* $player_object = '
<object width="'. $WIDTH .'" height="'. $HEIGHT .'" classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000"
class="UstreavVideoPlayer">
<param name="flashvars" value="cid='.$UID.'&autoplay='. $AUTOPLAY .'"/>
<param name="allowfullscreen" value="true"/>
<param name="allowscriptaccess" value="always"/>
<param name="autoplay" value="'. $AUTOPLAY .'" />
<param name="src" value="http://www.ustream.tv/flash/viewer.swf"/>
<embed flashvars="cid='.$UID.'&autoplay='. $AUTOPLAY .'" width="'. $WIDTH .'" height="'. $HEIGHT .'" allowfullscreen="true" allowscriptaccess="always" src="http://www.ustream.tv/flash/viewer.swf" type="application/x-shockwave-flash"></embed>
</object>';*/
if( $AUTORESIZE )
{
$player_object .= '
<script type="text/javascript">
jQuery().ready(function(){
jQuery(\'.UstreavVideoPlayer embed\').each(function( index ) {
jQuery(this).height(jQuery(this).width() * 9 /16 );
});
});
jQuery( window ).resize(function() {
jQuery(\'.UstreavVideoPlayer embed\').each(function( index ) {
jQuery(this).height(jQuery(this).width() * 9 /16 );
});
});
</script>
<style type="text/css">
.UstreavVideoPlayer embed{
min-width: '. $WIDTH .'px;
min-height: '. $HEIGHT .'px;
width: 100%;
}
</style>';
}else{
$player_object .= '
<style type="text/css">
.UstreavVideoPlayer embed{
width: '. $WIDTH .'px;
height: '. $HEIGHT .'px;
}
</style>';
}
return $player_object;
}
public static function getAjax(){
if( isset( $_REQUEST['get_status'] ) && $_REQUEST['get_status'] == 1 ){
$WID = intval( $_REQUEST['WID'] );
$dummy = new UstreamLiveStatusWidget();
$settings = $dummy->get_settings();
if( !isset( $settings[ $WID ] ))
wp_die( json_encode( false ) );
$instance = $settings[$WID];
wp_die( json_encode( self::is_broadcast_live( $instance['UID'], $instance['dev_key'], $instance['CacheInterval'] ) ) );
}
elseif( isset( $_REQUEST['getPlayer'] ) && !empty( $_REQUEST['getPlayer'] ) ){
$WID = intval( $_REQUEST['WID'] );
$dummy = new UstreamLiveStatusWidget();
$settings = $dummy->get_settings();
if( !isset( $settings[ $WID ] ))
wp_die( json_encode( false ) );
$instance = $settings[$WID];
echo self::get_player( $instance['UID'], $instance['VideoWidth'], $instance['VideoHeight'], $instance['AutoPlay'] );
}
elseif( isset( $_REQUEST['popOut'] ) ){
$WID = intval( $_REQUEST['WID'] );
$dummy = new UstreamLiveStatusWidget();
$settings = $dummy->get_settings();
if( !isset( $settings[ $WID ] ))
wp_die( json_encode( false ) );
$instance = $settings[$WID];
?>
<!doctype HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title><?php echo $instance['WidgetTitle']; ?></title>
</head>
<body>
<h1><?php echo $instance['WidgetTitle']; ?></h1>
<p><?php echo $instance['WidgetText']; ?></p>
<?php echo self::get_player( $instance['UID'], $instance['FullVideoWidth'], $instance['FullVideoHeight'], $instance['PlayerAutoResize'] ); ?>
</body>
</html>
<?php
wp_die();
}
}
/**
* @param $atts array the parameters for the player
*
* example: [UstreamPlayer uid="12345" width="480" height="302" autoplay="true" playerautoresize="true"]
*
* @return string the result in html
*/
public static function getPlayer( $atts ){
$args = shortcode_atts( array(
'uid' => '0',
'width' => '480',
'height' => '302',
'autoplay' => 'true',
'playerautoresize' => true,
),
$atts );
$player = self::get_player( $args['uid'], $args['width'], $args['height'], $args['autoplay'], $args['playerautoresize'] );
return $player;
}
} //endClass
// register widget
$widget = new UstreamLiveStatusWidget();
add_action( 'widgets_init', array( $widget, 'init' ) );
add_shortcode( 'UstreamPlayer', array( 'UstreamLiveStatusWidget', 'getPlayer' ) );