PluginProbe
WPVR – 360 Panorama viewer and Virtual Tour Builder for WordPress / 8.5.77
WPVR – 360 Panorama viewer and Virtual Tour Builder for WordPress v8.5.77
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.77, at vendor/posthog/posthog-php/lib/PostHog.php

238 lines 6.3 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 = '2.1.0';
10 public const ENV_API_KEY = "POSTHOG_API_KEY";
11 public const ENV_HOST = "POSTHOG_HOST";
12
13 private static $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(?string $apiKey = null, ?array $options = [], ?Client $client = null): void
23 {
24 if (null === $client) {
25 $apiKey = $apiKey ?: getenv(self::ENV_API_KEY);
26
27
28 if (array_key_exists("host", $options)) {
29 $options["host"] = self::cleanHost($options["host"]);
30 } else {
31 $envHost = getenv(self::ENV_HOST) ?: null;
32 if (null !== $envHost) {
33 $options["host"] = self::cleanHost(getenv(self::ENV_HOST));
34 }
35 }
36
37 self::assert($apiKey, "PostHog::init() requires an apiKey");
38 self::$client = new Client($apiKey, $options);
39 } else {
40 self::$client = $client;
41 }
42 }
43
44 /**
45 * Captures a user action
46 *
47 * @param array $message
48 * @return boolean whether the capture call succeeded
49 * @throws Exception
50 */
51 public static function capture(array $message)
52 {
53 self::checkClient();
54 $event = !empty($message["event"]);
55 self::assert($event, "PostHog::capture() expects an event");
56 self::validate($message, "capture");
57
58 return self::$client->capture($message);
59 }
60
61 /**
62 * Tags properties about the user.
63 *
64 * @param array $message
65 * @return boolean whether the identify call succeeded
66 * @throws Exception
67 */
68 public static function identify(array $message)
69 {
70 self::checkClient();
71 $message["type"] = "identify";
72 self::validate($message, "identify");
73
74 return self::$client->identify($message);
75 }
76
77 /**
78 * Adds properties to a group.
79 *
80 * @param array $message Must contain keys `groupType`, `groupKey`, `properties`
81 * @return boolean whether the groupIdentify call succeeded
82 * @throws Exception
83 */
84 public static function groupIdentify(array $message)
85 {
86 self::assert(!empty($message["groupType"]), "PostHog::groupIdentify() expects a groupType");
87 self::assert(!empty($message["groupKey"]), "PostHog::groupIdentify() expects a groupKey");
88
89 if (!isset($message["properties"])) {
90 $message["properties"] = array();
91 }
92
93 $msg = array(
94 "event" => "\$groupidentify",
95 "distinctId" => "\${$message['groupType']}_{$message['groupKey']}",
96 "properties" => array(
97 "\$group_type" => $message["groupType"],
98 "\$group_key" => $message["groupKey"],
99 "\$group_set" => $message["properties"],
100 )
101 );
102
103 return self::capture($msg);
104 }
105
106 /**
107 * decide if the feature flag is enabled for this distinct id.
108 *
109 * @param string $key
110 * @param string $distinctId
111 * @param mixed $default
112 * @return boolean
113 * @throws Exception
114 */
115 public static function isFeatureEnabled(
116 string $key,
117 string $distinctId,
118 $default = false,
119 array $groups = array()
120 ): bool {
121 self::checkClient();
122 return self::$client->isFeatureEnabled($key, $distinctId, $default, $groups);
123 }
124
125 /**
126 *
127 * @param string $distinctId
128 * @return array
129 * @throws Exception
130 */
131 public static function fetchEnabledFeatureFlags(string $distinctId, array $groups = array()): array
132 {
133 self::checkClient();
134 return self::$client->fetchEnabledFeatureFlags($distinctId, $groups);
135 }
136
137 /**
138 * Aliases the distinct id from a temporary id to a permanent one
139 *
140 * @param array $message distinct id to alias from
141 * @return boolean whether the alias call succeeded
142 * @throws Exception
143 */
144 public static function alias(array $message)
145 {
146 self::checkClient();
147 $alias = !empty($message["alias"]);
148 self::assert($alias, "PostHog::alias() requires an alias");
149 self::validate($message, "alias");
150
151 return self::$client->alias($message);
152 }
153
154 /**
155 * Send a raw (prepared) message
156 *
157 * @param array $message distinct id to alias from
158 * @return boolean whether the alias call succeeded
159 */
160 public static function raw(array $message)
161 {
162 return self::$client->raw($message);
163 }
164
165
166 /**
167 * Validate common properties.
168 *
169 * @param array $msg
170 * @param string $type
171 * @throws Exception
172 */
173 public static function validate($msg, $type)
174 {
175 $distinctId = !empty($msg["distinctId"]);
176 self::assert($distinctId, "PostHog::{$type}() requires distinctId");
177 }
178
179 /**
180 * Flush the client
181 */
182
183 public static function flush()
184 {
185 self::checkClient();
186
187 return self::$client->flush();
188 }
189
190 private static function cleanHost(?string $host): string
191 {
192 if (!isset($host)) {
193 return $host;
194 }
195 // remove protocol
196 if (substr($host, 0, 8) === "https://") {
197 $host = str_replace('https://', '', $host);
198 } elseif (substr($host, 0, 7) === "http://") {
199 $host = str_replace('http://', '', $host);
200 }
201
202 // remove trailing slash
203 if (substr($host, strlen($host) - 1, 1) === "/") {
204 $host = substr($host, 0, strlen($host) - 1);
205 }
206
207 return $host;
208 }
209
210 /**
211 * Check the client.
212 *
213 * @throws Exception
214 */
215 private static function checkClient()
216 {
217 if (null != self::$client) {
218 return;
219 }
220
221 throw new Exception("PostHog::init() must be called before any other capturing method.");
222 }
223
224 /**
225 * Assert `value` or throw.
226 *
227 * @param mixed $value
228 * @param string $msg
229 * @throws Exception
230 */
231 private static function assert($value, $msg)
232 {
233 if (!$value) {
234 throw new Exception($msg);
235 }
236 }
237 }
238