Analytics_Manager.php
9 months ago
Browser_Detector.php
9 months ago
Content_Cache_Manager.php
9 months ago
Data_Collector.php
7 months ago
Email_Reports.php
9 months ago
Export_Manager.php
9 months ago
License_Manager.php
9 months ago
Milestone_Manager.php
9 months ago
Pro_Data_Collector.php
9 months ago
REST_API.php
9 months ago
Email_Reports.php
429 lines
| 1 | <?php |
| 2 | |
| 3 | namespace EmbedPress\Includes\Classes\Analytics; |
| 4 | |
| 5 | /** |
| 6 | * EmbedPress Analytics Email Reports (Pro Feature) |
| 7 | * |
| 8 | * Handles automated email reports for analytics data |
| 9 | * |
| 10 | * @package EmbedPress |
| 11 | * @author EmbedPress <help@embedpress.com> |
| 12 | * @copyright Copyright (C) 2023 WPDeveloper. All rights reserved. |
| 13 | * @license GPLv3 or later |
| 14 | * @since 4.2.7 |
| 15 | */ |
| 16 | class Email_Reports |
| 17 | { |
| 18 | /** |
| 19 | * Data collector instance |
| 20 | * |
| 21 | * @var Data_Collector |
| 22 | */ |
| 23 | private $data_collector; |
| 24 | |
| 25 | /** |
| 26 | * Constructor |
| 27 | */ |
| 28 | public function __construct() |
| 29 | { |
| 30 | $this->data_collector = new Data_Collector(); |
| 31 | $this->init_hooks(); |
| 32 | } |
| 33 | |
| 34 | /** |
| 35 | * Initialize hooks |
| 36 | * |
| 37 | * @return void |
| 38 | */ |
| 39 | private function init_hooks() |
| 40 | { |
| 41 | // Schedule email reports |
| 42 | add_action('init', [$this, 'schedule_reports']); |
| 43 | |
| 44 | // Handle scheduled reports |
| 45 | add_action('embedpress_weekly_analytics_report', [$this, 'send_weekly_report']); |
| 46 | add_action('embedpress_monthly_analytics_report', [$this, 'send_monthly_report']); |
| 47 | |
| 48 | // Admin settings |
| 49 | add_action('admin_init', [$this, 'register_settings']); |
| 50 | } |
| 51 | |
| 52 | /** |
| 53 | * Schedule email reports |
| 54 | * |
| 55 | * @return void |
| 56 | */ |
| 57 | public function schedule_reports() |
| 58 | { |
| 59 | // Check if pro feature is available |
| 60 | if (!License_Manager::has_analytics_feature('email_reports')) { |
| 61 | return; |
| 62 | } |
| 63 | |
| 64 | $settings = $this->get_email_settings(); |
| 65 | |
| 66 | if ($settings['weekly_enabled']) { |
| 67 | if (!wp_next_scheduled('embedpress_weekly_analytics_report')) { |
| 68 | wp_schedule_event(time(), 'weekly', 'embedpress_weekly_analytics_report'); |
| 69 | } |
| 70 | } else { |
| 71 | wp_clear_scheduled_hook('embedpress_weekly_analytics_report'); |
| 72 | } |
| 73 | |
| 74 | if ($settings['monthly_enabled']) { |
| 75 | if (!wp_next_scheduled('embedpress_monthly_analytics_report')) { |
| 76 | wp_schedule_event(time(), 'monthly', 'embedpress_monthly_analytics_report'); |
| 77 | } |
| 78 | } else { |
| 79 | wp_clear_scheduled_hook('embedpress_monthly_analytics_report'); |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | /** |
| 84 | * Send weekly report |
| 85 | * |
| 86 | * @return void |
| 87 | */ |
| 88 | public function send_weekly_report() |
| 89 | { |
| 90 | if (!License_Manager::has_analytics_feature('email_reports')) { |
| 91 | return; |
| 92 | } |
| 93 | |
| 94 | $settings = $this->get_email_settings(); |
| 95 | |
| 96 | if (!$settings['weekly_enabled']) { |
| 97 | return; |
| 98 | } |
| 99 | |
| 100 | $data = $this->get_report_data(7); |
| 101 | $this->send_report_email('weekly', $data, $settings); |
| 102 | } |
| 103 | |
| 104 | /** |
| 105 | * Send monthly report |
| 106 | * |
| 107 | * @return void |
| 108 | */ |
| 109 | public function send_monthly_report() |
| 110 | { |
| 111 | if (!License_Manager::has_analytics_feature('email_reports')) { |
| 112 | return; |
| 113 | } |
| 114 | |
| 115 | $settings = $this->get_email_settings(); |
| 116 | |
| 117 | if (!$settings['monthly_enabled']) { |
| 118 | return; |
| 119 | } |
| 120 | |
| 121 | $data = $this->get_report_data(30); |
| 122 | $this->send_report_email('monthly', $data, $settings); |
| 123 | } |
| 124 | |
| 125 | /** |
| 126 | * Get report data |
| 127 | * |
| 128 | * @param int $days |
| 129 | * @return array |
| 130 | */ |
| 131 | private function get_report_data($days) |
| 132 | { |
| 133 | $args = ['date_range' => $days]; |
| 134 | |
| 135 | return [ |
| 136 | 'period' => $days, |
| 137 | 'content_by_type' => $this->data_collector->get_total_content_by_type(), |
| 138 | 'views_analytics' => $this->data_collector->get_views_analytics($args), |
| 139 | 'total_unique_viewers' => $this->data_collector->get_total_unique_viewers($args), |
| 140 | 'browser_analytics' => $this->data_collector->get_browser_analytics($args), |
| 141 | 'geo_analytics' => $this->data_collector->get_geo_analytics($args), |
| 142 | 'device_analytics' => $this->data_collector->get_device_analytics($args), |
| 143 | 'referral_analytics' => $this->data_collector->get_referral_analytics($args) |
| 144 | ]; |
| 145 | } |
| 146 | |
| 147 | /** |
| 148 | * Send report email |
| 149 | * |
| 150 | * @param string $type |
| 151 | * @param array $data |
| 152 | * @param array $settings |
| 153 | * @return bool |
| 154 | */ |
| 155 | private function send_report_email($type, $data, $settings) |
| 156 | { |
| 157 | $subject = sprintf( |
| 158 | __('EmbedPress %s Analytics Report - %s', 'embedpress'), |
| 159 | ucfirst($type), |
| 160 | get_bloginfo('name') |
| 161 | ); |
| 162 | |
| 163 | $message = $this->generate_email_content($type, $data); |
| 164 | |
| 165 | $headers = [ |
| 166 | 'Content-Type: text/html; charset=UTF-8', |
| 167 | 'From: ' . get_bloginfo('name') . ' <' . get_option('admin_email') . '>' |
| 168 | ]; |
| 169 | |
| 170 | $recipients = $this->get_email_recipients($settings); |
| 171 | |
| 172 | $sent = false; |
| 173 | foreach ($recipients as $email) { |
| 174 | if (is_email($email)) { |
| 175 | $sent = wp_mail($email, $subject, $message, $headers) || $sent; |
| 176 | } |
| 177 | } |
| 178 | |
| 179 | return $sent; |
| 180 | } |
| 181 | |
| 182 | /** |
| 183 | * Generate email content |
| 184 | * |
| 185 | * @param string $type |
| 186 | * @param array $data |
| 187 | * @return string |
| 188 | */ |
| 189 | private function generate_email_content($type, $data) |
| 190 | { |
| 191 | $period_text = $type === 'weekly' ? __('last 7 days', 'embedpress') : __('last 30 days', 'embedpress'); |
| 192 | |
| 193 | ob_start(); |
| 194 | ?> |
| 195 | <!DOCTYPE html> |
| 196 | <html> |
| 197 | <head> |
| 198 | <meta charset="UTF-8"> |
| 199 | <title><?php echo esc_html(sprintf(__('EmbedPress %s Analytics Report', 'embedpress'), ucfirst($type))); ?></title> |
| 200 | <style> |
| 201 | body { font-family: Arial, sans-serif; line-height: 1.6; color: #333; } |
| 202 | .container { max-width: 600px; margin: 0 auto; padding: 20px; } |
| 203 | .header { background: #3498db; color: white; padding: 20px; text-align: center; } |
| 204 | .content { padding: 20px; background: #f9f9f9; } |
| 205 | .metric { background: white; padding: 15px; margin: 10px 0; border-radius: 5px; } |
| 206 | .metric h3 { margin: 0 0 10px 0; color: #3498db; } |
| 207 | .metric .value { font-size: 24px; font-weight: bold; color: #2c3e50; } |
| 208 | .table { width: 100%; border-collapse: collapse; margin: 10px 0; } |
| 209 | .table th, .table td { padding: 8px; text-align: left; border-bottom: 1px solid #ddd; } |
| 210 | .table th { background: #3498db; color: white; } |
| 211 | .footer { text-align: center; padding: 20px; color: #666; } |
| 212 | </style> |
| 213 | </head> |
| 214 | <body> |
| 215 | <div class="container"> |
| 216 | <div class="header"> |
| 217 | <h1><?php echo esc_html(sprintf(__('EmbedPress %s Analytics Report', 'embedpress'), ucfirst($type))); ?></h1> |
| 218 | <p><?php echo esc_html(sprintf(__('Analytics summary for %s', 'embedpress'), $period_text)); ?></p> |
| 219 | </div> |
| 220 | |
| 221 | <div class="content"> |
| 222 | <!-- Overview Metrics --> |
| 223 | <h2><?php _e('Overview', 'embedpress'); ?></h2> |
| 224 | |
| 225 | <div class="metric"> |
| 226 | <h3><?php _e('Total Embeds', 'embedpress'); ?></h3> |
| 227 | <div class="value"><?php echo esc_html($data['content_by_type']['total']); ?></div> |
| 228 | <p> |
| 229 | Elementor: <?php echo esc_html($data['content_by_type']['elementor']); ?> | |
| 230 | Gutenberg: <?php echo esc_html($data['content_by_type']['gutenberg']); ?> | |
| 231 | Shortcode: <?php echo esc_html($data['content_by_type']['shortcode']); ?> |
| 232 | </p> |
| 233 | </div> |
| 234 | |
| 235 | <div class="metric"> |
| 236 | <h3><?php _e('Total Views', 'embedpress'); ?></h3> |
| 237 | <div class="value"><?php echo esc_html($data['views_analytics']['total_views']); ?></div> |
| 238 | </div> |
| 239 | |
| 240 | <div class="metric"> |
| 241 | <h3><?php _e('Unique Viewers', 'embedpress'); ?></h3> |
| 242 | <div class="value"><?php echo esc_html($data['total_unique_viewers']); ?></div> |
| 243 | </div> |
| 244 | |
| 245 | <!-- Top Content --> |
| 246 | <?php if (!empty($data['views_analytics']['top_content'])): ?> |
| 247 | <h2><?php _e('Top Performing Content', 'embedpress'); ?></h2> |
| 248 | <table class="table"> |
| 249 | <thead> |
| 250 | <tr> |
| 251 | <th><?php _e('Content', 'embedpress'); ?></th> |
| 252 | <th><?php _e('Type', 'embedpress'); ?></th> |
| 253 | <th><?php _e('Views', 'embedpress'); ?></th> |
| 254 | <th><?php _e('Clicks', 'embedpress'); ?></th> |
| 255 | <th><?php _e('Impressions', 'embedpress'); ?></th> |
| 256 | </tr> |
| 257 | </thead> |
| 258 | <tbody> |
| 259 | <?php foreach (array_slice($data['views_analytics']['top_content'], 0, 5) as $content): ?> |
| 260 | <tr> |
| 261 | <td><?php echo esc_html($content['title'] ?: 'Untitled'); ?></td> |
| 262 | <td><?php echo esc_html($content['embed_type']); ?></td> |
| 263 | <td><?php echo esc_html($content['total_views']); ?></td> |
| 264 | <td><?php echo esc_html($content['total_clicks']); ?></td> |
| 265 | <td><?php echo esc_html($content['total_impressions'] ?? 0); ?></td> |
| 266 | </tr> |
| 267 | <?php endforeach; ?> |
| 268 | </tbody> |
| 269 | </table> |
| 270 | <?php endif; ?> |
| 271 | |
| 272 | <!-- Geo Analytics (Pro) --> |
| 273 | <?php if (!empty($data['geo_analytics']['countries'])): ?> |
| 274 | <h2><?php _e('Top Countries', 'embedpress'); ?></h2> |
| 275 | <table class="table"> |
| 276 | <thead> |
| 277 | <tr> |
| 278 | <th><?php _e('Country', 'embedpress'); ?></th> |
| 279 | <th><?php _e('Visitors', 'embedpress'); ?></th> |
| 280 | </tr> |
| 281 | </thead> |
| 282 | <tbody> |
| 283 | <?php foreach (array_slice($data['geo_analytics']['countries'], 0, 5) as $country): ?> |
| 284 | <tr> |
| 285 | <td><?php echo esc_html($country['country']); ?></td> |
| 286 | <td><?php echo esc_html($country['visitors']); ?></td> |
| 287 | </tr> |
| 288 | <?php endforeach; ?> |
| 289 | </tbody> |
| 290 | </table> |
| 291 | <?php endif; ?> |
| 292 | |
| 293 | <!-- Device Analytics (Pro) --> |
| 294 | <?php if (!empty($data['device_analytics']['devices'])): ?> |
| 295 | <h2><?php _e('Device Types', 'embedpress'); ?></h2> |
| 296 | <table class="table"> |
| 297 | <thead> |
| 298 | <tr> |
| 299 | <th><?php _e('Device', 'embedpress'); ?></th> |
| 300 | <th><?php _e('Visitors', 'embedpress'); ?></th> |
| 301 | </tr> |
| 302 | </thead> |
| 303 | <tbody> |
| 304 | <?php foreach ($data['device_analytics']['devices'] as $device): ?> |
| 305 | <tr> |
| 306 | <td><?php echo esc_html(ucfirst($device['device_type'])); ?></td> |
| 307 | <td><?php echo esc_html($device['visitors']); ?></td> |
| 308 | </tr> |
| 309 | <?php endforeach; ?> |
| 310 | </tbody> |
| 311 | </table> |
| 312 | <?php endif; ?> |
| 313 | </div> |
| 314 | |
| 315 | <div class="footer"> |
| 316 | <p><?php _e('This report was generated automatically by EmbedPress Pro.', 'embedpress'); ?></p> |
| 317 | <p><a href="<?php echo esc_url(admin_url('admin.php?page=embedpress#/analytics')); ?>"><?php _e('View Full Analytics Dashboard', 'embedpress'); ?></a></p> |
| 318 | </div> |
| 319 | </div> |
| 320 | </body> |
| 321 | </html> |
| 322 | <?php |
| 323 | |
| 324 | return ob_get_clean(); |
| 325 | } |
| 326 | |
| 327 | /** |
| 328 | * Get email recipients |
| 329 | * |
| 330 | * @param array $settings |
| 331 | * @return array |
| 332 | */ |
| 333 | private function get_email_recipients($settings) |
| 334 | { |
| 335 | $recipients = []; |
| 336 | |
| 337 | if (!empty($settings['recipients'])) { |
| 338 | $emails = explode(',', $settings['recipients']); |
| 339 | foreach ($emails as $email) { |
| 340 | $email = trim($email); |
| 341 | if (is_email($email)) { |
| 342 | $recipients[] = $email; |
| 343 | } |
| 344 | } |
| 345 | } |
| 346 | |
| 347 | // Fallback to admin email |
| 348 | if (empty($recipients)) { |
| 349 | $recipients[] = get_option('admin_email'); |
| 350 | } |
| 351 | |
| 352 | return $recipients; |
| 353 | } |
| 354 | |
| 355 | /** |
| 356 | * Get email settings |
| 357 | * |
| 358 | * @return array |
| 359 | */ |
| 360 | private function get_email_settings() |
| 361 | { |
| 362 | $defaults = [ |
| 363 | 'weekly_enabled' => false, |
| 364 | 'monthly_enabled' => false, |
| 365 | 'recipients' => get_option('admin_email') |
| 366 | ]; |
| 367 | |
| 368 | $settings = get_option('embedpress_email_reports', []); |
| 369 | |
| 370 | return wp_parse_args($settings, $defaults); |
| 371 | } |
| 372 | |
| 373 | /** |
| 374 | * Register settings |
| 375 | * |
| 376 | * @return void |
| 377 | */ |
| 378 | public function register_settings() |
| 379 | { |
| 380 | register_setting('embedpress_settings', 'embedpress_email_reports'); |
| 381 | } |
| 382 | |
| 383 | /** |
| 384 | * Update email settings |
| 385 | * |
| 386 | * @param array $settings |
| 387 | * @return bool |
| 388 | */ |
| 389 | public function update_settings($settings) |
| 390 | { |
| 391 | if (!License_Manager::has_analytics_feature('email_reports')) { |
| 392 | return false; |
| 393 | } |
| 394 | |
| 395 | $updated = update_option('embedpress_email_reports', $settings); |
| 396 | |
| 397 | if ($updated) { |
| 398 | // Reschedule reports |
| 399 | wp_clear_scheduled_hook('embedpress_weekly_analytics_report'); |
| 400 | wp_clear_scheduled_hook('embedpress_monthly_analytics_report'); |
| 401 | $this->schedule_reports(); |
| 402 | } |
| 403 | |
| 404 | return $updated; |
| 405 | } |
| 406 | |
| 407 | /** |
| 408 | * Send test email |
| 409 | * |
| 410 | * @param string $email |
| 411 | * @return bool |
| 412 | */ |
| 413 | public function send_test_email($email) |
| 414 | { |
| 415 | if (!License_Manager::has_analytics_feature('email_reports')) { |
| 416 | return false; |
| 417 | } |
| 418 | |
| 419 | if (!is_email($email)) { |
| 420 | return false; |
| 421 | } |
| 422 | |
| 423 | $data = $this->get_report_data(7); |
| 424 | $settings = ['recipients' => $email]; |
| 425 | |
| 426 | return $this->send_report_email('test', $data, $settings); |
| 427 | } |
| 428 | } |
| 429 |