Giter Site home page Giter Site logo

iniznet / acf-builder-callback Goto Github PK

View Code? Open in Web Editor NEW
5.0 1.0 1.0 35 KB

Enhance your ACF Builder with an extension that provides a sanitization-escape callback and enables dynamic population of choice fields.

License: MIT License

PHP 100.00%
acf acf-builder acf-field advanced-custom-fields sage sage-theme sage10 wordpress

acf-builder-callback's Introduction

ACF Builder Callback

A package made for ACF Builder extension to quickly create ACF configuration with callback within builder.

Simple Example

$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();

If you're using the 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();

TODO

  • Field sanitization_cb callback
  • Field escape_cb callback
  • Field choices_cb callback, expect an array of choices as return value
  • Field default_value_cb callback
  • Refactor package to standalone & doesn't extending ACF Builder as child class
  • Refactor package again but with best practices instead of the current one

Requirements

PHP 7.4 through 8.0 Tested.

Install

Use composer to install:

composer require iniznet/acf-builder-callback

If your project isn't using composer, you can require the autoload.php file.

Tests

There are no tests for this package yet.

Bug Reports

If you discover a bug in ACF Builder Callback, please open an issue.

Contributing

Contributing whether it be through PRs, reporting an issue, or suggesting an idea is encouraged and appreciated.

License

ACF Builder Callback is provided under the MIT License.

acf-builder-callback's People

Contributors

iniznet avatar renovate[bot] avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

Forkers

reswordpress

acf-builder-callback's Issues

Dependency Dashboard

This issue lists Renovate updates and detected dependencies. Read the Dependency Dashboard docs to learn more.

Open

These updates have all been created already. Click a checkbox below to force a retry/rebase of any.

Detected dependencies

composer
composer.json
  • php ^7.4|^8.0
  • stoutlogic/acf-builder 1.12.0
  • phpunit/phpunit 9.5.25

  • Check this box to trigger a request for Renovate to run again on this repository

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.