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 |