<?php
/**
* Block ajax functions
*
* @package Katerio
*/
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly
}
/*
--------------------------------------------------------------------------------
* single ajax load next
* ------------------------------------------------------------------------------
*/
if ( ! function_exists( 'th90_single_load_next_redirect' ) ) {
add_action( 'template_redirect', 'th90_single_load_next_redirect' );
function th90_single_load_next_redirect() {
$setting = th90_opt( 'single_ajax_post' );
if ( empty( $setting ) ) {
return;
}
global $wp_query;
if ( ! isset( $wp_query->query_vars['th90_single_ajax'] ) || ! is_single() ) {
return;
}
$file = '/template-parts/article/article-ajax.php';
$template = locate_template( $file );
if ( $template ) {
include( $template );
}
exit;
}
}
/*
--------------------------------------------------------------------------------
* single load next update permalink
* ------------------------------------------------------------------------------
*/
if ( ! function_exists( 'th90_single_load_next_endpoint' ) ) {
add_action( 'init', 'th90_single_load_next_endpoint' );
function th90_single_load_next_endpoint() {
add_rewrite_endpoint( 'th90_single_ajax', EP_PERMALINK );
flush_rewrite_rules();
}
}
/*
--------------------------------------------------------------------------------
* Blocks Ajax Load More
* ------------------------------------------------------------------------------
*/
if ( ! function_exists( 'th90_blocks_load_more' ) ) {
add_action( 'wp_ajax_nopriv_th90_blocks_load_more', 'th90_blocks_load_more' );
add_action( 'wp_ajax_th90_blocks_load_more', 'th90_blocks_load_more' );
function th90_blocks_load_more() {
$block = $_REQUEST['block'];
$count = 0;
if ( ! empty( $_REQUEST['page'] ) ) {
$block['target_page'] = $_REQUEST['page'];
}
if ( ! empty( $block ) ) {
foreach ( $block as $key => $value ) {
if ( 'false' == $value ) {
$block[$key] = false;
} elseif ( 'true' == $value ) {
$block[$key] = true;
}
}
}
// Run the query ----------
$block_query = th90_query( $block );
ob_start();
if ( $block_query->have_posts() ) {
while ( $block_query->have_posts() ) {
$block_query->the_post();
$count++;
get_template_part( 'template-parts/posts-module/' . $block['block_style'], '', array(
'block' => $block,
'count' => $count,
'post_count' => $block_query->post_count,
) );
}
$hide_next = $hide_prev = false;
if ( isset( $block_query->query_vars['new_max_num_pages'] ) ) {
if ( $block_query->query_vars['new_max_num_pages'] == 1 || ( $block_query->query_vars['new_max_num_pages'] == $block_query->query_vars['paged'] ) ) {
$hide_next = true;
}
}
if ( empty( $block_query->query_vars['paged'] ) || $block_query->query_vars['paged'] == 1 ) {
$hide_prev = true;
}
wp_send_json( wp_json_encode (
array(
'hide_next' => $hide_next,
'hide_prev' => $hide_prev,
'code' => ob_get_clean(),
'button' => esc_html__( 'No More', 'katerio' ),
) ) );
} else {
wp_send_json( wp_json_encode (
array(
'hide_next' => true,
'hide_prev' => $hide_prev,
'code' => esc_html__( 'No More', 'katerio' ),
'button' => esc_html__( 'No More', 'katerio' ),
) ) );
}
die;
}
}