Analytics.php
102 lines
| 1 | <?php |
| 2 | |
| 3 | namespace EmbedPress\Analytics; |
| 4 | |
| 5 | /** |
| 6 | * EmbedPress Analytics Page Handler |
| 7 | * |
| 8 | * Manages the standalone analytics page with React component mounting |
| 9 | */ |
| 10 | class Analytics |
| 11 | { |
| 12 | /** |
| 13 | * Initialize hooks |
| 14 | */ |
| 15 | public function __construct() |
| 16 | { |
| 17 | add_action('admin_menu', [$this, 'register_submenu']); |
| 18 | add_action('admin_enqueue_scripts', [$this, 'enqueue_analytics_scripts']); |
| 19 | } |
| 20 | |
| 21 | /** |
| 22 | * Register the submenu page under EmbedPress menu |
| 23 | */ |
| 24 | public function register_submenu() |
| 25 | { |
| 26 | add_submenu_page( |
| 27 | 'embedpress', // Parent slug |
| 28 | __('Analytics', 'embedpress'), // Page title |
| 29 | __('Analytics', 'embedpress'), // Menu title |
| 30 | 'manage_options', // Capability |
| 31 | 'embedpress-analytics', // Menu slug |
| 32 | [$this, 'render_analytics_page'] // Callback to render page |
| 33 | ); |
| 34 | } |
| 35 | |
| 36 | /** |
| 37 | * Enqueue analytics scripts and styles |
| 38 | */ |
| 39 | public function enqueue_analytics_scripts($hook) |
| 40 | { |
| 41 | // Only load on the analytics page |
| 42 | if ($hook !== 'embedpress_page_embedpress-analytics') { |
| 43 | return; |
| 44 | } |
| 45 | |
| 46 | // Enqueue the analytics script (this should be built by Vite) |
| 47 | wp_enqueue_script( |
| 48 | 'embedpress-analytics', |
| 49 | EMBEDPRESS_URL_ASSETS . 'js/analytics.build.js', |
| 50 | ['wp-element'], |
| 51 | EMBEDPRESS_PLUGIN_VERSION, |
| 52 | true |
| 53 | ); |
| 54 | |
| 55 | // Add module attribute for ES modules |
| 56 | add_filter('script_loader_tag', function($tag, $handle) { |
| 57 | if ($handle === 'embedpress-analytics') { |
| 58 | return str_replace('<script ', '<script type="module" ', $tag); |
| 59 | } |
| 60 | return $tag; |
| 61 | }, 10, 2); |
| 62 | |
| 63 | // Enqueue analytics styles |
| 64 | wp_enqueue_style( |
| 65 | 'embedpress-analytics', |
| 66 | EMBEDPRESS_URL_ASSETS . 'css/analytics.build.css', |
| 67 | [], |
| 68 | EMBEDPRESS_PLUGIN_VERSION |
| 69 | ); |
| 70 | |
| 71 | // Localize script with API settings |
| 72 | $tracking_enabled = get_option('embedpress_analytics_tracking_enabled', true); |
| 73 | wp_localize_script('embedpress-analytics', 'embedpressAnalyticsData', [ |
| 74 | 'restUrl' => rest_url('embedpress/v1/analytics/'), |
| 75 | 'nonce' => wp_create_nonce('wp_rest'), |
| 76 | 'ajaxUrl' => admin_url('admin-ajax.php'), |
| 77 | 'cacheNonce' => wp_create_nonce('embedpress_clear_cache'), |
| 78 | 'isProActive' => defined('EMBEDPRESS_SL_ITEM_SLUG'), |
| 79 | 'currentUser' => wp_get_current_user()->ID, |
| 80 | 'siteUrl' => site_url(), |
| 81 | 'trackingEnabled' => (bool) $tracking_enabled, |
| 82 | ]); |
| 83 | |
| 84 | // Also make WordPress REST API settings available |
| 85 | wp_localize_script('embedpress-analytics', 'wpApiSettings', [ |
| 86 | 'root' => rest_url(), |
| 87 | 'nonce' => wp_create_nonce('wp_rest'), |
| 88 | ]); |
| 89 | } |
| 90 | |
| 91 | /** |
| 92 | * Render the Analytics page with a React root div |
| 93 | */ |
| 94 | public function render_analytics_page() |
| 95 | { |
| 96 | ?> |
| 97 | <div id="embedpress-analytics-root"></div> |
| 98 | |
| 99 | <?php |
| 100 | } |
| 101 | } |
| 102 |