fix(pay): render paid reading as fragments

This commit is contained in:
kate522 committed 2026-08-13 18:58:24 +08:00
1 parent 4cc1f2b2ea
commit f040a680d6
9 files changed
+316 -65

No files matched your search

+57
View File
@@ -16,6 +16,7 @@
var el = wp.element.createElement;
var Fragment = wp.element.Fragment;
var RawHTML = wp.element.RawHTML;
var registerBlockTypeRaw = wp.blocks.registerBlockType;
var blockEditor = wp.blockEditor || wp.editor || {};
var InspectorControls = blockEditor.InspectorControls || null;
@@ -28,6 +29,8 @@
var TextControl = wp.components.TextControl;
var ToggleControl = wp.components.ToggleControl;
var RangeControl = wp.components.RangeControl;
var Notice = wp.components.Notice;
var useSelect = window.wp.data && window.wp.data.useSelect;
if (!registerBlockTypeRaw || !el) return;
@@ -750,4 +753,58 @@
});
}
// ================================================================
// 9. Paid Fragment Block
// ================================================================
function usePaidReadingConfigured() {
if (!useSelect) return true;
return useSelect(function (select) {
var editor = select('core/editor');
if (!editor || !editor.getEditedPostAttribute) return true;
var meta = editor.getEditedPostAttribute('meta') || {};
var pay = meta._aksrt_pay || {};
if (pay.pay_type !== 'reading') return false;
return pay.pay_modo === 'points'
? Number(pay.points_price || 0) > 0
: Number(pay.price || 0) > 0;
}, []);
}
registerBlockType('aksrt/paid-fragment', {
title: '付费可见',
icon: 'lock',
category: CATEGORY,
description: '仅购买文章付费阅读后可见的片段。',
attributes: {
content: { type: 'string', default: '' },
},
edit: function (props) {
var configured = usePaidReadingConfigured();
return h('div', { className: 'aksrt-paid-fragment-block' },
!configured && Notice ? h(Notice, { status: 'warning', isDismissible: false },
'请在“付费内容设置”中选择“付费阅读”并设置有效价格;未配置时此片段会公开显示。'
) : null,
h('div', { className: 'aksrt-paid-fragment-block__label' },
h('span', { className: 'dashicons dashicons-lock', 'aria-hidden': true }),
'付费可见片段'
),
h(RichText, {
tagName: 'div',
className: 'aksrt-paid-fragment-block__content',
value: props.attributes.content,
onChange: function (value) { props.setAttributes({ content: value }); },
placeholder: '输入购买后可见的内容…',
})
);
},
save: function (props) {
// Save a single canonical shortcode so hand-written and block-authored
// content use the identical server-side rendering path.
var shortcode = '[hidecontent type="payshow"]' + props.attributes.content + '[/hidecontent]';
return RawHTML ? h(RawHTML, null, shortcode) : shortcode;
},
});
})();