PluginProbe
The GDPR Framework By Data443 / trunk
The GDPR Framework By Data443 vtrunk
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 trunk, at src/Modules/WPML/WPML.php

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