除虫&实时搜索

pull/105/head v3.2.2
Spirit 2019-08-18 01:50:21 +08:00
parent 0e52e37046
commit b3bab414e6
10 changed files with 352 additions and 10 deletions

View File

@ -53,6 +53,7 @@ get_header(); ?>
</main><!-- #main -->
<?php if ( akina_option('pagenav_style') == 'ajax') { ?>
<div id="pagination" <?php if(akina_option('image_category') && is_category(explode(',',akina_option('image_category')))) echo 'class="pagination-archive"'; ?>><?php next_posts_link(__('Previous')); ?></div>
<div id="add_post"><span id="add_post_time" style="visibility: hidden;" title="<?php echo akina_option('auto_load_post',''); ?>" ></span></div>
<?php }else{ ?>
<nav class="navigator">
<?php previous_posts_link('<i class="iconfont icon-back"></i>') ?><?php next_posts_link('<i class="iconfont icon-right"></i>') ?>

View File

@ -74,6 +74,7 @@ get_header();
</main><!-- #main -->
<?php if ( akina_option('pagenav_style') == 'ajax') { ?>
<div id="pagination"><?php next_posts_link(__('Previous')); ?></div>
<div id="add_post"><span id="add_post_time" style="visibility: hidden;" title="<?php echo akina_option('auto_load_post',''); ?>" ></span></div>
<?php }else{ ?>
<nav class="navigator">
<?php previous_posts_link('<i class="iconfont icon-back"></i>') ?><?php next_posts_link('<i class="iconfont icon-right"></i>') ?>

View File

@ -69,11 +69,22 @@
<!-- search start -->
<form class="js-search search-form search-form--modal" method="get" action="<?php echo home_url(); ?>" role="search">
<div class="search-form__inner">
<div>
<?php if(akina_option('live_search')){ ?>
<div class="micro">
<i class="iconfont icon-search"></i>
<input id="search-input" class="text-input" type="search" name="s" placeholder="<?php _e('想要找点什么呢?', 'akina') ?>" required>
</div>
<div class="ins-section-wrapper">
<a id="Ty" href="#"></a>
<div class="ins-section-container" id="PostlistBox"></div>
</div>
<?php }else{ ?>
<div class="micro">
<p class="micro mb-"><?php _e('想要找点什么呢?', 'akina') ?></p>
<i class="iconfont icon-search"></i>
<input class="text-input" type="search" name="s" placeholder="<?php _e('Search', 'akina') ?>" required>
</div>
<?php } ?>
</div>
<div class="search_close"></div>
</form>

View File

@ -7,7 +7,7 @@
* @package Sakura
*/
define( 'SAKURA_VERSION', '3.2.1' );
define( 'SAKURA_VERSION', '3.2.2' );
define( 'BUILD_VERSION', '3' );
define( 'JSDELIVR_VERSION', '3.6.7' );
@ -28,7 +28,16 @@ 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() {
/*
@ -1602,4 +1611,9 @@ function DEFAULT_FEATURE_IMAGE() {
}
}
//防止设置置顶文章造成的图片同侧bug
add_action( 'pre_get_posts', function( $q ){
if ( $q->is_home() && $q->is_main_query() && $q->get( 'paged' ) > 1 )
$q->set( 'post__not_in', get_option( 'sticky_posts' ) );
});
//code end

View File

@ -0,0 +1,71 @@
<?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);

View File

@ -50,6 +50,12 @@ if ( akina_option('theme_skin') ) { ?>
background-color: <?php echo akina_option('theme_skin'); ?>
}
<?php } ?>
<?php if(akina_option('live_search')){ ?>
.search-form--modal .search-form__inner {
bottom: unset !important;
top: 10% !important;
}
<?php } ?>
<?php } // theme-skin ?>
<?php // Custom style

View File

@ -15,6 +15,7 @@ mashiro_option.template_url = "<?php echo get_template_directory_uri(); ?>";
mashiro_option.site_url = "<?php echo site_url(); ?>";
mashiro_option.qq_api_url = "https://api.2heng.xin/qqinfo/";
mashiro_option.qq_avatar_api_url = "https://api.2heng.xin/qqinfo/";
mashiro_option.live_search = <?php if ( akina_option('live_search') ){ echo 'true'; } else { echo 'false'; } ?>;
<?php if( is_home() ){ ?>
mashiro_option.land_at_home = true;

View File

@ -1331,10 +1331,10 @@ var home = location.href,
window.resizeFlag= null;
$(window).resize(function () {
//直接resize性能爆炸改成延时
if(resizeFlag!=null){
if(resizeFlag=null){
clearTimeout(resizeFlag);
}
resizeFlag = setTimeout(function(){ Siren.AH();resizeFlag=null }, 1000);
resizeFlag = setTimeout(function(){ Siren.AH();}, 1000);
})
}
} else {
@ -1386,11 +1386,131 @@ var home = location.href,
$('.js-toggle-search').on('click', function () {
$('.js-toggle-search').toggleClass('is-active');
$('.js-search').toggleClass('is-visible');
$('html').css('overflow-y','hidden');
if (mashiro_option.live_search) {
var QueryStorage = [];
search_a("https://"+document.domain+"/wp-content/themes/Sakura/cache/search.json");
var otxt = document.getElementById("search-input"),
list = document.getElementById("PostlistBox"),
Record = list.innerHTML,
searchFlag = null;
otxt.oninput = function () {
if(searchFlag=null){
clearTimeout(searchFlag);
}
searchFlag = setTimeout(function(){
query(QueryStorage, otxt.value, Record);
div_href();
}, 250);
};
function search_a(val) {
if(sessionStorage.getItem('search')!=null){
QueryStorage = JSON.parse(sessionStorage.getItem('search'));
query(QueryStorage, $("#search-input").val(), Record);
div_href();
}else{
var _xhr = new XMLHttpRequest();
_xhr.open("GET", val, true)
_xhr.send();
_xhr.onreadystatechange = function () {
if (_xhr.readyState == 4 && _xhr.status == 200) {
json = _xhr.responseText;
if (json != "") {
sessionStorage.setItem('search',json);
QueryStorage = JSON.parse(json);
query(QueryStorage, otxt.value, Record);
div_href();
}
}
}
}
}
if (!Object.values) Object.values = function (obj) {
if (obj !== Object(obj))
throw new TypeError('Object.values called on a non-object');
var val = [],
key;
for (key in obj) {
if (Object.prototype.hasOwnProperty.call(obj, key)) {
val.push(obj[key]);
}
}
return val;
}
function Cx(arr, q) {
q = q.replace(q,"^(?=.*?"+q+").+$").replace(/\s/g,")(?=.*?");
i = arr.filter(
v => Object.values(v).some(
v => new RegExp(q + '').test(v)
)
);
return i;
}
function div_href(){
$(".ins-selectable").each(function(){
$(this).click(function(){
$("#Ty").attr('href',$(this).attr('href'));
$("#Ty").click();
$(".search_close").click();
});
});
}
function search_result(keyword, link, fa, title, iconfont, comments, text){
if(keyword){
var s = keyword.trim().split(" "),
a = title.indexOf(s[s.length-1]),
b = text.indexOf(s[s.length-1]);
title=a<60 ? title.slice(0,80):title.slice(a-30,a+30);
title=title.replace(s[s.length-1], '<mark class="search-keyword"> ' + s[s.length-1].toUpperCase() + ' </mark>');
text=b<60 ? text.slice(0,80):text.slice(b-30,b+30);
text=text.replace(s[s.length-1], '<mark class="search-keyword"> ' + s[s.length-1].toUpperCase() + ' </mark>');
}
return '<div class="ins-selectable ins-search-item" href="' + link + '"><header><i class="fa fa-'+ fa +'" aria-hidden="true"></i>' + title + '<i class="iconfont icon-'+ iconfont +'"> ' + comments + '</i>' + '</header><p class="ins-search-preview">' + text + '</p></div>';
}
function query(B, A, z) {
var x, v, s, y = "",
w = "",
u = "",
r = "",
p = "",
F = "",
H = "",
G = '<section class="ins-section"><header class="ins-section-header">',
D = "</section>",
E = "</header>",
C = Cx(B, A.trim());
for (x = 0; x < Object.keys(C).length; x++) {
H = C[x];
switch (v = H.type) {
case "post":
w = w + search_result(A, H.link, "file", H.title, "mark", H.comments, H.text);
break;
case "tag":
p = p + search_result("", H.link, "tag", H.title, "none", "", "");
break;
case "category":
r = r + search_result("", H.link, "folder", H.title, "none", "", "");
break;
case "page":
u = u + search_result(A, H.link, "file", H.title, "mark", H.comments, H.text);
break;
case "comment":
F = F + search_result(A, H.link, "comment", H.title, "none", "", H.text);
break
}
}
w && (y = y + G + "文章" + E + w + D), u && (y = y + G + "页面" + E + u + D), r && (y = y + G + "分类" + E + r + D), p && (y = y + G + "标签" + E + p + D), F && (y = y + G + "评论" + E + F + D), s = document.getElementById("PostlistBox"), s.innerHTML = y
}
}
});
$('.search_close').on('click', function () {
if ($('.js-search').hasClass('is-visible')) {
$('.js-toggle-search').toggleClass('is-active');
$('.js-search').toggleClass('is-visible');
$('html').css('overflow-y','unset');
}
});
$('#show-nav').on('click', function () {
@ -1711,6 +1831,7 @@ $(function () {
if ($('.js-search.is-visible').length > 0) {
$('.js-toggle-search').toggleClass('is-active');
$('.js-search').toggleClass('is-visible');
$('html').css('overflow-y','unset');
}
});
window.addEventListener('popstate', function (e) {

View File

@ -967,6 +967,20 @@ function optionsframework_options() {
'type_4' => __('23k 次访问(中式)', ''),
));
$options[] = array(
'name' => __('启用实时搜索', 'options_framework_theme'),
'desc' => __('前台实现实时搜索每3小时更新一次缓存可通过删除主题文件夹下的 cache 文件夹手动更新缓存'),
'id' => 'live_search',
'std' => '0',
'type' => 'checkbox');
$options[] = array(
'name' => __('实时搜索包含评论', 'options_framework_theme'),
'desc' => __('在实时搜索中搜索评论(如果网站评论数量太多不建议开启)'),
'id' => 'live_search_comment',
'std' => '0',
'type' => 'checkbox');
$options[] = array(
'name' => __('启用 baguetteBox', 'options_framework_theme'),
'desc' => __('默认禁用,<a href="https://github.com/mashirozx/Sakura/wiki/Fancybox">请阅读说明</a>', 'options_framework_theme'),

112
style.css
View File

@ -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.1
Version: 3.2.2
License: GNU General Public License v2 or later
License URI: http://www.gnu.org/licenses/gpl-2.0.html
Text Domain: akina
@ -4176,10 +4176,6 @@ i.iconfont.js-toggle-search.iconsearch {
background-position: bottom right
}
.search-form div {
position: relative
}
.search-form .search_close {
position: absolute;
width: 35px;
@ -4283,6 +4279,112 @@ i.iconfont.js-toggle-search.iconsearch {
list-style-type: decimal
}
.micro {
position: relative
}
.ins-section-wrapper a {
-webkit-transition: none;
transition: none;
}
.ins-section-wrapper header {
position: unset;
width: auto;
}
.ins-selectable {
cursor: pointer;
}
.ins-section-wrapper {
left: 0;
right: 0;
top: 45px;
bottom: 0;
margin-top: 45px;
height: 70vh;
overflow-y: auto;
position: absolute;
border-radius: 14px 0 0 14px;
scroll-behavior: smooth;
}
.ins-section-container {
position: relative;
background: rgba(241, 248, 253, 0.5);
border-radius: 14px;
font-family: none;
}
.ins-section {
font-size: 14px;
}
.ins-section .iconfont.icon-mark {
font-size: 13px;
color: #ccc;
bottom: unset;
margin-top: unset;
line-height: unset;
margin-left: 16px;
left: unset;
position: unset;
}
.ins-section .fa {
font-size: 18px;
color: #333;
bottom: unset;
left: 12px;
margin-top: -3px;
line-height: unset;
}
.ins-section .ins-section-header,.ins-section .ins-search-item {
padding: 8px 15px;
cursor: url(https://cdn.jsdelivr.net/gh/moezx/cdn@3.1.9/img/Sakura/cursor/ayuda.cur),auto;
}
.ins-section .ins-search-item .search-keyword {
font-size: 15px;
border-radius: 5px;
}
.ins-section .ins-section-header {
color: #9a9a9a;
border-bottom: 1px solid #e2e2e2;
}
.ins-section .ins-search-item header,.ins-section .ins-search-item .ins-search-preview {
overflow: hidden;
white-space: nowrap;
text-overflow: ellipsis;
color: #9a9a9a;
margin-left: 20px;
}
.ins-section .ins-search-item header i {
margin-right: 8px;
}
.ins-section .ins-search-item .ins-search-preview {
font-size: 12px;
color: #9a9a9a;
margin: 5px 0 0 20px;
cursor: url(https://cdn.jsdelivr.net/gh/moezx/cdn@3.1.9/img/Sakura/cursor/ayuda.cur),auto;
}
.ins-section .ins-search-item:hover,.ins-section .ins-search-item.active {
color: #fff;
background: rgba(22, 160, 133, 0.3);
border-radius: 14px;
}
.ins-section .ins-search-item:hover .ins-slug,.ins-section .ins-search-item.active .ins-slug,.ins-section .ins-search-item:hover .ins-search-preview,.ins-section .ins-search-item.active .ins-search-preview,.ins-section .ins-search-item:hover header,.ins-section .ins-search-item:hover .iconfont {
color: #333;
}
.sorry ul {
padding: 0 0 0 20px;
margin: 0