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

Extract [video] shortcodes 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. {
  • 'video'
    (int)
  • 'videos'
    (array) Extracted video. { Array of extracted media.
  • 'source'
    (string) Media source. Currently only "shortcodes".
  • 'url'
    (string) Link to audio.
  • Source

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

    709
    710
    711
    712
    713
    714
    715
    716
    717
    718
    719
    720
    721
    722
    723
    724
    725
    726
    727
    728
    729
    730
    731
    732
    733
    734
    735
    736
    737
    738
    739
    740
    741
    742
    743
    744
    745
    746
    747
    748
    749
    750
    751
    752
    753
    754
    755
    756
    757
    758
    759
    760
    protected function extract_video( $richtext, $plaintext, $extra_args = array() ) {
        $data   = array( 'has' => array( 'videos' => 0 ), 'videos' => array() );
        $videos = $this->extract_shortcodes( $richtext, $plaintext, $extra_args );
     
        $video_types = wp_get_video_extensions();
     
     
        // [video]
        $videos = wp_list_filter( $videos['shortcodes'], array( 'type' => 'video' ) );
        foreach ( $videos as $video ) {
     
            // Media URL can appear as the first parameter inside the shortcode brackets.
            if ( isset( $video['attributes']['src'] ) ) {
                $src_param = 'src';
            } elseif ( isset( $video['attributes'][0] ) ) {
                $src_param = 0;
            } else {
                continue;
            }
     
            $path = untrailingslashit( parse_url( $video['attributes'][ $src_param ], PHP_URL_PATH ) );
     
            foreach ( $video_types as $extension ) {
                $extension = '.' . $extension;
     
                // Check this URL's file extension matches that of an accepted video format (-5 for webm).
                if ( ! $path || ( substr( $path, -4 ) !== $extension && substr( $path, -5 ) !== $extension ) ) {
                    continue;
                }
     
                $data['videos'][] = array(
                    'original' => $video['original'],  // Entire shortcode.
                    'source'   => 'shortcodes',
                    'url'      => esc_url_raw( $video['attributes'][ $src_param ] ),
                );
            }
        }
     
        $data['has']['videos'] = count( $data['videos'] );
     
        /**
         * Filters videos extracted from text.
         *
         * @since BuddyPress 2.3.0
         *
         * @param array  $data       Extracted videos. See {@link BP_Media_Extractor::extract_videos()} 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_videos', $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.