Нет описания

brightcove.php 8.8KB

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