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

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


Description Description


Parameters Parameters

$richtext

(Required) Content to parse.

$plaintext

(Required) Sanitized version of the content.

$extra_args

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

Default value: array()


Top ↑

Return Return

(array)

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

  • Top ↑

    Source 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 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 );
    	}
    

    Top ↑

    Changelog Changelog

    Changelog
    Version Description
    2.3.0 Introduced.

    Top ↑

    User Contributed Notes User Contributed Notes

    You must log in before being able to contribute a note or feedback.