feat(links): move friend links to native WordPress links with review flow

- enable the native Links manager and drop the custom friend-link option/REST path
- add per-page link settings for the friends template
- render link categories from get_bookmarks() with search and page-content options
- add a validated public link submission endpoint with logo upload, captcha, rate limiting and duplicate checks
- create pending links for review and notify administrators by email and in-app message
- add native Links admin pending count, columns and edit notes
- cover the new submission and cleanup contracts

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
This commit is contained in:
kate522andClaude Opus 5 committed 2026-11-05 19:54:33 +08:00
1 parent ef7b5939ba
commit 4c562428b4
24 files changed
+792 -273

No files matched your search

+203
View File
@@ -0,0 +1,203 @@
<?php
/**
* Page configuration for native link listings.
*
* @package Jio
*/
namespace Jio\Admin;
defined('ABSPATH') || exit;
final class PageLinksMetaBox {
private const NONCE_ACTION = 'jio_page_links_save';
private const NONCE_NAME = 'jio_page_links_nonce';
public static function init(): void {
add_action('add_meta_boxes_page', [self::class, 'register']);
add_action('save_post_page', [self::class, 'save'], 10, 3);
}
public static function register(\WP_Post $post): void {
if (!self::is_links_page($post)) {
return;
}
add_meta_box(
'jio-page-links',
__('链接页设置', 'jio'),
[self::class, 'render'],
'page',
'normal',
'high'
);
}
public static function render(\WP_Post $post): void {
$settings = function_exists('jio_get_page_links_settings')
? jio_get_page_links_settings((int) $post->ID)
: [];
if ($settings === [] && !metadata_exists('post', (int) $post->ID, 'page_links_nofollow_s')) {
$settings['nofollow_s'] = true;
$settings['submit_sign_s'] = true;
}
wp_nonce_field(self::NONCE_ACTION, self::NONCE_NAME);
$terms = get_terms(['taxonomy' => 'link_category', 'hide_empty' => false]);
$terms = is_wp_error($terms) ? [] : $terms;
$selected_categories = array_map('absint', (array) ($settings['category'] ?? []));
$submit_categories = array_map('absint', (array) ($settings['submit_cats'] ?? []));
$search_types = (array) ($settings['search_types'] ?? []);
?>
<table class="form-table" role="presentation">
<tbody>
<tr>
<th scope="row"><?php esc_html_e('页面正文', 'jio'); ?></th>
<td>
<label><input type="checkbox" name="page_links_content_s" value="1" <?php checked(!empty($settings['content_s'])); ?>> <?php esc_html_e('显示页面正文', 'jio'); ?></label>
<p><select name="page_links_content_position">
<option value="top" <?php selected($settings['content_position'] ?? 'top', 'top'); ?>><?php esc_html_e('正文在链接列表上方', 'jio'); ?></option>
<option value="bottom" <?php selected($settings['content_position'] ?? 'top', 'bottom'); ?>><?php esc_html_e('正文在链接列表下方', 'jio'); ?></option>
</select></p>
</td>
</tr>
<tr>
<th scope="row"><?php esc_html_e('搜索模块', 'jio'); ?></th>
<td>
<label><input type="checkbox" name="page_links_search_s" value="1" <?php checked(!empty($settings['search_s'])); ?>> <?php esc_html_e('显示搜索引擎模块', 'jio'); ?></label>
<p><?php foreach (self::search_types() as $key => $label) : ?>
<label style="margin-right:12px"><input type="checkbox" name="page_links_search_types[]" value="<?php echo esc_attr($key); ?>" <?php checked(in_array($key, $search_types, true)); ?>> <?php echo esc_html($label); ?></label>
<?php endforeach; ?></p>
</td>
</tr>
<tr>
<th scope="row"><label for="jio-page-links-categories"><?php esc_html_e('显示链接分类', 'jio'); ?></label></th>
<td>
<select id="jio-page-links-categories" name="page_links_category[]" multiple size="6" style="min-width:280px">
<?php foreach ($terms as $term) : ?>
<option value="<?php echo (int) $term->term_id; ?>" <?php selected(in_array((int) $term->term_id, $selected_categories, true)); ?>><?php echo esc_html($term->name); ?></option>
<?php endforeach; ?>
</select>
<p class="description"><?php esc_html_e('可以多选并拖动排序;未选择分类时不显示链接列表。', 'jio'); ?></p>
</td>
</tr>
<tr>
<th scope="row"><?php esc_html_e('显示方式与排序', 'jio'); ?></th>
<td>
<label><?php esc_html_e('样式', 'jio'); ?>
<select name="page_links_style">
<option value="card" <?php selected($settings['style'] ?? 'card', 'card'); ?>><?php esc_html_e('图文列表', 'jio'); ?></option>
<option value="bigcard" <?php selected($settings['style'] ?? 'card', 'bigcard'); ?>><?php esc_html_e('大卡片', 'jio'); ?></option>
<option value="image" <?php selected($settings['style'] ?? 'card', 'image'); ?>><?php esc_html_e('纯图片', 'jio'); ?></option>
</select>
</label>
<label style="margin-left:12px"><?php esc_html_e('每类最多显示', 'jio'); ?>
<input type="number" min="0" step="1" name="page_links_limit" value="<?php echo (int) ($settings['limit'] ?? 0); ?>" style="width:90px"> <?php esc_html_e('条(0 为不限)', 'jio'); ?>
</label>
<p>
<label><?php esc_html_e('排序依据', 'jio'); ?>
<select name="page_links_orderby">
<option value="name" <?php selected($settings['orderby'] ?? 'name', 'name'); ?>><?php esc_html_e('名称', 'jio'); ?></option>
<option value="updated" <?php selected($settings['orderby'] ?? 'name', 'updated'); ?>><?php esc_html_e('更新时间', 'jio'); ?></option>
<option value="rating" <?php selected($settings['orderby'] ?? 'name', 'rating'); ?>><?php esc_html_e('评分', 'jio'); ?></option>
<option value="rand" <?php selected($settings['orderby'] ?? 'name', 'rand'); ?>><?php esc_html_e('随机', 'jio'); ?></option>
</select>
</label>
<label style="margin-left:12px"><input type="radio" name="page_links_order" value="ASC" <?php checked($settings['order'] ?? 'ASC', 'ASC'); ?>> <?php esc_html_e('升序', 'jio'); ?></label>
<label><input type="radio" name="page_links_order" value="DESC" <?php checked($settings['order'] ?? 'ASC', 'DESC'); ?>> <?php esc_html_e('降序', 'jio'); ?></label>
</p>
<label><input type="checkbox" name="page_links_go_s" value="1" <?php checked(!empty($settings['go_s'])); ?>> <?php esc_html_e('外链使用中转页', 'jio'); ?></label>
<label style="margin-left:12px"><input type="checkbox" name="page_links_blank_s" value="1" <?php checked(!empty($settings['blank_s'])); ?>> <?php esc_html_e('新标签页打开', 'jio'); ?></label>
<label style="margin-left:12px"><input type="checkbox" name="page_links_nofollow_s" value="1" <?php checked(!empty($settings['nofollow_s'])); ?>> <?php esc_html_e('添加 nofollow', 'jio'); ?></label>
</td>
</tr>
<tr>
<th scope="row"><?php esc_html_e('链接申请', 'jio'); ?></th>
<td>
<label><input type="checkbox" name="page_links_submit_s" value="1" <?php checked(!empty($settings['submit_s'])); ?>> <?php esc_html_e('显示申请链接入口', 'jio'); ?></label>
<p><label><input type="checkbox" name="page_links_submit_sign_s" value="1" <?php checked(!empty($settings['submit_sign_s'])); ?>> <?php esc_html_e('登录后才能申请', 'jio'); ?></label></p>
<label for="jio-page-links-submit-cats"><?php esc_html_e('允许选择的申请分类', 'jio'); ?></label><br>
<select id="jio-page-links-submit-cats" name="page_links_submit_cats[]" multiple size="5" style="min-width:280px">
<?php foreach ($terms as $term) : ?>
<option value="<?php echo (int) $term->term_id; ?>" <?php selected(in_array((int) $term->term_id, $submit_categories, true)); ?>><?php echo esc_html($term->name); ?></option>
<?php endforeach; ?>
</select>
<p class="description"><?php esc_html_e('留空表示允许选择所有链接分类。', 'jio'); ?></p>
<p><label><?php esc_html_e('申请入口标题', 'jio'); ?><br><input type="text" name="page_links_submit_title" value="<?php echo esc_attr((string) ($settings['submit_title'] ?? '')); ?>" class="regular-text"></label></p>
<label for="jio-page-links-submit-dec"><?php esc_html_e('申请说明', 'jio'); ?></label>
<textarea id="jio-page-links-submit-dec" name="page_links_submit_dec" rows="6" class="large-text code"><?php echo esc_textarea((string) ($settings['submit_dec'] ?? '')); ?></textarea>
</td>
</tr>
</tbody>
</table>
<?php
}
public static function save(int $post_id, \WP_Post $post, bool $update): void {
if (wp_is_post_autosave($post_id) || wp_is_post_revision($post_id)) {
return;
}
if (!isset($_POST[self::NONCE_NAME]) || !wp_verify_nonce(sanitize_text_field(wp_unslash((string) $_POST[self::NONCE_NAME])), self::NONCE_ACTION)) {
return;
}
if (!self::is_links_page($post) || !current_user_can('edit_page', $post_id)) {
return;
}
$raw = wp_unslash($_POST);
$term_ids = self::sanitize_term_ids($raw['page_links_category'] ?? []);
$submit_term_ids = self::sanitize_term_ids($raw['page_links_submit_cats'] ?? []);
$search_types = array_values(array_intersect(array_map('sanitize_key', (array) ($raw['page_links_search_types'] ?? [])), array_keys(self::search_types())));
$style = sanitize_key((string) ($raw['page_links_style'] ?? 'card'));
$orderby = sanitize_key((string) ($raw['page_links_orderby'] ?? 'name'));
$order = strtoupper(sanitize_text_field((string) ($raw['page_links_order'] ?? 'ASC')));
$position = sanitize_key((string) ($raw['page_links_content_position'] ?? 'top'));
$values = [
'page_links_content_s' => !empty($raw['page_links_content_s']),
'page_links_content_position' => in_array($position, ['top', 'bottom'], true) ? $position : 'top',
'page_links_search_s' => !empty($raw['page_links_search_s']),
'page_links_search_types' => $search_types,
'page_links_category' => $term_ids,
'page_links_style' => in_array($style, ['card', 'bigcard', 'image'], true) ? $style : 'card',
'page_links_limit' => max(0, min(500, (int) ($raw['page_links_limit'] ?? 0))),
'page_links_orderby' => in_array($orderby, ['name', 'updated', 'rating', 'rand'], true) ? $orderby : 'name',
'page_links_order' => in_array($order, ['ASC', 'DESC'], true) ? $order : 'ASC',
'page_links_go_s' => !empty($raw['page_links_go_s']),
'page_links_blank_s' => !empty($raw['page_links_blank_s']),
'page_links_nofollow_s' => !empty($raw['page_links_nofollow_s']),
'page_links_submit_s' => !empty($raw['page_links_submit_s']),
'page_links_submit_sign_s' => !empty($raw['page_links_submit_sign_s']),
'page_links_submit_cats' => $submit_term_ids,
'page_links_submit_title' => sanitize_text_field((string) ($raw['page_links_submit_title'] ?? '')),
'page_links_submit_dec' => wp_kses_post((string) ($raw['page_links_submit_dec'] ?? '')),
];
foreach ($values as $key => $value) {
update_post_meta($post_id, $key, $value);
}
}
private static function is_links_page(\WP_Post $post): bool {
return $post->post_name === 'friends' || get_page_template_slug($post->ID) === 'templates/pages/page-friends.php';
}
/** @return array<string,string> */
private static function search_types(): array {
return [
'self' => __('站内', 'jio'),
'baidu' => __('百度', 'jio'),
'google' => __('谷歌', 'jio'),
'bing' => __('必应', 'jio'),
'sogou' => __('搜狗', 'jio'),
'360' => '360',
];
}
/** @param mixed $raw @return list<int> */
private static function sanitize_term_ids($raw): array {
$ids = array_values(array_unique(array_filter(array_map('absint', (array) $raw))));
return array_values(array_filter($ids, static function (int $id): bool {
$term = get_term($id, 'link_category');
return $term && !is_wp_error($term);
}));
}
}