201 lines
10 KiB
PHP
201 lines
10 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
define('ABSPATH', dirname(__DIR__, 2) . DIRECTORY_SEPARATOR);
|
|
define('COOKIEPATH', '/');
|
|
define('COOKIE_DOMAIN', '');
|
|
define('MONTH_IN_SECONDS', 2592000);
|
|
|
|
function __($text): string { return (string) $text; }
|
|
function esc_html($value): string { return htmlspecialchars((string) $value, ENT_QUOTES | ENT_SUBSTITUTE, 'UTF-8'); }
|
|
function esc_attr($value): string { return esc_html($value); }
|
|
function esc_url($value): string { return esc_attr($value); }
|
|
function esc_url_raw($value): string { return (string) $value; }
|
|
function esc_html__($text): string { return esc_html($text); }
|
|
function esc_attr__($text): string { return esc_attr($text); }
|
|
function wp_strip_all_tags($value): string { return strip_tags((string) $value); }
|
|
function wp_parse_args($args, $defaults = []): array { return array_merge($defaults, is_array($args) ? $args : []); }
|
|
function sanitize_html_class($class, $fallback = ''): string {
|
|
$value = preg_replace('/[^A-Za-z0-9_-]/', '', (string) $class) ?? '';
|
|
return $value !== '' ? $value : (string) $fallback;
|
|
}
|
|
function sanitize_key($value): string { return preg_replace('/[^a-z0-9_-]/', '', strtolower((string) $value)) ?? ''; }
|
|
function wp_unslash($value) { return $value; }
|
|
function absint($value): int { return abs((int) $value); }
|
|
function is_search(): bool { return true; }
|
|
function get_search_query(bool $escaped = true): string { return $escaped ? esc_attr($GLOBALS['search_text']) : $GLOBALS['search_text']; }
|
|
function get_query_var($key) { return 1; }
|
|
function home_url($path = '/'): string { return 'https://example.test' . $path; }
|
|
function add_query_arg($key, $value = null, $url = ''): string {
|
|
if (is_array($key)) {
|
|
$args = $key;
|
|
$url = (string) $value;
|
|
} else {
|
|
$args = [(string) $key => $value];
|
|
$url = (string) $url;
|
|
}
|
|
return rtrim($url, '/') . '/?' . http_build_query($args);
|
|
}
|
|
function get_bloginfo($key): string { return $key === 'name' ? 'AKSRT' : ''; }
|
|
function number_format_i18n($number): string { return number_format((float) $number); }
|
|
function wp_kses_post($html): string { return strip_tags((string) $html, '<a><b><span><i><div><p><ul><li>'); }
|
|
function is_wp_error($value): bool { return false; }
|
|
function is_active_sidebar($id): bool { return false; }
|
|
function dynamic_sidebar($id): void {}
|
|
function get_header(): void {}
|
|
function get_sidebar(): void {}
|
|
function get_footer(): void {}
|
|
function aksrt_save_history_search(string $value): void {}
|
|
function aksrt_update_search_keywords(string $value): void {}
|
|
function aksrt_search_history_enabled(): bool { return false; }
|
|
function aksrt_get_search_hot_keywords(): array { return []; }
|
|
function aksrt_get_search_type_options(): array { return ['post' => '文章', 'user' => '用户']; }
|
|
function aksrt_get_search_form_type(): string { return (string) ($_REQUEST['type'] ?? 'post'); }
|
|
function aksrt_get_search_form_state(): array {
|
|
return ['type' => aksrt_get_search_form_type(), 'types' => aksrt_get_search_type_options(), 'category_id' => 0, 'category_enabled' => false, 'categories' => []];
|
|
}
|
|
|
|
class WP_Term {
|
|
public function __construct(public int $term_id, public string $name, public string $taxonomy = 'category') {}
|
|
}
|
|
function aksrt_get_search_categories(?array $ids = null): array {
|
|
return array_map(static fn($id) => new WP_Term((int) $id, (int) $id === 7 ? 'Tech' : 'News'), $ids ?? []);
|
|
}
|
|
function get_term($id) { return new WP_Term((int) $id, 'Tech'); }
|
|
function get_term_link($term): string { return 'https://example.test/category/' . $term->term_id; }
|
|
function get_taxonomy($taxonomy) { return (object) ['labels' => (object) ['singular_name' => '分类']]; }
|
|
|
|
class WP_Query {
|
|
public int $found_posts = 0;
|
|
public array $posts = [];
|
|
public function have_posts(): bool { return (bool) $this->posts; }
|
|
public function the_post(): void {}
|
|
}
|
|
class WP_User_Query {
|
|
public function __construct(private array $args) { $GLOBALS['last_user_query_args'] = $args; }
|
|
public function get_results(): array { return $GLOBALS['users']; }
|
|
public function get_total(): int { return $GLOBALS['user_total']; }
|
|
}
|
|
function get_userdata($id) { return $GLOBALS['user_data'][(int) $id] ?? false; }
|
|
function aksrt_get_user_home_url($id): string { return 'https://example.test/user/' . (int) $id; }
|
|
function get_author_posts_url($id): string { return aksrt_get_user_home_url($id); }
|
|
function aksrt_get_avatar_url($id, $args = []): string { return 'https://example.test/avatar/' . (int) $id . '.png'; }
|
|
function aksrt_get_lazy_attr($mode, $url, $class = '', $placeholder = ''): string {
|
|
return ' class="' . esc_attr($class) . '" src="' . esc_attr($url) . '"';
|
|
}
|
|
function aksrt_get_lazy_thumb($size): string { return ''; }
|
|
function aksrt_get_user_name($args): string {
|
|
$user = get_userdata((int) $args['id']);
|
|
$name = function_exists('aksrt_renderer_highlight_text')
|
|
? aksrt_renderer_highlight_text((string) $user->display_name, (string) ($args['highlight_query'] ?? ''))
|
|
: esc_html($user->display_name);
|
|
return '<name class="flex ac flex1"><a class="display-name" href="' . esc_url(aksrt_get_user_home_url($user->ID)) . '">' . $name . '</a></name>';
|
|
}
|
|
function aksrt_get_user_follow($class, $id): string { return '<a class="follow flex0" data-follow="' . (int) $id . '">Follow</a>'; }
|
|
function aksrt_get_user_description($id): string { return (string) (get_userdata($id)->description ?? ''); }
|
|
function get_user_meta($id, $key, $single = true): string { return ''; }
|
|
function aksrt_get_ajax_null(?string $text = null, $margin = 60, string $img = 'null.svg', int $width = 280): string {
|
|
return '<div class="ajax-item aksrt-ajax-null" data-img="' . esc_attr($img) . '" data-width="' . $width . '">' . esc_html((string) $text) . '</div>';
|
|
}
|
|
function aksrt_get_null($text = null, $margin = 60, string $img = 'null.svg', string $class = '', int $width = 280): string { return ''; }
|
|
function aksrt_get_search_tab_nav_filter(string $type): string { return ''; }
|
|
function aksrt_archive_get_render_config(array $args): array { return $args + ['mode' => 'list']; }
|
|
function aksrt_render_post_list(array $args, WP_Query $query): void {
|
|
$title = $query->posts[0]->post_title ?? '';
|
|
echo '<div class="posts-item list ajax-item">' . aksrt_renderer_highlight_text((string) $title, (string) ($args['highlight_query'] ?? '')) . '</div>';
|
|
}
|
|
function aksrt_render_pagination($query, array $args): void {}
|
|
function aksrt_render_placeholder($kind, $count): string { return ''; }
|
|
|
|
require ABSPATH . 'inc/helpers/posts-renderer/config.php';
|
|
require ABSPATH . 'inc/helpers/aksrt-graphic-card.php';
|
|
require ABSPATH . 'inc/helpers/aksrt-ajax-list.php';
|
|
require ABSPATH . 'inc/helpers/search/aksrt-search-box.php';
|
|
|
|
function render_search_case(string $query, string $type, array $users = [], int $user_total = 0, array $posts = []): string {
|
|
$GLOBALS['search_text'] = $query;
|
|
$GLOBALS['users'] = $users;
|
|
$GLOBALS['user_total'] = $user_total;
|
|
$GLOBALS['last_user_query_args'] = [];
|
|
$GLOBALS['user_data'] = [];
|
|
foreach ($users as $user) {
|
|
$GLOBALS['user_data'][$user->ID] = $user;
|
|
}
|
|
$_REQUEST = ['type' => $type];
|
|
$_GET = $_REQUEST;
|
|
$GLOBALS['wp_query'] = new WP_Query();
|
|
$GLOBALS['wp_query']->found_posts = count($posts);
|
|
$GLOBALS['wp_query']->posts = $posts;
|
|
ob_start();
|
|
include ABSPATH . 'search.php';
|
|
return (string) ob_get_clean();
|
|
}
|
|
|
|
$empty = render_search_case('', 'post');
|
|
if (!str_contains($empty, 'null-search.svg') || !str_contains($empty, 'data-width="300"') || str_contains($empty, 'search-page-ajaxpager')) {
|
|
throw new RuntimeException('Empty search markup does not match the reference state.');
|
|
}
|
|
|
|
$query = 'A&B "Z"';
|
|
$post_html = render_search_case($query, 'post', [], 0, [(object) ['post_title' => $query]]);
|
|
if (!str_contains($post_html, 'posts-item list ajax-item') || !str_contains($post_html, 'A&B') || !str_contains($post_html, '"Z"')) {
|
|
throw new RuntimeException('Post result markup or escaping failed.');
|
|
}
|
|
|
|
$user = (object) ['ID' => 5, 'display_name' => $query, 'description' => 'About ' . $query];
|
|
$user_html = render_search_case($query, 'user', [$user], 25);
|
|
if (!str_contains($user_html, 'author-minicard') || !str_contains($user_html, 'follow flex0') || !str_contains($user_html, 'user_paged=2') || !str_contains($user_html, 'type=user') || !str_contains($user_html, 's=A%26B') || !str_contains($user_html, 'orderby=name')) {
|
|
throw new RuntimeException('User card or AJAX pagination markup failed.');
|
|
}
|
|
if (($GLOBALS['last_user_query_args']['orderby'] ?? '') !== 'display_name') {
|
|
throw new RuntimeException('User name ordering is not mapped to WP_User_Query.');
|
|
}
|
|
|
|
$highlight = aksrt_renderer_highlight_text('<b>A&B</b> "Z" <script>x</script>', $query);
|
|
$plain = html_entity_decode(strip_tags($highlight), ENT_QUOTES | ENT_HTML5, 'UTF-8');
|
|
if ($plain !== 'A&B "Z" x' || !str_contains($highlight, '<b class="focus-color">')) {
|
|
throw new RuntimeException('Keyword highlighting does not preserve safe text.');
|
|
}
|
|
|
|
$form_with_category = aksrt_get_search_box([
|
|
'class' => 'main-search page-search-box',
|
|
's' => '',
|
|
'show_keywords' => false,
|
|
'show_history' => false,
|
|
'show_posts' => false,
|
|
'show_input_cat' => true,
|
|
'more_cats' => [7],
|
|
'show_type' => true,
|
|
'in_type' => 'post',
|
|
], false);
|
|
$form_without_category = aksrt_get_search_box([
|
|
'class' => 'main-search page-search-box',
|
|
's' => '',
|
|
'show_keywords' => false,
|
|
'show_history' => false,
|
|
'show_posts' => false,
|
|
'show_input_cat' => true,
|
|
'more_cats' => [],
|
|
'show_type' => true,
|
|
'in_type' => 'post',
|
|
], false);
|
|
if (!str_contains((string) $form_with_category, 'main-search page-search-box') || !str_contains((string) $form_with_category, 'Tech') || !str_contains((string) $form_with_category, 'search-type-but-average')) {
|
|
throw new RuntimeException('WP_Term category selector or multi-class search wrapper failed.');
|
|
}
|
|
if (!str_contains((string) $form_without_category, 'name="type"') || !str_contains((string) $form_without_category, 'dropdown-menu')) {
|
|
throw new RuntimeException('Search type dropdown is missing when no category picker is available.');
|
|
}
|
|
if (!str_contains((string) $form_with_category, 'class="line-form-input" tabindex="1" value=""')) {
|
|
throw new RuntimeException('The header form must preserve its own empty query on a search results page.');
|
|
}
|
|
|
|
foreach (['post' => 'null-post.svg', 'user' => 'null-user.svg'] as $type => $image) {
|
|
$no_results = render_search_case('No matches', $type);
|
|
if (!str_contains($no_results, 'class="ajax-item aksrt-ajax-null"') || !str_contains($no_results, $image)) {
|
|
throw new RuntimeException('Empty results must remain an AJAX replaceable illustrated item.');
|
|
}
|
|
}
|
|
|
|
echo "Search render harness passed: empty, post, user, escaping, WP_Term category and no-category type dropdown.\n";
|