You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

66 lines
1.8 KiB

<?php
namespace Grav\Plugin\YT\Twig;
use Grav\Common\Grav;
class YTTwigExtension extends \Twig_Extension
{
/**
* Returns extension name.
*
* @return string
*/
public function getName()
{
return 'YTTwigExtension';
}
/**
* {@inheritdoc}
*/
public function getFunctions()
{
return [
new \Twig_SimpleFunction('yt_embed_url', [$this, 'embedUrl']),
];
}
/**
* Builds YouTube video embed URL.
*
* @param string $video_id
* @param array $player_parameters
* @param bool $privacy_enhanced_mode
* @return string
*/
public function embedUrl($video_id, array $player_parameters = array(), $privacy_enhanced_mode = FALSE)
{
$grav = Grav::instance();
// build base video embed URL (while respecting privacy enhanced mode setting)
$url = 'https://www.youtube' . ($privacy_enhanced_mode ? '-nocookie' : '') . '.com/embed/' . $video_id;
// filter player parameters to only those not matching YouTube defaults
$filtered_player_parameters = array();
foreach ($player_parameters as $key => $value) {
$parameter_blueprint = $grav['config']->blueprints()->get('plugins.yt.player_parameters.' . $key);
// configured value matches YouTube default -> skip parameter
if (isset($parameter_blueprint['default']) && $parameter_blueprint['default'] == $value) {
continue;
}
$filtered_player_parameters[$key] = $value;
}
// append query string (if any)
if (!empty($filtered_player_parameters) && ($query_string = http_build_query($filtered_player_parameters))) {
$url .= '?' . $query_string;
}
return $url;
}
}