PluginProbe
Double Opt-In for Contact Form 7 – Secure, GDPR-Compliant Email Verification / 5.1.5
Double Opt-In for Contact Form 7 – Secure, GDPR-Compliant Email Verification v5.1.5
5.6.2 5.6.3 5.6.1 5.6.0 5.5.0 5.4.0 5.3.2 5.3.1 5.1.6 5.1.5 trunk 2.1.5 2.11 2.12 2.13 2.15 3.0.0 3.0.1 3.0.2 3.0.3 3.0.5 3.0.51 3.0.60 3.0.61 3.0.62 All 38 releases
double-opt-in / core / telemetry.php

telemetry.php in Double Opt-In for Contact Form 7 – Secure, GDPR-Compliant Email Verification 5.1.5, at core/telemetry.php

175 lines 4.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace forge12\contactform7\CF7DoubleOptIn;
4
5 use Exception;
6 use Forge12\Shared\Logger;
7 use Forge12\Shared\LoggerInterface;
8
9 if (!class_exists(__NAMESPACE__ . '\\Telemetry')) {
10 class Telemetry {
11
12 /** @var string */
13 private $option_key = 'f12_cf7_doubleoptin_telemetry_counters';
14
15 /** @var \Forge12\Shared\LoggerInterface */
16 private $logger;
17
18 public function __construct(LoggerInterface $logger) {
19 $this->logger = $logger;
20 }
21
22 public function get_features(): array {
23 include_once(ABSPATH . 'wp-admin/includes/plugin.php');
24 return [
25 'double-opt-in-pro' => is_plugin_active('double-opt-in-pro/CF7DoubleOptIn.class.php'),
26 ];
27 }
28
29 /**
30 * Increment a counter by name
31 */
32 public function increment(string $counter): void {
33 $counters = get_option($this->option_key, []);
34
35 if (!is_array($counters)) {
36 $counters = maybe_unserialize($counters);
37 }
38 if (empty($counters) || !is_array($counters)) {
39 $counters = [];
40 }
41
42 if (!isset($counters[$counter])) {
43 $counters[$counter] = 0;
44 }
45
46 $counters[$counter]++;
47
48 update_option($this->option_key, $counters, false);
49
50 $this->logger->debug("Telemetry counter incremented", [
51 'plugin' => FORGE12_OPTIN_SLUG,
52 'counter' => $counter,
53 'value' => $counters[$counter],
54 ]);
55 }
56
57 /**
58 * Get all counters
59 */
60 public function get_counters(): array {
61 $counters = get_option($this->option_key, []);
62 return is_array($counters) ? $counters : [];
63 }
64
65 /**
66 * Reset counters (optional)
67 */
68 public function reset(): void {
69 update_option($this->option_key, [], false);
70 $this->logger->info("Telemetry counters reset", [
71 'plugin' => FORGE12_OPTIN_SLUG,
72 ]);
73 }
74
75 /**
76 * Static: build payload
77 */
78 public static function build_payload(): array {
79 $logger = Logger::getInstance();
80 $telemetry = new self($logger);
81
82 $logger->info("Telemetry payload creation started", [
83 'plugin' => FORGE12_OPTIN_SLUG,
84 ]);
85
86 try {
87 $counters = $telemetry->get_counters();
88
89 if (empty($counters)) {
90 $logger->info("Telemetry: Counters are empty, initializing as stdClass.", [
91 'plugin' => FORGE12_OPTIN_SLUG,
92 ]);
93 $counters = new \stdClass();
94 }
95
96 $payload = [
97 'installation_uuid' => f12_cf7_doubleoptin_get_installation_uuid(),
98 'plugin_slug' => FORGE12_OPTIN_SLUG,
99 'plugin_version' => FORGE12_OPTIN_VERSION,
100 'snapshot_date' => gmdate('Y-m-d'),
101 'settings' => get_option('f12-doi-settings', []),
102 'features' => $telemetry->get_features(),
103 'counters' => $counters,
104 'wp_version' => get_bloginfo('version'),
105 'php_version' => PHP_VERSION,
106 'locale' => get_locale(),
107 ];
108
109 $logger->info("Telemetry payload created successfully", [
110 'plugin' => FORGE12_OPTIN_SLUG,
111 'payload' => $payload,
112 ]);
113
114 return $payload;
115
116 } catch (Exception $e) {
117 $logger->error("Error creating telemetry payload", [
118 'plugin' => FORGE12_OPTIN_SLUG,
119 'message' => $e->getMessage(),
120 'trace' => $e->getTraceAsString(),
121 ]);
122
123 return [];
124 }
125 }
126
127 /**
128 * Static: send snapshot
129 */
130 public static function send_snapshot(): void {
131 $logger = Logger::getInstance();
132 $payload = self::build_payload();
133
134 $logger->debug("Telemetry Payload prepared", [
135 'plugin' => FORGE12_OPTIN_SLUG,
136 'payload' => $payload,
137 ]);
138
139 $response = wp_remote_post('https://silentshield.forge12.com/api/telemetry/snapshot', [
140 'headers' => [
141 'Content-Type' => 'application/json; charset=utf-8',
142 ],
143 'body' => wp_json_encode($payload),
144 'timeout' => 15,
145 ]);
146
147 if (is_wp_error($response)) {
148 $logger->error("Telemetry failed", [
149 'plugin' => FORGE12_OPTIN_SLUG,
150 'error' => $response->get_error_message(),
151 ]);
152 return;
153 }
154
155 $code = wp_remote_retrieve_response_code($response);
156
157 if ($code === 201) {
158 $logger->info("Telemetry sent successfully", [
159 'plugin' => FORGE12_OPTIN_SLUG,
160 'code' => $code,
161 ]);
162 } else {
163 $logger->warning("Telemetry unexpected response", [
164 'plugin' => FORGE12_OPTIN_SLUG,
165 'code' => $code,
166 'response' => wp_remote_retrieve_body($response),
167 ]);
168 }
169 }
170 }
171 }
172
173 // Cron Hook registrieren
174 add_action('f12_cf7_doubleoptin_daily_telemetry', [Telemetry::class, 'send_snapshot']);
175