fix(analytics): Corregir URLs en analytics usando post_name desde BD

Problema:
- URLs en analytics mostraban dominio incorrecto (HTTP_HOST)
- URLs usaban formato /?p=ID en lugar de permalinks

Solución:
- class-search-engine.php: Agregar propiedad $prefix, incluir p.post_name
  en 5 queries fetch, agregar helpers getSiteUrlFromDb(),
  getPermalinkStructure() y buildPermalink()
- search-endpoint.php: Obtener site_url y permalink_structure desde
  wp_options, construir URLs con post_name
- click-endpoint.php: Fallback de dest usando post_name desde BD

Archivos modificados:
- includes/class-search-engine.php
- api/search-endpoint.php
- api/click-endpoint.php

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
FrankZamora
2025-12-03 10:59:56 -06:00
commit 81d65c0f9a
10 changed files with 3220 additions and 0 deletions

102
api/click-endpoint.php Normal file
View File

@@ -0,0 +1,102 @@
<?php
/**
* Click Tracking Endpoint with SHORTINIT bypass
*
* This endpoint logs clicks and redirects to the destination.
* Use this directly via plugin URL for fastest tracking.
*
* @package ROI_APU_Search
*/
declare(strict_types=1);
// BYPASS: Don't load full WordPress
define('SHORTINIT', true);
define('WP_USE_THEMES', false);
// Find wp-load.php (go up from plugin directory)
$wp_load = dirname(__FILE__, 5) . '/wp-load.php';
if (!file_exists($wp_load)) {
// Fallback: try alternative paths
$wp_load = dirname(__FILE__, 6) . '/wp-load.php';
if (!file_exists($wp_load)) {
// Can't log, just redirect
$postId = isset($_GET['pid']) ? (int) $_GET['pid'] : 0;
$dest = isset($_GET['dest']) ? $_GET['dest'] : '/?p=' . $postId;
header('Location: ' . $dest, true, 302);
exit;
}
}
require_once $wp_load;
// Get request parameters
$searchId = isset($_GET['sid']) ? (int) $_GET['sid'] : 0;
$postId = isset($_GET['pid']) ? (int) $_GET['pid'] : 0;
$position = isset($_GET['pos']) ? (int) $_GET['pos'] : 0;
$page = isset($_GET['page']) ? (int) $_GET['page'] : 1;
$dest = isset($_GET['dest']) ? $_GET['dest'] : '';
// Load plugin classes first (needed for DB access)
$plugin_dir = dirname(__FILE__, 2);
require_once $plugin_dir . '/includes/class-db-connection.php';
require_once $plugin_dir . '/includes/class-analytics.php';
try {
// Get database connection
$db = ROI_APU_Search_DB::get_instance();
$pdo = $db->get_pdo();
$prefix = $db->get_prefix();
// Build destination URL if empty
if (empty($dest)) {
// Obtain site_url from database (not HTTP_HOST)
$stmt = $pdo->prepare(
"SELECT option_value FROM {$prefix}options
WHERE option_name = 'home' LIMIT 1"
);
$stmt->execute();
$home = $stmt->fetch(PDO::FETCH_ASSOC);
$site_url = $home ? rtrim($home['option_value'], '/') : '';
// Try to get post_name for proper permalink
$stmt = $pdo->prepare(
"SELECT post_name FROM {$prefix}posts WHERE ID = ? LIMIT 1"
);
$stmt->execute([$postId]);
$post = $stmt->fetch(PDO::FETCH_ASSOC);
if ($post && !empty($post['post_name'])) {
$dest = $site_url . '/' . $post['post_name'] . '/';
} else {
$dest = $site_url . '/?p=' . $postId;
}
}
// Get analytics config
$cfg = ROI_APU_Search_Analytics::get_config();
// Log click
ROI_APU_Search_Analytics::logClick(
$pdo,
$prefix,
$cfg,
$searchId,
$postId,
max(1, $position),
max(1, $page),
$dest
);
} catch (\Throwable $e) {
error_log('ROI APU Search Click Error: ' . $e->getMessage());
// Never break redirect due to tracking
// Fallback dest if error occurred before setting it
if (empty($dest)) {
$dest = '/?p=' . $postId;
}
}
// Redirect to destination
header('Location: ' . $dest, true, 302);
exit;