PluginProbe
WPVR – 360 Panorama viewer and Virtual Tour Builder for WordPress / 8.5.37
WPVR – 360 Panorama viewer and Virtual Tour Builder for WordPress v8.5.37
9.1.3 9.1.2 9.1.1 9.1.0 9.0.3 9.0.2 9.0.1 9.0.0 8.5.79 8.5.78 8.5.77 8.5.76 8.5.75 8.5.74 8.5.73 8.5.72 8.5.71 8.5.70 8.5.69 8.5.68 8.5.35 8.5.36 8.5.37 8.5.38 8.5.39 All 222 releases
wpvr / vendor / posthog / posthog-php / lib / PostHog.php

PostHog.php in WPVR – 360 Panorama viewer and Virtual Tour Builder for WordPress 8.5.37, at vendor/posthog/posthog-php/lib/PostHog.php

338 lines 8.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace PostHog;
4
5 use Exception;
6
7 class PostHog
8 {
9 public const VERSION = '3.6.0';
10 public const ENV_API_KEY = "POSTHOG_API_KEY";
11 public const ENV_HOST = "POSTHOG_HOST";
12
13 private static Client $client;
14
15 /**
16 * Initializes the default client to use. Uses the libcurl consumer by default.
17 * @param string|null $apiKey your project's API key
18 * @param array|null $options passed straight to the client
19 * @param Client|null $client
20 * @throws Exception
21 */
22 public static function init(
23 ?string $apiKey = null,
24 ?array $options = [],
25 ?Client $client = null,
26 ?string $personalAPIKey = null
27 ): void {
28 if (null === $client) {
29 $apiKey = $apiKey ?: getenv(self::ENV_API_KEY);
30
31 if (array_key_exists("host", $options)) {
32 $options["host"] = self::cleanHost($options["host"]);
33 } else {
34 $envHost = getenv(self::ENV_HOST) ?: null;
35 if (null !== $envHost) {
36 $options["host"] = self::cleanHost(getenv(self::ENV_HOST));
37 }
38 }
39
40 self::assert($apiKey, "PostHog::init() requires an apiKey");
41 self::$client = new Client($apiKey, $options, null, $personalAPIKey);
42 } else {
43 self::$client = $client;
44 }
45 }
46
47 /**
48 * Captures a user action
49 *
50 * @param array $message
51 * @return boolean whether the capture call succeeded
52 * @throws Exception
53 */
54 public static function capture(array $message)
55 {
56 self::checkClient();
57 $event = !empty($message["event"]);
58 self::assert($event, "PostHog::capture() expects an event");
59 self::validate($message, "capture");
60
61 return self::$client->capture($message);
62 }
63
64 /**
65 * Tags properties about the user.
66 *
67 * @param array $message
68 * @return boolean whether the identify call succeeded
69 * @throws Exception
70 */
71 public static function identify(array $message)
72 {
73 self::checkClient();
74 $message["type"] = "identify";
75 self::validate($message, "identify");
76
77 return self::$client->identify($message);
78 }
79
80 /**
81 * Adds properties to a group.
82 *
83 * @param array $message Must contain keys `groupType`, `groupKey`, `properties`
84 * @return boolean whether the groupIdentify call succeeded
85 * @throws Exception
86 */
87 public static function groupIdentify(array $message)
88 {
89 self::assert(!empty($message["groupType"]), "PostHog::groupIdentify() expects a groupType");
90 self::assert(!empty($message["groupKey"]), "PostHog::groupIdentify() expects a groupKey");
91
92 if (!isset($message["properties"])) {
93 $message["properties"] = array();
94 }
95
96 $msg = array(
97 "event" => "\$groupidentify",
98 "distinctId" => "\${$message['groupType']}_{$message['groupKey']}",
99 "properties" => array(
100 "\$group_type" => $message["groupType"],
101 "\$group_key" => $message["groupKey"],
102 "\$group_set" => $message["properties"],
103 )
104 );
105
106 return self::capture($msg);
107 }
108
109 /**
110 * decide if the feature flag is enabled for this distinct id.
111 *
112 * @param string $key
113 * @param string $distinctId
114 * @param array $groups
115 * @param array $personProperties
116 * @param array $groupProperties
117 * @return boolean
118 * @throws Exception
119 */
120 public static function isFeatureEnabled(
121 string $key,
122 string $distinctId,
123 array $groups = array(),
124 array $personProperties = array(),
125 array $groupProperties = array(),
126 bool $onlyEvaluateLocally = false,
127 bool $sendFeatureFlagEvents = true
128 ): null | bool {
129 self::checkClient();
130 return self::$client->isFeatureEnabled(
131 $key,
132 $distinctId,
133 $groups,
134 $personProperties,
135 $groupProperties,
136 $onlyEvaluateLocally,
137 $sendFeatureFlagEvents
138 );
139 }
140
141 /**
142 * get the feature flag value for this distinct id.
143 *
144 * @param string $key
145 * @param string $distinctId
146 * @param array $groups
147 * @param array $personProperties
148 * @param array $groupProperties
149 * @return boolean | string
150 * @throws Exception
151 */
152 public static function getFeatureFlag(
153 string $key,
154 string $distinctId,
155 array $groups = array(),
156 array $personProperties = array(),
157 array $groupProperties = array(),
158 bool $onlyEvaluateLocally = false,
159 bool $sendFeatureFlagEvents = true
160 ): null | bool | string {
161 self::checkClient();
162 return self::$client->GetFeatureFlag(
163 $key,
164 $distinctId,
165 $groups,
166 $personProperties,
167 $groupProperties,
168 $onlyEvaluateLocally,
169 $sendFeatureFlagEvents
170 );
171 }
172
173 /**
174 * @param string $key
175 * @param string $distinctId
176 * @param array $groups
177 * @param array $personProperties
178 * @param array $groupProperties
179 * @return mixed
180 */
181 public static function getFeatureFlagPayload(
182 string $key,
183 string $distinctId,
184 array $groups = array(),
185 array $personProperties = array(),
186 array $groupProperties = array(),
187 ): mixed {
188 return self::$client->getFeatureFlagPayload(
189 $key,
190 $distinctId,
191 $groups,
192 $personProperties,
193 $groupProperties
194 );
195 }
196
197 /**
198 * get all enabled flags for distinct_id
199 *
200 * @param string $distinctId
201 * @param array $groups
202 * @param array $personProperties
203 * @param array $groupProperties
204 * @return array
205 * @throws Exception
206 */
207 public static function getAllFlags(
208 string $distinctId,
209 array $groups = array(),
210 array $personProperties = array(),
211 array $groupProperties = array(),
212 bool $onlyEvaluateLocally = false
213 ): array {
214 self::checkClient();
215 return self::$client->getAllFlags(
216 $distinctId,
217 $groups,
218 $personProperties,
219 $groupProperties,
220 $onlyEvaluateLocally
221 );
222 }
223
224
225 /**
226 *
227 * @param string $distinctId
228 * @return array
229 * @throws Exception
230 */
231 public static function fetchFeatureVariants(string $distinctId, array $groups = array()): array
232 {
233 self::checkClient();
234 return self::$client->fetchFeatureVariants($distinctId, $groups);
235 }
236
237 /**
238 * Aliases the distinct id from a temporary id to a permanent one
239 *
240 * @param array $message distinct id to alias from
241 * @return boolean whether the alias call succeeded
242 * @throws Exception
243 */
244 public static function alias(array $message)
245 {
246 self::checkClient();
247 $alias = !empty($message["alias"]);
248 self::assert($alias, "PostHog::alias() requires an alias");
249 self::validate($message, "alias");
250
251 return self::$client->alias($message);
252 }
253
254 /**
255 * Send a raw (prepared) message
256 *
257 * @param array $message distinct id to alias from
258 * @return boolean whether the alias call succeeded
259 */
260 public static function raw(array $message)
261 {
262 return self::$client->raw($message);
263 }
264
265
266 /**
267 * Validate common properties.
268 *
269 * @param array $msg
270 * @param string $type
271 * @throws Exception
272 */
273 public static function validate($msg, $type)
274 {
275 $distinctId = !empty($msg["distinctId"]);
276 self::assert($distinctId, "PostHog::{$type}() requires distinctId");
277 }
278
279 /**
280 * Flush the client
281 */
282
283 public static function flush()
284 {
285 self::checkClient();
286
287 return self::$client->flush();
288 }
289
290 private static function cleanHost(?string $host): string
291 {
292 if (!isset($host)) {
293 return $host;
294 }
295 // remove protocol
296 if (substr($host, 0, 8) === "https://") {
297 $host = str_replace('https://', '', $host);
298 } elseif (substr($host, 0, 7) === "http://") {
299 $host = str_replace('http://', '', $host);
300 }
301
302 // remove trailing slash
303 if (substr($host, strlen($host) - 1, 1) === "/") {
304 $host = substr($host, 0, strlen($host) - 1);
305 }
306
307 return $host;
308 }
309
310 /**
311 * Check the client.
312 *
313 * @throws Exception
314 */
315 private static function checkClient()
316 {
317 if (null != self::$client) {
318 return;
319 }
320
321 throw new Exception("PostHog::init() must be called before any other capturing method.");
322 }
323
324 /**
325 * Assert `value` or throw.
326 *
327 * @param mixed $value
328 * @param string $msg
329 * @throws Exception
330 */
331 private static function assert($value, $msg)
332 {
333 if (!$value) {
334 throw new Exception($msg);
335 }
336 }
337 }
338