Files
wordpress-theme-aksrt/search.php
T

161 lines
8.7 KiB
PHP

<?php
/**
* legacy-contract search results template.
*
* @package AKSRT
*/
defined('ABSPATH') || exit;
global $wp_query;
$search_query = trim(wp_strip_all_tags(get_search_query(false)));
$search_type = function_exists('aksrt_get_search_form_type') ? aksrt_get_search_form_type() : 'post';
$search_types = function_exists('aksrt_get_search_type_options') ? aksrt_get_search_type_options() : ['post' => __('文章', 'aksrt')];
$search_category = isset($_REQUEST['trem']) ? absint(wp_unslash($_REQUEST['trem'])) : 0;
$search_user = isset($_REQUEST['user']) ? absint(wp_unslash($_REQUEST['user'])) : 0;
$paged = max(1, (int) get_query_var('paged'));
$history_value = $search_query . ($search_type !== '' ? '&type=' . $search_type : '');
if ($search_query !== '' && function_exists('aksrt_save_history_search')) {
aksrt_save_history_search($history_value);
}
$user_query = null;
if ($search_type === 'user' && $search_query !== '') {
$user_page = max(1, isset($_REQUEST['user_paged']) ? absint(wp_unslash($_REQUEST['user_paged'])) : 1);
$orderby = isset($_REQUEST['orderby']) ? sanitize_key(wp_unslash($_REQUEST['orderby'])) : 'name';
$orderby = in_array($orderby, ['name', 'registered'], true) ? $orderby : 'name';
$user_query = new WP_User_Query([
'search' => '*' . $search_query . '*',
'search_columns' => ['user_email', 'user_nicename', 'display_name', 'user_login'],
'count_total' => true,
'number' => 12,
'paged' => $user_page,
'orderby' => $orderby === 'registered' ? 'user_registered' : 'display_name',
'order' => 'DESC',
]);
}
$search_total = $user_query instanceof WP_User_Query ? (int) $user_query->get_total() : (int) $wp_query->found_posts;
if ($search_query !== '' && $search_total > 0 && $paged <= 1 && function_exists('aksrt_update_search_keywords')) {
aksrt_update_search_keywords($history_value);
}
get_header();
if (is_active_sidebar('all_top_fluid') || is_active_sidebar('search_top_fluid')) {
echo '<div class="fluid-widget-wrap">';
if (is_active_sidebar('all_top_fluid')) dynamic_sidebar('all_top_fluid');
if (is_active_sidebar('search_top_fluid')) dynamic_sidebar('search_top_fluid');
echo '</div>';
}
?>
<main class="container">
<div class="content-wrap">
<div class="content-layout">
<?php if (is_active_sidebar('search_top_content')) dynamic_sidebar('search_top_content'); ?>
<?php
$search_args = [
'class' => 'main-search page-search-box',
'show_posts' => false,
'in_cat' => $search_category,
'in_type' => $search_type,
'in_user' => $search_user,
's' => $search_query,
];
echo '<div class="aksrt-panel"' . ($paged < 2 ? ' win-ajax-replace="search"' : '') . '>';
if (function_exists('aksrt_get_main_search')) aksrt_get_main_search($search_args, true);
echo '</div>';
$base_url = add_query_arg(['s' => $search_query, 'user' => $search_user], home_url('/'));
$tab_nav = '';
foreach ($search_types as $key => $label) {
if ($search_type === $key) {
$tab_nav .= '<li class="active"><a class="focus-color" href="javascript:;">' . esc_html($label) . '</a></li>';
} else {
$route_title = $search_query . ' ' . __('搜索', 'aksrt') . $label . ' - ' . get_bloginfo('name');
$tab_nav .= '<li><a ajax-replace="true" route="1" route-title="' . esc_attr($route_title) . '" class="ajax-next" href="' . esc_url(add_query_arg('type', $key, $base_url)) . '">' . esc_html($label) . '</a></li>';
}
}
$tab_nav = '<div class="list-inline scroll-x mini-scrollbar tab-nav-theme">' . $tab_nav . '</div>';
$search_scope_parts = [];
if ($search_user > 0) {
$filtered_user = get_userdata($search_user);
if ($filtered_user) {
$user_home = function_exists('aksrt_get_user_home_url') ? aksrt_get_user_home_url($search_user) : get_author_posts_url($search_user);
$search_scope_parts[] = __('用户', 'aksrt') . ' <a class="focus-color" href="' . esc_url($user_home) . '"><b>' . esc_html($filtered_user->display_name) . '</b></a>';
}
}
if ($search_category > 0) {
$filtered_term = get_term($search_category);
if ($filtered_term && !is_wp_error($filtered_term)) {
$term_url = get_term_link($filtered_term);
if (!is_wp_error($term_url)) {
$taxonomy = get_taxonomy($filtered_term->taxonomy);
$taxonomy_label = $taxonomy && !empty($taxonomy->labels->singular_name) ? $taxonomy->labels->singular_name : __('分类', 'aksrt');
$search_scope_parts[] = esc_html($taxonomy_label) . ' <a class="focus-color" href="' . esc_url($term_url) . '"><b>' . esc_html($filtered_term->name) . '</b></a>';
}
}
}
$description = ($search_scope_parts ? sprintf(__('在%s中,', 'aksrt'), implode(__('、', 'aksrt'), $search_scope_parts)) : '')
. sprintf(
__('搜索 %1$s ,共找到 %2$s 个%3$s', 'aksrt'),
'<b class="search-key focus-color">' . esc_html($search_query) . '</b>',
'<b class="focus-color">' . esc_html(number_format_i18n($search_total)) . '</b>',
esc_html($search_types[$search_type] ?? __('内容', 'aksrt'))
);
?>
<?php if ($search_query === '') : ?>
<div class="aksrt-panel search-content"><?php echo function_exists('aksrt_get_ajax_null') ? aksrt_get_ajax_null(__('请输入搜索关键词', 'aksrt'), 60, 'null-search.svg', 300) : '<div class="ajax-item text-center muted-2-color padding-20">' . esc_html__('请输入搜索关键词', 'aksrt') . '</div>'; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?></div>
<?php else : ?>
<div class="ajaxpager search-page-ajaxpager">
<div class="ajax-item-header type-<?php echo esc_attr($search_type); ?>">
<div class="search-desc-text mb10 mt10 muted-2-color"><?php echo wp_kses_post($description); ?></div>
<div class="search-tab-header aksrt-panel relative padding-h10"><div><?php echo $tab_nav; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?></div><div class="search-tab-filter"><?php echo function_exists('aksrt_get_search_tab_nav_filter') ? aksrt_get_search_tab_nav_filter($search_type) : ''; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?></div></div>
</div>
<?php if ($user_query instanceof WP_User_Query && $user_query->get_results()) : ?>
<?php foreach ($user_query->get_results() as $result_user) :
$user_id = (int) $result_user->ID;
?>
<?php echo function_exists('aksrt_author_minicard') ? aksrt_author_minicard($user_id, 'ajax-item', false, $search_query) : ''; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?>
<?php endforeach; ?>
<?php
if (function_exists('aksrt_get_ajax_next_paginate')) {
$user_next_url = add_query_arg([
's' => $search_query,
'type' => 'user',
'orderby' => $orderby,
'user_paged' => $user_page + 1,
], home_url('/'));
echo aksrt_get_ajax_next_paginate($search_total, $user_page, 12, $user_next_url, 'text-center theme-pagination ajax-pag', 'next-page ajax-next'); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
}
?>
<?php elseif ($search_type === 'post' && $wp_query->have_posts()) : ?>
<?php
$config = function_exists('aksrt_archive_get_render_config') ? aksrt_archive_get_render_config(['context' => 'search', 'highlight_query' => $search_query, 'no_margin' => false]) : ['context' => 'search', 'mode' => 'list'];
aksrt_render_post_list(array_merge($config, ['skip_wrapper' => true]), $wp_query);
?>
<?php aksrt_render_pagination($wp_query, ['query_context' => 'search', 'render_config' => $config]); ?>
<?php else : ?>
<?php echo function_exists('aksrt_get_ajax_null')
? aksrt_get_ajax_null($search_type === 'user' ? __('未找到相关用户', 'aksrt') : __('暂无内容', 'aksrt'), $search_type === 'user' ? 75 : 100, $search_type === 'user' ? 'null-user.svg' : 'null-post.svg')
: '<div class="ajax-item text-center muted-2-color padding-20">' . esc_html($search_type === 'user' ? __('未找到相关用户', 'aksrt') : __('暂无内容', 'aksrt')) . '</div>'; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?>
<?php endif; ?>
<div class="post_ajax_loader search-loader"><?php echo function_exists('aksrt_render_placeholder') ? aksrt_render_placeholder('list', 6) : ''; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- 固定骨架结构。 ?></div>
</div>
<?php endif; ?>
<?php if (is_active_sidebar('search_bottom_content')) dynamic_sidebar('search_bottom_content'); ?>
</div>
</div>
<?php get_sidebar(); ?>
</main>
<?php
if (is_active_sidebar('search_bottom_fluid') || is_active_sidebar('all_bottom_fluid')) {
echo '<div class="fluid-widget-wrap">';
if (is_active_sidebar('search_bottom_fluid')) dynamic_sidebar('search_bottom_fluid');
if (is_active_sidebar('all_bottom_fluid')) dynamic_sidebar('all_bottom_fluid');
echo '</div>';
}
get_footer();