BP_Media_Extractor::extract_embeds( string $richtext, string $plaintext, array $extra_args = array() )

Extract any URL, matching a registered oEmbed endpoint, from text.

Description

Parameters

$richtext

(string) (Required) Content to parse.

$plaintext

(string) (Required) Sanitized version of the content.

$extra_args

(array) (Optional) Bespoke data for a particular extractor (optional).

Default value: array()

Return

(array)

  • 'has'
    (array) Extracted media counts. {
  • 'embeds'
    (int)
  • 'embeds'
    (array) Extracted oEmbeds. { Array of extracted media.
  • 'url'
    (string) oEmbed link.
  • Source

    File: bp-core/classes/class-bp-media-extractor.php

    	protected function extract_embeds( $richtext, $plaintext, $extra_args = array() ) {
    		$data   = array( 'has' => array( 'embeds' => 0 ), 'embeds' => array() );
    		$embeds = array();
    
    		if ( ! function_exists( '_wp_oembed_get_object' ) ) {
    			require( ABSPATH . WPINC . '/class-oembed.php' );
    		}
    
    
    		// Matches any links on their own lines. They may be oEmbeds.
    		if ( stripos( $richtext, 'http' ) !== false ) {
    			preg_match_all( '#^\s*(https?://[^\s"]+)\s*$#im', $richtext, $matches );
    
    			if ( ! empty( $matches[1] ) ) {
    				$matches[1] = array_unique( $matches[1] );
    				$oembed     = _wp_oembed_get_object();
    
    				foreach ( $matches[1] as $link ) {
    					// Skip data URIs.
    					if ( strtolower( substr( $link, 0, 5 ) ) === 'data:' ) {
    						continue;
    					}
    
    					foreach ( $oembed->providers as $matchmask => $oembed_data ) {
    						list( , $is_regex ) = $oembed_data;
    
    						// Turn asterisk-type provider URLs into regexs.
    						if ( ! $is_regex ) {
    							$matchmask = '#' . str_replace( '___wildcard___', '(.+)', preg_quote( str_replace( '*', '___wildcard___', $matchmask ), '#' ) ) . '#i';
    							$matchmask = preg_replace( '|^#http\\\://|', '#https?\://', $matchmask );
    						}
    
    						// Check whether this "link" is really an oEmbed.
    						if ( preg_match( $matchmask, $link ) ) {
    							$data['embeds'][] = array( 'url' => $link );
    
    							break;
    						}
    					}
    				}
    			}
    		}
    
    		$data['has']['embeds'] = count( $data['embeds'] );
    
    		/**
    		 * Filters embeds extracted from text.
    		 *
    		 * @since BuddyPress 2.3.0
    		 *
    		 * @param array  $data       Extracted embeds. See {@link BP_Media_Extractor::extract_embeds()} for format.
    		 * @param string $richtext   Content to parse.
    		 * @param string $plaintext  Copy of $richtext without any markup.
    		 * @param array  $extra_args Bespoke data for a particular extractor.
    		 */
    		return apply_filters( 'bp_media_extractor_embeds', $data, $richtext, $plaintext, $extra_args );
    	}
    

    Changelog

    Changelog
    Version Description
    BuddyPress 2.3.0 Introduced.

    Questions?

    We're always happy to help with code or other questions you might have! Search our developer docs, contact support, or connect with our sales team.