Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hawkidoki/06de8bc4ab6ca85c7c6c24537b30a714 to your computer and use it in GitHub Desktop.
Save hawkidoki/06de8bc4ab6ca85c7c6c24537b30a714 to your computer and use it in GitHub Desktop.
<?php
add_action('wp_ajax_hwk_ajax_template', 'hwk_ajax_template');
add_action('wp_ajax_nopriv_hwk_ajax_template', 'hwk_ajax_template');
function hwk_ajax_template($id = false){
$configs = apply_filters('hwk_ajax_configs', $configs);
if(empty($configs))
return;
$html = hwk_ajax_config($configs, $id);
if(wp_doing_ajax())
wp_send_json_success($html);
echo $html;
}
function hwk_ajax_config($configs, $id = false){
if(!$id && wp_doing_ajax() && isset($_POST['id']))
$id = $_POST['id'];
if(!$id)
return;
$config = array();
foreach($configs as $line){
if($line['id'] != $id)
continue;
$config = $line;
break;
}
return hwk_ajax_get_template($config);
}
function hwk_ajax_get_template($args = array()){
if(!$args['id'] || !$args['template'])
return;
$args = wp_parse_args($args, array(
'id' => false,
'template' => false,
'cache' => false,
'flush' => false,
'param' => false,
'interval' => 900,
));
if(!$args['param'] && isset($_POST['param']) && !empty($_POST['param']))
$args['param'] = $_POST['param'];
if($args['param'] && strpos($args['cache'], '%param%'))
$args['cache'] = str_replace('%param%', $args['param'], $args['cache']);
$file = hwk_ajax_cache_file($args['cache']);
// Fichier de cache: Permission de lecture Ajax
if($cache = hwk_ajax_cache_read($args, $file))
return $cache;
// Le fichier de Cache n'existe pas. Il faut le créer
ob_start();
set_query_var('hwk_ajax_data', $args);
get_template_part($args['template']);
$page = ob_get_clean();
// Mise à jour du fichier de cache
file_put_contents($file, $page);
// Pas de Flush de Cache
if(!$args['flush'])
return $page;
// WP Super Cache Flush
if(function_exists('wp_cache_clear_cache')){
if(is_int($args['flush']))
wp_cache_post_change($args['flush']);
else
wp_cache_clear_cache();
}
// W3TC Cache Flush
if(function_exists('w3tc_flush_post')){
if(is_int($args['flush']))
w3tc_flush_post($args['flush']);
else
w3tc_flush_posts();
}
// WP Rocket Cache Flush
if(function_exists('rocket_clean_post')){
if(is_int($args['flush']))
rocket_clean_post($args['flush']);
else
rocket_clean_domain();
}
// Affichage du contenu
return $page;
}
function hwk_ajax_cache_file($cache){
return get_template_directory() . '/' . $cache . '.html';
}
function hwk_ajax_cache_read($args, $file){
if(!file_exists($file))
return false;
if(wp_doing_ajax() && (filemtime($file) + $args['interval']) < time())
return false;
return file_get_contents($file);
}
function hwk_ajax_template_data(){
if(!$args = get_query_var('hwk_ajax_data'))
return;
echo 'data-hwk-ajax-action-id="' . $args['id'] . '" data-hwk-ajax-ts="' . (time() + $args['interval']) . '" data-hwk-ajax-param="' . $args['param'] . '"';
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment