| Server IP : 65.108.144.40 / Your IP : 216.73.217.165 Web Server : Apache/2.4.52 (Ubuntu) System : Linux ubuntu-8gb-hel1-1 5.15.0-173-generic #183-Ubuntu SMP Fri Mar 6 13:29:34 UTC 2026 x86_64 User : dev ( 1000) PHP Version : 8.2.30 Disable Function : NONE MySQL : OFF | cURL : ON | WGET : ON | Perl : ON | Python : ON | Sudo : ON | Pkexec : ON Directory : /var/www/html/successkpi/wp-content/themes/successkpi/inc/ |
Upload File : |
<?php
/**
* AJAX Handlers
*
* All wp_ajax_ and wp_ajax_nopriv_ handlers live here.
* Keep each handler focused and properly secured with nonce verification.
*/
// ─────────────────────────────────────────────────────────────────────────────
// Resource Tiles Block AJAX (resource-tiles-block)
// ─────────────────────────────────────────────────────────────────────────────
add_action('wp_ajax_resource_ajax_action', 'sk_handle_resource_tiles_ajax');
add_action('wp_ajax_nopriv_resource_ajax_action', 'sk_handle_resource_tiles_ajax');
/**
* Handle paginated AJAX requests for the simple resource-tiles-block.
* Block sends: action, block_id, page_no, post_type, per_page, cta_text
*/
function sk_handle_resource_tiles_ajax()
{
// Added taxonomy and term_id NULL check for the resource_category listing pages
// By default they will come NULL, on resource cat page there value comes
$taxonomy = (!empty($_POST['taxonomy']) && $_POST['taxonomy'] !== 'null')
? sanitize_text_field($_POST['taxonomy'])
: '';
$term_id = (!empty($_POST['term_id']) && $_POST['term_id'] !== 'null')
? intval($_POST['term_id'])
: '';
$paged = isset($_POST['page_no']) ? max(1, intval($_POST['page_no'])) : 1;
$per_page = isset($_POST['per_page']) ? max(1, min(100, intval($_POST['per_page']))) : 6;
$type_label = isset($_POST['type_label']) ? sanitize_text_field($_POST['type_label']) : '';
$cta_text = ( $_POST['cta_text'] !== 'null' && !empty($_POST['cta_text']) ) ? sanitize_text_field($_POST['cta_text']) : 'Read More';
// post_type may be a plain string or a JSON array string (when ACF field is multi-select)
$post_type_raw = isset($_POST['post_type']) ? wp_unslash($_POST['post_type']) : 'post';
$decoded_type = json_decode($post_type_raw, true);
if (is_array($decoded_type)) {
$meta = sk_get_resource_metadata();
$all_allowed = $meta['all_slugs'];
// Validate each type against allowed list
$post_type = array_values(array_filter($decoded_type, function ($t) use ($all_allowed) {
return in_array(sanitize_key($t), $all_allowed, true);
}));
if (empty($post_type))
$post_type = 'post';
} else {
$post_type = sanitize_text_field($post_type_raw);
}
require_once get_stylesheet_directory() . '/blocks/resource-tiles-block/query.php';
require_once get_stylesheet_directory() . '/blocks/resource-tiles-block/template.php';
$query = get_resource_query($post_type, $per_page, $paged, $taxonomy, $term_id);
ob_start();
if ($query->have_posts()) {
while ($query->have_posts()) {
$query->the_post();
render_resource_card(get_the_ID(), $cta_text, $type_label, $taxonomy, $term_id);
}
wp_reset_postdata();
}
$cards = ob_get_clean();
ob_start();
render_resource_pagination($query->max_num_pages, $paged);
$pagination = ob_get_clean();
wp_send_json_success(array(
'cards' => $cards,
'pagination' => $pagination,
'max_pages' => (int) $query->max_num_pages,
'paged' => $paged,
));
}
// ─────────────────────────────────────────────────────────────────────────────
// All Resources With Filters Block AJAX (all-resources-with-filters-block)
// ─────────────────────────────────────────────────────────────────────────────
add_action('wp_ajax_sk_filter_resources', 'sk_ajax_filter_resources');
add_action('wp_ajax_nopriv_sk_filter_resources', 'sk_ajax_filter_resources');
/**
* Handle filter + paginate AJAX for the all-resources-with-filters-block.
* Secured with a nonce. Block sends: nonce, type, category, paged, orderby,
* order, per_page, block_types (JSON), cta_map (JSON).
*/
function sk_ajax_filter_resources()
{
// 1. Nonce check
if (!check_ajax_referer('sk_resource_filter', 'nonce', false)) {
wp_send_json_error(array('message' => 'Security check failed.'), 403);
wp_die();
}
// 2. Sanitize inputs & get metadata
$meta = sk_get_resource_metadata();
$all_allowed = $meta['all_slugs'];
$type_label_map = $meta['labels'];
$play_btn_types = $meta['play_buttons'];
$type_raw = isset($_POST['type']) ? sanitize_text_field(wp_unslash($_POST['type'])) : 'initial';
$category_raw = isset($_POST['category']) ? sanitize_text_field(wp_unslash($_POST['category'])) : 'initial';
$paged = isset($_POST['paged']) ? max(1, (int) $_POST['paged']) : 1;
$orderby = isset($_POST['orderby']) ? sanitize_key($_POST['orderby']) : 'date';
$order = isset($_POST['order']) ? (strtoupper(sanitize_key($_POST['order'])) === 'ASC' ? 'ASC' : 'DESC') : 'DESC';
$per_page = isset($_POST['per_page']) ? max(1, min(100, (int) $_POST['per_page'])) : 12;
// 3. Build allowed post types (from block config)
$block_types = array();
$block_types_raw = isset($_POST['block_types']) ? sanitize_text_field(wp_unslash($_POST['block_types'])) : '';
if (!empty($block_types_raw)) {
$decoded = json_decode($block_types_raw, true);
if (is_array($decoded)) {
$block_types = array_values(
array_filter($decoded, function ($t) use ($all_allowed) {
return in_array($t, $all_allowed, true);
})
);
}
}
if (empty($block_types)) {
$block_types = $all_allowed;
}
// Narrow to specific type if selected
if ($type_raw !== 'initial' && in_array($type_raw, $block_types, true)) {
$query_types = array($type_raw);
} else {
$query_types = $block_types;
}
// 4. Build WP_Query args
$args = array(
'post_type' => $query_types,
'posts_per_page' => $per_page,
'paged' => $paged,
'post_status' => 'publish',
'orderby' => $orderby,
'order' => $order,
'ignore_sticky_posts' => true,
);
if ($category_raw !== 'initial') {
$args['tax_query'] = array(
array(
'taxonomy' => 'resource_category',
'field' => 'slug',
'terms' => $category_raw,
),
);
}
// 5. Run query
$query = new WP_Query($args);
// 6. Decode CTA map from JS
$cta_map = array();
$cta_map_raw = isset($_POST['cta_map']) ? sanitize_text_field(wp_unslash($_POST['cta_map'])) : '';
if (!empty($cta_map_raw)) {
$decoded = json_decode($cta_map_raw, true);
if (is_array($decoded)) {
$cta_map = array_map('sanitize_text_field', $decoded);
}
}
// 7. Render cards
ob_start();
if ($query->have_posts()):
while ($query->have_posts()):
$query->the_post();
sk_render_all_resources_card(get_the_ID(), $type_label_map, $cta_map, $play_btn_types);
endwhile;
wp_reset_postdata();
else:
echo '<div class="no-posts-message"><p>No resources found matching your criteria.</p></div>';
endif;
$html = ob_get_clean();
// 8. Return JSON
wp_send_json_success(array(
'html' => $html,
'max_pages' => (int) $query->max_num_pages,
'found' => (int) $query->found_posts,
'paged' => $paged,
));
wp_die();
}
// ─────────────────────────────────────────────────────────────────────────────
// Inline Script: Inject nonce + ajaxUrl for JS
// ─────────────────────────────────────────────────────────────────────────────
/**
* Pass ajaxUrl and nonce to the frontend JS so filters/pagination work.
*/
function sk_enqueue_resource_filter_data()
{
$data = array(
'ajaxUrl' => admin_url('admin-ajax.php'),
'nonce' => wp_create_nonce('sk_resource_filter'),
);
wp_register_script('sk-resource-filter-nonce', false, array(), null, true);
wp_enqueue_script('sk-resource-filter-nonce');
wp_add_inline_script(
'sk-resource-filter-nonce',
'var skResourceFilter = ' . wp_json_encode($data) . ';',
'before'
);
}
add_action('wp_enqueue_scripts', 'sk_enqueue_resource_filter_data');