600, 'display' => __('Every 10 minutes', 'statinform'), ]; return $s; }); add_action('init', function () { if (!wp_next_scheduled('eistat_aggregate_event')) { wp_schedule_event(time() + 60, 'eistat_ten_minutes', 'eistat_aggregate_event'); } }); add_action('eistat_aggregate_event', function () { $result = EISTAT_Aggregator::run(); if ($result !== false) { update_option('eistat_last_aggregation_ts', time()); } }); /** * Detect the current page type for analytics. * * @return string */ function eistat_detect_page_type() { if (is_singular('post')) return 'post'; if (is_page()) return 'page'; if (is_category()) return 'category'; if (is_tag()) return 'tag'; if (is_tax()) return 'tax'; if (is_archive()) return 'archive'; if (is_home()) return 'home'; if (is_search()) return 'search'; if (is_404()) return '404'; return 'other'; } // --- Front-end tracker --- add_action('wp_enqueue_scripts', function () { if (is_admin()) return; wp_enqueue_script( 'eistat-tracker', EISTAT_URL . 'public/tracker.js', [], EISTAT_VERSION, true ); $request_uri = isset($_SERVER['REQUEST_URI']) ? sanitize_text_field(wp_unslash($_SERVER['REQUEST_URI'])) : '/'; wp_localize_script('eistat-tracker', 'EISTAT_CONFIG', [ 'endpoint' => eistat_get_data_url() . 'collect.php', 'page_id' => (int) get_queried_object_id(), 'page_type' => eistat_detect_page_type(), 'page_url' => substr($request_uri, 0, 255), ]); }); // --- Admin menu --- add_action('admin_menu', function () { add_menu_page( __('Analytics', 'statinform'), __('Analytics', 'statinform'), 'manage_options', 'eistat-dashboard', function () { require EISTAT_DIR . 'admin/dashboard.php'; }, 'dashicons-chart-bar', 30 ); }); // --- "Update now" button handler --- add_action('admin_post_eistat_run_aggregator', function () { if (!current_user_can('manage_options')) { wp_die(esc_html__('Access denied.', 'statinform')); } check_admin_referer('eistat_run_aggregator'); $start = microtime(true); $result = EISTAT_Aggregator::run(true); $elapsed = round((microtime(true) - $start) * 1000); $result_int = ($result === false) ? -1 : (int)$result; update_option('eistat_last_manual_run', current_time('mysql')); update_option('eistat_last_manual_run_ms', $elapsed); update_option('eistat_last_manual_run_result', $result_int); if ($result !== false) { update_option('eistat_last_aggregation_ts', time()); } wp_safe_redirect(add_query_arg([ 'page' => 'eistat-dashboard', 'eistat_updated' => '1', ], admin_url('admin.php'))); exit; }); // --- Archive download --- add_action('admin_post_eistat_download_archive', function () { if (!current_user_can('manage_options')) { wp_die(esc_html__('Access denied.', 'statinform')); } check_admin_referer('eistat_download_archive'); $name = isset($_GET['name']) ? sanitize_file_name(wp_unslash($_GET['name'])) : ''; EISTAT_Exporter::download_archive($name); }); // --- CSV export --- add_action('admin_post_eistat_export_csv', function () { if (!current_user_can('manage_options')) { wp_die(esc_html__('Access denied.', 'statinform')); } check_admin_referer('eistat_export_csv'); $table = isset($_POST['table']) ? sanitize_key(wp_unslash($_POST['table'])) : ''; $from = isset($_POST['from']) ? sanitize_text_field(wp_unslash($_POST['from'])) : ''; $to = isset($_POST['to']) ? sanitize_text_field(wp_unslash($_POST['to'])) : ''; if (!preg_match('/^\d{4}-\d{2}-\d{2}$/', $from) || !preg_match('/^\d{4}-\d{2}-\d{2}$/', $to)) { wp_die(esc_html__('Invalid period.', 'statinform')); } EISTAT_Exporter::download_csv($table, $from, $to); }); // --- Admin assets --- add_action('admin_enqueue_scripts', function ($hook) { if ($hook !== 'toplevel_page_eistat-dashboard') { return; } wp_enqueue_style( 'eistat-admin', EISTAT_URL . 'assets/admin.css', [], EISTAT_VERSION ); });