Files
wordpress-theme-aksrt/src/AKSRT/Core/PostsNavs.php
T
kate522 1cb534e4fe feat(pages): 1:1 文章导航 posts-navs (zibll postsnavs)
Per-category full post lists, affix TOC shell, admin drag-sort settings
modal + AJAX save (navs_show_cat_id / name / desc).
2026-07-18 21:10:42 +08:00

78 lines
2.3 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<?php
/**
* 文章导航页设置 AJAX(zibll action/post_navs.php parity).
*
* @package AKSRT
*/
namespace AKSRT\Core;
defined('ABSPATH') || exit;
class PostsNavs {
private static $instance = null;
public static function get_instance(): self {
if (null === self::$instance) {
self::$instance = new self();
}
return self::$instance;
}
public function init(): void {
add_action('wp_ajax_aksrt_post_navs_settings', [$this, 'ajax_save_settings']);
}
/**
* Super-admin only: save navs_page_name / desc / ordered cat IDs.
*/
public function ajax_save_settings(): void {
if (!current_user_can('manage_options')) {
wp_send_json(['error' => 1, 'ys' => 'danger', 'msg' => __('权限不足', 'aksrt')]);
}
if (!check_ajax_referer('aksrt_post_navs_settings', '_wpnonce', false)) {
wp_send_json(['error' => 1, 'ys' => 'danger', 'msg' => __('安全校验失败', 'aksrt')]);
}
$page_id = isset($_POST['page_id']) ? absint($_POST['page_id']) : 0;
if ($page_id <= 0 || get_post_type($page_id) !== 'page') {
wp_send_json(['error' => 1, 'ys' => 'danger', 'msg' => __('无效页面', 'aksrt')]);
}
$cat_ids = [];
if (isset($_POST['navs_show_cat_id'])) {
$raw = wp_unslash($_POST['navs_show_cat_id']);
if (is_array($raw)) {
$cat_ids = array_values(array_filter(array_map('intval', $raw)));
} elseif (is_string($raw) && $raw !== '') {
$decoded = json_decode($raw, true);
if (is_array($decoded)) {
$cat_ids = array_values(array_filter(array_map('intval', $decoded)));
} else {
$cat_ids = array_values(array_filter(array_map('intval', explode(',', $raw))));
}
}
}
if (empty($cat_ids)) {
wp_send_json(['error' => 1, 'ys' => 'danger', 'msg' => __('请选择需要显示的分类', 'aksrt')]);
}
if (isset($_POST['navs_page_name'])) {
update_post_meta($page_id, 'navs_page_name', sanitize_text_field(wp_unslash((string) $_POST['navs_page_name'])));
}
if (isset($_POST['navs_page_desc'])) {
update_post_meta($page_id, 'navs_page_desc', sanitize_text_field(wp_unslash((string) $_POST['navs_page_desc'])));
}
update_post_meta($page_id, 'navs_show_cat_id', $cat_ids);
wp_send_json([
'error' => 0,
'msg' => __('保存成功', 'aksrt'),
'reload'=> true,
'ids' => $cat_ids,
]);
}
}