PluginProbe
The GDPR Framework By Data443 / 2.1.0
The GDPR Framework By Data443 v2.1.0
2.5.0 2.4.0 trunk 1.0.0 1.0.1 1.0.10 1.0.11 1.0.12 1.0.13 1.0.14 1.0.3 1.0.33 1.0.34 1.0.35 1.0.36 1.0.37 1.0.39 1.0.4 1.0.40 1.0.41 1.0.42 1.0.43 1.0.44 1.0.45 1.0.46 All 41 releases
gdpr-framework / src / Modules / WPML / WPML.php

WPML.php in The GDPR Framework By Data443 2.1.0, at src/Modules/WPML/WPML.php

155 lines 5.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace Codelight\GDPR\Modules\WPML;
4
5 class WPML
6 {
7 protected $prefix = 'gdpr_';
8
9 protected $translatableOptions = [
10 'tools_page',
11 'policy_page',
12 'terms_page',
13 'consent_info',
14 ];
15
16 public function __construct()
17 {
18 if (!class_exists('Sitepress')) {
19 return;
20 }
21
22 $this->setupOptionFilters();
23 }
24
25 protected function setupOptionFilters()
26 {
27 foreach ($this->translatableOptions as $option) {
28 add_filter("gdpr/options/get/{$option}", [$this, 'getTranslatedOption']);
29
30 $option = $this->prefix($option);
31 add_filter("pre_update_option_{$option}", [$this, 'setTranslatedOption'], 10, 2);
32 }
33
34 add_filter('gdpr/options/get/consent_types', [$this, 'getConsentTypes']);
35 add_filter('gdpr/options/set/consent_types', [$this, 'saveConsentTypes']);
36 }
37
38 public function getTranslatedOption($option)
39 {
40 if (!defined('ICL_LANGUAGE_CODE')) {
41 if (is_array($option)) {
42 return '';
43 } else {
44 return $option;
45 }
46 }
47
48 if (isset($option[(string)ICL_LANGUAGE_CODE])) {
49 return $option[(string)ICL_LANGUAGE_CODE];
50 } else {
51 return '';
52 }
53 }
54
55 public function setTranslatedOption($newValue, $oldValue)
56 {
57 if (!defined('ICL_LANGUAGE_CODE')) {
58 return $newValue;
59 }
60
61 if (is_array($oldValue)) {
62 $value = $oldValue;
63 } else {
64 $value = [];
65 }
66
67 $value[(string)ICL_LANGUAGE_CODE] = $newValue;
68
69 return $value;
70 }
71
72 public function getConsentTypes($consentTypes)
73 {
74 if (!defined('ICL_LANGUAGE_CODE')) {
75 return $consentTypes;
76 }
77
78 $code = (string)ICL_LANGUAGE_CODE;
79 $filteredConsentTypes = [];
80
81 if (isset($consentTypes) && !empty($consentTypes) && count($consentTypes)) {
82 foreach ($consentTypes as $consentType) {
83
84 if (isset($consentType['slug']) && ('privacy-policy' === $consentType['slug'] or 'terms-condition' === $consentType['slug'])) {
85 $filteredConsentTypes[] = [
86 'slug' => isset($consentType['slug']) ? $consentType['slug'] : '',
87 'visible' => isset($consentType['visible']) ? $consentType['visible'] : 0,
88 'title' => isset($consentType["title"]) ? $consentType["title"] : '',
89 'description' => isset($consentType["description"]) ? $consentType["description"] : '',
90 ];
91 } else {
92 $filteredConsentTypes[] = [
93 'slug' => isset($consentType['slug']) ? $consentType['slug'] : '',
94 'visible' => isset($consentType['visible']) ? $consentType['visible'] : 0,
95 'title' => isset($consentType["{$code}_title"]) ? $consentType["{$code}_title"] : '',
96 'description' => isset($consentType["{$code}_description"]) ? $consentType["{$code}_description"] : '',
97 ];
98 }
99 }
100 }
101
102 return $filteredConsentTypes;
103 }
104
105 public function saveConsentTypes($newConsentTypes)
106 {
107 global $gdpr;
108 if (!defined('ICL_LANGUAGE_CODE')) {
109 return $newConsentTypes;
110 }
111
112 $code = (string)ICL_LANGUAGE_CODE;
113 $translatedConsentTypes = [];
114 $currentConsentTypes = $gdpr->Options->get('consent_types', null, false);
115
116 if (count($newConsentTypes)) {
117 foreach ($newConsentTypes as &$newConsentType) {
118
119 // Match an existing consent type with the new one
120 $slug = $newConsentType['slug'];
121 $existingConsentType = current(array_filter($currentConsentTypes, function($value) use ($slug) {
122 return $value['slug'] === $slug;
123 }));
124
125 if ($existingConsentType) {
126 // We have a matching existing consent - update translations, keep the rest
127 $existingConsentType["{$code}_title"] = sanitize_text_field($newConsentType['title']);
128 $existingConsentType["{$code}_description"] = sanitize_text_field($newConsentType['description']);
129
130 $translatedConsentTypes[] = $existingConsentType;
131 } else {
132 // We do not have a matching existin consent - just adjust the keys for language
133 $newConsentType["{$code}_title"] = sanitize_text_field($newConsentType['title']);
134 $newConsentType["{$code}_description"] = sanitize_text_field($newConsentType['description']);
135 unset($newConsentType['title']);
136 unset($newConsentType['description']);
137
138 $translatedConsentTypes[] = $newConsentType;
139 }
140 }
141 }
142
143 return $translatedConsentTypes;
144 }
145
146 public function prefix($name)
147 {
148 if (0 === strpos($name, $this->prefix)) {
149 return $name;
150 }
151
152 return $this->prefix . $name;
153 }
154 }
155