Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
40 commits
Select commit Hold shift + click to select a range
7200284
docs: design field group location rules
coowinit Oct 18, 2026
5f0c534
feat: normalize field group location rules
coowinit Oct 18, 2026
fbb8afa
feat: validate field group location rules
coowinit Oct 18, 2026
abacba7
feat: load page options for field group rules
coowinit Oct 18, 2026
495b978
feat: add field group display conditions UI
coowinit Oct 18, 2026
06d9505
feat: match field groups to object location rules
coowinit Oct 18, 2026
2ce52cd
feat: apply location rules to meta boxes and saves
coowinit Oct 18, 2026
e44cda6
fix: enforce page location rules for REST meta access
coowinit Oct 18, 2026
4a399db
feat: toggle page selector for location rules
coowinit Oct 18, 2026
62efa28
fix: simplify page title rendering in location selector
coowinit Oct 18, 2026
7ac4e71
style: format field group location controls
coowinit Oct 18, 2026
450e45d
feat: export portable page location references
coowinit Oct 18, 2026
aceb4ac
feat: import schema v1 and v2 location rules
coowinit Oct 18, 2026
368e691
feat: bump export schema to v2 for location rules
coowinit Oct 18, 2026
290652e
fix: require Page content type for page location rules
coowinit Oct 18, 2026
d0662e5
docs: describe field group location rules in development
coowinit Oct 18, 2026
0994bd2
fix: correct importer error class namespace
coowinit Oct 18, 2026
ab4c2c8
fix: correct PHP namespace escaping in importer
coowinit Oct 18, 2026
d6d081c
fix: fail closed on invalid location modes
coowinit Oct 18, 2026
ef0b1a7
fix: skip groups with invalid location rules
coowinit Oct 18, 2026
4450ad7
fix: restrict page rules to Pages only
coowinit Oct 18, 2026
c5bb0ea
fix: fail closed for invalid REST location rules
coowinit Oct 18, 2026
f7938a9
fix: preserve invalid location state in export
coowinit Oct 18, 2026
ace56ba
fix: reject invalid location rules during import
coowinit Oct 18, 2026
eab36a8
fix: fail closed when field group location is invalid
coowinit Oct 18, 2026
e9fb98f
fix: correct invalid location import error namespace
coowinit Oct 18, 2026
f90a72e
docs: align schema documentation with v2 development
coowinit Oct 18, 2026
162a1a3
fix: fail closed on malformed location config
coowinit Oct 18, 2026
b8c545f
fix: reject malformed location config in schema v2
coowinit Oct 18, 2026
4380edd
fix: preserve malformed location config during export
coowinit Oct 18, 2026
3a01066
ui: clarify field group location settings
coowinit Oct 18, 2026
a2536c6
ui: constrain post types for page-only location mode
coowinit Oct 18, 2026
2962c81
style: improve field group location controls
coowinit Oct 18, 2026
6dbaf7e
ui: clarify location mode labels
coowinit Oct 18, 2026
cb3c2bc
fix: correct PHP template syntax for location labels
coowinit Oct 18, 2026
f356ebd
fix: correct PHP template syntax for specific page label
coowinit Oct 18, 2026
33a31b7
fix: correct PHP namespace syntax in importer
coowinit Oct 18, 2026
19f44be
ci: add PHP syntax checks for plugin files
coowinit Oct 18, 2026
11be415
release: bump plugin version to 1.1.0
coowinit Oct 18, 2026
7241ff7
docs: prepare v1.1.0 release notes
coowinit Oct 18, 2026
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions .github/workflows/php-lint.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
name: PHP Syntax Check

on:
push:
branches:
- main
- feature/field-group-location-rules
pull_request:
branches:
- main

jobs:
php-lint:
name: PHP lint
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout@v4

- name: Set up PHP
uses: shivammathur/setup-php@v2
with:
php-version: '7.4'
coverage: none

- name: Check PHP syntax
run: |
find . -type f -name '*.php' -not -path './.git/*' -print0 \
| xargs -0 -n 1 php -l
30 changes: 26 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,20 @@

插件以 WordPress 原生 API 为核心:字段定义保存到 `wp_options`,实际字段值保存到 `wp_postmeta`,并通过 `register_post_meta()` 接入 WordPress REST API。

当前稳定版本:**v1.0.0**
Schema:**v1**
当前版本:**v1.1.0**(新增字段组显示范围)
Schema:**v2(兼容导入 v1)**

## 字段组显示范围

字段组支持配置后台显示范围:

- 默认模式:字段组显示在已关联的内容类型中,保持原有行为。
- 指定页面模式:字段组仅显示在所选 WordPress Page 的编辑界面。
- 显示条件只影响后台字段组的适用范围,不会删除已有文章的字段值。
- JSON Schema v2 使用页面路径引用来迁移指定页面规则;目标站点找不到对应页面时会中止导入,避免意外扩大显示范围。
- Schema v1 导入仍兼容,旧字段组默认按原来的全局关联规则处理。

