5846c515f3
- 4 temas WordPress: persona-cv (activo), lumina, apex-cv, cvolio - Plugin pflores-cv-fields con CMB2 (100% gratuito) — admin editable - Generador PDF dinámico con Chromium headless (?cv_pdf=1) - Datos del CV de Patricia Flores cargados en CMB2 - URL dinámica con soporte proxy SSL (NPM/X-Forwarded-Proto) - WP Mail SMTP instalado (pendiente config SMTP) - Documentación completa: PROMPT_IA.md, DEPLOY_WEB.md, PRODUCCION.md, VERSIONADO.md
119 lines
3.5 KiB
PHP
119 lines
3.5 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
function cvolio_should_track_visit(): bool
|
|
{
|
|
if (is_admin() || wp_doing_ajax() || wp_doing_cron()) {
|
|
return false;
|
|
}
|
|
|
|
if (! isset($_SERVER['REQUEST_METHOD']) || $_SERVER['REQUEST_METHOD'] !== 'GET') {
|
|
return false;
|
|
}
|
|
|
|
if (is_user_logged_in() && current_user_can('manage_options')) {
|
|
return false;
|
|
}
|
|
|
|
return is_front_page() || is_home() || is_singular();
|
|
}
|
|
|
|
function cvolio_track_visit(): void
|
|
{
|
|
if (! cvolio_should_track_visit()) {
|
|
return;
|
|
}
|
|
|
|
$requestUri = isset($_SERVER['REQUEST_URI']) ? sanitize_text_field(wp_unslash($_SERVER['REQUEST_URI'])) : '';
|
|
$visitorHash = md5($requestUri . '|' . cvolio_get_client_ip() . '|' . gmdate('Y-m-d-H'));
|
|
$transientKey = 'cvolio_visit_' . $visitorHash;
|
|
|
|
if (get_transient($transientKey)) {
|
|
return;
|
|
}
|
|
|
|
set_transient($transientKey, '1', HOUR_IN_SECONDS);
|
|
|
|
$total = (int) get_option('cvolio_total_visits', 0);
|
|
update_option('cvolio_total_visits', $total + 1, false);
|
|
|
|
if (is_front_page()) {
|
|
$frontPage = (int) get_option('page_on_front');
|
|
if ($frontPage > 0) {
|
|
$views = (int) get_post_meta($frontPage, '_cvolio_views', true);
|
|
update_post_meta($frontPage, '_cvolio_views', $views + 1);
|
|
}
|
|
|
|
return;
|
|
}
|
|
|
|
$postId = get_queried_object_id();
|
|
if ($postId > 0) {
|
|
$views = (int) get_post_meta($postId, '_cvolio_views', true);
|
|
update_post_meta($postId, '_cvolio_views', $views + 1);
|
|
}
|
|
}
|
|
add_action('template_redirect', 'cvolio_track_visit');
|
|
|
|
function cvolio_get_client_ip(): string
|
|
{
|
|
$keys = [
|
|
'HTTP_X_FORWARDED_FOR',
|
|
'HTTP_CLIENT_IP',
|
|
'REMOTE_ADDR',
|
|
];
|
|
|
|
foreach ($keys as $key) {
|
|
if (! empty($_SERVER[$key])) {
|
|
$value = explode(',', (string) $_SERVER[$key]);
|
|
return trim($value[0]);
|
|
}
|
|
}
|
|
|
|
return '0.0.0.0';
|
|
}
|
|
|
|
function cvolio_register_visits_dashboard_widget(): void
|
|
{
|
|
wp_add_dashboard_widget(
|
|
'cvolio_visits_widget',
|
|
__('CVolio: Visitas', 'cvolio'),
|
|
'cvolio_render_visits_dashboard_widget'
|
|
);
|
|
}
|
|
add_action('wp_dashboard_setup', 'cvolio_register_visits_dashboard_widget');
|
|
|
|
function cvolio_render_visits_dashboard_widget(): void
|
|
{
|
|
$total = (int) get_option('cvolio_total_visits', 0);
|
|
$frontPage = (int) get_option('page_on_front');
|
|
$frontViews = $frontPage > 0 ? (int) get_post_meta($frontPage, '_cvolio_views', true) : 0;
|
|
?>
|
|
<div class="cvolio-visits-widget">
|
|
<p><strong><?php esc_html_e('Visitas totales registradas:', 'cvolio'); ?></strong> <?php echo esc_html((string) $total); ?></p>
|
|
<p><strong><?php esc_html_e('Visitas a la portada:', 'cvolio'); ?></strong> <?php echo esc_html((string) $frontViews); ?></p>
|
|
<p><?php esc_html_e('El contador evita duplicados del mismo visitante en la misma URL durante una hora.', 'cvolio'); ?></p>
|
|
</div>
|
|
<?php
|
|
}
|
|
|
|
function cvolio_add_views_column(array $columns): array
|
|
{
|
|
$columns['cvolio_views'] = __('Visitas', 'cvolio');
|
|
return $columns;
|
|
}
|
|
add_filter('manage_pages_columns', 'cvolio_add_views_column');
|
|
add_filter('manage_posts_columns', 'cvolio_add_views_column');
|
|
|
|
function cvolio_render_views_column(string $column, int $postId): void
|
|
{
|
|
if ($column !== 'cvolio_views') {
|
|
return;
|
|
}
|
|
|
|
echo esc_html((string) ((int) get_post_meta($postId, '_cvolio_views', true)));
|
|
}
|
|
add_action('manage_pages_custom_column', 'cvolio_render_views_column', 10, 2);
|
|
add_action('manage_posts_custom_column', 'cvolio_render_views_column', 10, 2);
|