| 1 |
<?php |
| 2 |
|
| 3 |
/* |
| 4 |
* To change this license header, choose License Headers in Project Properties. |
| 5 |
* To change this template file, choose Tools | Templates |
| 6 |
* and open the template in the editor. |
| 7 |
*/ |
| 8 |
|
| 9 |
|
| 10 |
/** |
| 11 |
* Description of class-wpstream-player |
| 12 |
* |
| 13 |
* @author cretu |
| 14 |
*/ |
| 15 |
class Wpstream_Player{ |
| 16 |
public function __construct($plugin_main) { |
| 17 |
$this->main = $plugin_main; |
| 18 |
|
| 19 |
add_filter( 'the_content',array($this, 'wpstream_filter_the_title') ); |
| 20 |
add_action( 'woocommerce_before_single_product', array($this,'wpstream_user_logged_in_product_already_bought') ); |
| 21 |
} |
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
/** |
| 27 |
* Insert player in page |
| 28 |
* |
| 29 |
* @author cretu |
| 30 |
*/ |
| 31 |
public function wpstream_filter_the_title( $content ) { |
| 32 |
if( is_singular('wpstream_product')){ |
| 33 |
global $post; |
| 34 |
$args=array('id'=>$post->ID); |
| 35 |
$custom_content = $this->wpstream_insert_player_inpage($args); |
| 36 |
$content = '<div class="wpestream_inserted_player">'.$custom_content.'</div>'.$content; |
| 37 |
return $content; |
| 38 |
}else{ |
| 39 |
return $content; |
| 40 |
} |
| 41 |
} |
| 42 |
|
| 43 |
/** |
| 44 |
* Insert player in page |
| 45 |
* |
| 46 |
* @author cretu |
| 47 |
*/ |
| 48 |
|
| 49 |
public function wpstream_insert_player_inpage($attributes, $content = null){ |
| 50 |
$product_id = ''; |
| 51 |
$return_string = ''; |
| 52 |
$attributes = shortcode_atts( |
| 53 |
array( |
| 54 |
'id' => 0, |
| 55 |
), $attributes) ; |
| 56 |
|
| 57 |
|
| 58 |
if ( isset($attributes['id']) ){ |
| 59 |
$product_id=$attributes['id']; |
| 60 |
} |
| 61 |
|
| 62 |
ob_start(); |
| 63 |
$this->wpstream_video_player_shortcode($product_id); |
| 64 |
$return_string= ob_get_contents(); |
| 65 |
ob_end_clean(); |
| 66 |
|
| 67 |
return $return_string; |
| 68 |
} |
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
/** |
| 74 |
* Video Player shortcode |
| 75 |
* |
| 76 |
* @author cretu |
| 77 |
*/ |
| 78 |
|
| 79 |
public function wpstream_video_player_shortcode($from_sh_id='') { |
| 80 |
|
| 81 |
if ( is_user_logged_in() ) { |
| 82 |
global $product; |
| 83 |
$current_user = wp_get_current_user(); |
| 84 |
$product_id = intval($from_sh_id); |
| 85 |
|
| 86 |
|
| 87 |
if ( ( in_array( 'woocommerce/woocommerce.php', apply_filters( 'active_plugins', get_option( 'active_plugins' ) ) ) && wc_customer_bought_product( $current_user->user_email, $current_user->ID, $product_id) ) || get_post_type($product_id)=='wpstream_product' ){ |
| 88 |
global $product; |
| 89 |
echo '<div class="wpstream_player_wrapper wpstream_player_shortcode"><div class="wpstream_player_container">'; |
| 90 |
|
| 91 |
|
| 92 |
if( get_post_type($product_id) == 'wpstream_product' ){ |
| 93 |
|
| 94 |
$wpstream_product_type = esc_html(get_post_meta($product_id, 'wpstream_product_type', true)); |
| 95 |
if($wpstream_product_type==1){ |
| 96 |
$this->wpstream_live_event_player($product_id); |
| 97 |
} else if($wpstream_product_type==2 || $wpstream_product_type==3){ |
| 98 |
$this->wpstream_video_on_demand_player($product_id); |
| 99 |
} |
| 100 |
|
| 101 |
}else{ |
| 102 |
$term_list = wp_get_post_terms($product_id, 'product_type'); |
| 103 |
$is_subscription_live_event = esc_html(get_post_meta($product_id,'_subscript_live_event',true)); |
| 104 |
|
| 105 |
if( $term_list[0]->name=='live_stream' || ( $term_list[0]->name=='subscription' && $is_subscription_live_event=='yes' ) ){ |
| 106 |
$this->wpstream_live_event_player($product_id); |
| 107 |
}else if( $term_list[0]->name=='video_on_demand' || ($term_list[0]->name=='subscription' && $is_subscription_live_event=='no' ) ){ |
| 108 |
$this->wpstream_video_on_demand_player($product_id); |
| 109 |
} |
| 110 |
} |
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
}else{ |
| 115 |
$thumb_id = get_post_thumbnail_id($product_id); |
| 116 |
$thumb = wp_get_attachment_image_src($thumb_id,'medium_large'); |
| 117 |
|
| 118 |
echo '<div class="wpstream_player_wrapper wpstream_player_shortcode no_buy"><div class="wpstream_player_container">'; |
| 119 |
echo '<div class="wpstream_notice">'.__('You did not buy this product!','wpstream'). |
| 120 |
'</div><img src="'.$thumb[0].'" alt="product_thumb">'; |
| 121 |
} |
| 122 |
|
| 123 |
echo '</div></div>'; |
| 124 |
}else{ |
| 125 |
$product_id = intval($from_sh_id); |
| 126 |
if( get_post_type($product_id) == 'wpstream_product' ){ |
| 127 |
|
| 128 |
$wpstream_product_type = esc_html(get_post_meta($product_id, 'wpstream_product_type', true)); |
| 129 |
if($wpstream_product_type==1){ |
| 130 |
$this->wpstream_live_event_player($product_id); |
| 131 |
} else if($wpstream_product_type==2 || $wpstream_product_type==3){ |
| 132 |
$this->wpstream_video_on_demand_player($product_id); |
| 133 |
} |
| 134 |
|
| 135 |
} |
| 136 |
} |
| 137 |
} |
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
/** |
| 145 |
* Live Event Player |
| 146 |
* |
| 147 |
* @author cretu |
| 148 |
*/ |
| 149 |
function remove_http($url) { |
| 150 |
$disallowed = array('http://', 'https://'); |
| 151 |
foreach($disallowed as $d) { |
| 152 |
if(strpos($url, $d) === 0) { |
| 153 |
return str_replace($d, '', $url); |
| 154 |
} |
| 155 |
} |
| 156 |
return $url; |
| 157 |
} |
| 158 |
|
| 159 |
|
| 160 |
/** |
| 161 |
* Live Event Player |
| 162 |
* |
| 163 |
* @author cretu |
| 164 |
*/ |
| 165 |
|
| 166 |
function wpstream_live_event_player($product_id){ |
| 167 |
$live_event_uri = get_post_meta($product_id,'live_event_uri',true); |
| 168 |
$api20_event_id = get_post_meta($product_id,'server_id',true); |
| 169 |
|
| 170 |
$thumb_id = get_post_thumbnail_id($product_id); |
| 171 |
$thumb = wp_get_attachment_image_src($thumb_id,'small'); |
| 172 |
|
| 173 |
$live_event_uri_final = $this->wpstream_request_hls_player_dynamic('',$product_id);// buleala lu Gabi |
| 174 |
$now = time().rand(0,10); |
| 175 |
$live_conect_array = explode('live.streamer.wpstream.net',$live_event_uri_final); |
| 176 |
$live_conect_views = $live_conect_array[0].'live.streamer.wpstream.net'; |
| 177 |
$live_conect_views = $this->remove_http($live_conect_views); |
| 178 |
|
| 179 |
echo '<div class="wpstream_live_player_wrapper" id="wpstream_live_player_wrapper'.$now.'" > ' |
| 180 |
. '<div id="wpestream_live_counting" class="wpestream_live_counting"></div>'; |
| 181 |
|
| 182 |
if(trim($live_event_uri_final)==''){ |
| 183 |
print '<div class="wpstream_not_live_mess"><div class="wpstream_not_live_mess_back"></div><div class="wpstream_not_live_mess_mess">'.esc_html__('We are not live at this moment. Please check back later.','wpstream').'</div></div>'; |
| 184 |
}else{ |
| 185 |
print '<script type="text/javascript"> |
| 186 |
//<![CDATA[ |
| 187 |
jQuery(document).ready(function(){ |
| 188 |
wpstream_count_connect_plugin("wpstream_live_player_wrapper'.$now.'" ,"'.$live_conect_views.'"); |
| 189 |
}); |
| 190 |
//]]> |
| 191 |
</script>'; |
| 192 |
} |
| 193 |
|
| 194 |
echo' |
| 195 |
<video id="wpstream-video'.$now.'" poster="'.$thumb[0].'" class="video-js vjs-default-skin vjs-16-9" controls> |
| 196 |
<source |
| 197 |
src="'.$live_event_uri_final.'" |
| 198 |
type="application/x-mpegURL"> |
| 199 |
</video>'; |
| 200 |
|
| 201 |
print '<script type="text/javascript"> |
| 202 |
//<![CDATA[ |
| 203 |
jQuery(document).ready(function(){ |
| 204 |
wpstream_player_initialize("'.$now.'","'.$live_event_uri_final.'","'.$live_conect_views.'"); |
| 205 |
}); |
| 206 |
//]]> |
| 207 |
</script>'; |
| 208 |
|
| 209 |
print '</div>'; |
| 210 |
usleep (10000); |
| 211 |
|
| 212 |
} |
| 213 |
|
| 214 |
|
| 215 |
/** |
| 216 |
* HLS PLAYER |
| 217 |
* |
| 218 |
* @author cretu |
| 219 |
*/ |
| 220 |
|
| 221 |
|
| 222 |
public function wpstream_request_hls_player_dynamic($server_id,$product_id){ |
| 223 |
|
| 224 |
$transient_name = 'hls_to_return_'.$product_id; |
| 225 |
$hls_to_return = get_transient( $transient_name ); |
| 226 |
|
| 227 |
if ( false === $hls_to_return || $hls_to_return=='' ) { |
| 228 |
|
| 229 |
$token = $this->main->wpstream_live_connection->wpstream_get_token(); |
| 230 |
$values_array = array(); |
| 231 |
$values_array['server_id'] = $server_id; |
| 232 |
|
| 233 |
if($server_id==''){ |
| 234 |
$this->main->wpstream_live_connection->wpstream_check_event_status_front_player($product_id,$server_id); |
| 235 |
$server_id = get_post_meta($product_id,'server_id', true ); |
| 236 |
$values_array['server_id'] = $server_id; |
| 237 |
} |
| 238 |
|
| 239 |
$url = WPSTREAM_CLUBLINKSSL."://www.".WPSTREAM_CLUBLINK."/wp-json/rcapi/v1/api20-livestrem/ap20_get_player_hls_dynamic/?access_token=".$token; |
| 240 |
|
| 241 |
|
| 242 |
$arguments = array( |
| 243 |
'method' => 'GET', |
| 244 |
'timeout' => 45, |
| 245 |
'redirection' => 5, |
| 246 |
'httpversion' => '1.0', |
| 247 |
'blocking' => true, |
| 248 |
'headers' => array(), |
| 249 |
'body' => $values_array, |
| 250 |
'cookies' => array() |
| 251 |
); |
| 252 |
$response = wp_remote_post($url,$arguments); |
| 253 |
|
| 254 |
$received_data = json_decode( wp_remote_retrieve_body($response) ,true); |
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
if( isset($response['response']['code']) && $response['response']['code']=='200'){ |
| 259 |
$hls_to_return = trim($received_data); |
| 260 |
set_transient( $transient_name, $hls_to_return, 60 ); |
| 261 |
return $hls_to_return = trim($received_data); |
| 262 |
}else{ |
| 263 |
return 'failed connection'; |
| 264 |
} |
| 265 |
exit(); |
| 266 |
}else{ |
| 267 |
return $hls_to_return; |
| 268 |
} |
| 269 |
|
| 270 |
} |
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
/** |
| 275 |
* HLS PLAYER |
| 276 |
* |
| 277 |
* @author cretu |
| 278 |
*/ |
| 279 |
|
| 280 |
|
| 281 |
public function wpstream_request_hls_player($product_id){ |
| 282 |
|
| 283 |
$transient_name = 'hls_to_return_'.$product_id; |
| 284 |
$hls_to_return = get_transient( $transient_name ); |
| 285 |
|
| 286 |
if ( false === $hls_to_return || $hls_to_return=='' ) { |
| 287 |
|
| 288 |
$show_id = intval($product_id); |
| 289 |
$token = $this->main->wpstream_live_connection->wpstream_get_token(); |
| 290 |
$values_array = array(); |
| 291 |
$values_array['show_id'] = $show_id; |
| 292 |
|
| 293 |
$url = WPSTREAM_CLUBLINKSSL."://www.".WPSTREAM_CLUBLINK."/wp-json/rcapi/v1/videos/ap20_get_player_hls/?access_token=".$token; |
| 294 |
|
| 295 |
|
| 296 |
$arguments = array( |
| 297 |
'method' => 'GET', |
| 298 |
'timeout' => 45, |
| 299 |
'redirection' => 5, |
| 300 |
'httpversion' => '1.0', |
| 301 |
'blocking' => true, |
| 302 |
'headers' => array(), |
| 303 |
'body' => $values_array, |
| 304 |
'cookies' => array() |
| 305 |
); |
| 306 |
$response = wp_remote_post($url,$arguments); |
| 307 |
|
| 308 |
$received_data = json_decode( wp_remote_retrieve_body($response) ,true); |
| 309 |
|
| 310 |
|
| 311 |
|
| 312 |
if( isset($response['response']['code']) && $response['response']['code']=='200'){ |
| 313 |
$hls_to_return = trim($received_data); |
| 314 |
set_transient( $transient_name, $hls_to_return, 60 ); |
| 315 |
return $hls_to_return = trim($received_data); |
| 316 |
}else{ |
| 317 |
return 'failed connection'; |
| 318 |
} |
| 319 |
exit(); |
| 320 |
}else{ |
| 321 |
return $hls_to_return; |
| 322 |
} |
| 323 |
|
| 324 |
} |
| 325 |
|
| 326 |
|
| 327 |
|
| 328 |
/** |
| 329 |
* VODPlayer |
| 330 |
* |
| 331 |
* @author cretu |
| 332 |
*/ |
| 333 |
|
| 334 |
public function wpstream_video_on_demand_player($product_id){ |
| 335 |
$thumb_id = get_post_thumbnail_id($product_id); |
| 336 |
$thumb = wp_get_attachment_image_src($thumb_id,'small'); |
| 337 |
$wpstream_data_setup = ' data-setup="{}" '; |
| 338 |
|
| 339 |
/* free_video_type |
| 340 |
* 1 - free live channel |
| 341 |
* 2 - free video encrypted |
| 342 |
* 3 - free video -not encrypted |
| 343 |
*/ |
| 344 |
$free_video_type = intval( get_post_meta($product_id, 'wpstream_product_type', true)); |
| 345 |
|
| 346 |
|
| 347 |
if($free_video_type==2 || get_post_type($product_id)=='product' ){ |
| 348 |
|
| 349 |
/* IF vide is encrypted- readed from vod,streaner |
| 350 |
*/ |
| 351 |
|
| 352 |
$video_type = 'application/x-mpegURL'; |
| 353 |
$video_path = get_post_meta($product_id,'_movie_url',true); |
| 354 |
if(get_post_type($product_id)=='wpstream_product'){ |
| 355 |
$video_path = esc_html(get_post_meta($product_id, 'wpstream_free_video', true)); |
| 356 |
} |
| 357 |
|
| 358 |
$username = esc_html ( get_option('wpstream_api_username','') ); |
| 359 |
if (filter_var($username, FILTER_VALIDATE_EMAIL)) { |
| 360 |
$username = $this->wpstream_retrive_username(); |
| 361 |
} |
| 362 |
|
| 363 |
if(strpos($username,'@')!=false){ |
| 364 |
$username_array= explode('@', $username); |
| 365 |
$username=$username_array[0]; |
| 366 |
} |
| 367 |
|
| 368 |
$video_path_final = 'https://vod.streamer.wpstream.net/'.$username.'/'.$video_path.'/index.m3u8?'.get_site_url(); |
| 369 |
if(!is_ssl()){ |
| 370 |
$video_path_final = 'http://vod.streamer.wpstream.net/'.$username.'/'.$video_path.'/index.m3u8?'.get_site_url(); |
| 371 |
} |
| 372 |
|
| 373 |
}else if($free_video_type==3){ |
| 374 |
|
| 375 |
/* Video is unecrypted - read from local or youtube / vimeo |
| 376 |
*/ |
| 377 |
|
| 378 |
$video_type = 'video/mp4'; |
| 379 |
$video_path_final=esc_html(get_post_meta($product_id, 'wpstream_free_video_external', true)); |
| 380 |
|
| 381 |
if (strpos($video_path_final, 'www.youtube') !== false) { |
| 382 |
$wpstream_data_setup= ' data-setup=\'{ "techOrder": ["youtube"], "sources": [{ "type": "video/youtube", "src": "'.$video_path_final.'"}] }\' '; |
| 383 |
$video_path_final=''; |
| 384 |
} |
| 385 |
if (strpos($video_path_final, 'vimeo.com') !== false) { |
| 386 |
$wpstream_data_setup= ' data-setup=\'{"techOrder": ["vimeo"], "sources": [{ "type": "video/vimeo", "src": "'.$video_path_final.'"}], "vimeo": { "color": "#fbc51b"} }\' '; |
| 387 |
$video_path_final=''; |
| 388 |
} |
| 389 |
|
| 390 |
} |
| 391 |
|
| 392 |
echo '<video id="wpstream-video'.time().'" class="video-js vjs-default-skin vjs-16-9 kuk wpstream_video_on_demand" controls preload="auto" |
| 393 |
poster="'.$thumb[0].'" '.$wpstream_data_setup.'> |
| 394 |
|
| 395 |
<source src="'.trim($video_path_final).'" type="'.$video_type.'"> |
| 396 |
<p class="vjs-no-js"> |
| 397 |
To view this video please enable JavaScript, and consider upgrading to a web browser that |
| 398 |
<a href="http://videojs.com/html5-video-support/" target="_blank">supports HTML5 video</a> |
| 399 |
</p> |
| 400 |
</video>'; |
| 401 |
|
| 402 |
} |
| 403 |
|
| 404 |
|
| 405 |
|
| 406 |
/** |
| 407 |
* Retreive username for vod path |
| 408 |
* |
| 409 |
* @author cretu |
| 410 |
*/ |
| 411 |
private function wpstream_retrive_username(){ |
| 412 |
$token = $this->main->wpstream_live_connection->wpstream_get_token(); |
| 413 |
|
| 414 |
$url=WPSTREAM_CLUBLINKSSL."://www.".WPSTREAM_CLUBLINK."/wp-json/rcapi/v1/return_login/?access_token=".$token; |
| 415 |
$arguments = array( |
| 416 |
'method' => 'GET', |
| 417 |
'timeout' => 45, |
| 418 |
'redirection' => 5, |
| 419 |
'httpversion' => '1.0', |
| 420 |
'blocking' => true, |
| 421 |
'headers' => array(), |
| 422 |
'cookies' => array() |
| 423 |
); |
| 424 |
$response = wp_remote_post($url,$arguments); |
| 425 |
$received_data = json_decode( wp_remote_retrieve_body($response) ,true); |
| 426 |
if( isset($response['response']['code']) && $response['response']['code']=='200'){ |
| 427 |
return ($received_data); die(); |
| 428 |
}else{ |
| 429 |
print 'Failed to conbect media server'; |
| 430 |
die(); |
| 431 |
} |
| 432 |
} |
| 433 |
|
| 434 |
|
| 435 |
|
| 436 |
|
| 437 |
|
| 438 |
/** |
| 439 |
* check if the user bought the product and display the player - TO REDo |
| 440 |
* |
| 441 |
* @since 3.0.1 |
| 442 |
* returns html of the player |
| 443 |
*/ |
| 444 |
public function wpstream_user_logged_in_product_already_bought($from_sh_id='') { |
| 445 |
|
| 446 |
if ( is_user_logged_in() ) { |
| 447 |
global $product; |
| 448 |
$current_user = wp_get_current_user(); |
| 449 |
$product_id = $product->get_id(); |
| 450 |
|
| 451 |
|
| 452 |
if ( wc_customer_bought_product( $current_user->user_email, $current_user->ID, $product_id) || ( function_exists('wcs_user_has_subscription') && wcs_user_has_subscription( $current_user->ID, $product_id ,'active') ) ){ |
| 453 |
|
| 454 |
|
| 455 |
echo '<div class="wpstream_player_wrapper"><div class="wpstream_player_container">'; |
| 456 |
|
| 457 |
$is_subscription_live_event = esc_html(get_post_meta($product_id,'_subscript_live_event',true)); |
| 458 |
$term_list = wp_get_post_terms($product_id, 'product_type'); |
| 459 |
|
| 460 |
|
| 461 |
if( $term_list[0]->name=='live_stream' || ($term_list[0]->name=='subscription' && $is_subscription_live_event=='yes' ) ){ |
| 462 |
$this->wpstream_live_event_player($product_id); |
| 463 |
}else if( $term_list[0]->name=='video_on_demand' || ($term_list[0]->name=='subscription' && $is_subscription_live_event=='no' ) ){ |
| 464 |
$this->wpstream_video_on_demand_player($product_id); |
| 465 |
} |
| 466 |
|
| 467 |
}else{ |
| 468 |
echo '<div class="wpstream_player_wrapper no_buy"><div class="wpstream_player_container">'; |
| 469 |
echo '<div class="wpstream_notice">'.__('You did not buy this product!','wpstream').'</div>'; |
| 470 |
} |
| 471 |
|
| 472 |
echo '</div></div>'; |
| 473 |
} |
| 474 |
} |
| 475 |
|
| 476 |
} |
| 477 |
|