feat(pay): mirror reference resource row with copy, icon and reorder

The post editor's download row only exposed address / button text / remark,
leaving copy_key, copy_val and class as hidden inputs with no UI, so the
reference copy area, custom button icon, text and color were unreachable.
The submission popup already emitted those keys, which made the two write
paths diverge from anything the editor could produce.

Expose the full reference field set (地址/备注/复制名称/复制内容/图标/文案/
颜色) as an accordion card with drag reordering, add the one genuinely new
`icon` key through aksrt_sanitize_icon_value(), and teach both frontend
download renderers to consume the fields they previously ignored.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
kate522andClaude Sonnet 4.6 committed 2026-09-24 20:27:30 +08:00
1 parent ab495854d5
commit b94c820361
10 files changed
+554 -37

No files matched your search

+20 -8
View File
@@ -24,6 +24,21 @@ class DomainModel {
public const RESOURCE_PAY_META_MIGRATED = 'aksrt_resource_pay_meta_migrated';
/**
* Allowed download-button color classes (legacy .but swatch system).
*
* Shared by the sanitizer and the meta box palette so the two can not drift.
*
* @var array<int, string>
*/
public const DOWNLOAD_BUTTON_CLASSES = [
'',
'c-red', 'c-yellow', 'c-cyan', 'c-blue', 'c-green', 'c-purple',
'b-red', 'b-yellow', 'b-cyan', 'b-blue', 'b-green', 'b-purple',
'jb-red', 'jb-pink', 'jb-yellow', 'jb-cyan', 'jb-blue', 'jb-green', 'jb-purple',
'jb-vip1', 'jb-vip2',
];
public const ORDER_PRODUCT_RESOURCE = 'resource';
public const ORDER_PRODUCT_VIP = 'vip';
public const ORDER_PRODUCT_RECHARGE = 'recharge';
@@ -174,7 +189,7 @@ class DomainModel {
/**
* @param mixed $value
* @return array<int, array{label:string,url:string,type:string,extract_code:string,copy_key:string,copy_val:string,class:string,attachment_id:int}>
* @return array<int, array{label:string,url:string,type:string,extract_code:string,copy_key:string,copy_val:string,icon:string,class:string,attachment_id:int}>
*/
public static function sanitize_downloads($value): array {
if (!is_array($value)) {
@@ -182,13 +197,7 @@ class DomainModel {
}
$allowed_types = ['direct', 'net_disk', 'external'];
$allowed_classes = [
'',
'c-red', 'c-yellow', 'c-cyan', 'c-blue', 'c-green', 'c-purple',
'b-red', 'b-yellow', 'b-cyan', 'b-blue', 'b-green', 'b-purple',
'jb-red', 'jb-pink', 'jb-yellow', 'jb-cyan', 'jb-blue', 'jb-green', 'jb-purple',
'jb-vip1', 'jb-vip2',
];
$allowed_classes = self::DOWNLOAD_BUTTON_CLASSES;
$downloads = [];
foreach ($value as $row) {
@@ -239,6 +248,9 @@ class DomainModel {
'extract_code' => $extract_code,
'copy_key' => isset($row['copy_key']) ? sanitize_text_field((string) $row['copy_key']) : '',
'copy_val' => isset($row['copy_val']) ? sanitize_text_field((string) $row['copy_val']) : '',
'icon' => isset($row['icon']) && function_exists('aksrt_sanitize_icon_value')
? aksrt_sanitize_icon_value((string) $row['icon'])
: (isset($row['icon']) ? sanitize_text_field((string) $row['icon']) : ''),
'class' => isset($row['class']) && in_array($row['class'], $allowed_classes, true)
? (string) $row['class']
: '',
+87 -4
View File
@@ -42,14 +42,24 @@ class ResourceMetaBox {
}
wp_enqueue_media();
if (function_exists('aksrt_icon_picker_assets')) {
aksrt_icon_picker_assets(['aksrt-pay-meta-box']);
}
$css = AKSRT_DIR . '/assets/css/admin/pay-meta-box.css';
$js = AKSRT_DIR . '/assets/js/admin-pay-meta-box.js';
if (is_file($css)) {
wp_enqueue_style('aksrt-pay-meta-box', AKSRT_URI . '/assets/css/admin/pay-meta-box.css', [], (string) filemtime($css));
wp_enqueue_style('aksrt-pay-meta-box', AKSRT_URI . '/assets/css/admin/pay-meta-box.css', ['aksrt-icon-picker'], (string) filemtime($css));
}
if (is_file($js)) {
wp_enqueue_script('aksrt-pay-meta-box', AKSRT_URI . '/assets/js/admin-pay-meta-box.js', [], (string) filemtime($js), true);
wp_enqueue_script('aksrt-pay-meta-box', AKSRT_URI . '/assets/js/admin-pay-meta-box.js', ['aksrt-icon-picker'], (string) filemtime($js), true);
}
wp_localize_script('aksrt-pay-meta-box', 'aksrtPayMetaBox', [
'buttonClasses' => DomainModel::DOWNLOAD_BUTTON_CLASSES,
'i18n' => [
'resource' => __('资源', 'aksrt'),
'downloadUrl' => __('下载地址', 'aksrt'),
],
]);
}
public function render(\WP_Post $post): void {
@@ -133,7 +143,7 @@ class ResourceMetaBox {
</div>
<div data-pay-type="download">
<?php $this->group('downloads', __('资源下载', 'aksrt'), (array) ($meta['downloads'] ?? []), 'download_row', __('添加资源', 'aksrt')); ?>
<?php $this->group('downloads', __('资源下载', 'aksrt'), (array) ($meta['downloads'] ?? []), 'download_row', __('添加资源', 'aksrt'), __('每个资源一张卡片,点击标题展开配置;拖动卡片左上角的手柄可调整下载源的显示顺序。', 'aksrt')); ?>
<?php $this->group('attributes', __('资源属性', 'aksrt'), (array) ($meta['attributes'] ?? []), 'attribute_row', __('添加属性', 'aksrt')); ?>
<?php $this->text('demo_url', __('演示地址', 'aksrt'), (string) ($meta['demo_url'] ?? ''), '', 'https://', 'url'); ?>
</div>
@@ -199,8 +209,81 @@ class ResourceMetaBox {
/** @param int|string $index */
private function download_row($index, array $row): void {
$url = (string) ($row['url'] ?? '');
$label = (string) ($row['label'] ?? '');
$extract_code = (string) ($row['extract_code'] ?? '');
$copy_key = (string) ($row['copy_key'] ?? '');
$copy_val = (string) ($row['copy_val'] ?? '');
$icon = (string) ($row['icon'] ?? '');
$class = (string) ($row['class'] ?? '');
$name = 'aksrt_pay[downloads][' . $index . ']';
$title = $label !== '' ? $label : ($url !== '' ? $url : __('下载地址', 'aksrt'));
?>
<div class="aksrt-pay-meta__group" data-group-row><div class="aksrt-pay-meta__upload"><input type="url" name="aksrt_pay[downloads][<?php echo esc_attr((string) $index); ?>][url]" value="<?php echo esc_attr((string) ($row['url'] ?? '')); ?>" placeholder="<?php esc_attr_e('上传文件或输入下载地址', 'aksrt'); ?>" data-media-input><button type="button" class="button" data-media-select data-library=""><?php esc_html_e('选择', 'aksrt'); ?></button></div><input type="text" name="aksrt_pay[downloads][<?php echo esc_attr((string) $index); ?>][label]" value="<?php echo esc_attr((string) ($row['label'] ?? '')); ?>" placeholder="<?php esc_attr_e('按钮文案', 'aksrt'); ?>"><textarea rows="1" name="aksrt_pay[downloads][<?php echo esc_attr((string) $index); ?>][extract_code]" placeholder="<?php esc_attr_e('资源备注、提取码或解压密码', 'aksrt'); ?>"><?php echo esc_textarea((string) ($row['extract_code'] ?? '')); ?></textarea><input type="hidden" name="aksrt_pay[downloads][<?php echo esc_attr((string) $index); ?>][copy_key]" value="<?php echo esc_attr((string) ($row['copy_key'] ?? '')); ?>"><input type="hidden" name="aksrt_pay[downloads][<?php echo esc_attr((string) $index); ?>][copy_val]" value="<?php echo esc_attr((string) ($row['copy_val'] ?? '')); ?>"><input type="hidden" name="aksrt_pay[downloads][<?php echo esc_attr((string) $index); ?>][class]" value="<?php echo esc_attr((string) ($row['class'] ?? '')); ?>"><button type="button" class="button-link-delete" data-group-remove><?php esc_html_e('移除', 'aksrt'); ?></button></div>
<div class="aksrt-pay-meta__group is-open" data-group-row>
<div class="aksrt-pay-meta__group-head">
<span class="aksrt-pay-meta__group-drag" data-group-drag title="<?php esc_attr_e('拖拽排序', 'aksrt'); ?>"><span class="dashicons dashicons-move"></span></span>
<button type="button" class="aksrt-pay-meta__group-toggle" data-group-toggle aria-expanded="true">
<span class="aksrt-pay-meta__group-title" data-group-title><?php echo esc_html($title); ?></span>
<span class="aksrt-pay-meta__group-caret dashicons dashicons-arrow-down"></span>
</button>
<button type="button" class="button-link-delete" data-group-remove><?php esc_html_e('移除', 'aksrt'); ?></button>
</div>
<div class="aksrt-pay-meta__group-body">
<div class="aksrt-pay-meta__group-field">
<label><?php esc_html_e('下载地址', 'aksrt'); ?></label>
<div class="aksrt-pay-meta__upload">
<input type="url" name="<?php echo esc_attr($name); ?>[url]" value="<?php echo esc_attr($url); ?>" placeholder="<?php esc_attr_e('上传文件或输入下载地址', 'aksrt'); ?>" data-media-input data-group-url>
<button type="button" class="button" data-media-select data-library=""><?php esc_html_e('选择', 'aksrt'); ?></button>
</div>
</div>
<div class="aksrt-pay-meta__group-field">
<label><?php esc_html_e('资源备注', 'aksrt'); ?></label>
<textarea rows="1" name="<?php echo esc_attr($name); ?>[extract_code]" placeholder="<?php esc_attr_e('按钮旁边的额外内容,例如提取码、解压密码等', 'aksrt'); ?>"><?php echo esc_textarea($extract_code); ?></textarea>
</div>
<div class="aksrt-pay-meta__group-field aksrt-pay-meta__group-field--split">
<div>
<label><?php esc_html_e('复制的名称', 'aksrt'); ?></label>
<input type="text" name="<?php echo esc_attr($name); ?>[copy_key]" value="<?php echo esc_attr($copy_key); ?>" placeholder="<?php esc_attr_e('例如:提取码', 'aksrt'); ?>">
</div>
<div>
<label><?php esc_html_e('复制的内容', 'aksrt'); ?></label>
<input type="text" name="<?php echo esc_attr($name); ?>[copy_val]" value="<?php echo esc_attr($copy_val); ?>" placeholder="<?php esc_attr_e('例如:a1b2', 'aksrt'); ?>">
</div>
</div>
<p class="description"><?php esc_html_e('为“资源备注”按钮添加点击复制功能,请设置复制名称和复制内容。', 'aksrt'); ?></p>
<div class="aksrt-pay-meta__group-field">
<label><?php esc_html_e('自定义按钮图标', 'aksrt'); ?></label>
<?php
if (function_exists('aksrt_icon_picker_field')) {
echo aksrt_icon_picker_field( // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
$name . '[icon]',
'aksrt-pay-download-icon-' . $index,
$icon,
['placeholder' => 'fa fas fa-download']
);
} else {
?>
<input type="text" name="<?php echo esc_attr($name); ?>[icon]" value="<?php echo esc_attr($icon); ?>" placeholder="fa fas fa-download">
<?php
}
?>
</div>
<div class="aksrt-pay-meta__group-field">
<label><?php esc_html_e('自定义按钮文案', 'aksrt'); ?></label>
<textarea rows="1" name="<?php echo esc_attr($name); ?>[label]" placeholder="<?php esc_attr_e('例如:立即下载', 'aksrt'); ?>" data-group-label><?php echo esc_textarea($label); ?></textarea>
</div>
<div class="aksrt-pay-meta__group-field">
<label><?php esc_html_e('自定义按钮颜色', 'aksrt'); ?></label>
<div class="aksrt-pay-meta__class-swatches" data-group-classes>
<?php foreach (DomainModel::DOWNLOAD_BUTTON_CLASSES as $swatch) : ?>
<button type="button" class="aksrt-pay-meta__class-swatch <?php echo $swatch !== '' ? esc_attr($swatch) : 'b-theme'; ?><?php echo $swatch === $class ? ' is-active' : ''; ?>" data-group-class="<?php echo esc_attr($swatch); ?>" aria-label="<?php echo $swatch !== '' ? esc_attr($swatch) : esc_attr__('默认', 'aksrt'); ?>" aria-pressed="<?php echo $swatch === $class ? 'true' : 'false'; ?>"></button>
<?php endforeach; ?>
</div>
<input type="hidden" name="<?php echo esc_attr($name); ?>[class]" value="<?php echo esc_attr($class); ?>" data-group-class-input>
<p class="description"><?php esc_html_e('按钮图标、文案、颜色默认均会自动获取,建议为空即可。', 'aksrt'); ?></p>
</div>
</div>
</div>
<?php
}
+1
View File
@@ -186,6 +186,7 @@ class Setup {
'extract_code' => ['type' => 'string'],
'copy_key' => ['type' => 'string'],
'copy_val' => ['type' => 'string'],
'icon' => ['type' => 'string'],
'class' => ['type' => 'string'],
],
],