PluginProbe
Jetpack – WP Security, Backup, Speed, & Growth / 7.2.2
Jetpack – WP Security, Backup, Speed, & Growth v7.2.2
16.2 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 All 502 releases
jetpack / modules / shortcodes / brightcove.php

brightcove.php in Jetpack – WP Security, Backup, Speed, & Growth 7.2.2, at modules/shortcodes/brightcove.php

296 lines 8.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php //phpcs:ignore WordPress.Files.FileName.InvalidClassFileName
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 class Jetpack_Brightcove_Shortcode {
21 /**
22 * Shortcode name.
23 *
24 * @var string
25 */
26 public static $shortcode = 'brightcove';
27
28 /**
29 * Parse shortcode arguments and render its output.
30 *
31 * @since 4.5.0
32 *
33 * @param array $atts Shortcode parameters.
34 *
35 * @return string
36 */
37 public static function convert( $atts ) {
38 $normalized_atts = self::normalize_attributes( $atts );
39
40 if ( empty( $atts ) ) {
41 return '<!-- Missing Brightcove parameters -->';
42 }
43
44 return self::has_legacy_atts( $normalized_atts )
45 ? self::convert_to_legacy_studio( $normalized_atts )
46 : self::convert_to_new_studio( $normalized_atts );
47 }
48
49 /**
50 * We need to take care of two kinds of shortcode format here.
51 * The latest: [shortcode a=1 b=2] and the legacy: [shortcode a=1&b=2]
52 * 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.
53 * 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.
54 * See http://jetpack.wp-a2z.org/oik_api/shortcode_new_to_old_params/
55 *
56 * @since 4.5.0
57 *
58 * @param array $atts Shortcode parameters.
59 *
60 * @return array
61 */
62 public static function normalize_attributes( $atts ) {
63 if ( is_array( $atts ) && 1 === count( $atts ) ) { // this is the case we need to take care of.
64 $parsed_atts = array();
65 $params = shortcode_new_to_old_params( $atts );
66
67 /**
68 * Filter the Brightcove shortcode parameters.
69 *
70 * @module shortcodes
71 *
72 * @since 4.5.0
73 *
74 * @param string $params String of shortcode parameters.
75 */
76 $params = apply_filters( 'brightcove_dimensions', $params );
77 parse_str( $params, $parsed_atts );
78
79 return $parsed_atts;
80 } else {
81 return $atts;
82 }
83 }
84
85 /**
86 * Check that it has legacy attributes.
87 *
88 * @since 4.5.0
89 *
90 * @param array $atts Shortcode parameters.
91 *
92 * @return bool
93 */
94 public static function has_legacy_atts( $atts ) {
95 return ( isset( $atts['vid'] ) || isset( $atts['vref'] ) )
96 && ( isset( $atts['exp'] ) || isset( $atts['exp3'] ) );
97 }
98
99 /**
100 * Convert to latest player format.
101 *
102 * @since 4.5.0
103 *
104 * @param array $atts Shortcode parameters.
105 *
106 * @return string
107 */
108 public static function convert_to_new_studio( $atts ) {
109 $defaults = array(
110 'account_id' => '',
111 'video_id' => '',
112 'player_id' => 'default',
113 'width' => '100%',
114 'height' => '100%',
115 );
116
117 $atts_applied = shortcode_atts( $defaults, $atts, self::$shortcode );
118
119 $player_url = sprintf(
120 '//players.brightcove.net/%s/%s_default/index.html?videoId=%s',
121 esc_attr( $atts_applied['account_id'] ),
122 esc_attr( $atts_applied['player_id'] ),
123 esc_attr( $atts_applied['video_id'] )
124 );
125
126 $output_html = sprintf(
127 '<iframe src="' . esc_url( $player_url ) . '" allowfullscreen webkitallowfullscreen mozallowfullscreen style="width: %spx; height: %spx;"></iframe>',
128 esc_attr( $atts_applied['width'] ),
129 esc_attr( $atts_applied['height'] )
130 );
131
132 return $output_html;
133 }
134
135 /**
136 * Convert to legacy player format.
137 *
138 * [brightcove exp=627045696&vid=1415670151] for the older player and backward compatibility
139 * [brightcove exp=1463233149&vref=1601200825] for the new player
140 *
141 * @since 4.5.0
142 *
143 * @param array $atts Shortcode parameters.
144 *
145 * @return string
146 */
147 public static function convert_to_legacy_studio( $atts ) {
148 $attr = shortcode_atts(
149 array(
150 'bg' => '',
151 'exp' => '',
152 'exp3' => '',
153 'h' => '',
154 'lbu' => '',
155 'pk' => '',
156 'pubid' => '',
157 's' => '',
158 'surl' => '',
159 'vid' => '',
160 'vref' => '',
161 'w' => '',
162 ),
163 $atts
164 );
165
166 if ( isset( $attr['pk'] ) ) {
167 $attr['pk'] = rawurlencode( preg_replace( '/[^a-zA-Z0-9!*\'();:@&=+$,\/?#\[\]\-_.~ ]/', '', $attr['pk'] ) );
168 }
169
170 if ( isset( $attr['bg'] ) ) {
171 $attr['bg'] = preg_replace( '![^-a-zA-Z0-9#]!', '', $attr['bg'] );
172 }
173
174 $fv = array(
175 'viewerSecureGatewayURL' => 'https://services.brightcove.com/services/amfgateway',
176 'servicesURL' => 'http://services.brightcove.com/services',
177 'cdnURL' => 'http://admin.brightcove.com',
178 'autoStart' => 'false',
179 );
180
181 $js_tld = 'com';
182 $src = '';
183 $name = 'flashObj';
184 $html5 = false;
185
186 if ( isset( $attr['exp3'] ) ) {
187 if ( isset( $attr['surl'] ) && strpos( $attr['surl'], 'brightcove.co.jp' ) ) {
188 $js_tld = 'co.jp';
189 }
190 if ( ! isset( $attr['surl'] ) || ! preg_match( '#^https?://(?:[a-z\d-]+\.)*brightcove\.(?:com|co\.jp)/#', $attr['surl'] ) ) {
191 $attr['surl'] = 'http://c.brightcove.com/services';
192 }
193
194 $attr['exp3'] = intval( $attr['exp3'] );
195 $attr['pubid'] = intval( $attr['pubid'] );
196 $attr['vid'] = intval( $attr['vid'] );
197
198 $fv['servicesURL'] = $attr['surl'];
199 $fv['playerID'] = $attr['exp3'];
200 $fv['domain'] = 'embed';
201 $fv['videoID'] = intval( $attr['vid'] );
202
203 $src = sprintf(
204 '%s/viewer/federated_f9/%s?isVid=1&amp;isUI=1&amp;publisherID=%s',
205 $attr['surl'],
206 $attr['exp3'],
207 $attr['pubid']
208 );
209 $html5 = true;
210 } elseif ( isset( $attr['exp'] ) ) {
211 $attr['exp'] = intval( $attr['exp'] );
212 $src = 'http://services.brightcove.com/services/viewer/federated_f8/' . $attr['exp'];
213 if ( $attr['vid'] ) {
214 $fv['videoId'] = $attr['vid'];
215 } elseif ( $attr['vref'] ) {
216 $fv['videoRef'] = $attr['vref'];
217 }
218
219 $fv['playerId'] = $attr['exp'];
220 $fv['domain'] = 'embed';
221 } else {
222 return '<small>brightcove error: missing required parameter exp or exp3</small>';
223 }
224
225 if ( ! empty( $attr['lbu'] ) ) {
226 $fv['linkBaseURL'] = $attr['lbu'];
227 }
228
229 $flashvars = trim( add_query_arg( array_map( 'urlencode', $fv ), '' ), '?' );
230
231 $width = null;
232 $height = null;
233
234 if ( ! empty( $attr['w'] ) && ! empty( $attr['h'] ) ) {
235 $w = abs( (int) $attr['w'] );
236 $h = abs( (int) $attr['h'] );
237 if ( $w && $h ) {
238 $width = $w;
239 $height = $h;
240 }
241 } elseif ( empty( $attr['s'] ) || 'l' === $attr['s'] ) {
242 $width = '480';
243 $height = '360';
244 }
245
246 if ( empty( $width ) || empty( $height ) ) {
247 $width = '280';
248 $height = '210';
249 }
250
251 if ( $html5 ) {
252 wp_enqueue_script(
253 'brightcove-loader',
254 Jetpack::get_file_url_for_environment( '_inc/build/shortcodes/js/brightcove.min.js', 'modules/shortcodes/js/brightcove.js' ),
255 array( 'jquery' ),
256 20121127,
257 false
258 );
259 wp_localize_script(
260 'brightcove-loader',
261 'brightcoveData',
262 array(
263 'tld' => esc_js( $js_tld ),
264 )
265 );
266
267 return '
268 <object id="myExperience" class="BrightcoveExperience">
269 <param name="bgcolor" value="' . esc_attr( $attr['bg'] ) . '" />
270 <param name="width" value="' . esc_attr( $width ) . '" />
271 <param name="height" value="' . esc_attr( $height ) . '" />
272 <param name="playerID" value="' . esc_attr( $attr['exp3'] ) . '" />
273 <param name="@videoPlayer" value="' . esc_attr( $attr['vid'] ) . '" />
274 <param name="playerKey" value="' . esc_attr( $attr['pk'] ) . '" />
275 <param name="isVid" value="1" />
276 <param name="isUI" value="1" />
277 <param name="dynamicStreaming" value="true" />
278 <param name="autoStart" value="false" />
279 <param name="secureConnections" value="true" />
280 <param name="secureHTMLConnections" value="true" />
281 </object>';
282 }
283
284 return sprintf(
285 '<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" />',
286 esc_url( $src ),
287 $flashvars,
288 esc_attr( $name ),
289 esc_attr( $width ),
290 esc_attr( $height )
291 );
292 }
293 }
294
295 add_shortcode( Jetpack_Brightcove_Shortcode::$shortcode, array( 'Jetpack_Brightcove_Shortcode', 'convert' ) );
296