Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 57 additions & 8 deletions library/AcfFields/json/options-theme-custom-js.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"key": "field_5731849ed0729",
"label": "JavaScript",
"name": "custom_js_input",
"aria-label": "",
"type": "textarea",
"instructions": "Skriv in din JS-kod h\u00e4r. Den kommer att omslutas med SCRIPT taggar och inkluderas p\u00e5 samtliga sidor.",
"required": 0,
Expand All @@ -21,12 +22,14 @@
"placeholder": "",
"rows": 20,
"readonly": 0,
"disabled": 0
"disabled": 0,
"acfe_textarea_code": 0
},
{
"key": "field_5d9b4091e1fa0",
"label": "Script Tags",
"name": "custom_js_tags",
"aria-label": "",
"type": "repeater",
"instructions": "Enter your JS-code here. It will NOT be wrapped with \"script\" tags and included on all pages.",
"required": 0,
Expand All @@ -36,16 +39,20 @@
"class": "",
"id": ""
},
"collapsed": "",
"acfe_repeater_stylised_button": 0,
"layout": "row",
"pagination": 0,
"min": 0,
"max": 0,
"layout": "row",
"button_label": "",
"collapsed": "",
"button_label": "Add Tag",
"rows_per_page": 20,
"sub_fields": [
{
"key": "field_5d9b40f4e1fa2",
"label": "JavaScript",
"name": "custom_js_tags_input",
"aria-label": "",
"type": "textarea",
"instructions": "Enter your Script tags here. eg. <script>console.log('hello world');<\/script>",
"required": 1,
Expand All @@ -59,12 +66,15 @@
"placeholder": "",
"maxlength": "",
"rows": 5,
"new_lines": ""
"new_lines": "",
"acfe_textarea_code": 0,
"parent_repeater": "field_5d9b4091e1fa0"
},
{
"key": "field_5d9b428dd29cf",
"label": "Location",
"name": "custom_js_tags_location",
"aria-label": "",
"type": "select",
"instructions": "",
"required": 1,
Expand All @@ -84,12 +94,44 @@
"ui": 0,
"return_format": "value",
"ajax": 0,
"placeholder": ""
"placeholder": "",
"allow_custom": 0,
"search_placeholder": "",
"parent_repeater": "field_5d9b4091e1fa0"
},
{
"key": "field_68274e62bc633",
"label": "Required cookie consents",
"name": "custom_js_tags_cookie_consents",
"aria-label": "",
"type": "checkbox",
"instructions": "What additional consents are required for this script to be run?",
"required": 0,
"conditional_logic": 0,
"wrapper": {
"width": "",
"class": "",
"id": ""
},
"choices": {
"analytics": "Performance & Analytics",
"targeting": "Advertisement & Targeting",
"preferences": "Functionality & Preferences"
},
"default_value": [],
"return_format": "value",
"allow_custom": 0,
"layout": "horizontal",
"toggle": 0,
"save_custom": 0,
"custom_choice_button_text": "Add new choice",
"parent_repeater": "field_5d9b4091e1fa0"
},
{
"key": "field_5d9b4175e1fa3",
"label": "Disable script",
"name": "custom_js_tags_disabled",
"aria-label": "",
"type": "true_false",
"instructions": "",
"required": 0,
Expand All @@ -103,7 +145,8 @@
"default_value": 0,
"ui": 1,
"ui_on_text": "",
"ui_off_text": ""
"ui_off_text": "",
"parent_repeater": "field_5d9b4091e1fa0"
}
]
}
Expand All @@ -124,6 +167,12 @@
"instruction_placement": "label",
"hide_on_screen": "",
"active": true,
"description": ""
"description": "",
"show_in_rest": 0,
"acfe_display_title": "",
"acfe_autosync": "",
"acfe_form": 0,
"acfe_meta": "",
"acfe_note": ""
}]

