PluginProbe ʕ •ᴥ•ʔ
Rank Math SEO – AI SEO Tools to Dominate SEO Rankings / 1.0.267
Rank Math SEO – AI SEO Tools to Dominate SEO Rankings v1.0.267
1.0.272 1.0.271 1.0.271.1 1.0.270 1.0.269 trunk 1.0.216 1.0.217 1.0.218 1.0.219 1.0.220 1.0.221 1.0.222 1.0.223 1.0.224 1.0.225 1.0.226 1.0.227 1.0.227.1 1.0.228 1.0.229 1.0.230 1.0.231 1.0.232 1.0.233 1.0.234 1.0.234.1 1.0.235 1.0.236 1.0.237 1.0.238 1.0.239 1.0.240 1.0.241 1.0.242 1.0.243 1.0.244 1.0.245 1.0.246 1.0.247 1.0.248 1.0.249 1.0.250 1.0.251 1.0.251.1 1.0.252 1.0.252.1 1.0.253 1.0.254 1.0.255 1.0.256 1.0.257 1.0.258 1.0.259 1.0.259.1 1.0.260 1.0.261 1.0.262 1.0.263 1.0.264 1.0.264.1 1.0.265 1.0.266 1.0.266.1 1.0.267 1.0.268
seo-by-rank-math / vendor / wp-media / wp-mixpanel / src / TrackingPlugin.php
seo-by-rank-math / vendor / wp-media / wp-mixpanel / src Last commit date
Classes 10 months ago Optin.php 9 months ago Tracking.php 8 months ago TrackingPlugin.php 7 months ago WPConsumer.php 10 months ago
TrackingPlugin.php
145 lines
1 <?php
2 declare(strict_types=1);
3
4 namespace WPMedia\Mixpanel;
5
6 class TrackingPlugin extends Tracking {
7 /**
8 * Plugin name & version
9 *
10 * @var string
11 */
12 private $plugin;
13
14 /**
15 * Brand name
16 *
17 * @var string
18 */
19 private $brand;
20
21 /**
22 * Application name
23 *
24 * @var string
25 */
26 private $app;
27
28 /**
29 * Constructor
30 *
31 * @param string $mixpanel_token Mixpanel token.
32 * @param string $plugin Plugin name.
33 * @param string $brand Brand name.
34 * @param string $app Application name.
35 */
36 public function __construct( string $mixpanel_token, string $plugin, string $brand = '', string $app = '' ) {
37 $options = [
38 'consumer' => 'wp',
39 'consumers' => [
40 'wp' => 'WPMedia\\Mixpanel\\WPConsumer',
41 ],
42 ];
43
44 parent::__construct( $mixpanel_token, $options );
45
46 $this->plugin = $plugin;
47 $this->brand = $brand;
48 $this->app = $app;
49 }
50
51 /**
52 * Track an event in Mixpanel with plugin context
53 *
54 * @param string $event Event name.
55 * @param mixed[] $properties Event properties.
56 * @param string $event_capability The capability required to track the event.
57 *
58 * @return void
59 */
60 public function track( string $event, array $properties, string $event_capability = '' ): void {
61 /**
62 * Filter the default capability required to track a specific event.
63 *
64 * @param string $capability The capability required to track the event.
65 * @param string $event The event name.
66 * @param string $app The application name.
67 */
68 $default = apply_filters( 'wp_mixpanel_event_capability', 'manage_options', $event, $this->app );
69
70 if ( empty( $event_capability ) ) {
71 $event_capability = $default;
72 }
73
74 if ( ! current_user_can( $event_capability ) ) {
75 return;
76 }
77
78 $this->track_direct( $event, $properties );
79 }
80
81 /**
82 * Track an event directly in Mixpanel, bypassing capability checks.
83 *
84 * @param string $event Event name.
85 * @param mixed[] $properties Event properties.
86 *
87 * @return void
88 */
89 public function track_direct( string $event, array $properties ): void {
90 $host = wp_parse_url( get_home_url(), PHP_URL_HOST );
91
92 if ( ! $host ) {
93 $host = '';
94 }
95
96 $defaults = [
97 'domain' => $this->hash( $host ),
98 'wp_version' => $this->get_wp_version(),
99 'php_version' => $this->get_php_version(),
100 'plugin' => strtolower( $this->plugin ),
101 'brand' => strtolower( $this->brand ),
102 'application' => strtolower( $this->app ),
103 ];
104
105 $properties = array_merge( $properties, $defaults );
106
107 parent::track( ucfirst( $event ), $properties );
108 }
109
110 /**
111 * Track opt-in status change in Mixpanel
112 *
113 * @param bool $status Opt-in status.
114 *
115 * @return void
116 */
117 public function track_optin( $status ): void {
118 $this->track(
119 'WordPress Plugin Data Consent Changed',
120 [
121 'opt_in_status' => $status,
122 ]
123 );
124 }
125
126 /**
127 * Add the Mixpanel script & initialize it
128 */
129 public function add_script(): void {
130 /**
131 * Filter the default capability required to track with JS.
132 *
133 * @param string $capability The capability required to track the event.
134 * @param string $app The application name.
135 */
136 $capability = apply_filters( 'wp_mixpanel_js_track_capability', 'manage_options', $this->app );
137
138 if ( ! current_user_can( $capability ) ) {
139 return;
140 }
141
142 parent::add_script();
143 }
144 }
145