| 1 |
<?php |
| 2 |
/** |
| 3 |
* @package FireBox |
| 4 |
* @version 2.1.14 Free |
| 5 |
* |
| 6 |
* @author FirePlugins <info@fireplugins.com> |
| 7 |
* @link https://www.fireplugins.com |
| 8 |
* @copyright Copyright © 2024 FirePlugins All Rights Reserved |
| 9 |
* @license GNU GPLv3 <http://www.gnu.org/licenses/gpl.html> or later |
| 10 |
*/ |
| 11 |
|
| 12 |
namespace FireBox\Core\Analytics\Ajax; |
| 13 |
|
| 14 |
if (!defined('ABSPATH')) |
| 15 |
{ |
| 16 |
exit; // Exit if accessed directly. |
| 17 |
} |
| 18 |
|
| 19 |
class Main |
| 20 |
{ |
| 21 |
use Shared; |
| 22 |
|
| 23 |
public function __construct() |
| 24 |
{ |
| 25 |
add_action('wp_ajax_firebox_analytics_stats', [$this, 'firebox_analytics_stats']); |
| 26 |
add_action('wp_ajax_nopriv_firebox_analytics_stats', [$this, 'firebox_analytics_stats']); |
| 27 |
|
| 28 |
add_action('wp_ajax_firebox_analytics_get_campaigns', [$this, 'firebox_analytics_get_campaigns']); |
| 29 |
add_action('wp_ajax_nopriv_firebox_analytics_get_campaigns', [$this, 'firebox_analytics_get_campaigns']); |
| 30 |
|
| 31 |
add_action('wp_ajax_firebox_analytics_get_dropdown_campaigns', [$this, 'firebox_analytics_get_dropdown_campaigns']); |
| 32 |
add_action('wp_ajax_nopriv_firebox_analytics_get_dropdown_campaigns', [$this, 'firebox_analytics_get_dropdown_campaigns']); |
| 33 |
|
| 34 |
add_action('wp_ajax_firebox_delete_campaign', [$this, 'firebox_delete_campaign']); |
| 35 |
add_action('wp_ajax_nopriv_firebox_delete_campaign', [$this, 'firebox_delete_campaign']); |
| 36 |
|
| 37 |
add_action('wp_ajax_firebox_duplicate_campaign', [$this, 'firebox_duplicate_campaign']); |
| 38 |
add_action('wp_ajax_nopriv_firebox_duplicate_campaign', [$this, 'firebox_duplicate_campaign']); |
| 39 |
|
| 40 |
add_action('wp_ajax_firebox_analytics_get_charts_data', [$this, 'firebox_analytics_get_charts_data']); |
| 41 |
add_action('wp_ajax_nopriv_firebox_analytics_get_charts_data', [$this, 'firebox_analytics_get_charts_data']); |
| 42 |
|
| 43 |
add_action('wp_ajax_firebox_analytics_get_trending_templates', [$this, 'firebox_analytics_get_trending_templates']); |
| 44 |
add_action('wp_ajax_nopriv_firebox_analytics_get_trending_templates', [$this, 'firebox_analytics_get_trending_templates']); |
| 45 |
} |
| 46 |
|
| 47 |
/** |
| 48 |
* Analytics Counts |
| 49 |
* |
| 50 |
* @return void |
| 51 |
*/ |
| 52 |
public function firebox_analytics_stats() |
| 53 |
{ |
| 54 |
if (!current_user_can('manage_options')) |
| 55 |
{ |
| 56 |
return; |
| 57 |
} |
| 58 |
|
| 59 |
$nonce = isset($_POST['nonce']) ? sanitize_text_field($_POST['nonce']) : ''; |
| 60 |
|
| 61 |
// verify nonce |
| 62 |
if (!$verify = wp_verify_nonce($nonce, 'fpf_js_nonce')) |
| 63 |
{ |
| 64 |
return false; |
| 65 |
} |
| 66 |
|
| 67 |
$date_period = isset($_POST['date_period']) ? sanitize_text_field($_POST['date_period']) : ''; |
| 68 |
$start_date = isset($_POST['start_date']) ? sanitize_text_field($_POST['start_date']) : ''; |
| 69 |
$end_date = isset($_POST['end_date']) ? sanitize_text_field($_POST['end_date']) : ''; |
| 70 |
|
| 71 |
if (!$start_date || $start_date === 'false' || !$end_date || $end_date === 'false') |
| 72 |
{ |
| 73 |
return; |
| 74 |
} |
| 75 |
|
| 76 |
$selected_campaign = isset($_POST['selected_campaign']) && $_POST['selected_campaign'] ? intval($_POST['selected_campaign']) : ''; |
| 77 |
|
| 78 |
$data = new \FireBox\Core\Analytics\Data($start_date, $end_date); |
| 79 |
|
| 80 |
$metrics = [ |
| 81 |
'views', |
| 82 |
'conversions', |
| 83 |
'conversionrate' |
| 84 |
]; |
| 85 |
$data->setMetrics($metrics); |
| 86 |
|
| 87 |
$filters = []; |
| 88 |
if ($selected_campaign) |
| 89 |
{ |
| 90 |
$filters['campaign'] = [ |
| 91 |
'value' => [$selected_campaign] |
| 92 |
]; |
| 93 |
} |
| 94 |
$data->setFilters($filters); |
| 95 |
|
| 96 |
$previousPeriodData = [ |
| 97 |
'views' => 0, |
| 98 |
'conversions' => 0, |
| 99 |
'conversionrate' => 0 |
| 100 |
]; |
| 101 |
|
| 102 |
// Calculate previous period data |
| 103 |
$start_date_ts = strtotime($start_date); |
| 104 |
$end_date_ts = strtotime($end_date); |
| 105 |
$days_between = ceil(abs($end_date_ts - $start_date_ts) / 86400); |
| 106 |
|
| 107 |
if ($previousPeriodDates = $this->getPreviousPeriodDates($start_date, $days_between)) |
| 108 |
{ |
| 109 |
$previousData = new \FireBox\Core\Analytics\Data($previousPeriodDates[0], $previousPeriodDates[1]); |
| 110 |
|
| 111 |
$metrics = [ |
| 112 |
'views', |
| 113 |
'conversions', |
| 114 |
'conversionrate' |
| 115 |
]; |
| 116 |
$previousData->setMetrics($metrics); |
| 117 |
|
| 118 |
$filters = []; |
| 119 |
if ($selected_campaign) |
| 120 |
{ |
| 121 |
$filters['campaign'] = [ |
| 122 |
'value' => [$selected_campaign] |
| 123 |
]; |
| 124 |
} |
| 125 |
$previousData->setFilters($filters); |
| 126 |
|
| 127 |
$previousPeriodData = $previousData->getData('count'); |
| 128 |
} |
| 129 |
|
| 130 |
echo wp_json_encode([ |
| 131 |
'current' => $data->getData('count'), |
| 132 |
'previous' => $previousPeriodData |
| 133 |
]); |
| 134 |
wp_die(); |
| 135 |
} |
| 136 |
|
| 137 |
/** |
| 138 |
* Analytics Get All Dropdown Campaigns |
| 139 |
* |
| 140 |
* @return void |
| 141 |
*/ |
| 142 |
public function firebox_analytics_get_dropdown_campaigns() |
| 143 |
{ |
| 144 |
if (!current_user_can('manage_options')) |
| 145 |
{ |
| 146 |
return; |
| 147 |
} |
| 148 |
|
| 149 |
$nonce = isset($_POST['nonce']) ? sanitize_text_field($_POST['nonce']) : ''; |
| 150 |
|
| 151 |
// verify nonce |
| 152 |
if (!$verify = wp_verify_nonce($nonce, 'fpf_js_nonce')) |
| 153 |
{ |
| 154 |
return false; |
| 155 |
} |
| 156 |
|
| 157 |
$boxes = \FireBox\Core\Helpers\BoxHelper::getAllBoxes(['draft', 'publish']); |
| 158 |
$boxes = $boxes->posts; |
| 159 |
|
| 160 |
if (!count($boxes)) |
| 161 |
{ |
| 162 |
echo wp_json_encode([]); |
| 163 |
wp_die(); |
| 164 |
} |
| 165 |
|
| 166 |
$data = [ |
| 167 |
[ |
| 168 |
'id' => null, |
| 169 |
'label' => 'All Campaigns' |
| 170 |
] |
| 171 |
]; |
| 172 |
|
| 173 |
foreach ($boxes as $box) |
| 174 |
{ |
| 175 |
$data[] = [ |
| 176 |
'id' => $box->ID, |
| 177 |
'label' => $box->post_title |
| 178 |
]; |
| 179 |
} |
| 180 |
|
| 181 |
echo wp_json_encode($data); |
| 182 |
wp_die(); |
| 183 |
} |
| 184 |
|
| 185 |
/** |
| 186 |
* Analytics Get Chart Data |
| 187 |
* |
| 188 |
* @return void |
| 189 |
*/ |
| 190 |
public function firebox_analytics_get_charts_data() |
| 191 |
{ |
| 192 |
if (!current_user_can('manage_options')) |
| 193 |
{ |
| 194 |
return; |
| 195 |
} |
| 196 |
|
| 197 |
$nonce = isset($_POST['nonce']) ? sanitize_text_field($_POST['nonce']) : ''; |
| 198 |
|
| 199 |
// verify nonce |
| 200 |
if (!$verify = wp_verify_nonce($nonce, 'fpf_js_nonce')) |
| 201 |
{ |
| 202 |
return false; |
| 203 |
} |
| 204 |
|
| 205 |
$start_date = isset($_POST['start_date']) ? sanitize_text_field($_POST['start_date']) : ''; |
| 206 |
$end_date = isset($_POST['end_date']) ? sanitize_text_field($_POST['end_date']) : ''; |
| 207 |
|
| 208 |
if (!$start_date || $start_date === 'false' || !$end_date || $end_date === 'false') |
| 209 |
{ |
| 210 |
return; |
| 211 |
} |
| 212 |
|
| 213 |
$filter = isset($_POST['filter']) ? sanitize_text_field($_POST['filter']) : 'list'; |
| 214 |
|
| 215 |
$start_date_ts = strtotime($start_date); |
| 216 |
$end_date_ts = strtotime($end_date); |
| 217 |
$days_between = ceil(abs($end_date_ts - $start_date_ts) / 86400); |
| 218 |
|
| 219 |
// prepare labels |
| 220 |
$labels = []; |
| 221 |
|
| 222 |
// We are fetching data for a single day |
| 223 |
if ($days_between == 1) |
| 224 |
{ |
| 225 |
for ($hour = 0; $hour < 24; $hour++) |
| 226 |
{ |
| 227 |
$start_hour = sprintf('%02d', $hour); |
| 228 |
$labels[] = $start_hour . ':00'; |
| 229 |
} |
| 230 |
} |
| 231 |
// Multiple days |
| 232 |
else if ($days_between > 1) |
| 233 |
{ |
| 234 |
switch ($filter) |
| 235 |
{ |
| 236 |
case 'list': |
| 237 |
default: |
| 238 |
$tmp_start_date_ts = $start_date_ts; |
| 239 |
for ($i = 0; $i < $days_between; $i++) |
| 240 |
{ |
| 241 |
$labels[] = gmdate('Y-m-d', $tmp_start_date_ts); |
| 242 |
$tmp_start_date_ts = strtotime("+1 day", $tmp_start_date_ts); |
| 243 |
} |
| 244 |
break; |
| 245 |
|
| 246 |
case 'weekly': |
| 247 |
$startDate = new \DateTime(); |
| 248 |
$startDate->setTimestamp($start_date_ts); |
| 249 |
|
| 250 |
$endDate = new \DateTime(); |
| 251 |
$endDate->setTimestamp($end_date_ts); |
| 252 |
|
| 253 |
// Find the nearest Monday to the start date |
| 254 |
while ($startDate->format('N') != 1) |
| 255 |
{ |
| 256 |
$startDate->modify('-1 day'); |
| 257 |
} |
| 258 |
|
| 259 |
while ($startDate <= $endDate) |
| 260 |
{ |
| 261 |
$labels[] = $startDate->format('d M y'); |
| 262 |
$startDate->modify('+1 week'); |
| 263 |
} |
| 264 |
break; |
| 265 |
|
| 266 |
case 'monthly': |
| 267 |
$startDate = new \DateTime(); |
| 268 |
$startDate->setTimestamp($start_date_ts); |
| 269 |
|
| 270 |
$endDate = new \DateTime(); |
| 271 |
$endDate->setTimestamp($end_date_ts); |
| 272 |
|
| 273 |
// Set the start date to the first day of the month |
| 274 |
$startDate->modify('first day of this month'); |
| 275 |
|
| 276 |
while ($startDate <= $endDate) |
| 277 |
{ |
| 278 |
$labels[] = $startDate->format('M Y'); |
| 279 |
$startDate->modify('+1 month'); |
| 280 |
} |
| 281 |
break; |
| 282 |
} |
| 283 |
} |
| 284 |
|
| 285 |
$selected_campaign = isset($_POST['selected_campaign']) && $_POST['selected_campaign'] ? intval($_POST['selected_campaign']) : ''; |
| 286 |
|
| 287 |
$data = new \FireBox\Core\Analytics\Data($start_date, $end_date); |
| 288 |
|
| 289 |
$metrics = [ |
| 290 |
'views', |
| 291 |
'conversions', |
| 292 |
'conversionrate' |
| 293 |
]; |
| 294 |
$data->setMetrics($metrics); |
| 295 |
|
| 296 |
if ($selected_campaign) |
| 297 |
{ |
| 298 |
$filters = [ |
| 299 |
'campaign' => [ |
| 300 |
'value' => [$selected_campaign] |
| 301 |
] |
| 302 |
]; |
| 303 |
$data->setFilters($filters); |
| 304 |
} |
| 305 |
|
| 306 |
$data = $data->getData($filter); |
| 307 |
|
| 308 |
echo wp_json_encode([ |
| 309 |
'labels' => $labels, |
| 310 |
'data' => $data |
| 311 |
]); |
| 312 |
wp_die(); |
| 313 |
} |
| 314 |
|
| 315 |
/** |
| 316 |
* Analytics Get Latest 5 Campaigns |
| 317 |
* |
| 318 |
* @return void |
| 319 |
*/ |
| 320 |
public function firebox_analytics_get_campaigns() |
| 321 |
{ |
| 322 |
if (!current_user_can('manage_options')) |
| 323 |
{ |
| 324 |
return; |
| 325 |
} |
| 326 |
|
| 327 |
$nonce = isset($_POST['nonce']) ? sanitize_text_field($_POST['nonce']) : ''; |
| 328 |
|
| 329 |
// verify nonce |
| 330 |
if (!$verify = wp_verify_nonce($nonce, 'fpf_js_nonce')) |
| 331 |
{ |
| 332 |
return false; |
| 333 |
} |
| 334 |
|
| 335 |
$boxes = \FireBox\Core\Analytics\Helpers\Campaigns::getRecentCampaignsList(5); |
| 336 |
|
| 337 |
if (!count($boxes->posts)) |
| 338 |
{ |
| 339 |
echo wp_json_encode([]); |
| 340 |
wp_die(); |
| 341 |
} |
| 342 |
|
| 343 |
$start_date = isset($_POST['start_date']) ? sanitize_text_field($_POST['start_date']) : ''; |
| 344 |
$end_date = isset($_POST['end_date']) ? sanitize_text_field($_POST['end_date']) : ''; |
| 345 |
|
| 346 |
$data = []; |
| 347 |
|
| 348 |
foreach ($boxes->posts as $box) |
| 349 |
{ |
| 350 |
$meta = \FireBox\Core\Helpers\BoxHelper::getMeta($box->ID); |
| 351 |
|
| 352 |
$data[] = [ |
| 353 |
'id' => $box->ID, |
| 354 |
'label' => $box->post_title, |
| 355 |
'status' => $box->post_status, |
| 356 |
'trigger' => isset($meta['triggermethod']) ? $meta['triggermethod'] : '', |
| 357 |
'position' => isset($meta['position']) ? $meta['position'] : '', |
| 358 |
'totals' => $this->getBoxAnalyticsTotals($box->ID, $start_date, $end_date) |
| 359 |
]; |
| 360 |
} |
| 361 |
|
| 362 |
echo wp_json_encode($data); |
| 363 |
wp_die(); |
| 364 |
} |
| 365 |
|
| 366 |
/** |
| 367 |
* Delete a campaign by ID. |
| 368 |
* |
| 369 |
* @return void |
| 370 |
*/ |
| 371 |
public function firebox_delete_campaign() |
| 372 |
{ |
| 373 |
if (!current_user_can('manage_options')) |
| 374 |
{ |
| 375 |
return; |
| 376 |
} |
| 377 |
|
| 378 |
$nonce = isset($_POST['nonce']) ? sanitize_text_field($_POST['nonce']) : ''; |
| 379 |
|
| 380 |
// verify nonce |
| 381 |
if (!$verify = wp_verify_nonce($nonce, 'fpf_js_nonce')) |
| 382 |
{ |
| 383 |
return false; |
| 384 |
} |
| 385 |
|
| 386 |
$id = isset($_POST['id']) ? intval($_POST['id']) : ''; |
| 387 |
if (!$id) |
| 388 |
{ |
| 389 |
return; |
| 390 |
} |
| 391 |
|
| 392 |
$error = false; |
| 393 |
|
| 394 |
if (!wp_delete_post($id, true)) |
| 395 |
{ |
| 396 |
$error = true; |
| 397 |
} |
| 398 |
|
| 399 |
echo wp_json_encode([ |
| 400 |
'error' => $error |
| 401 |
]); |
| 402 |
wp_die(); |
| 403 |
} |
| 404 |
|
| 405 |
/** |
| 406 |
* Duplicate a campaign by ID. |
| 407 |
* |
| 408 |
* @return void |
| 409 |
*/ |
| 410 |
public function firebox_duplicate_campaign() |
| 411 |
{ |
| 412 |
if (!current_user_can('manage_options')) |
| 413 |
{ |
| 414 |
return; |
| 415 |
} |
| 416 |
|
| 417 |
$nonce = isset($_POST['nonce']) ? sanitize_text_field($_POST['nonce']) : ''; |
| 418 |
|
| 419 |
// verify nonce |
| 420 |
if (!$verify = wp_verify_nonce($nonce, 'fpf_js_nonce')) |
| 421 |
{ |
| 422 |
return false; |
| 423 |
} |
| 424 |
|
| 425 |
$id = isset($_POST['id']) ? intval($_POST['id']) : ''; |
| 426 |
if (!$id) |
| 427 |
{ |
| 428 |
return; |
| 429 |
} |
| 430 |
|
| 431 |
$error = false; |
| 432 |
|
| 433 |
if (!\FireBox\Core\Helpers\BoxHelper::duplicateBox($id)) |
| 434 |
{ |
| 435 |
$error = true; |
| 436 |
} |
| 437 |
|
| 438 |
echo wp_json_encode([ |
| 439 |
'error' => $error |
| 440 |
]); |
| 441 |
wp_die(); |
| 442 |
} |
| 443 |
|
| 444 |
/** |
| 445 |
* Retrieve trending templates. |
| 446 |
* |
| 447 |
* @return void |
| 448 |
*/ |
| 449 |
public function firebox_analytics_get_trending_templates() |
| 450 |
{ |
| 451 |
if (!current_user_can('manage_options')) |
| 452 |
{ |
| 453 |
return; |
| 454 |
} |
| 455 |
|
| 456 |
$nonce = isset($_POST['nonce']) ? sanitize_text_field($_POST['nonce']) : ''; |
| 457 |
|
| 458 |
// verify nonce |
| 459 |
if (!$verify = wp_verify_nonce($nonce, 'fpf_js_nonce')) |
| 460 |
{ |
| 461 |
return false; |
| 462 |
} |
| 463 |
|
| 464 |
$templates = \FPFramework\Helpers\Templates::getTemplates('firebox'); |
| 465 |
|
| 466 |
if (!isset($templates->templates)) |
| 467 |
{ |
| 468 |
echo wp_json_encode([ |
| 469 |
'templates' => [] |
| 470 |
]); |
| 471 |
wp_die(); |
| 472 |
} |
| 473 |
|
| 474 |
$templates = $templates->templates; |
| 475 |
|
| 476 |
// Find top trending templates |
| 477 |
usort($templates, function($a, $b) { |
| 478 |
return $b->sort->trending - $a->sort->trending; |
| 479 |
}); |
| 480 |
|
| 481 |
// First top 6 trending templates |
| 482 |
$templates = array_slice($templates, 0, 6); |
| 483 |
|
| 484 |
echo wp_json_encode($templates); |
| 485 |
wp_die(); |
| 486 |
} |
| 487 |
|
| 488 |
private function getBoxAnalyticsTotals($id = null, $start_date = null, $end_date = null) |
| 489 |
{ |
| 490 |
if (!$id || !$start_date || !$end_date) |
| 491 |
{ |
| 492 |
return; |
| 493 |
} |
| 494 |
|
| 495 |
$data = new \FireBox\Core\Analytics\Data($start_date, $end_date); |
| 496 |
|
| 497 |
$metrics = [ |
| 498 |
'views', |
| 499 |
'conversions', |
| 500 |
'conversionrate' |
| 501 |
]; |
| 502 |
$data->setMetrics($metrics); |
| 503 |
|
| 504 |
$filters = [ |
| 505 |
'campaign' => [ |
| 506 |
'value' => [$id] |
| 507 |
] |
| 508 |
]; |
| 509 |
$data->setFilters($filters); |
| 510 |
|
| 511 |
return $data->getData('count'); |
| 512 |
} |
| 513 |
} |