此功能已纳入 v1.1.0 发布范围。

## 项目定位

Expand Down Expand Up @@ -72,6 +84,7 @@ key
title
description
post_types[]
location
fields[]
```

Expand Down Expand Up @@ -248,7 +261,7 @@ JSON 顶层结构:
```json
{
"format": "wem-content-fields",
"schema_version": 1,
"schema_version": 2,
"plugin_version": "1.0.0",
"exported_at": "2026-09-16T00:00:00+00:00",
"field_groups": {}
Expand Down Expand Up @@ -290,6 +303,7 @@ JSON 顶层结构:
- Field Key 已存在
- Meta Key 已存在
- Schema 数据不合法
- 指定页面规则无效或无法在目标站点唯一匹配
- Field / Option 定义不合法

如果目标网站当前还没有对应 Post Type,只显示“当前不可用”提醒,不阻止导入。以后该 CPT 注册后,字段组会自动重新生效。
Expand Down Expand Up @@ -436,7 +450,7 @@ v1.0.0 暂不包含:
- User Meta
- Repeater
- Relationship
- Conditional Logic
- 字段级 Conditional Logic(根据其他字段值动态显示/隐藏)
- Gallery / 多图字段
- 通用文件字段
- Frontend Form
Expand All @@ -450,6 +464,14 @@ v1.0.0 暂不包含:

## 版本

### v1.1.0

- 新增字段组显示范围:全部关联内容类型或指定 WordPress Pages。
- 指定页面模式仅适用于 Pages,并支持选择多个页面。
- REST API Meta 授权遵守字段组的页面显示范围。
- JSON Schema v2 导出页面路径引用,导入时映射到目标站点页面;继续兼容 Schema v1 导入。
- 增加 GitHub Actions PHP 语法检查。

### v1.0.0

首个稳定版本:
Expand Down
1 change: 1 addition & 0 deletions admin/class-admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ private function render_form( ?string $key ): void {
);

$available_post_types = $this->available_post_types( (array) $config['post_types'] );
$available_pages = get_pages( [ 'sort_column' => 'post_title', 'sort_order' => 'ASC', 'post_status' => [ 'publish', 'draft', 'private', 'pending' ] ] );
$field_type_labels = $this->types->labels();
$field_settings = $this->types->settings_schemas();
include WEM_CF_PATH . 'admin/views/field-group-edit.php';
Expand Down
8 changes: 5 additions & 3 deletions admin/class-meta-boxes.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@ public function assets(): void {

$has_editor = false;
$has_media = false;
foreach ( $this->definitions->for_post_type( (string) $screen->post_type ) as $group ) {
$post_id = isset( $_GET['post'] ) ? absint( $_GET['post'] ) : 0;
foreach ( $this->definitions->for_object( (string) $screen->post_type, $post_id ) as $group ) {
foreach ( (array) ( $group['fields'] ?? [] ) as $field ) {
$field_type = (string) ( $field['type'] ?? '' );
if ( 'editor' === $field_type ) {
Expand Down Expand Up @@ -84,7 +85,8 @@ public function add_meta_boxes( string $post_type, $post ): void {
return;
}

$groups = $this->definitions->for_post_type( $post_type );
$post_id = is_object( $post ) && isset( $post->ID ) ? (int) $post->ID : 0;
$groups = $this->definitions->for_object( $post_type, $post_id );
if ( empty( $groups ) ) {
return;
}
Expand Down Expand Up @@ -164,7 +166,7 @@ public function save_post( int $post_id, $post ): void {
? wp_unslash( $_POST['wem_cf_values'] )
: [];

foreach ( $this->definitions->for_post_type( $post_type ) as $group ) {
foreach ( $this->definitions->for_object( $post_type, $post_id ) as $group ) {
$nonce_name = 'wem_cf_nonce_' . $group['key'];
$nonce = isset( $_POST[ $nonce_name ] ) ? sanitize_text_field( wp_unslash( $_POST[ $nonce_name ] ) ) : '';
if ( ! wp_verify_nonce( $nonce, 'wem_cf_save_values_' . $group['key'] ) ) {
Expand Down
19 changes: 19 additions & 0 deletions admin/views/field-group-edit.php
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,25 @@
</div>
</section>

<section class="wem-cf-panel">
<h2>字段组显示范围</h2>
<p class="description">先选择适用的内容类型,再决定字段组出现在哪些编辑页面。此设置只影响后台编辑界面,不改变前台布局,也不会删除已有字段数据。</p>
<?php $location = isset( $config['location'] ) && is_array( $config['location'] ) ? $config['location'] : [ 'mode' => 'all', 'page_ids' => [] ]; ?>
<fieldset class="wem-cf-location-options">
<label><input type="radio" name="group[location][mode]" value="all" <?php checked( ( $location['mode'] ?? 'all' ), 'all' ); ?>> 所选内容类型的全部内容</label>
<label><input type="radio" name="group[location][mode]" value="specific_pages" <?php checked( ( $location['mode'] ?? 'all' ), 'specific_pages' ); ?>> 仅在指定页面显示</label>
</fieldset>
<div class="wem-cf-location-pages">
<label for="wem-cf-location-page-ids"><strong>选择页面</strong></label>
<select id="wem-cf-location-page-ids" name="group[location][page_ids][]" multiple size="6" class="large-text">
<?php foreach ( $available_pages as $page ) : ?>
<option value="<?php echo esc_attr( (string) $page->ID ); ?>" <?php selected( in_array( (int) $page->ID, array_map( 'absint', (array) ( $location['page_ids'] ?? [] ) ), true ) ); ?>><?php echo esc_html( $page->post_title ); ?> (ID: <?php echo esc_html( (string) $page->ID ); ?>)</option>
<?php endforeach; ?>
</select>
<p class="description">可以选择一个或多个页面。此模式仅适用于 Pages;选择此模式后,系统会自动只保留 Pages。</p>
</div>
</section>

<section class="wem-cf-panel">
<div class="wem-cf-panel-head">
<div>
Expand Down
47 changes: 47 additions & 0 deletions assets/css/admin.css
Original file line number Diff line number Diff line change
Expand Up @@ -445,3 +445,50 @@
grid-template-columns: repeat(2, minmax(0, 1fr));
}
}


/* Field group location rules */
.wem-cf-location-options {
display: flex;
flex-wrap: wrap;
gap: 16px;
margin: 12px 0 16px;
}
.wem-cf-location-options label {
display: inline-flex;
align-items: center;
gap: 6px;
}
.wem-cf-location-pages {
max-width: 640px;
}
.wem-cf-location-pages select {
width: 100%;
min-height: 140px;
}
.wem-cf-location-pages[hidden] {
display: none !important;
}


/* Clear visual feedback for page-only location mode */
.wem-cf-post-types label.is-disabled {
opacity: .55;
cursor: not-allowed;
}
.wem-cf-post-types label.is-disabled input {
cursor: not-allowed;
}
.wem-cf-location-options {
gap: 12px 22px;
}
.wem-cf-location-options label {
padding: 2px 0;
}
.wem-cf-location-pages {
margin-top: 14px;
padding: 14px;
background: #f6f7f7;
border: 1px solid #dcdcde;
border-radius: 4px;
}
56 changes: 56 additions & 0 deletions assets/js/admin.js
Original file line number Diff line number Diff line change
Expand Up @@ -273,9 +273,65 @@
});
}

var locationRadios = qsa('input[name="group[location][mode]"]');
var locationPages = qs('.wem-cf-location-pages');
var postTypeInputs = qsa('input[name="group[post_types][]"]');
var previousPostTypeState = null;

function syncLocation() {
var selected = qs('input[name="group[location][mode]"]:checked');
var restricted = selected && selected.value === 'specific_pages';

if (locationPages) {
locationPages.hidden = !restricted;
var pageSelect = qs('#wem-cf-location-page-ids', locationPages);
if (pageSelect) { pageSelect.required = !!restricted; }
}

if (restricted) {
if (previousPostTypeState === null) {
previousPostTypeState = postTypeInputs.map(function (input) {
return { input: input, checked: input.checked };
});
}
postTypeInputs.forEach(function (input) {
var isPage = input.value === 'page';
input.checked = isPage;
input.disabled = !isPage;
var label = input.closest('label');
if (label) { label.classList.toggle('is-disabled', !isPage); }
});
} else {
postTypeInputs.forEach(function (input) {
input.disabled = false;
var label = input.closest('label');
if (label) { label.classList.remove('is-disabled'); }
});
if (previousPostTypeState !== null) {
previousPostTypeState.forEach(function (item) {
item.input.checked = item.checked;
});
previousPostTypeState = null;
}
}
}

locationRadios.forEach(function (radio) {
radio.addEventListener('change', syncLocation);
});
syncLocation();

var groupForm = qs('#wem-cf-group-form');
if (groupForm && fields) {
groupForm.addEventListener('submit', function (event) {
var selectedLocation = qs('input[name="group[location][mode]"]:checked');
var pageSelect = qs('#wem-cf-location-page-ids');
if (selectedLocation && selectedLocation.value === 'specific_pages' && pageSelect && !Array.from(pageSelect.selectedOptions).length) {
event.preventDefault();
window.alert('请选择至少一个指定页面。');
pageSelect.focus();
return;
}
if (!qs('.wem-cf-field-card', fields)) {
event.preventDefault();
window.alert('请至少添加一个字段后再保存字段组。');
Expand Down
Loading
Loading