iniznet/acf-builder-callback

使用ACF Builder构建字段并使用sanitize-escape回调

1.1.2 2022-10-14 10:48 UTC

This package is auto-updated.

Last update: 2024-09-19 13:15:31 UTC


README

ACF Builder扩展创建的包,用于在构建器中快速创建具有回调的ACF配置。

简单示例

$banner = new StoutLogic\AcfBuilder\FieldsBuilder('banner');
$banner
    ->addText('title', [
        'label' => 'Title',
        'instructions' => 'Enter the title of the banner.',
        'required' => true,
        'maxlength' => 100,
        'placeholder' => 'Enter title',
        'sanitization_cb' => 'sanitize_greater_than_30',
        'escape_cb' => 'escape_greater_than_30',
    ])
    ->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());
});

/**
 * Handle the sanitization of the title field.
 * Ensures that the title is greater than 30 characters or nothing.
 * 
 * @param mixed         $value      The value of the title field.
 * @param int|string    $post_id    The post ID.
 * @param array         $field      The field settings.
 * 
 * @return mixed                    The sanitized value.
 */
function sanitize_greater_than_30($value, $post_id, $field) {
    if (strlen($value) > 30) {
        return $value;
    }
    return '';
}

/**
* Handle the escaping of the title field.
* Ensures that the title is greater than 30 characters or nothing.
* 
* @param mixed         $value      The value of the title field.
* @param int|string    $post_id    The post ID.
* @param array         $field      The field settings.
* 
* @return mixed                    The escaped value.
*/
function escape_greater_than_30($value, $post_id, $field) {
    if (strlen($value) > 30) {
        return esc_html($value);
    }
    return '';
}

// Call below somewhere within your application especially during initialization.
iniznet\AcfBuilderCallback\FieldCallback::run();

如果您正在使用ACF Composer

<?php

namespace App\Fields;

use Log1x\AcfComposer\Field;
use StoutLogic\AcfBuilder\FieldsBuilder;

class Example extends Field
{
    /**
     * The field group.
     *
     * @return array
     */
    public function fields()
    {
        $example = new FieldsBuilder('example');

        $example
            ->setLocation('post_type', '==', 'post');

        $example
            ->addRepeater('items')
                ->addText('item', [
                    'label' => 'Item',
                    'instructions' => 'Enter the item.',
                    'required' => true,
                    'maxlength' => 100,
                    'placeholder' => 'Enter item',
                    'sanitization_cb' => function ($value) {
                        return strlen($value) > 30 ? $value : '';
                    },
                    'escape_cb' => function ($value) {
                        return strlen($value) > 30 ? esc_html($value) : '';
                    },
                ])
            ->endRepeater();

        return $example->build();
    }
}

// Call below somewhere within your application especially during initialization.
iniznet\AcfBuilderCallback\FieldCallback::run();

待办事项

  • 字段 sanitization_cb 回调
  • 字段 escape_cb 回调
  • 字段 choices_cb 回调,期望返回一个选择数组
  • 字段 default_value_cb 回调
  • 重构包为独立版本,不再作为ACF Builder的子类扩展
  • 再次重构包,但使用最佳实践而不是当前实践

要求

PHP 7.4至8.0已测试。

安装

使用composer安装

composer require iniznet/acf-builder-callback

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

测试

此包尚未进行测试。

错误报告

如果您在ACF Builder Callback中发现了错误,请打开问题

贡献

鼓励并感谢通过PR、报告问题或提出建议进行贡献。

许可证

ACF Builder Callback在MIT许可证下提供。