get(ContaiOnePlatformEndpoints::ANALYTICS_OAUTH_AUTHORIZE . '?website_id=' . urlencode($website_id)); if ($response->isSuccess()) { wp_send_json_success($response->getData()); } else { wp_send_json_error(['message' => $response->getMessage()]); } } public function handleCheckOauth(): void { check_ajax_referer('contai_analytics_oauth'); if (!current_user_can('manage_options')) wp_send_json_error('Insufficient permissions'); $website_id = isset($_POST['website_id']) ? sanitize_text_field(wp_unslash($_POST['website_id'])) : ''; if (empty($website_id)) wp_send_json_error('Website ID required'); $client = ContaiOnePlatformClient::create(); $response = $client->get(ContaiOnePlatformEndpoints::ANALYTICS_OAUTH_STATUS . '?website_id=' . urlencode($website_id)); if ($response->isSuccess()) { wp_send_json_success($response->getData()); } else { wp_send_json_error(['message' => $response->getMessage()]); } } public function handleSetup(): void { check_ajax_referer('contai_analytics_setup'); if (!current_user_can('manage_options')) wp_send_json_error('Insufficient permissions'); $website_id = isset($_POST['website_id']) ? sanitize_text_field(wp_unslash($_POST['website_id'])) : ''; if (empty($website_id)) wp_send_json_error('Website ID required'); $client = ContaiOnePlatformClient::create(); $response = $client->post(ContaiOnePlatformEndpoints::ANALYTICS_SETUP, [ 'website_id' => $website_id, 'account_id' => 'accounts/0', ]); if ($response->isSuccess()) { $data = $response->getData(); if (!empty($data['measurement_id'])) { update_option('1platform_ga4_measurement_id', sanitize_text_field($data['measurement_id']), false); } wp_send_json_success($data); } else { wp_send_json_error(['message' => $response->getMessage()]); } } public function handlePollStatus(): void { check_ajax_referer('contai_analytics_setup'); if (!current_user_can('manage_options')) wp_send_json_error('Insufficient permissions'); $website_id = isset($_POST['website_id']) ? sanitize_text_field(wp_unslash($_POST['website_id'])) : ''; if (empty($website_id)) wp_send_json_error('Website ID required'); $client = ContaiOnePlatformClient::create(); $response = $client->get(ContaiOnePlatformEndpoints::ANALYTICS_STATUS . '?website_id=' . urlencode($website_id)); if ($response->isSuccess()) { $data = $response->getData(); if (!empty($data['measurement_id']) && ($data['status'] ?? '') === 'active') { update_option('1platform_ga4_measurement_id', sanitize_text_field($data['measurement_id']), false); } wp_send_json_success($data); } else { wp_send_json_error(['message' => $response->getMessage()]); } } }