-
Notifications
You must be signed in to change notification settings - Fork 3
/
functions.php
510 lines (436 loc) · 12.5 KB
/
functions.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
<?php
defined( 'ABSPATH' ) or die( "you do not have acces to this page!" );
if ( ! function_exists( 'cmplz_tc_get_template' ) ) {
/**
* Get a template based on filename, overridable in theme dir
* @param string $filename
* @param array $args
* @return string
*/
function cmplz_tc_get_template( $filename , $args = array() ) {
$file = apply_filters('cmplz_tc_template_file', trailingslashit( cmplz_tc_path ) . 'templates/' . $filename, $filename);
$theme_file = trailingslashit( get_stylesheet_directory() )
. trailingslashit( basename( cmplz_tc_path ) )
. 'templates/' . $filename;
if ( !file_exists( $file ) ) {
return false;
}
if ( file_exists( $theme_file ) ) {
$file = $theme_file;
}
if ( strpos( $file, '.php' ) !== false ) {
ob_start();
require $file;
$contents = ob_get_clean();
} else {
$contents = file_get_contents( $file );
}
if ( !empty($args) && is_array($args) ) {
foreach($args as $fieldname => $value ) {
$contents = str_replace( '{'.$fieldname.'}', $value, $contents );
}
}
return $contents;
}
}
if ( !function_exists('cmplz_tc_settings_page') ) {
function cmplz_tc_settings_page(){
if (!defined('cmplz_version')) {
return add_query_arg(array('page' => 'terms-conditions'), admin_url('tools.php') );
} else {
return add_query_arg(array('page' => 'terms-conditions'), admin_url('admin.php') );
}
}
}
if ( ! function_exists( 'cmplz_tc_get_value' ) ) {
/**
* Get value for an a complianz option
* For usage very early in the execution order, use the $page option. This bypasses the class usage.
*
* @param string $fieldname
* @param bool|string $page
* @param bool $use_default
* @param bool $use_translate
*
* @return array|bool|mixed|string
*/
function cmplz_tc_get_value( $fieldname, $page = false, $use_default = true, $use_translate = true ) {
if ( ! $page && ! isset( COMPLIANZ_TC::$config->fields[ $fieldname ] ) ) {
return false;
}
if ( ! $page ) {
$page = COMPLIANZ_TC::$config->fields[ $fieldname ]['source'];
}
$fields = get_option( 'complianz_tc_options_' . $page );
$default = ( $use_default && $page && isset( COMPLIANZ_TC::$config->fields[ $fieldname ]['default'] ) ) ? COMPLIANZ_TC::$config->fields[ $fieldname ]['default'] : '';
$value = isset( $fields[ $fieldname ] ) ? $fields[ $fieldname ] : $default;
/**
* Translate output
*
* */
if ($use_translate) {
if (isset(COMPLIANZ_TC::$config->fields[$fieldname]['translatable'])
&& COMPLIANZ_TC::$config->fields[$fieldname]['translatable']
) {
if (function_exists('pll__')) {
$value = pll__($value);
}
if (function_exists('icl_translate')) {
$value = icl_translate('complianz', $fieldname, $value);
}
$value = apply_filters('wpml_translate_single_string', $value, 'complianz', $fieldname);
}
}
return $value;
}
}
if ( ! function_exists( 'cmplz_tc_intro' ) ) {
/**
* @param string $msg
*
* @return string|void
*/
function cmplz_tc_intro( $msg ) {
if ( $msg == '' ) {
return;
}
$html = "<div class='cmplz-panel cmplz-notification cmplz-intro'>{$msg}</div>";
echo $html;
}
}
if ( ! function_exists( 'cmplz_tc_notice' ) ) {
/**
* Notification without arrow on the left. Should be used outside notifications center
* @param string $msg
* @param string $type notice | warning | success
* @param bool $remove_after_change
* @param bool $echo
* @param array $condition $condition['question'] $condition['answer']
*
* @return string|void
*/
function cmplz_tc_notice( $msg, $type = 'notice', $remove_after_change = false, $echo = true, $condition = false) {
if ( $msg == '' ) {
return;
}
// Condition
$condition_check = "";
$condition_question = "";
$condition_answer = "";
$cmplz_hidden = "";
if ($condition) {
$condition_check = "condition-check";
$condition_question = "data-condition-question='{$condition['question']}'";
$condition_answer = "data-condition-answer='{$condition['answer']}'";
$args['condition'] = array($condition['question'] => $condition['answer']);
$cmplz_hidden = cmplz_field::this()->condition_applies($args) ? "" : "cmplz-hidden";;
}
// Hide
$remove_after_change_class = $remove_after_change ? "cmplz-remove-after-change" : "";
$html = "<div class='cmplz-panel-wrap'><div class='cmplz-panel cmplz-notification cmplz-{$type} {$remove_after_change_class} {$cmplz_hidden} {$condition_check}' {$condition_question} {$condition_answer}><div>{$msg}</div></div></div>";
if ( $echo ) {
echo $html;
} else {
return $html;
}
}
}
if ( ! function_exists( 'cmplz_tc_sidebar_notice' ) ) {
/**
* @param string $msg
* @param string $type notice | warning | success
* @param bool $remove_after_change
* @param bool $echo
* @param bool|array $condition $condition['question'] $condition['answer']
*
* @return string|void
*/
function cmplz_tc_sidebar_notice( $msg, $type = 'notice', $remove_after_change = false, $echo = true, $condition = false) {
if ( $msg == '' ) {
return;
}
// Condition
$condition_check = "";
$condition_question = "";
$condition_answer = "";
$cmplz_hidden = "";
if ($condition) {
$condition_check = "condition-check";
$condition_question = "data-condition-question='{$condition['question']}'";
$condition_answer = "data-condition-answer='{$condition['answer']}'";
$args['condition'] = array($condition['question'] => $condition['answer']);
$cmplz_hidden = cmplz_field::this()->condition_applies($args) ? "" : "cmplz-hidden";;
}
// Hide
$remove_after_change_class = $remove_after_change ? "cmplz-remove-after-change" : "";
$html = "<div class='cmplz-help-modal cmplz-notice cmplz-{$type} {$remove_after_change_class} {$cmplz_hidden} {$condition_check}' {$condition_question} {$condition_answer}>{$msg}</div>";
if ( $echo ) {
echo $html;
} else {
return $html;
}
}
}
if ( ! function_exists( 'cmplz_tc_localize_date' ) ) {
function cmplz_tc_localize_date( $date ) {
$month = date( 'F', strtotime( $date ) ); //june
$month_localized = __( $month ); //juni
$date = str_replace( $month, $month_localized, $date );
$weekday = date( 'l', strtotime( $date ) ); //wednesday
$weekday_localized = __( $weekday ); //woensdag
$date = str_replace( $weekday, $weekday_localized, $date );
return $date;
}
}
if (!function_exists('cmplz_tc_read_more')) {
/**
* Create a generic read more text with link for help texts.
*
* @param string $url
* @param bool $add_space
*
* @return string
*/
function cmplz_tc_read_more( $url, $add_space = true ) {
$html
= sprintf( __("For more information on this subject, please read this %sarticle%s.",
'complianz-terms-conditions' ), '<a target="_blank" href="' . $url . '">',
'</a>' );
if ( $add_space ) {
$html = ' ' . $html;
}
return $html;
}
}
if ( ! function_exists( 'cmplz_tc_get_regions' ) ) {
/**
* At this moment, only one document, for all regions
* @return array
*/
function cmplz_tc_get_regions() {
$output['all'] = __( 'All regions', 'complianz-terms-conditions' );
return $output;
}
}
register_activation_hook( __FILE__, 'cmplz_tc_set_activation_time_stamp' );
if ( ! function_exists( 'cmplz_tc_set_activation_time_stamp' ) ) {
function cmplz_tc_set_activation_time_stamp( $networkwide ) {
update_option( 'cmplz_tc_activation_time', time() );
}
}
if ( ! function_exists( 'cmplz_tc_allowed_html' ) ) {
function cmplz_tc_allowed_html() {
$allowed_tags = array(
'a' => array(
'class' => array(),
'href' => array(),
'rel' => array(),
'title' => array(),
'target' => array(),
'id' => array(),
),
'button' => array(
'id' => array(),
'class' => array(),
'href' => array(),
'rel' => array(),
'title' => array(),
'target' => array(),
),
'b' => array(),
'br' => array(),
'blockquote' => array(
'cite' => array(),
),
'div' => array(
'class' => array(),
'id' => array(),
),
'h1' => array(),
'h2' => array(),
'h3' => array(),
'h4' => array(),
'h5' => array(),
'h6' => array(),
'i' => array(),
'input' => array(
'type' => array(),
'class' => array(),
'id' => array(),
'required' => array(),
'value' => array(),
'placeholder' => array(),
'data-category' => array(),
'style' => array(
'color' => array(),
), ),
'img' => array(
'alt' => array(),
'class' => array(),
'height' => array(),
'src' => array(),
'width' => array(),
),
'label' => array(
'for' => array(),
'class' => array(),
'style' => array(
'visibility' => array(),
),
),
'li' => array(
'class' => array(),
'id' => array(),
),
'ol' => array(
'class' => array(),
'id' => array(),
),
'p' => array(
'class' => array(),
'id' => array(),
),
'span' => array(
'class' => array(),
'title' => array(),
'style' => array(
'color' => array(),
'display' => array(),
),
'id' => array(),
),
'strong' => array(),
'table' => array(
'class' => array(),
'id' => array(),
),
'tr' => array(),
'svg' => array(
'width' => array(),
'height' => array(),
'viewBox' => array(),
),
'polyline' => array(
'points' => array(),
),
'path' => array(
'd' => array(),
),
'style' => array(),
'td' => array( 'colspan' => array(), 'scope' => array() ),
'th' => array( 'scope' => array() ),
'ul' => array(
'class' => array(),
'id' => array(),
),
);
return apply_filters( "cmplz_tc_allowed_html", $allowed_tags );
}
}
/**
* Check if this field is translatable
*
* @param $fieldname
*
* @return bool
*/
if ( ! function_exists( 'cmplz_tc_translate' ) ) {
function cmplz_tc_translate( $value, $fieldname ) {
if ( function_exists( 'pll__' ) ) {
$value = pll__( $value );
}
if ( function_exists( 'icl_translate' ) ) {
$value = icl_translate( 'complianz', $fieldname, $value );
}
$value = apply_filters( 'wpml_translate_single_string', $value, 'complianz', $fieldname );
return $value;
}
}
if ( ! function_exists( 'cmplz_tc_sanitize_language' ) ) {
/**
* Validate a language string
*
* @param $language
*
* @return bool|string
*/
function cmplz_tc_sanitize_language( $language ) {
$pattern = '/^[a-zA-Z]{2}$/';
if ( ! is_string( $language ) ) {
return false;
}
$language = substr( $language, 0, 2 );
if ( (bool) preg_match( $pattern, $language ) ) {
$language = strtolower( $language );
return $language;
}
return false;
}
}
if ( ! function_exists( 'cmplz_tcf_creative_commons' ) ) {
/**
* Check if creative commons applies
*
*
* @return bool
*/
function cmplz_tcf_creative_commons() {
$type = cmplz_tc_get_value('about_copyright');
if ( $type === 'allrights' || $type === 'norights') {
return false;
} else {
return true;
}
}
}
if ( ! function_exists( 'cmplz_tcf_nuts' ) ) {
/**
* Check if nuts applies
*
*
* @return bool
*/
function cmplz_tcf_nuts() {
$services = cmplz_tc_get_value('about_returns') === 'nuts_services';
$utilities = cmplz_tc_get_value('about_returns') === 'nuts_utilities';
if ( $services || $utilities ) {
return true;
} else {
return false;
}
}
}
if ( ! function_exists( 'cmplz_tc_uses_gutenberg' ) ) {
function cmplz_tc_uses_gutenberg() {
if ( function_exists( 'has_block' )
&& ! class_exists( 'Classic_Editor' )
) {
return true;
}
return false;
}
}
if ( ! function_exists( 'cmplz_tc_user_can_manage' ) ) {
function cmplz_tc_user_can_manage() {
if ( ! is_user_logged_in() ) {
return false;
}
if ( ! current_user_can( 'manage_options' ) ) {
return false;
}
return true;
}
}
if ( ! function_exists( 'cmplz_tc_array_filter_multidimensional' ) ) {
function cmplz_tc_array_filter_multidimensional(
$array, $filter_key, $filter_value
) {
$new = array_filter( $array,
function ( $var ) use ( $filter_value, $filter_key ) {
return isset( $var[ $filter_key ] ) ? ( $var[ $filter_key ]
== $filter_value )
: false;
} );
return $new;
}
}