PluginProbe
WpStream – Live Streaming, Video on Demand, Pay Per View / 4.5.11.4
WpStream – Live Streaming, Video on Demand, Pay Per View v4.5.11.4
4.14.1 4.14.0 4.13.2 4.13.1 4.13 4.12.5 4.12.4 4.12.3 4.12.2 4.12.1 4.12 4.4.4 4.4.5 4.4.6 4.4.7 4.4.8 4.4.9 4.5 4.5.1 4.5.11 4.5.11.1 4.5.11.2 4.5.11.4 4.5.11.5 4.5.11.6 All 181 releases
wpstream / includes / class-wpstream-player.php

class-wpstream-player.php in WpStream – Live Streaming, Video on Demand, Pay Per View 4.5.11.4, at includes/class-wpstream-player.php

1,331 lines 55.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 add_action( 'wp_ajax_wpstream_player_check_status', array($this,'wpstream_player_check_status') );
23 add_action('wp_ajax_nopriv_wpstream_player_check_status', array($this,'wpstream_player_check_status'));
24
25
26
27
28
29 }
30
31
32
33
34
35
36 /**
37 *
38 * edited 4.0
39 *
40 * check player status
41 *
42 * @author cretu
43 */
44
45 public function wpstream_player_check_status(){
46 $channel_id = intval($_POST['channel_id']);
47
48
49 $transient_name = 'event_data_to_return_'. $channel_id;
50 $event_data_for_transient = get_transient( $transient_name );
51
52
53
54 if ( false === $event_data_for_transient || $event_data_for_transient=='' ) { //ws || $hls_to_return==''
55 $notes = 'wpstream_player_check_status_note_from_js';
56 $event_status = $this->main->wpstream_live_connection-> wpstream_check_event_status_api_call($channel_id,$notes);
57 $event_data_for_transient = $event_status;
58
59 set_transient($transient_name,$event_data_for_transient,45);
60 }
61
62
63 if( isset($event_data_for_transient['hls_playback_url']) && $event_data_for_transient['hls_playback_url']!=''){
64 echo json_encode( array(
65
66 'started' => 'yes',
67 'channel_id' => $channel_id,
68 'event_uri' => $event_data_for_transient['hls_playback_url'],
69 'live_conect_views' => $event_data_for_transient['stats_url'],
70 'chat_url' => $event_data_for_transient['chat_url'],
71 '$event_data_for_transient'=>$event_data_for_transient
72
73
74 ));
75 update_post_meta($channel_id,'stream_name',$event_data_for_transient['stream_name']);
76 update_post_meta($channel_id,'hls_key_retrieval_url',$event_data_for_transient['hls_key_retrieval_url']);
77 delete_transient( 'free_event_streamName_'.$event_data_for_transient['stream_name']);
78
79 }else{
80 echo json_encode( array(
81 'started' => 'no',
82 'server_id' => '',
83 'channel_id' => $channel_id,
84 'event_uri' => '',
85 'live_conect_views' => '',
86 'chat_url' => '',
87
88 ));
89
90 }
91
92 die();
93 }
94
95
96
97 /**
98 * Insert player in page
99 *
100 * @author cretu
101 */
102 public function wpstream_filter_the_title( $content ) {
103 if(function_exists('remove_wpstream_filter')){
104 return $content;
105 }
106
107 if( is_singular('wpstream_product') || is_singular('wpstream_product_vod') ){
108 global $post;
109 $args=array('id'=>$post->ID);
110 $custom_content = $this->wpstream_insert_player_inpage($args);
111 $content = '<div class="wpestream_inserted_player">'.$custom_content.'</div>'.$content;
112 return $content;
113 }else{
114 return $content;
115 }
116 }
117
118 /**
119 * Insert player in page
120 *
121 * @author cretu
122 */
123
124 public function wpstream_insert_player_inpage($attributes, $content = null){
125 $product_id = '';
126 $return_string = '';
127 $attributes = shortcode_atts(
128 array(
129 'id' => 0,
130 ), $attributes) ;
131
132
133 if ( isset($attributes['id']) ){
134 $product_id=$attributes['id'];
135 }
136
137 if(intval($product_id)==0){
138 $product_id= $this->wpstream_player_retrive_first_id();
139 }
140
141 ob_start();
142
143 $this->wpstream_video_player_shortcode($product_id);
144 $return_string= ob_get_contents();
145 ob_end_clean();
146
147 return $return_string;
148 }
149
150
151
152
153 /**
154 * Video Player shortcode
155 *
156 * @author cretu
157 */
158
159 public function wpstream_video_player_shortcode($from_sh_id='') {
160
161
162
163 if ( is_user_logged_in() ) {
164 global $product;
165 $current_user = wp_get_current_user();
166 $product_id = intval($from_sh_id);
167 $term_list = wp_get_post_terms($product_id, 'product_type');
168 $possible_bundle = get_post_meta($product_id, 'wpstream_part_of_bundle', true);
169
170
171
172
173 if (
174
175 ( 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) ) ||
176 ( in_array( 'woocommerce/woocommerce.php', apply_filters( 'active_plugins', get_option( 'active_plugins' ) ) ) && (intval($possible_bundle)!=0) && wc_customer_bought_product( $current_user->user_email, $current_user->ID, $possible_bundle) ) ||
177 get_post_type($product_id)=='wpstream_product' ||
178 get_post_type($product_id)=='wpstream_product_vod' ){
179 global $product;
180 echo '<div class="wpstream_player_wrapper wpstream_player_shortcode"><div class="wpstream_player_container">';
181
182
183 if( get_post_type($product_id) == 'wpstream_product' ){
184 $this->wpstream_live_event_player($product_id);
185 }else if( get_post_type($product_id) == 'wpstream_product_vod' ){
186 $this->wpstream_video_on_demand_player($product_id);
187 }else{
188
189 $is_subscription_live_event = esc_html(get_post_meta($product_id,'_subscript_live_event',true));
190
191 if( $term_list[0]->name=='live_stream' || ( $term_list[0]->name=='subscription' && $is_subscription_live_event=='yes' ) ){
192 $this->wpstream_live_event_player($product_id);
193 }else if( $term_list[0]->name=='video_on_demand' || ($term_list[0]->name=='subscription' && $is_subscription_live_event=='no' ) ){
194 $this->wpstream_video_on_demand_player($product_id);
195 }
196 }
197
198
199 echo '</div></div>';
200 }else{
201
202 if( get_post_type($product_id) == 'product' ){
203 echo '<div class="wpstream_player_wrapper wpstream_player_shortcode no_buy"><div class="wpstream_player_container">';
204 $message =esc_html( get_option('wpstream_product_not_bought','You did not yet purchase this item.')) ;
205 echo '<div class="wpstream_notice" style="background:#e16767;">'.esc_html($message).'</div>';
206 echo '</div></div>';
207 }
208 }
209
210
211 }else{
212
213 $product_id = intval($from_sh_id);
214 $term_list = wp_get_post_terms($product_id, 'product_type');
215
216 if( get_post_type($product_id) == 'product' && ($term_list[0]->name=='live_stream' || $term_list[0]->name=='video_on_demand') ){
217
218 echo '<div class="wpstream_player_wrapper wpstream_player_shortcode no_buy"><div class="wpstream_player_container">';
219 $message= esc_html( get_option('wpstream_product_not_login','You must be logged in to watch this video.')) ;
220
221
222 echo '<div class="wpstream_notice" style="background:#e16767;">'.esc_html($message).'</div>';
223 echo '</div></div>';
224 }elseif( get_post_type($product_id) == 'wpstream_product' ){
225 $this->wpstream_live_event_player($product_id);
226 } else if( get_post_type($product_id) == 'wpstream_product_vod' ){
227 $this->wpstream_video_on_demand_player($product_id);
228 }
229 }
230 }
231
232
233
234 /**
235 * Video Player shortcode - low latency
236 *
237 * @author cretu
238 */
239
240 public function wpstream_video_player_shortcode_low_latency($from_sh_id='') {
241
242 if ( is_user_logged_in() ) {
243 global $product;
244 $current_user = wp_get_current_user();
245 $product_id = intval($from_sh_id);
246
247
248 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' ){
249 global $product;
250 echo '<div class="wpstream_player_wrapper wpstream_player_shortcode"><div class="wpstream_player_container">';
251
252
253 if( get_post_type($product_id) == 'wpstream_product' ){
254 $this->wpstream_live_event_player_low_latency($product_id);
255 }else{
256 $term_list = wp_get_post_terms($product_id, 'product_type');
257 $is_subscription_live_event = esc_html(get_post_meta($product_id,'_subscript_live_event',true));
258
259 if( $term_list[0]->name=='live_stream' || ( $term_list[0]->name=='subscription' && $is_subscription_live_event=='yes' ) ){
260 $this->wpstream_live_event_player_low_latency($product_id);
261 }
262 }
263
264
265 echo '</div></div>';
266 }else{
267
268 if( get_post_type($product_id) == 'product' ){
269 echo '<div class="wpstream_player_wrapper wpstream_player_shortcode no_buy"><div class="wpstream_player_container">';
270
271 $message =esc_html( get_option('wpstream_product_not_bought','You did not yet purchase this item.')) ;
272 echo '<div class="wpstream_notice" style="background:#e16767;">'.$message.'</div>';
273 echo '</div></div>';
274 }
275 }
276
277
278 }else{
279 $product_id = intval($from_sh_id);
280 if( get_post_type($product_id) == 'wpstream_product' ){
281 $this->wpstream_live_event_player_low_latency($product_id);
282 }
283 }
284 }
285
286
287
288
289
290 /**
291 * Live Event Player
292 *
293 * @author cretu
294 */
295 function remove_http($url) {
296 $disallowed = array('http://', 'https://');
297 foreach($disallowed as $d) {
298 if(strpos($url, $d) === 0) {
299 return str_replace($d, '', $url);
300 }
301 }
302 return $url;
303 }
304
305 /**
306 * Get event settings
307 *
308 * @author cretu
309 */
310
311 function wpestream_return_event_settings($product_id){
312
313 $local_event_options = get_post_meta($product_id,'local_event_options',true);
314 if(!is_array($local_event_options)){
315 $local_event_options = get_option('wpstream_user_streaming_global_channel_options') ;
316 }
317
318 return $local_event_options;
319 }
320
321
322
323 /**
324 * edited in 4.0
325 *
326 * Live Event Player
327 *
328 * @author cretu
329 */
330
331 function wpstream_live_event_player($channel_id,$poster_show='',$use_chat=''){
332
333 $now = time().rand(0,1000000);
334 $thumb_id = get_post_thumbnail_id($channel_id);
335 $thumb = wp_get_attachment_image_src($thumb_id,'small');
336 $usernamestream = esc_html ( get_option('wpstream_api_username','') );
337 $autoplay = true;
338
339 $event_settings = $this->wpestream_return_event_settings($channel_id);
340 $notes = 'wpstream_live_event_player_note';
341 $event_status = $this->main->wpstream_live_connection-> wpstream_check_event_status_api_call($channel_id,$notes);
342 $hls_playback_url = '';
343 $live_conect_views = '';
344
345 if(isset($event_status['status']) && $event_status['status']=='active'){
346 //live event
347 if(isset($event_status['hls_playback_url'])){
348 $hls_playback_url = $event_status['hls_playback_url'];
349
350 update_post_meta($channel_id,'stream_name',$event_status['stream_name']);
351 update_post_meta($channel_id,'hls_key_retrieval_url',$event_status['hls_key_retrieval_url']);
352 delete_transient( 'free_event_streamName_'.$event_status['stream_name']);
353
354 $live_conect_array = explode('live.streamer.wpstream.net',$hls_playback_url);
355 $live_conect_views = $live_conect_array[0].'live.streamer.wpstream.net';
356 $live_conect_views = $this->remove_http($live_conect_views);
357
358 }
359 if(isset($event_status['chat_url'])){
360 $chat_url = $event_status['chat_url'];
361 }
362
363 }else{
364 // event not live
365 }
366
367 if(isset($event_settings['autoplay']) && intval($event_settings['autoplay'])==0){
368 $autoplay=false;
369 }
370
371
372
373 echo '<div class="wpstream_live_player_wrapper function_wpstream_live_event_player" data-now="'.$now.'" data-me="'.esc_attr($usernamestream).'" data-product-id="'.$channel_id.'" id="wpstream_live_player_wrapper'.$now.'" > ';
374
375 if( ( isset($event_settings['view_count'] ) && intval($event_settings['view_count'])==1 ) || !isset($event_settings['view_count']) ){
376 echo '<div id="wpestream_live_counting" class="wpestream_live_counting"></div>';
377 }
378
379 $show_wpstream_not_live_mess=' style="display:none;" ';
380 if(trim($hls_playback_url) ==''){
381
382 $show_wpstream_not_live_mess='';
383 }
384
385 $message_show= esc_html( get_option('wpstream_you_are_not_live','We are not live at this moment')) ;
386
387 if(function_exists('wpstream_theme_not_live_section')){
388 print wpstream_theme_not_live_section($channel_id);
389 }else{
390 print '<div class="wpstream_not_live_mess" '.$show_wpstream_not_live_mess.'><div class="wpstream_not_live_mess_back"></div><div class="wpstream_not_live_mess_mess">'.esc_html($message_show).'</div></div>';
391 }
392
393
394 $poster_data='';
395 if(isset($thumb[0])){
396 $poster_data=' poster="'.$thumb[0].'" ';
397 }
398 if($poster_show=='no'){
399 $poster_data='';
400 }
401
402 $is_muted=false;
403 if( isset($event_settings['mute']) && intval($event_settings['mute'])==1){
404 $is_muted=true;
405 }
406 // override $is_muted and $autoplay here - for testing
407 // $autoplay = true;
408 // $is_muted = false;
409
410 $autoplay_str = $autoplay ? 'autoplay' : '';
411 $is_muted_str = $is_muted ? 'muted' : '';
412
413 $video_trailer = '';
414 $trailer_attachment_id = intval (get_post_meta( $channel_id, 'video_trailer', true ));
415 $video_trailer = '';
416 $video_trailer_type = '';
417 if($trailer_attachment_id!=0) {
418 $video_trailer = wp_get_attachment_url( $trailer_attachment_id );
419 $attachment_metadata = wp_get_attachment_metadata($trailer_attachment_id);
420 $video_trailer_type = $attachment_metadata['mime_type'];
421 }
422
423 // override trailer url here - for testing
424 // $video_trailer = '';
425 // $video_trailer = '/wp-content/uploads/2023/10/production-ID_4608975.mp4';
426 // $video_trailer = '/wp-content/uploads/2023/10/ultrawide.mp4';
427
428
429 echo'
430 <video id="wpstream-video'.$now.'" '.$poster_data.' class="video-js vjs-default-skin vjs-fluid vjs-wpstream" playsinline="true" '.$is_muted_str." ".$autoplay_str.'>
431
432 </video>';
433 if ($video_trailer){
434 print '<div id="wpstream_live_video_play_trailer_btn_' . $now . '" style="display: none;" class="wpstream_video_on_demand_play_trailer">'.esc_html('Play Trailer').'</div>';
435 print '<div id="wpstream_live_video_mute_trailer_btn_' . $now . '" style="display: none;" class="wpstream_video_on_demand_mute_trailer">'.esc_html('mute trailer').'</div>';
436 print '<div id="wpstream_live_video_unmute_trailer_btn_' . $now . '" style="display: none;" class="wpstream_video_on_demand_unmute_trailer">'.esc_html('unmute trailer').'</div>';
437 }
438 print '<script type="text/javascript">
439 //<![CDATA[
440 jQuery(document).ready(function(){
441 wpstream_player_initialize({
442 videoElementId: "'.$now.'",
443 trailerUrl: "'.$video_trailer.'",
444 contentUrl: "'.$hls_playback_url.'",
445 statsUri: "'.$live_conect_views.'",
446 autoplay: '.var_export($autoplay, true).',
447 muted: '.var_export($is_muted, true).',
448 playTrailerButtonElementId: "wpstream_live_video_play_trailer_btn_'.$now.'",
449 muteTrailerButtonElementId: "wpstream_live_video_mute_trailer_btn_'.$now.'",
450 unmuteTrailerButtonElementId: "wpstream_live_video_unmute_trailer_btn_'.$now.'",
451 });
452 });
453 //]]>
454 </script>';
455 print '</div>';
456
457
458 if(trim($hls_playback_url) ==''){
459 // $show_wpstream_not_live_mess='';
460 }else{
461 print '<script type="text/javascript">
462 //<![CDATA[
463 jQuery(document).ready(function(){
464 var player_wrapper = jQuery(".wpstream_live_player_wrapper");
465 wpstream_read_websocket_info("'.$channel_id.'","wpstream_live_player_wrapper'.$now.'", player_wrapper ,"'.$chat_url.'", "'.$hls_playback_url.'");
466 });
467 //]]>
468 </script>';
469 }
470
471
472 if($use_chat=="yes"){
473 $this->wpstream_connect_to_chat($channel_id);
474 }
475
476 usleep (10000);
477
478 }
479
480
481
482
483
484 /**
485 *
486 * Edited in 4.0
487 *
488 *
489 * Live Event Player
490 *
491 * @author cretu
492 */
493
494 function wpstream_live_event_player_low_latency($channel_id,$poster_show='',$use_chat=''){
495 $usernamestream = esc_html ( get_option('wpstream_api_username','') );
496 $thumb_id = get_post_thumbnail_id($channel_id);
497 $thumb = wp_get_attachment_image_src($thumb_id,'small');
498
499 $event_settings = $this->wpestream_return_event_settings($channel_id);
500 $notes = 'wpstream_live_event_player_low_latency_note';
501 $event_status = $this->main->wpstream_live_connection-> wpstream_check_event_status_api_call($channel_id,$notes);
502 $hls_playback_url = '';
503 $live_conect_views = '';
504 $now = time().rand(0,10);
505
506
507
508 if(isset($event_status['status']) && $event_status['status']=='active'){
509 //live event
510 if(isset($event_status['sldp_playback_url'])){
511 $hls_playback_url = $event_status['sldp_playback_url'];
512
513 $live_conect_array = explode('live.streamer.wpstream.net',$hls_playback_url);
514 $live_conect_views = $live_conect_array[0].'live.streamer.wpstream.net';
515 $live_conect_views = $this->remove_http($live_conect_views);
516
517 }
518 if(isset($event_status['chat_url'])){
519 $chat_url =$event_status['chat_url'];
520 }
521
522 }else{
523 // event not live
524 }
525
526
527
528
529 echo '<div class="wpstream_live_player_wrapper function_wpstream_live_event_player_low_latency wpstream_low_latency" data-now="'.$now.'" data-me="'.esc_attr($usernamestream).'" data-product-id="'.$channel_id.'" id="wpstream_live_player_wrapper'.$now.'" > ';
530
531
532 if( ( isset($event_settings['view_count'] ) && intval($event_settings['view_count'])==1 ) || !isset($event_settings['view_count']) ){
533 echo '<div id="wpestream_live_counting" class="wpestream_live_counting"></div>';
534 }
535
536 $show_wpstream_not_live_mess=' style="display:none;" ';
537 if(trim($hls_playback_url) ==''){
538 $show_wpstream_not_live_mess='';
539 }
540
541 $message_show= esc_html( get_option('wpstream_you_are_not_live','We are not live at this moment')) ;
542 print '<div class="wpstream_not_live_mess " '.$show_wpstream_not_live_mess.' ><div class="wpstream_not_live_mess_back"></div><div class="wpstream_not_live_mess_mess">'. $message_show.'</div></div>';
543
544
545 $poster_data=' poster="'.$thumb[0].'" ';
546 if($poster_show=='no'){
547 $poster_data='';
548 }
549
550
551 $is_muted='';
552 if( isset($event_settings['mute']) && intval($event_settings['mute'])==1){
553 $is_muted=' muted ';
554 }
555
556
557 $autoplay='autoplay';
558 if(isset($event_settings['autoplay']) && intval($event_settings['autoplay'])==0){
559 $autoplay='';
560 }
561
562 echo'
563 <div iccd="player" id="wpstream-video'.$now.'" '.$poster_data.' '.$is_muted.' class="" >
564 </div>';
565
566 print '<script type="text/javascript">
567 //<![CDATA[
568 jQuery(document).ready(function(){
569 var low_latencyid="wpstream-video'.$now.'";
570 document.addEventListener("DOMContentLoaded", initPlayer(low_latencyid, "'.$hls_playback_url.'","'.$is_muted.'","'.$autoplay.'" ) ); ';
571 print'});
572 //]]>
573 </script>';
574
575
576
577 if(trim($hls_playback_url) ==''){
578 // $show_wpstream_not_live_mess='';
579 }else{
580 print '<script type="text/javascript">
581 //<![CDATA[
582 jQuery(document).ready(function(){
583 var player_wrapper = jQuery(".wpstream_live_player_wrapper");
584 wpstream_read_websocket_info("'.$channel_id.'","wpstream_live_player_wrapper'.$now.'", player_wrapper ,"'.$chat_url.'", "'.$hls_playback_url.'");
585 });
586 //]]>
587 </script>';
588 }
589
590
591 if($use_chat=="yes"){
592 $this->wpstream_connect_to_chat($channel_id);
593 }
594
595 usleep (10000);
596
597 }
598
599
600
601
602
603 /*
604 *
605 * Request HLS player
606 *
607 *
608 *
609 */
610 public function wpstream_request_video_on_demand_hls_player($video_name,$product_id){
611 if($video_name==''){
612 return '';
613 }
614
615 $transient_name = 'wpstream_video_on_demand_'.$video_name;
616 $hls_to_return = get_transient( $transient_name );
617 $hls_to_return = false;
618
619
620 if($hls_to_return==false){
621
622 $access_token = $this->main->wpstream_live_connection->wpstream_get_token();
623 $url = 'video/info';
624
625 //corsorigin de check
626 $local_event_options = get_option('wpstream_user_streaming_global_channel_options') ;
627 $domain = parse_url ( get_site_url() );
628 $domain_scheme = 'http';
629 if(is_ssl()){
630 $domain_scheme='https';
631 }
632
633 $corsorigin='';
634 if( isset($local_event_options['vod_domain_lock']) ){
635 if (intval( $local_event_options['vod_domain_lock']) ==0 ){
636 $corsorigin='*';
637 }
638 }else{
639 $corsorigin='*';
640 }
641
642 if($corsorigin!='*'){
643 $corsorigin=$domain_scheme.'://'.$domain['host'];
644 }
645
646 $is_encrypt="false";
647 if( isset($local_event_options['vod_encrypt']) && intval( $local_event_options['vod_encrypt']) ==1 ){
648 $is_encrypt="true";
649 }
650
651 $hlsKeysUrlPrefix = get_site_url().'?wpstream_voddrm=';
652 $encrypt = $is_encrypt;
653 $debugDrm = false;
654
655 $curl_post_fields=array(
656 'access_token' => $access_token,
657 'name' => $video_name,
658 'corsOrigin' => $corsorigin,
659 'encryptHls' => $encrypt,
660 'hlsKeysUrlPrefix' => $hlsKeysUrlPrefix,
661 'debugDrm' => $debugDrm,
662 );
663
664
665 $curl_response = $this->main->wpstream_live_connection->wpstream_baker_do_curl_base($url,$curl_post_fields);
666 $curl_response_decoded = json_decode($curl_response,JSON_OBJECT_AS_ARRAY);
667
668 if($curl_response_decoded['success']){
669 set_transient( $transient_name, $curl_response_decoded['hlsUrl'] ,300);
670 $hls_to_return = $curl_response_decoded['hlsUrl'];
671 if( isset($curl_response_decoded['hlsDecryptionKey']) && isset($curl_response_decoded['hlsDecryptionKeyIndex']) ){
672 update_post_meta($product_id,'hlsDecryptionKey',$curl_response_decoded['hlsDecryptionKey']);
673 update_post_meta($product_id,'hlsDecryptionKeyIndex',$curl_response_decoded['hlsDecryptionKeyIndex']);
674 }else{
675 delete_post_meta($product_id,'hlsDecryptionKey');
676 delete_post_meta($product_id,'hlsDecryptionKeyIndex');
677 }
678
679 }else{
680 return '';
681 }
682
683 }
684
685 return $hls_to_return;
686
687 }
688
689
690
691
692 /**
693 * VODPlayer uri details
694 *
695 * @author cretu
696 */
697 public function wpstream_video_on_demand_player_uri_request($product_id){
698
699 $wpstream_data_setup = ' data-setup="{}" ';
700
701 /* free_video_type
702 * 1 - free live channel
703 * 2 - free video encrypted
704 * 3 - free video -not encrypted
705 */
706
707 $post_type = get_post_type($product_id);
708 $free_video_type = intval( get_post_meta($product_id, 'wpstream_product_type', true));
709 if( ( $post_type =='wpstream_product_vod' && $free_video_type==2 ) || get_post_type($product_id)=='product' ){
710
711 /*
712 * IF vide is encrypted- readed from vod,streaner
713 *
714 */
715
716
717 $video_type = 'application/x-mpegURL';
718 $video_path = get_post_meta($product_id,'_movie_url',true);
719 if(get_post_type($product_id)=='wpstream_product_vod'){
720 $video_path = esc_html(get_post_meta($product_id, 'wpstream_free_video', true));
721 }
722 $video_path_final = $this->wpstream_request_video_on_demand_hls_player($video_path,$product_id);
723
724
725 }else if( $post_type =='wpstream_product_vod' && $free_video_type==3 ){
726
727 /* Video is unecrypted - read from local or youtube / vimeo
728 */
729
730 $video_type = 'video/mp4';
731 $video_path_final=esc_html(get_post_meta($product_id, 'wpstream_free_video_external', true));
732
733 if (strpos($video_path_final, 'www.youtube') !== false) {
734 $wpstream_data_setup= ' data-setup=\'{ "techOrder": ["youtube"], "sources": [{ "type": "video/youtube", "src": "'.$video_path_final.'"}] }\' ';
735 $video_path_final='';
736 }
737 /* disabled due to old lib
738 if (strpos($video_path_final, 'vimeo.com') !== false) {
739 $wpstream_data_setup= ' data-setup=\'{"techOrder": ["vimeo"], "sources": [{ "type": "video/vimeo", "src": "'.$video_path_final.'"}], "vimeo": { "color": "#fbc51b"} }\' ';
740 $video_path_final='';
741 }*/
742
743 }
744
745 $return_array = array();
746 $return_array['video_path_final'] = $video_path_final;
747 $return_array['wpstream_data_setup']= $wpstream_data_setup;
748 $return_array['video_type'] = $video_type;
749 $return_array['free_video_type'] = $free_video_type;
750 $return_array['post_type'] = $post_type ;
751 return $return_array;
752 }
753
754
755
756 /**
757 * VODPlayer url
758 *
759 * @author cretu
760 */
761
762 public function wpstream_video_on_demand_player($product_id){
763
764 $uri_details = $this->wpstream_video_on_demand_player_uri_request($product_id);
765 $video_path_final = $uri_details['video_path_final'];
766 $wpstream_data_setup = $uri_details['wpstream_data_setup'];
767 $video_type = $uri_details['video_type'];
768 $now = time().rand(0,1000000);
769
770 $thumb_id = get_post_thumbnail_id($product_id);
771 $thumb = wp_get_attachment_image_src($thumb_id,'small');
772 $usernamestream = esc_html ( get_option('wpstream_api_username','') );
773
774 $poster_thumb = '';
775 if(isset($thumb[0])){
776 $poster_thumb=$thumb[0];
777 }
778
779 $hlsDecryptionKey = get_post_meta($product_id,'hlsDecryptionKey',true);
780 $hlsDecryptionKeyIndex = get_post_meta($product_id,'hlsDecryptionKeyIndex',true);
781
782
783 $pack = $this->main->wpstream_live_connection->wpstream_request_pack_data_per_user();
784
785
786
787 $trailer_attachment_id = intval (get_post_meta( $product_id, 'video_trailer', true ));
788 $video_trailer = '';
789 $video_trailer_type = '';
790 if($trailer_attachment_id!=0) {
791 $video_trailer = wp_get_attachment_url( $trailer_attachment_id );
792 $attachment_metadata = wp_get_attachment_metadata($trailer_attachment_id);
793 $video_trailer_type = $attachment_metadata['mime_type'];
794 }
795
796 // override trailer setup here (for testing)
797 // $trailer_attachment_id = 1;
798 // $video_trailer = '/wp-content/uploads/2023/10/production-ID_4608975.mp4';
799 // $video_trailer = '/wp-content/uploads/2023/10/ultrawide.mp4';
800
801
802 if(isset($pack['available_data_mb']) && $pack['available_data_mb']>0){
803
804 if($video_path_final==''){
805 if( $uri_details['post_type']=='wpstream_product_vod' && $uri_details['free_video_type']==3 ){
806 }else{
807 print '<div class="wpstream_vod_notice">This video does not exist or it has been deleted!</div>';
808 }
809
810 }
811
812 // TODO (crerem) populate these from VOD settings
813 $autoplay = true;
814 $muted = false;
815
816
817 echo '<video id="wpstream-video-vod-'.$now.'" class="video-js vjs-default-skin vjs-fluid kuk wpstream_video_on_demand vjs-wpstream" data-me="'.esc_attr($usernamestream).'" data-product-id="'.$product_id.'" playsinline preload="auto"
818 poster="'.esc_url($poster_thumb).'" '.$wpstream_data_setup.'>
819 <p class="vjs-no-js">
820 To view this video please enable JavaScript, and consider upgrading to a web browser that
821 <a href="http://videojs.com/html5-video-support/" target="_blank">supports HTML5 video</a>
822 </p>
823 </video>';
824
825 if($trailer_attachment_id !=0){
826 print '<div id="wpstream_video_on_demand_play_trailer_btn_' . $now . '" class="wpstream_video_on_demand_play_trailer">'.esc_html('play trailer').'</div>';
827 print '<div id="wpstream_video_on_demand_play_video_btn_' . $now . '" class="wpstream_video_on_demand_play_video">'.esc_html('play real video').'</div>';
828 print '<div id="wpstream_video_on_demand_mute_trailer_btn_' . $now . '" class="wpstream_video_on_demand_mute_trailer">'.esc_html('mute trailer').'</div>';
829 print '<div id="wpstream_video_on_demand_unmute_trailer_btn_' . $now . '" class="wpstream_video_on_demand_unmute_trailer">'.esc_html('unmute trailer').'</div>';
830 print '<script type="text/javascript">
831 //<![CDATA[
832 jQuery(document).ready(function(){
833 wpstream_player_initialize_vod({
834 videoElementId: "wpstream-video-vod-'.$now.'",
835 trailerUrl: "'.$video_trailer.'",
836 videoUrl: "'.$video_path_final.'",
837 autoplay: '.var_export($autoplay, true).',
838 muted: '.var_export($muted, true).',
839 playTrailerButtonElementId: "wpstream_video_on_demand_play_trailer_btn_'.$now.'",
840 muteTrailerButtonElementId: "wpstream_video_on_demand_mute_trailer_btn_'.$now.'",
841 unmuteTrailerButtonElementId: "wpstream_video_on_demand_unmute_trailer_btn_'.$now.'",
842 playVideoButtonElementId: "wpstream_video_on_demand_play_video_btn_'.$now.'",
843 });
844 });
845 //]]>
846 </script>';
847 }
848 else {
849 print '<script type="text/javascript">
850 //<![CDATA[
851 jQuery(document).ready(function(){
852 wpstream_player_initialize_vod({
853 videoElementId: "wpstream-video-vod-'.$now.'",
854 videoUrl: "'.$video_path_final.'",
855 autoplay: '.var_export($autoplay, true).',
856 muted: '.var_export($muted, true).',
857 });
858 });
859 //]]>
860 </script>';
861 }
862 }else{
863 print esc_html_e('Insufficient resources to stream this title','wpstream');
864 }
865
866 }
867
868
869
870 /**
871 * VODPlayer url - only trailer - used in theme
872 *
873 * @author cretu
874 */
875 public function wpstream_video_on_demand_player_only_trailer($product_id){
876
877
878 $now = time().rand(0,1000000);
879 $thumb_id = get_post_thumbnail_id($product_id);
880 $thumb = wp_get_attachment_image_src($thumb_id,'small');
881 $usernamestream = esc_html ( get_option('wpstream_api_username','') );
882
883 $poster_thumb = '';
884 if(isset($thumb[0])){
885 $poster_thumb=$thumb[0];
886 }
887
888
889 $trailer_attachment_id = intval (get_post_meta( $product_id, 'video_trailer', true ));
890 $video_trailer = '';
891 $video_trailer_type = '';
892 if($trailer_attachment_id!=0) {
893 $video_trailer = wp_get_attachment_url( $trailer_attachment_id );
894 $attachment_metadata = wp_get_attachment_metadata($trailer_attachment_id);
895 $video_trailer_type = $attachment_metadata['mime_type'];
896 }
897
898
899 // TODO (crerem) populate these from VOD settings
900 $autoplay = false;
901 $muted = true;
902 $video_path_final='';
903
904 echo '<video id="wpstream-video-vod-'.$now.'" class="video-js vjs-default-skin vjs-fluid kuk wpstream_video_on_demand vjs-wpstream" data-me="'.esc_attr($usernamestream).'" data-product-id="'.$product_id.'" playsinline preload="auto"
905 poster="'.esc_url($poster_thumb).'">
906 <p class="vjs-no-js">
907 To view this video please enable JavaScript, and consider upgrading to a web browser that
908 <a href="http://videojs.com/html5-video-support/" target="_blank">supports HTML5 video</a>
909 </p>
910 </video>';
911
912 if($trailer_attachment_id !=0){
913 print '<div id="wpstream_video_on_demand_play_trailer_btn_' . $now . '" class="wpstream_video_on_demand_play_trailer">'.esc_html('play trailer').'</div>';
914 print '<div id="wpstream_video_on_demand_mute_trailer_btn_' . $now . '" class="wpstream_video_on_demand_mute_trailer">'.esc_html('mute trailer').'</div>';
915 print '<div id="wpstream_video_on_demand_unmute_trailer_btn_' . $now . '" class="wpstream_video_on_demand_unmute_trailer">'.esc_html('unmute trailer').'</div>';
916 print '<script type="text/javascript">
917 //<![CDATA[
918 jQuery(document).ready(function(){
919 wpstream_player_initialize_vod({
920 videoElementId: "wpstream-video-vod-'.$now.'",
921 trailerUrl: "'.$video_trailer.'",
922 autoplay: '.var_export($autoplay, true).',
923 muted: '.var_export($muted, true).',
924 playTrailerButtonElementId: "wpstream_video_on_demand_play_trailer_btn_'.$now.'",
925 muteTrailerButtonElementId: "wpstream_video_on_demand_mute_trailer_btn_'.$now.'",
926 unmuteTrailerButtonElementId: "wpstream_video_on_demand_unmute_trailer_btn_'.$now.'",
927 });
928 });
929 //]]>
930 </script>';
931 }
932 else {
933 //just show the poster or don't show anything; no player needed
934 }
935
936 }
937
938
939
940 /**
941 *
942 * edited 4.0
943 *
944 * Retreive username for vod path
945 *
946 * @author cretu
947 */
948 private function wpstream_retrive_username(){
949
950 return get_option('wpstream_api_username_from_token');
951 }
952
953 /**
954 * check if the we can add display the player
955 *
956 * @since 3.12
957 * returns html of the player
958 */
959
960 public function wpstream_check_if_player_can_dsplay($product_id){
961 if ( is_user_logged_in() ) {
962
963 $term_list = wp_get_post_terms($product_id, 'product_type');
964 $current_user = wp_get_current_user();
965 $subscription_model = intval( get_option('wpstream_global_sub','')) ;
966
967 $product = wc_get_product($product_id);
968 $product_type = $product->get_type();
969
970
971 if($subscription_model==1){ // if we have Neflix mode
972 if( $product_type=='subscription' ){ // if the product loaded is a subscription and we are on netflix mode
973 return false;
974 }
975 if($this->wpstream_in_plugin_check_global_subscription_model($product_id)){
976 return true;
977 }
978 }else{
979 // ppv mode
980
981 if( $product_type=='subscription' ){
982
983 // $user_subscriptions = wcs_get_users_subscriptions($current_user->ID );
984 // foreach ($user_subscriptions as $subscription) {
985 // $subscription_status = $subscription->get_status();
986 // echo "</br>*** Subscription ID $subscription->ID for User ID $current_user->ID has status: $subscription_status";
987 // }
988
989
990 if( wcs_user_has_subscription( $current_user->ID, $product_id ,'active') ) {
991 // user has active subcription
992 return true;
993 }
994
995 }else{
996 if( wc_customer_bought_product( $current_user->user_email, $current_user->ID, $product_id)){
997 return true;
998 }
999 }
1000
1001
1002 }
1003 return false;
1004
1005 }
1006 return false;
1007
1008 }
1009
1010
1011
1012 /**
1013 * check if the we can add display the player - theme variant
1014 *
1015 * @since 3.12
1016 * returns html of the player
1017 */
1018 public function wpstream_check_if_player_can_dsplay_theme($product_id){
1019
1020 $post_type= get_post_type($product_id);
1021
1022
1023
1024 if($post_type=='wpstream_product_vod' || $post_type=='wpstream_product'){
1025 return true; // if we have free vod or live
1026 }
1027
1028
1029
1030 if ( is_user_logged_in() && $post_type==='product' ) {
1031
1032 $product = wc_get_product( $product_id );
1033 $product_type = $product->get_type();
1034 $possible_bundle = get_post_meta($product_id, 'wpstream_part_of_bundle',true);
1035
1036
1037
1038
1039
1040 $current_user = wp_get_current_user();
1041 $subscription_model = intval( get_option('wpstream_global_sub','')) ;
1042 // print 'subscription model '.$subscription_model.'</br>';
1043
1044 if($subscription_model==1){ // if we have Neflix mode
1045 if( $product_type=='subscription' ){ // if the product loaded is a subscription and we are on netflix mode
1046 return false;
1047 }
1048 if($this->wpstream_in_plugin_check_global_subscription_model($product_id)){
1049 return true;
1050 }
1051 }else{
1052 // ppv mode
1053 if ( wc_customer_bought_product( $current_user->user_email, $current_user->ID, $product_id) ){
1054 return true; //simple product bought
1055 }else if (function_exists('wcs_user_has_subscription') && wcs_user_has_subscription( $current_user->ID, $product_id ,'active') ){
1056 return true; // subscription boght
1057 }else if( get_post_type($product_id) =='product' &&
1058 intval($possible_bundle )!=0 &&
1059 wc_customer_bought_product( $current_user->user_email, $current_user->ID, $possible_bundle)
1060 ){
1061 return true; // part of a boght bundle
1062 }
1063 }
1064 // print ' fac return false </br>';
1065 return false;
1066
1067 }
1068 return false;
1069
1070 }
1071
1072
1073 /**
1074 * in plugin - check if global subscription model is enabled
1075 *
1076 * @since 3.12
1077 *
1078 */
1079 public function wpstream_in_plugin_check_global_subscription_model($product_id) {
1080
1081 // $selected_sub= get_post_meta($post->ID,'_wpstream_parent_sub',true);
1082 if( is_user_logged_in() && function_exists('wcs_user_has_subscription') ){
1083
1084 global $woocommerce;
1085 $current_user = wp_get_current_user();
1086
1087 $subscription_model = intval( get_option('wpstream_global_sub','')) ;
1088 $main_subscription = intval( get_option('wpstream_global_sub_id',''));
1089
1090 if($subscription_model==1){
1091 $selected_sub= get_post_meta($product_id,'_wpstream_parent_sub',true) ;
1092
1093 if( is_array($selected_sub) ){
1094
1095 // we have per product sub
1096 foreach($selected_sub as $key=>$subscrition_id ):
1097 if( wcs_user_has_subscription( $current_user->ID, $subscrition_id ,'active') ) {
1098 return true;
1099 }
1100 endforeach;
1101
1102 } else if($main_subscription!=0){
1103
1104 // if we have one main subscription
1105 if( wcs_user_has_subscription( $current_user->ID, $main_subscription ,'active') ) {
1106 return true;
1107 }
1108 }
1109
1110
1111 }
1112 return false;
1113
1114 }
1115 // there is no woo subscription or user not logged in
1116 return false;
1117 }
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127 /**
1128 *
1129 *
1130 *
1131 *
1132 * check if the user bought the product and display the player - TO REDo
1133 *
1134 * @since 3.0.1
1135 * returns html of the player
1136 *
1137 *
1138 *
1139 *
1140 *
1141 */
1142 public function wpstream_user_logged_in_product_already_bought($from_sh_id='') {
1143 if(function_exists('remove_wpstream_filter')){
1144 return;
1145 }
1146 global $product;
1147 $product_id = $product->get_id();
1148 $current_user = wp_get_current_user();
1149
1150 if ( is_user_logged_in() ) {
1151
1152
1153 if($this->wpstream_check_if_player_can_dsplay($product_id) ){
1154
1155 echo '<div class="wpstream_player_wrapper "><div class="wpstream_player_container">';
1156
1157 $is_subscription_live_event = esc_html(get_post_meta($product_id,'_subscript_live_event',true));
1158 $term_list = wp_get_post_terms($product_id, 'product_type');
1159
1160
1161 if( $term_list[0]->name=='live_stream' || ($term_list[0]->name=='subscription' && $is_subscription_live_event=='yes' ) ){
1162 $this->wpstream_live_event_player($product_id);
1163 }else if( $term_list[0]->name=='video_on_demand' || ($term_list[0]->name=='subscription' && $is_subscription_live_event=='no' ) ){
1164 $this->wpstream_video_on_demand_player($product_id);
1165 }
1166 echo '</div></div>';
1167 }else{
1168
1169
1170 $term_list = wp_get_post_terms($product_id, 'product_type');
1171
1172 if( $term_list[0]->name=='subscription' ){
1173
1174 if( !wcs_user_has_subscription( $current_user->ID, $product_id ,'active') ) {
1175 $this->wpstream_display_no_buy_message('nobuy',$product_id);
1176 }
1177
1178 }else{
1179 $this->wpstream_display_no_buy_message('nobuy',$product_id);
1180 }
1181
1182
1183
1184 }
1185
1186
1187 }else{
1188
1189 $this->wpstream_display_no_buy_message('nolog',$product_id);
1190 }
1191 }
1192
1193
1194
1195
1196
1197 /**
1198 *
1199 *
1200 * check if the user bought the product and display the player - TO REDo
1201 *
1202 * @since 3.0.1
1203 * returns html of the player
1204 */
1205
1206 public function wpstream_chat_wrapper($product_id){
1207 require_once plugin_dir_path( dirname( __FILE__ ) ) . 'includes/templates/wpstream_chat_template.php';
1208 }
1209
1210
1211
1212
1213
1214
1215
1216
1217 /**
1218 * coonect to chat
1219 *
1220 * @since 1.12.2
1221 *
1222 */
1223
1224
1225
1226 public function wpstream_connect_to_chat($product_id){
1227 $current_user = wp_get_current_user();
1228 $userID = $current_user->ID;
1229 $user_login = $current_user->user_login;
1230
1231 $key='';
1232
1233 $chat_url = get_post_meta($product_id,'chat_url',true);
1234
1235
1236 wp_enqueue_script( 'sockjs-0.3.min' );
1237 wp_enqueue_script( 'emojione.min.js' );
1238 wp_enqueue_script( "jquery-effects-core");
1239 wp_enqueue_script( 'jquery.linkify.min.js');
1240 wp_enqueue_script( 'ripples.min.js');
1241 wp_enqueue_script( 'material.min.js');
1242 wp_enqueue_script( 'chat.js');
1243
1244
1245
1246 wp_enqueue_style( 'chat.css');
1247 wp_enqueue_style( 'ripples.css');
1248 wp_enqueue_style( 'emojione.min.css');
1249
1250
1251
1252
1253 if(!is_user_logged_in()){
1254 $user_login='';
1255 $chat_url='';
1256 }
1257
1258
1259 print '<script type="text/javascript">
1260 //<![CDATA[
1261 jQuery(document).ready(function(){
1262 username = "'.$user_login.'";
1263 key="'.$key.'";
1264
1265 });
1266 //]]>
1267 </script>';
1268
1269 }
1270
1271
1272 /**
1273 * display no buy Message
1274 *
1275 * @since 3.12.2
1276 *
1277 */
1278 public function wpstream_display_no_buy_message($log,$product_id) {
1279
1280 if($log=='sub_active'){
1281 $message= esc_html( get_option('wpstream_subscription_active','Your Subscription is Active.')) ;
1282 echo '<div class="wpstream_player_wrapper no_buy"><div class="wpstream_player_container">';
1283 echo '<div class="wpstream_notice"> '.$message.'</div>';
1284 echo '</div></div>';
1285 return;
1286
1287 }else if($log=='nolog'){
1288 $message= esc_html( get_option('wpstream_product_not_login','You must be logged in to watch this video.')) ;
1289 }else{
1290 $message =esc_html( get_option('wpstream_product_not_bought','You did not yet purchase this item.')) ;
1291 }
1292 $subscription_model = intval( get_option('wpstream_global_sub','')) ;
1293 if($subscription_model==1){
1294 $message =esc_html( get_option('wpstream_product_not_subscribe','You did not yet subscribe to this item.'));
1295 }
1296
1297
1298 if( get_post_type($product_id) == 'product' && $subscription_model==0 ){
1299 $product = wc_get_product($product_id);
1300 $term_list = wp_get_post_terms($product_id, 'product_type');
1301 $product_type = $product->get_type();
1302 $is_subscription_live_event = esc_html(get_post_meta($product_id,'_subscript_live_event',true));
1303
1304
1305
1306 if( $term_list[0]->name=='video_on_demand' || $term_list[0]->name=='live_stream' || $product_type=='subscription'){
1307
1308 echo '<div class="wpstream_player_wrapper no_buy"><div class="wpstream_player_container">';
1309 echo '<div class="wpstream_notice">'.$message.'</div>';
1310 echo '</div></div>';
1311
1312 }
1313
1314 }else if( get_post_type($product_id) == 'product' && $subscription_model==1 ){
1315 $term_list = wp_get_post_terms($product_id, 'product_type');
1316 if( $term_list[0]->name!=='simple'){
1317 echo '<div class="wpstream_player_wrapper no_buy"><div class="wpstream_player_container">';
1318 echo '<div class="wpstream_notice"> '.$message.'</div>';
1319 echo '</div></div>';
1320 }
1321 }
1322
1323
1324
1325
1326
1327
1328 }
1329
1330
1331 }