From ea8fdad5011f57c58fb2a7290915f14a4f68cb15 Mon Sep 17 00:00:00 2001 From: Spirit Date: Sun, 18 Aug 2019 18:50:11 +0800 Subject: [PATCH] Close issue #103 --- functions.php | 28 +++++++---------- inc/cache-search.php | 71 -------------------------------------------- js/sakura-app.js | 2 +- options.php | 2 +- style.css | 4 +-- 5 files changed, 14 insertions(+), 93 deletions(-) delete mode 100644 inc/cache-search.php diff --git a/functions.php b/functions.php index d0f3011..423ffa5 100644 --- a/functions.php +++ b/functions.php @@ -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 () { - register_rest_route( 'cache_search/v1', '/json/', array( - 'methods' => 'GET', - 'callback' => 'cache_search_json', - ) ); -} ); +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 diff --git a/inc/cache-search.php b/inc/cache-search.php deleted file mode 100644 index 2e5fd0d..0000000 --- a/inc/cache-search.php +++ /dev/null @@ -1,71 +0,0 @@ -_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 = <<])*>|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); \ No newline at end of file diff --git a/js/sakura-app.js b/js/sakura-app.js index b930cf6..e1ee193 100644 --- a/js/sakura-app.js +++ b/js/sakura-app.js @@ -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"), diff --git a/options.php b/options.php index b03b32b..27c1488 100644 --- a/options.php +++ b/options.php @@ -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'); diff --git a/style.css b/style.css index 326fdb5..19fd47b 100644 --- a/style.css +++ b/style.css @@ -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; }