__( 'Video', 'fl-builder' ),
'description' => __( 'Render a WordPress or embedable video.', 'fl-builder' ),
'category' => __( 'Basic', 'fl-builder' ),
'partial_refresh' => true,
'icon' => 'format-video.svg',
));
$this->add_js( 'jquery-fitvids' );
add_filter( 'wp_video_shortcode', __CLASS__ . '::mute_video', 10, 4 );
}
/**
* @method get_data
*/
public function get_data() {
if ( ! $this->data ) {
$this->data = FLBuilderPhoto::get_attachment_data( $this->settings->video );
if ( ! $this->data && isset( $this->settings->data ) ) {
$this->data = $this->settings->data;
}
if ( $this->data ) {
$parts = explode( '.', $this->data->filename );
$this->data->extension = array_pop( $parts );
$this->data->poster = isset( $this->settings->poster_src ) ? $this->settings->poster_src : '';
$this->data->loop = isset( $this->settings->loop ) && $this->settings->loop ? ' loop="yes"' : '';
$this->data->autoplay = isset( $this->settings->autoplay ) && $this->settings->autoplay ? ' autoplay="yes"' : '';
// WebM format
$webm_data = FLBuilderPhoto::get_attachment_data( $this->settings->video_webm );
$this->data->video_webm = isset( $this->settings->video_webm ) && $webm_data ? ' webm="' . $webm_data->url . '"' : '';
}
}
return $this->data;
}
/**
* @since 2.4
* @method render_poster_html
*/
public function render_video_html( $schema ) {
$video_html = '';
$video_poster = $this->get_poster_url();
$video_meta = '';
if ( 'media_library' === $this->settings->video_type ) {
$vid_data = $this->get_data();
$preload = FLBuilderModel::is_builder_active() && ! empty( $vid_data->poster ) ? ' preload="none"' : '';
$video_meta .= '';
if ( $schema ) {
$video_meta .= '';
}
$video_html = $video_meta;
$video_sc = sprintf( '%s', __( 'Video not specified. Please select one to display.', 'fl-builder' ) );
if ( ! empty( $vid_data->url ) ) {
$video_sc = '[video ' . $vid_data->extension . '="' . preg_replace( '/\/?\?.*/', '', $vid_data->url ) . '"' . $vid_data->video_webm . ' poster="' . $video_poster . '" ' . $vid_data->autoplay . $vid_data->loop . $preload . '][/video]';
}
if ( 'yes' === $this->settings->video_lightbox ) {
$video_html .= '
';
$video_html .= $video_sc;
$video_html .= '
';
} else {
$video_html .= $video_sc;
}
} elseif ( 'embed' === $this->settings->video_type ) {
global $wp_embed;
$video_embed = '';
if ( ! empty( $this->settings->embed_code ) ) {
$video_embed = $wp_embed->autoembed( do_shortcode( $this->settings->embed_code ) );
} elseif ( ! isset( $this->settings->connections ) ) {
$video_embed = sprintf( '%s', __( 'Video embed code not specified.', 'fl-builder' ) );
}
if ( 'yes' == $this->settings->video_lightbox ) {
$video_html = '';
$video_html .= $video_embed;
$video_html .= '
';
} else {
$video_html = $video_embed;
}
}
echo $video_html;
}
/**
* @since 2.4
* @method render_poster_html
*/
public function render_poster_html() {
$poster_html = '';
if ( 'yes' === $this->settings->video_lightbox ) {
$poster_url = $this->get_poster_url();
if ( empty( $poster_url ) ) {
$poster_html .= '';
$poster_html .= sprintf( '%s', __( 'Please specify a poster image if Video Lightbox is enabled.', 'fl-builder' ) );
$poster_html .= '
';
} else {
$video_url = $this->get_video_url();
$size = isset( $this->settings->poster_size ) && ! empty( $this->settings->poster_size ) ? $this->settings->poster_size : 'large';
$poster_html .= '';
$poster_html .= wp_get_attachment_image( $this->settings->poster, $size, '', array( 'class' => 'img-responsive' ) );
$poster_html .= '
';
}
}
echo $poster_html;
}
/**
* @since 2.4
* @method get_poster_url
*/
private function get_poster_url() {
$url = empty( $this->settings->poster ) ? '' : $this->settings->poster_src;
return $url;
}
/**
* @since 2.4
* @method get_video_url
*/
private function get_video_url() {
$settings = $this->settings;
$video_url = '';
if ( 'yes' === $settings->video_lightbox ) {
if ( 'embed' == $settings->video_type ) {
if ( strstr( $settings->embed_code, 'vimeo.com' ) ) {
$vid_id = $this->get_video_id( 'vimeo', $settings->embed_code );
$video_url = 'https://vimeo.com/' . $vid_id;
} elseif ( strstr( $settings->embed_code, 'facebook.com' ) ) {
$video_url = $this->get_video_id( 'facebook', $settings->embed_code );
} elseif ( strstr( $settings->embed_code, 'youtube' ) || strstr( $settings->embed_code, 'youtu.be' ) ) {
$vid_id = $this->get_video_id( 'youtube', $settings->embed_code );
if ( strstr( $settings->embed_code, 'youtube-nocookie' ) ) {
$video_url = 'https://youtube-nocookie.com/embed/' . $vid_id;
} else {
$video_url = 'https://youtube.com/watch?v=' . $vid_id;
}
} else {
$video_url = '';
}
} elseif ( 'media_library' == $settings->video_type ) {
$vid_data = $this->get_data();
$video_url = ! empty( $vid_data->url ) ? $vid_data->url : '';
}
}
return $video_url;
}
/**
* @method get_video_id
* @param string $source
* @param string $embed_code
*/
private function get_video_id( $source = '', $embed_code = '' ) {
$matches = array();
$id = '';
$regex = '';
$youtube_regex = '#(?:youtube(?:-nocookie)?\.com\/(?:[^\/\n\s]+\/\S+\/|(?:v|e(?:mbed)?)\/|\S*?[?&]v=)|youtu\.be\/)([a-zA-Z0-9_-]{11})#ix';
$vimeo_regex = '~(?:)?(?:.*
)?~ix';
$facebook_regex = '/(?<=src=").*?(?=[\*"])/';
if ( 'vimeo' == $source ) {
$regex = $vimeo_regex;
} elseif ( 'youtube' == $source ) {
$regex = $youtube_regex;
} elseif ( 'facebook' == $source ) {
$regex = $facebook_regex;
}
preg_match( $regex, $embed_code, $matches );
if ( ! empty( $matches ) ) {
$id = $matches[ count( $matches ) - 1 ];
}
return $id;
}
/**
* @method update
* @param $settings {object}
*/
public function update( $settings ) {
// Cache the attachment data.
if ( 'media_library' == $settings->video_type ) {
$video = FLBuilderPhoto::get_attachment_data( $settings->video );
if ( $video ) {
$settings->data = $video;
} else {
$settings->data = null;
}
}
return $settings;
}
/**
* Temporary fix for autoplay in Chrome & Safari. Video shortcode doesn't support `muted` parameter.
* Bug report: https://core.trac.wordpress.org/ticket/42718.
*
* @since 2.1.3
* @param string $output Video shortcode HTML output.
* @param array $atts Array of video shortcode attributes.
* @param string $video Video file.
* @param int $post_id Post ID.
* @return string
*/
static public function mute_video( $output, $atts, $video, $post_id ) {
if ( false !== strpos( $output, 'autoplay="1"' ) && FLBuilderModel::get_post_id() == $post_id ) {
$output = str_replace( '