PluginProbe
FV Player 8 / trunk
FV Player 8 vtrunk
trunk 8.0.18 8.0.19 8.0.20 8.0.21 8.0.25 8.0.27 8.1 8.1.3
fv-player / models / subtitles.php

subtitles.php in FV Player 8 trunk, at models/subtitles.php

148 lines 4.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 if ( ! defined( 'ABSPATH' ) ) {
4 exit;
5 }
6
7 class FV_Player_Subtitles {
8
9 /**
10 * List of RTL languages
11 * @see https://meta.wikimedia.org/wiki/Template:List_of_language_names_ordered_by_code
12 *
13 * @var array
14 */
15 public $aRtlSubtitles = array(
16 'ar', 'arc', 'arz', 'ckb', 'dv', 'fa', 'ha', 'he', 'khw', 'ks', 'ku', 'ps', 'sd', 'uz_af', 'yi'
17 );
18
19 public function __construct() {
20 add_filter('fv_player_item', array($this, 'add_subtitles'), 10, 3 );
21 }
22
23 function add_subtitles( $aItem, $index ) {
24 global $fv_fp;
25
26 $aSubtitles = $this->get_subtitles($index);
27 if( count($aSubtitles) == 0 ) return $aItem;
28
29 $aLangs = flowplayer::get_languages();
30 $countSubtitles = 0;
31 $aOutput = array();
32
33 foreach( $aSubtitles AS $key => $subtitles ) {
34 if( $key == 'iw' ) $key = 'he';
35 if( $key == 'in' ) $key = 'id';
36 if( $key == 'jw' ) $key = 'jv';
37 if( $key == 'mo' ) $key = 'ro';
38 if( $key == 'sh' ) $key = 'sr';
39
40 $objSubtitle = new stdClass;
41 if( $key == 'subtitles' ) {
42 $aLang = explode('-', get_bloginfo('language'));
43 if( !empty($aLang[0]) ) $objSubtitle->srclang = $aLang[0];
44 $sCode = $aLang[0];
45
46 $sCaption = '';
47 if( !empty($sCode) && $sCode == 'en' ) {
48 $sCaption = 'English';
49
50 } else if( !empty($sCode) ) {
51 $translations = get_site_transient( 'available_translations' );
52 $sLangCode = str_replace( '-', '_', get_bloginfo('language') );
53 if( $translations && isset($translations[$sLangCode]) && !empty($translations[$sLangCode]['native_name']) ) {
54 $sCaption = $translations[$sLangCode]['native_name'];
55 }
56
57 }
58
59 if( $sCaption ) {
60 $objSubtitle->label = $sCaption;
61 }
62
63 } else {
64 $objSubtitle->srclang = $key;
65 $objSubtitle->label = $aLangs[strtoupper($key)];
66 }
67
68 if( in_array( strtolower($key), $this->aRtlSubtitles) ) {
69 $objSubtitle->rtl = true;
70 }
71
72 $objSubtitle->src = $subtitles;
73 // default subtitle
74 if( $countSubtitles == 0 && $fv_fp->_get_option('subtitleOn') ) {
75 $objSubtitle->fv_default = true;
76 }
77
78 $aOutput[] = $objSubtitle;
79
80 $countSubtitles++;
81 }
82
83 if( count($aSubtitles) ) {
84 $aItem['subtitles'] = $aOutput;
85 }
86
87 return $aItem;
88 }
89
90 function get_subtitles($index = 0) {
91 global $fv_fp;
92
93 $aSubtitles = array();
94
95 // each video can have subtitles in any language with new DB-based shortcodes
96 if ( $fv_fp->current_video()) {
97 if ( $fv_fp->current_video()->getMetaData()) {
98 foreach ( $fv_fp->current_video()->getMetaData() as $meta_object) {
99 if (strpos($meta_object->getMetaKey(), 'subtitles') !== false) {
100 // subtitles meta data found, create URL from it
101 // note: we ignore $index here, as it's used to determine an index
102 // for a single subtitle file from all subtitles set for the whole
103 // playlist, which was the old way of doing stuff
104 $aSubtitles[str_replace( 'subtitles_', '', $meta_object->getMetaKey() )] = $this->get_subtitles_url( $meta_object->getMetaValue() );
105 }
106 }
107 }
108 } else {
109 if( !empty( $fv_fp->aCurArgs ) && count($fv_fp->aCurArgs) > 0 ) {
110 foreach( $fv_fp->aCurArgs AS $key => $subtitles ) {
111 if( stripos($key,'subtitles') !== 0 || empty($subtitles) ) {
112 continue;
113 }
114
115 $subtitles = explode( ";",$subtitles);
116 if( empty($subtitles[$index]) ) continue;
117
118 $aSubtitles[str_replace( 'subtitles_', '', $key )] = $this->get_subtitles_url( $subtitles[$index] );
119 }
120 }
121 }
122
123 return $aSubtitles;
124 }
125
126 function get_subtitles_url( $subtitles ) {
127 global $fv_fp;
128
129 if( strpos($subtitles,'http://') === false && strpos($subtitles,'https://') === false ) {
130 if ( $subtitles[0] === '/' ) {
131 $subtitles = substr($subtitles, 1);
132 }
133
134 $subtitles = $fv_fp->get_server_url() . $subtitles;
135 }
136 else {
137 $subtitles = trim($subtitles);
138 }
139
140 $subtitles = apply_filters( 'fv_flowplayer_resource', $subtitles );
141
142 return $subtitles;
143 }
144
145 }
146
147 $FV_Player_Subtitles = new FV_Player_Subtitles();
148