| 1 |
<?php |
| 2 |
/** |
| 3 |
* Handles data tracking functionalities. |
| 4 |
* |
| 5 |
* @package WP_Defender\Controller |
| 6 |
*/ |
| 7 |
|
| 8 |
namespace WP_Defender\Controller; |
| 9 |
|
| 10 |
use Exception; |
| 11 |
use WP_Defender\Event; |
| 12 |
use Calotes\Component\Response; |
| 13 |
use WP_Defender\Model\Setting\Main_Setting; |
| 14 |
use WP_Defender\Component\Config\Config_Hub_Helper; |
| 15 |
|
| 16 |
/** |
| 17 |
* Handles data tracking functionalities. |
| 18 |
* |
| 19 |
* @since 4.2.0 |
| 20 |
*/ |
| 21 |
class Data_Tracking extends Event { |
| 22 |
|
| 23 |
public const TRACKING_SLUG = 'wd_show_usage_data'; |
| 24 |
|
| 25 |
/** |
| 26 |
* Site option key that controls the dashboard share-usage bubble lifecycle. |
| 27 |
* Kept separate from TRACKING_SLUG so that skipping onboarding does not |
| 28 |
* suppress the bubble. |
| 29 |
*/ |
| 30 |
public const DASHBOARD_NOTICE_SLUG = 'wd_show_dashboard_usage_notice'; |
| 31 |
|
| 32 |
/** |
| 33 |
* Records whether the dashboard notice lifecycle has been initialized. |
| 34 |
*/ |
| 35 |
public const DASHBOARD_NOTICE_INITIALIZED_SLUG = 'wd_dashboard_usage_notice_initialized'; |
| 36 |
|
| 37 |
/** |
| 38 |
* Initializes the model and service, registers routes, and sets up scheduled events if the model is active. |
| 39 |
*/ |
| 40 |
public function __construct() { |
| 41 |
$this->register_routes(); |
| 42 |
} |
| 43 |
|
| 44 |
/** |
| 45 |
* Handles the closing of the tracking modal. |
| 46 |
* |
| 47 |
* @return Response Response object indicating success. |
| 48 |
* @defender_route |
| 49 |
*/ |
| 50 |
public function close_track_modal(): Response { |
| 51 |
$model_settings = wd_di()->get( Main_Setting::class ); |
| 52 |
|
| 53 |
if ( true === $model_settings->usage_tracking ) { |
| 54 |
$model_settings->toggle_tracking( false ); |
| 55 |
Config_Hub_Helper::set_clear_active_flag(); |
| 56 |
$this->track_opt_toggle( false, 'Tracking modal' ); |
| 57 |
} |
| 58 |
// Track. |
| 59 |
$this->track_feature( 'def_tracking_modal', array( 'Modal Action' => 'closed' ) ); |
| 60 |
self::dismiss_modal_key(); |
| 61 |
|
| 62 |
return new Response( true, array() ); |
| 63 |
} |
| 64 |
|
| 65 |
/** |
| 66 |
* Save Enabled tracking state. |
| 67 |
* |
| 68 |
* @return Response |
| 69 |
* @defender_route |
| 70 |
*/ |
| 71 |
public function save_track_modal(): Response { |
| 72 |
$model_settings = wd_di()->get( Main_Setting::class ); |
| 73 |
// Update the value if it's changed. |
| 74 |
if ( true !== $model_settings->usage_tracking ) { |
| 75 |
$model_settings->toggle_tracking( true ); |
| 76 |
// Changes for Hub. |
| 77 |
Config_Hub_Helper::set_clear_active_flag(); |
| 78 |
// Track#1. |
| 79 |
$this->track_opt_toggle( true, 'Tracking modal' ); |
| 80 |
// Track#2. |
| 81 |
$this->track_feature( 'def_tracking_modal', array( 'Modal Action' => 'cta_clicked' ) ); |
| 82 |
} |
| 83 |
// Hide the modal. |
| 84 |
self::dismiss_modal_key(); |
| 85 |
|
| 86 |
return new Response( true, array() ); |
| 87 |
} |
| 88 |
|
| 89 |
/** |
| 90 |
* Marks the tracking notice as dismissed without resetting initialization state. |
| 91 |
* |
| 92 |
* @return void |
| 93 |
*/ |
| 94 |
public static function dismiss_modal_key(): void { |
| 95 |
update_site_option( self::TRACKING_SLUG, false ); |
| 96 |
} |
| 97 |
|
| 98 |
/** |
| 99 |
* Deletes the site option that controls the visibility of the tracking modal. |
| 100 |
*/ |
| 101 |
public static function delete_modal_key(): void { |
| 102 |
delete_site_option( self::TRACKING_SLUG ); |
| 103 |
} |
| 104 |
|
| 105 |
/** |
| 106 |
* Delete all the data & the cache. |
| 107 |
*/ |
| 108 |
public function remove_data() { |
| 109 |
self::delete_modal_key(); |
| 110 |
delete_site_option( self::DASHBOARD_NOTICE_SLUG ); |
| 111 |
delete_site_option( self::DASHBOARD_NOTICE_INITIALIZED_SLUG ); |
| 112 |
} |
| 113 |
|
| 114 |
/** |
| 115 |
* Exports strings. |
| 116 |
* |
| 117 |
* @return array An array of strings. |
| 118 |
*/ |
| 119 |
public function export_strings(): array { |
| 120 |
return array(); |
| 121 |
} |
| 122 |
|
| 123 |
/** |
| 124 |
* Converts the object data to an array. |
| 125 |
* |
| 126 |
* @return array An array representation of the object. |
| 127 |
*/ |
| 128 |
public function to_array(): array { |
| 129 |
return array(); |
| 130 |
} |
| 131 |
|
| 132 |
/** |
| 133 |
* Imports data into the model. |
| 134 |
* |
| 135 |
* @param array $data Data to be imported into the model. |
| 136 |
* |
| 137 |
* @throws Exception If table is not defined. |
| 138 |
*/ |
| 139 |
public function import_data( array $data ) { |
| 140 |
} |
| 141 |
|
| 142 |
/** |
| 143 |
* Removes settings for all submodules. |
| 144 |
*/ |
| 145 |
public function remove_settings(): void { |
| 146 |
self::delete_modal_key(); |
| 147 |
delete_site_option( self::DASHBOARD_NOTICE_SLUG ); |
| 148 |
delete_site_option( self::DASHBOARD_NOTICE_INITIALIZED_SLUG ); |
| 149 |
} |
| 150 |
|
| 151 |
/** |
| 152 |
* Provides data for the frontend. |
| 153 |
* |
| 154 |
* @return array An array of data for the frontend. |
| 155 |
*/ |
| 156 |
public function data_frontend(): array { |
| 157 |
return array(); |
| 158 |
} |
| 159 |
|
| 160 |
/** |
| 161 |
* Initializes the dashboard notice lifecycle once. |
| 162 |
* |
| 163 |
* A dedicated marker distinguishes an intentionally consumed false value |
| 164 |
* from an option that an older or interrupted upgrade failed to seed. |
| 165 |
* |
| 166 |
* @return void |
| 167 |
*/ |
| 168 |
public static function initialize_dashboard_notice(): void { |
| 169 |
if ( (bool) get_site_option( self::DASHBOARD_NOTICE_INITIALIZED_SLUG ) ) { |
| 170 |
return; |
| 171 |
} |
| 172 |
|
| 173 |
$usage_tracking = wd_di()->get( Main_Setting::class )->usage_tracking; |
| 174 |
delete_site_option( self::DASHBOARD_NOTICE_SLUG ); |
| 175 |
add_site_option( self::DASHBOARD_NOTICE_SLUG, ! $usage_tracking ); |
| 176 |
update_site_option( self::DASHBOARD_NOTICE_INITIALIZED_SLUG, true ); |
| 177 |
} |
| 178 |
|
| 179 |
/** |
| 180 |
* Returns whether the dashboard share usage notice should be displayed. |
| 181 |
* |
| 182 |
* @return bool |
| 183 |
*/ |
| 184 |
public function should_show_dashboard_notice(): bool { |
| 185 |
$notice_option = (bool) get_site_option( self::DASHBOARD_NOTICE_SLUG ); |
| 186 |
|
| 187 |
// Falsey means not eligible or already displayed. |
| 188 |
if ( ! $notice_option ) { |
| 189 |
return false; |
| 190 |
} |
| 191 |
|
| 192 |
// Truthy means eligible; also guard against already opted-in. |
| 193 |
return ! wd_di()->get( Main_Setting::class )->usage_tracking; |
| 194 |
} |
| 195 |
|
| 196 |
/** |
| 197 |
* Returns dashboard notice data and routes for the share usage bubble. |
| 198 |
* |
| 199 |
* @return array |
| 200 |
*/ |
| 201 |
public function get_dashboard_notice_data(): array { |
| 202 |
self::initialize_dashboard_notice(); |
| 203 |
$result = $this->dump_routes_and_nonces(); |
| 204 |
|
| 205 |
return array( |
| 206 |
'shareUsageNotice' => array( |
| 207 |
'isVisible' => $this->should_show_dashboard_notice(), |
| 208 |
), |
| 209 |
'routes' => $result['routes'], |
| 210 |
'nonces' => $result['nonces'], |
| 211 |
); |
| 212 |
} |
| 213 |
|
| 214 |
/** |
| 215 |
* Marks the share usage dashboard notice as already shown. |
| 216 |
* |
| 217 |
* @return Response |
| 218 |
* @defender_route |
| 219 |
*/ |
| 220 |
public function mark_track_notice_displayed(): Response { |
| 221 |
update_site_option( self::DASHBOARD_NOTICE_SLUG, false ); |
| 222 |
|
| 223 |
return new Response( true, array() ); |
| 224 |
} |
| 225 |
} |
| 226 |
|