// ROUTE AJAX DE RECHERCHE AVEC PAGINATION EN HAUT ET EN BAS
add_action('wp_ajax_tng_media_search', 'tng_ms_ajax_search_handler');
add_action('wp_ajax_nopriv_tng_media_search', 'tng_ms_ajax_search_handler');
function tng_ms_ajax_search_handler() {
$search = isset($_POST['search']) ? sanitize_text_field($_POST['search']) : '';
$page = isset($_POST['page']) ? max(1, intval($_POST['page'])) : 1;
$tng_db = new TNG_DB();
$options = get_option('tng_ms_settings');
$max_pool = 500;
$per_page = 30;
$thumb_width = isset($options['thumb_width']) ? intval($options['thumb_width']) : 200;
$results = array();
try {
$results = $tng_db->search_media($search, $max_pool);
} catch (Exception $e) {
$results = array();
}
$is_logged = $tng_db->is_user_logged_into_tng();
$tng_url = rtrim($options['tng_url'] ?? '', '/') . '/';
$grid_html = '';
$total_count = !empty($results) && is_array($results) ? count($results) : 0;
if ($total_count > 0) {
$chunks = array_chunk($results, $per_page);
$total_pages = count($chunks);
$page = min($page, $total_pages);
$current_page_results = $chunks[$page - 1];
// Création de la structure HTML des boutons
$pagination_markup = '';
if ($total_pages > 1) {
$pagination_markup .= '
';
}
// 1. On injecte la pagination HAUTE
$grid_html .= $pagination_markup;
// 2. On injecte la grille de portraits
$grid_html .= '';
foreach ($current_page_results as $row) {
$is_living = intval($row['living ?? 0']) === 1;
if ($is_living && !$is_logged) {
$media_url = TNG_MEDIA_SYNC_URL . 'assets/images/living-placeholder.png';
$desc = "Média masqué - Personne vivante";
} else {
$media_url = $tng_url . ($row['full_url'] ?? '');
$desc = !empty($row['description']) ? esc_html($row['description']) : 'Média TNG';
}
$link_url = $tng_url . "showmedia.php?mediaID=" . ($row['mediaID'] ?? '');
$grid_html .= '
';
}
$grid_html .= '
';
// 3. On ré-injecte la pagination BASSE
$grid_html .= $pagination_markup;
} else {
$grid_html .= 'Aucun résultat trouvé pour votre recherche.
';
}
wp_send_json_success(array(
'grid_html' => $grid_html,
'count' => $total_count
));
}
Page non trouvée - Les Malvache au fil du temps
Skip to content