main = $plugin_main;
add_filter( 'the_content',array($this, 'wpstream_filter_the_title') );
add_action( 'woocommerce_before_single_product', array($this,'wpstream_user_logged_in_product_already_bought') );
add_action( 'wp_ajax_wpstream_player_check_status', array($this,'wpstream_player_check_status') );
add_action('wp_ajax_nopriv_wpstream_player_check_status', array($this,'wpstream_player_check_status'));
}
/**
*
* edited 4.0
*
* check player status
*
* @author cretu
*/
public function wpstream_player_check_status(){
// did not add a nonce check here because this is a call done from the frontend
// and the page might be cached, so the nonce would not be valid
$channel_id = intval($_POST['channel_id']);
$transient_name = 'event_data_to_return_'. $channel_id;
$event_data_for_transient = get_transient( $transient_name );
$usefull_info =' get cache from transiet';
if ( false === $event_data_for_transient || $event_data_for_transient=='' ) { //ws || $hls_to_return==''
$notes = 'wpstream_player_check_status_note_from_js';
$event_status = $this->main->wpstream_live_connection->wpstream_check_event_status_api_call($channel_id,$notes);
$event_data_for_transient = $event_status;
$usefull_info =' no cache found';
set_transient($transient_name,$event_data_for_transient,45);
}
if( isset($event_data_for_transient['hls_playback_url']) && $event_data_for_transient['hls_playback_url']!=''){
// cleanup any previous echo before sending json
ob_end_clean();
echo json_encode( array(
'started' => 'yes',
'usefull_info' => $usefull_info,
'channel_id' => $channel_id,
'event_uri' => $event_data_for_transient['hls_playback_url'],
'live_conect_views' => $event_data_for_transient['stats_url'],
'chat_url' => $event_data_for_transient['chat_url'],
'$event_data_for_transient'=>$event_data_for_transient
));
$usedfull_info =' ';
update_post_meta($channel_id,'stream_name',$event_data_for_transient['stream_name']);
update_post_meta($channel_id,'hls_key_retrieval_url',$event_data_for_transient['hls_key_retrieval_url']);
delete_transient( 'free_event_streamName_'.$event_data_for_transient['stream_name']);
}else{
// cleanup any previous echo before sending json
ob_end_clean();
echo json_encode( array(
'started' => 'no',
'usefull_info' => $usefull_info,
'server_id' => '',
'channel_id' => $channel_id,
'event_uri' => '',
'live_conect_views' => '',
'chat_url' => '',
));
}
die();
}
/**
* Insert player in page
*
* @author cretu
*/
public function wpstream_filter_the_title( $content ) {
if(function_exists('wpstream_remove_wpstream_filter')){
return $content;
}
if( is_singular('wpstream_product') || is_singular('wpstream_product_vod') ){
global $post;
$args=array('id'=>$post->ID);
$custom_content = $this->wpstream_insert_player_inpage($args);
$content = '
';
if( ( isset($event_settings['view_count'] ) && intval($event_settings['view_count'])==1 ) || !isset($event_settings['view_count']) ){
echo '
';
}
$show_wpstream_not_live_mess=' style="display:none;" ';
if(trim($hls_playback_url) ==''){
$show_wpstream_not_live_mess='';
}
$message_show= esc_html( get_option('wpstream_you_are_not_live','We are not live at this moment')) ;
print '
';
$poster_data=' poster="'.$thumb[0].'" ';
if($poster_show=='no'){
$poster_data='';
}
$is_muted='';
if( isset($event_settings['mute']) && intval($event_settings['mute'])==1){
$is_muted=' muted ';
}
$autoplay='autoplay';
if(isset($event_settings['autoplay']) && intval($event_settings['autoplay'])==0){
$autoplay='';
}
echo'
';
print '';
if(trim($hls_playback_url) ==''){
// $show_wpstream_not_live_mess='';
}else{
print '';
}
if($use_chat=="yes"){
$this->wpstream_connect_to_chat($channel_id);
}
usleep (10000);
}
/*
*
* Request HLS player
*
*
*
*/
public function wpstream_request_video_on_demand_hls_player($video_name,$product_id){
if($video_name==''){
return '';
}
$transient_name = 'wpstream_video_on_demand_'.$video_name;
$hls_to_return = get_transient( $transient_name );
$hls_to_return = false;
if($hls_to_return==false){
$access_token = $this->main->wpstream_live_connection->wpstream_get_token();
$url = 'video/info';
//corsorigin de check
$local_event_options = get_option('wpstream_user_streaming_global_channel_options') ;
$domain = parse_url ( get_site_url() );
$domain_scheme = 'http';
if(is_ssl()){
$domain_scheme='https';
}
$wpstream_vod_domain_lock = intval( get_option('wpstream_vod_domain_lock','') ) ;
$corsorigin='*';
if($wpstream_vod_domain_lock !== 0 ){
$corsorigin=$domain_scheme.'://'.$domain['host'];
}
$is_encrypt="false";
$wpstream_vod_encrypt = intval( get_option('wpstream_vod_encrypt','') ) ;
if( intval( $wpstream_vod_encrypt ) ==1 ){
$is_encrypt="true";
}
$hlsKeysUrlPrefix = get_site_url().'?wpstream_voddrm=';
$encrypt = $is_encrypt;
$debugDrm = false;
$curl_post_fields=array(
'access_token' => $access_token,
'name' => $video_name,
'corsOrigin' => $corsorigin,
'encryptHls' => $encrypt,
'hlsKeysUrlPrefix' => $hlsKeysUrlPrefix,
'debugDrm' => $debugDrm,
);
$curl_response = $this->main->wpstream_live_connection->wpstream_baker_do_curl_base($url,$curl_post_fields);
$curl_response_decoded = json_decode($curl_response,JSON_OBJECT_AS_ARRAY);
if($curl_response_decoded['success']){
set_transient( $transient_name, $curl_response_decoded['hlsUrl'] ,300);
$hls_to_return = $curl_response_decoded['hlsUrl'];
if( isset($curl_response_decoded['hlsDecryptionKey']) && isset($curl_response_decoded['hlsDecryptionKeyIndex']) ){
update_post_meta($product_id,'hlsDecryptionKey',$curl_response_decoded['hlsDecryptionKey']);
update_post_meta($product_id,'hlsDecryptionKeyIndex',$curl_response_decoded['hlsDecryptionKeyIndex']);
}else{
delete_post_meta($product_id,'hlsDecryptionKey');
delete_post_meta($product_id,'hlsDecryptionKeyIndex');
}
}else{
return '';
}
}
return $hls_to_return;
}
function wpstream_get_player_theme( $channel_id = null ) {
$player_theme = get_option('wpstream_video_player_theme');
$is_streamify_user = $this->wpstream_is_streamify_user( $channel_id );
if ( !empty($player_theme) && !$is_streamify_user ) {
$this->wpstream_enqueue_player_theme_style( $player_theme );
return 'vjs-theme-' . $player_theme;
}
return '';
}
function wpstream_enqueue_player_theme_style( $player_theme ) {
if ( $player_theme != 'default' ) {
wp_enqueue_style('videojs-theme-' . $player_theme, 'https://cdn.jsdelivr.net/npm/@videojs/themes@1/dist/' . $player_theme . '/index.min.css', array(), '1.0.0');
}
}
/**
* VODPlayer uri details
*
* @author cretu
*/
public function wpstream_video_on_demand_player_uri_request($product_id){
$wpstream_data_setup = ' data-setup="{}" ';
/* free_video_type
* 1 - free live channel
* 2 - free video encrypted
* 3 - free video -not encrypted
*/
$post_type = get_post_type($product_id);
$free_video_type = intval( get_post_meta($product_id, 'wpstream_product_type', true));
if( ( $post_type =='wpstream_product_vod' && $free_video_type==2 ) || get_post_type($product_id)=='product' ){
/*
* IF vide is encrypted- readed from vod,streaner
*
*/
$video_type = 'application/x-mpegURL';
$video_path = get_post_meta($product_id,'_movie_url',true);
if(get_post_type($product_id)=='wpstream_product_vod'){
$video_path = esc_html(get_post_meta($product_id, 'wpstream_free_video', true));
}
$video_path_final = $this->wpstream_request_video_on_demand_hls_player($video_path,$product_id);
}else if( $post_type =='wpstream_product_vod' && $free_video_type==3 ){
/* Video is unecrypted - read from local or youtube / vimeo
*/
$video_type = 'video/mp4';
$video_path_final=esc_html(get_post_meta($product_id, 'wpstream_free_video_external', true));
wp_enqueue_script('youtube.min');
}
$return_array = array();
$return_array['video_path_final'] = $video_path_final;
$return_array['wpstream_data_setup']= $wpstream_data_setup;
$return_array['video_type'] = $video_type;
$return_array['free_video_type'] = $free_video_type;
$return_array['post_type'] = $post_type ;
return $return_array;
}
/**
* VODPlayer url
*
* @author cretu
*/
public function wpstream_video_on_demand_player($product_id){
wp_enqueue_script('video.min');
wp_enqueue_script('wpstream-player');
$player_theme = $this->wpstream_get_player_theme();
$uri_details = $this->wpstream_video_on_demand_player_uri_request($product_id);
$video_path_final = $uri_details['video_path_final'];
$wpstream_data_setup = $uri_details['wpstream_data_setup'];
$video_type = $uri_details['video_type'];
$now = time().rand(0,1000000);
$overlay_video_div_id = "random_id_".$now;
$this->wpstream_render_vod_title_overlay(
$overlay_video_div_id,
get_the_title($product_id)
);
$thumb_id = get_post_thumbnail_id($product_id);
$thumb = wp_get_attachment_image_src($thumb_id,'small');
$usernamestream = esc_html ( get_option('wpstream_api_username','') );
$poster_thumb = '';
if(isset($thumb[0])){
$poster_thumb=$thumb[0];
}
$hlsDecryptionKey = get_post_meta($product_id,'hlsDecryptionKey',true);
$hlsDecryptionKeyIndex = get_post_meta($product_id,'hlsDecryptionKeyIndex',true);
$pack = $this->main->quota_manager->get_live_quota_data( 'wpstream_video_on_demand_player' );
$trailer_attachment_id = intval (get_post_meta( $product_id, 'video_trailer', true ));
$video_trailer = '';
$video_trailer_type = '';
if($trailer_attachment_id!=0) {
$video_trailer = wp_get_attachment_url( $trailer_attachment_id );
$attachment_metadata = wp_get_attachment_metadata($trailer_attachment_id);
if( isset ($attachment_metadata['mime_type']) ) {
$video_trailer_type = $attachment_metadata['mime_type'];
}
}
// override trailer setup here (for testing)
// $trailer_attachment_id = 1;
// $video_trailer = '/wp-content/uploads/2023/10/production-ID_4608975.mp4';
// $video_trailer = '/wp-content/uploads/2023/10/ultrawide.mp4';
// If the video is self hosted or external, we should let the user see it
$video_type = intval( get_post_meta($product_id, 'wpstream_product_type', true));
if ( (isset($pack['available_data_mb']) && $pack['available_data_mb']>0) || $video_type === 3 ) {
if($video_path_final==''){
if( $uri_details['post_type']=='wpstream_product_vod' && $uri_details['free_video_type']==3 ){
}else{
print '
This video does not exist or it has been deleted!
';
}
}
// TODO (crerem) populate these from VOD settings
$autoplay = false;
$muted = false;
$wpstream_vod_start_muted = intval ( get_option('wpstream_vod_start_muted','') );
if($wpstream_vod_start_muted===1){
$muted=true;
}
$wpstream_vod_autoplay = intval ( get_option('wpstream_vod_autoplay','') );
if($wpstream_vod_autoplay===1){
$autoplay=true;
}
$poster_data = 'poster="'.esc_url($poster_thumb).'"';
$has_trailer_class='';
if($trailer_attachment_id !=0){
$poster_data=''; // cancel poster for theme
$has_trailer_class='wpstream_theme_player_has_trailer';
}
$player_logo_position = get_option('wpstream_player_logo_position');
$player_logo_position_class = '';
$player_logo_horizontal_position = '';
if( $player_logo_position && $player_logo_position!='' ){
$player_logo_position_class = 'logo-' . explode( '-', $player_logo_position )[0];
$player_logo_horizontal_position = 'logo-' . explode( '-', $player_logo_position )[1];
}
$captionsUrl = get_post_meta( $product_id, 'wpstream_closed_captions_file', true );
echo '
To view this video please enable JavaScript, and consider upgrading to a web browser that
supports HTML5 video
';
if($trailer_attachment_id !=0){
print '
';
print '
'.esc_html__('Play Trailer','wpstream').'
';
print '
'.esc_html__('Play Video','wpstream').'
';
print '
';
print '
';
print '';
print '
';
}
else {
print '';
}
}else{
print '
'.esc_html__('Insufficient resources to stream this title','wpstream').'
';
}
}
/**
* VODPlayer url - only trailer - used in theme
*
* @author cretu
*/
public function wpstream_video_on_demand_player_only_trailer($product_id){
wp_enqueue_script('video.min');
wp_enqueue_script('wpstream-player');
$player_theme = $this->wpstream_get_player_theme();
$now = time().rand(0,1000000);
$overlay_video_div_id = "random_id_".$now;
$this->wpstream_render_vod_title_overlay(
$overlay_video_div_id,
get_the_title($product_id)
);
$thumb_id = get_post_thumbnail_id($product_id);
$thumb = wp_get_attachment_image_src($thumb_id,'small');
$usernamestream = esc_html ( get_option('wpstream_api_username','') );
$poster_thumb = '';
if(isset($thumb[0])){
$poster_thumb=$thumb[0];
}
$trailer_attachment_id = intval (get_post_meta( $product_id, 'video_trailer', true ));
$video_trailer = '';
$video_trailer_type = '';
if($trailer_attachment_id!=0) {
$video_trailer = wp_get_attachment_url( $trailer_attachment_id );
$attachment_metadata = wp_get_attachment_metadata($trailer_attachment_id);
if(isset($attachment_metadata['mime_type'])){
$video_trailer_type = $attachment_metadata['mime_type'];
}
}
$autoplay = false;
$muted = false;
if( intval ( get_option('wpstream_vod_start_muted','') ) === 1){
$muted = true;
}
if( intval ( get_option('wpstream_vod_autoplay','') ) === 1 ){
$autoplay = true;
}
$video_path_final='';
$has_trailer_class='wpstream_theme_player_has_trailer';
echo '
To view this video please enable JavaScript, and consider upgrading to a web browser that
supports HTML5 video
';
if($trailer_attachment_id !=0){
print '
';
print '
'.esc_html__('Play Trailer','wpstream').'
';
print '
';
print '
';
print '';
print '
';
}
else {
//just show the poster or don't show anything; no player needed
}
}
/**
*
* edited 4.0
*
* Retreive username for vod path
*
* @author cretu
*/
private function wpstream_retrive_username(){
return get_option('wpstream_api_username_from_token');
}
/**
* Render title overlay via action hook.
*
* @param string $overlay_id Overlay element id.
* @param string $title_text Title text.
*/
private function wpstream_render_vod_title_overlay( $overlay_id, $title_text ) {
if ( has_action( 'wpstream_vod_title_overlay' ) ) {
do_action(
'wpstream_vod_title_overlay',
$overlay_id,
$title_text,
esc_html__( 'Playing:', 'wpstream' )
);
}
}
/**
* check if the we can add display the player
*
* @since 3.12
* returns html of the player
*/
public function wpstream_check_if_player_can_dsplay($product_id){
if ( is_user_logged_in() ) {
$term_list = wp_get_post_terms($product_id, 'product_type');
$current_user = wp_get_current_user();
$subscription_model = intval( get_option('wpstream_global_sub','')) ;
$product = wc_get_product($product_id);
$product_type = $product->get_type();
if($subscription_model==1){ // if we have Neflix mode
if( $product_type=='subscription' ){ // if the product loaded is a subscription and we are on netflix mode
return false;
}
if($this->wpstream_in_plugin_check_global_subscription_model($product_id)){
return true;
}
}else{
// ppv mode
if( $product_type=='subscription' ){
// $user_subscriptions = wcs_get_users_subscriptions($current_user->ID );
// foreach ($user_subscriptions as $subscription) {
// $subscription_status = $subscription->get_status();
// echo "*** Subscription ID $subscription->ID for User ID $current_user->ID has status: $subscription_status";
// }
if( wcs_user_has_subscription( $current_user->ID, $product_id ,'active') ) {
// user has active subcription
return true;
}
}else{
if( wc_customer_bought_product( $current_user->user_email, $current_user->ID, $product_id)){
return true;
}
}
}
return false;
}
return false;
}
/**
* check if the we can add display the player - theme variant
*
* @since 3.12
* returns html of the player
*/
public function wpstream_check_if_player_can_dsplay_theme($product_id){
$post_type= get_post_type($product_id);
if($post_type=='wpstream_product_vod' || $post_type=='wpstream_product'){
return true; // if we have free vod or live
}
if ( is_user_logged_in() && $post_type==='product' ) {
$product = wc_get_product( $product_id );
$product_type = $product->get_type();
$possible_bundle = get_post_meta($product_id, 'wpstream_part_of_bundle',true);
$current_user = wp_get_current_user();
$subscription_model = intval( get_option('wpstream_global_sub','')) ;
// print 'subscription model '.$subscription_model.'';
if($subscription_model==1){ // if we have Neflix mode
if( $product_type=='subscription' ){ // if the product loaded is a subscription and we are on netflix mode
return false;
}
if($this->wpstream_in_plugin_check_global_subscription_model($product_id)){
return true;
}
}else{
// ppv mode
if ( wc_customer_bought_product( $current_user->user_email, $current_user->ID, $product_id) ){
return true; //simple product bought
}else if (function_exists('wcs_user_has_subscription') && wcs_user_has_subscription( $current_user->ID, $product_id ,'active') ){
return true; // subscription boght
}else if( get_post_type($product_id) =='product' &&
intval($possible_bundle )!=0 &&
wc_customer_bought_product( $current_user->user_email, $current_user->ID, $possible_bundle)
){
return true; // part of a boght bundle
}
}
// print ' fac return false ';
return false;
}
return false;
}
/**
* in plugin - check if global subscription model is enabled
*
* @since 3.12
*
*/
public function wpstream_in_plugin_check_global_subscription_model($product_id) {
// $selected_sub= get_post_meta($post->ID,'_wpstream_parent_sub',true);
if( is_user_logged_in() && function_exists('wcs_user_has_subscription') ){
global $woocommerce;
$current_user = wp_get_current_user();
$subscription_model = intval( get_option('wpstream_global_sub','')) ;
$main_subscription = intval( get_option('wpstream_global_sub_id',''));
if($subscription_model==1){
$selected_sub= get_post_meta($product_id,'_wpstream_parent_sub',true) ;
if( is_array($selected_sub) ){
// we have per product sub
foreach($selected_sub as $key=>$subscrition_id ):
if( wcs_user_has_subscription( $current_user->ID, $subscrition_id ,'active') ) {
return true;
}
endforeach;
} else if($main_subscription!=0){
// if we have one main subscription
if( wcs_user_has_subscription( $current_user->ID, $main_subscription ,'active') ) {
return true;
}
}
}
return false;
}
// there is no woo subscription or user not logged in
return false;
}
/**
*
*
*
*
* check if the user bought the product and display the player - TO REDo
*
* @since 3.0.1
* returns html of the player
*
*
*
*
*
*/
public function wpstream_user_logged_in_product_already_bought($from_sh_id='') {
if(function_exists('wpstream_remove_wpstream_filter')){
return;
}
global $product;
$product_id = $product->get_id();
$current_user = wp_get_current_user();
if ( is_user_logged_in() ) {
if($this->wpstream_check_if_player_can_dsplay($product_id) ){
echo '
';
$is_subscription_live_event = esc_html(get_post_meta($product_id,'_subscript_live_event',true));
$term_list = wp_get_post_terms($product_id, 'product_type');
if( $term_list[0]->name=='live_stream' || ($term_list[0]->name=='subscription' && $is_subscription_live_event=='yes' ) ){
$this->wpstream_live_event_player($product_id);
}else if( $term_list[0]->name=='video_on_demand' || ($term_list[0]->name=='subscription' && $is_subscription_live_event=='no' ) ){
$this->wpstream_video_on_demand_player($product_id);
}
echo '
';
}else{
$term_list = wp_get_post_terms($product_id, 'product_type');
if( $term_list[0]->name=='subscription' ){
if( !wcs_user_has_subscription( $current_user->ID, $product_id ,'active') ) {
$this->wpstream_display_no_buy_message('nobuy',$product_id);
}
}else{
$this->wpstream_display_no_buy_message('nobuy',$product_id);
}
}
}else{
$this->wpstream_display_no_buy_message('nolog',$product_id);
}
}
/**
*
*
* check if the user bought the product and display the player - TO REDo
*
* @since 3.0.1
* returns html of the player
*/
public function wpstream_chat_wrapper($product_id){
require_once plugin_dir_path( dirname( __FILE__ ) ) . 'includes/templates/wpstream_chat_template.php';
}
/**
* coonect to chat
*
* @since 1.12.2
*
*/
public function wpstream_connect_to_chat($product_id){
$current_user = wp_get_current_user();
$userID = $current_user->ID;
$user_login = $current_user->user_login;
$key='';
$chat_url = get_post_meta($product_id,'chat_url',true);
wp_enqueue_script( 'sockjs-0.3.min' );
wp_enqueue_script( 'emojione.min.js' );
wp_enqueue_script( "jquery-effects-core");
wp_enqueue_script( 'jquery.linkify.min.js');
wp_enqueue_script( 'ripples.min.js');
wp_enqueue_script( 'material.min.js');
wp_enqueue_script( 'chat.js');
wp_enqueue_style( 'chat.css');
wp_enqueue_style( 'ripples.css');
wp_enqueue_style( 'emojione.min.css');
if(!is_user_logged_in()){
$user_login='';
$chat_url='';
}
print '';
}
/**
* display no buy Message
*
* @since 3.12.2
*
*/
public function wpstream_display_no_buy_message($log,$product_id) {
if($log=='sub_active'){
$message= esc_html( get_option('wpstream_subscription_active','Your Subscription is Active.')) ;
echo '
';
echo '
'.$message.'
';
echo '
';
return;
}else if($log=='nolog'){
$message= esc_html( get_option('wpstream_product_not_login','You must be logged in to watch this video.')) ;
}else{
$message =esc_html( get_option('wpstream_product_not_bought','You did not yet purchase this item.')) ;
}
$subscription_model = intval( get_option('wpstream_global_sub','')) ;
if($subscription_model==1){
$message =esc_html( get_option('wpstream_product_not_subscribe','You did not yet subscribe to this item.'));
}
if( get_post_type($product_id) == 'product' && $subscription_model==0 ){
$product = wc_get_product($product_id);
$term_list = wp_get_post_terms($product_id, 'product_type');
$product_type = $product->get_type();
$is_subscription_live_event = esc_html(get_post_meta($product_id,'_subscript_live_event',true));
if( $term_list[0]->name=='video_on_demand' || $term_list[0]->name=='live_stream' || $product_type=='subscription'){
echo '
';
echo '
'.$message.'
';
echo '
';
}
}else if( get_post_type($product_id) == 'product' && $subscription_model==1 ){
$term_list = wp_get_post_terms($product_id, 'product_type');
if( $term_list[0]->name!=='simple'){
echo '
';
echo '
'.$message.'
';
echo '
';
}
}
}
/**
* Get the video player logo URL.
*
* @param int $product_id Product ID.
* @return mixed|string
*/
public function wpstream_get_video_player_logo( $product_id ) {
$is_streamify_user = $this->wpstream_is_streamify_user( $product_id );
if ( $is_streamify_user ) {
return WPSTREAM_PLUGIN_DIR_URL . 'img/wpstream-symbol-large.png';
}
$logo = get_option( 'wpstream_player_logo', '' );
if ( ! empty( $logo ) ) {
return $logo;
}
}
/**
* Get cached pack data or request fresh pack data
*
* @param bool $force_refresh Force a refresh of the cached data
* @return array Pack data
*/
private function wpstream_get_cached_pack_data( $force_refresh = false ) {
$cached_data = get_transient( 'wpstream_user_pack_data' );
if ( $force_refresh || $cached_data === false ) {
$fresh_data = $this->main->wpstream_live_connection->wpstream_request_pack_data_per_user('wpstream_get_cached_pack_data');
set_transient( 'wpstream_user_pack_data', $fresh_data, 60);
return $fresh_data;
}
return $cached_data;
}
/*
* Check if the user is on a basic subscription
*
* @param int $channel_id Channel ID
* @return bool
*/
public function wpstream_is_streamify_user( $channel_id ) {
$is_basic_streaming = get_post_meta( $channel_id, 'basicStreaming', true );
if ( $is_basic_streaming === '1' ) {
return true;
}
return false;
}
}