PluginProbe
Jetpack – WP Security, Backup, Speed, & Growth / 6.4
Jetpack – WP Security, Backup, Speed, & Growth v6.4
16.2-beta 12.0.3 12.1.3 12.2.3 12.3.2 12.4.2 12.5.2 12.6.4 12.7.3 12.8.3 12.9.5 13.0.2 13.1.5 13.2.4 13.3.3 13.4.5 13.5.2 13.6.2 13.7.2 13.8.3 13.9.2 14.0.1 14.1.1 14.2.2 14.3.1 All 501 releases
jetpack / modules / shortcodes / brightcove.php
brightcove.php
271 lines 8.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * Brightcove shortcode.
5 *
6 * Brighcove had renovated their video player embedding code since they introduced their "new studio".
7 * See https://support.brightcove.com/en/video-cloud/docs.
8 * The new code is not 100% backward compatible, as long as a customized player is used.
9 * By the time I wrote this, there were about 150000+ posts embedded legacy players, so it would be a bad
10 * idea either to introduce a new brightcove shortcode, or to break those posts completely.
11 *
12 * That's why we introduce a less aggressive way: leaving the old embedding code untouched, and
13 * introduce a new set of shortcode parameters which are translated to the latest Brightcove embedding code.
14 *
15 * e.g.
16 * [brightcove video_id="12345" account_id="99999"] will be translated to the latest embedding code.
17 * [brightcove exp=627045696&vid=1415670151] or [brightcove exp=1463233149&vref=1601200825] will be translated
18 * to the legacy code.
19 *
20 */
21 class Jetpack_Brightcove_Shortcode {
22 static $shortcode = 'brightcove';
23
24 /**
25 * Parse shortcode arguments and render its output.
26 *
27 * @since 4.5.0
28 *
29 * @param array $atts Shortcode parameters.
30 *
31 * @return string
32 */
33 static public function convert( $atts ) {
34 $normalized_atts = self::normalize_attributes( $atts );
35
36 if ( empty( $atts ) ) {
37 return '<!-- Missing Brightcove parameters -->';
38 }
39
40 return self::has_legacy_atts( $normalized_atts )
41 ? self::convert_to_legacy_studio( $normalized_atts )
42 : self::convert_to_new_studio( $normalized_atts );
43 }
44
45 /**
46 * We need to take care of two kinds of shortcode format here.
47 * The latest: [shortcode a=1 b=2] and the legacy: [shortcode a=1&b=2]
48 * For an old shortcode: [shortcode a=1&b=2&c=3], it would be parsed into array( 'a' => 1&b=2&c=3' ), which is useless.
49 * However, since we want to determine whether to call convert_to_legacy_studio() or convert_to_new_studio() via passed parameters, we still need to parse the two properly.
50 * See http://jetpack.wp-a2z.org/oik_api/shortcode_new_to_old_params/
51 *
52 * @since 4.5.0
53 *
54 * @param array $atts Shortcode parameters.
55 *
56 * @return array
57 */
58 static public function normalize_attributes( $atts ) {
59 if ( is_array( $atts ) && 1 == count( $atts ) ) { // this is the case we need to take care of.
60 $parsed_atts = array();
61 $params = shortcode_new_to_old_params( $atts );
62 $params = apply_filters( 'brightcove_dimensions', $params );
63 parse_str( $params, $parsed_atts );
64
65 return $parsed_atts;
66 } else {
67 return $atts;
68 }
69 }
70
71 /**
72 * Check that it has legacy attributes.
73 *
74 * @since 4.5.0
75 *
76 * @param array $atts Shortcode parameters.
77 *
78 * @return bool
79 */
80 static public function has_legacy_atts( $atts ) {
81 return ( isset( $atts[ 'vid' ] ) || isset( $atts[ 'vref' ] ) )
82 && ( isset( $atts[ 'exp' ] ) || isset( $atts[ 'exp3' ] ) );
83 }
84
85 /**
86 * Convert to latest player format.
87 *
88 * @since 4.5.0
89 *
90 * @param array $atts Shortcode parameters.
91 *
92 * @return string
93 */
94 static public function convert_to_new_studio( $atts ) {
95 $defaults = array(
96 'account_id' => '',
97 'video_id' => '',
98 'player_id' => 'default',
99 'width' => '100%',
100 'height' => '100%',
101 );
102
103 $atts_applied = shortcode_atts( $defaults, $atts, self::$shortcode );
104
105 $player_url = sprintf(
106 '//players.brightcove.net/%s/%s_default/index.html?videoId=%s',
107 esc_attr( $atts_applied['account_id'] ),
108 esc_attr( $atts_applied['player_id'] ),
109 esc_attr( $atts_applied['video_id'] )
110 );
111
112 $output_html = sprintf(
113 '<iframe src="' . esc_url( $player_url ) . '" allowfullscreen webkitallowfullscreen mozallowfullscreen style="width: %spx; height: %spx;"></iframe>',
114 esc_attr( $atts_applied['width'] ),
115 esc_attr( $atts_applied['height'] )
116 );
117
118 return $output_html;
119 }
120
121 /**
122 * Convert to legacy player format.
123 *
124 * [brightcove exp=627045696&vid=1415670151] for the older player and backward compatibility
125 * [brightcove exp=1463233149&vref=1601200825] for the new player
126 *
127 * @since 4.5.0
128 *
129 * @param array $atts Shortcode parameters.
130 *
131 * @return string
132 */
133 static public function convert_to_legacy_studio( $atts ) {
134 $attr = shortcode_atts( array(
135 'bg' => '',
136 'exp' => '',
137 'exp3' => '',
138 'h' => '',
139 'lbu' => '',
140 'pk' => '',
141 'pubid' => '',
142 's' => '',
143 'surl' => '',
144 'vid' => '',
145 'vref' => '',
146 'w' => '',
147 ), $atts );
148
149 if ( isset( $attr['pk'] ) ) {
150 $attr['pk'] = rawurlencode( preg_replace( '/[^a-zA-Z0-9!*\'();:@&=+$,\/?#\[\]\-_.~ ]/', '', $attr['pk'] ) );
151 }
152
153 if ( isset( $attr['bg'] ) ) {
154 $attr['bg'] = preg_replace( '![^-a-zA-Z0-9#]!', '', $attr['bg'] );
155 }
156
157 $fv = array(
158 'viewerSecureGatewayURL' => 'https://services.brightcove.com/services/amfgateway',
159 'servicesURL' => 'http://services.brightcove.com/services',
160 'cdnURL' => 'http://admin.brightcove.com',
161 'autoStart' => 'false',
162 );
163
164 $js_tld = 'com';
165 $src = '';
166 $name = 'flashObj';
167 $html5 = false;
168
169 if ( isset( $attr['exp3'] ) ) {
170 if ( isset( $attr['surl'] ) && strpos( $attr['surl'], 'brightcove.co.jp' ) ) {
171 $js_tld = 'co.jp';
172 }
173 if ( ! isset( $attr['surl'] ) || ! preg_match( '#^https?://(?:[a-z\d-]+\.)*brightcove\.(?:com|co\.jp)/#', $attr['surl'] ) ) {
174 $attr['surl'] = 'http://c.brightcove.com/services';
175 }
176
177 $attr['exp3'] = intval( $attr['exp3'] );
178 $attr['pubid'] = intval( $attr['pubid'] );
179 $attr['vid'] = intval( $attr['vid'] );
180
181 $fv['servicesURL'] = $attr['surl'];
182 $fv['playerID'] = $attr['exp3'];
183 $fv['domain'] = 'embed';
184 $fv['videoID'] = intval( $attr['vid'] );
185
186 $src = sprintf( '%s/viewer/federated_f9/%s?isVid=1&amp;isUI=1&amp;publisherID=%s',
187 $attr['surl'],
188 $attr['exp3'],
189 $attr['pubid']
190 );
191 $html5 = true;
192 } elseif ( isset( $attr['exp'] ) ) {
193 $attr['exp'] = intval( $attr['exp'] );
194 $src = 'http://services.brightcove.com/services/viewer/federated_f8/' . $attr['exp'];
195 if ( $attr['vid'] ) {
196 $fv['videoId'] = $attr['vid'];
197 } else if ( $attr['vref'] ) {
198 $fv['videoRef'] = $attr['vref'];
199 }
200
201 $fv['playerId'] = $attr['exp'];
202 $fv['domain'] = 'embed';
203 } else {
204 return '<small>brightcove error: missing required parameter exp or exp3</small>';
205 }
206
207 if ( ! empty( $attr['lbu'] ) ) {
208 $fv['linkBaseURL'] = $attr['lbu'];
209 }
210
211 $flashvars = trim( add_query_arg( array_map( 'urlencode', $fv ), '' ), '?' );
212
213 $width = $height = null;
214 if ( ! empty( $attr['w'] ) && ! empty( $attr['h'] ) ) {
215 $w = abs( (int) $attr['w'] );
216 $h = abs( (int) $attr['h'] );
217 if ( $w && $h ) {
218 $width = $w;
219 $height = $h;
220 }
221 } elseif ( empty( $attr['s'] ) || 'l' === $attr['s'] ) {
222 $width = '480';
223 $height = '360';
224 }
225
226 if ( empty( $width ) || empty( $height ) ) {
227 $width = '280';
228 $height = '210';
229 }
230
231 if ( $html5 ) {
232 wp_enqueue_script(
233 'brightcove-loader',
234 Jetpack::get_file_url_for_environment( '_inc/build/shortcodes/js/brightcove.min.js', 'modules/shortcodes/js/brightcove.js' ),
235 array( 'jquery' ),
236 20121127,
237 false
238 );
239 wp_localize_script( 'brightcove-loader', 'brightcoveData', array(
240 'tld' => esc_js( $js_tld )
241 ) );
242
243 return '
244 <object id="myExperience" class="BrightcoveExperience">
245 <param name="bgcolor" value="' . esc_attr( $attr['bg'] ) . '" />
246 <param name="width" value="' . esc_attr( $width ) . '" />
247 <param name="height" value="' . esc_attr( $height ) . '" />
248 <param name="playerID" value="' . esc_attr( $attr['exp3'] ) . '" />
249 <param name="@videoPlayer" value="' . esc_attr( $attr['vid'] ) . '" />
250 <param name="playerKey" value="' . esc_attr( $attr['pk'] ) . '" />
251 <param name="isVid" value="1" />
252 <param name="isUI" value="1" />
253 <param name="dynamicStreaming" value="true" />
254 <param name="autoStart" value="false" />
255 <param name="secureConnections" value="true" />
256 <param name="secureHTMLConnections" value="true" />
257 </object>';
258 }
259
260 return sprintf( '<embed src="%s" bgcolor="#FFFFFF" flashvars="%s" base="http://admin.brightcove.com" name="%s" width="%s" height="%s" allowFullScreen="true" seamlesstabbing="false" type="application/x-shockwave-flash" swLiveConnect="true" pluginspage="http://www.macromedia.com/shockwave/download/index.cgi?P1_Prod_Version=ShockwaveFlash" />',
261 esc_url( $src ),
262 $flashvars,
263 esc_attr( $name ),
264 esc_attr( $width ),
265 esc_attr( $height )
266 );
267 }
268 }
269
270 add_shortcode( Jetpack_Brightcove_Shortcode::$shortcode, array( 'Jetpack_Brightcove_Shortcode', 'convert' ) );
271