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:
1 parent
ef7b5939ba
commit
4c562428b4
24 files changed
+792
-273
No files matched your search
@@ -48,23 +48,6 @@ class GlobalController extends ControllerBase {
|
||||
'permission_callback' => [$this, 'check_permission'],
|
||||
]);
|
||||
|
||||
// Friend Links
|
||||
register_rest_route(self::API_NAMESPACE, '/friend-links', [
|
||||
'methods' => ['GET', 'POST'],
|
||||
'callback' => [$this, 'rest_handle_friend_links'],
|
||||
'permission_callback' => [$this, 'check_permission'],
|
||||
]);
|
||||
register_rest_route(self::API_NAMESPACE, '/friend-links/(?P<id>\d+)', [
|
||||
'methods' => 'DELETE',
|
||||
'callback' => [$this, 'rest_delete_friend_link'],
|
||||
'permission_callback' => [$this, 'check_permission'],
|
||||
]);
|
||||
register_rest_route(self::API_NAMESPACE, '/friend-links/reorder', [
|
||||
'methods' => 'POST',
|
||||
'callback' => [$this, 'rest_reorder_friend_links'],
|
||||
'permission_callback' => [$this, 'check_permission'],
|
||||
]);
|
||||
|
||||
// CMS Features
|
||||
register_rest_route(self::API_NAMESPACE, '/settings/cms-features', [
|
||||
'methods' => ['GET', 'POST'],
|
||||
@@ -167,63 +150,6 @@ class GlobalController extends ControllerBase {
|
||||
return $this->success(['data' => $data]);
|
||||
}
|
||||
|
||||
public function rest_handle_friend_links(\WP_REST_Request $request): \WP_REST_Response {
|
||||
if ($request->get_method() === 'GET') {
|
||||
$links = jio_get_friend_links();
|
||||
return rest_ensure_response(['code' => 'success', 'data' => $links]);
|
||||
}
|
||||
|
||||
$params = $this->get_json_params($request);
|
||||
$links = get_option('jio_friend_links', []);
|
||||
$new_link = [
|
||||
'id' => uniqid('fl_'),
|
||||
'name' => sanitize_text_field($params['name'] ?? ''),
|
||||
'url' => esc_url_raw($params['url'] ?? ''),
|
||||
'logo' => esc_url_raw($params['logo'] ?? ''),
|
||||
'description' => sanitize_textarea_field($params['description'] ?? ''),
|
||||
'sort_order' => (int) ($params['sort_order'] ?? 0),
|
||||
'active' => 1,
|
||||
];
|
||||
$links[] = $new_link;
|
||||
usort($links, function ($a, $b) {
|
||||
return ($a['sort_order'] ?? 0) <=> ($b['sort_order'] ?? 0);
|
||||
});
|
||||
update_option('jio_friend_links', $links);
|
||||
do_action('jio_friend_link_submitted', $new_link);
|
||||
|
||||
return rest_ensure_response(['code' => 'success', 'data' => $new_link, 'message' => __('Friend link added.', 'jio')]);
|
||||
}
|
||||
|
||||
public function rest_delete_friend_link(\WP_REST_Request $request): \WP_REST_Response {
|
||||
$id = $request->get_param('id');
|
||||
$links = get_option('jio_friend_links', []);
|
||||
$links = array_filter($links, function ($link) use ($id) {
|
||||
return ($link['id'] ?? '') !== $id;
|
||||
});
|
||||
$links = array_values($links);
|
||||
update_option('jio_friend_links', $links);
|
||||
return rest_ensure_response(['code' => 'success', 'message' => __('Friend link deleted.', 'jio')]);
|
||||
}
|
||||
|
||||
public function rest_reorder_friend_links(\WP_REST_Request $request): \WP_REST_Response {
|
||||
$ordered_ids = $request->get_param('ids');
|
||||
if (!is_array($ordered_ids)) {
|
||||
return new \WP_REST_Response(['code' => 'error', 'message' => __('参数错误', 'jio')], 400);
|
||||
}
|
||||
$links = get_option('jio_friend_links', []);
|
||||
$order_map = array_flip($ordered_ids);
|
||||
usort($links, function ($a, $b) use ($order_map) {
|
||||
$idx_a = $order_map[($a['id'] ?? '')] ?? 999;
|
||||
$idx_b = $order_map[($b['id'] ?? '')] ?? 999;
|
||||
return $idx_a <=> $idx_b;
|
||||
});
|
||||
foreach ($links as $index => &$link) {
|
||||
$link['sort_order'] = $index;
|
||||
}
|
||||
update_option('jio_friend_links', $links);
|
||||
return rest_ensure_response(['code' => 'success', 'message' => __('Order saved.', 'jio')]);
|
||||
}
|
||||
|
||||
public function rest_handle_footer_tabbar(\WP_REST_Request $request): \WP_REST_Response {
|
||||
if ($request->get_method() === 'GET') {
|
||||
$settings = class_exists('\Jio\Settings\Settings')
|
||||
|
||||
@@ -0,0 +1,89 @@
|
||||
<?php
|
||||
/**
|
||||
* Native WordPress link administration enhancements.
|
||||
*
|
||||
* @package Jio
|
||||
*/
|
||||
|
||||
namespace Jio\Admin;
|
||||
|
||||
defined('ABSPATH') || exit;
|
||||
|
||||
final class LinkManager {
|
||||
public static function init(): void {
|
||||
add_action('admin_bar_menu', [self::class, 'add_toolbar_node'], 999);
|
||||
add_filter('manage_link-manager_columns', [self::class, 'filter_columns']);
|
||||
add_action('manage_link_custom_column', [self::class, 'render_column'], 10, 2);
|
||||
add_action('add_meta_boxes_link', [self::class, 'add_edit_help']);
|
||||
}
|
||||
|
||||
public static function add_toolbar_node(\WP_Admin_Bar $admin_bar): void {
|
||||
if (!current_user_can('manage_links')) {
|
||||
return;
|
||||
}
|
||||
$count = self::pending_count();
|
||||
$title = '<span class="ab-icon dashicons-before dashicons-admin-links"></span>' . ($count > 0 ? (string) $count : '');
|
||||
$admin_bar->add_node([
|
||||
'id' => 'jio-links',
|
||||
'title' => $title,
|
||||
'href' => admin_url('link-manager.php' . ($count > 0 ? '?orderby=visible&order=asc' : '')),
|
||||
'meta' => ['title' => $count > 0 ? sprintf(__('有 %d 个链接待审核', 'jio'), $count) : __('链接管理', 'jio')],
|
||||
]);
|
||||
}
|
||||
|
||||
/** @param array<string,string> $columns @return array<string,string> */
|
||||
public static function filter_columns(array $columns): array {
|
||||
$columns['visible'] = sprintf(
|
||||
__('可见性(待审 %d)', 'jio'),
|
||||
self::pending_count()
|
||||
);
|
||||
unset($columns['rel']);
|
||||
$columns['link_description'] = __('简介', 'jio');
|
||||
$columns['link_image'] = __('Logo', 'jio');
|
||||
return $columns;
|
||||
}
|
||||
|
||||
public static function render_column(string $column, int $link_id): void {
|
||||
$link = get_bookmark($link_id);
|
||||
if (!$link) {
|
||||
return;
|
||||
}
|
||||
switch ($column) {
|
||||
case 'visible':
|
||||
echo !empty($link->link_visible)
|
||||
? esc_html__('已公开', 'jio')
|
||||
: '<strong>' . esc_html__('待审核', 'jio') . '</strong>';
|
||||
break;
|
||||
case 'link_description':
|
||||
echo esc_html((string) $link->link_description);
|
||||
break;
|
||||
case 'link_image':
|
||||
$image = esc_url((string) $link->link_image);
|
||||
if ($image !== '') {
|
||||
printf('<img src="%s" alt="" width="50" height="50" style="object-fit:cover">', $image);
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
public static function add_edit_help(): void {
|
||||
add_meta_box(
|
||||
'jio-link-fields-help',
|
||||
__('链接资料说明', 'jio'),
|
||||
static function (): void {
|
||||
echo '<p>' . esc_html__('链接图像、RSS、备注和评分可在链接编辑页的“高级”区域设置;将链接设为私密即可保持待审核状态。', 'jio') . '</p>';
|
||||
},
|
||||
'link',
|
||||
'side',
|
||||
'high'
|
||||
);
|
||||
}
|
||||
|
||||
private static function pending_count(): int {
|
||||
global $wpdb;
|
||||
return (int) $wpdb->get_var($wpdb->prepare(
|
||||
"SELECT COUNT(*) FROM {$wpdb->links} WHERE link_visible != %s",
|
||||
'Y'
|
||||
));
|
||||
}
|
||||
}
|
||||
@@ -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);
|
||||
}));
|
||||
}
|
||||
}
|
||||
@@ -83,6 +83,7 @@ class SchemaPermissions {
|
||||
['label' => '打赏用户', 'value' => 'tip_user'],
|
||||
['label' => '提交工单', 'value' => 'ticket_create'],
|
||||
['label' => '回复工单', 'value' => 'ticket_reply'],
|
||||
['label' => '友情链接申请', 'value' => 'friend_link'],
|
||||
]],
|
||||
]],
|
||||
['key' => 'providers', 'title' => '服务商配置', 'dependsOn' => ['key' => 'enabled', 'value' => true], 'fields' => $provider_fields],
|
||||
|
||||
@@ -0,0 +1,214 @@
|
||||
<?php
|
||||
/**
|
||||
* Public submission of native WordPress links.
|
||||
*
|
||||
* @package Jio
|
||||
*/
|
||||
|
||||
namespace Jio\Core;
|
||||
|
||||
defined('ABSPATH') || exit;
|
||||
|
||||
final class LinksSubmission {
|
||||
private const ACTION = 'jio_frontend_links_submit';
|
||||
private const NONCE_ACTION_PREFIX = 'jio_frontend_links_submit_';
|
||||
private const RATE_LIMIT_SECONDS = 30;
|
||||
|
||||
public static function init(): void {
|
||||
add_action('wp_ajax_' . self::ACTION, [self::class, 'handle']);
|
||||
add_action('wp_ajax_nopriv_' . self::ACTION, [self::class, 'handle']);
|
||||
}
|
||||
|
||||
public static function handle(): void {
|
||||
$page_id = absint($_POST['page_id'] ?? 0);
|
||||
$page = get_post($page_id);
|
||||
if (!$page instanceof \WP_Post || !self::is_links_page($page) || $page->post_status !== 'publish') {
|
||||
self::fail(__('申请页面无效,请刷新后重试。', 'jio'), 400);
|
||||
}
|
||||
if (!isset($_POST['_wpnonce']) || !wp_verify_nonce(
|
||||
sanitize_text_field(wp_unslash((string) $_POST['_wpnonce'])),
|
||||
self::NONCE_ACTION_PREFIX . $page_id
|
||||
)) {
|
||||
self::fail(__('安全验证失败,请刷新页面后重试。', 'jio'), 403);
|
||||
}
|
||||
|
||||
$settings = jio_get_page_links_settings($page_id);
|
||||
if (empty($settings['submit_s'])) {
|
||||
self::fail(__('当前页面未开放链接申请。', 'jio'), 403);
|
||||
}
|
||||
if (!empty($settings['submit_sign_s']) && !is_user_logged_in()) {
|
||||
self::fail(__('请先登录后再申请。', 'jio'), 401);
|
||||
}
|
||||
|
||||
$payload = wp_unslash($_POST);
|
||||
if (function_exists('jio_require_captcha')) {
|
||||
$captcha = jio_require_captcha('friend_link', is_array($payload) ? $payload : []);
|
||||
if (is_wp_error($captcha)) {
|
||||
self::fail($captcha->get_error_message(), 400);
|
||||
}
|
||||
}
|
||||
|
||||
$ip = function_exists('jio_get_client_ip')
|
||||
? (string) jio_get_client_ip()
|
||||
: (string) ($_SERVER['REMOTE_ADDR'] ?? '0.0.0.0');
|
||||
if (function_exists('jio_captcha_rate_limited') && jio_captcha_rate_limited('friend-link-submit', $ip, 1, self::RATE_LIMIT_SECONDS)) {
|
||||
self::fail(__('操作过于频繁,请稍候再试。', 'jio'), 429);
|
||||
}
|
||||
|
||||
$name = sanitize_text_field((string) ($payload['link_name'] ?? ''));
|
||||
$name = function_exists('mb_substr') ? mb_substr($name, 0, 255) : substr($name, 0, 255);
|
||||
$description = sanitize_text_field((string) ($payload['link_description'] ?? ''));
|
||||
$description = function_exists('mb_substr') ? mb_substr($description, 0, 255) : substr($description, 0, 255);
|
||||
$url = self::sanitize_url((string) ($payload['link_url'] ?? '')); if ($name === '') {
|
||||
self::fail(__('请填写网站名称。', 'jio'));
|
||||
}
|
||||
if ($url === '') {
|
||||
self::fail(__('请填写有效的网站地址。', 'jio'));
|
||||
}
|
||||
|
||||
$category_ids = self::allowed_categories($settings);
|
||||
$submitted_category = absint($payload['link_category'] ?? 0);
|
||||
$categories = [];
|
||||
if ($category_ids === [] && $submitted_category > 0) {
|
||||
self::fail(__('请选择有效的网站分类。', 'jio'));
|
||||
}
|
||||
if ($category_ids !== []) {
|
||||
if ($submitted_category <= 0 || !in_array($submitted_category, $category_ids, true)) {
|
||||
self::fail(__('请选择有效的网站分类。', 'jio'));
|
||||
}
|
||||
$categories[] = $submitted_category;
|
||||
}
|
||||
if (self::duplicate_url($url)) {
|
||||
self::fail(__('该链接已存在,请勿重复提交。', 'jio'), 409);
|
||||
}
|
||||
|
||||
$image_url = self::upload_image();
|
||||
if (is_wp_error($image_url)) {
|
||||
self::fail($image_url->get_error_message());
|
||||
}
|
||||
|
||||
if (!function_exists('wp_insert_link')) {
|
||||
require_once ABSPATH . 'wp-admin/includes/bookmark.php';
|
||||
}
|
||||
$link_data = [
|
||||
'link_name' => $name,
|
||||
'link_url' => $url,
|
||||
'link_description' => $description,
|
||||
'link_image' => $image_url,
|
||||
'link_visible' => is_super_admin() ? 'Y' : 'N',
|
||||
'link_category' => $categories,
|
||||
];
|
||||
$link_id = wp_insert_link($link_data, true);
|
||||
if (is_wp_error($link_id) || (int) $link_id <= 0) {
|
||||
self::fail(__('链接保存失败,请稍后重试。', 'jio'), 500);
|
||||
}
|
||||
|
||||
if ($link_data['link_visible'] !== 'Y') {
|
||||
do_action('jio_friend_link_submitted', array_merge($link_data, [
|
||||
'link_id' => (int) $link_id,
|
||||
'username' => is_user_logged_in() ? wp_get_current_user()->display_name : __('访客', 'jio'),
|
||||
]));
|
||||
}
|
||||
wp_send_json_success([
|
||||
'message' => $link_data['link_visible'] === 'Y'
|
||||
? __('链接提交成功。', 'jio')
|
||||
: __('链接提交成功,等待管理员审核。', 'jio'),
|
||||
]);
|
||||
}
|
||||
|
||||
private static function is_links_page(\WP_Post $page): bool {
|
||||
return $page->post_type === 'page'
|
||||
&& ($page->post_name === 'friends' || get_page_template_slug($page->ID) === 'templates/pages/page-friends.php');
|
||||
}
|
||||
|
||||
/** @param array<string,mixed> $settings @return list<int> */
|
||||
private static function allowed_categories(array $settings): array {
|
||||
$configured = array_values(array_filter(array_map('absint', (array) ($settings['submit_cats'] ?? []))));
|
||||
if ($configured !== []) {
|
||||
$ids = $configured;
|
||||
} else {
|
||||
$terms = get_terms(['taxonomy' => 'link_category', 'hide_empty' => false, 'fields' => 'ids']);
|
||||
$ids = is_wp_error($terms) ? [] : array_map('absint', (array) $terms);
|
||||
}
|
||||
$ids = array_values(array_unique($ids));
|
||||
return array_values(array_filter($ids, static function (int $id): bool {
|
||||
$term = get_term($id, 'link_category');
|
||||
return $term && !is_wp_error($term);
|
||||
}));
|
||||
}
|
||||
|
||||
private static function sanitize_url(string $raw): string {
|
||||
$url = esc_url_raw(trim($raw), ['http', 'https']);
|
||||
$parts = wp_parse_url($url);
|
||||
if (!is_array($parts) || empty($parts['host']) || !in_array(strtolower((string) ($parts['scheme'] ?? '')), ['http', 'https'], true)) {
|
||||
return '';
|
||||
}
|
||||
if (isset($parts['user']) || isset($parts['pass'])) {
|
||||
return '';
|
||||
}
|
||||
return $url;
|
||||
}
|
||||
|
||||
private static function duplicate_url(string $url): bool {
|
||||
global $wpdb;
|
||||
$target = self::normalize_url($url);
|
||||
$urls = $wpdb->get_col($wpdb->prepare("SELECT link_url FROM {$wpdb->links} WHERE link_url <> %s", ''));
|
||||
foreach ((array) $urls as $existing) {
|
||||
if (self::normalize_url((string) $existing) === $target) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
private static function normalize_url(string $url): string {
|
||||
$parts = wp_parse_url($url);
|
||||
if (!is_array($parts) || empty($parts['host'])) {
|
||||
return strtolower(trim($url));
|
||||
}
|
||||
$scheme = strtolower((string) ($parts['scheme'] ?? 'http'));
|
||||
$host = strtolower((string) $parts['host']);
|
||||
$port = isset($parts['port']) && !in_array([(string) $scheme, (int) $parts['port']], [['http', 80], ['https', 443]], true)
|
||||
? ':' . (int) $parts['port']
|
||||
: '';
|
||||
$path = (string) ($parts['path'] ?? '/');
|
||||
$path = $path === '/' ? '/' : rtrim($path, '/');
|
||||
$query = isset($parts['query']) ? '?' . $parts['query'] : '';
|
||||
return $scheme . '://' . $host . $port . $path . $query;
|
||||
}
|
||||
|
||||
/** @return string|\WP_Error */
|
||||
private static function upload_image() {
|
||||
$file = $_FILES['image'] ?? null;
|
||||
if (!is_array($file) || (int) ($file['error'] ?? UPLOAD_ERR_NO_FILE) === UPLOAD_ERR_NO_FILE) {
|
||||
return '';
|
||||
}
|
||||
if ((int) ($file['error'] ?? UPLOAD_ERR_OK) !== UPLOAD_ERR_OK) {
|
||||
return new \WP_Error('jio_link_upload', __('Logo 上传失败,请重试。', 'jio'));
|
||||
}
|
||||
if ((int) ($file['size'] ?? 0) > 3 * 1024 * 1024) {
|
||||
return new \WP_Error('jio_link_upload', __('Logo 图片不能超过 3MB。', 'jio'));
|
||||
}
|
||||
|
||||
require_once ABSPATH . 'wp-admin/includes/file.php';
|
||||
require_once ABSPATH . 'wp-admin/includes/image.php';
|
||||
require_once ABSPATH . 'wp-admin/includes/media.php';
|
||||
$attachment_id = media_handle_upload('image', 0, [], [
|
||||
'test_form' => false,
|
||||
'mimes' => [
|
||||
'jpg|jpeg|jpe' => 'image/jpeg',
|
||||
'png' => 'image/png',
|
||||
'gif' => 'image/gif',
|
||||
],
|
||||
]);
|
||||
if (is_wp_error($attachment_id)) {
|
||||
return new \WP_Error('jio_link_upload', __('Logo 上传失败,请检查图片格式和大小。', 'jio'));
|
||||
}
|
||||
$image_url = wp_get_attachment_image_url((int) $attachment_id, 'medium');
|
||||
return $image_url ? esc_url_raw($image_url) : new \WP_Error('jio_link_upload', __('Logo 读取失败,请重试。', 'jio'));
|
||||
}
|
||||
|
||||
private static function fail(string $message, int $status = 400): void {
|
||||
wp_send_json_error(['message' => $message], $status);
|
||||
}
|
||||
}
|
||||
+30
-11
@@ -536,20 +536,39 @@ trait ScriptsPart1b {
|
||||
);
|
||||
}
|
||||
|
||||
// 友情链接页:路由专属 CSS
|
||||
// 友情链接页:原生链接列表 + 申请表单
|
||||
if (is_page_template('templates/pages/page-friends.php')) {
|
||||
wp_enqueue_style(
|
||||
'jio-route-friends',
|
||||
$base_uri . '/assets/css/routes/friends.css',
|
||||
['jio-public'],
|
||||
file_exists(get_template_directory() . '/assets/css/routes/friends.css')
|
||||
? (string) filemtime(get_template_directory() . '/assets/css/routes/friends.css')
|
||||
: $version
|
||||
);
|
||||
$links_css = get_template_directory() . '/assets/css/routes/links.css';
|
||||
if (file_exists($links_css)) {
|
||||
wp_enqueue_style(
|
||||
'jio-route-links',
|
||||
$base_uri . '/assets/css/routes/links.css',
|
||||
['jio-public'],
|
||||
(string) filemtime($links_css)
|
||||
);
|
||||
}
|
||||
$links_js = get_template_directory() . '/assets/js/links-submit.js';
|
||||
if (file_exists($links_js)) {
|
||||
wp_enqueue_script(
|
||||
'jio-links-submit',
|
||||
$base_uri . '/assets/js/links-submit.js',
|
||||
['loader_js'],
|
||||
(string) filemtime($links_js),
|
||||
true
|
||||
);
|
||||
wp_localize_script('jio-links-submit', 'jioLinksSubmit', [
|
||||
'ajaxUrl' => admin_url('admin-ajax.php'),
|
||||
'i18n' => [
|
||||
'submitting' => __('正在提交…', 'jio'),
|
||||
'success' => __('提交成功', 'jio'),
|
||||
'error' => __('提交失败,请重试', 'jio'),
|
||||
'captcha' => __('请完成人机验证', 'jio'),
|
||||
],
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
// 网址导航页(legacy links.php)
|
||||
// 文章导航页(legacy postsnavs.php)
|
||||
// 文章导航页(postsnavs.php)
|
||||
if (is_page_template('templates/pages/page-posts-navs.php')) {
|
||||
$page_navs_js = get_template_directory() . '/assets/js/page-navs.js';
|
||||
if (file_exists($page_navs_js)) {
|
||||
|
||||
@@ -26,6 +26,7 @@ class Setup {
|
||||
add_action('after_setup_theme', [$this, 'load_textdomain'], 1);
|
||||
add_action('after_setup_theme', [$this, 'setup_theme']);
|
||||
add_filter('dynamic_sidebar_params', [$this, 'filter_sidebar_widget_params']);
|
||||
add_filter('pre_option_link_manager_enabled', '__return_true');
|
||||
add_action('init', [$this, 'register_nav_menus'], 0);
|
||||
add_action('init', [$this, 'register_post_types'], 1);
|
||||
add_action('init', [$this, 'register_meta_fields'], 5);
|
||||
|
||||
@@ -189,7 +189,6 @@ trait DisplayPageGroups {
|
||||
'tabbar' => self::get_footer_tabbar_settings(),
|
||||
'floating_buttons' => self::get_floating_buttons_settings(),
|
||||
'header_theme' => self::get_header_theme_config(),
|
||||
'friend_links' => self::get_friend_links(),
|
||||
'cms_features' => self::get_cms_features(),
|
||||
'page_display' => self::get_page_display_settings(),
|
||||
'footer_theme' => self::get_footer_theme_config(),
|
||||
|
||||
@@ -1,26 +0,0 @@
|
||||
<?php
|
||||
/**
|
||||
* @package Jio
|
||||
*/
|
||||
|
||||
namespace Jio\Settings;
|
||||
|
||||
defined('ABSPATH') || exit;
|
||||
|
||||
class DisplayRuntime {
|
||||
|
||||
/** 获取友链列表 */
|
||||
public static function get_friend_links(): array {
|
||||
$links = get_option('jio_friend_links', []) ?: [];
|
||||
|
||||
return array_values(array_filter($links, static function ($link) {
|
||||
return !empty($link['active']);
|
||||
}));
|
||||
}
|
||||
|
||||
/** 保存友链 */
|
||||
public static function save_friend_links(array $links): void {
|
||||
update_option('jio_friend_links', $links);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -220,7 +220,30 @@ final class MailNotifications {
|
||||
}
|
||||
|
||||
public static function friend_link_submitted(array $link): void {
|
||||
self::send_admin('links_submit_admin', ['title' => (string) ($link['name'] ?? ''), 'username' => __('访客', 'jio'), 'url' => (string) ($link['url'] ?? '')]);
|
||||
$url = admin_url('link-manager.php?orderby=visible&order=asc');
|
||||
self::send_admin('links_submit_admin', [
|
||||
'title' => (string) ($link['link_name'] ?? $link['name'] ?? ''),
|
||||
'username' => (string) ($link['username'] ?? __('访客', 'jio')),
|
||||
'url' => (string) ($link['link_url'] ?? $link['url'] ?? $url),
|
||||
'review_url' => $url,
|
||||
]);
|
||||
|
||||
if (!class_exists('\Jio\Messages\Message') || !class_exists('\Jio\Settings\Integration') || !\Jio\Settings\Integration::message_enabled()) {
|
||||
return;
|
||||
}
|
||||
$title = sprintf(__('新的链接待审核:%s', 'jio'), (string) ($link['link_name'] ?? $link['name'] ?? ''));
|
||||
$content = sprintf(
|
||||
__('网站有新的链接提交:%1$s(%2$s),提交人:%3$s。', 'jio'),
|
||||
(string) ($link['link_name'] ?? $link['name'] ?? ''),
|
||||
(string) ($link['link_url'] ?? $link['url'] ?? ''),
|
||||
(string) ($link['username'] ?? __('访客', 'jio'))
|
||||
);
|
||||
foreach (get_users(['role' => 'administrator', 'fields' => 'ID', 'number' => 20]) as $admin_id) {
|
||||
\Jio\Messages\Message::get_instance()->send((int) $admin_id, 'system', $content, [
|
||||
'title' => $title,
|
||||
'target_url' => $url,
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
/** @param array<string,mixed> $vars */
|
||||
|
||||
@@ -13,7 +13,7 @@ final class MailTemplates {
|
||||
/** @return array<string, array<string, mixed>> */
|
||||
public static function definitions(): array {
|
||||
return [
|
||||
'links_submit_admin' => self::event('友情链接提交通知', '新友情链接提交:{title}\n提交人:{username}\n链接:{url}\n请登录后台审核。', ['title', 'username', 'url']),
|
||||
'links_submit_admin' => self::event('友情链接提交通知', '新友情链接提交:{title}\n提交人:{username}\n链接:{url}\n请登录后台审核:{review_url}', ['title', 'username', 'url', 'review_url']),
|
||||
'order_paid_admin' => self::event('新订单通知(管理员)', '收到一笔新订单。\n订单号:{order_number}\n用户:{username}\n商品:{product}\n金额:{amount} {currency}\n支付方式:{payment_method}', ['order_number', 'username', 'product', 'amount', 'currency', 'payment_method']),
|
||||
'withdraw_submitted_admin' => self::event('提现申请通知(管理员)', '用户 {username} 提交了提现申请。\n订单号:{withdrawal_number}\n金额:{amount}\n方式:{method}', ['username', 'withdrawal_number', 'amount', 'method']),
|
||||
'post_pending_admin' => self::event('投稿待审核通知', '用户 {username} 提交了待审核内容:{title}\n查看地址:{url}', ['username', 'title', 'url']),
|
||||
|
||||
@@ -40,7 +40,6 @@ class Settings {
|
||||
'jio_floating_buttons',
|
||||
'jio_credit_settings',
|
||||
'jio_notification_settings',
|
||||
'jio_friend_links',
|
||||
'jio_category_recommend',
|
||||
];
|
||||
|
||||
@@ -705,21 +704,6 @@ class Settings {
|
||||
public static function apply_list_per_page($query): void {
|
||||
PostDisplay::apply_list_per_page($query);
|
||||
}
|
||||
|
||||
|
||||
|
||||
/** 获取友链列表 */
|
||||
public static function get_friend_links(): array {
|
||||
return DisplayRuntime::get_friend_links();
|
||||
}
|
||||
|
||||
|
||||
|
||||
/** 保存友链 */
|
||||
public static function save_friend_links(array $links): void {
|
||||
DisplayRuntime::save_friend_links($links);
|
||||
}
|
||||
|
||||
/** 获取微信公众号登录完整配置。 */
|
||||
public static function get_weixingzh_settings(): array {
|
||||
return Integration::get_weixingzh_settings();
|
||||
|
||||
Reference in new issue
Block a user