function gp_obtener_directorio_ligero($request) { $per_page = (int) $request->get_param('per_page'); if ( !$per_page || $per_page > 100 ) $per_page = 50; $page = (int) $request->get_param('page'); if ( !$page ) $page = 1; $filter = $request->get_param('filter'); $total_usuarios = 0; $paged_ids = array(); // ✅ OBTENER CREDENCIALES DESDE wp-config.php // (AJUSTA LOS NOMBRES 'SUPABASE_URL' Y 'SUPABASE_ANON_KEY' SI LOS LLAMASTE DIFERENTE EN TU CONFIG) $supabase_url = defined('SUPABASE_URL') ? SUPABASE_URL : ''; $supabase_key = defined('SUPABASE_ANON_KEY') ? SUPABASE_ANON_KEY : ''; // CAMINO 1: FILTRO "EN LÍNEA" (Tiempo real, ignora el caché) if ( $filter === 'online' ) { if ( empty($supabase_url) || empty($supabase_key) ) { return new WP_REST_Response(array('users' => array(), 'total' => 0, 'page' => $page, 'per_page' => $per_page), 200); } $fifteen_mins_ago = gmdate('Y-m-d\TH:i:s\Z', time() - 900); $ch = curl_init(); $query_url = $supabase_url . '/rest/v1/users_status?select=nicename&last_seen=gte.' . $fifteen_mins_ago; curl_setopt($ch, CURLOPT_URL, $query_url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_HTTPHEADER, [ 'apikey: ' . $supabase_key, 'Authorization: Bearer ' . $supabase_key ]); $response = curl_exec($ch); curl_close($ch); $online_ids = array(); if ( $response ) { $data = json_decode($response, true); if ( is_array($data) ) { global $wpdb; $nickenames = array(); foreach ( $data as $row ) { if ( !empty($row['nicename']) ) $nickenames[] = sanitize_text_field($row['nicename']); } if ( !empty($nickenames) ) { $placeholders = implode(',', array_fill(0, count($nickenames), '%s')); $query = $wpdb->prepare( "SELECT ID FROM {$wpdb->users} WHERE user_nicename IN ($placeholders)", ...$nickenames ); $online_ids = $wpdb->get_col($query); } } } $total_usuarios = count($online_ids); if ( empty($online_ids) ) { return new WP_REST_Response(array('users' => array(), 'total' => 0, 'page' => $page, 'per_page' => $per_page), 200); } $offset = ($page - 1) * $per_page; $paged_ids = array_slice($online_ids, $offset, $per_page); } // CAMINO 2: VISTA POR DEFECTO (Pool Híbrido: Activos de Supabase + WP con Foto) else { $pool = get_transient('gp_smart_directory_pool_v3'); if ( false === $pool ) { global $wpdb; $active_ids = array(); if ( !empty($supabase_url) && !empty($supabase_key) ) { $seven_days_ago = gmdate('Y-m-d\TH:i:s\Z', time() - (7 * 24 * 60 * 60)); $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $supabase_url . '/rest/v1/users_status?select=nicename&last_seen=gte.' . $seven_days_ago); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_HTTPHEADER, ['apikey: ' . $supabase_key, 'Authorization: Bearer ' . $supabase_key]); $supa_response = curl_exec($ch); curl_close($ch); if ( $supa_response ) { $supa_data = json_decode($supa_response, true); if ( is_array($supa_data) ) { $active_nickenames = array(); foreach ( $supa_data as $row ) { if ( !empty($row['nicename'] ) ) $active_nickenames[] = sanitize_text_field($row['nicename']); } if ( !empty($active_nickenames) ) { $placeholders = implode(',', array_fill(0, count($active_nickenames), '%s')); $query = $wpdb->prepare("SELECT ID FROM {$wpdb->users} WHERE user_nicename IN ($placeholders)", ...$active_nickenames); $active_ids = $wpdb->get_col($query); } } } } $exclude_str = !empty($active_ids) ? 'AND user_id NOT IN (' . implode(',', array_map('intval', $active_ids)) . ')' : ''; $wp_photo_ids = $wpdb->get_col(" SELECT DISTINCT user_id FROM {$wpdb->usermeta} WHERE meta_key IN ('gp_foto_perfil', 'gp_avatar_id', 'profile_photo') AND meta_value != '' $exclude_str ORDER BY user_id DESC LIMIT 1500 "); $pool = array_merge($active_ids, $wp_photo_ids); shuffle($pool); set_transient('gp_smart_directory_pool_v3', $pool, 3 * HOUR_IN_SECONDS); } $total_usuarios = count($pool); $offset = ($page - 1) * $per_page; $paged_ids = array_slice($pool, $offset, $per_page); } if ( empty($paged_ids) ) { return new WP_REST_Response(array('users' => array(), 'total' => $total_usuarios, 'page' => $page, 'per_page' => $per_page), 200); } $args = array( 'include' => $paged_ids, 'fields' => array('ID', 'display_name', 'user_nicename') ); $user_query = new WP_User_Query($args); $users = $user_query->get_results(); $data = array(); foreach ($users as $user) { $user_id = $user->ID; $avatar = function_exists('gp_get_real_avatar_url') ? gp_get_real_avatar_url($user_id) : ''; if ( strpos($avatar, 'gravatar') !== false || strpos($avatar, 'mystery') !== false || strpos($avatar, 'default') !== false ) { $avatar = ''; } $data[] = array( 'id' => $user_id, 'name' => $user->display_name, 'slug' => $user->user_nicename, 'gp_foto_url' => $avatar, 'gp_edad' => get_user_meta($user_id, 'gp_age', true) ?: get_user_meta($user_id, 'gp_edad', true), 'gp_distrito' => get_user_meta($user_id, 'gp_distrito', true) ?: get_user_meta($user_id, 'gp_district', true) ?: get_user_meta($user_id, 'gp_city', true), 'gp_rol' => get_user_meta($user_id, 'gp_rol', true) ?: get_user_meta($user_id, 'role', true), 'gp_genero' => get_user_meta($user_id, 'gp_genero', true) ); } return new WP_REST_Response(array( 'users' => $data, 'total' => $total_usuarios, 'page' => $page, 'per_page' => $per_page ), 200); }