PluginProbe
HD Quiz / trunk
HD Quiz vtrunk
2.2.2 2.2.1 2.2.0 2.1.1 trunk 1.4.2 1.5.0 1.6.0 1.6.1 1.7.0 1.8.1 1.8.12 1.8.15 1.8.2 1.8.4 1.8.5 1.8.6 1.8.9 2.0.0 2.0.1 2.0.10 2.0.2 2.0.3 2.0.4 2.0.5 All 29 releases
hd-quiz / classes / settings.php

settings.php in HD Quiz trunk, at classes/settings.php

339 lines 12.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 class _hdq_settings
4 {
5 public $data = array(); // product data
6 public $fields = array();
7
8 function __construct($flat = false)
9 {
10 $this->fields();
11 $this->get($flat);
12 }
13
14 private function fields()
15 {
16
17 $ssv_addon = "";
18 if (function_exists("hdq_a_ssv_submit")) {
19 $ssv_addon = "<br/><strong>NOTE:</strong> You have the SSV Addon installed. Please deactivate that addon before enabling this to avoid conflicts.";
20 }
21
22
23 $fields = '[
24 {
25 "label": "' . esc_attr(trim(__("General", "hd-quiz"))) . '",
26 "id": "General",
27 "children": [
28 {
29 "id": "i_love_hd_quiz",
30 "label": "I ❤️ HD Quiz",
31 "required": "",
32 "default": "",
33 "tooltip": "",
34 "description": "Enable this to show your appreciation for the work I\'ve put into HD Quiz. This will add a small, subtle link to your quiz results letting users know that your quizzes were built using HD Quiz.",
35 "placeholder": "",
36 "options": [{ "label": "Yes", "value": "yes" }],
37 "type": "radio"
38 },
39 {"heading_type": "H3", "heading_content": "Social Media", "type": "heading" },
40 {
41 "column_type": "1-1",
42 "type": "column",
43 "children": [
44 {
45 "id": "allow_social_media",
46 "label": "Allow social media sharing",
47 "required": "",
48 "default": "yes",
49 "tooltip": "",
50 "description": "Disable this to remove the social media icons on share functionality from quiz results",
51 "placeholder": "",
52 "options": [{ "label": "Yes", "value": "yes" }],
53 "type": "radio"
54 },
55 {
56 "id": "enhanced_facebook",
57 "label": "Enable enhanced Facebook sharing",
58 "required": "",
59 "tooltip": "",
60 "description": "This is an *experimental feature* and will not work for all users. It works by creating a dedicated share page just for Facebook. Do not ask for support for this feature.",
61 "placeholder": "",
62 "options": [{ "label": "Yes", "value": "yes" }],
63 "type": "radio"
64 },
65 { "id": "twitter_handle", "label": "Twitter/X handle", "required": "", "default": "", "placeholder": "", "description": "do NOT include the @ symbol", "tooltip": "The sent tweet will mention your handle", "prefix": "", "postfix": "", "type": "text" }
66 ]
67 },
68 {
69 "column_type": "1-1",
70 "type": "column",
71 "children": [
72 {
73 "id": "share_text",
74 "label": "Share text [general / scored]",
75 "required": "",
76 "default": "I scored %score% on the %quiz% quiz. Can you beat me?",
77 "placeholder": "",
78 "description": "You can use custom variables here to add dynamic content. <code>%quiz%</code> will display the quiz name, and <code>%score%</code> will display the user\'s score as a fraction.",
79 "tooltip": "This is the text that will appear when a user share\'s their quiz",
80 "prefix": "",
81 "postfix": "",
82 "type": "text"
83 },
84 {
85 "id": "share_text_personality",
86 "label": "Share text [personality]",
87 "required": "",
88 "default": "I got %score% on the %quiz% quiz.",
89 "placeholder": "",
90 "description": "You can use custom variables here to add dynamic content. <code>%quiz%</code> will display the quiz name, and <code>%score%</code> will display the outcome title.",
91 "tooltip": "This is the text that will appear when a user share\'s their quiz",
92 "prefix": "",
93 "postfix": "",
94 "type": "text"
95 }
96 ]
97 },
98 { "heading_type": "H3", "heading_content": "Other Options", "type": "heading" },
99 {
100 "column_type": "1-1",
101 "type": "column",
102 "children": [
103 {
104 "id": "allow_authors_access",
105 "label": "Allow authors access to create quizzes",
106 "required": "",
107 "default": "no",
108 "tooltip": "Authors can only edit their own quizzes. Enabling this also grants the Editor role access to edit all quizzes.",
109 "description": "By default, only Admins can add or edit questions. Enabling this will allow Authors to create quizzes as well.",
110 "placeholder": "",
111 "options": [{ "label": "Yes", "value": "yes" }],
112 "type": "radio"
113 },
114 {
115 "id": "enable_percent_results",
116 "label": "Show results as percentage",
117 "required": "",
118 "default": "yes",
119 "tooltip": "",
120 "description": "Disable if you want to hide the percentage score in the results, and only show the score as a fraction.",
121 "placeholder": "",
122 "options": [{ "label": "Yes", "value": "yes" }],
123 "type": "radio"
124 },
125 {
126 "id": "replace_the_content_filter",
127 "label": "Replace <code>the_content</code> filter for quizzes",
128 "required": "",
129 "default": "yes",
130 "tooltip": "",
131 "description": "HD Quiz uses a custom version of <code>the_content</code> filter to print content to the page (extra text, and result text). This is to prevent other plugins from automatically adding their own code to quizzes. You can disable this if you want to allow other plugins to add their content again.",
132 "placeholder": "",
133 "options": [{ "label": "Yes", "value": "yes" }],
134 "type": "radio"
135 },
136 {
137 "id": "secure_mode",
138 "label": "Enable secure mode",
139 "required": "",
140 "default": "",
141 "tooltip": "This is for scored/general quizzes only, and may not have full compatibility with all features.",
142 "description": "Enable this if you want to make \"cheating\" significantly harder. This feature uses your server to validate answers, which is slower, but more secure. ' . $ssv_addon . '<small>If you are using HD Quiz for just fun little quizzes, it is not recommended to enable this</small>",
143 "placeholder": "",
144 "options": [{ "label": "Yes", "value": "yes" }],
145 "type": "radio"
146 }
147 ]
148 },
149 {
150 "id": "adset_code",
151 "label": "Adset Code",
152 "required": "",
153 "default": "",
154 "placeholder": "You can paste your ad code here",
155 "description": "<p><strong>DO NOT USE</strong> if you are already displaying auto ads on your site. HD Quiz will place the ad code after every 5th question, but how the ad appears is entirely based on the ads and ad networks themselves. Some ads do not play nice with pagination. This is because they do not like loading on the page hidden.</p><p><small><strong>NOTE</strong>: If using JavaScript, it is best to have all of your code on one line. This is to stop WordPress from adding paragraph tags where you don\'t want them</small></p>",
156 "prefix": "",
157 "postfix": "",
158 "type": "textarea_code"
159 }
160 ]
161 },
162 {
163 "label": "' . esc_attr(trim(__("Translations", "hd-quiz"))) . '",
164 "id": "Translations",
165 "children": [
166 {
167 "column_type": "1-1",
168 "type": "column",
169 "children": [
170 { "id": "translate_finish", "label": "Rename \"Finish\" Button", "required": "", "default": "' . esc_attr(trim(__("Finish", "hd-quiz"))) . '", "placeholder": "", "description": "", "tooltip": "", "prefix": "", "postfix": "", "type": "text" },
171 { "id": "translate_next", "label": "Rename \"Next\" Button", "required": "", "default": "' . esc_attr(trim(__("Next", "hd-quiz"))) . '", "placeholder": "", "description": "", "tooltip": "", "prefix": "", "postfix": "", "type": "text" },
172 { "id": "translate_previous", "label": "Rename \"Previous\" Button", "required": "", "default": "' . esc_attr(trim(__("Previous", "hd-quiz"))) . '", "placeholder": "", "description": "", "tooltip": "", "prefix": "", "postfix": "", "type": "text" },
173 { "id": "translate_results", "label": "Rename \"Results\" text", "required": "", "default": "' . esc_attr(trim(__("Results", "hd-quiz"))) . '", "placeholder": "", "description": "", "tooltip": "", "prefix": "", "postfix": "", "type": "text" },
174 {
175 "id": "translate_quiz_start",
176 "label": "Rename \"QUIZ START\" text",
177 "required": "",
178 "default": "' . esc_attr(trim(__("QUIZ START", "hd-quiz"))) . '",
179 "placeholder": "",
180 "description": "",
181 "tooltip": "Used if you are using a timer feature, or for direct links to the quiz on category/search pages",
182 "prefix": "",
183 "postfix": "",
184 "type": "text"
185 },
186 {
187 "id": "translate_enter_answer_here",
188 "label": "Rename \"enter answer here\" text",
189 "required": "",
190 "default": "' . esc_attr(trim(__("enter answer here", "hd-quiz"))) . '",
191 "placeholder": "",
192 "description": "This text appears as a placeholder for the \"Text Based Answers\" question type",
193 "tooltip": "",
194 "prefix": "",
195 "postfix": "",
196 "type": "text"
197 },
198 {
199 "id": "translate_select_all_that_apply",
200 "label": "Rename \"Select all that apply:\" text",
201 "required": "",
202 "default": "' . esc_attr(trim(__("Select all that apply:", "hd-quiz"))) . '",
203 "placeholder": "",
204 "description": "This text appears as a placeholder for the \"Select All That Apply\" question type",
205 "tooltip": "",
206 "prefix": "",
207 "postfix": "",
208 "type": "text"
209 },
210 {
211 "id": "translate_submit",
212 "label": "Rename \"Submit\" button text",
213 "required": "",
214 "default": "' . esc_attr(trim(__("Submit", "hd-quiz"))) . '",
215 "placeholder": "",
216 "description": "This button appears with the \"Select all that apply\" question type when either \"Immediately mark answer as correct or incorrect\" is enabled, or Timer per question is enabled.",
217 "tooltip": "",
218 "prefix": "",
219 "postfix": "",
220 "type": "text"
221 }
222 ]
223 }
224 ]
225 }
226 ]
227 ';
228 $data = json_decode($fields, true);
229
230 $fields = array();
231 foreach ($data as $k => $tab) {
232 $fields[$tab["id"]] = $tab;
233 }
234
235 $fields = apply_filters("hdq_add_settings_field", $fields);
236 $this->fields = $fields;
237 }
238
239 private function get($flat)
240 {
241 $data = get_option("hdq_settings", array());
242 $data = $this->mapOld($data);
243 $fields = new hdquiz\_hd_fields($this->fields, $data);
244 $data = $fields->get_values($flat);
245 $this->data = $data;
246 }
247
248 private function mapOld($data)
249 {
250 $data_clean = array();
251 if (!isset($data["hd_qu_fb"])) {
252 return $data;
253 }
254
255 $map = array(
256 "hd_qu_tw" => "twitter_handle",
257 "hd_qu_share_text" => "share_text",
258 "hd_qu_authors" => "allow_authors_access",
259 "hd_qu_percent" => "enable_percent_results",
260 "hd_qu_the_content" => "replace_the_content_filter",
261 "hd_qu_legacy_scroll" => "enable_legacy_scroll",
262 "hd_qu_heart" => "i_love_hd_quiz",
263 "hd_qu_adcode" => "adset_code",
264 "hd_qu_finish" => "translate_finish",
265 "hd_qu_next" => "translate_next",
266 "hd_qu_results" => "translate_results",
267 "hd_qu_start" => "translate_quiz_start",
268 "hd_qu_text_based_answers" => "translate_enter_answer_here",
269 "hd_qu_select_all_apply" => "translate_select_all_that_apply"
270 );
271
272 foreach ($map as $k => $setting) {
273 if (isset($data[$k]["value"])) {
274 if (is_array($data[$k]["value"])) {
275 $data[$k]["value"] = $data[$k]["value"][0];
276 }
277 $data_clean[$setting] = array("value" => $data[$k]["value"]);
278
279 if ($k === "hd_qu_adcode") {
280 $data_clean[$setting] = array("value" => $this->decode($data[$k]["value"]));
281 }
282 }
283 }
284 return $data_clean;
285 }
286
287 // old encryption function
288 private function decode($ciphertext = "")
289 {
290 if ($ciphertext === "") {
291 return "";
292 }
293 $c = base64_decode($ciphertext);
294 $cipher = "AES-128-CBC";
295 $ivlen = openssl_cipher_iv_length($cipher);
296 $iv = substr($c, 0, $ivlen);
297 $hmac = substr($c, $ivlen, $sha2len = 32);
298 $ciphertext_raw = substr($c, $ivlen + $sha2len);
299 $original_plaintext = stripslashes(openssl_decrypt($ciphertext_raw, $cipher, wp_salt(), OPENSSL_RAW_DATA, $iv));
300 $calcmac = hash_hmac('sha256', $ciphertext_raw, wp_salt(), true);
301 if (@hash_equals($hmac, $calcmac)) {
302 return $original_plaintext;
303 } else {
304 return "";
305 }
306 }
307
308 public function save($data)
309 {
310 if (!current_user_can("manage_options")) {
311 $res = new stdClass();
312 $res->status = "fail";
313 $res->message = "Permission denied";
314 return $res;
315 }
316
317 $data = new hdquiz\_hd_fields($this->fields, $data, "horizontal");
318 $data = $data->get_values(false);
319
320 update_option("hdq_settings", $data);
321
322 $res = new stdClass();
323 $res->status = "success";
324 return $res;
325 }
326
327 public function display()
328 {
329 if (!current_user_can("manage_options")) {
330 die();
331 }
332
333 wp_enqueue_style("hdfields", plugins_url('/../hdfields/style.css', __FILE__), array(), HDFIELDS_VERSION);
334 wp_enqueue_script("hdfields", plugins_url('/../hdfields/script.js', __FILE__), array(), HDFIELDS_VERSION);
335 $fields = new hdquiz\_hd_fields($this->fields, $this->data, "vertical");
336 echo $fields->display();
337 }
338 }
339