| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* Usage Tracker Manager |
| 5 |
* |
| 6 |
* Bootstraps the opt-in Plugin_Usage_Tracker SDK: configures the opt-in |
| 7 |
* notice copy and registers the tracker hooks on admin init. |
| 8 |
* |
| 9 |
* @package ThinkRank\Core |
| 10 |
* @since 1.12.0 |
| 11 |
*/ |
| 12 |
|
| 13 |
declare(strict_types=1); |
| 14 |
|
| 15 |
namespace ThinkRank\Core; |
| 16 |
|
| 17 |
// Prevent direct access |
| 18 |
if (!defined('ABSPATH')) { |
| 19 |
exit; |
| 20 |
} |
| 21 |
|
| 22 |
/** |
| 23 |
* Usage Tracker Manager |
| 24 |
* |
| 25 |
* Single Responsibility: wire the usage tracker into WordPress. |
| 26 |
* |
| 27 |
* @since 1.12.0 |
| 28 |
*/ |
| 29 |
class Usage_Tracker_Manager { |
| 30 |
|
| 31 |
/** |
| 32 |
* WP Insights item id for this product. |
| 33 |
* |
| 34 |
* Obtain the real id from the WP Insights dashboard and place it here |
| 35 |
* (or filter `thinkrank_usage_tracker_item_id`). Without a valid id the |
| 36 |
* tracker collects data but the initial site registration is skipped, |
| 37 |
* so nothing is transmitted. |
| 38 |
* |
| 39 |
* @var string|false |
| 40 |
*/ |
| 41 |
private const ITEM_ID = '08649e95a94ecddfd027'; |
| 42 |
|
| 43 |
/** |
| 44 |
* Tracker instance. |
| 45 |
* |
| 46 |
* @var Plugin_Usage_Tracker|null |
| 47 |
*/ |
| 48 |
private ?Plugin_Usage_Tracker $tracker = null; |
| 49 |
|
| 50 |
/** |
| 51 |
* Register the bootstrap hook. |
| 52 |
* |
| 53 |
* Mirrors the reference integration: the tracker is built on `init` |
| 54 |
* (admin context) so the opt-in notice and cron handlers are wired |
| 55 |
* once per request. The activation/deactivation hooks are registered |
| 56 |
* inside the tracker constructor. |
| 57 |
* |
| 58 |
* @return void |
| 59 |
*/ |
| 60 |
public function init(): void { |
| 61 |
add_action('init', [$this, 'start_tracking']); |
| 62 |
} |
| 63 |
|
| 64 |
/** |
| 65 |
* Build and configure the tracker. |
| 66 |
* |
| 67 |
* @return void |
| 68 |
*/ |
| 69 |
public function start_tracking(): void { |
| 70 |
$item_id = apply_filters('thinkrank_usage_tracker_item_id', self::ITEM_ID); |
| 71 |
|
| 72 |
$this->tracker = Plugin_Usage_Tracker::get_instance(THINKRANK_PLUGIN_FILE, [ |
| 73 |
'opt_in' => true, |
| 74 |
'goodbye_form' => true, |
| 75 |
'item_id' => $item_id, |
| 76 |
]); |
| 77 |
|
| 78 |
$this->tracker->set_notice_options([ |
| 79 |
'notice_title' => __('Want to help make ThinkRank even better?', 'thinkrank'), |
| 80 |
'notice' => __('Allow us to collect non-sensitive diagnostic data and usage information.', 'thinkrank'), |
| 81 |
'extra_notice' => __('We collect non-sensitive diagnostic data and plugin usage information — your site URL, WordPress & PHP version, active plugins & theme, and admin email. This lets us keep ThinkRank compatible with the most popular plugins and themes. No spam, we promise.', 'thinkrank'), |
| 82 |
]); |
| 83 |
|
| 84 |
$this->tracker->init(); |
| 85 |
} |
| 86 |
|
| 87 |
/** |
| 88 |
* Record explicit user consent for usage tracking. |
| 89 |
* |
| 90 |
* Called by flows outside the opt-in notice (e.g. the Setup Wizard |
| 91 |
* "Get Started" action). Builds the tracker if it has not been wired |
| 92 |
* yet so consent works regardless of hook timing. |
| 93 |
* |
| 94 |
* @return void |
| 95 |
*/ |
| 96 |
public function grant_consent(): void { |
| 97 |
if (null === $this->tracker) { |
| 98 |
$this->start_tracking(); |
| 99 |
} |
| 100 |
$this->tracker->opt_in(); |
| 101 |
} |
| 102 |
|
| 103 |
/** |
| 104 |
* Expose the tracker instance (mainly for tests / diagnostics). |
| 105 |
* |
| 106 |
* @return Plugin_Usage_Tracker|null |
| 107 |
*/ |
| 108 |
public function get_tracker(): ?Plugin_Usage_Tracker { |
| 109 |
return $this->tracker; |
| 110 |
} |
| 111 |
} |
| 112 |
|