brightcove.php 8.4 KB

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