PluginProbe
WpStream – Live Streaming, Video on Demand, Pay Per View / 3.2.1
WpStream – Live Streaming, Video on Demand, Pay Per View v3.2.1
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 3.2.1, at includes/class-wpstream-player.php

473 lines 18.5 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
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 // $live_event_uri_final = $this->wpstream_request_hls_player($product_id);
173 $live_event_uri_final = $this->wpstream_request_hls_player_dynamic($api20_event_id);
174
175 $now = time().rand(0,10);
176 $live_conect_array = explode('live.streamer.wpstream.net',$live_event_uri_final);
177 $live_conect_views = $live_conect_array[0].'live.streamer.wpstream.net';
178 $live_conect_views = $this->remove_http($live_conect_views);
179
180 echo '<div class="wpstream_live_player_wrapper" id="wpstream_live_player_wrapper'.$now.'" > '
181 . '<div id="wpestream_live_counting" class="wpestream_live_counting"></div>';
182
183 if(trim($live_event_uri_final)==''){
184 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>';
185 }else{
186 print '<script type="text/javascript">
187 //<![CDATA[
188 jQuery(document).ready(function(){
189 wpstream_count_connect_plugin("wpstream_live_player_wrapper'.$now.'" ,"'.$live_conect_views.'");
190 });
191 //]]>
192 </script>';
193 }
194
195 echo'
196 <video id="wpstream-video'.$now.'" poster="'.$thumb[0].'" class="video-js vjs-default-skin vjs-16-9" controls>
197 <source
198 src="'.$live_event_uri_final.'"
199 type="application/x-mpegURL">
200 </video>';
201
202 print '<script type="text/javascript">
203 //<![CDATA[
204 jQuery(document).ready(function(){
205 wpstream_player_initialize("'.$now.'","'.$live_event_uri_final.'","'.$live_conect_views.'");
206 });
207 //]]>
208 </script>';
209
210 print '</div>';
211 usleep (10000);
212
213 }
214
215
216 /**
217 * HLS PLAYER
218 *
219 * @author cretu
220 */
221
222
223 public function wpstream_request_hls_player_dynamic($server_id){
224
225 $transient_name = 'hls_to_return_'.$server_id;
226 $hls_to_return = get_transient( $transient_name );
227
228 if ( false === $hls_to_return || $hls_to_return=='' ) {
229
230
231 $token = $this->main->wpstream_live_connection->wpstream_get_token();
232 $values_array = array();
233 $values_array['server_id'] = $server_id;
234
235 $url = WPSTREAM_CLUBLINKSSL."://www.".WPSTREAM_CLUBLINK."/wp-json/rcapi/v1/api20-livestrem/ap20_get_player_hls_dynamic/?access_token=".$token;
236
237
238 $arguments = array(
239 'method' => 'GET',
240 'timeout' => 45,
241 'redirection' => 5,
242 'httpversion' => '1.0',
243 'blocking' => true,
244 'headers' => array(),
245 'body' => $values_array,
246 'cookies' => array()
247 );
248 $response = wp_remote_post($url,$arguments);
249
250 $received_data = json_decode( wp_remote_retrieve_body($response) ,true);
251
252
253
254 if( isset($response['response']['code']) && $response['response']['code']=='200'){
255 $hls_to_return = trim($received_data);
256 set_transient( $transient_name, $hls_to_return, 60 );
257 return $hls_to_return = trim($received_data);
258 }else{
259 return 'failed connection';
260 }
261 exit();
262 }else{
263 return $hls_to_return;
264 }
265
266 }
267
268
269
270 /**
271 * HLS PLAYER
272 *
273 * @author cretu
274 */
275
276
277 public function wpstream_request_hls_player($product_id){
278
279 $transient_name = 'hls_to_return_'.$product_id;
280 $hls_to_return = get_transient( $transient_name );
281
282 if ( false === $hls_to_return || $hls_to_return=='' ) {
283
284 $show_id = intval($product_id);
285 $token = $this->main->wpstream_live_connection->wpstream_get_token();
286 $values_array = array();
287 $values_array['show_id'] = $show_id;
288
289 $url = WPSTREAM_CLUBLINKSSL."://www.".WPSTREAM_CLUBLINK."/wp-json/rcapi/v1/videos/ap20_get_player_hls/?access_token=".$token;
290
291
292 $arguments = array(
293 'method' => 'GET',
294 'timeout' => 45,
295 'redirection' => 5,
296 'httpversion' => '1.0',
297 'blocking' => true,
298 'headers' => array(),
299 'body' => $values_array,
300 'cookies' => array()
301 );
302 $response = wp_remote_post($url,$arguments);
303
304 $received_data = json_decode( wp_remote_retrieve_body($response) ,true);
305
306
307
308 if( isset($response['response']['code']) && $response['response']['code']=='200'){
309 $hls_to_return = trim($received_data);
310 set_transient( $transient_name, $hls_to_return, 60 );
311 return $hls_to_return = trim($received_data);
312 }else{
313 return 'failed connection';
314 }
315 exit();
316 }else{
317 return $hls_to_return;
318 }
319
320 }
321
322
323
324 /**
325 * VODPlayer
326 *
327 * @author cretu
328 */
329
330 public function wpstream_video_on_demand_player($product_id){
331 $thumb_id = get_post_thumbnail_id($product_id);
332 $thumb = wp_get_attachment_image_src($thumb_id,'small');
333 $wpstream_data_setup = ' data-setup="{}" ';
334
335 /* free_video_type
336 * 1 - free live channel
337 * 2 - free video encrypted
338 * 3 - free video -not encrypted
339 */
340 $free_video_type = intval( get_post_meta($product_id, 'wpstream_product_type', true));
341
342
343 if($free_video_type==2 || get_post_type($product_id)=='product' ){
344
345 /* IF vide is encrypted- readed from vod,streaner
346 */
347
348 $video_type = 'application/x-mpegURL';
349 $video_path = get_post_meta($product_id,'_movie_url',true);
350 if(get_post_type($product_id)=='wpstream_product'){
351 $video_path = esc_html(get_post_meta($product_id, 'wpstream_free_video', true));
352 }
353
354 $username = esc_html ( get_option('wpstream_api_username','') );
355 if (filter_var($username, FILTER_VALIDATE_EMAIL)) {
356 $username = $this->wpstream_retrive_username();
357 }
358
359 if(strpos($username,'@')!=false){
360 $username_array= explode('@', $username);
361 $username=$username_array[0];
362 }
363
364 $video_path_final = 'https://vod.streamer.wpstream.net/'.$username.'/'.$video_path.'/index.m3u8?'.get_site_url();
365 if(!is_ssl()){
366 $video_path_final = 'http://vod.streamer.wpstream.net/'.$username.'/'.$video_path.'/index.m3u8?'.get_site_url();
367 }
368
369 }else if($free_video_type==3){
370
371 /* Video is unecrypted - read from local or youtube / vimeo
372 */
373
374 $video_type = 'video/mp4';
375 $video_path_final=esc_html(get_post_meta($product_id, 'wpstream_free_video_external', true));
376
377 if (strpos($video_path_final, 'www.youtube') !== false) {
378 $wpstream_data_setup= ' data-setup=\'{ "techOrder": ["youtube"], "sources": [{ "type": "video/youtube", "src": "'.$video_path_final.'"}] }\' ';
379 $video_path_final='';
380 }
381 if (strpos($video_path_final, 'vimeo.com') !== false) {
382 $wpstream_data_setup= ' data-setup=\'{"techOrder": ["vimeo"], "sources": [{ "type": "video/vimeo", "src": "'.$video_path_final.'"}], "vimeo": { "color": "#fbc51b"} }\' ';
383 $video_path_final='';
384 }
385
386 }
387
388 echo '<video id="wpstream-video'.time().'" class="video-js vjs-default-skin vjs-16-9 kuk wpstream_video_on_demand" controls preload="auto"
389 poster="'.$thumb[0].'" '.$wpstream_data_setup.'>
390
391 <source src="'.trim($video_path_final).'" type="'.$video_type.'">
392 <p class="vjs-no-js">
393 To view this video please enable JavaScript, and consider upgrading to a web browser that
394 <a href="http://videojs.com/html5-video-support/" target="_blank">supports HTML5 video</a>
395 </p>
396 </video>';
397
398 }
399
400
401
402 /**
403 * Retreive username for vod path
404 *
405 * @author cretu
406 */
407 private function wpstream_retrive_username(){
408 $token = $this->main->wpstream_live_connection->wpstream_get_token();
409
410 $url=WPSTREAM_CLUBLINKSSL."://www.".WPSTREAM_CLUBLINK."/wp-json/rcapi/v1/return_login/?access_token=".$token;
411 $arguments = array(
412 'method' => 'GET',
413 'timeout' => 45,
414 'redirection' => 5,
415 'httpversion' => '1.0',
416 'blocking' => true,
417 'headers' => array(),
418 'cookies' => array()
419 );
420 $response = wp_remote_post($url,$arguments);
421 $received_data = json_decode( wp_remote_retrieve_body($response) ,true);
422 if( isset($response['response']['code']) && $response['response']['code']=='200'){
423 return ($received_data); die();
424 }else{
425 print 'Failed to conbect media server';
426 die();
427 }
428 }
429
430
431
432
433
434 /**
435 * check if the user bought the product and display the player - TO REDo
436 *
437 * @since 3.0.1
438 * returns html of the player
439 */
440 public function wpstream_user_logged_in_product_already_bought($from_sh_id='') {
441
442 if ( is_user_logged_in() ) {
443 global $product;
444 $current_user = wp_get_current_user();
445 $product_id = $product->get_id();
446
447
448 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') ) ){
449
450
451 echo '<div class="wpstream_player_wrapper"><div class="wpstream_player_container">';
452
453 $is_subscription_live_event = esc_html(get_post_meta($product_id,'_subscript_live_event',true));
454 $term_list = wp_get_post_terms($product_id, 'product_type');
455
456
457 if( $term_list[0]->name=='live_stream' || ($term_list[0]->name=='subscription' && $is_subscription_live_event=='yes' ) ){
458 $this->wpstream_live_event_player($product_id);
459 }else if( $term_list[0]->name=='video_on_demand' || ($term_list[0]->name=='subscription' && $is_subscription_live_event=='no' ) ){
460 $this->wpstream_video_on_demand_player($product_id);
461 }
462
463 }else{
464 echo '<div class="wpstream_player_wrapper no_buy"><div class="wpstream_player_container">';
465 echo '<div class="wpstream_notice">'.__('You did not buy this product!','wpstream').'</div>';
466 }
467
468 echo '</div></div>';
469 }
470 }
471
472 }
473