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

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