Jetpack_Brightcove_Shortcode

Brightcove shortcode.

Defined (1)

The class is defined in the following location(s).

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