feat(navigation): hard-switch to Zibll navigation and Font Awesome 5
This commit is contained in:
1 parent
3c3ddac8be
commit
a0b84977a2
630 files changed
+101647
-11293
No files matched your search
+168
-81
@@ -14,31 +14,22 @@ class ThemeSearch {
|
||||
return $placeholder !== '' ? $placeholder : __('开启精彩搜索', 'aksrt');
|
||||
}
|
||||
|
||||
public static function search_mode(): string {
|
||||
$mode = sanitize_key((string) Settings::get_value('search_mode', ''));
|
||||
if ($mode === '' || !in_array($mode, ['default', 'ajax', 'redirect'], true)) {
|
||||
$mode = sanitize_key((string) Settings::get_value('search_type', 'default'));
|
||||
}
|
||||
return in_array($mode, ['default', 'ajax', 'redirect'], true) ? $mode : 'default';
|
||||
}
|
||||
|
||||
public static function search_history_enabled(): bool {
|
||||
$settings = Settings::get_all();
|
||||
return !array_key_exists('search_history_enabled', $settings) || !empty($settings['search_history_enabled']);
|
||||
return !array_key_exists('search_history', $settings) || !empty($settings['search_history']);
|
||||
}
|
||||
|
||||
public static function hot_search_enabled(): bool {
|
||||
$settings = Settings::get_all();
|
||||
return !array_key_exists('hot_search_enabled', $settings) || !empty($settings['hot_search_enabled']);
|
||||
return !array_key_exists('search_popular_key', $settings) || !empty($settings['search_popular_key']);
|
||||
}
|
||||
|
||||
public static function hot_search_keywords(): array {
|
||||
$raw = Settings::get_value('hot_search_keywords', '');
|
||||
if (is_array($raw)) {
|
||||
$items = $raw;
|
||||
} else {
|
||||
$items = preg_split('/[\r\n,]+/', (string) $raw) ?: [];
|
||||
}
|
||||
$items = self::search_popular_sticky();
|
||||
$counts = get_option('search_keywords', []);
|
||||
if (is_array($counts)) {
|
||||
$items = array_merge($items, array_keys($counts));
|
||||
}
|
||||
|
||||
$items = array_map(
|
||||
static function ($keyword): string {
|
||||
@@ -66,16 +57,12 @@ class ThemeSearch {
|
||||
if (is_array($raw)) {
|
||||
$items = $raw;
|
||||
} else {
|
||||
$items = preg_split('/[\r\n,]+/', (string) $raw) ?: [];
|
||||
$items = preg_split('/[\r\n,,]+/', (string) $raw) ?: [];
|
||||
}
|
||||
$items = array_map(static fn($k) => trim(sanitize_text_field((string) $k)), $items);
|
||||
return array_values(array_filter(array_unique($items), fn(string $k) => $k !== ''));
|
||||
}
|
||||
|
||||
public static function search_suggestions_enabled(): bool {
|
||||
return self::search_mode() === 'ajax' && !empty(Settings::get_value('search_suggestions_enabled', false));
|
||||
}
|
||||
|
||||
public static function search_posts_enabled(): bool {
|
||||
return !empty(Settings::get_value('search_posts', false));
|
||||
}
|
||||
@@ -103,8 +90,10 @@ class ThemeSearch {
|
||||
return trim((string) Settings::get_value('search_cat_in', ''));
|
||||
}
|
||||
|
||||
public static function search_more_cat_obj(): bool {
|
||||
return !empty(Settings::get_value('search_more_cat_obj', true));
|
||||
public static function search_more_cat_obj(): array {
|
||||
$raw = Settings::get_value('search_more_cat_obj', []);
|
||||
$items = is_array($raw) ? $raw : (preg_split('/[,,\s]+/', (string) $raw) ?: []);
|
||||
return array_values(array_filter(array_unique(array_map('absint', $items))));
|
||||
}
|
||||
|
||||
public static function search_custom_key_enabled(): bool {
|
||||
@@ -112,42 +101,47 @@ class ThemeSearch {
|
||||
}
|
||||
|
||||
public static function search_custom_key(): array {
|
||||
$raw = Settings::get_value('search_custom_key', 'title,content');
|
||||
$raw = Settings::get_value('search_custom_key', ['title', 'content', 'excerpt', 'post_tag', 'forum_topic', 'shop_cat', 'shop_discount']);
|
||||
if (is_array($raw)) {
|
||||
$items = $raw;
|
||||
} else {
|
||||
$items = preg_split('/[,,\s]+/', (string) $raw) ?: [];
|
||||
}
|
||||
$items = array_map(static fn($k) => trim(sanitize_key((string) $k)), $items);
|
||||
$valid = ['title', 'content', 'excerpt', 'category', 'tag', 'author'];
|
||||
$valid = ['title', 'content', 'excerpt', 'category', 'post_tag', 'topics', 'coment', 'user', 'forum_topic', 'forum_tag', 'shop_cat', 'shop_tag', 'shop_discount'];
|
||||
return array_values(array_intersect(array_filter(array_unique($items)), $valid));
|
||||
}
|
||||
|
||||
public static function search_facets_enabled(): bool {
|
||||
return !empty(Settings::get_value('search_facets', false));
|
||||
return !empty(Settings::get_value('search_facets', []));
|
||||
}
|
||||
|
||||
public static function search_facets_order(): array {
|
||||
$raw = Settings::get_value('search_facets_order', "category\ntag");
|
||||
$items = is_array($raw) ? $raw : (preg_split('/[\r\n]+/', (string) $raw) ?: []);
|
||||
$items = array_map(static fn($s) => trim(sanitize_key((string) $s)), $items);
|
||||
$raw = Settings::get_value('search_facets', []);
|
||||
$items = is_array($raw) ? array_keys($raw) : [];
|
||||
$items = array_map(static fn($s) => trim(sanitize_key((string) $s)), $items);
|
||||
return array_values(array_filter(array_unique($items), fn(string $s) => $s !== ''));
|
||||
}
|
||||
|
||||
public static function get_meilisearch_config(): array {
|
||||
$s = Settings::get_all();
|
||||
$s = Settings::get_all();
|
||||
$opt = is_array($s['search_ms_opt'] ?? null) ? $s['search_ms_opt'] : [];
|
||||
|
||||
$enabled = !empty($s['search_ms_enabled']);
|
||||
$api_url = esc_url_raw(trim((string) ($s['search_ms_api_url'] ?? '')));
|
||||
$api_key = sanitize_text_field((string) ($s['search_ms_api_key'] ?? ''));
|
||||
$index_prefix = sanitize_key((string) ($s['search_ms_index_prefix'] ?? 'aksrt'));
|
||||
$synonyms_raw = trim((string) ($s['search_ms_synonyms'] ?? ''));
|
||||
$synonyms = [];
|
||||
if ($synonyms_raw !== '') {
|
||||
$decoded = json_decode($synonyms_raw, true);
|
||||
if (is_array($decoded)) {
|
||||
$synonyms = $decoded;
|
||||
}
|
||||
$enabled = !empty($s['search_ms_s']);
|
||||
$api_url = esc_url_raw(trim((string) ($opt['api_url'] ?? 'http://127.0.0.1:7700')));
|
||||
$api_key = sanitize_text_field((string) ($opt['api_key'] ?? ''));
|
||||
$index_prefix = sanitize_key((string) ($opt['index_prefix'] ?? 'aksrt'));
|
||||
$synonyms_raw = $opt['synonyms'] ?? [];
|
||||
$synonyms = [];
|
||||
foreach ((array) $synonyms_raw as $row) {
|
||||
if (!is_array($row)) {
|
||||
continue;
|
||||
}
|
||||
$key = trim(sanitize_text_field((string) ($row['key'] ?? '')));
|
||||
$values = array_values(array_filter(array_map('trim', explode(',', sanitize_text_field((string) ($row['val'] ?? ''))))));
|
||||
if ($key !== '' && $values) {
|
||||
$synonyms[$key] = $values;
|
||||
}
|
||||
}
|
||||
|
||||
return [
|
||||
@@ -156,15 +150,96 @@ class ThemeSearch {
|
||||
'api_key' => $api_key,
|
||||
'index_prefix' => $index_prefix !== '' ? $index_prefix : 'aksrt',
|
||||
'synonyms' => $synonyms,
|
||||
'index_name' => ($index_prefix !== '' ? $index_prefix : 'aksrt') . '_posts',
|
||||
];
|
||||
}
|
||||
'index_name' => ($index_prefix !== '' ? $index_prefix : 'aksrt') . '_posts',
|
||||
];
|
||||
}
|
||||
|
||||
public static function filter_posts_search(string $search, \WP_Query $query): string {
|
||||
if (!$query->is_search() || !$query->get('s') || !self::search_custom_key_enabled()) {
|
||||
return $search;
|
||||
}
|
||||
if (sanitize_key((string) $query->get('type')) === 'user') {
|
||||
return ' AND 1=0';
|
||||
}
|
||||
$config = self::get_meilisearch_config();
|
||||
if (!empty($config['enabled'])) {
|
||||
return $search;
|
||||
}
|
||||
$keys = self::search_custom_key();
|
||||
if (!$keys) {
|
||||
return $search;
|
||||
}
|
||||
global $wpdb;
|
||||
$terms = (array) ($query->query_vars['search_terms'] ?? []);
|
||||
if (!$terms) {
|
||||
$terms = [(string) $query->get('s')];
|
||||
}
|
||||
$groups = [];
|
||||
$taxonomies = ['post_tag', 'category', 'topics', 'forum_tag', 'forum_topic', 'shop_tag', 'shop_cat', 'shop_discount'];
|
||||
foreach ($terms as $term) {
|
||||
$like = (!empty($query->query_vars['exact']) ? '' : '%') . $wpdb->esc_like((string) $term) . (!empty($query->query_vars['exact']) ? '' : '%');
|
||||
$parts = [];
|
||||
foreach ($keys as $key) {
|
||||
if ($key === 'title') $parts[] = $wpdb->prepare("{$wpdb->posts}.post_title LIKE %s", $like);
|
||||
elseif ($key === 'content') $parts[] = $wpdb->prepare("{$wpdb->posts}.post_content LIKE %s", $like);
|
||||
elseif ($key === 'excerpt') $parts[] = $wpdb->prepare("{$wpdb->posts}.post_excerpt LIKE %s", $like);
|
||||
elseif (in_array($key, $taxonomies, true)) $parts[] = $wpdb->prepare('(aksrt_search_tt.taxonomy = %s AND aksrt_search_terms.name LIKE %s)', $key, $like);
|
||||
elseif ($key === 'coment') $parts[] = $wpdb->prepare('aksrt_search_comments.comment_content LIKE %s', $like);
|
||||
elseif ($key === 'user') $parts[] = $wpdb->prepare('(aksrt_search_users.user_nicename LIKE %s OR aksrt_search_users.display_name LIKE %s)', $like, $like);
|
||||
}
|
||||
if ($parts) {
|
||||
$groups[] = '(' . implode(' OR ', $parts) . ')';
|
||||
}
|
||||
}
|
||||
if (!$groups) {
|
||||
return $search;
|
||||
}
|
||||
if (!is_user_logged_in()) {
|
||||
$groups[] = "({$wpdb->posts}.post_password = '')";
|
||||
}
|
||||
return ' AND ' . implode(' AND ', $groups);
|
||||
}
|
||||
|
||||
public static function filter_posts_search_join(string $join, \WP_Query $query): string {
|
||||
if (!$query->is_search() || !$query->get('s') || !self::search_custom_key_enabled() || !empty(self::get_meilisearch_config()['enabled'])) {
|
||||
return $join;
|
||||
}
|
||||
global $wpdb;
|
||||
$keys = self::search_custom_key();
|
||||
if (in_array('coment', $keys, true)) {
|
||||
$join .= " LEFT JOIN {$wpdb->comments} AS aksrt_search_comments ON ({$wpdb->posts}.ID = aksrt_search_comments.comment_post_ID)";
|
||||
}
|
||||
if (in_array('user', $keys, true)) {
|
||||
$join .= " LEFT JOIN {$wpdb->users} AS aksrt_search_users ON ({$wpdb->posts}.post_author = aksrt_search_users.ID)";
|
||||
}
|
||||
if (array_intersect(['post_tag', 'category', 'topics', 'forum_tag', 'forum_topic', 'shop_tag', 'shop_cat', 'shop_discount'], $keys)) {
|
||||
$join .= " LEFT JOIN {$wpdb->term_relationships} AS aksrt_search_tr ON ({$wpdb->posts}.ID = aksrt_search_tr.object_id)";
|
||||
$join .= " LEFT JOIN {$wpdb->term_taxonomy} AS aksrt_search_tt ON (aksrt_search_tr.term_taxonomy_id = aksrt_search_tt.term_taxonomy_id)";
|
||||
$join .= " LEFT JOIN {$wpdb->terms} AS aksrt_search_terms ON (aksrt_search_tt.term_id = aksrt_search_terms.term_id)";
|
||||
}
|
||||
return $join;
|
||||
}
|
||||
|
||||
public static function filter_posts_search_distinct(string $distinct, \WP_Query $query): string {
|
||||
return $query->is_search() && $query->get('s') && self::search_custom_key_enabled() ? 'DISTINCT' : $distinct;
|
||||
}
|
||||
|
||||
public static function filter_search_query($query): void {
|
||||
if (is_admin() || !$query->is_main_query() || !$query->is_search()) {
|
||||
return;
|
||||
}
|
||||
|
||||
$type = isset($_REQUEST['type']) ? sanitize_key((string) wp_unslash($_REQUEST['type'])) : '';
|
||||
if (!in_array($type, ['post', 'user'], true)) {
|
||||
$type = self::get_search_content_types()['default'];
|
||||
}
|
||||
$query->set('type', $type);
|
||||
|
||||
if ($type === 'user') {
|
||||
$query->set('post__in', [0]);
|
||||
return;
|
||||
}
|
||||
|
||||
if (self::search_no_page()) {
|
||||
$existing = $query->get('post_type');
|
||||
if (empty($existing)) {
|
||||
@@ -174,17 +249,52 @@ class ThemeSearch {
|
||||
|
||||
$exclude_cats = self::search_exclude_cats();
|
||||
if (!empty($exclude_cats)) {
|
||||
$cat_query = $query->get('cat');
|
||||
$negatives = array_map(fn(int $id) => -$id, $exclude_cats);
|
||||
if (!empty($cat_query)) {
|
||||
$existing = is_array($cat_query) ? $cat_query : explode(',', (string) $cat_query);
|
||||
$existing = array_map('intval', $existing);
|
||||
$merged = array_merge($existing, $negatives);
|
||||
$query->set('cat', implode(',', array_unique($merged)));
|
||||
} else {
|
||||
$query->set('cat', implode(',', $negatives));
|
||||
foreach ($exclude_cats as $category_id) {
|
||||
$children = get_term_children($category_id, 'category');
|
||||
if (!is_wp_error($children)) {
|
||||
$exclude_cats = array_merge($exclude_cats, $children);
|
||||
}
|
||||
}
|
||||
$query->set('category__not_in', array_values(array_unique(array_map('absint', $exclude_cats))));
|
||||
}
|
||||
|
||||
$term_id = isset($_REQUEST['trem']) ? absint(wp_unslash($_REQUEST['trem'])) : 0;
|
||||
if ($term_id > 0) {
|
||||
$term = get_term($term_id);
|
||||
if ($term instanceof \WP_Term) {
|
||||
$tax_query = (array) $query->get('tax_query');
|
||||
$tax_query[] = [
|
||||
'taxonomy' => $term->taxonomy,
|
||||
'field' => 'term_id',
|
||||
'terms' => $term->term_id,
|
||||
];
|
||||
$query->set('tax_query', $tax_query);
|
||||
}
|
||||
}
|
||||
|
||||
$user_id = isset($_REQUEST['user']) ? absint(wp_unslash($_REQUEST['user'])) : 0;
|
||||
if ($user_id > 0) {
|
||||
$query->set('author', $user_id);
|
||||
}
|
||||
|
||||
$orderby = isset($_REQUEST['orderby']) ? sanitize_key((string) wp_unslash($_REQUEST['orderby'])) : '';
|
||||
$meta_keys = [
|
||||
'views' => 'aksrt_post_views',
|
||||
'like' => '_aksrt_like_count',
|
||||
'favorite' => '_aksrt_favorite_count',
|
||||
'sales_volume' => '_aksrt_sales_volume',
|
||||
];
|
||||
if (isset($meta_keys[$orderby])) {
|
||||
$query->set('meta_key', $meta_keys[$orderby]);
|
||||
$query->set('orderby', ['meta_value_num' => 'DESC', 'date' => 'DESC']);
|
||||
$query->set('order', 'DESC');
|
||||
} elseif ($orderby === 'comment_count') {
|
||||
$query->set('orderby', 'comment_count');
|
||||
$query->set('order', 'DESC');
|
||||
} elseif ($orderby === 'date') {
|
||||
$query->set('orderby', 'date');
|
||||
$query->set('order', 'DESC');
|
||||
}
|
||||
}
|
||||
|
||||
public static function apply_list_orderby($query): void {
|
||||
@@ -221,37 +331,14 @@ class ThemeSearch {
|
||||
}
|
||||
|
||||
public static function get_search_content_types(): array {
|
||||
$all_types = [
|
||||
'post' => 'search_type_post',
|
||||
'user' => 'search_type_user',
|
||||
'category' => 'search_type_category',
|
||||
'tag' => 'search_type_tag',
|
||||
];
|
||||
|
||||
$enabled = [];
|
||||
$all = Settings::get_all();
|
||||
foreach ($all_types as $type => $key) {
|
||||
$default_on = in_array($type, ['post', 'category', 'tag'], true);
|
||||
$is_enabled = array_key_exists($key, $all)
|
||||
? !empty(Settings::get_value($key, $default_on))
|
||||
: $default_on;
|
||||
|
||||
if ($is_enabled) {
|
||||
$enabled[] = $type;
|
||||
}
|
||||
}
|
||||
|
||||
if (empty($enabled)) {
|
||||
$enabled = ['post'];
|
||||
}
|
||||
|
||||
$default = sanitize_key((string) Settings::get_value('search_default_type', 'post'));
|
||||
if (!in_array($default, $enabled, true)) {
|
||||
$default = $enabled[0];
|
||||
$types = ['post', 'user'];
|
||||
$default = sanitize_key((string) Settings::get_value('search_type_in', 'post'));
|
||||
if (!in_array($default, $types, true)) {
|
||||
$default = 'post';
|
||||
}
|
||||
|
||||
return [
|
||||
'types' => $enabled,
|
||||
'types' => $types,
|
||||
'default' => $default,
|
||||
];
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user