|
<?php |
|
|
|
/* |
|
Plugin Name: FV Player - Handle video tags |
|
Description: Changes any <video> tag to FV Player |
|
Author: Foliovision |
|
Version: 0.2 |
|
Author URI: https://foliovision.com |
|
*/ |
|
|
|
// Work in front-end only |
|
add_action( 'wp_head', function() { |
|
ob_start(); |
|
|
|
// Check the output in wp_footer to ensure it's not too late to output CSS and JS |
|
add_action( 'wp_footer', function() { |
|
$html = ob_get_clean(); |
|
|
|
$replacements = 0; |
|
|
|
// Match all <video> tags |
|
preg_match_all( '/(?:<video[^>]*)(?:(?:\/>)|(?:>.*?<\/video>))/', $html, $videos, PREG_SET_ORDER ); |
|
foreach ( (array) $videos as $video_html ) { |
|
if ( empty( $video_html ) ) { |
|
continue; |
|
} |
|
|
|
// parse the <video> tag and its <source> tags |
|
$dom = new \DOMDocument(); |
|
$dom->loadHTML( $video_html[0] ); |
|
$video_tag = $dom->getElementsByTagName( 'video' )->item( 0 ); |
|
$sources = $video_tag->getElementsByTagName( 'source' ); |
|
if( empty($sources) ) { |
|
continue; |
|
} |
|
|
|
// Create FV Player shortcode |
|
$shortcode_args = array(); |
|
foreach( array( |
|
'poster' => 'splash', |
|
//'width' => 'width', |
|
//'height' => 'height', |
|
) AS $old => $new ) { |
|
if( $value = $video_tag->getAttribute($old) ) { |
|
$shortcode_args[$new] = $value; |
|
} |
|
} |
|
|
|
$count = 0; |
|
foreach( $sources as $source ) { |
|
$shortcode_args['src'.( $count > 0 ? $count : '' )] = $source->getAttribute( 'src' ); |
|
$count++; |
|
} |
|
|
|
// Did it find any video sources? |
|
if( $count > 0 ) { |
|
$shortcode = '[fvplayer'; |
|
foreach( $shortcode_args AS $k => $v ) { |
|
$shortcode .= ' '.$k.'="'.esc_attr($v).'"'; |
|
} |
|
$shortcode .= ']'; |
|
|
|
// create the FV Player |
|
$fv_player = do_shortcode($shortcode); |
|
if( isset($_GET['fv_player_debug']) ) { |
|
$fv_player .= "\n<!--debug video ".var_export( str_replace( '<video', '', $video_html[0] ),true)."-->\n"; |
|
$fv_player .="\n<!--debug shortcode ".$shortcode."-->\n"; |
|
} |
|
|
|
$replacements++; |
|
|
|
// Put it into page HTML |
|
$html = str_replace( $video_html[0], $fv_player, $html ); |
|
} |
|
} |
|
|
|
$html .= "<!-- FV Player - Handle video tags did ".$replacements." replacements -->\n"; |
|
echo $html; |
|
}, -1 /* Magic number - ensure FV Player Pro didn't try to init the scripts yet */ ); |
|
} ); |