bp_activity_embed_media

Outputs the first embedded item in the activity oEmbed template.

Description

bp_activity_embed_media(); 

Usage

  1. if ( !function_exists( 'bp_activity_embed_media' ) ) { 
  2. require_once ABSPATH . PLUGINDIR . 'buddypress/bp-activity/bp-activity-embeds.php'; 
  3.  
  4.  
  5. // NOTICE! Understand what this does before running. 
  6. $result = bp_activity_embed_media(); 
  7.  

Defined (1)

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

/bp-activity/bp-activity-embeds.php  
  1. function bp_activity_embed_media() { 
  2. // Bail if oEmbed request explicitly hides media. 
  3. if ( isset( $_GET['hide_media'] ) && true == wp_validate_boolean( $_GET['hide_media'] ) ) { 
  4. /** 
  5. * Do something after media is rendered for an activity oEmbed item. 
  6. * @since 2.6.0 
  7. */ 
  8.  
  9. return; 
  10.  
  11. /** 
  12. * Should we display media in the oEmbed template? 
  13. * @since 2.6.0 
  14. * @param bool $retval Defaults to true. 
  15. */ 
  16.  
  17. // Find oEmbeds from only WP registered providers. 
  18. bp_remove_all_filters( 'oembed_providers' ); 
  19. $media = bp_core_extract_media_from_content( $GLOBALS['activities_template']->activity->content, 'embeds' ); 
  20. bp_restore_all_filters( 'oembed_providers' ); 
  21.  
  22. // oEmbeds have precedence over inline video / audio. 
  23. if ( isset( $media['embeds'] ) && true === $allow_media ) { 
  24. // Autoembed first URL. 
  25. $oembed_defaults = wp_embed_defaults(); 
  26. $oembed_args = array( 
  27. 'width' => $oembed_defaults['width'],  
  28. 'height' => $oembed_defaults['height'],  
  29. 'discover' => true 
  30. ); 
  31. $url = $media['embeds'][0]['url']; 
  32. $cachekey = '_oembed_response_' . md5( $url . serialize( $oembed_args ) ); 
  33.  
  34. // Try to fetch oEmbed response from meta. 
  35. $oembed = bp_activity_get_meta( bp_get_activity_id(), $cachekey ); 
  36.  
  37. // No cache, so fetch full oEmbed response now! 
  38. if ( '' === $oembed ) { 
  39. $o = _wp_oembed_get_object(); 
  40. $oembed = $o->fetch( $o->get_provider( $url, $oembed_args ), $url, $oembed_args ); 
  41.  
  42. // Cache oEmbed response. 
  43. bp_activity_update_meta( bp_get_activity_id(), $cachekey, $oembed ); 
  44.  
  45. $content = ''; 
  46.  
  47. /** 
  48. * Filters the default embed display max width. 
  49. * This is used if the oEmbed response does not return a thumbnail width. 
  50. * @since 2.6.0 
  51. * @param int $width. 
  52. */ 
  53.  
  54. // Set thumbnail. 
  55. if ( 'photo' === $oembed->type ) { 
  56. $thumbnail = $oembed->url; 
  57. } elseif ( isset( $oembed->thumbnail_url ) ) { 
  58. $thumbnail = $oembed->thumbnail_url; 
  59.  
  60. /** Non-oEmbed standard attributes */ 
  61. // Mixcloud 
  62. } elseif ( isset( $oembed->image ) ) { 
  63. $thumbnail = $oembed->image; 
  64. // ReverbNation 
  65. } elseif ( isset( $oembed->{'thumbnail-url'} ) ) { 
  66. $thumbnail = $oembed->{'thumbnail-url'}; 
  67.  
  68. // Display thumb and related oEmbed meta. 
  69. if ( true === isset ( $thumbnail ) ) { 
  70. $play_icon = $caption = ''; 
  71.  
  72. // Add play icon for non-photos. 
  73. if ( 'photo' !== $oembed->type ) { 
  74. /** 
  75. * ion-play icon from Ionicons. 
  76. * @link http://ionicons.com/ 
  77. * @license MIT 
  78. */ 
  79. $play_icon = <<<EOD 
  80. <svg id="Layer_1" style="enable-background:new 0 0 512 512;" version="1.1" viewBox="0 0 512 512" xml:space="preserve" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink"><path d="M405.2, 232.9L126.8, 67.2c-3.4-2-6.9-3.2-10.9-3.2c-10.9, 0-19.8, 9-19.8, 20H96v344h0.1c0, 11, 8.9, 20, 19.8, 20 c4.1, 0, 7.5-1.4, 11.2-3.4l278.1-165.5c6.6-5.5, 10.8-13.8, 10.8-23.1C416, 246.7, 411.8, 238.5, 405.2, 232.9z"/></svg> 
  81. EOD; 
  82.  
  83. $play_icon = sprintf( '<a rel="nofollow" class="play-btn" href="%1$s" onclick="top.location.href=\'%1$s\'">%2$s</a>', esc_url( $url ), $play_icon ); 
  84.  
  85. // Thumb width 
  86. $thumb_width = isset( $oembed->thumbnail_width ) && 'photo' !== $oembed->type && (int) $oembed->thumbnail_width < 550 ? (int) $oembed->thumbnail_width : $width; 
  87.  
  88. $float_width = 350; 
  89.  
  90. // Set up thumb. 
  91. $content = sprintf( '<div class="thumb" style="max-width:%1$spx">%2$s<a href="%3$s" rel="nofollow" onclick="top.location.href=\'%3$s\'"><img src="%4$s" /></a></div>', $thumb_width, $play_icon, esc_url( $url ), esc_url( $thumbnail ) ); 
  92.  
  93. // Show title. 
  94. if ( isset( $oembed->title ) ) { 
  95. $caption .= sprintf( '<p class="caption-title"><strong>%s</strong></p>', apply_filters( 'single_post_title', $oembed->title ) ); 
  96.  
  97. // Show description (non-oEmbed standard) 
  98. if ( isset( $oembed->description ) ) { 
  99. $caption .= sprintf( '<div class="caption-description">%s</div>', apply_filters( 'bp_activity_get_embed_excerpt', $oembed->description ) ); 
  100.  
  101. // Show author info. 
  102. if ( isset( $oembed->provider_name ) && isset( $oembed->author_name ) ) { 
  103. /** translators: By [oEmbed author] on [oEmbed provider]. eg. By BuddyPress on YouTube. */ 
  104. $anchor_text = sprintf( __( 'By %1$s on %2$s', buddypress ), $oembed->author_name, $oembed->provider_name ); 
  105.  
  106. } elseif ( isset( $oembed->provider_name ) ) { 
  107. $anchor_text = sprintf( __( 'View on %s', buddypress ), $oembed->provider_name ); 
  108.  
  109. if ( true === isset( $anchor_text ) ) { 
  110. $caption .= sprintf( '<a rel="nofollow" href="%1$s" onclick="top.location.href=\'%1$s\'">%2$s</a>', esc_url( $url ), apply_filters( 'the_title', $anchor_text ) ); 
  111.  
  112. // Set up caption. 
  113. if ( '' !== $caption ) { 
  114. $css_class = isset( $oembed->provider_name ) ? sprintf( ' provider-%s', sanitize_html_class( strtolower( $oembed->provider_name ) ) ) : ''; 
  115. $caption = sprintf( '<div class="caption%1$s" style="width:%2$s">%3$s</div>',  
  116. $css_class,  
  117. $thumb_width > $float_width ? 100 . '%' : round( ( $width - (int) $thumb_width ) / $width * 100 ) . '%',  
  118. $caption 
  119. ); 
  120.  
  121. $content .= $caption; 
  122.  
  123. // Print rich content. 
  124. if ( '' !== $content ) { 
  125. printf( '<div class="bp-activity-embed-display-media %s" style="max-width:%spx">%s</div>',  
  126. $thumb_width < $float_width ? 'two-col' : 'one-col',  
  127. $thumb_width < $float_width ? $width : $thumb_width,  
  128. $content 
  129. ); 
  130.  
  131. // Video / audio. 
  132. } elseif ( true === $allow_media ) { 
  133. // Call BP_Embed if it hasn't already loaded. 
  134.  
  135. // Run shortcode and embed routine. 
  136. $content =buddypress)->embed->run_shortcode( $GLOBALS['activities_template']->activity->content ); 
  137. $content =buddypress)->embed->autoembed( $content ); 
  138.  
  139. // Try to find inline video / audio. 
  140. $media = bp_core_extract_media_from_content( $content, 96 ); 
  141.  
  142. // Video takes precedence. HTML5-only. 
  143. if ( isset( $media['videos'] ) && 'shortcodes' === $media['videos'][0]['source'] ) { 
  144. printf( '<video controls preload="metadata"><source src="%1$s"><p>%2$s</p></video>',  
  145. esc_url( $media['videos'][0]['url'] ),  
  146. esc_html__( 'Your browser does not support HTML5 video', buddypress ) 
  147. ); 
  148.  
  149. // No video? Try audio. HTML5-only. 
  150. } elseif ( isset( $media['audio'] ) && 'shortcodes' === $media['audio'][0]['source'] ) { 
  151. printf( '<audio controls preload="metadata"><source src="%1$s"><p>%2$s</p></audio>',  
  152. esc_url( $media['audio'][0]['url'] ),  
  153. esc_html__( 'Your browser does not support HTML5 audio', buddypress ) 
  154. ); 
  155.  
  156.  
  157. /** This hook is documented in /bp-activity/bp-activity-embeds.php */