Files
wordpress-theme-aksrt/src/Core/LinksPageMeta.php
T
kate522 8c5b1555c9 refactor: flatten src/AKSRT → src (drop theme-identifier dir)
Move all domain folders from src/AKSRT/{Domain} to src/{Domain}; remap
Composer PSR-4 "AKSRT\\": "src/"; update audit script + README + ADR
paths; remove dead tools/build-psr4-map.php (scanned deleted inc/classes).
2026-08-02 22:06:06 +08:00

303 lines
12 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
/**
* Page meta for 网址导航 template (page_links_* keys, zibll metabox parity).
*
* @package AKSRT
*/
namespace AKSRT\Core;
defined('ABSPATH') || exit;
class LinksPageMeta {
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('add_meta_boxes', [$this, 'register_meta_box']);
add_action('save_post_page', [$this, 'save_meta_box']);
}
public function register_meta_box(): void {
add_meta_box(
'aksrt_page_links_meta',
__('网址导航设置', 'aksrt'),
[$this, 'render_meta_box'],
'page',
'normal',
'high'
);
}
/**
* Whether current page uses the links template.
*/
private function is_links_template(int $post_id): bool {
$tpl = (string) get_post_meta($post_id, '_wp_page_template', true);
return $tpl === 'templates/pages/page-links.php';
}
public function render_meta_box(\WP_Post $post): void {
$post_id = (int) $post->ID;
if (!$this->is_links_template($post_id)) {
echo '<p class="description">' . esc_html__('将页面模板设为「AKSRT - 网址导航」后在此配置分类、样式与提交模块。', 'aksrt') . '</p>';
return;
}
$s = function_exists('aksrt_get_page_links_settings')
? aksrt_get_page_links_settings($post_id)
: [];
wp_nonce_field('aksrt_page_links_meta', 'aksrt_page_links_meta_nonce');
$terms = get_terms([
'taxonomy' => 'link_category',
'hide_empty' => false,
]);
if (is_wp_error($terms)) {
$terms = [];
}
$selected_cats = array_map('intval', (array) ($s['category'] ?? []));
$submit_cats = array_map('intval', (array) ($s['submit_cats'] ?? []));
$search_types = (array) ($s['search_types'] ?? []);
$engine_options = [
'self' => __('站内', 'aksrt'),
'baidu' => __('百度', 'aksrt'),
'google' => __('谷歌', 'aksrt'),
'bing' => __('必应', 'aksrt'),
'sogou' => __('搜狗', 'aksrt'),
'360' => '360',
];
?>
<table class="form-table aksrt-page-links-meta" role="presentation">
<tr>
<th scope="row"><?php esc_html_e('显示页面内容', 'aksrt'); ?></th>
<td>
<label>
<input type="checkbox" name="page_links_content_s" value="1" <?php checked(!empty($s['content_s'])); ?>>
<?php esc_html_e('在链接列表上下方展示页面正文', 'aksrt'); ?>
</label>
<p>
<label>
<input type="radio" name="page_links_content_position" value="top" <?php checked(($s['content_position'] ?? 'top'), 'top'); ?>>
<?php esc_html_e('链接列表上面', 'aksrt'); ?>
</label>
&nbsp;
<label>
<input type="radio" name="page_links_content_position" value="bottom" <?php checked(($s['content_position'] ?? ''), 'bottom'); ?>>
<?php esc_html_e('链接列表下面', 'aksrt'); ?>
</label>
</p>
</td>
</tr>
<tr>
<th scope="row"><?php esc_html_e('搜索引擎模块', 'aksrt'); ?></th>
<td>
<label>
<input type="checkbox" name="page_links_search_s" value="1" <?php checked(!empty($s['search_s'])); ?>>
<?php esc_html_e('显示搜索封面(使用特色图作背景)', 'aksrt'); ?>
</label>
<p class="description"><?php esc_html_e('选择引擎(可多选,按住 Ctrl)', 'aksrt'); ?></p>
<select name="page_links_search_types[]" multiple class="widefat" size="6">
<?php foreach ($engine_options as $key => $label) : ?>
<option value="<?php echo esc_attr($key); ?>" <?php selected(in_array($key, $search_types, true)); ?>>
<?php echo esc_html($label); ?>
</option>
<?php endforeach; ?>
</select>
</td>
</tr>
<tr>
<th scope="row"><?php esc_html_e('链接分类(必选)', 'aksrt'); ?></th>
<td>
<?php if (empty($terms)) : ?>
<p class="description">
<?php
printf(
/* translators: %s: link manager URL */
wp_kses_post(__('暂无链接分类。请先在 <a href="%s">链接管理</a> 中创建分类与链接。', 'aksrt')),
esc_url(admin_url('link-manager.php'))
);
?>
</p>
<?php else : ?>
<select name="page_links_category[]" multiple class="widefat" size="8">
<?php foreach ($terms as $term) : ?>
<option value="<?php echo (int) $term->term_id; ?>" <?php selected(in_array((int) $term->term_id, $selected_cats, true)); ?>>
<?php echo esc_html($term->name . ' (' . (int) $term->count . ')'); ?>
</option>
<?php endforeach; ?>
</select>
<p class="description"><?php esc_html_e('多选时按导航目录展示;一个都不选则不显示链接。', 'aksrt'); ?></p>
<?php endif; ?>
</td>
</tr>
<tr>
<th scope="row"><?php esc_html_e('显示样式', 'aksrt'); ?></th>
<td>
<?php
$style = (string) ($s['style'] ?? 'card');
$styles = [
'card' => __('图文列表', 'aksrt'),
'bigcard' => __('大卡片', 'aksrt'),
'image' => __('纯图片', 'aksrt'),
];
foreach ($styles as $key => $label) :
?>
<label style="margin-right:12px;">
<input type="radio" name="page_links_style" value="<?php echo esc_attr($key); ?>" <?php checked($style, $key); ?>>
<?php echo esc_html($label); ?>
</label>
<?php endforeach; ?>
</td>
</tr>
<tr>
<th scope="row"><?php esc_html_e('数量与排序', 'aksrt'); ?></th>
<td>
<label>
<?php esc_html_e('每类最多', 'aksrt'); ?>
<input type="number" name="page_links_limit" value="<?php echo esc_attr((string) (int) ($s['limit'] ?? 0)); ?>" min="0" step="1" class="small-text">
<?php esc_html_e('个(0=不限)', 'aksrt'); ?>
</label>
<p>
<select name="page_links_orderby">
<?php
$orderbys = [
'name' => __('名称', 'aksrt'),
'updated' => __('更新时间', 'aksrt'),
'rating' => __('评分', 'aksrt'),
'rand' => __('随机', 'aksrt'),
];
$orderby = (string) ($s['orderby'] ?? 'name');
foreach ($orderbys as $key => $label) :
?>
<option value="<?php echo esc_attr($key); ?>" <?php selected($orderby, $key); ?>><?php echo esc_html($label); ?></option>
<?php endforeach; ?>
</select>
<select name="page_links_order">
<option value="ASC" <?php selected(($s['order'] ?? 'ASC'), 'ASC'); ?>><?php esc_html_e('升序', 'aksrt'); ?></option>
<option value="DESC" <?php selected(($s['order'] ?? ''), 'DESC'); ?>><?php esc_html_e('降序', 'aksrt'); ?></option>
</select>
</p>
</td>
</tr>
<tr>
<th scope="row"><?php esc_html_e('链接行为', 'aksrt'); ?></th>
<td>
<label><input type="checkbox" name="page_links_go_s" value="1" <?php checked(!empty($s['go_s'])); ?>> <?php esc_html_e('外链重定向 /go/', 'aksrt'); ?></label><br>
<label><input type="checkbox" name="page_links_blank_s" value="1" <?php checked(!empty($s['blank_s'])); ?>> <?php esc_html_e('新标签打开', 'aksrt'); ?></label><br>
<label><input type="checkbox" name="page_links_nofollow_s" value="1" <?php checked(!empty($s['nofollow_s'])); ?>> <?php esc_html_e('添加 nofollow', 'aksrt'); ?></label>
</td>
</tr>
<tr>
<th scope="row"><?php esc_html_e('提交链接', 'aksrt'); ?></th>
<td>
<label><input type="checkbox" name="page_links_submit_s" value="1" <?php checked(!empty($s['submit_s'])); ?>> <?php esc_html_e('显示申请入驻', 'aksrt'); ?></label><br>
<label><input type="checkbox" name="page_links_submit_sign_s" value="1" <?php checked(!empty($s['submit_sign_s'])); ?>> <?php esc_html_e('登录后才能提交', 'aksrt'); ?></label>
<p>
<label><?php esc_html_e('按钮标题', 'aksrt'); ?>
<input type="text" class="regular-text" name="page_links_submit_title" value="<?php echo esc_attr((string) ($s['submit_title'] ?? '')); ?>">
</label>
</p>
<p>
<label for="page_links_submit_dec"><?php esc_html_e('提交说明(HTML)', 'aksrt'); ?></label><br>
<textarea class="large-text" rows="5" id="page_links_submit_dec" name="page_links_submit_dec"><?php echo esc_textarea((string) ($s['submit_dec'] ?? '')); ?></textarea>
</p>
<?php if (!empty($terms)) : ?>
<p class="description"><?php esc_html_e('提交时可选分类(空=全部分类)', 'aksrt'); ?></p>
<select name="page_links_submit_cats[]" multiple class="widefat" size="6">
<?php foreach ($terms as $term) : ?>
<option value="<?php echo (int) $term->term_id; ?>" <?php selected(in_array((int) $term->term_id, $submit_cats, true)); ?>>
<?php echo esc_html($term->name); ?>
</option>
<?php endforeach; ?>
</select>
<?php endif; ?>
</td>
</tr>
</table>
<?php
}
public function save_meta_box(int $post_id): void {
if (!isset($_POST['aksrt_page_links_meta_nonce']) || !wp_verify_nonce(sanitize_text_field(wp_unslash((string) $_POST['aksrt_page_links_meta_nonce'])), 'aksrt_page_links_meta')) {
return;
}
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) {
return;
}
if (!current_user_can('edit_post', $post_id)) {
return;
}
if (!$this->is_links_template($post_id) && empty($_POST['page_links_category']) && empty($_POST['page_links_submit_s'])) {
// Still allow save if user just switched template in same request.
$tpl = isset($_POST['page_template']) ? (string) wp_unslash($_POST['page_template']) : '';
if ($tpl !== 'templates/pages/page-links.php') {
return;
}
}
$bool_keys = [
'page_links_content_s',
'page_links_search_s',
'page_links_go_s',
'page_links_blank_s',
'page_links_nofollow_s',
'page_links_submit_s',
'page_links_submit_sign_s',
];
foreach ($bool_keys as $key) {
update_post_meta($post_id, $key, isset($_POST[$key]) ? 1 : 0);
}
$position = isset($_POST['page_links_content_position']) ? sanitize_key((string) wp_unslash($_POST['page_links_content_position'])) : 'top';
if (!in_array($position, ['top', 'bottom'], true)) {
$position = 'top';
}
update_post_meta($post_id, 'page_links_content_position', $position);
$style = isset($_POST['page_links_style']) ? sanitize_key((string) wp_unslash($_POST['page_links_style'])) : 'card';
if (!in_array($style, ['card', 'bigcard', 'image', 'simple'], true)) {
$style = 'card';
}
update_post_meta($post_id, 'page_links_style', $style);
$orderby = isset($_POST['page_links_orderby']) ? sanitize_key((string) wp_unslash($_POST['page_links_orderby'])) : 'name';
if (!in_array($orderby, ['name', 'updated', 'rating', 'rand', 'id', 'length'], true)) {
$orderby = 'name';
}
update_post_meta($post_id, 'page_links_orderby', $orderby);
$order = isset($_POST['page_links_order']) ? strtoupper(sanitize_text_field(wp_unslash((string) $_POST['page_links_order']))) : 'ASC';
update_post_meta($post_id, 'page_links_order', $order === 'DESC' ? 'DESC' : 'ASC');
$limit = isset($_POST['page_links_limit']) ? max(0, (int) $_POST['page_links_limit']) : 0;
update_post_meta($post_id, 'page_links_limit', $limit);
$cats = isset($_POST['page_links_category']) ? array_values(array_filter(array_map('intval', (array) wp_unslash($_POST['page_links_category'])))) : [];
update_post_meta($post_id, 'page_links_category', $cats);
$submit_cats = isset($_POST['page_links_submit_cats']) ? array_values(array_filter(array_map('intval', (array) wp_unslash($_POST['page_links_submit_cats'])))) : [];
update_post_meta($post_id, 'page_links_submit_cats', $submit_cats);
$engines = isset($_POST['page_links_search_types']) ? array_values(array_filter(array_map('sanitize_key', (array) wp_unslash($_POST['page_links_search_types'])))) : [];
$allowed = ['self', 'baidu', 'google', 'bing', 'sogou', '360'];
$engines = array_values(array_intersect($engines, $allowed));
update_post_meta($post_id, 'page_links_search_types', $engines);
$title = isset($_POST['page_links_submit_title']) ? sanitize_text_field(wp_unslash((string) $_POST['page_links_submit_title'])) : '';
update_post_meta($post_id, 'page_links_submit_title', $title);
$dec = isset($_POST['page_links_submit_dec']) ? wp_kses_post(wp_unslash((string) $_POST['page_links_submit_dec'])) : '';
update_post_meta($post_id, 'page_links_submit_dec', $dec);
}
}