easyiit_auto_cache_html.php 6.6 KB
<?php
/*
Plugin Name: 全静态插件
Plugin URI: #
Description: 全静态插件 Globalso.com
Version: 6.0
Author: #
date:2019-09-04
Author URI:#
*/
/*ini_set('user_agent','Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; SV1; .NET CLR 1.1.4322)');
require_once(ABSPATH . 'wp-admin/includes/file.php');

if ( ! defined( 'EACH_PLUGIN_DIR' ) )
	define( 'EACH_PLUGIN_DIR', untrailingslashit( dirname( __FILE__ ) ) );

if ( ! defined( 'EACH_PLUGIN_URL' ) )
	define( 'EACH_PLUGIN_URL', untrailingslashit( plugins_url( '', __FILE__ ) ) );
	
$script_uri = rtrim( "http://".$_SERVER["HTTP_HOST"].$_SERVER["REQUEST_URI"]  ,"/");
$home_path = get_home_path();
$page_posts = get_option('posts_per_page');
$home_url_length = strlen ( get_option ( 'home' ) . "/" );
$cache_update=array();
if (get_option ( 'permalink_structure' ) == "")
	define ( 'eac_nonpermanent', true );
else
	define ( 'eac_nonpermanent', false );

require_once EACH_PLUGIN_DIR . '/inc/easyiit_auto_cache_html.class.php';
require_once EACH_PLUGIN_DIR . '/inc/functions.php';

add_action('admin_menu', 'cache_html_create');
function cache_html_create() {
    add_menu_page('生成全站静态', '生成全站静态', 7, 'cache_html_create');
	add_submenu_page('cache_html_create', '批量生成全站静态', '批量生成全站静态', 7, 'cache_html_create', array('easyiit_auto_cache_html','html_create_form_init'));
}

add_action('get_home_url',array('easyiit_auto_cache_html','cache_home_url'));
add_action('get_cat_url',array('easyiit_auto_cache_html','cache_cat_url'));
add_action('get_posts_url',array('easyiit_auto_cache_html','cache_posts_url'));
add_action('get_pages_url',array('easyiit_auto_cache_html','cache_pages_url'));
add_action('get_tags_url',array('easyiit_auto_cache_html','cache_tags_url'));
add_action('edit_post', array('easyiit_auto_cache_html','edit_post_static_html'), 10 );
add_action('delete_post', array('easyiit_auto_cache_html','delete_post_static_html'), 10 );
 add_action('save_post', array('easyiit_auto_cache_html','save_post_static_html_home'), 10 );

$auto_cache = get_option('eac_auto_cache');
if($auto_cache==1){
   add_action('save_post', array('easyiit_auto_cache_html','save_post_static_html'), 10 );
}



function easyiit_make_cache_html(){
  global $wp; 
  $file = 'cache_flie_url.php';
  $cache_type = $_GET['cache_type'];
  if(file_exists(EACH_PLUGIN_DIR."/cache/".$file)){
	  include EACH_PLUGIN_DIR."/cache/".$file;
	  $eac_all_refresh_cache = $cache_html[$cache_type];
  }else{
	  easyiit_auto_cache_html::update_cache();
	  if(file_exists(EACH_PLUGIN_DIR."/cache/".$file)){
	      include EACH_PLUGIN_DIR."/cache/".$file;
	      $eac_all_refresh_cache = $cache_html[$cache_type];
	  }else{
		  exit();
	  }
  }  
  $total = count($eac_all_refresh_cache);
  $k=1;
  $t = date("Y-m-d H:i:s");
  if(!$_GET['total']&&!$_GET['num']){	  
     $ret['total'] = $total;
	 $ret['num'] = 0;
	 $ret['next_num'] = $ret['num']+1;
	 $ret['file'] = $file = "获取数据中...";	 
  }else{
	 $ret['total'] = $total;
	 $ret['num'] = $_GET['num'];	 
	 if($_GET['total']==$_GET['num']){
		 $ret['next_num'] = -1;
	 }else{
		 $ret['next_num'] = $_GET['num']+1;
	 }
	 $ret['file'] = $file = $eac_all_refresh_cache[$ret['num']]['file'];
	 $url = $eac_all_refresh_cache[$ret['num']]['url'];
	 $ret['url'] = $url;
	 if(file_exists(ABSPATH.$file)){
		 del_dir_and_file(ABSPATH.$file);
	 }
	 ob_start();  
	 echo get_static_content($url);
	 $data = ob_get_contents();
	 if($data){
		$data = $data."<!--Globalso.com - Static Page Time: $t-->";
	 }
	 ob_clean();
	 $dir = ABSPATH.dirname($file);
	 if(!is_dir($dir)) {
		mkdir($dir, 0777,1);
	 }
	 if($data){
		$strlen = file_put_contents(ABSPATH.$file, $data);
	 }
	 @chmod($file,0777);
  }
  echo json_encode($ret);
  exit();
}

add_action( 'wp_ajax_easyiit_make_cache_html', 'easyiit_make_cache_html' );

function easyiit_unset_cache_html(){
  global $wp;
  $cache_type = $_GET['cache_type'];

  if(file_exists(EACH_PLUGIN_DIR."/cache/cache_flie_url.php")&&file_exists(EACH_PLUGIN_DIR."/cache/cache_dir.php")){
	  include EACH_PLUGIN_DIR."/cache/cache_flie_url.php";
	  $eac_all_refresh_cache = $cache_html[$cache_type];
	  include EACH_PLUGIN_DIR."/cache/cache_dir.php";
	  $eac_all_refresh_dir_cache = $cache_dir[$cache_type];
  }else{
	  easyiit_auto_cache_html::update_cache();
	  include EACH_PLUGIN_DIR."/cache/cache_flie_url.php";
	  $eac_all_refresh_cache = $cache_html[$cache_type];
	  include EACH_PLUGIN_DIR."/cache/cache_dir.php";
	  $eac_all_refresh_dir_cache = $cache_dir[$cache_type];
  }
  if($cache_type=='index'||$cache_type=='post'){
	  $eac_all_refresh_arr = $eac_all_refresh_cache;
  }else{
	  $eac_all_refresh_arr = $eac_all_refresh_dir_cache;
  }
  if(!is_array($eac_all_refresh_arr)){
	  return;
  } 
  $k=1;
  $eac_all_refresh = array();
  foreach($eac_all_refresh_arr as $val){
      $eac_all_refresh[$k]=$val['file'];
	  $k++;
  }
  $total = count($eac_all_refresh);
  if(!$_GET['total']&&!$_GET['num']){	  
     $ret['total'] = $total;
	 $ret['num'] = $ret['next_num'] = 1;
	 $ret['file'] = $file = "获取数据中...";	 
  }else{
	$ret['total'] = $total;
	$num = $ret['num'] = $_GET['num'];
	
	if($_GET['total']==$_GET['num']){
		 $ret['next_num'] = -1;
	 }else{
		 $ret['next_num'] = $_GET['num']+1;
	 }	
	if($num==-1||!$num){
		$ret['file'] = "跳转下一分类"; 
		echo json_encode($ret);
        exit();
	}
	$file = ABSPATH.$eac_all_refresh[$num]; 
	if(del_dir_and_file($file, true)){
		$t_res ='-删除成功';
		$ret['file'] = $file.$t_res; 
	}else{
		$t_res ='-删除失败';
		$ret['file'] = $file.$t_res; 
	}	    	 
  }
  
 echo json_encode($ret);
 exit();
}
add_action( 'wp_ajax_easyiit_unset_cache_html', 'easyiit_unset_cache_html' );


//更新链接接口  并更新首页
add_action( 'wp_ajax_nopriv_easyiit_update_cache', 'easyiit_update_cache' );
add_action( 'wp_ajax_easyiit_update_cache', 'easyiit_update_cache' );

function easyiit_update_cache(){
	global $post, $cache_update, $page_posts, $home_url_length;
	easyiit_auto_cache_html::update_cache();
	$cur_time = time();
	if(file_exists(__DIR__.'/cache/cache_flie_url.php')){
		$utime = filemtime(__DIR__.'/cache/cache_flie_url.php');
		if(abs($utime-$cur_time) < 60){
			 easyiit_auto_cache_html::make_html('index.html',get_option ( 'home' ) . "/index.php");
			echo 200;exit;
		}else{
			echo 1;exit;
		}
	}else{
		echo 2;exit;
	}
}*/



function ppm_fixe_trailingslash($url, $type){
  if ('single' === $type){
    return $url;
  }
  return trailingslashit($url);
} 
add_filter('user_trailingslashit', 'ppm_fixe_trailingslash', 10, 2);


 
?>