Files
wordpress-theme-aksrt/search.php
T

137 lines
6.9 KiB
PHP

<?php
/**
* Zibll-contract search results template.
*
* @package AKSRT
*/
defined('ABSPATH') || exit;
global $wp_query;
$search_query = trim(wp_strip_all_tags(get_search_query()));
$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('search_top_fluid')) {
echo '<div class="fluid-widget-wrap">';
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="zib-widget"' . ($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 {
$tab_nav .= '<li><a ajax-replace="true" route="1" 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>';
$description = 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'))
);
?>
<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 zib-widget 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 ($search_query === '') : ?>
<div class="zib-widget search-content"><div class="text-center muted-2-color padding-20"><?php esc_html_e('请输入搜索关键词', 'aksrt'); ?></div></div>
<?php elseif ($user_query instanceof WP_User_Query && $user_query->get_results()) : ?>
<div class="zib-widget">
<?php foreach ($user_query->get_results() as $result_user) :
$user_id = (int) $result_user->ID;
$user_url = function_exists('aksrt_get_user_home_url') ? aksrt_get_user_home_url($user_id) : get_author_posts_url($user_id);
?>
<a class="author-minicard radius8 flex ac ajax-item" href="<?php echo esc_url($user_url); ?>">
<span class="avatar-img mr10"><?php echo function_exists('aksrt_get_avatar_html') ? aksrt_get_avatar_html($user_id, 48) : get_avatar($user_id, 48); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?></span>
<span class="flex1"><b><?php echo esc_html($result_user->display_name); ?></b><span class="muted-2-color em09 block text-ellipsis"><?php echo esc_html(wp_trim_words((string) $result_user->description, 16)); ?></span></span>
</a>
<?php endforeach; ?>
</div>
<?php
$user_pages = (int) ceil($search_total / 12);
if ($user_pages > 1) {
echo '<div class="text-center theme-pagination ajax-pag">' . paginate_links(['base' => esc_url_raw(add_query_arg('user_paged', '%#%')), 'current' => $user_page, 'total' => $user_pages, 'prev_text' => '<i class="fa fas fa-angle-left"></i>', 'next_text' => '<i class="fa fas fa-angle-right"></i>']) . '</div>';
}
?>
<?php elseif ($search_type === 'post' && $wp_query->have_posts()) : ?>
<div class="posts-row mtn6">
<?php
$config = function_exists('aksrt_archive_get_render_config') ? aksrt_archive_get_render_config(['context' => 'search', 'highlight_query' => $search_query]) : ['context' => 'search', 'mode' => 'list'];
aksrt_render_post_list(array_merge($config, ['wrapper_class' => '']), $wp_query);
?>
</div>
<?php aksrt_render_pagination($wp_query, ['query_context' => 'search', 'render_config' => $config]); ?>
<?php else : ?>
<div class="zib-widget search-content"><div class="text-center muted-2-color padding-20"><?php esc_html_e('未找到相关内容', 'aksrt'); ?></div></div>
<?php endif; ?>
<div class="post_ajax_loader search-loader" style="display:none"><div class="mb20"><div class="text-center muted-2-color mt20"><i class="loading mr10"></i><?php esc_html_e('加载中...', 'aksrt'); ?></div></div></div>
</div>
<?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')) {
echo '<div class="fluid-widget-wrap">';
dynamic_sidebar('search_bottom_fluid');
echo '</div>';
}
get_footer();