fix(frontend): align search and header components with reference
This commit is contained in:
1 parent
4c13ef478c
commit
542daca457
34 files changed
+803
-705
No files matched your search
@@ -0,0 +1,126 @@
|
||||
<?php
|
||||
/** Render-level harness for the header multifunction component. */
|
||||
|
||||
namespace AKSRT\Core {
|
||||
final class Color {
|
||||
public static function sanitize(string $value): string { return $value; }
|
||||
}
|
||||
}
|
||||
|
||||
namespace AKSRT\Settings {
|
||||
final class Settings {
|
||||
public static array $headerSlider = [];
|
||||
public static function get_header_slider_settings(): array { return self::$headerSlider; }
|
||||
}
|
||||
}
|
||||
|
||||
namespace {
|
||||
define('ABSPATH', __DIR__);
|
||||
$GLOBALS['aksrt_mobile'] = false;
|
||||
$GLOBALS['aksrt_lazy'] = true;
|
||||
|
||||
final class WP_Query {
|
||||
public function get(string $key) { return 0; }
|
||||
}
|
||||
$GLOBALS['wp_query'] = new WP_Query();
|
||||
|
||||
function is_admin(): bool { return false; }
|
||||
function wp_is_mobile(): bool { return !empty($GLOBALS['aksrt_mobile']); }
|
||||
function is_author(): bool { return false; }
|
||||
function is_front_page(): bool { return true; }
|
||||
function is_category(): bool { return false; }
|
||||
function is_tag(): bool { return false; }
|
||||
function is_singular($type = ''): bool { return false; }
|
||||
function is_search(): bool { return false; }
|
||||
function is_page(): bool { return false; }
|
||||
function is_home(): bool { return false; }
|
||||
function get_query_var(string $key) { return ''; }
|
||||
function sanitize_key($value): string { return preg_replace('/[^a-z0-9_-]/', '', strtolower((string) $value)); }
|
||||
function sanitize_html_class($value): string { return preg_replace('/[^a-zA-Z0-9_-]/', '', (string) $value); }
|
||||
function wp_parse_args($args, array $defaults = []): array { return array_merge($defaults, is_array($args) ? $args : []); }
|
||||
function esc_url($value): string { return htmlspecialchars((string) $value, ENT_QUOTES); }
|
||||
function esc_url_raw($value): string { return (string) $value; }
|
||||
function esc_attr($value): string { return htmlspecialchars((string) $value, ENT_QUOTES); }
|
||||
function esc_html($value): string { return htmlspecialchars((string) $value, ENT_QUOTES); }
|
||||
function wp_strip_all_tags($value): string { return strip_tags((string) $value); }
|
||||
function __($value, $domain = ''): string { return (string) $value; }
|
||||
function get_bloginfo($show = ''): string { return 'AKSRT'; }
|
||||
function home_url($path = ''): string { return 'https://example.test/' . ltrim((string) $path, '/'); }
|
||||
function wp_kses($value, $allowed = []): string { return (string) $value; }
|
||||
function aksrt_is_lazy(string $key): bool { return !empty($GLOBALS['aksrt_lazy']); }
|
||||
function aksrt_get_lazy_thumb(string $size = ''): string { return 'https://example.test/placeholder.svg'; }
|
||||
function aksrt_get_lazy_attr(string $key, string $src, string $class = '', string $lazy_src = ''): string {
|
||||
if (aksrt_is_lazy($key)) {
|
||||
return ' class="lazyload ' . esc_attr($class) . '" src="' . esc_url($lazy_src) . '" data-src="' . esc_url($src) . '"';
|
||||
}
|
||||
return ' class="' . esc_attr($class) . '" src="' . esc_url($src) . '"';
|
||||
}
|
||||
function aksrt_get_search_box(array $args = []): string { return '<div class="search-input">search-box</div>'; }
|
||||
function aksrt_icon_cover_card(array $card): string {
|
||||
return '<div class="icon-cover-card flex ac ' . esc_attr((string) ($card['class'] ?? '')) . '">' . esc_html((string) ($card['title'] ?? '')) . '</div>';
|
||||
}
|
||||
|
||||
require_once dirname(__DIR__, 2) . '/inc/helpers/homepage/header-components.php';
|
||||
|
||||
$slides = [
|
||||
['background' => 'https://example.test/slide.jpg', 'link' => ['url' => 'https://example.test/one', 'target' => '_blank', 'text' => 'One'], 'text' => ['title' => 'Title', 'desc' => 'Description'], 'image_layer' => [['image' => 'https://example.test/layer.png', 'free_size' => true, 'align' => 'right', 'parallax' => 40, 'parallax_opacity' => 80, 'parallax_scale' => 110]]],
|
||||
['background_video' => 'https://example.test/background.mp4', 'background' => 'https://example.test/poster.jpg'],
|
||||
['video' => 'https://example.test/content.mp4', 'background' => 'https://example.test/content-poster.jpg'],
|
||||
['background' => 'https://example.test/pc-only.jpg', 'hide' => 'm'],
|
||||
['background' => 'https://example.test/mobile-only.jpg', 'hide' => 'pc'],
|
||||
];
|
||||
$settings = [
|
||||
'slides' => $slides,
|
||||
'show_on_pages' => ['home'],
|
||||
'show_type' => '',
|
||||
'nav_color' => '#ffffff',
|
||||
'slider_config' => ['loop' => true, 'autoplay' => true, 'interval' => 6, 'pagination' => true],
|
||||
'search_show' => ['pc_s', 'm_s'],
|
||||
'search_config' => ['before_html' => '<div>Search intro</div>'],
|
||||
'card_show' => ['pc_s', 'm_s'],
|
||||
'card_config' => ['show_widget_bg' => false, 'cards' => [['icon' => 'fa fas fa-star', 'title' => 'Card one']]],
|
||||
'filter_blur' => ['pc_s', 'm_s'],
|
||||
];
|
||||
\AKSRT\Settings\Settings::$headerSlider = $settings;
|
||||
|
||||
ob_start();
|
||||
aksrt_header_slide();
|
||||
$desktop = (string) ob_get_clean();
|
||||
|
||||
$GLOBALS['aksrt_mobile'] = true;
|
||||
ob_start();
|
||||
aksrt_header_slide();
|
||||
$mobile = (string) ob_get_clean();
|
||||
|
||||
$settings['card_config']['show_widget_bg'] = true;
|
||||
\AKSRT\Settings\Settings::$headerSlider = $settings;
|
||||
ob_start();
|
||||
aksrt_header_slide();
|
||||
$module_background = (string) ob_get_clean();
|
||||
|
||||
$GLOBALS['aksrt_mobile'] = false;
|
||||
$GLOBALS['aksrt_lazy'] = false;
|
||||
$lazy_disabled = aksrt_get_header_slider_markup([
|
||||
'slides' => [['background' => 'https://example.test/eager.jpg']],
|
||||
'class' => 'slide-index slide-header',
|
||||
]);
|
||||
$single_slide = aksrt_get_header_slider_markup([
|
||||
'slides' => [['background' => 'https://example.test/single.jpg']],
|
||||
'class' => 'slide-index slide-header',
|
||||
'pagination' => true,
|
||||
'button' => true,
|
||||
]);
|
||||
|
||||
require_once dirname(__DIR__, 2) . '/src/Admin/PageConfigMetaBox.php';
|
||||
$sanitize_cards = new \ReflectionMethod('AKSRT\\Admin\\PageConfigMetaBox', 'sanitize_card_options');
|
||||
$page_config_save = $sanitize_cards->invoke(null, ['show_widget_bg' => true, 'cards' => []]);
|
||||
|
||||
echo json_encode([
|
||||
'desktop' => $desktop,
|
||||
'mobile' => $mobile,
|
||||
'module_background' => $module_background,
|
||||
'lazy_disabled' => $lazy_disabled,
|
||||
'single_slide' => $single_slide,
|
||||
'page_config_save' => $page_config_save,
|
||||
], JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);
|
||||
}
|
||||
@@ -0,0 +1,62 @@
|
||||
'use strict';
|
||||
|
||||
const assert = require('node:assert/strict');
|
||||
const { spawnSync } = require('node:child_process');
|
||||
const fs = require('node:fs');
|
||||
const path = require('node:path');
|
||||
const test = require('node:test');
|
||||
|
||||
const root = path.resolve(__dirname, '..', '..');
|
||||
|
||||
test('header multifunction renders reference slide, search, and card contracts on desktop and mobile', () => {
|
||||
const rendered = spawnSync(
|
||||
process.env.PHP_BINARY || 'php',
|
||||
[path.join(__dirname, 'header-multifunction-render-harness.php')],
|
||||
{ cwd: root, encoding: 'utf8' },
|
||||
);
|
||||
assert.equal(rendered.status, 0, rendered.stderr || rendered.stdout);
|
||||
const { desktop, mobile, module_background: moduleBackground } = JSON.parse(rendered.stdout);
|
||||
|
||||
assert.match(desktop, /data-src="https:\/\/example\.test\/slide\.jpg"/);
|
||||
assert.match(desktop, /data-swiper-parallax="40%" data-swiper-parallax-opacity="0\.8" data-swiper-parallax-scale="1\.1"/);
|
||||
assert.match(desktop, /data-src="https:\/\/example\.test\/pc-only\.jpg"/);
|
||||
assert.doesNotMatch(desktop, /mobile-only\.jpg/);
|
||||
assert.match(desktop, /class="swiper-slide slide-background-video"/);
|
||||
assert.match(desktop, /data-src="https:\/\/example\.test\/background\.mp4"/);
|
||||
assert.match(desktop, /class="new-dplayer" video-url="https:\/\/example\.test\/content\.mp4" video-type="auto"/);
|
||||
assert.match(desktop, /class="absolute dplayer-poster"/);
|
||||
assert.doesNotMatch(desktop, /<video controls/);
|
||||
assert.match(desktop, /header-slider-search-more text-center before/);
|
||||
assert.match(desktop, /<div class="search-input">search-box<\/div>/);
|
||||
assert.match(desktop, /<div class="icon-cover-card flex ac aksrt-panel">Card one<\/div>/);
|
||||
|
||||
assert.doesNotMatch(mobile, /background\.mp4/);
|
||||
assert.match(mobile, /data-src="https:\/\/example\.test\/mobile-only\.jpg"/);
|
||||
assert.doesNotMatch(mobile, /pc-only\.jpg/);
|
||||
assert.match(mobile, /data-src="https:\/\/example\.test\/poster\.jpg"/);
|
||||
assert.match(mobile, /class="new-dplayer" video-url="https:\/\/example\.test\/content\.mp4"/);
|
||||
assert.match(mobile, /header-slider-search-more text-center before/);
|
||||
assert.match(mobile, /<div class="icon-cover-card flex ac aksrt-panel">Card one<\/div>/);
|
||||
|
||||
assert.match(moduleBackground, /<div class="aksrt-panel padding-10"><div class="flex jse/);
|
||||
assert.match(moduleBackground, /<div class="icon-cover-card flex ac padding-10">Card one<\/div>/);
|
||||
assert.equal(JSON.parse(rendered.stdout).page_config_save.show_widget_bg, true);
|
||||
|
||||
const { lazy_disabled: eager, single_slide: single } = JSON.parse(rendered.stdout);
|
||||
assert.match(eager, /<img class="radius8" src="https:\/\/example\.test\/eager\.jpg"/);
|
||||
assert.doesNotMatch(eager, /data-src=|lazyload/);
|
||||
assert.match(single, /swiper-container-initialized/);
|
||||
assert.doesNotMatch(single, /swiper-pagination|swiper-button-prev/);
|
||||
});
|
||||
|
||||
test('header card background setting is editable, saved, and defaults off', () => {
|
||||
const schema = fs.readFileSync(path.join(root, 'src/Admin/Schema/SchemaPageDisplay.php'), 'utf8');
|
||||
const settings = fs.readFileSync(path.join(root, 'src/Settings/DisplayPageGroups.php'), 'utf8');
|
||||
const metaBox = fs.readFileSync(path.join(root, 'src/Admin/PageConfigMetaBox.php'), 'utf8');
|
||||
|
||||
assert.match(schema, /field\('show_widget_bg', '模块背景', 'switcher', false/);
|
||||
assert.match(schema, /'show_widget_bg'\s*=>\s*false/);
|
||||
assert.match(settings, /'show_widget_bg'\s*=>\s*false/);
|
||||
assert.match(metaBox, /'show_widget_bg'\s*=>\s*!empty\(\$value\['show_widget_bg'\]\)/);
|
||||
assert.match(metaBox, /'show_widget_bg'\s*=>\s*false/);
|
||||
});
|
||||
@@ -0,0 +1,45 @@
|
||||
'use strict';
|
||||
|
||||
const assert = require('node:assert/strict');
|
||||
const fs = require('node:fs');
|
||||
const path = require('node:path');
|
||||
const test = require('node:test');
|
||||
const vm = require('node:vm');
|
||||
const root = path.resolve(__dirname, '../..');
|
||||
const runtime = fs.readFileSync(path.join(root, 'assets/js/runtime/main.js'), 'utf8');
|
||||
|
||||
test('video poster clicks play the same DPlayer instance and reveal its controls', () => {
|
||||
const slide = {};
|
||||
let played = 0;
|
||||
let revealed = false;
|
||||
let handler;
|
||||
const start = runtime.indexOf("_win.bd.on('click', '.swiper-slide.slide-dplayer");
|
||||
const end = runtime.indexOf('//视频列表图封面', start);
|
||||
assert.ok(start > -1 && end > start);
|
||||
vm.runInNewContext(runtime.slice(start, end), {
|
||||
_win: { bd: { on(event, selector, callback) {
|
||||
assert.equal(event, 'click');
|
||||
assert.equal(selector, '.swiper-slide.slide-dplayer:not(.slide-dplayer-played) > *');
|
||||
handler = callback;
|
||||
} } },
|
||||
jQuery(target) {
|
||||
assert.equal(target, slide);
|
||||
return {
|
||||
find(selector) {
|
||||
assert.equal(selector, '.new-dplayer');
|
||||
return { data(key) {
|
||||
assert.equal(key, 'dplayer');
|
||||
return { play() { played++; } };
|
||||
} };
|
||||
},
|
||||
parent() { return { addClass(name) {
|
||||
assert.equal(name, 'slide-dplayer-played');
|
||||
revealed = true;
|
||||
} }; },
|
||||
};
|
||||
},
|
||||
});
|
||||
handler.call(slide);
|
||||
assert.equal(played, 1);
|
||||
assert.equal(revealed, true);
|
||||
});
|
||||
@@ -12,6 +12,7 @@ test('icon card renderers keep vertical and horizontal legacy DOM contracts sepa
|
||||
const helper = read('inc/helpers/aksrt-icon-cards.php');
|
||||
const widgets = read('src/WidgetSystem/Renderers/GeneralRenderer.php');
|
||||
const header = read('inc/helpers/homepage/header-components.php');
|
||||
const mediaBrand = read('inc/helpers/template-tags/media-brand.php');
|
||||
|
||||
assert.match(helper, /<div class="icon-card /);
|
||||
assert.match(helper, /<div class="icon-cover-card flex ac /);
|
||||
@@ -23,20 +24,26 @@ test('icon card renderers keep vertical and horizontal legacy DOM contracts sepa
|
||||
// combo_info legitimately renders icon_image cards; icon cards must not leak media-card fields.
|
||||
assert.doesNotMatch(widgets, /\$card\['description'\]/);
|
||||
assert.match(header, /empty\(\$card\['custom_icon'\]\)/);
|
||||
assert.doesNotMatch(mediaBrand, /function aksrt_render_icon_cover_card/);
|
||||
});
|
||||
|
||||
test('homepage horizontal cards render fully from legacy CSS with no dedicated stylesheet', () => {
|
||||
const baseCss = read('assets/css/frontend/base.css');
|
||||
const legacyUtilities = read('assets/css/frontend/legacy-utilities.css');
|
||||
const mainCss = read('assets/css/main.css');
|
||||
const widgetCss = read('assets/widgets/frontend.css');
|
||||
const scripts = read('src/Core/ScriptsPart1b.php');
|
||||
|
||||
assert.equal(fs.existsSync(path.join(root, 'assets/css/frontend/header-cards.css')), false);
|
||||
assert.doesNotMatch(baseCss, /\.icon-cover-card\.aksrt-icon-cover-card/);
|
||||
assert.doesNotMatch(baseCss, /\.header-components-card/);
|
||||
assert.match(
|
||||
baseCss,
|
||||
/\.header-slider-card\s+\.icon-cover-card\s*\{[^}]*font-size:\s*12px/s,
|
||||
);
|
||||
assert.doesNotMatch(baseCss, /\.header-slider-card\s+\.icon-cover-card\s*\{[^}]*font-size:/s);
|
||||
assert.doesNotMatch(legacyUtilities, /\.px12-sm\s*\{[^}]*font-size:\s*12px/);
|
||||
const mobileTextSize = mainCss.lastIndexOf('.px12-sm,');
|
||||
const mobileBreakpoint = mainCss.lastIndexOf('@media', mobileTextSize);
|
||||
assert.ok(mobileBreakpoint > 0, 'the compact card text utility must remain inside a responsive media rule');
|
||||
assert.match(mainCss.slice(mobileBreakpoint, mobileTextSize + 120), /max-width:\s*640px/);
|
||||
assert.match(mainCss.slice(mobileTextSize, mobileTextSize + 120), /font-size:\s*12px/);
|
||||
assert.doesNotMatch(scripts, /header-cards/);
|
||||
assert.doesNotMatch(widgetCss, /aksrt-widget-icon-cards \.icon-cover-card/);
|
||||
});
|
||||
|
||||
@@ -447,6 +447,7 @@ test('save contract rejects invalid input and round-trips canonical nested value
|
||||
assert.equal(data.unknown_section, true)
|
||||
assert.equal(data.invalid_enum_rejected, true)
|
||||
assert.equal(data.nested_multiselect_valid, true)
|
||||
assert.equal(data.header_card_bg, true)
|
||||
assert.equal(data.footer_valid, true)
|
||||
assert.equal(data.vip_switches_valid, true)
|
||||
assert.deepEqual(data.vip_switches_clean, {
|
||||
@@ -495,5 +496,6 @@ test('save contract rejects invalid input and round-trips canonical nested value
|
||||
message_excludes_home: false,
|
||||
home_excludes_public_home: false,
|
||||
home_excludes_user_center: false,
|
||||
paged_archive_hidden: false,
|
||||
})
|
||||
})
|
||||
@@ -176,6 +176,7 @@ namespace {
|
||||
'link' => ['url' => 'https://example.test/slide', 'target' => '_blank', 'text' => '<b>Go</b>', 'unknown' => 'drop-me'],
|
||||
'text' => ['title' => '<strong>Safe</strong><script>bad()</script>', 'desc' => '<em>Copy</em>', 'unknown' => 'drop-me'],
|
||||
]],
|
||||
'header_slider_card_option' => ['show_widget_bg' => true, 'cards' => []],
|
||||
];
|
||||
$header_clean = $provider->sanitize_payload('page_display', $header_payload, 'header-components');
|
||||
|
||||
@@ -219,9 +220,10 @@ namespace {
|
||||
* @param list<string> $show_on
|
||||
* @param array<string,mixed> $page_config
|
||||
*/
|
||||
function aksrt_visibility_case(string $query, array $show_on, array $page_config = []): bool {
|
||||
function aksrt_visibility_case(string $query, array $show_on, array $page_config = [], int $paged = 0): bool {
|
||||
$GLOBALS['aksrt_visibility_query'] = $query;
|
||||
$GLOBALS['aksrt_visibility_page_config'] = $page_config;
|
||||
$GLOBALS['wp_query'] = new WP_Query(['paged' => $paged]);
|
||||
\AKSRT\Settings\Settings::$headerSlider = [
|
||||
'show_on_pages' => $show_on,
|
||||
'show_type' => '',
|
||||
@@ -249,6 +251,7 @@ namespace {
|
||||
'message_excludes_home' => aksrt_visibility_case('message', ['home']),
|
||||
'home_excludes_public_home' => aksrt_visibility_case('user_home', ['home']),
|
||||
'home_excludes_user_center' => aksrt_visibility_case('user_center', ['home']),
|
||||
'paged_archive_hidden' => aksrt_visibility_case('home', ['home'], [], 2),
|
||||
];
|
||||
|
||||
echo json_encode([
|
||||
@@ -260,6 +263,7 @@ namespace {
|
||||
'invalid_enum_rejected' => !$invalid_validation['valid'],
|
||||
'nested_multiselect_valid' => $category_validation['valid'],
|
||||
'nested' => $header_clean,
|
||||
'header_card_bg' => $header_clean['header_slider_card_option']['show_widget_bg'] ?? null,
|
||||
'footer_valid' => $footer_validation['valid'],
|
||||
'footer_clean' => $footer_clean,
|
||||
'footer_readback' => $footer_readback,
|
||||
|
||||
@@ -0,0 +1,45 @@
|
||||
const test = require('node:test');
|
||||
const assert = require('node:assert/strict');
|
||||
const fs = require('node:fs');
|
||||
const path = require('node:path');
|
||||
const { spawnSync } = require('node:child_process');
|
||||
|
||||
const root = path.resolve(__dirname, '..', '..');
|
||||
const read = (file) => fs.readFileSync(path.join(root, file), 'utf8');
|
||||
|
||||
test('search route keeps the reference Zibll results and empty-state structure', () => {
|
||||
const template = read('search.php');
|
||||
const styles = read('assets/css/main.css');
|
||||
|
||||
for (const token of ['search-input', 'search-page-ajaxpager', 'search-tab-header', 'search-filter-item', 'post_ajax_loader']) {
|
||||
assert.match(template + styles, new RegExp(token.replace(/[.*+?^${}()|[\\]\\]/g, '\\$&')));
|
||||
}
|
||||
assert.match(template, /aksrt_get_ajax_null\(__\('请输入搜索关键词'/);
|
||||
assert.match(template, /'null-search\.svg'/);
|
||||
assert.match(template, /user_query instanceof WP_User_Query/);
|
||||
assert.equal(fs.existsSync(path.join(root, 'assets/css/routes/search.css')), false);
|
||||
assert.match(template, /'skip_wrapper' => true/);
|
||||
assert.match(template, /null-user\.svg/);
|
||||
assert.match(template, /null-post\.svg/);
|
||||
assert.match(template, /route-title=/);
|
||||
for (const image of ['null-search.svg', 'null-user.svg', 'null-post.svg']) {
|
||||
assert.match(read(`assets/img/${image}`), /<svg\b/);
|
||||
}
|
||||
assert.equal(fs.existsSync(path.join(root, 'assets/img/null-search.svg')), true);
|
||||
assert.equal(fs.existsSync(path.join(root, 'assets/img/null-user.svg')), true);
|
||||
});
|
||||
|
||||
test('search rendering preserves form state, result markup, paging and escaped keywords', () => {
|
||||
const result = spawnSync(process.env.PHP_BINARY || 'php', [path.join(__dirname, 'search-render-harness.php')], {
|
||||
cwd: root,
|
||||
encoding: 'utf8',
|
||||
});
|
||||
assert.equal(result.status, 0, result.stderr || result.stdout);
|
||||
assert.match(result.stdout, /Search render harness passed/);
|
||||
});
|
||||
|
||||
test('search route renders reference fluid widget sidebars in order', () => {
|
||||
const template = read('search.php');
|
||||
assert.match(template, /dynamic_sidebar\('all_top_fluid'\)[\s\S]*dynamic_sidebar\('search_top_fluid'\)/);
|
||||
assert.match(template, /dynamic_sidebar\('search_bottom_fluid'\)[\s\S]*dynamic_sidebar\('all_bottom_fluid'\)/);
|
||||
});
|
||||
@@ -0,0 +1,200 @@
|
||||
<?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";
|
||||
@@ -0,0 +1,100 @@
|
||||
'use strict';
|
||||
|
||||
const assert = require('node:assert/strict');
|
||||
const fs = require('node:fs');
|
||||
const path = require('node:path');
|
||||
const test = require('node:test');
|
||||
const vm = require('node:vm');
|
||||
const root = path.resolve(__dirname, '../..');
|
||||
const read = (file) => fs.readFileSync(path.join(root, file), 'utf8');
|
||||
const runtime = read('assets/js/runtime/main.js');
|
||||
|
||||
function handlerBetween(start, end, context) {
|
||||
let handler;
|
||||
vm.runInNewContext(runtime.slice(runtime.indexOf(start), runtime.indexOf(end)), {
|
||||
_win: { bd: { on(event, selector, callback) { handler = callback; } } },
|
||||
...context,
|
||||
});
|
||||
assert.equal(typeof handler, 'function');
|
||||
return handler;
|
||||
}
|
||||
|
||||
test('search type changes reset only the active form placeholder', () => {
|
||||
const field = { length: 1, attr: () => '开启精彩搜索', text(value) { this.value = value; } };
|
||||
const control = {};
|
||||
const handler = handlerBetween('//搜索功能-恢复默认占位符', '//搜索功能->删除多余的input', {
|
||||
jQuery(target) {
|
||||
assert.equal(target, control, 'must not select all search boxes');
|
||||
return { closest(selector) {
|
||||
assert.equal(selector, 'form');
|
||||
return { find(selector) { assert.equal(selector, '.scale-placeholder'); return field; } };
|
||||
} };
|
||||
},
|
||||
});
|
||||
handler.call(control);
|
||||
assert.equal(field.value, '开启精彩搜索');
|
||||
});
|
||||
|
||||
test('clearing history expires the saved cookie at its original WordPress path and domain', () => {
|
||||
const control = {};
|
||||
const history = {};
|
||||
const cookies = [];
|
||||
let removed = false;
|
||||
let confirmed = true;
|
||||
const jQuery = (target) => {
|
||||
if (target === control) return { attr(name) {
|
||||
return { 'data-cookie-path': '/blog/', 'data-cookie-domain': '.example.test' }[name];
|
||||
} };
|
||||
if (target === '.history-search') return { slideUp(duration, complete) {
|
||||
assert.equal(duration, 300);
|
||||
complete.call(history);
|
||||
} };
|
||||
assert.equal(target, history);
|
||||
return { remove() { removed = true; } };
|
||||
};
|
||||
jQuery.cookie = (name, value, options) => cookies.push({ name, value, ...options });
|
||||
const handler = handlerBetween('//清空搜索关键词', '//用户签到', {
|
||||
jQuery, confirm: () => confirmed, aksrt__: (key) => key,
|
||||
});
|
||||
handler.call(control);
|
||||
assert.deepEqual(cookies, [{ name: 'history_search', value: null, path: '/blog/', domain: '.example.test' }]);
|
||||
assert.equal(removed, true);
|
||||
confirmed = false;
|
||||
handler.call(control);
|
||||
assert.equal(cookies.length, 1, 'cancelling must preserve history');
|
||||
});
|
||||
|
||||
test('removed search markup has no orphaned JavaScript owner', () => {
|
||||
assert.doesNotMatch(read('assets/js/core/article.js'), /initSearchClear|search-clear-btn|search-page-input|search-category-filter|aksrt_search_type|aksrt_category/);
|
||||
assert.doesNotMatch(runtime, /eval\('\/' \+ val|search-tab \.item-heading/);
|
||||
assert.doesNotMatch(runtime, /css\/dplayer\.min\.css/);
|
||||
});
|
||||
|
||||
test('search submissions replace results with encoded filters and navigate normally when cleared', () => {
|
||||
const handlers = {};
|
||||
const form = {};
|
||||
let data = { s: '主题 & "搜索"', type: 'post', trem: '7' };
|
||||
let linkMarkup = '';
|
||||
let clicks = 0;
|
||||
const link = { click() { clicks++; return this; }, remove() {} };
|
||||
const pager = { length: 1, append(node) { assert.equal(node, link); } };
|
||||
const jQuery = (target) => {
|
||||
if (target === '.search-page-ajaxpager') return pager;
|
||||
if (target === form) return { serializeObject: () => data, attr: () => 'https://example.test/blog/' };
|
||||
linkMarkup = target;
|
||||
return link;
|
||||
};
|
||||
vm.runInNewContext(runtime.slice(runtime.indexOf('//搜索页面关键词ajax'), runtime.indexOf('function nav_folding()')), {
|
||||
_win: { bd: { on(event, selector, handler) { handlers[event] = handler; } } },
|
||||
jQuery, URL, URLSearchParams,
|
||||
});
|
||||
assert.equal(handlers.submit.call(form), false);
|
||||
const url = new URL(linkMarkup.match(/href="([^"]+)"/)[1]);
|
||||
assert.equal(url.searchParams.get('s'), data.s);
|
||||
assert.equal(url.searchParams.get('type'), 'post');
|
||||
assert.equal(url.searchParams.get('trem'), '7');
|
||||
assert.equal(clicks, 1);
|
||||
data = { s: ' ', type: 'post' };
|
||||
assert.equal(handlers.submit.call(form), undefined);
|
||||
assert.equal(clicks, 1, 'the empty page has no AJAX pager and must use normal form navigation');
|
||||
});
|
||||
Reference in new issue
Block a user