Merge pull request 'Worktree ux depth batch6' (#224) from worktree-ux-depth-batch6 into main
Reviewed-on: CYL/wordpress-theme-aksrt#224
This commit is contained in:
@@ -307,15 +307,35 @@
|
||||
.aksrt-bookmark-links__desc { font-size: 11px; color: var(--muted-2-color, #999); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
|
||||
|
||||
|
||||
/* Search keywords */
|
||||
.aksrt-search-widget__keywords { margin-top: 10px; }
|
||||
/* Search keywords + history */
|
||||
.aksrt-search-widget__keywords,
|
||||
.aksrt-search-widget__history { margin-top: 10px; }
|
||||
.aksrt-search-widget__kw-head {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: space-between;
|
||||
gap: 8px;
|
||||
margin-bottom: 6px;
|
||||
}
|
||||
.aksrt-search-widget__kw-title {
|
||||
font-size: 12px;
|
||||
color: var(--muted-2-color, #888);
|
||||
margin-bottom: 6px;
|
||||
}
|
||||
.aksrt-search-widget__kw-list { display: flex; flex-wrap: wrap; gap: 6px; }
|
||||
.aksrt-search-widget__kw {
|
||||
.aksrt-search-widget__kw-head .aksrt-search-widget__kw-title { margin-bottom: 0; }
|
||||
.aksrt-search-widget__history-clear {
|
||||
border: 0;
|
||||
background: transparent;
|
||||
color: var(--muted-2-color, #999);
|
||||
font-size: 12px;
|
||||
cursor: pointer;
|
||||
padding: 0;
|
||||
}
|
||||
.aksrt-search-widget__history-clear:hover { color: var(--color-primary, #3b82f6); }
|
||||
.aksrt-search-widget__kw-list,
|
||||
.aksrt-search-widget__history-list { display: flex; flex-wrap: wrap; gap: 6px; }
|
||||
.aksrt-search-widget__kw,
|
||||
.aksrt-search-widget__history-item {
|
||||
display: inline-block;
|
||||
padding: 3px 10px;
|
||||
border-radius: 999px;
|
||||
@@ -324,9 +344,110 @@
|
||||
text-decoration: none;
|
||||
font-size: 12px;
|
||||
}
|
||||
.aksrt-search-widget__history-empty {
|
||||
font-size: 12px;
|
||||
color: var(--muted-2-color, #999);
|
||||
}
|
||||
.aksrt-search-widget__more { margin-top: 8px; text-align: right; font-size: 12px; }
|
||||
.aksrt-search-widget__more a { color: var(--color-primary, #3b82f6); text-decoration: none; }
|
||||
|
||||
/* Product list widgets (batch6) */
|
||||
.aksrt-product-widget { margin-bottom: 8px; }
|
||||
.aksrt-product-widget__grid {
|
||||
display: grid;
|
||||
grid-template-columns: repeat(2, minmax(0, 1fr));
|
||||
gap: 10px;
|
||||
}
|
||||
.aksrt-product-widget--list .aksrt-product-widget__grid,
|
||||
.aksrt-product-widget--mini .aksrt-product-widget__grid {
|
||||
grid-template-columns: 1fr;
|
||||
}
|
||||
.aksrt-product-widget--oneline .aksrt-product-widget__grid {
|
||||
display: flex;
|
||||
overflow-x: auto;
|
||||
gap: 10px;
|
||||
scroll-snap-type: x mandatory;
|
||||
-webkit-overflow-scrolling: touch;
|
||||
padding-bottom: 4px;
|
||||
}
|
||||
.aksrt-product-widget--oneline .aksrt-product-widget__item,
|
||||
.aksrt-product-widget--oneline .aksrt-product-card {
|
||||
flex: 0 0 42%;
|
||||
min-width: 140px;
|
||||
scroll-snap-align: start;
|
||||
}
|
||||
.aksrt-product-widget__item,
|
||||
.aksrt-product-widget .aksrt-product-card {
|
||||
border: 1px solid var(--color-line, #e5e7eb);
|
||||
border-radius: 10px;
|
||||
overflow: hidden;
|
||||
background: var(--surface-1, #fff);
|
||||
}
|
||||
.aksrt-product-widget__thumb,
|
||||
.aksrt-product-widget .aksrt-product-card__image {
|
||||
display: block;
|
||||
aspect-ratio: 1 / 1;
|
||||
background: var(--surface-2, #f3f4f6);
|
||||
overflow: hidden;
|
||||
}
|
||||
.aksrt-product-widget__thumb img,
|
||||
.aksrt-product-widget .aksrt-product-card__image img {
|
||||
width: 100%;
|
||||
height: 100%;
|
||||
object-fit: cover;
|
||||
display: block;
|
||||
}
|
||||
.aksrt-product-widget__info,
|
||||
.aksrt-product-widget .aksrt-product-card__body { padding: 8px 10px 10px; }
|
||||
.aksrt-product-widget__title,
|
||||
.aksrt-product-widget .aksrt-product-card__title {
|
||||
display: -webkit-box;
|
||||
-webkit-line-clamp: 2;
|
||||
-webkit-box-orient: vertical;
|
||||
overflow: hidden;
|
||||
font-size: 13px;
|
||||
font-weight: 600;
|
||||
line-height: 1.35;
|
||||
color: inherit;
|
||||
text-decoration: none;
|
||||
}
|
||||
.aksrt-product-widget__meta {
|
||||
display: flex;
|
||||
justify-content: space-between;
|
||||
gap: 6px;
|
||||
margin-top: 6px;
|
||||
font-size: 12px;
|
||||
}
|
||||
.aksrt-product-widget__price,
|
||||
.aksrt-product-widget .aksrt-product-card__price {
|
||||
color: var(--color-primary, #ef4444);
|
||||
font-weight: 700;
|
||||
}
|
||||
.aksrt-product-widget__sales { color: var(--muted-2-color, #999); font-size: 11px; }
|
||||
.aksrt-product-widget__empty { font-size: 13px; color: var(--muted-2-color, #999); padding: 12px 0; }
|
||||
.aksrt-product-widget .ajax-pag { margin-top: 12px; text-align: center; }
|
||||
.aksrt-product-tab__headers {
|
||||
display: flex;
|
||||
gap: 6px;
|
||||
overflow-x: auto;
|
||||
margin-bottom: 12px;
|
||||
-webkit-overflow-scrolling: touch;
|
||||
}
|
||||
.aksrt-product-tab__header {
|
||||
flex: 0 0 auto;
|
||||
border: 0;
|
||||
background: var(--surface-2, #f3f4f6);
|
||||
border-radius: 999px;
|
||||
padding: 6px 12px;
|
||||
font-size: 13px;
|
||||
cursor: pointer;
|
||||
color: inherit;
|
||||
}
|
||||
.aksrt-product-tab__header.is-active {
|
||||
background: var(--color-primary, #3b82f6);
|
||||
color: #fff;
|
||||
}
|
||||
|
||||
/* Scroll notice */
|
||||
.aksrt-scroll-notice {
|
||||
display: flex;
|
||||
@@ -552,3 +673,106 @@
|
||||
color: #b45309;
|
||||
}
|
||||
.aksrt-author-card__badge img { border-radius: 50%; }
|
||||
|
||||
/* Badge remote modal (batch6) */
|
||||
.aksrt-badge-modal { padding: 8px 4px 12px; }
|
||||
.aksrt-badge-modal__head {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 10px;
|
||||
margin-bottom: 12px;
|
||||
position: relative;
|
||||
padding-right: 28px;
|
||||
}
|
||||
.aksrt-badge-modal__avatar {
|
||||
width: 40px; height: 40px; border-radius: 50%; object-fit: cover;
|
||||
}
|
||||
.aksrt-badge-modal__title { font-weight: 700; font-size: 15px; flex: 1; min-width: 0; }
|
||||
.aksrt-badge-modal__wear-tag {
|
||||
display: inline-block;
|
||||
margin-left: 6px;
|
||||
font-size: 12px;
|
||||
font-weight: 500;
|
||||
color: var(--color-primary, #3b82f6);
|
||||
}
|
||||
.aksrt-badge-modal__list { max-height: 50vh; overflow: auto; }
|
||||
.aksrt-badge-modal__item {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 10px;
|
||||
padding: 10px 4px;
|
||||
border-bottom: 1px solid var(--color-line, #eee);
|
||||
}
|
||||
.aksrt-badge-modal__item:last-child { border-bottom: 0; }
|
||||
.aksrt-badge-modal__item .medal-icon { width: 40px; height: 40px; object-fit: contain; }
|
||||
.aksrt-badge-modal__icon-fa {
|
||||
width: 40px; height: 40px; border-radius: 50%;
|
||||
display: inline-flex; align-items: center; justify-content: center;
|
||||
background: var(--surface-2, #f3f4f6); color: #f59e0b;
|
||||
}
|
||||
.aksrt-badge-modal__icon-fa.lg { width: 72px; height: 72px; font-size: 28px; }
|
||||
.aksrt-badge-modal__meta { flex: 1; min-width: 0; }
|
||||
.aksrt-badge-modal__name { font-weight: 600; font-size: 14px; }
|
||||
.aksrt-badge-modal__empty { text-align: center; color: var(--muted-2-color, #999); padding: 24px 0; }
|
||||
.aksrt-badge-modal--single { text-align: center; padding: 16px 8px 20px; }
|
||||
.aksrt-badge-modal__card { padding: 12px 8px 8px; }
|
||||
.aksrt-badge-modal__actions { margin-top: 12px; }
|
||||
.aksrt-author-hero__badges { margin-top: 8px; }
|
||||
|
||||
/* Forum tab topics (batch6) */
|
||||
.aksrt-ftab__nav {
|
||||
display: flex;
|
||||
gap: 6px;
|
||||
list-style: none;
|
||||
margin: 0 0 10px;
|
||||
padding: 0;
|
||||
overflow-x: auto;
|
||||
}
|
||||
.aksrt-ftab__btn {
|
||||
border: 0;
|
||||
background: var(--surface-2, #f3f4f6);
|
||||
border-radius: 999px;
|
||||
padding: 5px 12px;
|
||||
font-size: 13px;
|
||||
cursor: pointer;
|
||||
color: inherit;
|
||||
}
|
||||
.aksrt-ftab__btn.is-active {
|
||||
background: var(--color-primary, #3b82f6);
|
||||
color: #fff;
|
||||
}
|
||||
.aksrt-ftab__panel { display: none; }
|
||||
.aksrt-ftab__panel.is-active { display: block; }
|
||||
.aksrt-ftab__empty { font-size: 13px; color: var(--muted-2-color, #999); padding: 8px 0; }
|
||||
.aksrt-ftopic-list { list-style: none; margin: 0; padding: 0; }
|
||||
.aksrt-ftopic-list__item {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: space-between;
|
||||
gap: 8px;
|
||||
padding: 7px 0;
|
||||
border-bottom: 1px dashed var(--color-line, #eee);
|
||||
font-size: 13px;
|
||||
}
|
||||
.aksrt-ftopic-list__item:last-child { border-bottom: 0; }
|
||||
.aksrt-ftopic-list__link {
|
||||
color: inherit;
|
||||
text-decoration: none;
|
||||
flex: 1;
|
||||
min-width: 0;
|
||||
white-space: nowrap;
|
||||
overflow: hidden;
|
||||
text-overflow: ellipsis;
|
||||
}
|
||||
.aksrt-ftopic-list__views { color: var(--muted-2-color, #999); font-size: 12px; flex-shrink: 0; }
|
||||
.aksrt-ftopic-list__meta {
|
||||
display: flex;
|
||||
gap: 8px;
|
||||
font-size: 11px;
|
||||
color: var(--muted-2-color, #999);
|
||||
margin-top: 4px;
|
||||
}
|
||||
.aksrt-ftopic-list.is-detail .aksrt-ftopic-list__item {
|
||||
flex-direction: column;
|
||||
align-items: flex-start;
|
||||
}
|
||||
|
||||
@@ -390,6 +390,109 @@
|
||||
|
||||
window.__aksrtOpenSearchPanel = function () { setOpen(true); };
|
||||
window.__aksrtCloseSearchPanel = function () { setOpen(false); };
|
||||
|
||||
// Sidebar / widget search history (zibll widget_ui_search show_history).
|
||||
initWidgetSearchHistory({
|
||||
historyEnabled: historyEnabled,
|
||||
readHistory: readHistory,
|
||||
writeHistory: writeHistory,
|
||||
requestHistorySync: requestHistorySync,
|
||||
pushHistory: pushHistory,
|
||||
getSearchType: getSearchType,
|
||||
searchConfig: searchConfig,
|
||||
});
|
||||
}
|
||||
|
||||
function initWidgetSearchHistory(ctx) {
|
||||
var lists = document.querySelectorAll("[data-aksrt-search-history-list]");
|
||||
if (!lists.length) return;
|
||||
if (!ctx.historyEnabled) {
|
||||
document.querySelectorAll("[data-aksrt-search-history]").forEach(function (el) {
|
||||
el.hidden = true;
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
function escapeHtml(s) {
|
||||
return String(s == null ? "" : s)
|
||||
.replace(/&/g, "&")
|
||||
.replace(/</g, "<")
|
||||
.replace(/>/g, ">")
|
||||
.replace(/"/g, """);
|
||||
}
|
||||
|
||||
function paint(items) {
|
||||
var list = Array.isArray(items) ? items : [];
|
||||
lists.forEach(function (wrap) {
|
||||
var section = wrap.closest("[data-aksrt-search-history]") || wrap.parentElement;
|
||||
var clearBtn = section ? section.querySelector("[data-aksrt-search-history-clear]") : null;
|
||||
if (!list.length) {
|
||||
wrap.innerHTML = '<span class="aksrt-search-widget__history-empty">' + escapeHtml("暂无搜索历史") + "</span>";
|
||||
if (clearBtn) clearBtn.hidden = true;
|
||||
return;
|
||||
}
|
||||
if (clearBtn) clearBtn.hidden = false;
|
||||
wrap.innerHTML = list
|
||||
.map(function (item) {
|
||||
var kw = item && item.keyword ? String(item.keyword) : "";
|
||||
if (!kw) return "";
|
||||
var type = (item && item.type) || "post";
|
||||
var href = "?s=" + encodeURIComponent(kw);
|
||||
return (
|
||||
'<a class="aksrt-search-widget__kw aksrt-search-widget__history-item" href="' +
|
||||
href +
|
||||
'" data-history-keyword="' +
|
||||
encodeURIComponent(kw) +
|
||||
'" data-history-type="' +
|
||||
encodeURIComponent(type) +
|
||||
'">' +
|
||||
escapeHtml(kw) +
|
||||
"</a>"
|
||||
);
|
||||
})
|
||||
.join("");
|
||||
});
|
||||
}
|
||||
|
||||
function load() {
|
||||
if (window.aksrtData && aksrtData.isLoggedIn) {
|
||||
ctx.requestHistorySync("get", null, function (items) {
|
||||
paint(items);
|
||||
});
|
||||
return;
|
||||
}
|
||||
paint(ctx.readHistory());
|
||||
}
|
||||
|
||||
document.querySelectorAll("[data-aksrt-search-history-clear]").forEach(function (btn) {
|
||||
btn.addEventListener("click", function (e) {
|
||||
e.preventDefault();
|
||||
if (window.aksrtData && aksrtData.isLoggedIn) {
|
||||
ctx.requestHistorySync("save", [], function (items) {
|
||||
paint(items);
|
||||
});
|
||||
return;
|
||||
}
|
||||
ctx.writeHistory([]);
|
||||
paint([]);
|
||||
});
|
||||
});
|
||||
|
||||
// Also push history when submitting bare widget forms without data-aksrt-search-form.
|
||||
document.querySelectorAll(".aksrt-search-widget form").forEach(function (form) {
|
||||
if (form.hasAttribute("data-aksrt-search-form")) return;
|
||||
form.addEventListener("submit", function () {
|
||||
var input = form.querySelector('input[name="s"]');
|
||||
var typeInput = form.querySelector('input[name="aksrt_search_type"]');
|
||||
var keyword = input ? (input.value || "").trim() : "";
|
||||
var type = typeInput
|
||||
? typeInput.value || ctx.searchConfig.defaultType || "post"
|
||||
: ctx.searchConfig.defaultType || "post";
|
||||
if (keyword) ctx.pushHistory(keyword, type);
|
||||
});
|
||||
});
|
||||
|
||||
load();
|
||||
}
|
||||
|
||||
window.AksrtFrontend.register("aksrt-search-panel", function () { initSearchPanel(); });
|
||||
|
||||
@@ -139,6 +139,38 @@
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
// Global wear handler (remote modal + UC modal).
|
||||
document.addEventListener("click", function (e) {
|
||||
var wearBtn = e.target.closest("[data-aksrt-badge-wear]");
|
||||
if (!wearBtn) return;
|
||||
// UC modal has its own handler when canWear on modal root.
|
||||
var modalRoot = wearBtn.closest("[data-aksrt-badges-modal]");
|
||||
if (modalRoot) return;
|
||||
e.preventDefault();
|
||||
var slug = wearBtn.getAttribute("data-aksrt-badge-wear") || "";
|
||||
if (!slug) return;
|
||||
wearBtn.disabled = true;
|
||||
api("/users/me/badges/wear", {
|
||||
method: "POST",
|
||||
body: JSON.stringify({ slug: slug }),
|
||||
}).then(function (r) {
|
||||
if (!r.ok) {
|
||||
toast((r.json && (r.json.message || (r.json.data && r.json.data.message))) || "佩戴失败", "error");
|
||||
wearBtn.disabled = false;
|
||||
return;
|
||||
}
|
||||
toast((r.json && r.json.message) || "已佩戴徽章", "success");
|
||||
// Soft refresh modal content if remote host present.
|
||||
try {
|
||||
document.dispatchEvent(new CustomEvent("aksrt:remote-modal:close"));
|
||||
} catch (_) {}
|
||||
}).catch(function () {
|
||||
toast("网络错误", "error");
|
||||
wearBtn.disabled = false;
|
||||
});
|
||||
});
|
||||
|
||||
if (document.readyState === 'loading') {
|
||||
document.addEventListener('DOMContentLoaded', init);
|
||||
} else {
|
||||
|
||||
@@ -65,6 +65,12 @@ extract($context, EXTR_SKIP); // phpcs:ignore WordPress.PHP.DontExtract.extract_
|
||||
<button type="button" class="btn-ghost btn-sm" data-aksrt-badges-open>
|
||||
<i class="fas fa-award" aria-hidden="true"></i> <?php esc_html_e('我的徽章', 'aksrt'); ?>
|
||||
</button>
|
||||
<?php
|
||||
$remote_badge = \AKSRT\User\Badges::get_info_link((int) $user_id, 'btn-ghost btn-sm', __('徽章弹层', 'aksrt'));
|
||||
if ($remote_badge !== '') {
|
||||
echo $remote_badge; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
|
||||
}
|
||||
?>
|
||||
<?php endif; ?>
|
||||
|
||||
<a href="<?php echo esc_url($messages_url); ?>" class="btn-ghost btn-sm">
|
||||
|
||||
@@ -379,6 +379,13 @@ class ShopController extends ControllerBase {
|
||||
'permission_callback' => '__return_true',
|
||||
]);
|
||||
|
||||
// Product list widget AJAX (zibll ajax_widget_ui product lists).
|
||||
register_rest_route(self::API_NAMESPACE, '/shop/frontend/widgets/products', [
|
||||
'methods' => 'GET',
|
||||
'callback' => [$this, 'get_frontend_widget_products'],
|
||||
'permission_callback' => '__return_true',
|
||||
]);
|
||||
|
||||
register_rest_route(self::API_NAMESPACE, '/shop/frontend/revenue', [
|
||||
'methods' => 'GET',
|
||||
'callback' => [$this, 'get_frontend_revenue'],
|
||||
|
||||
@@ -509,6 +509,38 @@ trait ShopControllerFrontendFlowOperations {
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* GET /shop/frontend/widgets/products — product list widget AJAX pager.
|
||||
*/
|
||||
public function get_frontend_widget_products(\WP_REST_Request $request): \WP_REST_Response {
|
||||
if (!class_exists('\AKSRT\Widgets\WidgetProductList')) {
|
||||
return $this->error('商品组件不可用', 500);
|
||||
}
|
||||
|
||||
$instance = [
|
||||
'per_page' => max(1, min(50, (int) ($request->get_param('per_page') ?? 8))),
|
||||
'orderby' => sanitize_key((string) ($request->get_param('orderby') ?? 'date')),
|
||||
'order' => sanitize_key((string) ($request->get_param('order') ?? 'DESC')),
|
||||
'cat_ids' => sanitize_text_field((string) ($request->get_param('cat_ids') ?? '')),
|
||||
'tag_ids' => sanitize_text_field((string) ($request->get_param('tag_ids') ?? '')),
|
||||
'discount_ids' => sanitize_text_field((string) ($request->get_param('discount_ids') ?? '')),
|
||||
'exclude_cat' => !empty($request->get_param('exclude_cat')) ? 1 : 0,
|
||||
'style' => sanitize_key((string) ($request->get_param('style') ?? 'default')),
|
||||
'paginate' => sanitize_key((string) ($request->get_param('paginate') ?? 'ajax')),
|
||||
];
|
||||
$page = max(1, (int) ($request->get_param('page') ?? $request->get_param('paged') ?? 1));
|
||||
$payload = \AKSRT\Widgets\WidgetProductList::query_payload($instance, $page);
|
||||
|
||||
return $this->success([
|
||||
'html' => (string) ($payload['html'] ?? ''),
|
||||
'paginate_html' => (string) ($payload['paginate_html'] ?? ''),
|
||||
'has_more' => !empty($payload['has_more']),
|
||||
'page' => (int) ($payload['page'] ?? $page),
|
||||
'max_pages' => (int) ($payload['max_pages'] ?? 1),
|
||||
'total' => (int) ($payload['total'] ?? 0),
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Render cat filter bar HTML for AJAX header replace.
|
||||
*/
|
||||
|
||||
@@ -968,7 +968,28 @@ trait ScriptsPart1b {
|
||||
}
|
||||
|
||||
|
||||
$this->enqueue_public_fonts_js_and_data($base_uri, $core_dep, $deps, $is_fixed_homepage, $is_home_or_archive, $is_single_post, $is_single_resource, $version);
|
||||
|
||||
// Badges UI: user center + public profile (zibll medal modal/list).
|
||||
if (class_exists('\AKSRT\User\Badges') && \AKSRT\User\Badges::enabled()) {
|
||||
$is_badge_surface = $is_profile_page
|
||||
|| is_author()
|
||||
|| (function_exists('aksrt_is_user_home_page') && aksrt_is_user_home_page())
|
||||
|| (function_exists('aksrt_is_user_center_page') && aksrt_is_user_center_page());
|
||||
if ($is_badge_surface) {
|
||||
$badges_js_path_global = get_template_directory() . '/assets/js/user-badges.js';
|
||||
if (file_exists($badges_js_path_global) && !wp_script_is('aksrt-user-badges', 'enqueued')) {
|
||||
wp_enqueue_script(
|
||||
'aksrt-user-badges',
|
||||
$base_uri . '/assets/js/user-badges.js',
|
||||
['aksrt-script', 'aksrt-core-remote-modal'],
|
||||
(string) filemtime($badges_js_path_global),
|
||||
true
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$this->enqueue_public_fonts_js_and_data($base_uri, $core_dep, $deps, $is_fixed_homepage, $is_home_or_archive, $is_single_post, $is_single_resource, $version);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -246,6 +246,40 @@ class Forum {
|
||||
$this->view_builder()->ajax_board_topics();
|
||||
}
|
||||
|
||||
public function register_widget_routes(): void {
|
||||
register_rest_route('aksrt/v1', '/forum/frontend/widgets/topics', [
|
||||
'methods' => 'GET',
|
||||
'callback' => [$this, 'rest_widget_topics'],
|
||||
'permission_callback' => '__return_true',
|
||||
]);
|
||||
}
|
||||
|
||||
public function rest_widget_topics(\WP_REST_Request $request) {
|
||||
if (!class_exists('\AKSRT\Widgets\WidgetForumTopicLists')) {
|
||||
return new \WP_REST_Response(['success' => false, 'message' => 'widget unavailable'], 500);
|
||||
}
|
||||
$instance = [
|
||||
'count' => max(1, min(30, (int) ($request->get_param('count') ?? 8))),
|
||||
'orderby' => sanitize_key((string) ($request->get_param('orderby') ?? 'date')),
|
||||
'style' => sanitize_key((string) ($request->get_param('style') ?? 'mini')),
|
||||
'plate_id' => max(0, (int) ($request->get_param('plate_id') ?? 0)),
|
||||
'paginate' => sanitize_key((string) ($request->get_param('paginate') ?? 'ajax')),
|
||||
];
|
||||
$page = max(1, (int) ($request->get_param('page') ?? $request->get_param('paged') ?? 1));
|
||||
$payload = \AKSRT\Widgets\WidgetForumTopicLists::query_payload($instance, $page);
|
||||
return new \WP_REST_Response([
|
||||
'success' => true,
|
||||
'html' => (string) ($payload['html'] ?? ''),
|
||||
'paginate_html' => (string) ($payload['paginate_html'] ?? ''),
|
||||
'has_more' => !empty($payload['has_more']),
|
||||
'page' => (int) ($payload['page'] ?? $page),
|
||||
'max_pages' => (int) ($payload['max_pages'] ?? 1),
|
||||
'total' => (int) ($payload['total'] ?? 0),
|
||||
], 200);
|
||||
}
|
||||
|
||||
|
||||
|
||||
public function render_board_cards(array $boards): void {
|
||||
$this->view_builder()->render_board_cards($boards);
|
||||
}
|
||||
@@ -396,6 +430,7 @@ class Forum {
|
||||
// Public plate/board topic list (sort tabs)
|
||||
add_action('wp_ajax_aksrt_forum_board_topics', [$this, 'ajax_board_topics']);
|
||||
add_action('wp_ajax_nopriv_aksrt_forum_board_topics', [$this, 'ajax_board_topics']);
|
||||
add_action('rest_api_init', [$this, 'register_widget_routes']);
|
||||
|
||||
add_action('admin_init', [$this, 'maybe_migrate_boards'], 25);
|
||||
add_filter('template_include', [$this, 'template_routing']);
|
||||
|
||||
@@ -25,6 +25,209 @@ class Badges {
|
||||
|
||||
public function init(): void {
|
||||
add_action('rest_api_init', [$this, 'register_routes']);
|
||||
add_action('aksrt_badge_granted', [$this, 'on_badge_granted_notify'], 10, 3);
|
||||
// HTML modal for remote-modal (zibll user_medal_info_modal).
|
||||
add_action('wp_ajax_aksrt_user_badge_info_modal', [$this, 'ajax_info_modal']);
|
||||
add_action('wp_ajax_nopriv_aksrt_user_badge_info_modal', [$this, 'ajax_info_modal']);
|
||||
add_action('wp_ajax_aksrt_single_badge_info_modal', [$this, 'ajax_single_info_modal']);
|
||||
add_action('wp_ajax_nopriv_aksrt_single_badge_info_modal', [$this, 'ajax_single_info_modal']);
|
||||
}
|
||||
|
||||
/**
|
||||
* Notify user when a badge is granted (zibll user_add_medal message).
|
||||
*/
|
||||
public function on_badge_granted_notify(int $user_id, string $slug, int $actor = 0): void {
|
||||
if ($user_id <= 0 || $slug === '') {
|
||||
return;
|
||||
}
|
||||
$map = self::get_dictionary_map();
|
||||
if (!isset($map[$slug])) {
|
||||
return;
|
||||
}
|
||||
$label = (string) ($map[$slug]['label'] ?? $slug);
|
||||
$title = sprintf(
|
||||
/* translators: %s badge name */
|
||||
__('恭喜您!获得新的徽章[%s]', 'aksrt'),
|
||||
$label
|
||||
);
|
||||
$content = sprintf(
|
||||
/* translators: %s badge name */
|
||||
__('恭喜您获得徽章「%s」', 'aksrt'),
|
||||
$label
|
||||
);
|
||||
if (class_exists('\AKSRT\Messages\Message')) {
|
||||
\AKSRT\Messages\Message::get_instance()->send($user_id, 'medal', $content, [
|
||||
'title' => $title,
|
||||
'target_url' => function_exists('aksrt_get_user_center_url')
|
||||
? aksrt_get_user_center_url()
|
||||
: home_url('/'),
|
||||
'slug' => $slug,
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* AJAX: full badge list modal HTML.
|
||||
*/
|
||||
public function ajax_info_modal(): void {
|
||||
$user_id = max(0, (int) ($_REQUEST['id'] ?? $_REQUEST['user_id'] ?? 0)); // phpcs:ignore WordPress.Security.NonceVerification.Recommended
|
||||
if ($user_id <= 0) {
|
||||
$user_id = get_current_user_id();
|
||||
}
|
||||
if ($user_id <= 0 || !get_userdata($user_id)) {
|
||||
wp_send_json_error(['message' => __('用户不存在', 'aksrt')], 404);
|
||||
}
|
||||
$html = self::render_info_modal_html($user_id);
|
||||
wp_send_json_success(['html' => $html]);
|
||||
}
|
||||
|
||||
/**
|
||||
* AJAX: single badge detail modal.
|
||||
*/
|
||||
public function ajax_single_info_modal(): void {
|
||||
$slug = sanitize_key((string) ($_REQUEST['name'] ?? $_REQUEST['slug'] ?? '')); // phpcs:ignore WordPress.Security.NonceVerification.Recommended
|
||||
$user_id = max(0, (int) ($_REQUEST['user_id'] ?? $_REQUEST['id'] ?? 0)); // phpcs:ignore WordPress.Security.NonceVerification.Recommended
|
||||
if ($slug === '') {
|
||||
wp_send_json_error(['message' => __('徽章不存在', 'aksrt')], 404);
|
||||
}
|
||||
$html = self::render_single_modal_html($slug, $user_id);
|
||||
wp_send_json_success(['html' => $html]);
|
||||
}
|
||||
|
||||
/**
|
||||
* zibll zib_get_user_medal_info_modal lite.
|
||||
*/
|
||||
public static function render_info_modal_html(int $user_id): string {
|
||||
$user = get_userdata($user_id);
|
||||
if (!$user) {
|
||||
return '<div class="aksrt-badge-modal"><p>' . esc_html__('用户不存在', 'aksrt') . '</p></div>';
|
||||
}
|
||||
$current = get_current_user_id();
|
||||
$is_self = $current > 0 && $current === $user_id;
|
||||
$display = $is_self ? __('我', 'aksrt') : $user->display_name;
|
||||
$avatar = function_exists('aksrt_get_avatar_url')
|
||||
? aksrt_get_avatar_url($user_id, ['size' => 64])
|
||||
: get_avatar_url($user_id, ['size' => 64]);
|
||||
$data = self::list_for_user($user_id, $is_self || self::can_manually_set($current));
|
||||
$items = is_array($data['items'] ?? null) ? $data['items'] : [];
|
||||
$wear = (string) ($data['wear'] ?? '');
|
||||
|
||||
$html = '<div class="aksrt-badge-modal">';
|
||||
$html .= '<div class="aksrt-badge-modal__head">';
|
||||
$html .= '<img class="aksrt-badge-modal__avatar" src="' . esc_url($avatar) . '" alt="" width="40" height="40" />';
|
||||
$html .= '<div class="aksrt-badge-modal__title">' . esc_html(sprintf(__('%s的徽章', 'aksrt'), $display));
|
||||
if ($wear !== '') {
|
||||
$map = self::get_dictionary_map();
|
||||
if (isset($map[$wear])) {
|
||||
$html .= ' <span class="aksrt-badge-modal__wear-tag">' . esc_html(sprintf(__('佩戴:%s', 'aksrt'), (string) $map[$wear]['label'])) . '</span>';
|
||||
}
|
||||
} else {
|
||||
$html .= ' <span class="badg badg-sm c-red">' . esc_html__('暂无徽章', 'aksrt') . '</span>';
|
||||
}
|
||||
$html .= '</div>';
|
||||
$html .= '<button type="button" class="aksrt-remote-modal__close" data-aksrt-remote-modal-close aria-label="' . esc_attr__('关闭', 'aksrt') . '">×</button>';
|
||||
$html .= '</div>';
|
||||
|
||||
$html .= '<div class="aksrt-badge-modal__list mini-scrollbar">';
|
||||
if (!$items) {
|
||||
$html .= '<div class="aksrt-badge-modal__empty">' . esc_html__('暂无徽章', 'aksrt') . '</div>';
|
||||
} else {
|
||||
foreach ($items as $item) {
|
||||
$slug = (string) ($item['slug'] ?? '');
|
||||
$label = (string) ($item['label'] ?? $slug);
|
||||
$icon = (string) ($item['icon_url'] ?? '');
|
||||
$is_wear = !empty($item['wear']);
|
||||
$html .= '<div class="aksrt-badge-modal__item' . ($is_wear ? ' is-wear' : '') . '" data-slug="' . esc_attr($slug) . '">';
|
||||
if ($icon !== '') {
|
||||
$html .= '<img class="medal-icon img-icon" src="' . esc_url($icon) . '" alt="" width="40" height="40" loading="lazy" />';
|
||||
} else {
|
||||
$html .= '<span class="aksrt-badge-modal__icon-fa"><i class="fas fa-award" aria-hidden="true"></i></span>';
|
||||
}
|
||||
$html .= '<div class="aksrt-badge-modal__meta"><div class="aksrt-badge-modal__name">' . esc_html($label) . '</div>';
|
||||
if ($is_wear) {
|
||||
$html .= '<div class="em09 c-blue">' . esc_html__('当前佩戴', 'aksrt') . '</div>';
|
||||
}
|
||||
$html .= '</div>';
|
||||
if ($is_self && !$is_wear) {
|
||||
$html .= '<button type="button" class="but jb-blue but-sm" data-aksrt-badge-wear="' . esc_attr($slug) . '">' . esc_html__('佩戴此徽章', 'aksrt') . '</button>';
|
||||
}
|
||||
$html .= '</div>';
|
||||
}
|
||||
}
|
||||
$html .= '</div></div>';
|
||||
return $html;
|
||||
}
|
||||
|
||||
/**
|
||||
* Single badge card modal.
|
||||
*/
|
||||
public static function render_single_modal_html(string $slug, int $user_id = 0): string {
|
||||
$map = self::get_dictionary_map();
|
||||
$slug = sanitize_key($slug);
|
||||
if ($slug === '' || !isset($map[$slug])) {
|
||||
return '<div class="aksrt-badge-modal"><p>' . esc_html__('徽章不存在', 'aksrt') . '</p></div>';
|
||||
}
|
||||
$item = $map[$slug];
|
||||
$current = get_current_user_id();
|
||||
$is_self = $user_id > 0 && $current === $user_id;
|
||||
$owned = $user_id > 0 ? self::get_owned($user_id) : [];
|
||||
$has = in_array($slug, $owned, true);
|
||||
$wear = $user_id > 0 ? self::get_wear($user_id) : '';
|
||||
$is_wear = $has && $wear === $slug;
|
||||
|
||||
$html = '<div class="aksrt-badge-modal aksrt-badge-modal--single">';
|
||||
$html .= '<button type="button" class="aksrt-remote-modal__close" data-aksrt-remote-modal-close aria-label="' . esc_attr__('关闭', 'aksrt') . '">×</button>';
|
||||
$html .= '<div class="aksrt-badge-modal__card">';
|
||||
if (!empty($item['icon_url'])) {
|
||||
$html .= '<img class="medal-icon img-icon" src="' . esc_url((string) $item['icon_url']) . '" alt="" width="72" height="72" />';
|
||||
} else {
|
||||
$html .= '<span class="aksrt-badge-modal__icon-fa lg"><i class="fas fa-award" aria-hidden="true"></i></span>';
|
||||
}
|
||||
$html .= '<div class="aksrt-badge-modal__name">' . esc_html((string) $item['label']) . '</div>';
|
||||
if ($is_self) {
|
||||
$html .= $has
|
||||
? '<div class="c-blue em09 mt6">' . esc_html__('已获得', 'aksrt') . '</div>'
|
||||
: '<div class="c-yellow em09 mt6">' . esc_html__('暂未获得', 'aksrt') . '</div>';
|
||||
}
|
||||
$html .= '</div>';
|
||||
if ($is_self && $has && !$is_wear) {
|
||||
$html .= '<div class="aksrt-badge-modal__actions"><button type="button" class="but jb-blue btn-block" data-aksrt-badge-wear="' . esc_attr($slug) . '">' . esc_html__('佩戴此徽章', 'aksrt') . '</button></div>';
|
||||
} elseif ($is_self && $is_wear) {
|
||||
$html .= '<div class="aksrt-badge-modal__actions"><span class="badg btn-block c-blue">' . esc_html__('当前佩戴此徽章', 'aksrt') . '</span></div>';
|
||||
}
|
||||
$html .= '</div>';
|
||||
return $html;
|
||||
}
|
||||
|
||||
/**
|
||||
* Remote-modal trigger for a user's badge list (zibll zib_get_user_medal_info_link).
|
||||
*/
|
||||
public static function get_info_link(int $user_id, string $class = '', ?string $text = null): string {
|
||||
if ($user_id <= 0 || !self::enabled()) {
|
||||
return '';
|
||||
}
|
||||
if ($text === null) {
|
||||
$text = __('徽章明细', 'aksrt');
|
||||
}
|
||||
if (!function_exists('aksrt_get_remote_modal_link')) {
|
||||
return '';
|
||||
}
|
||||
$url = add_query_arg(
|
||||
[
|
||||
'action' => 'aksrt_user_badge_info_modal',
|
||||
'id' => $user_id,
|
||||
],
|
||||
admin_url('admin-ajax.php')
|
||||
);
|
||||
return aksrt_get_remote_modal_link([
|
||||
'tag' => 'a',
|
||||
'class' => trim('user-medal-info ' . $class),
|
||||
'text' => $text,
|
||||
'url' => $url,
|
||||
'data_class' => 'full-sm',
|
||||
'height' => 240,
|
||||
'mobile_bottom' => true,
|
||||
]);
|
||||
}
|
||||
|
||||
public function register_routes(): void {
|
||||
|
||||
@@ -47,21 +47,25 @@ class SearchWidget extends \WP_Widget {
|
||||
}
|
||||
?>
|
||||
<div class="widget-search-box aksrt-search-widget">
|
||||
<form role="search" method="get" action="<?php echo esc_url(home_url('/')); ?>">
|
||||
<div class="widget-search-box__inner">
|
||||
<input type="search" name="s" class="widget-search-box__input" placeholder="<?php echo esc_attr($placeholder); ?>" value="<?php echo esc_attr(get_search_query()); ?>" autocomplete="off">
|
||||
<button type="submit" class="widget-search-box__btn" aria-label="<?php esc_attr_e('搜索', 'aksrt'); ?>">
|
||||
<i class="fas fa-search"></i>
|
||||
</button>
|
||||
</div>
|
||||
</form>
|
||||
<?php if ($show_history) : ?>
|
||||
<div class="aksrt-search-widget__history" data-aksrt-search-history>
|
||||
<div class="aksrt-search-widget__kw-title"><?php esc_html_e('搜索历史', 'aksrt'); ?></div>
|
||||
<div class="aksrt-search-widget__history-list" data-aksrt-search-history-list></div>
|
||||
</div>
|
||||
<?php endif; ?>
|
||||
<?php if ($keywords) : ?>
|
||||
<form role="search" method="get" action="<?php echo esc_url(home_url('/')); ?>" data-aksrt-search-form>
|
||||
<div class="widget-search-box__inner">
|
||||
<input type="search" name="s" class="widget-search-box__input" placeholder="<?php echo esc_attr($placeholder); ?>" value="<?php echo esc_attr(get_search_query()); ?>" autocomplete="off">
|
||||
<input type="hidden" name="aksrt_search_type" value="post">
|
||||
<button type="submit" class="widget-search-box__btn" aria-label="<?php esc_attr_e('搜索', 'aksrt'); ?>">
|
||||
<i class="fas fa-search"></i>
|
||||
</button>
|
||||
</div>
|
||||
</form>
|
||||
<?php if ($show_history) : ?>
|
||||
<div class="aksrt-search-widget__history" data-aksrt-search-history>
|
||||
<div class="aksrt-search-widget__kw-head">
|
||||
<div class="aksrt-search-widget__kw-title"><?php esc_html_e('搜索历史', 'aksrt'); ?></div>
|
||||
<button type="button" class="aksrt-search-widget__history-clear" data-aksrt-search-history-clear hidden><?php esc_html_e('清空', 'aksrt'); ?></button>
|
||||
</div>
|
||||
<div class="aksrt-search-widget__history-list" data-aksrt-search-history-list></div>
|
||||
</div>
|
||||
<?php endif; ?>
|
||||
<?php if ($keywords) : ?>
|
||||
<div class="aksrt-search-widget__keywords">
|
||||
<?php if ($kw_title !== '') : ?>
|
||||
<div class="aksrt-search-widget__kw-title"><?php echo esc_html($kw_title); ?></div>
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
<?php
|
||||
/**
|
||||
* Multi-tab forum topic lists (zibll zib_bbs_widget_ui_tab_posts lite).
|
||||
* Multi-tab forum topic lists (zibll zib_bbs_widget_ui_tab_posts lite + lazy REST).
|
||||
*
|
||||
* @package AKSRT
|
||||
*/
|
||||
@@ -16,7 +16,7 @@ class WidgetForumTabTopics extends \WP_Widget {
|
||||
'aksrt_forum_tab_topics',
|
||||
esc_html__('论坛 - 多栏目帖子', 'aksrt'),
|
||||
[
|
||||
'description' => esc_html__('热门/最新/精华等栏目切换的帖子列表。', 'aksrt'),
|
||||
'description' => esc_html__('热门/最新/精华等栏目切换的帖子列表(懒加载)。', 'aksrt'),
|
||||
'classname' => 'widget_aksrt_forum_tab_topics',
|
||||
]
|
||||
);
|
||||
@@ -36,6 +36,7 @@ class WidgetForumTabTopics extends \WP_Widget {
|
||||
}
|
||||
$count = max(1, min(20, (int) ($instance['count'] ?? 6)));
|
||||
$uid = 'aksrt-ftab-' . esc_attr($this->id);
|
||||
$rest = rest_url('aksrt/v1/forum/frontend/widgets/topics');
|
||||
|
||||
echo $args['before_widget']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
|
||||
$title = apply_filters('widget_title', (string) ($instance['title'] ?? ''), $instance, $this->id_base);
|
||||
@@ -43,32 +44,60 @@ class WidgetForumTabTopics extends \WP_Widget {
|
||||
echo $args['before_title'] . esc_html($title) . $args['after_title']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
|
||||
}
|
||||
|
||||
echo '<div class="aksrt-ftab" id="' . $uid . '">';
|
||||
echo '<div class="aksrt-ftab" id="' . $uid . '" data-rest="' . esc_url($rest) . '" data-count="' . esc_attr((string) $count) . '">';
|
||||
echo '<ul class="aksrt-ftab__nav" role="tablist">';
|
||||
foreach ($tabs as $i => $tab) {
|
||||
$cls = $i === 0 ? ' is-active' : '';
|
||||
echo '<li><button type="button" class="aksrt-ftab__btn' . esc_attr($cls) . '" data-index="' . (int) $i . '">' . esc_html($tab['title']) . '</button></li>';
|
||||
echo '<li><button type="button" class="aksrt-ftab__btn' . esc_attr($cls) . '" data-index="' . (int) $i . '" data-orderby="' . esc_attr($tab['orderby']) . '">' . esc_html($tab['title']) . '</button></li>';
|
||||
}
|
||||
echo '</ul>';
|
||||
|
||||
foreach ($tabs as $i => $tab) {
|
||||
$cls = $i === 0 ? ' is-active' : '';
|
||||
echo '<div class="aksrt-ftab__panel' . esc_attr($cls) . '" data-index="' . (int) $i . '">';
|
||||
$this->render_list($tab, $count);
|
||||
$loaded = $i === 0 ? '1' : '0';
|
||||
echo '<div class="aksrt-ftab__panel' . esc_attr($cls) . '" data-index="' . (int) $i . '" data-loaded="' . esc_attr($loaded) . '">';
|
||||
if ($i === 0) {
|
||||
$payload = WidgetForumTopicLists::query_payload([
|
||||
'count' => $count,
|
||||
'orderby' => $tab['orderby'],
|
||||
'style' => 'mini',
|
||||
'paginate' => 'none',
|
||||
], 1);
|
||||
echo $payload['html']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
|
||||
} else {
|
||||
echo '<div class="post_ajax_loader" data-aksrt-ajax-loader></div>';
|
||||
}
|
||||
echo '</div>';
|
||||
}
|
||||
echo '</div>';
|
||||
|
||||
?>
|
||||
<script>
|
||||
(function(){
|
||||
var root=document.getElementById(<?php echo wp_json_encode($uid); ?>);
|
||||
if(!root||root._aksrtFtab)return; root._aksrtFtab=1;
|
||||
var rest=root.getAttribute('data-rest')||'';
|
||||
var count=root.getAttribute('data-count')||'6';
|
||||
var btns=[].slice.call(root.querySelectorAll('.aksrt-ftab__btn'));
|
||||
var panels=[].slice.call(root.querySelectorAll('.aksrt-ftab__panel'));
|
||||
function load(panel, orderby){
|
||||
if(!panel||panel.getAttribute('data-loaded')==='1'||!rest)return;
|
||||
var qs=new URLSearchParams({page:'1',count:count,orderby:orderby||'date',style:'mini',paginate:'none'});
|
||||
var url=rest+(rest.indexOf('?')>-1?'&':'?')+qs.toString();
|
||||
panel.classList.add('is-ajax-loading');
|
||||
fetch(url,{credentials:'same-origin',headers:{Accept:'application/json','X-WP-Nonce':(window.aksrtData&&aksrtData.restNonce)||''}})
|
||||
.then(function(r){return r.json()}).then(function(j){
|
||||
var d=(j&&j.data)||j||{};
|
||||
var html=d.html||'';
|
||||
panel.innerHTML=html||'<p class="aksrt-ftab__empty">暂无内容</p>';
|
||||
panel.setAttribute('data-loaded','1');
|
||||
panel.classList.remove('is-ajax-loading');
|
||||
}).catch(function(){panel.classList.remove('is-ajax-loading');});
|
||||
}
|
||||
function go(i){
|
||||
btns.forEach(function(b,idx){b.classList.toggle('is-active',idx===i);});
|
||||
panels.forEach(function(p,idx){p.classList.toggle('is-active',idx===i);});
|
||||
var btn=btns[i], panel=panels[i];
|
||||
if(btn&&panel) load(panel, btn.getAttribute('data-orderby')||'date');
|
||||
}
|
||||
btns.forEach(function(b){ b.addEventListener('click', function(){ go(parseInt(b.getAttribute('data-index')||'0',10)); }); });
|
||||
})();
|
||||
@@ -78,105 +107,72 @@ class WidgetForumTabTopics extends \WP_Widget {
|
||||
echo $args['after_widget']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array{title:string,orderby:string} $tab
|
||||
*/
|
||||
private function render_list(array $tab, int $count): void {
|
||||
$query = [
|
||||
'post_type' => 'aksrt_forum_topic',
|
||||
'post_status' => 'publish',
|
||||
'posts_per_page' => $count,
|
||||
'order' => 'DESC',
|
||||
];
|
||||
switch ($tab['orderby']) {
|
||||
case 'views':
|
||||
$query['meta_key'] = '_aksrt_views';
|
||||
$query['orderby'] = 'meta_value_num';
|
||||
break;
|
||||
case 'comments':
|
||||
$query['orderby'] = 'comment_count';
|
||||
break;
|
||||
case 'essence':
|
||||
$query['meta_query'] = [[
|
||||
'key' => '_aksrt_essence',
|
||||
'value' => '1',
|
||||
]];
|
||||
$query['orderby'] = 'date';
|
||||
break;
|
||||
default:
|
||||
$query['orderby'] = 'date';
|
||||
}
|
||||
$q = new \WP_Query($query);
|
||||
if (!$q->have_posts()) {
|
||||
echo '<p class="aksrt-ftab__empty">' . esc_html__('暂无内容', 'aksrt') . '</p>';
|
||||
wp_reset_postdata();
|
||||
return;
|
||||
}
|
||||
echo '<ul class="aksrt-ftopic-list is-mini">';
|
||||
while ($q->have_posts()) {
|
||||
$q->the_post();
|
||||
$views = (int) get_post_meta(get_the_ID(), '_aksrt_views', true);
|
||||
echo '<li class="aksrt-ftopic-list__item">';
|
||||
echo '<a class="aksrt-ftopic-list__link" href="' . esc_url(get_permalink()) . '">' . esc_html(get_the_title()) . '</a>';
|
||||
echo '<span class="aksrt-ftopic-list__views">' . esc_html((string) $views) . '</span>';
|
||||
echo '</li>';
|
||||
}
|
||||
echo '</ul>';
|
||||
wp_reset_postdata();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param mixed $raw
|
||||
* @return array<int, array{title:string,orderby:string}>
|
||||
* @return list<array{title:string,orderby:string}>
|
||||
*/
|
||||
private function normalize_tabs($raw): array {
|
||||
if (is_string($raw)) {
|
||||
$decoded = json_decode($raw, true);
|
||||
$raw = is_array($decoded) ? $decoded : [];
|
||||
}
|
||||
if (!is_array($raw)) {
|
||||
return [];
|
||||
}
|
||||
$out = [];
|
||||
foreach ($raw as $row) {
|
||||
if (!is_array($row)) {
|
||||
continue;
|
||||
if (is_string($raw)) {
|
||||
foreach (array_filter(explode("\n", $raw)) as $line) {
|
||||
$parts = array_map('trim', explode('|', $line));
|
||||
if ($parts[0] === '') {
|
||||
continue;
|
||||
}
|
||||
$out[] = [
|
||||
'title' => $parts[0],
|
||||
'orderby' => sanitize_key($parts[1] ?? 'date'),
|
||||
];
|
||||
}
|
||||
$title = sanitize_text_field((string) ($row['title'] ?? ''));
|
||||
if ($title === '') {
|
||||
continue;
|
||||
}
|
||||
$orderby = sanitize_key((string) ($row['orderby'] ?? 'date'));
|
||||
if (!in_array($orderby, ['date', 'views', 'comments', 'essence'], true)) {
|
||||
$orderby = 'date';
|
||||
}
|
||||
$out[] = ['title' => $title, 'orderby' => $orderby];
|
||||
if (count($out) >= 6) {
|
||||
break;
|
||||
return $out;
|
||||
}
|
||||
if (is_array($raw)) {
|
||||
foreach ($raw as $tab) {
|
||||
if (!is_array($tab)) {
|
||||
continue;
|
||||
}
|
||||
$title = trim((string) ($tab['title'] ?? ''));
|
||||
if ($title === '') {
|
||||
continue;
|
||||
}
|
||||
$out[] = [
|
||||
'title' => $title,
|
||||
'orderby' => sanitize_key((string) ($tab['orderby'] ?? 'date')),
|
||||
];
|
||||
}
|
||||
}
|
||||
return $out;
|
||||
}
|
||||
|
||||
public function form($instance): void {
|
||||
$tabs = is_string($instance['tabs'] ?? null)
|
||||
? (string) $instance['tabs']
|
||||
: wp_json_encode($this->normalize_tabs($instance['tabs'] ?? []), JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
|
||||
$defaults = [
|
||||
'title' => __('论坛栏目', 'aksrt'),
|
||||
'count' => 6,
|
||||
'tabs' => "最新|date\n热门|views\n精华|essence",
|
||||
];
|
||||
$instance = wp_parse_args($instance, $defaults);
|
||||
$tabs_val = $instance['tabs'];
|
||||
if (is_array($tabs_val)) {
|
||||
$lines = [];
|
||||
foreach ($tabs_val as $t) {
|
||||
$lines[] = ($t['title'] ?? '') . '|' . ($t['orderby'] ?? 'date');
|
||||
}
|
||||
$tabs_val = implode("\n", $lines);
|
||||
}
|
||||
?>
|
||||
<p>
|
||||
<label><?php esc_html_e('标题', 'aksrt'); ?>
|
||||
<input class="widefat" type="text" name="<?php echo esc_attr($this->get_field_name('title')); ?>" value="<?php echo esc_attr((string) ($instance['title'] ?? '')); ?>">
|
||||
<input class="widefat" type="text" name="<?php echo esc_attr($this->get_field_name('title')); ?>" value="<?php echo esc_attr((string) $instance['title']); ?>">
|
||||
</label>
|
||||
</p>
|
||||
<p>
|
||||
<label><?php esc_html_e('栏目 JSON', 'aksrt'); ?>
|
||||
<textarea class="widefat" rows="5" name="<?php echo esc_attr($this->get_field_name('tabs')); ?>" placeholder='[{"title":"最新","orderby":"date"},{"title":"热门","orderby":"views"},{"title":"精华","orderby":"essence"}]'><?php echo esc_textarea((string) $tabs); ?></textarea>
|
||||
</label>
|
||||
<small><?php esc_html_e('orderby: date|views|comments|essence', 'aksrt'); ?></small>
|
||||
</p>
|
||||
<p>
|
||||
<label><?php esc_html_e('每栏数量', 'aksrt'); ?>
|
||||
<input class="tiny-text" type="number" min="1" max="20" name="<?php echo esc_attr($this->get_field_name('count')); ?>" value="<?php echo esc_attr((string) ((int) ($instance['count'] ?? 6))); ?>">
|
||||
<input class="tiny-text" type="number" min="1" max="20" name="<?php echo esc_attr($this->get_field_name('count')); ?>" value="<?php echo esc_attr((string) $instance['count']); ?>">
|
||||
</label>
|
||||
</p>
|
||||
<p>
|
||||
<label><?php esc_html_e('栏目(每行: 标题|排序 date/views/essence/comments)', 'aksrt'); ?>
|
||||
<textarea class="widefat" rows="4" name="<?php echo esc_attr($this->get_field_name('tabs')); ?>"><?php echo esc_textarea((string) $tabs_val); ?></textarea>
|
||||
</label>
|
||||
</p>
|
||||
<?php
|
||||
@@ -185,8 +181,8 @@ class WidgetForumTabTopics extends \WP_Widget {
|
||||
public function update($new_instance, $old_instance): array {
|
||||
return [
|
||||
'title' => sanitize_text_field((string) ($new_instance['title'] ?? '')),
|
||||
'tabs' => $this->normalize_tabs($new_instance['tabs'] ?? []),
|
||||
'count' => max(1, min(20, (int) ($new_instance['count'] ?? 6))),
|
||||
'tabs' => sanitize_textarea_field((string) ($new_instance['tabs'] ?? '')),
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
<?php
|
||||
/**
|
||||
* Forum topic list widget (zibll zib_bbs_widget_ui_posts_lists lite).
|
||||
* Forum topic list widget (zibll zib_bbs_widget_ui_posts_lists lite + ajax pager).
|
||||
*
|
||||
* @package AKSRT
|
||||
*/
|
||||
@@ -16,35 +16,27 @@ class WidgetForumTopicLists extends \WP_Widget {
|
||||
'aksrt_forum_topic_lists',
|
||||
esc_html__('论坛 - 帖子列表', 'aksrt'),
|
||||
[
|
||||
'description' => esc_html__('按排序/版块筛选展示论坛帖子。', 'aksrt'),
|
||||
'description' => esc_html__('按排序/版块筛选展示论坛帖子(支持 AJAX 加载更多)。', 'aksrt'),
|
||||
'classname' => 'widget_aksrt_forum_topic_lists',
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
public function widget($args, $instance): void {
|
||||
if (!post_type_exists('aksrt_forum_topic')) {
|
||||
return;
|
||||
}
|
||||
|
||||
$count = max(1, min(30, (int) ($instance['count'] ?? 8)));
|
||||
$orderby = sanitize_key((string) ($instance['orderby'] ?? 'date'));
|
||||
$style = sanitize_key((string) ($instance['style'] ?? 'mini'));
|
||||
/**
|
||||
* @param array $instance
|
||||
* @param int $paged
|
||||
* @return array
|
||||
*/
|
||||
public static function build_query_args(array $instance, int $paged = 1): array {
|
||||
$count = max(1, min(30, (int) ($instance['count'] ?? 8)));
|
||||
$orderby = sanitize_key((string) ($instance['orderby'] ?? 'date'));
|
||||
$plate_id = (int) ($instance['plate_id'] ?? 0);
|
||||
$current_plate = !empty($instance['current_plate']);
|
||||
|
||||
if ($current_plate) {
|
||||
$resolved = $this->resolve_current_plate_id();
|
||||
if ($resolved <= 0) {
|
||||
return;
|
||||
}
|
||||
$plate_id = $resolved;
|
||||
}
|
||||
|
||||
$query = [
|
||||
'post_type' => 'aksrt_forum_topic',
|
||||
'post_status' => 'publish',
|
||||
'posts_per_page' => $count,
|
||||
'paged' => max(1, $paged),
|
||||
'order' => 'DESC',
|
||||
];
|
||||
|
||||
@@ -59,35 +51,41 @@ class WidgetForumTopicLists extends \WP_Widget {
|
||||
case 'modified':
|
||||
$query['orderby'] = 'modified';
|
||||
break;
|
||||
case 'essence':
|
||||
$query['meta_query'] = [[
|
||||
'key' => '_aksrt_essence',
|
||||
'value' => '1',
|
||||
]];
|
||||
$query['orderby'] = 'date';
|
||||
break;
|
||||
default:
|
||||
$query['orderby'] = 'date';
|
||||
}
|
||||
|
||||
if ($plate_id > 0) {
|
||||
$query['meta_query'] = [[
|
||||
'key' => '_aksrt_plate_id',
|
||||
'value' => $plate_id,
|
||||
]];
|
||||
$query['meta_query'] = array_values(array_filter(array_merge(
|
||||
isset($query['meta_query']) ? $query['meta_query'] : [],
|
||||
[[
|
||||
'key' => '_aksrt_plate_id',
|
||||
'value' => $plate_id,
|
||||
]]
|
||||
)));
|
||||
}
|
||||
|
||||
$q = new \WP_Query($query);
|
||||
return $query;
|
||||
}
|
||||
|
||||
public static function render_items_html(\WP_Query $q, string $style = 'mini'): string {
|
||||
if (!$q->have_posts()) {
|
||||
wp_reset_postdata();
|
||||
return;
|
||||
return '';
|
||||
}
|
||||
|
||||
echo $args['before_widget']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
|
||||
$title = apply_filters('widget_title', (string) ($instance['title'] ?? ''), $instance, $this->id_base);
|
||||
if ($title !== '') {
|
||||
echo $args['before_title'] . esc_html($title) . $args['after_title']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
|
||||
}
|
||||
|
||||
$list_class = $style === 'detail' ? 'aksrt-ftopic-list is-detail' : 'aksrt-ftopic-list is-mini';
|
||||
ob_start();
|
||||
echo '<ul class="' . esc_attr($list_class) . '">';
|
||||
while ($q->have_posts()) {
|
||||
$q->the_post();
|
||||
$views = (int) get_post_meta(get_the_ID(), '_aksrt_views', true);
|
||||
echo '<li class="aksrt-ftopic-list__item">';
|
||||
echo '<li class="aksrt-ftopic-list__item ajax-item">';
|
||||
echo '<a class="aksrt-ftopic-list__link" href="' . esc_url(get_permalink()) . '">' . esc_html(get_the_title()) . '</a>';
|
||||
if ($style === 'detail') {
|
||||
echo '<div class="aksrt-ftopic-list__meta">';
|
||||
@@ -102,6 +100,96 @@ class WidgetForumTopicLists extends \WP_Widget {
|
||||
}
|
||||
echo '</ul>';
|
||||
wp_reset_postdata();
|
||||
return (string) ob_get_clean();
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array{html:string,paginate_html:string,has_more:bool,page:int,max_pages:int,total:int}
|
||||
*/
|
||||
public static function query_payload(array $instance, int $page = 1): array {
|
||||
$page = max(1, $page);
|
||||
$style = sanitize_key((string) ($instance['style'] ?? 'mini'));
|
||||
$paginate = sanitize_key((string) ($instance['paginate'] ?? 'ajax'));
|
||||
$q = new \WP_Query(self::build_query_args($instance, $page));
|
||||
$html = self::render_items_html($q, $style);
|
||||
if ($html === '' && $page === 1) {
|
||||
$html = function_exists('aksrt_get_ajax_null')
|
||||
? aksrt_get_ajax_null(__('暂无内容', 'aksrt'))
|
||||
: '<p class="aksrt-ftopic-list__empty ajax-item">' . esc_html__('暂无内容', 'aksrt') . '</p>';
|
||||
}
|
||||
$max_pages = max(1, (int) $q->max_num_pages);
|
||||
$total = (int) $q->found_posts;
|
||||
$has_more = $page < $max_pages;
|
||||
$paginate_html = '';
|
||||
if ($paginate === 'ajax' && $has_more) {
|
||||
$next = add_query_arg(
|
||||
array_filter([
|
||||
'page' => $page + 1,
|
||||
'count' => max(1, min(30, (int) ($instance['count'] ?? 8))),
|
||||
'orderby' => sanitize_key((string) ($instance['orderby'] ?? 'date')),
|
||||
'style' => $style,
|
||||
'plate_id' => (int) ($instance['plate_id'] ?? 0),
|
||||
'paginate' => 'ajax',
|
||||
]),
|
||||
rest_url('aksrt/v1/forum/frontend/widgets/topics')
|
||||
);
|
||||
$paginate_html = function_exists('aksrt_get_ajax_next_paginate')
|
||||
? aksrt_get_ajax_next_paginate($total, $page, max(1, (int) ($instance['count'] ?? 8)), $next)
|
||||
: '<div class="text-center theme-pagination ajax-pag"><div class="next-page ajax-next"><a href="' . esc_url($next) . '">' . esc_html__('加载更多', 'aksrt') . '</a></div></div>';
|
||||
} else {
|
||||
$paginate_html = '<div class="ajax-pag hide"><div class="next-page ajax-next"><a href="#"></a></div></div>';
|
||||
}
|
||||
return [
|
||||
'html' => $html,
|
||||
'paginate_html' => $paginate_html,
|
||||
'has_more' => $has_more,
|
||||
'page' => $page,
|
||||
'max_pages' => $max_pages,
|
||||
'total' => $total,
|
||||
];
|
||||
}
|
||||
|
||||
public function widget($args, $instance): void {
|
||||
if (!post_type_exists('aksrt_forum_topic')) {
|
||||
return;
|
||||
}
|
||||
|
||||
$count = max(1, min(30, (int) ($instance['count'] ?? 8)));
|
||||
$orderby = sanitize_key((string) ($instance['orderby'] ?? 'date'));
|
||||
$style = sanitize_key((string) ($instance['style'] ?? 'mini'));
|
||||
$plate_id = (int) ($instance['plate_id'] ?? 0);
|
||||
$current_plate = !empty($instance['current_plate']);
|
||||
$paginate = sanitize_key((string) ($instance['paginate'] ?? 'ajax'));
|
||||
|
||||
if ($current_plate) {
|
||||
$resolved = $this->resolve_current_plate_id();
|
||||
if ($resolved <= 0) {
|
||||
return;
|
||||
}
|
||||
$plate_id = $resolved;
|
||||
}
|
||||
|
||||
$instance['count'] = $count;
|
||||
$instance['orderby'] = $orderby;
|
||||
$instance['style'] = $style;
|
||||
$instance['plate_id'] = $plate_id;
|
||||
$instance['paginate'] = $paginate;
|
||||
|
||||
$payload = self::query_payload($instance, 1);
|
||||
$rest = rest_url('aksrt/v1/forum/frontend/widgets/topics');
|
||||
|
||||
echo $args['before_widget']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
|
||||
$title = apply_filters('widget_title', (string) ($instance['title'] ?? ''), $instance, $this->id_base);
|
||||
if ($title !== '') {
|
||||
echo $args['before_title'] . esc_html($title) . $args['after_title']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
|
||||
}
|
||||
|
||||
echo '<div class="aksrt-forum-topic-widget" data-aksrt-forum-topic-widget>';
|
||||
echo '<div class="ajaxpager" data-aksrt-ajaxpager data-aksrt-ajax-url="' . esc_url($rest) . '">';
|
||||
echo $payload['html']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
|
||||
echo $payload['paginate_html']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
|
||||
echo '<div class="post_ajax_loader" data-aksrt-ajax-loader data-aksrt-ajax-loader-hide hidden></div>';
|
||||
echo '</div></div>';
|
||||
|
||||
echo $args['after_widget']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
|
||||
}
|
||||
@@ -119,58 +207,74 @@ class WidgetForumTopicLists extends \WP_Widget {
|
||||
}
|
||||
|
||||
public function form($instance): void {
|
||||
$defaults = [
|
||||
'title' => __('帖子列表', 'aksrt'),
|
||||
'count' => 8,
|
||||
'orderby' => 'date',
|
||||
'style' => 'mini',
|
||||
'plate_id' => 0,
|
||||
'current_plate' => 0,
|
||||
'paginate' => 'ajax',
|
||||
];
|
||||
$instance = wp_parse_args($instance, $defaults);
|
||||
?>
|
||||
<p>
|
||||
<label><?php esc_html_e('标题', 'aksrt'); ?>
|
||||
<input class="widefat" type="text" name="<?php echo esc_attr($this->get_field_name('title')); ?>" value="<?php echo esc_attr((string) ($instance['title'] ?? __('最新帖子', 'aksrt'))); ?>">
|
||||
<input class="widefat" type="text" name="<?php echo esc_attr($this->get_field_name('title')); ?>" value="<?php echo esc_attr((string) $instance['title']); ?>">
|
||||
</label>
|
||||
</p>
|
||||
<p>
|
||||
<label><?php esc_html_e('数量', 'aksrt'); ?>
|
||||
<input class="tiny-text" type="number" min="1" max="30" name="<?php echo esc_attr($this->get_field_name('count')); ?>" value="<?php echo esc_attr((string) ((int) ($instance['count'] ?? 8))); ?>">
|
||||
<input class="tiny-text" type="number" min="1" max="30" name="<?php echo esc_attr($this->get_field_name('count')); ?>" value="<?php echo esc_attr((string) $instance['count']); ?>">
|
||||
</label>
|
||||
</p>
|
||||
<p>
|
||||
<label><?php esc_html_e('排序', 'aksrt'); ?>
|
||||
<select name="<?php echo esc_attr($this->get_field_name('orderby')); ?>">
|
||||
<option value="date" <?php selected(($instance['orderby'] ?? 'date'), 'date'); ?>><?php esc_html_e('发布时间', 'aksrt'); ?></option>
|
||||
<option value="views" <?php selected(($instance['orderby'] ?? ''), 'views'); ?>><?php esc_html_e('浏览量', 'aksrt'); ?></option>
|
||||
<option value="comments" <?php selected(($instance['orderby'] ?? ''), 'comments'); ?>><?php esc_html_e('评论数', 'aksrt'); ?></option>
|
||||
<option value="modified" <?php selected(($instance['orderby'] ?? ''), 'modified'); ?>><?php esc_html_e('更新时间', 'aksrt'); ?></option>
|
||||
<select class="widefat" name="<?php echo esc_attr($this->get_field_name('orderby')); ?>">
|
||||
<option value="date" <?php selected($instance['orderby'], 'date'); ?>><?php esc_html_e('最新', 'aksrt'); ?></option>
|
||||
<option value="views" <?php selected($instance['orderby'], 'views'); ?>><?php esc_html_e('热门', 'aksrt'); ?></option>
|
||||
<option value="comments" <?php selected($instance['orderby'], 'comments'); ?>><?php esc_html_e('评论', 'aksrt'); ?></option>
|
||||
<option value="essence" <?php selected($instance['orderby'], 'essence'); ?>><?php esc_html_e('精华', 'aksrt'); ?></option>
|
||||
<option value="modified" <?php selected($instance['orderby'], 'modified'); ?>><?php esc_html_e('更新', 'aksrt'); ?></option>
|
||||
</select>
|
||||
</label>
|
||||
</p>
|
||||
<p>
|
||||
<label><?php esc_html_e('样式', 'aksrt'); ?>
|
||||
<select name="<?php echo esc_attr($this->get_field_name('style')); ?>">
|
||||
<option value="mini" <?php selected(($instance['style'] ?? 'mini'), 'mini'); ?>><?php esc_html_e('紧凑', 'aksrt'); ?></option>
|
||||
<option value="detail" <?php selected(($instance['style'] ?? ''), 'detail'); ?>><?php esc_html_e('详情', 'aksrt'); ?></option>
|
||||
<select class="widefat" name="<?php echo esc_attr($this->get_field_name('style')); ?>">
|
||||
<option value="mini" <?php selected($instance['style'], 'mini'); ?>><?php esc_html_e('迷你', 'aksrt'); ?></option>
|
||||
<option value="detail" <?php selected($instance['style'], 'detail'); ?>><?php esc_html_e('详情', 'aksrt'); ?></option>
|
||||
</select>
|
||||
</label>
|
||||
</p>
|
||||
<p>
|
||||
<label><?php esc_html_e('限定版块 ID(0=全部)', 'aksrt'); ?>
|
||||
<input class="tiny-text" type="number" min="0" name="<?php echo esc_attr($this->get_field_name('plate_id')); ?>" value="<?php echo esc_attr((string) ((int) ($instance['plate_id'] ?? 0))); ?>">
|
||||
<label><?php esc_html_e('版块 ID', 'aksrt'); ?>
|
||||
<input class="tiny-text" type="number" min="0" name="<?php echo esc_attr($this->get_field_name('plate_id')); ?>" value="<?php echo esc_attr((string) $instance['plate_id']); ?>">
|
||||
</label>
|
||||
</p>
|
||||
<p>
|
||||
<label><input type="checkbox" name="<?php echo esc_attr($this->get_field_name('current_plate')); ?>" value="1" <?php checked(!empty($instance['current_plate'])); ?>> <?php esc_html_e('仅当前版块(版块/帖子页)', 'aksrt'); ?></label>
|
||||
<label><input type="checkbox" name="<?php echo esc_attr($this->get_field_name('current_plate')); ?>" value="1" <?php checked(!empty($instance['current_plate'])); ?>> <?php esc_html_e('跟随当前版块', 'aksrt'); ?></label>
|
||||
</p>
|
||||
<p>
|
||||
<label><?php esc_html_e('分页', 'aksrt'); ?>
|
||||
<select class="widefat" name="<?php echo esc_attr($this->get_field_name('paginate')); ?>">
|
||||
<option value="ajax" <?php selected($instance['paginate'], 'ajax'); ?>><?php esc_html_e('AJAX', 'aksrt'); ?></option>
|
||||
<option value="none" <?php selected($instance['paginate'], 'none'); ?>><?php esc_html_e('无', 'aksrt'); ?></option>
|
||||
</select>
|
||||
</label>
|
||||
</p>
|
||||
<?php
|
||||
}
|
||||
|
||||
public function update($new_instance, $old_instance): array {
|
||||
$orderby = sanitize_key((string) ($new_instance['orderby'] ?? 'date'));
|
||||
if (!in_array($orderby, ['date', 'views', 'comments', 'modified'], true)) {
|
||||
$orderby = 'date';
|
||||
}
|
||||
return [
|
||||
'title' => sanitize_text_field((string) ($new_instance['title'] ?? '')),
|
||||
'count' => max(1, min(30, (int) ($new_instance['count'] ?? 8))),
|
||||
'orderby' => $orderby,
|
||||
'style' => ($new_instance['style'] ?? '') === 'detail' ? 'detail' : 'mini',
|
||||
'orderby' => sanitize_key((string) ($new_instance['orderby'] ?? 'date')),
|
||||
'style' => sanitize_key((string) ($new_instance['style'] ?? 'mini')),
|
||||
'plate_id' => max(0, (int) ($new_instance['plate_id'] ?? 0)),
|
||||
'current_plate' => !empty($new_instance['current_plate']) ? 1 : 0,
|
||||
'paginate' => sanitize_key((string) ($new_instance['paginate'] ?? 'ajax')),
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
@@ -8,225 +8,330 @@ namespace AKSRT\Widgets;
|
||||
defined('ABSPATH') || exit;
|
||||
|
||||
/**
|
||||
* Shop product widgets (§6.9)
|
||||
*
|
||||
* - WidgetProductList: product list widget (filter/sort/paginate/AJAX)
|
||||
* - WidgetProductOneline: horizontal swiper product widget
|
||||
* - WidgetProductTab: multi-tab product list widget
|
||||
*
|
||||
* @package AKSRT
|
||||
*/
|
||||
|
||||
defined('ABSPATH') || exit;
|
||||
|
||||
/**
|
||||
* Product list widget — equivalent to zibll `zib_shop_widget_ui_product_lists`.
|
||||
* Shop product list widget — zibll `zib_shop_widget_ui_product_lists` lite.
|
||||
*
|
||||
* Supports: include/exclude category, discount, tag; sort order; list style;
|
||||
* AJAX pagination; per-page count.
|
||||
* AJAX load-more (via rest widget list endpoint); per-page count.
|
||||
*/
|
||||
class WidgetProductList extends \WP_Widget {
|
||||
|
||||
public function __construct() {
|
||||
parent::__construct(
|
||||
'aksrt_product_list',
|
||||
__('商品列表', 'aksrt'),
|
||||
['description' => __('AKSRT 商品列表组件:筛选、排序、AJAX 翻页。', 'aksrt')]
|
||||
);
|
||||
}
|
||||
public function __construct() {
|
||||
parent::__construct(
|
||||
'aksrt_product_list',
|
||||
__('商品列表', 'aksrt'),
|
||||
['description' => __('AKSRT 商品列表组件:筛选、排序、AJAX 翻页。', 'aksrt')]
|
||||
);
|
||||
}
|
||||
|
||||
public function widget($args, $instance): void {
|
||||
$title = apply_filters('widget_title', $instance['title'] ?? '');
|
||||
$per_page = max(1, min(50, (int) ($instance['per_page'] ?? 8)));
|
||||
$orderby = sanitize_key($instance['orderby'] ?? 'date');
|
||||
$order = in_array(strtoupper($instance['order'] ?? ''), ['ASC', 'DESC'], true) ? strtoupper($instance['order']) : 'DESC';
|
||||
$cat_ids = array_filter(array_map('intval', explode(',', (string) ($instance['cat_ids'] ?? ''))));
|
||||
$tag_ids = array_filter(array_map('intval', explode(',', (string) ($instance['tag_ids'] ?? ''))));
|
||||
$discount_ids = array_filter(array_map('intval', explode(',', (string) ($instance['discount_ids'] ?? ''))));
|
||||
$exclude_cat = !empty($instance['exclude_cat']);
|
||||
$style = sanitize_key($instance['style'] ?? 'default');
|
||||
$paged = max(1, (int) ($_GET['aksrt_product_page'] ?? 1));
|
||||
/**
|
||||
* Build WP_Query args from instance + page.
|
||||
*
|
||||
* @param array $instance
|
||||
* @param int $paged
|
||||
* @return array
|
||||
*/
|
||||
public static function build_query_args(array $instance, int $paged = 1): array {
|
||||
$per_page = max(1, min(50, (int) ($instance['per_page'] ?? 8)));
|
||||
$orderby = sanitize_key($instance['orderby'] ?? 'date');
|
||||
$order = in_array(strtoupper((string) ($instance['order'] ?? '')), ['ASC', 'DESC'], true)
|
||||
? strtoupper((string) $instance['order'])
|
||||
: 'DESC';
|
||||
$cat_ids = array_values(array_filter(array_map('intval', explode(',', (string) ($instance['cat_ids'] ?? '')))));
|
||||
$tag_ids = array_values(array_filter(array_map('intval', explode(',', (string) ($instance['tag_ids'] ?? '')))));
|
||||
$discount_ids = array_values(array_filter(array_map('intval', explode(',', (string) ($instance['discount_ids'] ?? '')))));
|
||||
$exclude_cat = !empty($instance['exclude_cat']);
|
||||
|
||||
$query_args = [
|
||||
'post_type' => 'aksrt_product',
|
||||
'post_status' => 'publish',
|
||||
'posts_per_page' => $per_page,
|
||||
'paged' => $paged,
|
||||
'orderby' => $orderby === 'sales' ? 'meta_value_num' : $orderby,
|
||||
'order' => $order,
|
||||
];
|
||||
$query_args = [
|
||||
'post_type' => 'aksrt_product',
|
||||
'post_status' => 'publish',
|
||||
'posts_per_page' => $per_page,
|
||||
'paged' => max(1, $paged),
|
||||
'orderby' => $orderby === 'sales' ? 'meta_value_num' : ($orderby === 'views' ? 'meta_value_num' : $orderby),
|
||||
'order' => $order,
|
||||
'ignore_sticky_posts' => true,
|
||||
];
|
||||
|
||||
if ($orderby === 'sales') {
|
||||
$query_args['meta_key'] = '_aksrt_sales';
|
||||
}
|
||||
if ($orderby === 'sales') {
|
||||
$query_args['meta_key'] = '_aksrt_sales';
|
||||
} elseif ($orderby === 'views') {
|
||||
$query_args['meta_key'] = 'aksrt_post_views';
|
||||
}
|
||||
|
||||
// Taxonomy filters
|
||||
$tax_query = [];
|
||||
if (!empty($cat_ids)) {
|
||||
$tax_query[] = [
|
||||
'taxonomy' => 'aksrt_product_cat',
|
||||
'field' => 'term_id',
|
||||
'terms' => $cat_ids,
|
||||
'operator' => $exclude_cat ? 'NOT IN' : 'IN',
|
||||
];
|
||||
}
|
||||
if (!empty($tag_ids)) {
|
||||
$tax_query[] = [
|
||||
'taxonomy' => 'aksrt_product_tag',
|
||||
'field' => 'term_id',
|
||||
'terms' => $tag_ids,
|
||||
];
|
||||
}
|
||||
if (!empty($discount_ids)) {
|
||||
$tax_query[] = [
|
||||
'taxonomy' => 'shop_discount',
|
||||
'field' => 'term_id',
|
||||
'terms' => $discount_ids,
|
||||
];
|
||||
}
|
||||
if (!empty($tax_query)) {
|
||||
$query_args['tax_query'] = $tax_query;
|
||||
}
|
||||
$tax_query = [];
|
||||
if (!empty($cat_ids)) {
|
||||
$tax_query[] = [
|
||||
'taxonomy' => 'aksrt_product_cat',
|
||||
'field' => 'term_id',
|
||||
'terms' => $cat_ids,
|
||||
'operator' => $exclude_cat ? 'NOT IN' : 'IN',
|
||||
];
|
||||
}
|
||||
if (!empty($tag_ids)) {
|
||||
$tax_query[] = [
|
||||
'taxonomy' => 'aksrt_product_tag',
|
||||
'field' => 'term_id',
|
||||
'terms' => $tag_ids,
|
||||
];
|
||||
}
|
||||
if (!empty($discount_ids)) {
|
||||
$tax_query[] = [
|
||||
'taxonomy' => 'shop_discount',
|
||||
'field' => 'term_id',
|
||||
'terms' => $discount_ids,
|
||||
];
|
||||
}
|
||||
if (!empty($tax_query)) {
|
||||
$query_args['tax_query'] = $tax_query;
|
||||
}
|
||||
return $query_args;
|
||||
}
|
||||
|
||||
$products = new \WP_Query($query_args);
|
||||
/**
|
||||
* Render product cards HTML for a query.
|
||||
*/
|
||||
public static function render_items_html(\WP_Query $products, string $style = 'default'): string {
|
||||
if (!$products->have_posts()) {
|
||||
return '';
|
||||
}
|
||||
ob_start();
|
||||
while ($products->have_posts()) {
|
||||
$products->the_post();
|
||||
$pid = get_the_ID();
|
||||
$card = class_exists('\AKSRT\Shop\Shop')
|
||||
? \AKSRT\Shop\Shop::get_instance()->build_product_card_view($pid)
|
||||
: [];
|
||||
if (function_exists('get_template_part') && !empty($card['id'])) {
|
||||
get_template_part('templates/parts/shop/product', 'card', [
|
||||
'product' => $card,
|
||||
'show_sales' => true,
|
||||
]);
|
||||
continue;
|
||||
}
|
||||
// Fallback mini card.
|
||||
$price = (float) ($card['price'] ?? 0);
|
||||
$image = (string) ($card['image'] ?? '');
|
||||
echo '<div class="aksrt-product-widget__item ajax-item">';
|
||||
if ($image !== '') {
|
||||
echo '<a href="' . esc_url(get_permalink($pid)) . '" class="aksrt-product-widget__thumb">';
|
||||
echo '<img src="' . esc_url($image) . '" alt="' . esc_attr(get_the_title($pid)) . '" loading="lazy">';
|
||||
echo '</a>';
|
||||
}
|
||||
echo '<div class="aksrt-product-widget__info">';
|
||||
echo '<a href="' . esc_url(get_permalink($pid)) . '" class="aksrt-product-widget__title">' . esc_html(get_the_title($pid)) . '</a>';
|
||||
echo '<div class="aksrt-product-widget__meta">';
|
||||
echo '<span class="aksrt-product-widget__price">¥' . esc_html(number_format_i18n($price, 2)) . '</span>';
|
||||
if (!empty($card['sales'])) {
|
||||
echo '<span class="aksrt-product-widget__sales">' . esc_html((string) $card['sales']) . esc_html__(' 已售', 'aksrt') . '</span>';
|
||||
}
|
||||
echo '</div></div></div>';
|
||||
}
|
||||
wp_reset_postdata();
|
||||
return (string) ob_get_clean();
|
||||
}
|
||||
|
||||
echo $args['before_widget'];
|
||||
if ($title) {
|
||||
echo $args['before_title'] . esc_html($title) . $args['after_title'];
|
||||
}
|
||||
/**
|
||||
* Shared payload for REST widget list.
|
||||
*
|
||||
* @param array $instance
|
||||
* @param int $page
|
||||
* @return array{html:string,paginate_html:string,has_more:bool,page:int,max_pages:int,total:int}
|
||||
*/
|
||||
public static function query_payload(array $instance, int $page = 1): array {
|
||||
$page = max(1, $page);
|
||||
$args = self::build_query_args($instance, $page);
|
||||
$style = sanitize_key((string) ($instance['style'] ?? 'default'));
|
||||
$paginate = sanitize_key((string) ($instance['paginate'] ?? 'ajax'));
|
||||
$products = new \WP_Query($args);
|
||||
$html = self::render_items_html($products, $style);
|
||||
if ($html === '' && $page === 1) {
|
||||
$html = function_exists('aksrt_get_ajax_null')
|
||||
? aksrt_get_ajax_null(__('暂无商品', 'aksrt'))
|
||||
: '<p class="aksrt-product-widget__empty ajax-item">' . esc_html__('暂无商品', 'aksrt') . '</p>';
|
||||
}
|
||||
$max_pages = max(1, (int) $products->max_num_pages);
|
||||
$total = (int) $products->found_posts;
|
||||
$has_more = $page < $max_pages;
|
||||
$paginate_html = '';
|
||||
if ($paginate === 'ajax' && $has_more) {
|
||||
$next = add_query_arg(
|
||||
array_filter([
|
||||
'page' => $page + 1,
|
||||
'per_page' => max(1, (int) ($instance['per_page'] ?? 8)),
|
||||
'orderby' => sanitize_key((string) ($instance['orderby'] ?? 'date')),
|
||||
'order' => sanitize_key((string) ($instance['order'] ?? 'DESC')),
|
||||
'cat_ids' => (string) ($instance['cat_ids'] ?? ''),
|
||||
'tag_ids' => (string) ($instance['tag_ids'] ?? ''),
|
||||
'discount_ids' => (string) ($instance['discount_ids'] ?? ''),
|
||||
'exclude_cat' => !empty($instance['exclude_cat']) ? 1 : 0,
|
||||
'style' => sanitize_key((string) ($instance['style'] ?? 'default')),
|
||||
'paginate' => 'ajax',
|
||||
], static function ($v) {
|
||||
return $v !== '' && $v !== null;
|
||||
}),
|
||||
rest_url('aksrt/v1/shop/frontend/widgets/products')
|
||||
);
|
||||
if (function_exists('aksrt_get_ajax_next_paginate')) {
|
||||
$paginate_html = aksrt_get_ajax_next_paginate(
|
||||
$total,
|
||||
$page,
|
||||
max(1, (int) ($instance['per_page'] ?? 8)),
|
||||
$next
|
||||
);
|
||||
} else {
|
||||
$paginate_html = '<div class="text-center theme-pagination ajax-pag"><div class="next-page ajax-next"><a href="' . esc_url($next) . '">' . esc_html__('加载更多', 'aksrt') . '</a></div></div>';
|
||||
}
|
||||
} elseif ($paginate === 'number' && $max_pages > 1) {
|
||||
$paginate_html = '<div class="aksrt-product-widget__pagination">' . paginate_links([
|
||||
'base' => add_query_arg('aksrt_product_page', '%#%'),
|
||||
'format' => '',
|
||||
'current' => $page,
|
||||
'total' => $max_pages,
|
||||
'prev_text' => '«',
|
||||
'next_text' => '»',
|
||||
]) . '</div>';
|
||||
} else {
|
||||
$paginate_html = '<div class="ajax-pag hide"><div class="next-page ajax-next"><a href="#"></a></div></div>';
|
||||
}
|
||||
|
||||
$style_class = 'aksrt-product-widget--' . $style;
|
||||
echo '<div class="aksrt-product-widget ' . esc_attr($style_class) . '" data-aksrt-product-widget>';
|
||||
return [
|
||||
'html' => $html,
|
||||
'paginate_html' => $paginate_html,
|
||||
'has_more' => $has_more,
|
||||
'page' => $page,
|
||||
'max_pages' => $max_pages,
|
||||
'total' => $total,
|
||||
];
|
||||
}
|
||||
|
||||
if ($products->have_posts()) {
|
||||
echo '<div class="aksrt-product-widget__grid">';
|
||||
while ($products->have_posts()) {
|
||||
$products->the_post();
|
||||
$pid = get_the_ID();
|
||||
$card = class_exists('\AKSRT\Shop\Shop')
|
||||
? \AKSRT\Shop\Shop::get_instance()->build_product_card_view($pid)
|
||||
: [];
|
||||
$price = (float) ($card['price'] ?? 0);
|
||||
$image = $card['image'] ?? '';
|
||||
echo '<div class="aksrt-product-widget__item">';
|
||||
if ($image) {
|
||||
echo '<a href="' . esc_url(get_permalink($pid)) . '" class="aksrt-product-widget__thumb">';
|
||||
echo '<img src="' . esc_url($image) . '" alt="' . esc_attr(get_the_title($pid)) . '" loading="lazy">';
|
||||
echo '</a>';
|
||||
}
|
||||
echo '<div class="aksrt-product-widget__info">';
|
||||
echo '<a href="' . esc_url(get_permalink($pid)) . '" class="aksrt-product-widget__title">' . esc_html(get_the_title($pid)) . '</a>';
|
||||
echo '<div class="aksrt-product-widget__meta">';
|
||||
echo '<span class="aksrt-product-widget__price">¥' . esc_html(number_format_i18n($price, 2)) . '</span>';
|
||||
if (!empty($card['sales'])) {
|
||||
echo '<span class="aksrt-product-widget__sales">' . esc_html($card['sales']) . __(' 已售', 'aksrt') . '</span>';
|
||||
}
|
||||
echo '</div></div></div>';
|
||||
}
|
||||
echo '</div>';
|
||||
public function widget($args, $instance): void {
|
||||
$title = apply_filters('widget_title', (string) ($instance['title'] ?? ''), $instance, $this->id_base);
|
||||
$style = sanitize_key((string) ($instance['style'] ?? 'default'));
|
||||
$paginate = sanitize_key((string) ($instance['paginate'] ?? 'ajax'));
|
||||
$page = max(1, (int) ($_GET['aksrt_product_page'] ?? 1)); // phpcs:ignore WordPress.Security.NonceVerification.Recommended
|
||||
|
||||
// Pagination
|
||||
if ($products->max_num_pages > 1) {
|
||||
echo '<div class="aksrt-product-widget__pagination">';
|
||||
echo paginate_links([
|
||||
'base' => add_query_arg('aksrt_product_page', '%#%'),
|
||||
'format' => '',
|
||||
'current' => $paged,
|
||||
'total' => $products->max_num_pages,
|
||||
'prev_text' => '«',
|
||||
'next_text' => '»',
|
||||
]);
|
||||
echo '</div>';
|
||||
}
|
||||
} else {
|
||||
echo '<p class="aksrt-product-widget__empty">' . esc_html__('暂无商品', 'aksrt') . '</p>';
|
||||
}
|
||||
$cfg = [
|
||||
'per_page' => max(1, min(50, (int) ($instance['per_page'] ?? 8))),
|
||||
'orderby' => sanitize_key((string) ($instance['orderby'] ?? 'date')),
|
||||
'order' => in_array(strtoupper((string) ($instance['order'] ?? '')), ['ASC', 'DESC'], true) ? strtoupper((string) $instance['order']) : 'DESC',
|
||||
'cat_ids' => (string) ($instance['cat_ids'] ?? ''),
|
||||
'tag_ids' => (string) ($instance['tag_ids'] ?? ''),
|
||||
'discount_ids' => (string) ($instance['discount_ids'] ?? ''),
|
||||
'exclude_cat' => !empty($instance['exclude_cat']) ? 1 : 0,
|
||||
'style' => $style,
|
||||
'paginate' => $paginate,
|
||||
];
|
||||
|
||||
echo '</div>';
|
||||
echo $args['after_widget'];
|
||||
$payload = self::query_payload($instance, $page);
|
||||
$ajax_url = rest_url('aksrt/v1/shop/frontend/widgets/products');
|
||||
|
||||
wp_reset_postdata();
|
||||
}
|
||||
echo $args['before_widget']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
|
||||
if ($title !== '') {
|
||||
echo $args['before_title'] . esc_html($title) . $args['after_title']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
|
||||
}
|
||||
|
||||
public function form($instance): void {
|
||||
$defaults = [
|
||||
'title' => __('商品推荐', 'aksrt'),
|
||||
'per_page' => 8,
|
||||
'orderby' => 'date',
|
||||
'order' => 'DESC',
|
||||
'cat_ids' => '',
|
||||
'tag_ids' => '',
|
||||
'discount_ids' => '',
|
||||
'exclude_cat' => false,
|
||||
'style' => 'default',
|
||||
];
|
||||
$instance = wp_parse_args($instance, $defaults);
|
||||
?>
|
||||
<p>
|
||||
<label for="<?php echo esc_attr($this->get_field_id('title')); ?>"><?php esc_html_e('标题:', 'aksrt'); ?></label>
|
||||
<input class="widefat" type="text" id="<?php echo esc_attr($this->get_field_id('title')); ?>" name="<?php echo esc_attr($this->get_field_name('title')); ?>" value="<?php echo esc_attr($instance['title']); ?>">
|
||||
</p>
|
||||
<p>
|
||||
<label for="<?php echo esc_attr($this->get_field_id('per_page')); ?>"><?php esc_html_e('每页数量:', 'aksrt'); ?></label>
|
||||
<input class="tiny-text" type="number" min="1" max="50" id="<?php echo esc_attr($this->get_field_id('per_page')); ?>" name="<?php echo esc_attr($this->get_field_name('per_page')); ?>" value="<?php echo esc_attr((string) $instance['per_page']); ?>">
|
||||
</p>
|
||||
<p>
|
||||
<label for="<?php echo esc_attr($this->get_field_id('orderby')); ?>"><?php esc_html_e('排序:', 'aksrt'); ?></label>
|
||||
<select class="widefat" id="<?php echo esc_attr($this->get_field_id('orderby')); ?>" name="<?php echo esc_attr($this->get_field_name('orderby')); ?>">
|
||||
<option value="date" <?php selected($instance['orderby'], 'date'); ?>><?php esc_html_e('最新', 'aksrt'); ?></option>
|
||||
<option value="sales" <?php selected($instance['orderby'], 'sales'); ?>><?php esc_html_e('销量', 'aksrt'); ?></option>
|
||||
<option value="title" <?php selected($instance['orderby'], 'title'); ?>><?php esc_html_e('标题', 'aksrt'); ?></option>
|
||||
<option value="rand" <?php selected($instance['orderby'], 'rand'); ?>><?php esc_html_e('随机', 'aksrt'); ?></option>
|
||||
</select>
|
||||
</p>
|
||||
<p>
|
||||
<label for="<?php echo esc_attr($this->get_field_id('order')); ?>"><?php esc_html_e('方向:', 'aksrt'); ?></label>
|
||||
<select class="widefat" id="<?php echo esc_attr($this->get_field_id('order')); ?>" name="<?php echo esc_attr($this->get_field_name('order')); ?>">
|
||||
<option value="DESC" <?php selected($instance['order'], 'DESC'); ?>><?php esc_html_e('降序', 'aksrt'); ?></option>
|
||||
<option value="ASC" <?php selected($instance['order'], 'ASC'); ?>><?php esc_html_e('升序', 'aksrt'); ?></option>
|
||||
</select>
|
||||
</p>
|
||||
<p>
|
||||
<label for="<?php echo esc_attr($this->get_field_id('cat_ids')); ?>"><?php esc_html_e('分类ID(逗号分隔):', 'aksrt'); ?></label>
|
||||
<input class="widefat" type="text" id="<?php echo esc_attr($this->get_field_id('cat_ids')); ?>" name="<?php echo esc_attr($this->get_field_name('cat_ids')); ?>" value="<?php echo esc_attr($instance['cat_ids']); ?>">
|
||||
</p>
|
||||
<p>
|
||||
<label>
|
||||
<input type="checkbox" name="<?php echo esc_attr($this->get_field_name('exclude_cat')); ?>" value="1" <?php checked($instance['exclude_cat']); ?>>
|
||||
<?php esc_html_e('排除上述分类', 'aksrt'); ?>
|
||||
</label>
|
||||
</p>
|
||||
<p>
|
||||
<label for="<?php echo esc_attr($this->get_field_id('tag_ids')); ?>"><?php esc_html_e('标签ID(逗号分隔):', 'aksrt'); ?></label>
|
||||
<input class="widefat" type="text" id="<?php echo esc_attr($this->get_field_id('tag_ids')); ?>" name="<?php echo esc_attr($this->get_field_name('tag_ids')); ?>" value="<?php echo esc_attr($instance['tag_ids']); ?>">
|
||||
</p>
|
||||
<p>
|
||||
<label for="<?php echo esc_attr($this->get_field_id('discount_ids')); ?>"><?php esc_html_e('优惠活动ID(逗号分隔):', 'aksrt'); ?></label>
|
||||
<input class="widefat" type="text" id="<?php echo esc_attr($this->get_field_id('discount_ids')); ?>" name="<?php echo esc_attr($this->get_field_name('discount_ids')); ?>" value="<?php echo esc_attr($instance['discount_ids']); ?>">
|
||||
</p>
|
||||
<p>
|
||||
<label for="<?php echo esc_attr($this->get_field_id('style')); ?>"><?php esc_html_e('列表样式:', 'aksrt'); ?></label>
|
||||
<select class="widefat" id="<?php echo esc_attr($this->get_field_id('style')); ?>" name="<?php echo esc_attr($this->get_field_name('style')); ?>">
|
||||
<option value="default" <?php selected($instance['style'], 'default'); ?>><?php esc_html_e('默认', 'aksrt'); ?></option>
|
||||
<option value="card" <?php selected($instance['style'], 'card'); ?>><?php esc_html_e('卡片', 'aksrt'); ?></option>
|
||||
<option value="list" <?php selected($instance['style'], 'list'); ?>><?php esc_html_e('列表', 'aksrt'); ?></option>
|
||||
<option value="mini" <?php selected($instance['style'], 'mini'); ?>><?php esc_html_e('迷你', 'aksrt'); ?></option>
|
||||
</select>
|
||||
</p>
|
||||
<?php
|
||||
}
|
||||
$style_class = 'aksrt-product-widget--' . $style;
|
||||
echo '<div class="aksrt-product-widget ' . esc_attr($style_class) . '" data-aksrt-product-widget data-aksrt-product-config="' . esc_attr(wp_json_encode($cfg)) . '">';
|
||||
// Items sit directly under .ajaxpager so ajax-list inject/append works (zibll product-lists-row).
|
||||
echo '<div class="ajaxpager aksrt-product-widget__grid product-lists-row" data-aksrt-ajaxpager data-aksrt-ajax-url="' . esc_url($ajax_url) . '">';
|
||||
echo $payload['html']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped — cards escaped upstream
|
||||
echo $payload['paginate_html']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
|
||||
echo '<div class="post_ajax_loader" data-aksrt-ajax-loader data-aksrt-ajax-loader-hide hidden></div>';
|
||||
echo '</div></div>';
|
||||
|
||||
public function update($new_instance, $old_instance): array {
|
||||
return [
|
||||
'title' => sanitize_text_field($new_instance['title'] ?? ''),
|
||||
'per_page' => max(1, min(50, (int) ($new_instance['per_page'] ?? 8))),
|
||||
'orderby' => sanitize_key($new_instance['orderby'] ?? 'date'),
|
||||
'order' => in_array(strtoupper($new_instance['order'] ?? ''), ['ASC', 'DESC'], true) ? strtoupper($new_instance['order']) : 'DESC',
|
||||
'cat_ids' => sanitize_text_field($new_instance['cat_ids'] ?? ''),
|
||||
'tag_ids' => sanitize_text_field($new_instance['tag_ids'] ?? ''),
|
||||
'discount_ids' => sanitize_text_field($new_instance['discount_ids'] ?? ''),
|
||||
'exclude_cat' => !empty($new_instance['exclude_cat']),
|
||||
'style' => sanitize_key($new_instance['style'] ?? 'default'),
|
||||
];
|
||||
}
|
||||
echo $args['after_widget']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
|
||||
}
|
||||
|
||||
public function form($instance): void {
|
||||
$defaults = [
|
||||
'title' => __('商品推荐', 'aksrt'),
|
||||
'per_page' => 8,
|
||||
'orderby' => 'date',
|
||||
'order' => 'DESC',
|
||||
'cat_ids' => '',
|
||||
'tag_ids' => '',
|
||||
'discount_ids' => '',
|
||||
'exclude_cat' => false,
|
||||
'style' => 'default',
|
||||
'paginate' => 'ajax',
|
||||
];
|
||||
$instance = wp_parse_args($instance, $defaults);
|
||||
?>
|
||||
<p>
|
||||
<label for="<?php echo esc_attr($this->get_field_id('title')); ?>"><?php esc_html_e('标题:', 'aksrt'); ?></label>
|
||||
<input class="widefat" type="text" id="<?php echo esc_attr($this->get_field_id('title')); ?>" name="<?php echo esc_attr($this->get_field_name('title')); ?>" value="<?php echo esc_attr((string) $instance['title']); ?>">
|
||||
</p>
|
||||
<p>
|
||||
<label for="<?php echo esc_attr($this->get_field_id('per_page')); ?>"><?php esc_html_e('每页数量:', 'aksrt'); ?></label>
|
||||
<input class="tiny-text" type="number" min="1" max="50" id="<?php echo esc_attr($this->get_field_id('per_page')); ?>" name="<?php echo esc_attr($this->get_field_name('per_page')); ?>" value="<?php echo esc_attr((string) $instance['per_page']); ?>">
|
||||
</p>
|
||||
<p>
|
||||
<label for="<?php echo esc_attr($this->get_field_id('orderby')); ?>"><?php esc_html_e('排序:', 'aksrt'); ?></label>
|
||||
<select class="widefat" id="<?php echo esc_attr($this->get_field_id('orderby')); ?>" name="<?php echo esc_attr($this->get_field_name('orderby')); ?>">
|
||||
<option value="date" <?php selected($instance['orderby'], 'date'); ?>><?php esc_html_e('最新', 'aksrt'); ?></option>
|
||||
<option value="sales" <?php selected($instance['orderby'], 'sales'); ?>><?php esc_html_e('销量', 'aksrt'); ?></option>
|
||||
<option value="views" <?php selected($instance['orderby'], 'views'); ?>><?php esc_html_e('热度', 'aksrt'); ?></option>
|
||||
<option value="title" <?php selected($instance['orderby'], 'title'); ?>><?php esc_html_e('标题', 'aksrt'); ?></option>
|
||||
<option value="rand" <?php selected($instance['orderby'], 'rand'); ?>><?php esc_html_e('随机', 'aksrt'); ?></option>
|
||||
</select>
|
||||
</p>
|
||||
<p>
|
||||
<label for="<?php echo esc_attr($this->get_field_id('order')); ?>"><?php esc_html_e('方向:', 'aksrt'); ?></label>
|
||||
<select class="widefat" id="<?php echo esc_attr($this->get_field_id('order')); ?>" name="<?php echo esc_attr($this->get_field_name('order')); ?>">
|
||||
<option value="DESC" <?php selected($instance['order'], 'DESC'); ?>><?php esc_html_e('降序', 'aksrt'); ?></option>
|
||||
<option value="ASC" <?php selected($instance['order'], 'ASC'); ?>><?php esc_html_e('升序', 'aksrt'); ?></option>
|
||||
</select>
|
||||
</p>
|
||||
<p>
|
||||
<label for="<?php echo esc_attr($this->get_field_id('cat_ids')); ?>"><?php esc_html_e('分类ID(逗号分隔):', 'aksrt'); ?></label>
|
||||
<input class="widefat" type="text" id="<?php echo esc_attr($this->get_field_id('cat_ids')); ?>" name="<?php echo esc_attr($this->get_field_name('cat_ids')); ?>" value="<?php echo esc_attr((string) $instance['cat_ids']); ?>">
|
||||
</p>
|
||||
<p>
|
||||
<label>
|
||||
<input type="checkbox" name="<?php echo esc_attr($this->get_field_name('exclude_cat')); ?>" value="1" <?php checked(!empty($instance['exclude_cat'])); ?>>
|
||||
<?php esc_html_e('排除上述分类', 'aksrt'); ?>
|
||||
</label>
|
||||
</p>
|
||||
<p>
|
||||
<label for="<?php echo esc_attr($this->get_field_id('tag_ids')); ?>"><?php esc_html_e('标签ID(逗号分隔):', 'aksrt'); ?></label>
|
||||
<input class="widefat" type="text" id="<?php echo esc_attr($this->get_field_id('tag_ids')); ?>" name="<?php echo esc_attr($this->get_field_name('tag_ids')); ?>" value="<?php echo esc_attr((string) $instance['tag_ids']); ?>">
|
||||
</p>
|
||||
<p>
|
||||
<label for="<?php echo esc_attr($this->get_field_id('discount_ids')); ?>"><?php esc_html_e('优惠活动ID(逗号分隔):', 'aksrt'); ?></label>
|
||||
<input class="widefat" type="text" id="<?php echo esc_attr($this->get_field_id('discount_ids')); ?>" name="<?php echo esc_attr($this->get_field_name('discount_ids')); ?>" value="<?php echo esc_attr((string) $instance['discount_ids']); ?>">
|
||||
</p>
|
||||
<p>
|
||||
<label for="<?php echo esc_attr($this->get_field_id('style')); ?>"><?php esc_html_e('列表样式:', 'aksrt'); ?></label>
|
||||
<select class="widefat" id="<?php echo esc_attr($this->get_field_id('style')); ?>" name="<?php echo esc_attr($this->get_field_name('style')); ?>">
|
||||
<option value="default" <?php selected($instance['style'], 'default'); ?>><?php esc_html_e('默认', 'aksrt'); ?></option>
|
||||
<option value="card" <?php selected($instance['style'], 'card'); ?>><?php esc_html_e('卡片', 'aksrt'); ?></option>
|
||||
<option value="list" <?php selected($instance['style'], 'list'); ?>><?php esc_html_e('列表', 'aksrt'); ?></option>
|
||||
<option value="mini" <?php selected($instance['style'], 'mini'); ?>><?php esc_html_e('迷你', 'aksrt'); ?></option>
|
||||
<option value="oneline" <?php selected($instance['style'], 'oneline'); ?>><?php esc_html_e('横滑', 'aksrt'); ?></option>
|
||||
</select>
|
||||
</p>
|
||||
<p>
|
||||
<label for="<?php echo esc_attr($this->get_field_id('paginate')); ?>"><?php esc_html_e('分页:', 'aksrt'); ?></label>
|
||||
<select class="widefat" id="<?php echo esc_attr($this->get_field_id('paginate')); ?>" name="<?php echo esc_attr($this->get_field_name('paginate')); ?>">
|
||||
<option value="ajax" <?php selected($instance['paginate'], 'ajax'); ?>><?php esc_html_e('AJAX 加载更多', 'aksrt'); ?></option>
|
||||
<option value="number" <?php selected($instance['paginate'], 'number'); ?>><?php esc_html_e('数字分页', 'aksrt'); ?></option>
|
||||
<option value="none" <?php selected($instance['paginate'], 'none'); ?>><?php esc_html_e('无', 'aksrt'); ?></option>
|
||||
</select>
|
||||
</p>
|
||||
<?php
|
||||
}
|
||||
|
||||
public function update($new_instance, $old_instance): array {
|
||||
return [
|
||||
'title' => sanitize_text_field((string) ($new_instance['title'] ?? '')),
|
||||
'per_page' => max(1, min(50, (int) ($new_instance['per_page'] ?? 8))),
|
||||
'orderby' => sanitize_key((string) ($new_instance['orderby'] ?? 'date')),
|
||||
'order' => in_array(strtoupper((string) ($new_instance['order'] ?? '')), ['ASC', 'DESC'], true) ? strtoupper((string) $new_instance['order']) : 'DESC',
|
||||
'cat_ids' => sanitize_text_field((string) ($new_instance['cat_ids'] ?? '')),
|
||||
'tag_ids' => sanitize_text_field((string) ($new_instance['tag_ids'] ?? '')),
|
||||
'discount_ids' => sanitize_text_field((string) ($new_instance['discount_ids'] ?? '')),
|
||||
'exclude_cat' => !empty($new_instance['exclude_cat']) ? 1 : 0,
|
||||
'style' => sanitize_key((string) ($new_instance['style'] ?? 'default')),
|
||||
'paginate' => sanitize_key((string) ($new_instance['paginate'] ?? 'ajax')),
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
@@ -8,163 +8,188 @@ namespace AKSRT\Widgets;
|
||||
defined('ABSPATH') || exit;
|
||||
|
||||
/**
|
||||
* Multi-tab product widget — equivalent to zibll `zib_shop_widget_ui_tab_product`.
|
||||
* Each tab has independent category/discount filter.
|
||||
* Multi-tab product widget — zibll `zib_shop_widget_ui_tab_product` lite.
|
||||
* First tab renders immediately via shared payload; other tabs lazy-load via REST.
|
||||
*/
|
||||
class WidgetProductTab extends \WP_Widget {
|
||||
|
||||
public function __construct() {
|
||||
parent::__construct(
|
||||
'aksrt_product_tab',
|
||||
__('多TAB商品', 'aksrt'),
|
||||
['description' => __('AKSRT 多 TAB 商品列表组件(每 tab 独立筛选)。', 'aksrt')]
|
||||
);
|
||||
}
|
||||
public function __construct() {
|
||||
parent::__construct(
|
||||
'aksrt_product_tab',
|
||||
__('多TAB商品', 'aksrt'),
|
||||
['description' => __('AKSRT 多 TAB 商品列表组件(每 tab 独立筛选 + AJAX)。', 'aksrt')]
|
||||
);
|
||||
}
|
||||
|
||||
public function widget($args, $instance): void {
|
||||
$title = apply_filters('widget_title', $instance['title'] ?? '');
|
||||
$tabs_raw = (string) ($instance['tabs'] ?? '');
|
||||
$per_page = max(1, min(20, (int) ($instance['per_page'] ?? 8)));
|
||||
/**
|
||||
* @return list<array{label:string,cat_ids:string,discount_ids:string,tag_ids:string,orderby:string}>
|
||||
*/
|
||||
private static function parse_tabs(string $tabs_raw): array {
|
||||
$tabs = [];
|
||||
foreach (array_filter(explode("\n", $tabs_raw)) as $line) {
|
||||
$parts = explode('|', trim($line));
|
||||
if (count($parts) >= 1 && trim($parts[0]) !== '') {
|
||||
$tabs[] = [
|
||||
'label' => trim($parts[0]),
|
||||
'cat_ids' => trim($parts[1] ?? ''),
|
||||
'discount_ids' => trim($parts[2] ?? ''),
|
||||
'tag_ids' => trim($parts[3] ?? ''),
|
||||
'orderby' => trim($parts[4] ?? 'date'),
|
||||
];
|
||||
}
|
||||
}
|
||||
return $tabs;
|
||||
}
|
||||
|
||||
// Parse tabs: "Tab Name|cat_ids|discount_ids\n..."
|
||||
$tabs = [];
|
||||
foreach (array_filter(explode("\n", $tabs_raw)) as $line) {
|
||||
$parts = explode('|', trim($line));
|
||||
if (count($parts) >= 1 && trim($parts[0]) !== '') {
|
||||
$tabs[] = [
|
||||
'label' => trim($parts[0]),
|
||||
'cat_ids' => trim($parts[1] ?? ''),
|
||||
'discount_ids' => trim($parts[2] ?? ''),
|
||||
];
|
||||
}
|
||||
}
|
||||
if (empty($tabs)) {
|
||||
return;
|
||||
}
|
||||
public function widget($args, $instance): void {
|
||||
$title = apply_filters('widget_title', (string) ($instance['title'] ?? ''), $instance, $this->id_base);
|
||||
$tabs_raw = (string) ($instance['tabs'] ?? '');
|
||||
$per_page = max(1, min(20, (int) ($instance['per_page'] ?? 8)));
|
||||
$style = sanitize_key((string) ($instance['style'] ?? 'default'));
|
||||
$paginate = sanitize_key((string) ($instance['paginate'] ?? 'ajax'));
|
||||
$tabs = self::parse_tabs($tabs_raw);
|
||||
if (!$tabs) {
|
||||
return;
|
||||
}
|
||||
|
||||
echo $args['before_widget'];
|
||||
if ($title) {
|
||||
echo $args['before_title'] . esc_html($title) . $args['after_title'];
|
||||
}
|
||||
echo $args['before_widget']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
|
||||
if ($title !== '') {
|
||||
echo $args['before_title'] . esc_html($title) . $args['after_title']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
|
||||
}
|
||||
|
||||
echo '<div class="aksrt-product-tab-widget" data-aksrt-product-tab>';
|
||||
$rest = rest_url('aksrt/v1/shop/frontend/widgets/products');
|
||||
echo '<div class="aksrt-product-tab-widget" data-aksrt-product-tab data-per-page="' . esc_attr((string) $per_page) . '" data-style="' . esc_attr($style) . '" data-paginate="' . esc_attr($paginate) . '" data-rest="' . esc_url($rest) . '">';
|
||||
|
||||
// Tab headers
|
||||
echo '<div class="aksrt-product-tab__headers">';
|
||||
foreach ($tabs as $i => $tab) {
|
||||
$active = $i === 0 ? ' is-active' : '';
|
||||
echo '<button type="button" class="aksrt-product-tab__header' . esc_attr($active) . '" data-tab-index="' . esc_attr((string) $i) . '">';
|
||||
echo esc_html($tab['label']);
|
||||
echo '</button>';
|
||||
}
|
||||
echo '</div>';
|
||||
echo '<div class="aksrt-product-tab__headers index-tab">';
|
||||
foreach ($tabs as $i => $tab) {
|
||||
$active = $i === 0 ? ' is-active' : '';
|
||||
echo '<button type="button" class="aksrt-product-tab__header' . esc_attr($active) . '" data-tab-index="' . esc_attr((string) $i) . '"';
|
||||
echo ' data-cat-ids="' . esc_attr($tab['cat_ids']) . '"';
|
||||
echo ' data-discount-ids="' . esc_attr($tab['discount_ids']) . '"';
|
||||
echo ' data-tag-ids="' . esc_attr($tab['tag_ids']) . '"';
|
||||
echo ' data-orderby="' . esc_attr(sanitize_key($tab['orderby'] ?: 'date')) . '"';
|
||||
echo '>' . esc_html($tab['label']) . '</button>';
|
||||
}
|
||||
echo '</div>';
|
||||
|
||||
// Tab panels
|
||||
foreach ($tabs as $i => $tab) {
|
||||
$hidden = $i > 0 ? ' style="display:none"' : '';
|
||||
echo '<div class="aksrt-product-tab__panel" data-tab-index="' . esc_attr((string) $i) . '"' . $hidden . '>';
|
||||
foreach ($tabs as $i => $tab) {
|
||||
$hidden = $i > 0 ? ' style="display:none"' : '';
|
||||
$loaded = $i === 0 ? '1' : '0';
|
||||
echo '<div class="aksrt-product-tab__panel" data-tab-index="' . esc_attr((string) $i) . '" data-loaded="' . esc_attr($loaded) . '"' . $hidden . '>';
|
||||
|
||||
$query_args = [
|
||||
'post_type' => 'aksrt_product',
|
||||
'post_status' => 'publish',
|
||||
'posts_per_page' => $per_page,
|
||||
'orderby' => 'date',
|
||||
'order' => 'DESC',
|
||||
];
|
||||
if ($i === 0) {
|
||||
$inst = [
|
||||
'per_page' => $per_page,
|
||||
'orderby' => sanitize_key($tab['orderby'] ?: 'date'),
|
||||
'order' => 'DESC',
|
||||
'cat_ids' => $tab['cat_ids'],
|
||||
'tag_ids' => $tab['tag_ids'],
|
||||
'discount_ids' => $tab['discount_ids'],
|
||||
'style' => $style,
|
||||
'paginate' => $paginate,
|
||||
];
|
||||
$payload = class_exists('\AKSRT\Widgets\WidgetProductList')
|
||||
? WidgetProductList::query_payload($inst, 1)
|
||||
: ['html' => '', 'paginate_html' => ''];
|
||||
echo '<div class="ajaxpager aksrt-product-widget__grid product-lists-row aksrt-product-widget--' . esc_attr($style) . '" data-aksrt-ajaxpager>';
|
||||
echo $payload['html']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
|
||||
echo $payload['paginate_html']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
|
||||
echo '<div class="post_ajax_loader" data-aksrt-ajax-loader data-aksrt-ajax-loader-hide hidden></div>';
|
||||
echo '</div>';
|
||||
} else {
|
||||
echo '<div class="ajaxpager aksrt-product-widget__grid product-lists-row aksrt-product-widget--' . esc_attr($style) . ' is-lazy" data-aksrt-ajaxpager>';
|
||||
echo '<div class="post_ajax_loader" data-aksrt-ajax-loader></div>';
|
||||
echo '<div class="ajax-pag hide"><div class="next-page ajax-next"><a href="#"></a></div></div>';
|
||||
echo '</div>';
|
||||
}
|
||||
echo '</div>';
|
||||
}
|
||||
|
||||
$tax_query = [];
|
||||
$cat_ids = array_filter(array_map('intval', explode(',', $tab['cat_ids'])));
|
||||
$discount_ids = array_filter(array_map('intval', explode(',', $tab['discount_ids'])));
|
||||
if (!empty($cat_ids)) {
|
||||
$tax_query[] = ['taxonomy' => 'aksrt_product_cat', 'field' => 'term_id', 'terms' => $cat_ids];
|
||||
}
|
||||
if (!empty($discount_ids)) {
|
||||
$tax_query[] = ['taxonomy' => 'shop_discount', 'field' => 'term_id', 'terms' => $discount_ids];
|
||||
}
|
||||
if (!empty($tax_query)) {
|
||||
$query_args['tax_query'] = $tax_query;
|
||||
}
|
||||
// Lightweight tab switch + lazy REST load (no inline product HTML duplication).
|
||||
echo '<script>(function(){';
|
||||
echo 'var root=document.currentScript&&document.currentScript.parentElement;if(!root)return;';
|
||||
echo 'var rest=root.getAttribute("data-rest")||"";';
|
||||
echo 'var per=root.getAttribute("data-per-page")||"8";';
|
||||
echo 'var style=root.getAttribute("data-style")||"default";';
|
||||
echo 'var paginate=root.getAttribute("data-paginate")||"ajax";';
|
||||
echo 'function loadPanel(panel,btn){if(!panel||panel.getAttribute("data-loaded")==="1")return;';
|
||||
echo 'var grid=panel.querySelector("[data-aksrt-ajaxpager]");if(!grid||!rest)return;';
|
||||
echo 'var qs=new URLSearchParams({page:"1",per_page:per,style:style,paginate:paginate,';
|
||||
echo 'orderby:btn.getAttribute("data-orderby")||"date",order:"DESC",';
|
||||
echo 'cat_ids:btn.getAttribute("data-cat-ids")||"",tag_ids:btn.getAttribute("data-tag-ids")||"",';
|
||||
echo 'discount_ids:btn.getAttribute("data-discount-ids")||""});';
|
||||
echo 'var url=rest+(rest.indexOf("?")>-1?"&":"?")+qs.toString();';
|
||||
echo 'grid.classList.add("is-ajax-loading");';
|
||||
echo 'fetch(url,{credentials:"same-origin",headers:{Accept:"application/json","X-WP-Nonce":(window.aksrtData&&aksrtData.restNonce)||""}})';
|
||||
echo '.then(function(r){return r.json()}).then(function(j){';
|
||||
echo 'var d=(j&&j.data&&(j.data.data||j.data))||j||{};';
|
||||
echo 'var html=d.html||""; var pag=d.paginate_html||d.paginateHtml||"";';
|
||||
echo 'var loader=grid.querySelector("[data-aksrt-ajax-loader]");';
|
||||
echo 'grid.innerHTML=""; if(html)grid.insertAdjacentHTML("beforeend",html); if(pag)grid.insertAdjacentHTML("beforeend",pag);';
|
||||
echo 'if(loader)grid.appendChild(loader); panel.setAttribute("data-loaded","1"); grid.classList.remove("is-ajax-loading");';
|
||||
echo '}).catch(function(){grid.classList.remove("is-ajax-loading");});}';
|
||||
echo 'root.querySelectorAll(".aksrt-product-tab__header").forEach(function(btn){';
|
||||
echo 'btn.addEventListener("click",function(){';
|
||||
echo 'root.querySelectorAll(".aksrt-product-tab__header").forEach(function(b){b.classList.remove("is-active")});';
|
||||
echo 'root.querySelectorAll(".aksrt-product-tab__panel").forEach(function(p){p.style.display="none"});';
|
||||
echo 'btn.classList.add("is-active");';
|
||||
echo 'var idx=btn.getAttribute("data-tab-index");';
|
||||
echo 'var panel=root.querySelector(\'.aksrt-product-tab__panel[data-tab-index="\'+idx+\'"]\');';
|
||||
echo 'if(panel){panel.style.display="";loadPanel(panel,btn);}';
|
||||
echo '});});';
|
||||
echo '})();</script>';
|
||||
|
||||
$products = new \WP_Query($query_args);
|
||||
if ($products->have_posts()) {
|
||||
echo '<div class="aksrt-product-widget__grid">';
|
||||
while ($products->have_posts()) {
|
||||
$products->the_post();
|
||||
$pid = get_the_ID();
|
||||
$card = class_exists('\AKSRT\Shop\Shop')
|
||||
? \AKSRT\Shop\Shop::get_instance()->build_product_card_view($pid)
|
||||
: [];
|
||||
$price = (float) ($card['price'] ?? 0);
|
||||
$image = $card['image'] ?? '';
|
||||
echo '<div class="aksrt-product-widget__item">';
|
||||
if ($image) {
|
||||
echo '<a href="' . esc_url(get_permalink($pid)) . '" class="aksrt-product-widget__thumb">';
|
||||
echo '<img src="' . esc_url($image) . '" alt="' . esc_attr(get_the_title($pid)) . '" loading="lazy">';
|
||||
echo '</a>';
|
||||
}
|
||||
echo '<div class="aksrt-product-widget__info">';
|
||||
echo '<a href="' . esc_url(get_permalink($pid)) . '" class="aksrt-product-widget__title">' . esc_html(get_the_title($pid)) . '</a>';
|
||||
echo '<span class="aksrt-product-widget__price">¥' . esc_html(number_format_i18n($price, 2)) . '</span>';
|
||||
echo '</div></div>';
|
||||
}
|
||||
echo '</div>';
|
||||
} else {
|
||||
echo '<p class="aksrt-product-widget__empty">' . esc_html__('暂无商品', 'aksrt') . '</p>';
|
||||
}
|
||||
echo '</div>';
|
||||
echo $args['after_widget']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
|
||||
}
|
||||
|
||||
echo '</div>';
|
||||
wp_reset_postdata();
|
||||
}
|
||||
public function form($instance): void {
|
||||
$defaults = [
|
||||
'title' => __('商品精选', 'aksrt'),
|
||||
'tabs' => "热门推荐|\n最新上架|",
|
||||
'per_page' => 8,
|
||||
'style' => 'default',
|
||||
'paginate' => 'ajax',
|
||||
];
|
||||
$instance = wp_parse_args($instance, $defaults);
|
||||
?>
|
||||
<p>
|
||||
<label for="<?php echo esc_attr($this->get_field_id('title')); ?>"><?php esc_html_e('标题:', 'aksrt'); ?></label>
|
||||
<input class="widefat" type="text" id="<?php echo esc_attr($this->get_field_id('title')); ?>" name="<?php echo esc_attr($this->get_field_name('title')); ?>" value="<?php echo esc_attr((string) $instance['title']); ?>">
|
||||
</p>
|
||||
<p>
|
||||
<label for="<?php echo esc_attr($this->get_field_id('tabs')); ?>"><?php esc_html_e('TAB配置(每行: 名称|分类ID|优惠ID|标签ID|排序):', 'aksrt'); ?></label>
|
||||
<textarea class="widefat" rows="5" id="<?php echo esc_attr($this->get_field_id('tabs')); ?>" name="<?php echo esc_attr($this->get_field_name('tabs')); ?>"><?php echo esc_textarea((string) $instance['tabs']); ?></textarea>
|
||||
</p>
|
||||
<p>
|
||||
<label for="<?php echo esc_attr($this->get_field_id('per_page')); ?>"><?php esc_html_e('每页数量:', 'aksrt'); ?></label>
|
||||
<input class="tiny-text" type="number" min="1" max="20" id="<?php echo esc_attr($this->get_field_id('per_page')); ?>" name="<?php echo esc_attr($this->get_field_name('per_page')); ?>" value="<?php echo esc_attr((string) $instance['per_page']); ?>">
|
||||
</p>
|
||||
<p>
|
||||
<label for="<?php echo esc_attr($this->get_field_id('style')); ?>"><?php esc_html_e('样式:', 'aksrt'); ?></label>
|
||||
<select class="widefat" id="<?php echo esc_attr($this->get_field_id('style')); ?>" name="<?php echo esc_attr($this->get_field_name('style')); ?>">
|
||||
<option value="default" <?php selected($instance['style'], 'default'); ?>><?php esc_html_e('默认', 'aksrt'); ?></option>
|
||||
<option value="card" <?php selected($instance['style'], 'card'); ?>><?php esc_html_e('卡片', 'aksrt'); ?></option>
|
||||
<option value="mini" <?php selected($instance['style'], 'mini'); ?>><?php esc_html_e('迷你', 'aksrt'); ?></option>
|
||||
</select>
|
||||
</p>
|
||||
<p>
|
||||
<label for="<?php echo esc_attr($this->get_field_id('paginate')); ?>"><?php esc_html_e('分页:', 'aksrt'); ?></label>
|
||||
<select class="widefat" id="<?php echo esc_attr($this->get_field_id('paginate')); ?>" name="<?php echo esc_attr($this->get_field_name('paginate')); ?>">
|
||||
<option value="ajax" <?php selected($instance['paginate'], 'ajax'); ?>><?php esc_html_e('AJAX', 'aksrt'); ?></option>
|
||||
<option value="none" <?php selected($instance['paginate'], 'none'); ?>><?php esc_html_e('无', 'aksrt'); ?></option>
|
||||
</select>
|
||||
</p>
|
||||
<?php
|
||||
}
|
||||
|
||||
// Tab switching JS
|
||||
echo '<script>';
|
||||
echo '(function(){';
|
||||
echo 'var w=document.currentScript.parentElement;';
|
||||
echo 'w.querySelectorAll(".aksrt-product-tab__header").forEach(function(btn){';
|
||||
echo 'btn.addEventListener("click",function(){';
|
||||
echo 'w.querySelectorAll(".aksrt-product-tab__header").forEach(function(b){b.classList.remove("is-active")});';
|
||||
echo 'w.querySelectorAll(".aksrt-product-tab__panel").forEach(function(p){p.style.display="none"});';
|
||||
echo 'btn.classList.add("is-active");';
|
||||
echo 'var idx=btn.getAttribute("data-tab-index");';
|
||||
echo 'var panel=w.querySelector(".aksrt-product-tab__panel[data-tab-index=\'\"+idx+\"\']");';
|
||||
echo 'if(panel)panel.style.display="";';
|
||||
echo '});';
|
||||
echo '});';
|
||||
echo '})();';
|
||||
echo '</script>';
|
||||
|
||||
echo '</div>';
|
||||
echo $args['after_widget'];
|
||||
}
|
||||
|
||||
public function form($instance): void {
|
||||
$defaults = [
|
||||
'title' => __('商品精选', 'aksrt'),
|
||||
'tabs' => "热门推荐|\n最新上架|",
|
||||
'per_page' => 8,
|
||||
];
|
||||
$instance = wp_parse_args($instance, $defaults);
|
||||
?>
|
||||
<p>
|
||||
<label for="<?php echo esc_attr($this->get_field_id('title')); ?>"><?php esc_html_e('标题:', 'aksrt'); ?></label>
|
||||
<input class="widefat" type="text" id="<?php echo esc_attr($this->get_field_id('title')); ?>" name="<?php echo esc_attr($this->get_field_name('title')); ?>" value="<?php echo esc_attr($instance['title']); ?>">
|
||||
</p>
|
||||
<p>
|
||||
<label for="<?php echo esc_attr($this->get_field_id('tabs')); ?>"><?php esc_html_e('TAB配置(每行: 名称|分类ID|优惠ID):', 'aksrt'); ?></label>
|
||||
<textarea class="widefat" rows="5" id="<?php echo esc_attr($this->get_field_id('tabs')); ?>" name="<?php echo esc_attr($this->get_field_name('tabs')); ?>"><?php echo esc_textarea($instance['tabs']); ?></textarea>
|
||||
</p>
|
||||
<p>
|
||||
<label for="<?php echo esc_attr($this->get_field_id('per_page')); ?>"><?php esc_html_e('每页数量:', 'aksrt'); ?></label>
|
||||
<input class="tiny-text" type="number" min="1" max="20" id="<?php echo esc_attr($this->get_field_id('per_page')); ?>" name="<?php echo esc_attr($this->get_field_name('per_page')); ?>" value="<?php echo esc_attr((string) $instance['per_page']); ?>">
|
||||
</p>
|
||||
<?php
|
||||
}
|
||||
|
||||
public function update($new_instance, $old_instance): array {
|
||||
return [
|
||||
'title' => sanitize_text_field($new_instance['title'] ?? ''),
|
||||
'tabs' => sanitize_textarea_field($new_instance['tabs'] ?? ''),
|
||||
'per_page' => max(1, min(20, (int) ($new_instance['per_page'] ?? 8))),
|
||||
];
|
||||
}
|
||||
public function update($new_instance, $old_instance): array {
|
||||
return [
|
||||
'title' => sanitize_text_field((string) ($new_instance['title'] ?? '')),
|
||||
'tabs' => sanitize_textarea_field((string) ($new_instance['tabs'] ?? '')),
|
||||
'per_page' => max(1, min(20, (int) ($new_instance['per_page'] ?? 8))),
|
||||
'style' => sanitize_key((string) ($new_instance['style'] ?? 'default')),
|
||||
'paginate' => sanitize_key((string) ($new_instance['paginate'] ?? 'ajax')),
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
@@ -102,6 +102,16 @@ $pm_url = ($pm_enabled && $messages_url !== '')
|
||||
</h1>
|
||||
<?php endif; ?>
|
||||
|
||||
<?php
|
||||
// Badge detail remote-modal (zibll user medal info).
|
||||
if (class_exists('\AKSRT\User\Badges') && \AKSRT\User\Badges::enabled() && \AKSRT\User\Badges::public_display()) {
|
||||
$badge_link = \AKSRT\User\Badges::get_info_link($user_id, 'but but-sm jb-blue mt6', __('徽章明细', 'aksrt'));
|
||||
if ($badge_link !== '') {
|
||||
echo '<div class="aksrt-author-hero__badges">' . $badge_link . '</div>'; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
|
||||
}
|
||||
}
|
||||
?>
|
||||
|
||||
<?php if ($show_desc && $user->description) : ?>
|
||||
<p class="aksrt-author-hero__desc"><?php echo esc_html($user->description); ?></p>
|
||||
<?php endif; ?>
|
||||
|
||||
Reference in New Issue
Block a user