60 changes: 55 additions & 5 deletions library/AcfFields/php/options-theme-custom-js.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@
if (function_exists('acf_add_local_field_group')) {
acf_add_local_field_group(array(
'key' => 'group_573184999aa2c',
'title' => 'Custom JS',
'title' => __('Custom JS', 'municipio'),
'fields' => array(
0 => array(
'key' => 'field_5731849ed0729',
'label' => __('JavaScript', 'municipio'),
'name' => 'custom_js_input',
'aria-label' => '',
'type' => 'textarea',
'instructions' => __('Skriv in din JS-kod här. Den kommer att omslutas med SCRIPT taggar och inkluderas på samtliga sidor.', 'municipio'),
'required' => 0,
Expand All @@ -25,11 +26,13 @@
'rows' => 20,
'readonly' => 0,
'disabled' => 0,
'acfe_textarea_code' => 0,
),
1 => array(
'key' => 'field_5d9b4091e1fa0',
'label' => __('Script Tags', 'municipio'),
'name' => 'custom_js_tags',
'aria-label' => '',
'type' => 'repeater',
'instructions' => __('Enter your JS-code here. It will NOT be wrapped with "script" tags and included on all pages.', 'municipio'),
'required' => 0,
Expand All @@ -39,16 +42,20 @@
'class' => '',
'id' => '',
),
'collapsed' => '',
'acfe_repeater_stylised_button' => 0,
'layout' => 'row',
'pagination' => 0,
'min' => 0,
'max' => 0,
'layout' => 'row',
'button_label' => '',
'collapsed' => '',
'button_label' => __('Add Tag', 'municipio'),
'rows_per_page' => 20,
'sub_fields' => array(
0 => array(
'key' => 'field_5d9b40f4e1fa2',
'label' => __('JavaScript', 'municipio'),
'name' => 'custom_js_tags_input',
'aria-label' => '',
'type' => 'textarea',
'instructions' => __('Enter your Script tags here. eg. <script>console.log(\'hello world\');</script>', 'municipio'),
'required' => 1,
Expand All @@ -63,11 +70,14 @@
'maxlength' => '',
'rows' => 5,
'new_lines' => '',
'acfe_textarea_code' => 0,
'parent_repeater' => 'field_5d9b4091e1fa0',
),
1 => array(
'key' => 'field_5d9b428dd29cf',
'label' => __('Location', 'municipio'),
'name' => 'custom_js_tags_location',
'aria-label' => '',
'type' => 'select',
'instructions' => '',
'required' => 1,
Expand All @@ -81,18 +91,51 @@
'head' => __('Include script in head', 'municipio'),
'footer' => __('Include script in footer', 'municipio'),
),
'default_value' => 'footer',
'default_value' => __('footer', 'municipio'),
'allow_null' => 0,
'multiple' => 0,
'ui' => 0,
'return_format' => 'value',
'ajax' => 0,
'placeholder' => '',
'allow_custom' => 0,
'search_placeholder' => '',
'parent_repeater' => 'field_5d9b4091e1fa0',
),
2 => array(
'key' => 'field_68274e62bc633',
'label' => __('Required cookie consents', 'municipio'),
'name' => 'custom_js_tags_cookie_consents',
'aria-label' => '',
'type' => 'checkbox',
'instructions' => __('What additional consents are required for this script to be run?', 'municipio'),
'required' => 0,
'conditional_logic' => 0,
'wrapper' => array(
'width' => '',
'class' => '',
'id' => '',
),
'choices' => array(
'analytics' => __('Performance & Analytics', 'municipio'),
'targeting' => __('Advertisement & Targeting', 'municipio'),
'preferences' => __('Functionality & Preferences', 'municipio'),
),
'default_value' => array(
),
'return_format' => 'value',
'allow_custom' => 0,
'layout' => 'horizontal',
'toggle' => 0,
'save_custom' => 0,
'custom_choice_button_text' => 'Add new choice',
'parent_repeater' => 'field_5d9b4091e1fa0',
),
3 => array(
'key' => 'field_5d9b4175e1fa3',
'label' => __('Disable script', 'municipio'),
'name' => 'custom_js_tags_disabled',
'aria-label' => '',
'type' => 'true_false',
'instructions' => '',
'required' => 0,
Expand All @@ -107,6 +150,7 @@
'ui' => 1,
'ui_on_text' => '',
'ui_off_text' => '',
'parent_repeater' => 'field_5d9b4091e1fa0',
),
),
),
Expand All @@ -128,5 +172,11 @@
'hide_on_screen' => '',
'active' => true,
'description' => '',
'show_in_rest' => 0,
'acfe_display_title' => '',
'acfe_autosync' => '',
'acfe_form' => 0,
'acfe_meta' => '',
'acfe_note' => '',
));
}
Expand Down
19 changes: 19 additions & 0 deletions library/Admin/Acf/CustomJsCookieConsent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php
namespace Municipio\Admin\Acf;

use Municipio\Helper\CookieConsent;

class CustomJsCookieConsent
{
public function __construct()
{
// Hide additional consent fields if the Pressidium plugin is not enabled
if (is_admin() && !CookieConsent::pressidiumIsEnabled()) {
add_filter('acf/load_field/name=custom_js_tags_cookie_consents', function ($field) {
$field['wrapper']['class'] = 'hidden';

return $field;
});
}
}
}
1 change: 1 addition & 0 deletions library/App.php
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,7 @@ public function __construct(

new \Municipio\Admin\Acf\PrefillIconChoice($this->wpService);
new \Municipio\Admin\Acf\ImageAltTextValidation();
new \Municipio\Admin\Acf\CustomJsCookieConsent();

new \Municipio\Admin\Roles\General($this->wpService);
new \Municipio\Admin\Roles\Editor($userHelper);
Expand Down
82 changes: 82 additions & 0 deletions library/Helper/CookieConsent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
<?php
namespace Municipio\Helper;

class CookieConsent
{
private static bool|array $cookieConsent;

/**
* Get the cookie consents from the Pressidium plugin.
*
* @return array|false The consents or false if not set.
*/
public static function getConsents()
{
if (self::pressidiumIsEnabled()) {
$consents = isset($_COOKIE['pressidium_cookie_consent'])
? json_decode(stripslashes($_COOKIE['pressidium_cookie_consent']), true)
: [];
return self::parsePressidiumConsents($consents);
}

return false;
}

/**
* Check if the required consents are present.
*
* @param array $requiredConsents The required consents.
* @return bool True if all required consents are present, false otherwise.
*/
public static function hasConsents(array $requiredConsents)
{
if (!isset(self::$cookieConsent)) {
self::$cookieConsent = self::getConsents();
}

if (self::$cookieConsent === false) {
return true;
}

foreach ($requiredConsents as $type) {
if (!isset(self::$cookieConsent[$type]) || !self::$cookieConsent[$type]) {
return false;
}
}

return true;
}

/**
* Check if the Pressidium cookie consent plugin is enabled.
*
* @return bool True if the plugin is enabled, false otherwise.
*/
public static function pressidiumIsEnabled()
{
return class_exists('Pressidium\WP\CookieConsent\Plugin')
&& is_plugin_active('pressidium-cookie-consent/pressidium-cookie-consent.php');
}

/**
* Parse the Pressidium cookie consent data.
*
* @param array $data The cookie consent data.
* @return array The parsed consents.
*/
private static function parsePressidiumConsents($data)
{
$consents = [
'necessary' => false,
'preferences' => false,
'analytics' => false,
'targeting' => false,
];

foreach ($data['categories'] as $type) {
$consents[$type] = true;
}

return $consents;
}
}
Loading