BP_Media_Extractor::extract_audio( string $richtext, string $plaintext, array $extra_args = array() )
Extract [audio] shortcodes and <a href="*.mp3"> tags, from text.
Description
See also
- wp_get_audio_extensions(): for supported audio formats.
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()
Return
(array)
- 'has'
(array) Extracted media counts. { - 'audio'
(int)
(array) Extracted audio. { Array of extracted media.
(string) The entire shortcode.
(string) Media source. Either "html" or "shortcodes".
(string) Link to audio.
Source
File: bp-core/classes/class-bp-media-extractor.php
protected function extract_audio( $richtext, $plaintext, $extra_args = array() ) {
$data = array( 'has' => array( 'audio' => 0 ), 'audio' => array() );
$audios = $this->extract_shortcodes( $richtext, $plaintext, $extra_args );
$links = $this->extract_links( $richtext, $plaintext, $extra_args );
$audio_types = wp_get_audio_extensions();
// [audio]
$audios = wp_list_filter( $audios['shortcodes'], array( 'type' => 'audio' ) );
foreach ( $audios as $audio ) {
// Media URL can appear as the first parameter inside the shortcode brackets.
if ( isset( $audio['attributes']['src'] ) ) {
$src_param = 'src';
} elseif ( isset( $audio['attributes'][0] ) ) {
$src_param = 0;
} else {
continue;
}
$path = untrailingslashit( parse_url( $audio['attributes'][ $src_param ], PHP_URL_PATH ) );
foreach ( $audio_types as $extension ) {
$extension = '.' . $extension;
// Check this URL's file extension matches that of an accepted audio format.
if ( ! $path || substr( $path, -4 ) !== $extension ) {
continue;
}
$data['audio'][] = array(
'original' => '[audio src="' . esc_url_raw( $audio['attributes'][ $src_param ] ) . '"]',
'source' => 'shortcodes',
'url' => esc_url_raw( $audio['attributes'][ $src_param ] ),
);
}
}
// <a href="*.mp3"> tags.
foreach ( $audio_types as $extension ) {
$extension = '.' . $extension;
foreach ( $links['links'] as $link ) {
$path = untrailingslashit( parse_url( $link['url'], PHP_URL_PATH ) );
// Check this URL's file extension matches that of an accepted audio format.
if ( ! $path || substr( $path, -4 ) !== $extension ) {
continue;
}
$data['audio'][] = array(
'original' => '[audio src="' . esc_url_raw( $link['url'] ) . '"]', // Build an audio shortcode.
'source' => 'html',
'url' => esc_url_raw( $link['url'] ),
);
}
}
$data['has']['audio'] = count( $data['audio'] );
/**
* Filters audio extracted from text.
*
* @since BuddyPress 2.3.0
*
* @param array $data Extracted audio. See {@link BP_Media_Extractor::extract_audio()} 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_audio', $data, $richtext, $plaintext, $extra_args );
}
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.