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>
126 lines
4.4 KiB
PHP
126 lines
4.4 KiB
PHP
<?php
|
|
/**
|
|
* Shortcode Handler
|
|
*
|
|
* @package ROI_APU_Search
|
|
*/
|
|
|
|
declare(strict_types=1);
|
|
|
|
// Prevent direct access
|
|
if (!defined('ABSPATH')) {
|
|
exit;
|
|
}
|
|
|
|
/**
|
|
* Handles the [roi_apu_search] shortcode
|
|
*/
|
|
final class ROI_APU_Search_Shortcode
|
|
{
|
|
/**
|
|
* Render the shortcode
|
|
*
|
|
* @param array|string $atts Shortcode attributes
|
|
* @return string HTML output
|
|
*/
|
|
public function render($atts): string
|
|
{
|
|
$atts = shortcode_atts([
|
|
'categories' => '', // Comma-separated category IDs or slugs
|
|
'per_page' => 10, // Results per page
|
|
'placeholder' => 'Buscar analisis de precios unitarios...',
|
|
'min_chars' => 3, // Minimum characters to search
|
|
'show_info' => 'true', // Show search info (total, time)
|
|
], $atts, 'roi_apu_search');
|
|
|
|
// Generate unique ID for multiple shortcodes on same page
|
|
$instance_id = 'roi-apu-' . wp_unique_id();
|
|
|
|
// Parse categories to validate
|
|
$categories = $this->sanitize_categories($atts['categories']);
|
|
$per_page = max(1, min(100, (int) $atts['per_page']));
|
|
$min_chars = max(1, min(10, (int) $atts['min_chars']));
|
|
$show_info = filter_var($atts['show_info'], FILTER_VALIDATE_BOOLEAN);
|
|
|
|
ob_start();
|
|
?>
|
|
<div id="<?php echo esc_attr($instance_id); ?>"
|
|
class="roi-apu-search-container"
|
|
data-categories="<?php echo esc_attr($categories); ?>"
|
|
data-per-page="<?php echo esc_attr((string) $per_page); ?>"
|
|
data-min-chars="<?php echo esc_attr((string) $min_chars); ?>"
|
|
data-show-info="<?php echo esc_attr($show_info ? 'true' : 'false'); ?>">
|
|
|
|
<!-- Search Form -->
|
|
<div class="roi-apu-search-form">
|
|
<div class="roi-apu-input-wrapper">
|
|
<input type="text"
|
|
class="roi-apu-search-input"
|
|
placeholder="<?php echo esc_attr($atts['placeholder']); ?>"
|
|
minlength="<?php echo esc_attr((string) $min_chars); ?>"
|
|
maxlength="250"
|
|
autocomplete="off">
|
|
<button type="button" class="roi-apu-search-btn">
|
|
<svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" fill="currentColor" viewBox="0 0 16 16">
|
|
<path d="M11.742 10.344a6.5 6.5 0 1 0-1.397 1.398h-.001c.03.04.062.078.098.115l3.85 3.85a1 1 0 0 0 1.415-1.414l-3.85-3.85a1.007 1.007 0 0 0-.115-.1zM12 6.5a5.5 5.5 0 1 1-11 0 5.5 5.5 0 0 1 11 0z"/>
|
|
</svg>
|
|
<span class="roi-apu-btn-text">Buscar</span>
|
|
</button>
|
|
</div>
|
|
<div class="roi-apu-error" style="display: none;"></div>
|
|
</div>
|
|
|
|
<!-- Loading Indicator -->
|
|
<div class="roi-apu-loading" style="display: none;">
|
|
<div class="roi-apu-spinner"></div>
|
|
<span>Buscando...</span>
|
|
</div>
|
|
|
|
<!-- Search Info -->
|
|
<div class="roi-apu-info" style="display: none;"></div>
|
|
|
|
<!-- Results Container -->
|
|
<div class="roi-apu-results"></div>
|
|
|
|
<!-- Pagination -->
|
|
<div class="roi-apu-pagination" style="display: none;"></div>
|
|
</div>
|
|
<?php
|
|
return ob_get_clean();
|
|
}
|
|
|
|
/**
|
|
* Sanitize and validate categories string
|
|
*
|
|
* @param string $categories Comma-separated categories
|
|
* @return string Sanitized categories string
|
|
*/
|
|
private function sanitize_categories(string $categories): string
|
|
{
|
|
if (empty($categories)) {
|
|
return '';
|
|
}
|
|
|
|
$parts = array_filter(array_map('trim', explode(',', $categories)));
|
|
$sanitized = [];
|
|
|
|
foreach ($parts as $part) {
|
|
if (is_numeric($part)) {
|
|
// Validate category exists
|
|
$term = get_term((int) $part, 'category');
|
|
if ($term && !is_wp_error($term)) {
|
|
$sanitized[] = (int) $part;
|
|
}
|
|
} else {
|
|
// Try by slug
|
|
$term = get_term_by('slug', sanitize_title($part), 'category');
|
|
if ($term) {
|
|
$sanitized[] = (int) $term->term_id;
|
|
}
|
|
}
|
|
}
|
|
|
|
return implode(',', array_unique($sanitized));
|
|
}
|
|
}
|