praiadeseselle/site/modules/PrivacyWire/TextformatterPrivacyWire.module
2024-04-16 11:51:21 +02:00

68 lines
2.7 KiB
PHP

<?php namespace ProcessWire;
/**
* Textformatter PrivacyWire
* This module adds the textformatter for PrivacyWire
*
*
* @author blaueQuelle
*
* ProcessWire 3.x
* Copyright (C) 2011 by Ryan Cramer
* Licensed under GNU/GPL v2, see LICENSE.TXT
*
* http://www.processwire.com
* http://www.ryancramer.com
*
*/
class TextformatterPrivacyWire extends Textformatter implements Module
{
public static function getModuleInfo()
{
return [
'title' => 'PrivacyWire Textformatter',
'summary' => "PrivacyWire Textformatter to render privacy options via shortcode [[privacywire-choose-cookies]]",
'author' => 'blaueQuelle',
'href' => "https://github.com/blaueQuelle/privacywire",
'version' => 10,
'requires' => [
"PHP>=7.2",
"ProcessWire>=3.0.110"
],
];
}
/**
* Formats the given $str reference.
* Page and Field context are currently not necessary for the formatter to work.
* The formatter can be called via format(&$str) or formatValue(Page $page, Field $field, &$value)
* formatValue(Page $page, Field $field, &$value) internally calls format(&$str), so the former does not need to be overwritten.
* @param mixed $str
* @return void
*/
public function format(&$str)
{
// Replace privacywire-choose-cookies with button element
$tag_search = $this->open_tag . "privacywire-choose-cookies" . $this->close_tag;
if (strpos($str, $tag_search) !== false) {
$privacyWire = $this->modules->get("PrivacyWire");
// Multi Language Support
$lang = ($this->wire('languages') && !$this->wire('user')->language->isDefault()) ? '__' . $this->wire('user')->language->id : '';
$tag_replace = "<button class='button privacywire-show-options'>{$privacyWire->get("textformatter_choose_label$lang|textformatter_choose_label")}</button>";
$str = str_replace($tag_search, $tag_replace, $str);
}
// Optional: enable PrivacyWire support for embedded media
if ($this->video_category && (strpos($str, 'www.youtube.com/embed/') !== false || strpos($str, 'www.youtube-nocookie.com/embed/') !== false || strpos($str, 'player.vimeo.com') !== false)) {
if (preg_match_all('/\<iframe.*?src=("|\')(?:https?:)\/\/(?:(?:www\.)?youtube-nocookie|(?:www\.)?youtube|player\.vimeo)\..*?\1.*?\<\/iframe\>/is', $str, $matches)) {
foreach ($matches[0] as $match) {
$new_match = str_replace(' src=', ' data-category="' . $this->video_category . '" data-ask-consent=1 data-src=', $match);
$str = str_replace($match, $new_match, $str);
}
}
}
}
}