stoutlogic/acf-builder

高级自定义字段配置构建器

v1.12.0 2021-09-17 17:32 UTC

README

使用构建模式和流畅的API为Advanced Custom Fields Pro创建配置数组。

快速创建、注册和重用ACF配置,并将它们保存在您的源代码仓库中。有关通过PHP注册ACF字段的更多信息,请参阅https://www.advancedcustomfields.com/resources/register-fields-via-php/

Latest Stable Version Build Status Scrutinizer Code Quality Join the chat at https://gitter.im/StoutLogic/acf-builder

简单示例

$banner = new StoutLogic\AcfBuilder\FieldsBuilder('banner');
$banner
    ->addText('title')
    ->addWysiwyg('content')
    ->addImage('background_image')
    ->setLocation('post_type', '==', 'page')
        ->or('post_type', '==', 'post');

add_action('acf/init', function() use ($banner) {
   acf_add_local_field_group($banner->build());
});

$banner->build(); 将返回

[
    'key' => 'group_banner',
    'title' => 'Banner',
    'fields' => [
        [
            'key' => 'field_title',
            'name' => 'title',
            'label' => 'Title',
            'type' => 'text'
        ],
        [
            'key' => 'field_content',
            'name' => 'content',
            'label' => 'Content',
            'type' => 'wysiwyg'
        ],
        [
            'key' => 'field_background_image',
            'name' => 'background_image',
            'label' => 'Background Image',
            'type' => 'image'
        ],
    ],
    'location' => [
        [
            [
                'param' => 'post_type',
                'operator' => '==',
                'value' => 'page'
            ]
        ],
        [
            [
                'param' => 'post_type',
                'operator' => '==',
                'value' => 'post'
            ]
        ]
    ]
]

如您所见,这可以为您节省大量时间并降低出错率。但简洁和正确性并非唯一的好处,您可以在多个地方重用字段配置。例如,一组用于背景的字段

重用示例

use StoutLogic\AcfBuilder\FieldsBuilder;

$background = new FieldsBuilder('background');
$background
    ->addTab('Background')
    ->addImage('background_image')
    ->addTrueFalse('fixed')
        ->instructions("Check to add a parallax effect where the background image doesn't move when scrolling")
    ->addColorPicker('background_color');

$banner = new FieldsBuilder('banner');
$banner
    ->addTab('Content')
    ->addText('title')
    ->addWysiwyg('content')
    ->addFields($background)
    ->setLocation('post_type', '==', 'page');

$section = new FieldsBuilder('section');
$section
    ->addTab('Content')
    ->addText('section_title')
    ->addRepeater('columns', ['min' => 1, 'layout' => 'block'])
        ->addTab('Content')
        ->addText('title')
        ->addWysiwyg('content')
        ->addFields($background)
        ->endRepeater()
    ->addFields($background)
    ->setLocation('post_type', '==', 'page');

在这里创建了一个 background 字段组,并在两个其他字段组中使用它,包括在 section 字段组中两次使用。这真的可以使您的代码更加DRY,并保持您的管理UI一致性。如果您想根据使用的背景添加用于文字颜色的浅色/深色字段,只需在一点添加并到处使用即可。

安装

使用composer进行安装

composer require stoutlogic/acf-builder

如果您的项目未使用composer,您可以要求autoload.php文件。

测试

要运行测试,您可以手动运行

vendor/bin/phpunit

或者您可以使用内置的gulp任务在文件更改时运行它

npm install
gulp

要求

PHP 5.4至7.2受支持,但自动测试无法再运行,因此它可能在某个时候停止工作。

= 7.4, 8 已测试

文档

有关更详细的文档,请参阅wiki。该文档有自己的存储库并接受贡献的拉取请求。对master的任何合并都将同步到这里的wiki中,在主项目下。