Initial commit
This commit is contained in:
@@ -0,0 +1,122 @@
|
||||
<?php
|
||||
|
||||
namespace Grav\Plugin\Shortcodes;
|
||||
|
||||
use Thunder\Shortcode\Shortcode\ShortcodeInterface;
|
||||
|
||||
class YouTubeConsentShortcode extends Shortcode
|
||||
{
|
||||
public function init()
|
||||
{
|
||||
$this->shortcode->getHandlers()->add('youtube', function (ShortcodeInterface $shortcode) {
|
||||
// get default settings
|
||||
$pluginConfig = $this->config->get('plugins.youtubeconsent');
|
||||
|
||||
// get the current page in process (i.e. the page where the shortcode is being processed)
|
||||
// warning, it can be different from $this->grav['page'], if ever we browse a collection
|
||||
// this is exactly what the Feed plugin does
|
||||
$currentPage = $this->grav['plugins']->getPlugin('youtubeconsent')->getCurrentPage();
|
||||
|
||||
// values to check if we are in a feed (RSS, Atom, JSON)
|
||||
$type = $this->grav['uri']->extension(); // Get current page extension
|
||||
$feed_config = $this->grav['config']->get('plugins.feed');
|
||||
$feed_types = array('rss','atom');
|
||||
if ($feed_config && $feed_config['enable_json_feed'])
|
||||
$feed_types[] = 'json';
|
||||
|
||||
// check if the rendered page will be cached or not
|
||||
$renderingCacheDisabled = !is_null($currentPage)
|
||||
&& isset($currentPage->header()->cache_enable)
|
||||
&& !$currentPage->header()->cache_enable
|
||||
|| !$this->grav['config']->get('system.cache.enabled');
|
||||
|
||||
// check if we are in a feed (RSS, Atom, JSON)
|
||||
// we also check that the page will not be cached once rendered (otherwise the iframe will not be generated on the normal page)
|
||||
if ( $renderingCacheDisabled && // if the current page does not cache its rendering
|
||||
$feed_config && $feed_config['enabled'] && // and the Feed plugin is enabled
|
||||
isset($this->grav['page']->header()->content) && // and the current page has a collection
|
||||
$feed_types && in_array($type, $feed_types) ) { // and the Feed plugin handles it
|
||||
return $shortcode->getContent(); // return unprocessed content (because in RSS, Javascripts don't work)
|
||||
}
|
||||
|
||||
// get parameters the user provided (or set to defaults)
|
||||
$start = $shortcode->getParameter('start', 0);
|
||||
$width = $shortcode->getParameter('width', $pluginConfig['default_width']);
|
||||
$height = $shortcode->getParameter('height', $pluginConfig['default_height']);
|
||||
$quality = $shortcode->getParameter('quality', $pluginConfig['default_quality']);
|
||||
|
||||
// get the YouTube video ID from the shortcode content
|
||||
$videoId = trim(preg_replace('(<p>|</p>)', '', $shortcode->getContent()));
|
||||
|
||||
// check validity
|
||||
if ($videoId === "")
|
||||
{
|
||||
$errorMsg = $this->grav['language']->translate('PLUGIN_YOUTUBECONSENT.ERROR_NO_ID');
|
||||
return $this->twig->processTemplate(
|
||||
'partials/shortcodeerror.html.twig',
|
||||
[
|
||||
'errorMessage' => $errorMsg
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
// a random id for each gallery
|
||||
$id = mt_rand();
|
||||
|
||||
// give JS and CSS so that they can be cached
|
||||
$this->shortcode->addAssets('css', "plugin://youtubeconsent/assets/css/youtubeconsent.css");
|
||||
$this->shortcode->addAssets('js', "plugin://youtubeconsent/assets/js/js.cookie.min.js");
|
||||
$this->shortcode->addAssets('js', "plugin://youtubeconsent/assets/js/youtubeconsent.js");
|
||||
|
||||
// Assemble parameter string
|
||||
$parameterList = [];
|
||||
$parameters = '';
|
||||
|
||||
if ($start > 0) $parameterList[] = "start=" . $start;
|
||||
if ($quality !== '') $parameterList[] = "vq=" . $quality;
|
||||
|
||||
foreach ($parameterList as $param) {
|
||||
if (strlen($parameters) === 0)
|
||||
$parameters = "?";
|
||||
else
|
||||
$parameters = $parameters . "&";
|
||||
|
||||
$parameters = $parameters . $param;
|
||||
}
|
||||
|
||||
// Get the notice from the configuration or - if empty - use the default
|
||||
$noticeText = $pluginConfig['notice_text'];
|
||||
$acceptText = $pluginConfig['accept_text'];
|
||||
$acceptAllText = $pluginConfig['accept_all_text'];
|
||||
if (!$noticeText) {
|
||||
$noticeText = $this->grav['language']->translate('PLUGIN_YOUTUBECONSENT.DEFAULT_NOTICE');
|
||||
}
|
||||
if (!$acceptText) {
|
||||
$acceptText = $this->grav['language']->translate('PLUGIN_YOUTUBECONSENT.DEFAULT_ACCEPT');
|
||||
}
|
||||
if (!$acceptAllText) {
|
||||
$acceptAllText = $this->grav['language']->translate('PLUGIN_YOUTUBECONSENT.DEFAULT_ACCEPT_ALL');
|
||||
}
|
||||
|
||||
return $this->twig->processTemplate(
|
||||
'partials/youtubeconsent.html.twig',
|
||||
[
|
||||
'page' => $this->grav['page'], // used for image resizing
|
||||
'id' => $id,
|
||||
'videoId' => $videoId,
|
||||
'videoStart' => $start,
|
||||
'videoWidth' => $width,
|
||||
'videoHeight' => $height,
|
||||
'parameterString' => $parameters,
|
||||
'noticeForeground' => $pluginConfig['notice_foreground'],
|
||||
'noticeBackground' => $pluginConfig['notice_background'],
|
||||
'acceptLinkClass' => $pluginConfig['accept_link_class'],
|
||||
'notice' => $noticeText,
|
||||
'accept' => $acceptText,
|
||||
'acceptAll' => $acceptAllText
|
||||
]
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user