homegrade_theme_production/includes/ajax.php
Antoine M 030ba4d38f
All checks were successful
continuous-integration/drone/push Build is passing
removing unnecessary write logs
2024-10-07 10:53:22 +02:00

123 lines
3.4 KiB
PHP

<?php
/* ---------------------------
AJAX PUBLICATIONS
---------------------------*/
function add_ajax_scripts()
{
if (is_page_template('template-publications.php')) {
wp_enqueue_script('ajax-load-more', get_template_directory_uri() . '/resources/js/ajax.js', array('jquery'), '1.0', true);
// pass Ajax Url to script.js
// wp_localize_script('script', 'ajaxurl', admin_url('admin-ajax.php'));
wp_localize_script('ajax-load-more', 'ajaxSiteConfig', [
'ajaxUrl' => admin_url('admin-ajax.php'),
'ajax_nonce' => wp_create_nonce('load_more_post_nonce')
]);
}
}
add_action('wp_enqueue_scripts', 'add_ajax_scripts');
/* ---------------------------
LOAD MORE BROCHURES
---------------------------*/
add_action('wp_ajax_get_more_brochures', 'load_more_brochures');
add_action('wp_ajax_nopriv_get_more_brochures', 'load_more_brochures');
function load_more_brochures()
{
$offset = $_POST['ajax_data']['offset'];
// $data['user1'] = 'server response user1';
// $data['user2'] = 'server response user2';
// wp_send_json_success($data);
$args = array(
'post_type' => 'brochures',
'posts_per_page' => 1,
'post_status' => 'publish',
'offset' => $offset,
'order' => 'DESC',
'orderby' => 'date',
'meta_key' => 'brochure_pdf',
'meta_value' => '',
'meta_compare' => '!='
);
$ajax_query = new WP_Query($args);
// $isQueryEmpty = $ajax_query->have_posts() ? false : true;
$isQueryEmpty = $offset + 1 >= $ajax_query->max_num_pages ? true : false;
// BREAK IF QUERY IS EMPTY
if ($ajax_query->have_posts() == false) {
wp_send_json(array('data' => '', 'isQueryEmpty' => $isQueryEmpty));
die();
}
ob_start();
if ($ajax_query->have_posts()) : while ($ajax_query->have_posts()) : $ajax_query->the_post();
get_template_part('template-components/archives/brochure-grid-row', null, array('ID' => get_the_ID()));
endwhile;
endif;
// ob_end_flush();
$response = ob_get_clean();
wp_send_json(array('data' => $response, 'isQueryEmpty' => $isQueryEmpty));
die();
}
/* ---------------------------
LOAD MORE FICHES INFOS
---------------------------*/
add_action('wp_ajax_get_more_fiches_infos', 'load_more_fiches_infos');
add_action('wp_ajax_nopriv_get_more_fiches_infos', 'load_more_fiches_infos');
function load_more_fiches_infos()
{
$offset = $_POST['ajax_data']['offset'];
$args = array(
'post_type' => 'fiches-infos',
'posts_per_page' => 1,
'post_status' => 'publish',
'offset' => $offset,
'order' => 'DESC',
'orderby' => 'date',
'meta_key' => 'brochure_pdf',
'meta_value' => '',
'meta_compare' => '!='
);
$ajax_query = new WP_Query($args);
// $isQueryEmpty = $ajax_query->have_posts() ? false : true;
$isQueryEmpty = $offset + 1 >= $ajax_query->max_num_pages ? true : false;
// BREAK IF QUERY IS EMPTY
if ($ajax_query->have_posts() == false) {
wp_send_json(array('data' => '', 'isQueryEmpty' => $isQueryEmpty));
die();
}
ob_start();
if ($ajax_query->have_posts()) : while ($ajax_query->have_posts()) : $ajax_query->the_post();
get_template_part('template-components/archives/brochure-grid-row', null, array('ID' => get_the_ID()));
endwhile;
endif;
// ob_end_flush();
$response = ob_get_clean();
wp_send_json(array('data' => $response, 'isQueryEmpty' => $isQueryEmpty));
die();
}