| 1 |
<?php |
| 2 |
namespace LinnoSDK\Telemetry; |
| 3 |
|
| 4 |
class Consent { |
| 5 |
/** |
| 6 |
* Client instance |
| 7 |
* |
| 8 |
* @var Client |
| 9 |
*/ |
| 10 |
private Client $client; |
| 11 |
|
| 12 |
/** |
| 13 |
* Text domain for i18n |
| 14 |
* |
| 15 |
* @var string |
| 16 |
*/ |
| 17 |
private string $textDomain; |
| 18 |
|
| 19 |
/** |
| 20 |
* Constructor |
| 21 |
* |
| 22 |
* @param Client $client |
| 23 |
*/ |
| 24 |
public function __construct( Client $client ) { |
| 25 |
$this->client = $client; |
| 26 |
$this->textDomain = $client->get_text_domain(); |
| 27 |
} |
| 28 |
|
| 29 |
/** |
| 30 |
* Initialize the consent functionality |
| 31 |
* |
| 32 |
* @return void |
| 33 |
*/ |
| 34 |
public function init(): void { |
| 35 |
add_action( 'admin_init', array( $this, 'handle_optin_optout' ) ); |
| 36 |
add_action( 'admin_notices', array( $this, 'show_consent_notice' ) ); |
| 37 |
} |
| 38 |
|
| 39 |
/** |
| 40 |
* Handle the user's opt-in/opt-out choice |
| 41 |
* |
| 42 |
* @return void |
| 43 |
*/ |
| 44 |
public function handle_optin_optout(): void { |
| 45 |
if ( ! isset( $_GET['action'] ) || ! isset( $_GET['plugin'] ) ) { |
| 46 |
return; |
| 47 |
} |
| 48 |
|
| 49 |
if ( $_GET['plugin'] !== $this->client->get_slug() ) { |
| 50 |
return; |
| 51 |
} |
| 52 |
|
| 53 |
if ( $_GET['action'] === 'optin' ) { |
| 54 |
$this->client->set_optin_state( 'yes' ); |
| 55 |
$this->dismiss_notice(); |
| 56 |
|
| 57 |
// Explicitly call activate() to ensure table is created and flags are set. |
| 58 |
// Since we just set optin to 'yes', activate() will now track the 'plugin_activated' event. |
| 59 |
$this->client->activate(); |
| 60 |
} |
| 61 |
|
| 62 |
if ( $_GET['action'] === 'optout' ) { |
| 63 |
$this->client->set_optin_state( 'no' ); |
| 64 |
$this->dismiss_notice(); |
| 65 |
} |
| 66 |
|
| 67 |
wp_safe_redirect( remove_query_arg( array( 'action', 'plugin' ) ) ); |
| 68 |
exit; |
| 69 |
} |
| 70 |
|
| 71 |
/** |
| 72 |
* Show the consent notice |
| 73 |
* |
| 74 |
* @return void |
| 75 |
*/ |
| 76 |
public function show_consent_notice(): void { |
| 77 |
|
| 78 |
if ( $this->is_notice_dismissed() ) { |
| 79 |
return; |
| 80 |
} |
| 81 |
|
| 82 |
// Don't show the notice if the user has already opted in or out |
| 83 |
if ( null !== $this->client->get_optin_state() ) { |
| 84 |
return; |
| 85 |
} |
| 86 |
|
| 87 |
$plugin_name = $this->client->get_plugin_name(); |
| 88 |
|
| 89 |
$optin_url = add_query_arg( array( |
| 90 |
'action' => 'optin', |
| 91 |
'plugin' => $this->client->get_slug(), |
| 92 |
) ); |
| 93 |
|
| 94 |
$optout_url = add_query_arg( array( |
| 95 |
'action' => 'optout', |
| 96 |
'plugin' => $this->client->get_slug(), |
| 97 |
) ); |
| 98 |
|
| 99 |
?> |
| 100 |
<div class="notice notice-info"> |
| 101 |
<p> |
| 102 |
<?php |
| 103 |
$privacy_policy_url = apply_filters( |
| 104 |
$this->client->get_slug() . '_telemetry_privacy_policy_url', |
| 105 |
$this->client->get_privacy_url() |
| 106 |
); |
| 107 |
$consent_service_name = apply_filters( |
| 108 |
$this->client->get_slug() . '_telemetry_consent_service_name', |
| 109 |
$this->client->get_consent_service_name() |
| 110 |
); |
| 111 |
$message = apply_filters( |
| 112 |
$this->client->get_slug() . '_telemetry_consent_message', |
| 113 |
sprintf( |
| 114 |
__( 'Help us improve %1$s. With your permission, this plugin sends usage and technical data to %3$s, including: site URL, plugin name and version, event timestamps, anonymous site profile ID, custom event properties, and your current admin profile details (email, first name, last name, and avatar) for identification and product support. Tracking is off by default. <a href="%2$s" target="_blank" rel="noopener noreferrer">Learn more</a>.', $this->textDomain ), |
| 115 |
'<strong>' . $plugin_name . '</strong>', |
| 116 |
esc_url( $privacy_policy_url ), |
| 117 |
esc_html( $consent_service_name ) |
| 118 |
) |
| 119 |
); |
| 120 |
|
| 121 |
echo '<p>' . $message . '</p>'; |
| 122 |
?> |
| 123 |
<p> |
| 124 |
<a href="<?php echo esc_url( $optin_url ); ?>" class="button-primary"><?php _e( 'Allow', $this->textDomain ); ?></a> |
| 125 |
<a href="<?php echo esc_url( $optout_url ); ?>" class="button-secondary"><?php _e( 'Do not allow', $this->textDomain ); ?></a> |
| 126 |
</p> |
| 127 |
</div> |
| 128 |
<?php |
| 129 |
} |
| 130 |
|
| 131 |
/** |
| 132 |
* Check if the notice has been dismissed |
| 133 |
* |
| 134 |
* @return bool |
| 135 |
*/ |
| 136 |
private function is_notice_dismissed(): bool { |
| 137 |
return 'yes' === get_option( $this->get_notice_dismissed_key() ); |
| 138 |
} |
| 139 |
|
| 140 |
/** |
| 141 |
* Dismiss the notice |
| 142 |
* |
| 143 |
* @return void |
| 144 |
*/ |
| 145 |
private function dismiss_notice(): void { |
| 146 |
update_option( $this->get_notice_dismissed_key(), 'yes' ); |
| 147 |
} |
| 148 |
|
| 149 |
/** |
| 150 |
* Get the option key for dismissing the notice |
| 151 |
* |
| 152 |
* @return string |
| 153 |
*/ |
| 154 |
private function get_notice_dismissed_key(): string { |
| 155 |
return $this->client->get_notice_dismissed_key(); |
| 156 |
} |
| 157 |
} |
| 158 |
|