PluginProbe
Simple Analytics / 1.110
Simple Analytics v1.110
1.110 1.109 1.108 1.107 1.106 1.73 1.74 1.75 1.76 1.77 1.78 1.79 1.8 1.80 1.81 1.82 1.83 1.84 1.85 1.86 1.87 1.88 1.89 1.9 1.90 All 99 releases
simpleanalytics / src / Settings / Blocks / Fields / IpList.php

IpList.php in Simple Analytics 1.110, at src/Settings/Blocks/Fields/IpList.php

86 lines 2.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace SimpleAnalytics\Settings\Blocks\Fields;
4
5 use SimpleAnalytics\Setting;
6 use SimpleAnalytics\Settings\Concerns\HasDocs;
7 use SimpleAnalytics\Settings\Concerns\HasPlaceholder;
8 use SimpleAnalytics\UI\LabelComponent;
9
10 class IpList extends Field
11 {
12 use HasDocs;
13 use HasPlaceholder;
14
15 public function getValueSanitizer(): callable
16 {
17 return function ($value) {
18 $ips = is_array($value) ? $value : (is_string($value) ? explode("\n", $value) : []);
19 $ips = array_filter($ips, 'is_string');
20 $ips = array_map('trim', $ips);
21 $ips = array_filter($ips, function ($ip) {
22 return filter_var($ip, FILTER_VALIDATE_IP) !== false;
23 });
24 $ips = array_unique($ips);
25 return array_values($ips);
26 };
27 }
28
29 public function getValueType(): string
30 {
31 return 'array';
32 }
33
34 public function render(): void
35 {
36 $value = implode("\n", Setting::array($this->getKey()));
37 $currentIp = $_SERVER['REMOTE_ADDR'];
38 ?>
39 <?php
40 (new LabelComponent(
41 $this->getLabel(),
42 $this->docs,
43 esc_attr($this->getKey()))
44 )();
45 ?>
46 <div class="mt-2">
47 <textarea
48 name="<?php
49 echo esc_attr($this->getKey());
50 ?>"
51 id="<?php
52 echo esc_attr($this->getKey());
53 ?>"
54 rows="5"
55 class="block w-full rounded-md border-0 text-gray-900 shadow-sm ring-1 ring-inset ring-gray-300 placeholder:text-gray-400 focus:ring-2 focus:ring-inset focus:ring-primary sm:py-1.5 sm:text-sm sm:leading-6"
56 <?php
57 if ($this->placeholder): ?>
58 placeholder="<?php echo esc_attr($this->placeholder); ?>"
59 <?php endif;
60 ?>
61 ><?php
62 echo esc_textarea($value);
63 ?></textarea>
64 </div>
65 <div class="mt-2">
66 <button
67 type="button"
68 onclick="sa_textarea_add_value(this.form.elements['<?php
69 echo esc_js($this->getKey());
70 ?>'], '<?php
71 echo esc_js($currentIp);
72 ?>')"
73 class="rounded bg-white px-2 py-1 text-xs font-semibold text-gray-900 shadow-sm ring-1 ring-inset ring-gray-300 hover:bg-gray-50"
74 >
75 Add Current IP (<?php
76 echo esc_html($currentIp);
77 ?>)
78 </button>
79 </div>
80 <p class="mt-2 text-sm text-gray-500">
81 Enter IP addresses to exclude from tracking, one per line.
82 </p>
83 <?php
84 }
85 }
86