mirror of
https://github.com/mashirozx/sakura.git
synced 2024-11-22 06:48:12 +08:00
Close issue #103
This commit is contained in:
parent
8d0a24d35c
commit
ea8fdad501
@ -7,7 +7,7 @@
|
||||
* @package Sakura
|
||||
*/
|
||||
|
||||
define( 'SAKURA_VERSION', '3.2.2' );
|
||||
define( 'SAKURA_VERSION', '3.2.3' );
|
||||
define( 'BUILD_VERSION', '3' );
|
||||
define( 'JSDELIVR_VERSION', '3.6.7' );
|
||||
|
||||
@ -28,16 +28,7 @@ if ( !function_exists( 'optionsframework_init' ) ) {
|
||||
require_once dirname( __FILE__ ) . '/inc/options-framework.php';
|
||||
}
|
||||
|
||||
//live search
|
||||
if(akina_option('live_search')){
|
||||
if (file_exists(get_wp_root_path().'/themes/Sakura/cache/search.json')) {
|
||||
if (time() - filemtime(get_wp_root_path().'/themes/Sakura/cache/search.json') > 10800) {
|
||||
require_once(dirname( __FILE__ ) .'/inc/cache-search.php');
|
||||
}
|
||||
}else {
|
||||
require_once(dirname( __FILE__ ) .'/inc/cache-search.php');
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
function akina_setup() {
|
||||
/*
|
||||
@ -1670,12 +1661,13 @@ EOS;
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
add_action( 'rest_api_init', function () {
|
||||
if(akina_option('live_search')){
|
||||
add_action( 'rest_api_init', function () {
|
||||
register_rest_route( 'cache_search/v1', '/json/', array(
|
||||
'methods' => 'GET',
|
||||
'callback' => 'cache_search_json',
|
||||
) );
|
||||
} );
|
||||
} );
|
||||
}
|
||||
|
||||
//code end
|
||||
|
@ -1,71 +0,0 @@
|
||||
<?php
|
||||
class cacheFile{
|
||||
private $_dir;
|
||||
const EXT = '.json';
|
||||
public function __construct() {
|
||||
$this->_dir = get_wp_root_path().'/themes/Sakura/cache/';
|
||||
}
|
||||
public function cacheData($key, $value = '', $path = '') {
|
||||
$filePath = $this->_dir.$path.$key.self::EXT;
|
||||
if ($value !== '') {
|
||||
if (is_null($value)) {
|
||||
return unlink($filePath);
|
||||
}
|
||||
$dir = dirname($filePath);
|
||||
if (!is_dir($dir)) {
|
||||
mkdir($dir, 0777);
|
||||
}
|
||||
return file_put_contents($filePath,$value);
|
||||
}
|
||||
if (!is_file($filePath)) {
|
||||
return false;
|
||||
} else {
|
||||
return json_decode(file_get_contents($filePath), true);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$vowels = array("[", "{","]","}","<",">","\r\n", "\r", "\n","-","'",'"','`'," ",":",";",'\\'," ","toc");
|
||||
$regex = <<<EOS
|
||||
/<\/?[a-zA-Z]+("[^"]*"|'[^']*'|[^'">])*>|begin[\S\s]*\/begin|hermit[\S\s]*\/hermit|img[\S\s]*\/img|{{.*?}}|:.*?:/m
|
||||
EOS;
|
||||
|
||||
$posts = new WP_Query('posts_per_page=-1&post_status=publish&post_type=post');
|
||||
while ($posts->have_posts()) : $posts->the_post();
|
||||
$output .= '{"type":"post","link":"'.get_post_permalink().'","title":'.json_encode(get_the_title()).',"comments":"'.get_comments_number('0', '1', '%').'","text":'.json_encode(str_replace($vowels, " ",preg_replace($regex,' ',get_the_content()))).'},';
|
||||
endwhile;
|
||||
wp_reset_postdata();
|
||||
|
||||
$pages = get_pages();
|
||||
foreach ($pages as $page) {
|
||||
$output .= '{"type":"page","link":"'.get_page_link($page).'","title":'.json_encode($page->post_title).',"comments":"'.$page->comment_count.'","text":'.json_encode(str_replace($vowels, " ",preg_replace($regex,' ',$page->post_content))).'},';
|
||||
}
|
||||
|
||||
$tags = get_tags();
|
||||
foreach ($tags as $tag) {
|
||||
$output .= '{"type":"tag","link":"'.get_term_link($tag).'","title":'.json_encode($tag->name).',"comments":"","text":""},';
|
||||
}
|
||||
|
||||
$categories = get_categories();
|
||||
foreach ($categories as $category) {
|
||||
$output .= '{"type":"category","link":"'.get_term_link($category).'","title":'.json_encode($category->name).',"comments":"","text":""},';
|
||||
}
|
||||
if(akina_option('live_search_comment')){
|
||||
$comments = get_comments();
|
||||
foreach ($comments as $comment) {
|
||||
$is_private = get_comment_meta($comment->comment_ID, '_private', true);
|
||||
if($is_private){
|
||||
$output .= '{"type":"comment","link":"'.get_comment_link($comment).'","title":'.json_encode(get_the_title($comment->comment_post_ID)).',"comments":"","text":'.json_encode($comment->comment_author.":该评论为私密评论").'},';
|
||||
continue;
|
||||
}else{
|
||||
$output .= '{"type":"comment","link":"'.get_comment_link($comment).'","title":'.json_encode(get_the_title($comment->comment_post_ID)).',"comments":"","text":'.json_encode(str_replace($vowels, " ",preg_replace($regex," ",$comment->comment_author.":".$comment->comment_content))).'},';
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$output = substr($output,0,strlen($output)-1);
|
||||
|
||||
$data = '['.$output.']';
|
||||
$TheFile = get_wp_root_path().'/themes/Sakura/cache/search.json';
|
||||
$cacheFile = new cacheFile();
|
||||
$cacheFile->cacheData('search', $data);
|
@ -1389,7 +1389,7 @@ var home = location.href,
|
||||
$('html').css('overflow-y','hidden');
|
||||
if (mashiro_option.live_search) {
|
||||
var QueryStorage = [];
|
||||
search_a("https://"+document.domain+"/wp-content/themes/Sakura/cache/search.json");
|
||||
search_a("https://"+document.domain+"/wp-json/cache_search/v1/json/");
|
||||
|
||||
var otxt = document.getElementById("search-input"),
|
||||
list = document.getElementById("PostlistBox"),
|
||||
|
@ -969,7 +969,7 @@ function optionsframework_options() {
|
||||
|
||||
$options[] = array(
|
||||
'name' => __('启用实时搜索', 'options_framework_theme'),
|
||||
'desc' => __('前台实现实时搜索,每3小时更新一次缓存,可通过删除主题文件夹下的 cache 文件夹手动更新缓存'),
|
||||
'desc' => __('前台实现实时搜索,调用 Rest API 每小时更新一次缓存,可在 functions.php 里手动设置缓存时间'),
|
||||
'id' => 'live_search',
|
||||
'std' => '0',
|
||||
'type' => 'checkbox');
|
||||
|
@ -5,7 +5,7 @@ Theme URI: https://2heng.xin/theme-sakura/
|
||||
Author: Mashiro, Louie, Fuzzz
|
||||
Author URI: http://2heng.xin
|
||||
Description: Akina主题分支(原版地址 http://www.akina.pw/themeakina)
|
||||
Version: 3.2.2
|
||||
Version: 3.2.3
|
||||
License: GNU General Public License v2 or later
|
||||
License URI: http://www.gnu.org/licenses/gpl-2.0.html
|
||||
Text Domain: akina
|
||||
@ -4371,7 +4371,7 @@ i.iconfont.js-toggle-search.iconsearch {
|
||||
.ins-section .ins-search-item .ins-search-preview {
|
||||
font-size: 12px;
|
||||
color: #9a9a9a;
|
||||
margin: 5px 0 0 20px;
|
||||
margin: 5px 0 0 0px;
|
||||
cursor: url(https://cdn.jsdelivr.net/gh/moezx/cdn@3.1.9/img/Sakura/cursor/ayuda.cur),auto;
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user