Align search experience with settings
This commit is contained in:
@@ -553,6 +553,73 @@
|
||||
font-size: 0.75rem;
|
||||
}
|
||||
|
||||
.nav-searchbox__meta {
|
||||
display: grid;
|
||||
gap: 10px;
|
||||
margin-top: 12px;
|
||||
}
|
||||
|
||||
.nav-searchbox__types {
|
||||
display: flex;
|
||||
flex-wrap: wrap;
|
||||
gap: 8px;
|
||||
}
|
||||
|
||||
.nav-searchbox__type {
|
||||
display: inline-flex;
|
||||
align-items: center;
|
||||
min-height: 32px;
|
||||
padding: 0 12px;
|
||||
border: 1px solid var(--color-line);
|
||||
border-radius: var(--radius-pill, 999px);
|
||||
background: var(--surface-2);
|
||||
color: var(--color-ink-3);
|
||||
font-size: 0.8125rem;
|
||||
font-weight: 600;
|
||||
cursor: pointer;
|
||||
}
|
||||
|
||||
.nav-searchbox__type.is-active {
|
||||
border-color: var(--color-primary);
|
||||
background: var(--color-primary-soft);
|
||||
color: var(--color-primary);
|
||||
}
|
||||
|
||||
.nav-searchbox__category-wrap {
|
||||
position: relative;
|
||||
}
|
||||
|
||||
.nav-searchbox__category {
|
||||
appearance: none;
|
||||
width: 100%;
|
||||
min-height: 36px;
|
||||
padding: 0 34px 0 12px;
|
||||
border: 1px solid var(--color-line);
|
||||
border-radius: var(--radius-pill, 999px);
|
||||
background: var(--surface-2);
|
||||
color: var(--color-ink);
|
||||
font: 500 0.875rem/1 var(--font-ui);
|
||||
}
|
||||
|
||||
.nav-searchbox__category-wrap::after {
|
||||
content: '';
|
||||
position: absolute;
|
||||
right: 12px;
|
||||
top: 50%;
|
||||
transform: translateY(-50%);
|
||||
width: 0;
|
||||
height: 0;
|
||||
border-left: 5px solid transparent;
|
||||
border-right: 5px solid transparent;
|
||||
border-top: 5px solid var(--color-ink-3);
|
||||
pointer-events: none;
|
||||
}
|
||||
|
||||
.nav-searchbox__category:disabled {
|
||||
opacity: 0.56;
|
||||
cursor: not-allowed;
|
||||
}
|
||||
|
||||
.nav-drawer__menu {
|
||||
flex: 1;
|
||||
padding: 16px 20px 32px;
|
||||
|
||||
@@ -242,13 +242,76 @@
|
||||
outline: none;
|
||||
}
|
||||
|
||||
/* ========== 关键词高亮 ========== */
|
||||
.item-heading mark,
|
||||
.item-excerpt mark {
|
||||
background: color-mix(in srgb, var(--color-primary) 18%, transparent);
|
||||
color: inherit;
|
||||
border-radius: 4px;
|
||||
padding: 0 0.15em;
|
||||
|
||||
.search-page-form__meta {
|
||||
display: flex;
|
||||
flex-wrap: wrap;
|
||||
align-items: center;
|
||||
justify-content: space-between;
|
||||
gap: 12px;
|
||||
margin: 0 0 16px;
|
||||
}
|
||||
|
||||
.search-page-form__types {
|
||||
display: flex;
|
||||
flex-wrap: wrap;
|
||||
gap: 8px;
|
||||
}
|
||||
|
||||
.search-page-form__type {
|
||||
display: inline-flex;
|
||||
align-items: center;
|
||||
min-height: 34px;
|
||||
padding: 0 14px;
|
||||
border: 1px solid var(--color-line);
|
||||
border-radius: var(--radius-pill);
|
||||
background: var(--surface-2);
|
||||
color: var(--color-ink-3);
|
||||
font-weight: 500;
|
||||
cursor: pointer;
|
||||
transition: border-color 0.2s ease, color 0.2s ease, background 0.2s ease;
|
||||
}
|
||||
|
||||
.search-page-form__type:hover,
|
||||
.search-page-form__type.is-active {
|
||||
border-color: var(--color-primary);
|
||||
color: var(--color-primary);
|
||||
background: var(--color-primary-soft, rgba(59, 130, 246, 0.12));
|
||||
}
|
||||
|
||||
.search-page-form__category-wrap {
|
||||
position: relative;
|
||||
}
|
||||
|
||||
.search-page-form__category {
|
||||
appearance: none;
|
||||
min-height: 36px;
|
||||
padding: 0 34px 0 14px;
|
||||
border: 1px solid var(--color-line);
|
||||
border-radius: var(--radius-pill);
|
||||
background: var(--surface-2);
|
||||
color: var(--color-ink);
|
||||
font: 500 14px/1 var(--font-ui);
|
||||
cursor: pointer;
|
||||
}
|
||||
|
||||
.search-page-form__category-wrap::after {
|
||||
content: '';
|
||||
position: absolute;
|
||||
right: 12px;
|
||||
top: 50%;
|
||||
transform: translateY(-50%);
|
||||
width: 0;
|
||||
height: 0;
|
||||
border-left: 5px solid transparent;
|
||||
border-right: 5px solid transparent;
|
||||
border-top: 5px solid var(--color-ink-3);
|
||||
pointer-events: none;
|
||||
}
|
||||
|
||||
.search-page-form__category:disabled {
|
||||
opacity: 0.56;
|
||||
cursor: not-allowed;
|
||||
}
|
||||
|
||||
/* ========== 空状态 ========== */
|
||||
@@ -339,6 +402,19 @@
|
||||
padding: 0 14px;
|
||||
}
|
||||
|
||||
.search-page-form__meta {
|
||||
flex-direction: column;
|
||||
align-items: stretch;
|
||||
}
|
||||
|
||||
.search-page-form__category-wrap {
|
||||
width: 100%;
|
||||
}
|
||||
|
||||
.search-page-form__category {
|
||||
width: 100%;
|
||||
}
|
||||
|
||||
.search-page-toolbar {
|
||||
flex-direction: column;
|
||||
align-items: flex-start;
|
||||
|
||||
@@ -49,6 +49,9 @@
|
||||
} else {
|
||||
url.searchParams.delete("aksrt_category");
|
||||
}
|
||||
if (url.searchParams.get("aksrt_search_type") && url.searchParams.get("aksrt_search_type") !== "post") {
|
||||
url.searchParams.delete("aksrt_category");
|
||||
}
|
||||
url.searchParams.delete("paged");
|
||||
window.location.href = url.toString();
|
||||
});
|
||||
|
||||
+66
-44
@@ -5,8 +5,6 @@
|
||||
"use strict";
|
||||
|
||||
function initSearchPanel() {
|
||||
var btn = document.getElementById("search-toggle");
|
||||
var panel = document.getElementById("nav-search-panel");
|
||||
var mobileOverlay = document.getElementById("tabbar-search-overlay");
|
||||
var mobileInput = document.getElementById("tabbar-search-input");
|
||||
var mobileClose = document.getElementById("tabbar-search-close");
|
||||
@@ -18,7 +16,8 @@
|
||||
var historyClear = document.getElementById("tabbar-search-history-clear");
|
||||
var suggestionsWrap = document.getElementById("tabbar-search-suggestions");
|
||||
var suggestionsSection = document.getElementById("tabbar-search-suggestions-section");
|
||||
if (!btn || !panel) return;
|
||||
var searchForms = Array.prototype.slice.call(document.querySelectorAll("[data-aksrt-search-form]"));
|
||||
if (!searchForms.length && !mobileForm) return;
|
||||
var HISTORY_KEY = "aksrt-mobile-search-history";
|
||||
var suggestTimer = null;
|
||||
var searchConfig = (window.aksrtData && aksrtData.search) || {};
|
||||
@@ -32,20 +31,66 @@
|
||||
suggestionsSection.hidden = true;
|
||||
}
|
||||
|
||||
function getDesktopSearchType() {
|
||||
if (!searchForms.length) return searchConfig.defaultType || "post";
|
||||
var input = searchForms[0].querySelector('input[name="aksrt_search_type"]');
|
||||
var value = input ? (input.value || "") : "";
|
||||
return value || searchConfig.defaultType || "post";
|
||||
}
|
||||
|
||||
function getDesktopCategory() {
|
||||
if (!searchForms.length) return "0";
|
||||
var input = searchForms[0].querySelector('input[name="aksrt_category"]');
|
||||
return input ? (input.value || "0") : "0";
|
||||
}
|
||||
|
||||
function syncSearchForms(type, category) {
|
||||
searchForms.forEach(function (form) {
|
||||
var typeInput = form.querySelector('input[name="aksrt_search_type"]');
|
||||
if (typeInput) {
|
||||
typeInput.value = type || searchConfig.defaultType || "post";
|
||||
}
|
||||
var categoryInput = form.querySelector('input[name="aksrt_category"]');
|
||||
if (categoryInput) {
|
||||
if ((type || searchConfig.defaultType || "post") === "post" && category && category !== "0") {
|
||||
categoryInput.value = category;
|
||||
} else {
|
||||
categoryInput.value = "";
|
||||
}
|
||||
}
|
||||
form.querySelectorAll("[data-search-type]").forEach(function (button) {
|
||||
button.classList.toggle("is-active", button.getAttribute("data-search-type") === (type || searchConfig.defaultType || "post"));
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
function syncCategorySelectors(type, category) {
|
||||
document.querySelectorAll("[data-search-category-select]").forEach(function (select) {
|
||||
select.value = category && category !== "" ? category : "0";
|
||||
select.disabled = (type || searchConfig.defaultType || "post") !== "post";
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
function getSearchType() {
|
||||
return mobileTypeInput ? (mobileTypeInput.value || "post") : "post";
|
||||
if (mobileTypeInput) {
|
||||
return mobileTypeInput.value || getDesktopSearchType();
|
||||
}
|
||||
return getDesktopSearchType();
|
||||
}
|
||||
|
||||
function setSearchType(type) {
|
||||
if (!mobileTypeInput) return;
|
||||
mobileTypeInput.value = type;
|
||||
var nextType = type || searchConfig.defaultType || getDesktopSearchType();
|
||||
if (mobileTypeInput) {
|
||||
mobileTypeInput.value = nextType;
|
||||
}
|
||||
mobileTypeButtons.forEach(function (button) {
|
||||
button.classList.toggle("is-active", button.getAttribute("data-search-type") === type);
|
||||
button.classList.toggle("is-active", button.getAttribute("data-search-type") === nextType);
|
||||
});
|
||||
syncSearchForms(nextType, getDesktopCategory());
|
||||
syncCategorySelectors(nextType, getDesktopCategory());
|
||||
requestSuggestions();
|
||||
}
|
||||
|
||||
function readHistory() {
|
||||
if (!historyEnabled) return [];
|
||||
if (aksrtData.isLoggedIn) return [];
|
||||
try {
|
||||
@@ -218,34 +263,10 @@
|
||||
document.body.classList.remove("tabbar-search-open");
|
||||
if (suggestionsSection) suggestionsSection.hidden = true;
|
||||
}
|
||||
btn.setAttribute("aria-expanded", open ? "true" : "false");
|
||||
return;
|
||||
}
|
||||
|
||||
panel.classList.toggle("is-open", open);
|
||||
btn.setAttribute("aria-expanded", open ? "true" : "false");
|
||||
if (open) {
|
||||
var input = panel.querySelector('input[type="search"]');
|
||||
if (input) input.focus();
|
||||
}
|
||||
}
|
||||
|
||||
btn.addEventListener("click", function (e) {
|
||||
e.stopPropagation();
|
||||
if (isMobileSearchMode()) {
|
||||
setOpen(!mobileOverlay || mobileOverlay.hasAttribute("hidden"));
|
||||
return;
|
||||
}
|
||||
setOpen(!panel.classList.contains("is-open"));
|
||||
});
|
||||
|
||||
document.addEventListener("click", function (e) {
|
||||
if (isMobileSearchMode()) return;
|
||||
if (!panel.classList.contains("is-open")) return;
|
||||
if (panel.contains(e.target) || btn.contains(e.target)) return;
|
||||
setOpen(false);
|
||||
});
|
||||
|
||||
if (mobileOverlay) {
|
||||
mobileOverlay.addEventListener("click", function (e) {
|
||||
if (e.target === mobileOverlay) setOpen(false);
|
||||
@@ -280,6 +301,14 @@
|
||||
});
|
||||
}
|
||||
|
||||
document.querySelectorAll("[data-search-category-select]").forEach(function (select) {
|
||||
select.addEventListener("change", function () {
|
||||
var nextCategory = this.value || "0";
|
||||
syncSearchForms(getSearchType(), nextCategory);
|
||||
syncCategorySelectors(getSearchType(), nextCategory);
|
||||
});
|
||||
});
|
||||
|
||||
mobileTypeButtons.forEach(function (button) {
|
||||
button.addEventListener("click", function () {
|
||||
setSearchType(button.getAttribute("data-search-type") || "post");
|
||||
@@ -298,18 +327,20 @@
|
||||
});
|
||||
}
|
||||
|
||||
document.querySelectorAll("[data-aksrt-search-form]").forEach(function (form) {
|
||||
searchForms.forEach(function (form) {
|
||||
if (form === mobileForm) return;
|
||||
form.addEventListener("submit", function () {
|
||||
if (!historyEnabled) return;
|
||||
var input = form.querySelector('input[name="s"]');
|
||||
var typeInput = form.querySelector('input[name="aksrt_search_type"]');
|
||||
var keyword = input ? (input.value || "").trim() : "";
|
||||
var type = typeInput ? (typeInput.value || "post") : "post";
|
||||
var type = typeInput ? (typeInput.value || searchConfig.defaultType || "post") : (searchConfig.defaultType || "post");
|
||||
pushHistory(keyword, type);
|
||||
});
|
||||
});
|
||||
|
||||
syncSearchForms(getSearchType(), getDesktopCategory());
|
||||
syncCategorySelectors(getSearchType(), getDesktopCategory());
|
||||
setSearchType(getSearchType());
|
||||
if (historyEnabled && aksrtData.isLoggedIn) {
|
||||
requestHistorySync("get", null, function (items) {
|
||||
@@ -318,17 +349,8 @@
|
||||
}
|
||||
|
||||
document.addEventListener("keydown", function (e) {
|
||||
if ((e.metaKey || e.ctrlKey) && e.key.toLowerCase() === "k") {
|
||||
e.preventDefault();
|
||||
setOpen(true);
|
||||
}
|
||||
if (e.key === "Escape" && panel.classList.contains("is-open")) {
|
||||
setOpen(false);
|
||||
btn.focus();
|
||||
}
|
||||
if (e.key === "Escape" && isMobileSearchMode() && mobileOverlay && !mobileOverlay.hasAttribute("hidden")) {
|
||||
setOpen(false);
|
||||
btn.focus();
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
+91
-52
@@ -118,8 +118,21 @@ if (!empty($aksrt_bg_styles)) {
|
||||
$aksrt_logo_url = function_exists('aksrt_get_site_logo_url') ? trim((string) aksrt_get_site_logo_url()) : '';
|
||||
$aksrt_logo_dark_url = function_exists('aksrt_get_site_logo_dark_url') ? trim((string) aksrt_get_site_logo_dark_url()) : '';
|
||||
$aksrt_logo_width = function_exists('aksrt_get_site_logo_width') ? aksrt_get_site_logo_width() : 150;
|
||||
$aksrt_search_state = function_exists('aksrt_get_search_form_state')
|
||||
? aksrt_get_search_form_state()
|
||||
: [
|
||||
'type' => 'post',
|
||||
'types' => ['post' => __('文章', 'aksrt')],
|
||||
'category_id' => 0,
|
||||
'category_enabled' => false,
|
||||
'categories' => [],
|
||||
];
|
||||
$aksrt_search_placeholder = function_exists('aksrt_get_search_placeholder') ? aksrt_get_search_placeholder() : __('开启精彩搜索', 'aksrt');
|
||||
$aksrt_search_type = function_exists('aksrt_get_search_form_type') ? aksrt_get_search_form_type() : 'post';
|
||||
$aksrt_search_type = $aksrt_search_state['type'];
|
||||
$aksrt_search_type_options = $aksrt_search_state['types'];
|
||||
$aksrt_search_category_enabled = !empty($aksrt_search_state['category_enabled']);
|
||||
$aksrt_search_category_id = (int) ($aksrt_search_state['category_id'] ?? 0);
|
||||
$aksrt_search_categories = is_array($aksrt_search_state['categories'] ?? null) ? $aksrt_search_state['categories'] : [];
|
||||
$aksrt_search_hot_keywords = function_exists('aksrt_get_search_hot_keywords') ? aksrt_get_search_hot_keywords() : [];
|
||||
$aksrt_brand_logo_src = '';
|
||||
$aksrt_mobile_menu_location = has_nav_menu('mobile') ? 'mobile' : 'primary';
|
||||
@@ -208,8 +221,11 @@ if (!empty($aksrt_bg_styles)) {
|
||||
|
||||
<div class="nav-actions">
|
||||
<?php if (!empty($aksrt_header_settings['search_enabled'])) : ?>
|
||||
<form role="search" method="get" class="nav-searchbox" action="<?php echo esc_url(home_url('/')); ?>" data-aksrt-search-form="desktop">
|
||||
<form role="search" method="get" class="nav-searchbox" action="<?php echo esc_url(home_url('/')); ?>" data-aksrt-search-form="desktop" data-default-search-type="<?php echo esc_attr($aksrt_search_type); ?>" data-default-category="<?php echo esc_attr((string) $aksrt_search_category_id); ?>">
|
||||
<input type="hidden" name="aksrt_search_type" value="<?php echo esc_attr($aksrt_search_type); ?>">
|
||||
<?php if ($aksrt_search_category_enabled) : ?>
|
||||
<input type="hidden" name="aksrt_category" value="<?php echo esc_attr((string) $aksrt_search_category_id); ?>">
|
||||
<?php endif; ?>
|
||||
<label class="screen-reader-text" for="site-nav-search-s"><?php esc_html_e('搜索', 'aksrt'); ?></label>
|
||||
<input class="nav-searchbox__input" type="search" id="site-nav-search-s" name="s" value="<?php echo esc_attr(get_search_query()); ?>" placeholder="<?php echo esc_attr($aksrt_search_placeholder); ?>">
|
||||
<button type="submit" class="nav-searchbox__submit"><?php esc_html_e('搜索', 'aksrt'); ?></button>
|
||||
@@ -296,8 +312,11 @@ if (!empty($aksrt_bg_styles)) {
|
||||
|
||||
<?php if (!empty($aksrt_header_settings['search_enabled'])) : ?>
|
||||
<div class="nav-drawer__search">
|
||||
<form role="search" method="get" class="nav-searchbox nav-searchbox--drawer" action="<?php echo esc_url(home_url('/')); ?>" data-aksrt-search-form="drawer">
|
||||
<form role="search" method="get" class="nav-searchbox nav-searchbox--drawer" action="<?php echo esc_url(home_url('/')); ?>" data-aksrt-search-form="drawer" data-default-search-type="<?php echo esc_attr($aksrt_search_type); ?>" data-default-category="<?php echo esc_attr((string) $aksrt_search_category_id); ?>">
|
||||
<input type="hidden" name="aksrt_search_type" value="<?php echo esc_attr($aksrt_search_type); ?>">
|
||||
<?php if ($aksrt_search_category_enabled) : ?>
|
||||
<input type="hidden" name="aksrt_category" value="<?php echo esc_attr((string) $aksrt_search_category_id); ?>">
|
||||
<?php endif; ?>
|
||||
<div class="nav-searchbox__field">
|
||||
<span class="nav-searchbox__icon" aria-hidden="true">
|
||||
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><circle cx="11" cy="11" r="8"/><path d="m21 21-4.3-4.3"/></svg>
|
||||
@@ -307,60 +326,80 @@ if (!empty($aksrt_bg_styles)) {
|
||||
<button type="submit" class="nav-searchbox__submit"><?php esc_html_e('搜索', 'aksrt'); ?></button>
|
||||
</div>
|
||||
</form>
|
||||
<?php if (!empty($aksrt_search_type_options)) : ?>
|
||||
<div class="nav-searchbox__meta">
|
||||
<div class="nav-searchbox__types" aria-label="<?php esc_attr_e('搜索类型', 'aksrt'); ?>">
|
||||
<?php foreach ($aksrt_search_type_options as $type_key => $type_label) : ?>
|
||||
<button type="button" class="nav-searchbox__type<?php echo $aksrt_search_type === $type_key ? ' is-active' : ''; ?>" data-search-type="<?php echo esc_attr($type_key); ?>"><?php echo esc_html($type_label); ?></button>
|
||||
<?php endforeach; ?>
|
||||
</div>
|
||||
<?php if ($aksrt_search_category_enabled && !empty($aksrt_search_categories)) : ?>
|
||||
<div class="nav-searchbox__category-wrap">
|
||||
<label class="screen-reader-text" for="nav-drawer-search-category"><?php esc_html_e('默认分类', 'aksrt'); ?></label>
|
||||
<select class="nav-searchbox__category" id="nav-drawer-search-category" data-search-category-select>
|
||||
<option value="0"><?php esc_html_e('全部分类', 'aksrt'); ?></option>
|
||||
<?php foreach ($aksrt_search_categories as $search_category) : ?>
|
||||
<option value="<?php echo esc_attr((string) $search_category->term_id); ?>"<?php selected($aksrt_search_category_id, (int) $search_category->term_id); ?>><?php echo esc_html($search_category->name); ?></option>
|
||||
<?php endforeach; ?>
|
||||
</select>
|
||||
</div>
|
||||
<?php endif; ?>
|
||||
</div>
|
||||
<?php endif; ?>
|
||||
<?php aksrt_render_search_hot_keywords($aksrt_search_hot_keywords, 'aksrt-search-hot-keywords'); ?>
|
||||
</div>
|
||||
<?php endif; ?>
|
||||
|
||||
<div class="nav-drawer__menu">
|
||||
<?php
|
||||
if (has_nav_menu($aksrt_mobile_menu_location)) {
|
||||
wp_nav_menu(
|
||||
[
|
||||
'theme_location' => $aksrt_mobile_menu_location,
|
||||
'container' => false,
|
||||
'menu_class' => '',
|
||||
'fallback_cb' => false,
|
||||
'depth' => 2,
|
||||
]
|
||||
);
|
||||
} else {
|
||||
echo '<a href="' . esc_url(home_url('/')) . '">' . esc_html__('首页', 'aksrt') . '</a>';
|
||||
}
|
||||
?>
|
||||
</div>
|
||||
|
||||
<?php if ($aksrt_can_publish) : ?>
|
||||
<div class="nav-drawer__publish">
|
||||
<a class="btn-primary" href="<?php echo esc_url(admin_url('post-new.php')); ?>">
|
||||
<i class="fas fa-pen-nib" aria-hidden="true"></i>
|
||||
<span><?php esc_html_e('发布内容', 'aksrt'); ?></span>
|
||||
</a>
|
||||
<div class="nav-drawer__menu">
|
||||
<?php
|
||||
if (has_nav_menu($aksrt_mobile_menu_location)) {
|
||||
wp_nav_menu(
|
||||
[
|
||||
'theme_location' => $aksrt_mobile_menu_location,
|
||||
'container' => false,
|
||||
'menu_class' => '',
|
||||
'fallback_cb' => false,
|
||||
'depth' => 2,
|
||||
]
|
||||
);
|
||||
} else {
|
||||
echo '<a href="' . esc_url(home_url('/')) . '">' . esc_html__('首页', 'aksrt') . '</a>';
|
||||
}
|
||||
?>
|
||||
</div>
|
||||
<?php endif; ?>
|
||||
|
||||
<?php if (in_array('m_menu', $aksrt_theme_toggle_positions, true)) : ?>
|
||||
<div class="nav-drawer__theme-toggle">
|
||||
<button type="button" class="btn-outline theme-toggle-mobile" id="theme-toggle-mobile" title="<?php esc_attr_e('切换亮暗色', 'aksrt'); ?>">
|
||||
<i class="fas fa-adjust" aria-hidden="true"></i>
|
||||
<span><?php esc_html_e('切换主题', 'aksrt'); ?></span>
|
||||
</button>
|
||||
<?php if ($aksrt_can_publish) : ?>
|
||||
<div class="nav-drawer__publish">
|
||||
<a class="btn-primary" href="<?php echo esc_url(admin_url('post-new.php')); ?>">
|
||||
<i class="fas fa-pen-nib" aria-hidden="true"></i>
|
||||
<span><?php esc_html_e('发布内容', 'aksrt'); ?></span>
|
||||
</a>
|
||||
</div>
|
||||
<?php endif; ?>
|
||||
|
||||
<?php if (in_array('m_menu', $aksrt_theme_toggle_positions, true)) : ?>
|
||||
<div class="nav-drawer__theme-toggle">
|
||||
<button type="button" class="btn-outline theme-toggle-mobile" id="theme-toggle-mobile" title="<?php esc_attr_e('切换亮暗色', 'aksrt'); ?>">
|
||||
<i class="fas fa-adjust" aria-hidden="true"></i>
|
||||
<span><?php esc_html_e('切换主题', 'aksrt'); ?></span>
|
||||
</button>
|
||||
</div>
|
||||
<?php endif; ?>
|
||||
|
||||
<?php if ($aksrt_show_user_entry && !is_user_logged_in()) : ?>
|
||||
<div class="nav-drawer__auth">
|
||||
<a class="btn-outline" href="<?php echo esc_url(aksrt_get_login_url()); ?>"><?php esc_html_e('登录', 'aksrt'); ?></a>
|
||||
<?php if (get_option('users_can_register')) : ?>
|
||||
<a class="btn-primary" href="<?php echo esc_url(aksrt_get_register_url()); ?>"><?php esc_html_e('注册', 'aksrt'); ?></a>
|
||||
<?php endif; ?>
|
||||
</div>
|
||||
<?php endif; ?>
|
||||
|
||||
<?php if (is_active_sidebar('mobile_nav_fluid')) : ?>
|
||||
<div class="nav-drawer__widgets">
|
||||
<?php dynamic_sidebar('mobile_nav_fluid'); ?>
|
||||
</div>
|
||||
<?php endif; ?>
|
||||
|
||||
<?php if ($aksrt_show_user_entry && !is_user_logged_in()) : ?>
|
||||
<div class="nav-drawer__auth">
|
||||
<a class="btn-outline" href="<?php echo esc_url(aksrt_get_login_url()); ?>"><?php esc_html_e('登录', 'aksrt'); ?></a>
|
||||
<?php if (get_option('users_can_register')) : ?>
|
||||
<a class="btn-primary" href="<?php echo esc_url(aksrt_get_register_url()); ?>"><?php esc_html_e('注册', 'aksrt'); ?></a>
|
||||
<?php endif; ?>
|
||||
</div>
|
||||
<?php endif; ?>
|
||||
|
||||
<?php if (is_active_sidebar('mobile_nav_fluid')) : ?>
|
||||
<div class="nav-drawer__widgets">
|
||||
<?php dynamic_sidebar('mobile_nav_fluid'); ?>
|
||||
</div>
|
||||
<?php endif; ?>
|
||||
<div class="fixed-body"></div>
|
||||
</nav>
|
||||
<?php endif; ?>
|
||||
<div class="fixed-body"></div>
|
||||
</nav>
|
||||
</div>
|
||||
|
||||
@@ -234,10 +234,12 @@ class AKSRT_Settings_Theme_Search {
|
||||
$all = AKSRT_Settings::get_all();
|
||||
foreach ($all_types as $type => $key) {
|
||||
$default_on = in_array($type, ['post', 'category', 'tag'], true);
|
||||
if (!isset($all[$key]) || !empty($all[$key]) && $default_on) {
|
||||
if (!empty(AKSRT_Settings::get_value($key, $default_on))) {
|
||||
$enabled[] = $type;
|
||||
}
|
||||
$is_enabled = array_key_exists($key, $all)
|
||||
? !empty(AKSRT_Settings::get_value($key, $default_on))
|
||||
: $default_on;
|
||||
|
||||
if ($is_enabled) {
|
||||
$enabled[] = $type;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -558,8 +558,175 @@ if (!function_exists('aksrt_get_search_form_type')):
|
||||
* @return string
|
||||
*/
|
||||
function aksrt_get_search_form_type(): string {
|
||||
$configured = ['post' => __('文章', 'aksrt')];
|
||||
|
||||
if (class_exists('AKSRT_Settings')) {
|
||||
$configured = aksrt_get_search_type_options();
|
||||
}
|
||||
|
||||
$allowed_types = array_keys($configured);
|
||||
if (empty($allowed_types)) {
|
||||
$allowed_types = ['post'];
|
||||
}
|
||||
|
||||
$type = sanitize_key((string) get_query_var('aksrt_search_type'));
|
||||
return in_array($type, ['post', 'category', 'tag'], true) ? $type : 'post';
|
||||
if ($type === '' && isset($_GET['aksrt_search_type'])) {
|
||||
$type = sanitize_key((string) wp_unslash($_GET['aksrt_search_type']));
|
||||
}
|
||||
|
||||
if (in_array($type, $allowed_types, true)) {
|
||||
return $type;
|
||||
}
|
||||
|
||||
return function_exists('aksrt_get_default_search_type') ? aksrt_get_default_search_type() : 'post';
|
||||
}
|
||||
endif;
|
||||
|
||||
if (!function_exists('aksrt_get_search_type_options')):
|
||||
/**
|
||||
* 获取前台支持的搜索类型选项
|
||||
*
|
||||
* @return array<string,string>
|
||||
*/
|
||||
function aksrt_get_search_type_options(): array {
|
||||
$label_map = [
|
||||
'post' => __('文章', 'aksrt'),
|
||||
'category' => __('分类', 'aksrt'),
|
||||
'tag' => __('标签', 'aksrt'),
|
||||
];
|
||||
|
||||
if (!class_exists('AKSRT_Settings')) {
|
||||
return ['post' => $label_map['post']];
|
||||
}
|
||||
|
||||
$config = AKSRT_Settings::get_search_content_types();
|
||||
$types = isset($config['types']) && is_array($config['types']) ? $config['types'] : ['post'];
|
||||
$options = [];
|
||||
|
||||
foreach ($types as $type) {
|
||||
$type = sanitize_key((string) $type);
|
||||
if (isset($label_map[$type])) {
|
||||
$options[$type] = $label_map[$type];
|
||||
}
|
||||
}
|
||||
|
||||
if (empty($options)) {
|
||||
$options['post'] = $label_map['post'];
|
||||
}
|
||||
|
||||
return $options;
|
||||
}
|
||||
endif;
|
||||
|
||||
if (!function_exists('aksrt_get_default_search_type')):
|
||||
/**
|
||||
* 获取默认搜索类型
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
function aksrt_get_default_search_type(): string {
|
||||
$options = function_exists('aksrt_get_search_type_options') ? aksrt_get_search_type_options() : ['post' => __('文章', 'aksrt')];
|
||||
$allowed = array_keys($options);
|
||||
|
||||
if (class_exists('AKSRT_Settings')) {
|
||||
$config = AKSRT_Settings::get_search_content_types();
|
||||
$default = sanitize_key((string) ($config['default'] ?? 'post'));
|
||||
if (in_array($default, $allowed, true)) {
|
||||
return $default;
|
||||
}
|
||||
}
|
||||
|
||||
return in_array('post', $allowed, true) ? 'post' : (string) reset($allowed);
|
||||
}
|
||||
endif;
|
||||
|
||||
if (!function_exists('aksrt_get_search_default_category_id')):
|
||||
/**
|
||||
* 获取默认搜索分类 ID
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
function aksrt_get_search_default_category_id(): int {
|
||||
if (!class_exists('AKSRT_Settings') || !AKSRT_Settings::search_cat_enabled()) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
return max(0, absint(AKSRT_Settings::search_cat_in()));
|
||||
}
|
||||
endif;
|
||||
|
||||
if (!function_exists('aksrt_get_search_categories')):
|
||||
/**
|
||||
* 获取搜索可选分类列表
|
||||
*
|
||||
* @return WP_Term[]
|
||||
*/
|
||||
function aksrt_get_search_categories(): array {
|
||||
$categories = get_categories([
|
||||
'taxonomy' => 'category',
|
||||
'hide_empty' => true,
|
||||
'number' => 0,
|
||||
'orderby' => 'name',
|
||||
'order' => 'ASC',
|
||||
'fields' => 'all',
|
||||
]);
|
||||
|
||||
if (!is_array($categories)) {
|
||||
return [];
|
||||
}
|
||||
|
||||
return array_values(array_filter($categories, static function ($term): bool {
|
||||
return $term instanceof WP_Term;
|
||||
}));
|
||||
}
|
||||
endif;
|
||||
|
||||
if (!function_exists('aksrt_get_search_form_state')):
|
||||
/**
|
||||
* 获取搜索表单运行时状态
|
||||
*
|
||||
* @return array{type:string,types:array<string,string>,category_id:int,category_enabled:bool,categories:array<int,WP_Term>}
|
||||
*/
|
||||
function aksrt_get_search_form_state(): array {
|
||||
$types = function_exists('aksrt_get_search_type_options') ? aksrt_get_search_type_options() : ['post' => __('文章', 'aksrt')];
|
||||
$type = function_exists('aksrt_get_search_form_type') ? aksrt_get_search_form_type() : 'post';
|
||||
$default_category = function_exists('aksrt_get_search_default_category_id') ? aksrt_get_search_default_category_id() : 0;
|
||||
$current_category = isset($_GET['aksrt_category']) ? absint(wp_unslash($_GET['aksrt_category'])) : $default_category;
|
||||
$category_enabled = class_exists('AKSRT_Settings') ? AKSRT_Settings::search_cat_enabled() : false;
|
||||
$categories = $category_enabled ? aksrt_get_search_categories() : [];
|
||||
|
||||
return [
|
||||
'type' => isset($types[$type]) ? $type : (function_exists('aksrt_get_default_search_type') ? aksrt_get_default_search_type() : 'post'),
|
||||
'types' => $types,
|
||||
'category_id' => $current_category,
|
||||
'category_enabled' => $category_enabled,
|
||||
'categories' => $categories,
|
||||
];
|
||||
}
|
||||
endif;
|
||||
|
||||
if (!function_exists('aksrt_build_search_url')):
|
||||
/**
|
||||
* 构建搜索跳转 URL
|
||||
*
|
||||
* @param string $keyword 搜索关键词
|
||||
* @param array $args 追加参数
|
||||
* @return string
|
||||
*/
|
||||
function aksrt_build_search_url(string $keyword = '', array $args = []): string {
|
||||
$params = [];
|
||||
if ($keyword !== '') {
|
||||
$params['s'] = $keyword;
|
||||
}
|
||||
|
||||
foreach ($args as $key => $value) {
|
||||
if ($value === null || $value === '' || $value === false) {
|
||||
continue;
|
||||
}
|
||||
$params[$key] = $value;
|
||||
}
|
||||
|
||||
return esc_url(add_query_arg($params, home_url('/')));
|
||||
}
|
||||
endif;
|
||||
|
||||
@@ -660,7 +827,7 @@ function aksrt_render_search_hot_keywords(array $keywords = [], string $class =
|
||||
<span class="<?php echo esc_attr($class . '__label'); ?>"><?php echo esc_html($title); ?></span>
|
||||
<div class="<?php echo esc_attr($class . '__list tabbar-search-overlay__tags'); ?>">
|
||||
<?php foreach ($keywords as $keyword) : ?>
|
||||
<a class="tabbar-search-overlay__tag" href="<?php echo esc_url(add_query_arg('s', $keyword, home_url('/'))); ?>">
|
||||
<a class="tabbar-search-overlay__tag" href="<?php echo aksrt_build_search_url($keyword, ['aksrt_search_type' => aksrt_get_default_search_type()]); ?>">
|
||||
<?php echo esc_html($keyword); ?>
|
||||
</a>
|
||||
<?php endforeach; ?>
|
||||
|
||||
+70
-30
@@ -12,9 +12,19 @@ global $wp_query;
|
||||
|
||||
$search_query = get_search_query();
|
||||
$loop_query = $wp_query;
|
||||
$search_state = function_exists('aksrt_get_search_form_state')
|
||||
? aksrt_get_search_form_state()
|
||||
: [
|
||||
'type' => 'post',
|
||||
'types' => ['post' => __('文章', 'aksrt')],
|
||||
'category_id' => 0,
|
||||
'category_enabled' => false,
|
||||
'categories' => [],
|
||||
];
|
||||
$has_search_query = '' !== trim($search_query);
|
||||
$search_placeholder = function_exists('aksrt_get_search_placeholder') ? aksrt_get_search_placeholder() : __('开启精彩搜索', 'aksrt');
|
||||
$search_type = function_exists('aksrt_get_search_form_type') ? aksrt_get_search_form_type() : 'post';
|
||||
$search_type = $search_state['type'];
|
||||
$search_type_options = !empty($search_state['types']) && is_array($search_state['types']) ? $search_state['types'] : ['post' => __('文章', 'aksrt')];
|
||||
$hot_keywords = function_exists('aksrt_get_search_hot_keywords') ? aksrt_get_search_hot_keywords() : [];
|
||||
$filter_settings = class_exists('AKSRT_Settings')
|
||||
? AKSRT_Settings::get_post_filter_settings()
|
||||
@@ -33,15 +43,9 @@ $popular_categories = get_categories([
|
||||
'order' => 'DESC',
|
||||
]);
|
||||
|
||||
$current_category = isset($_GET['aksrt_category']) ? absint($_GET['aksrt_category']) : 0;
|
||||
$search_categories = get_categories([
|
||||
'taxonomy' => 'category',
|
||||
'hide_empty' => true,
|
||||
'number' => 0,
|
||||
'orderby' => 'name',
|
||||
'order' => 'ASC',
|
||||
'fields' => 'all',
|
||||
]);
|
||||
$current_category = (int) ($search_state['category_id'] ?? 0);
|
||||
$search_categories = is_array($search_state['categories'] ?? null) ? $search_state['categories'] : [];
|
||||
$search_category_enabled = !empty($search_state['category_enabled']);
|
||||
|
||||
$search_sidebar_id = is_active_sidebar('search_sidebar') ? 'search_sidebar' : 'sidebar-blog';
|
||||
$has_sidebar = !empty($other_page_settings['page_search_sidebar']) && is_active_sidebar($search_sidebar_id);
|
||||
@@ -57,7 +61,7 @@ if ($current_orderby === 'date' && $current_order === 'DESC') {
|
||||
$current_sort = 'popular';
|
||||
}
|
||||
|
||||
if ($current_category > 0) {
|
||||
if ($current_category > 0 && $search_type === 'post') {
|
||||
$filtered_query_args = [
|
||||
's' => $search_query,
|
||||
'post_type' => 'post',
|
||||
@@ -87,16 +91,6 @@ $render_config = [
|
||||
'highlight_query' => $search_query,
|
||||
];
|
||||
|
||||
$search_type_options = [
|
||||
'post' => __('文章', 'aksrt'),
|
||||
];
|
||||
if (!empty($filter_settings['categories'])) {
|
||||
$search_type_options['category'] = __('分类', 'aksrt');
|
||||
}
|
||||
if (!empty($filter_settings['tags'])) {
|
||||
$search_type_options['tag'] = __('标签', 'aksrt');
|
||||
}
|
||||
|
||||
$search_sort_options = [
|
||||
'relevance' => [
|
||||
'label' => __('综合', 'aksrt'),
|
||||
@@ -148,8 +142,11 @@ $search_sort_options = [
|
||||
?>
|
||||
|
||||
<section class="content-stream content-stream--search" data-page-kind="search" data-render-mode="<?php echo esc_attr((string) $render_config['mode']); ?>" data-card-style="<?php echo esc_attr((string) $render_config['card_style']); ?>">
|
||||
<form role="search" method="get" class="search-hero-form search-page-form" action="<?php echo esc_url(home_url('/')); ?>" data-aksrt-search-form="search-page">
|
||||
<form role="search" method="get" class="search-hero-form search-page-form" action="<?php echo esc_url(home_url('/')); ?>" data-aksrt-search-form="search-page" data-default-search-type="<?php echo esc_attr($search_type); ?>" data-default-category="<?php echo esc_attr((string) $current_category); ?>">
|
||||
<input type="hidden" name="aksrt_search_type" value="<?php echo esc_attr($search_type); ?>">
|
||||
<?php if ($search_category_enabled) : ?>
|
||||
<input type="hidden" name="aksrt_category" value="<?php echo esc_attr((string) $current_category); ?>">
|
||||
<?php endif; ?>
|
||||
<span class="search-icon search-page-form__icon" aria-hidden="true">
|
||||
<svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><circle cx="11" cy="11" r="8"/><path d="m21 21-4.3-4.3"/></svg>
|
||||
</span>
|
||||
@@ -158,6 +155,26 @@ $search_sort_options = [
|
||||
<button type="button" class="clear-search search-page-form__clear" id="search-clear-btn" aria-label="<?php esc_attr_e('清空搜索内容', 'aksrt'); ?>"><svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><path d="M18 6 6 18"/><path d="m6 6 12 12"/></svg></button>
|
||||
<button type="submit" class="search-page-form__submit"><?php esc_html_e('搜索', 'aksrt'); ?></button>
|
||||
</form>
|
||||
<?php if (!empty($search_type_options)) : ?>
|
||||
<div class="search-page-form__meta">
|
||||
<div class="search-page-form__types" aria-label="<?php esc_attr_e('搜索类型', 'aksrt'); ?>">
|
||||
<?php foreach ($search_type_options as $type_key => $type_label) : ?>
|
||||
<button type="button" class="search-page-form__type<?php echo $search_type === $type_key ? ' is-active' : ''; ?>" data-search-type="<?php echo esc_attr($type_key); ?>"><?php echo esc_html($type_label); ?></button>
|
||||
<?php endforeach; ?>
|
||||
</div>
|
||||
<?php if ($search_category_enabled && !empty($search_categories)) : ?>
|
||||
<div class="search-page-form__category-wrap">
|
||||
<label class="screen-reader-text" for="search-page-category"><?php esc_html_e('筛选分类', 'aksrt'); ?></label>
|
||||
<select class="search-page-form__category" id="search-page-category" data-search-category-select>
|
||||
<option value="0"><?php esc_html_e('全部分类', 'aksrt'); ?></option>
|
||||
<?php foreach ($search_categories as $search_category) : ?>
|
||||
<option value="<?php echo esc_attr((string) $search_category->term_id); ?>"<?php selected($current_category, (int) $search_category->term_id); ?>><?php echo esc_html($search_category->name); ?></option>
|
||||
<?php endforeach; ?>
|
||||
</select>
|
||||
</div>
|
||||
<?php endif; ?>
|
||||
</div>
|
||||
<?php endif; ?>
|
||||
<?php aksrt_render_search_hot_keywords($hot_keywords, 'aksrt-search-hot-keywords'); ?>
|
||||
|
||||
<?php if (!empty($filter_settings['enabled'])) : ?>
|
||||
@@ -167,7 +184,18 @@ $search_sort_options = [
|
||||
<span class="search-page-filters__label"><?php esc_html_e('类型', 'aksrt'); ?></span>
|
||||
<div class="search-page-filters__chips">
|
||||
<?php foreach ($search_type_options as $type_key => $type_label) : ?>
|
||||
<a class="search-page-filters__chip<?php echo $search_type === $type_key ? ' is-active' : ''; ?>" href="<?php echo esc_url(add_query_arg(['aksrt_search_type' => $type_key, 'paged' => false])); ?>">
|
||||
<?php
|
||||
$type_args = [
|
||||
'aksrt_search_type' => $type_key,
|
||||
'paged' => false,
|
||||
];
|
||||
if ($type_key !== 'post') {
|
||||
$type_args['aksrt_category'] = false;
|
||||
} elseif ($search_category_enabled && $current_category > 0) {
|
||||
$type_args['aksrt_category'] = $current_category;
|
||||
}
|
||||
?>
|
||||
<a class="search-page-filters__chip<?php echo $search_type === $type_key ? ' is-active' : ''; ?>" href="<?php echo esc_url(add_query_arg($type_args)); ?>">
|
||||
<?php echo esc_html($type_label); ?>
|
||||
</a>
|
||||
<?php endforeach; ?>
|
||||
@@ -175,16 +203,17 @@ $search_sort_options = [
|
||||
</div>
|
||||
<?php endif; ?>
|
||||
|
||||
<?php if (!empty($filter_settings['categories']) && !empty($search_categories)) : ?>
|
||||
<?php if ($search_category_enabled && !empty($search_categories)) : ?>
|
||||
<div class="search-page-filters__group">
|
||||
<span class="search-page-filters__label"><?php esc_html_e('分类', 'aksrt'); ?></span>
|
||||
<div class="search-page-filters__select-wrap">
|
||||
<select class="search-page-filters__select" id="search-category-filter" aria-label="<?php esc_attr_e('选择分类', 'aksrt'); ?>">
|
||||
<select class="search-page-filters__select" id="search-category-filter" aria-label="<?php esc_attr_e('选择分类', 'aksrt'); ?>"<?php echo $search_type !== 'post' ? ' disabled' : ''; ?>>
|
||||
<option value=""><?php esc_html_e('全部分类', 'aksrt'); ?></option>
|
||||
<?php foreach ($search_categories as $cat) : ?>
|
||||
<option value="<?php echo esc_attr($cat->term_id); ?>"<?php selected($current_category, $cat->term_id); ?>>
|
||||
<?php $cat_count = isset($cat->count) ? (int) $cat->count : 0; ?>
|
||||
<option value="<?php echo esc_attr((string) $cat->term_id); ?>"<?php selected($current_category, (int) $cat->term_id); ?>>
|
||||
<?php echo esc_html($cat->name); ?>
|
||||
<?php if ($cat->count > 0) : ?>(<?php echo esc_html((string) $cat->count); ?>)<?php endif; ?>
|
||||
<?php if ($cat_count > 0) : ?>(<?php echo esc_html((string) $cat_count); ?>)<?php endif; ?>
|
||||
</option>
|
||||
<?php endforeach; ?>
|
||||
</select>
|
||||
@@ -198,15 +227,26 @@ $search_sort_options = [
|
||||
<div class="search-page-filters__chips">
|
||||
<?php foreach ($search_sort_options as $sort_key => $sort_option) : ?>
|
||||
<?php
|
||||
$sort_url = add_query_arg(array_filter([
|
||||
$sort_args = [
|
||||
'orderby' => $sort_option['args']['orderby'],
|
||||
'order' => $sort_option['args']['order'],
|
||||
'paged' => false,
|
||||
], static function ($value): bool {
|
||||
];
|
||||
if ($search_type !== 'post') {
|
||||
$sort_args['aksrt_category'] = false;
|
||||
} elseif ($search_category_enabled && $current_category > 0) {
|
||||
$sort_args['aksrt_category'] = $current_category;
|
||||
}
|
||||
$sort_url = add_query_arg(array_filter($sort_args, static function ($value): bool {
|
||||
return false !== $value;
|
||||
}));
|
||||
if ($sort_key === 'relevance') {
|
||||
$sort_url = remove_query_arg(['orderby', 'order', 'paged']);
|
||||
$sort_url = remove_query_arg(['orderby', 'order', 'paged'], $sort_url);
|
||||
if ($search_type !== 'post') {
|
||||
$sort_url = remove_query_arg('aksrt_category', $sort_url);
|
||||
} elseif ($search_category_enabled && $current_category > 0) {
|
||||
$sort_url = add_query_arg('aksrt_category', $current_category, $sort_url);
|
||||
}
|
||||
}
|
||||
?>
|
||||
<a class="search-page-filters__chip<?php echo $current_sort === $sort_key ? ' is-active' : ''; ?>" href="<?php echo esc_url($sort_url); ?>">
|
||||
|
||||
Reference in New Issue
Block a user