Skip to content

Commit

Permalink
support search filter
Browse files Browse the repository at this point in the history
  • Loading branch information
solstice23 committed Dec 4, 2021
1 parent 8c39e1b commit b033571
Show file tree
Hide file tree
Showing 5 changed files with 125 additions and 12 deletions.
47 changes: 39 additions & 8 deletions argontheme.js
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,20 @@ function __(text){
document.addEventListener("scroll", changeToolbarTransparency, {passive: true});
}();

/*搜索*/
function searchPosts(word){
if ($(".search-result").length > 0){
let url = new URL(window.location.href);
url.searchParams.set("s", word);
$.pjax({
url: url.href
});
}else{
$.pjax({
url: argonConfig.wp_path + "?s=" + encodeURI(word)
});
}
}
/*顶栏搜索*/
$(document).on("click" , "#navbar_search_input_container" , function(){
$(this).addClass("open");
Expand All @@ -306,9 +320,7 @@ $(document).on("keydown" , "#navbar_search_input_container #navbar_search_input"
return;
}
let scrolltop = $(document).scrollTop();
$.pjax({
url: argonConfig.wp_path + "?s=" + encodeURI(word)
});
searchPosts(word);
});
/*顶栏搜索 (Mobile)*/
$(document).on("keydown" , "#navbar_search_input_mobile" , function(e){
Expand All @@ -321,9 +333,7 @@ $(document).on("keydown" , "#navbar_search_input_mobile" , function(e){
return;
}
let scrolltop = $(document).scrollTop();
$.pjax({
url: argonConfig.wp_path + "?s=" + encodeURI(word)
});
searchPosts(word);
});
/*侧栏搜索*/
$(document).on("click" , "#leftbar_search_container" , function(){
Expand All @@ -347,8 +357,27 @@ $(document).on("keydown" , "#leftbar_search_input" , function(e){
return;
}
$("html").removeClass("leftbar-opened");
searchPosts(word);
});
/*搜索过滤器*/
$(document).on("change" , ".search-filter" , function(e){
if (pjaxLoading){
$(this).prop("checked", !$(this).prop("checked"));
e.preventDefault();
return;
}
pjaxLoading = true;
let postTypes = [];
$(".search-filter:checked").each(function(){
postTypes.push($(this).attr("name"));
});
if (postTypes.length == 0){
postTypes = ["none"];
}
let url = new URL(document.location.href);
url.searchParams.set("post_type", postTypes.join(","));
$.pjax({
url: argonConfig.wp_path + "?s=" + encodeURI(word)
url: url.href
});
});

Expand Down Expand Up @@ -1720,7 +1749,7 @@ if ($("html").hasClass("banner-as-cover")){
}

/*Pjax*/
var pjaxScrollTop = 0;
var pjaxScrollTop = 0, pjaxLoading = false;
$.pjax.defaults.timeout = 10000;
$.pjax.defaults.container = ['#primary', '#leftbar_part1_menu', '#leftbar_part2_inner', '.page-information-card-container', '#wpadminbar'];
$.pjax.defaults.fragment = ['#primary', '#leftbar_part1_menu', '#leftbar_part2_inner', '.page-information-card-container', '#wpadminbar'];
Expand All @@ -1732,6 +1761,7 @@ $(document).pjax("a[href]:not([no-pjax]):not(.no-pjax):not([target='_blank']):no
}
NProgress.remove();
NProgress.start();
pjaxLoading = true;
}).on('pjax:afterGetContainers', function(e, f, g) {
if (g.is("#main article.post-preview a.post-title")){
let $card = $(g.parents("article.post-preview")[0]);
Expand Down Expand Up @@ -1776,6 +1806,7 @@ $(document).pjax("a[href]:not([no-pjax]):not(.no-pjax):not([target='_blank']):no
}
}
}).on('pjax:complete', function() {
pjaxLoading = false;
NProgress.inc();
try{
if (MathJax != undefined){
Expand Down
49 changes: 48 additions & 1 deletion functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -2030,7 +2030,6 @@ function update_post_meta_ajax(){
return;
}


$result = update_post_meta($post_id, $meta_key, $meta_value);

if ($result){
Expand Down Expand Up @@ -4280,6 +4279,19 @@ function themeoptions_page(){
<p class="description"><?php _e('开启后,长说说在预览状态下会被折叠,需要手动展开', 'argon');?></p>
</td>
</tr>
<tr>
<th><label><?php _e('搜索结果类型过滤器', 'argon');?></label></th>
<td>
<select name="argon_search_post_filter">
<?php $argon_search_post_filter = get_option('argon_search_post_filter', 'post,page'); ?>
<option value="off" <?php if ($argon_search_post_filter=='off'){echo 'selected';} ?>><?php _e('禁用', 'argon');?></option>
<option value="post,page" <?php if ($argon_search_post_filter=='post,page'){echo 'selected';} ?>><?php _e('启用,默认不包括说说', 'argon');?></option>
<option value="post,page,shuoshuo" <?php if ($argon_search_post_filter=='post,page,shuoshuo'){echo 'selected';} ?>><?php _e('启用,默认包括说说', 'argon');?></option>
<option value="post,page,hide_shuoshuo" <?php if ($argon_search_post_filter=='post,page,hide_shuoshuo'){echo 'selected';} ?>><?php _e('启用,隐藏说说分类', 'argon');?></option>
</select>
<p class="description"><?php _e('开启后,将会在搜索结果界面显示一个过滤器,支持搜索说说', 'argon');?></p>
</td>
</tr>
<tr>
<th><label><?php _e('是否修正时区错误', 'argon');?></label></th>
<td>
Expand Down Expand Up @@ -4726,6 +4738,7 @@ function argon_update_themeoptions(){
argon_update_option('argon_comment_pagination_type');
argon_update_option('argon_who_can_visit_comment_edit_history');
argon_update_option('argon_home_show_shuoshuo');
argon_update_option('argon_search_post_filter');
argon_update_option('argon_darkmode_autoswitch');
argon_update_option('argon_enable_amoled_dark');
argon_update_option('argon_outdated_info_time_type');
Expand Down Expand Up @@ -4857,6 +4870,40 @@ function init_shuoshuo(){
register_post_type('shuoshuo', $args);
}

function argon_get_search_post_type_array(){
$search_filter_option = get_option('argon_search_post_filter', 'post,page');
if (!isset($_GET['post_type'])) {
if ($search_filter_option == 'off'){
return array('post', 'page');
}
$default = explode(',', $search_filter_option);
return $default;
}
$post_type = $_GET['post_type'];
$arr = array();
if (strpos($post_type, 'post') !== false) {
array_push($arr, 'post');
}
if (strpos($post_type, 'page') !== false) {
array_push($arr, 'page');
}
if (strpos($post_type, 'shuoshuo') !== false && !in_array('hide_shuoshuo', explode(',', $search_filter_option))) {
array_push($arr, 'shuoshuo');
}
if (count($arr) == 0) {
array_push($arr, 'none');
}
return $arr;
}
function search_filter($query) {
if (!$query -> is_search) {
return $query;
}
$query -> set('post_type', argon_get_search_post_type_array());
return $query;
}
add_filter('pre_get_posts', 'search_filter');

/*恢复链接管理器*/
add_filter('pre_option_link_manager_enabled', '__return_true');

Expand Down
28 changes: 26 additions & 2 deletions search.php
Original file line number Diff line number Diff line change
@@ -1,12 +1,36 @@
<?php get_header(); ?>

<div class="page-information-card-container">
<div class="page-information-card card bg-gradient-secondary shadow-lg border-0">
<div class="page-information-card card bg-gradient-secondary shadow-lg border-0" <?php if (isset($_GET['post_type'])){echo 'style="animation: none;"';}?>>
<div class="card-body">
<h3 class="text-black mr-2 d-inline-block"> <?php echo get_search_query();?> </h3>
<p class="lead text-black mt-0 d-inline-block">
<?php _e('的搜索结果', 'argon');?>
</p>
<?php
$search_post_type = argon_get_search_post_type_array();
if (get_option('argon_search_post_filter', 'post,page') != 'off'){ ?>
<div class="search-filters">
<div class="custom-control custom-checkbox search-filter-wrapper">
<input class="custom-control-input search-filter" name="post" id="search_filter_post" type="checkbox" <?php echo in_array('post', $search_post_type) ? 'checked="true"' : ''; ?>>
<label class="custom-control-label" for="search_filter_post">文章</label>
</div>
<div class="custom-control custom-checkbox search-filter-wrapper">
<input class="custom-control-input search-filter" name="page" id="search_filter_page" type="checkbox" <?php echo in_array('page', $search_post_type) ? 'checked="true"' : ''; ?>>
<label class="custom-control-label" for="search_filter_page">页面</label>
</div>
<?php if (strpos(get_option('argon_search_post_filter', 'post,page'), 'hide_shuoshuo') === false) { ?>
<div class="custom-control custom-checkbox search-filter-wrapper">
<input class="custom-control-input search-filter" name="shuoshuo" id="search_filter_shuoshuo" type="checkbox" <?php echo in_array('shuoshuo', $search_post_type) ? 'checked="true"' : ''; ?>>
<label class="custom-control-label" for="search_filter_shuoshuo">说说</label>
</div>
<?php } ?>
</div>
<script>
$(".search-filter").prop("checked", false);
$("<?php echo "#search_filter_" . implode(",#search_filter_", $search_post_type); ?>").prop("checked", true);
</script>
<?php } ?>
<p class="text-black mt-3 mb-0 opacity-8">
<i class="fa fa-file-o mr-1"></i>
<?php global $wp_query; echo $wp_query -> found_posts; ?> <?php _e('个结果', 'argon');?>
Expand All @@ -18,7 +42,7 @@
<?php get_sidebar(); ?>

<div id="primary" class="content-area">
<main id="main" class="site-main article-list" role="main">
<main id="main" class="site-main article-list search-result" role="main">
<?php if ( have_posts() ) : ?>
<?php
while ( have_posts() ) :
Expand Down
7 changes: 7 additions & 0 deletions style.css
Original file line number Diff line number Diff line change
Expand Up @@ -1988,6 +1988,13 @@ html.darkmode.amoled-dark .tippy-box[data-theme~="light"] > .tippy-svg-arrow {
margin: 30px 20px;
margin-top: 0px;
}
.search-filter-wrapper {
display: inline-block;
margin-right: 12px;
}
.search-filter-wrapper > .custom-control-label {
line-height: 25px;
}

/*文章导航 (上/下一篇文章)*/
.post-navigation {
Expand Down
6 changes: 5 additions & 1 deletion template-parts/content-none-search.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@
<header class="post-header text-center">
<i style="font-size:80px;opacity:.6;margin-top:30px;margin-bottom:15px;" aria-hidden="true" class="fa fa-folder-o"></i>
<h1 class="post-title"><?php _e('没有搜索结果', 'argon');?></h1>
<span><?php _e('换个关键词试试 ?', 'argon');?></span>
<?php if (($_GET['post_type'] ?? '') == 'none'){ ?>
<span><?php _e('似乎没有勾选任何分类', 'argon');?></span>
<?php } else { ?>
<span><?php _e('换个关键词试试 ?', 'argon');?></span>
<?php }?>
<br>
<a href="javascript:window.history.back(-1);" ondragstart="return false;" class="btn btn-outline-primary" style="margin-top:45px;">
<span class="btn-inner--icon"><i class="fa fa-chevron-left"></i></span>
Expand Down

0 comments on commit b033571

Please sign in to comment.