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

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)
  • 'audio'
    (array) Extracted audio. { Array of extracted media.
  • 'original'
    (string) The entire shortcode.
  • 'source'
    (string) Media source. Either "html" or "shortcodes".
  • 'url'
    (string) Link to audio.
  • Source

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

    611
    612
    613
    614
    615
    616
    617
    618
    619
    620
    621
    622
    623
    624
    625
    626
    627
    628
    629
    630
    631
    632
    633
    634
    635
    636
    637
    638
    639
    640
    641
    642
    643
    644
    645
    646
    647
    648
    649
    650
    651
    652
    653
    654
    655
    656
    657
    658
    659
    660
    661
    662
    663
    664
    665
    666
    667
    668
    669
    670
    671
    672
    673
    674
    675
    676
    677
    678
    679
    680
    681
    682
    683
    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

    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.