Classes
10 months ago
Optin.php
9 months ago
Tracking.php
8 months ago
TrackingPlugin.php
7 months ago
WPConsumer.php
10 months ago
Optin.php
100 lines
| 1 | <?php |
| 2 | declare(strict_types=1); |
| 3 | |
| 4 | namespace WPMedia\Mixpanel; |
| 5 | |
| 6 | class Optin { |
| 7 | /** |
| 8 | * The plugin slug. |
| 9 | * |
| 10 | * @var string |
| 11 | */ |
| 12 | private $plugin_slug; |
| 13 | |
| 14 | /** |
| 15 | * The capability required to enable/disable the opt-in. |
| 16 | * |
| 17 | * @var string |
| 18 | */ |
| 19 | private $capability; |
| 20 | |
| 21 | /** |
| 22 | * Constructor. |
| 23 | * |
| 24 | * @param string $plugin_slug The plugin slug. |
| 25 | * @param string $capability The capability required to enable/disable the opt-in. |
| 26 | */ |
| 27 | public function __construct( string $plugin_slug, string $capability ) { |
| 28 | $this->plugin_slug = $plugin_slug; |
| 29 | $this->capability = $capability; |
| 30 | } |
| 31 | |
| 32 | /** |
| 33 | * Check if the opt-in is enabled. |
| 34 | * |
| 35 | * @return bool True if the opt-in is enabled, false otherwise. |
| 36 | */ |
| 37 | public function is_enabled(): bool { |
| 38 | if ( ! current_user_can( $this->capability ) ) { |
| 39 | return false; |
| 40 | } |
| 41 | |
| 42 | $optin = get_option( $this->plugin_slug . '_mixpanel_optin', false ); |
| 43 | |
| 44 | if ( ! $optin ) { |
| 45 | return false; |
| 46 | } |
| 47 | |
| 48 | return true; |
| 49 | } |
| 50 | |
| 51 | /** |
| 52 | * Check if tracking is allowed. |
| 53 | * |
| 54 | * @return bool |
| 55 | */ |
| 56 | public function can_track(): bool { |
| 57 | return (bool) get_option( $this->plugin_slug . '_mixpanel_optin', false ); |
| 58 | } |
| 59 | |
| 60 | /** |
| 61 | * Enable the opt-in. |
| 62 | * |
| 63 | * @return void |
| 64 | */ |
| 65 | public function enable(): void { |
| 66 | if ( $this->is_enabled() ) { |
| 67 | return; |
| 68 | } |
| 69 | |
| 70 | update_option( $this->plugin_slug . '_mixpanel_optin', true ); |
| 71 | |
| 72 | /** |
| 73 | * Fires when the Mixpanel opt-in status changes to enabled. |
| 74 | * |
| 75 | * @param bool $status The opt-in status. |
| 76 | */ |
| 77 | do_action( $this->plugin_slug . '_mixpanel_optin_changed', true ); |
| 78 | } |
| 79 | |
| 80 | /** |
| 81 | * Disable the opt-in. |
| 82 | * |
| 83 | * @return void |
| 84 | */ |
| 85 | public function disable(): void { |
| 86 | if ( ! $this->is_enabled() ) { |
| 87 | return; |
| 88 | } |
| 89 | |
| 90 | delete_option( $this->plugin_slug . '_mixpanel_optin' ); |
| 91 | |
| 92 | /** |
| 93 | * Fires when the Mixpanel opt-in status changes to disabled. |
| 94 | * |
| 95 | * @param bool $status The opt-in status. |
| 96 | */ |
| 97 | do_action( $this->plugin_slug . '_mixpanel_optin_changed', false ); |
| 98 | } |
| 99 | } |
| 100 |