add( new My_Field() ); // extends \FWREN\Field_Block * } ); * * @package FWREN */ namespace FWREN; defined( 'ABSPATH' ) || exit; class Fields { /** @var array By field type. */ private static $types = array(); /** @var array By block name. */ private static $blocks = array(); /** @var bool */ private static $loaded = false; /** Hooks. */ public static function init() { add_action( 'fwren_bc/register_blocks', array( __CLASS__, 'register' ) ); add_filter( 'block_categories_all', array( __CLASS__, 'categories' ), 5, 2 ); add_filter( 'fwren_bc/attribute_type', array( __CLASS__, 'attribute_type' ), 10, 2 ); add_filter( 'fwren_bc/block_type_args', array( __CLASS__, 'block_args' ), 10, 2 ); add_action( 'fwren_bc/block/controls_registered', array( __CLASS__, 'container_controls' ) ); add_filter( 'render_block_' . self::CONTAINER, array( __CLASS__, 'container_render' ), 10, 2 ); } /** The bundled BS23 Container (rows and inner containers) used for form layout. */ const CONTAINER = 'fwrenbc/container'; /** * Conditional logic on the Container: same controls as on fields, in its Content tab. * * @param \FWREN_BC\Block_Base $block Block whose controls were just registered. */ public static function container_controls( $block ) { if ( self::CONTAINER !== $block->get_block_name() ) { return; } $block->start_controls_section( 'section_fwren_conditions', array( 'label' => __( 'Conditional logic (forms)', 'fieldwren' ) ) ); $block->add_control( 'cl_enabled', array( 'label' => __( 'Enable conditional logic', 'fieldwren' ), 'type' => \FWREN_BC\Controls::SWITCHER, 'default' => '' ) ); $block->add_control( 'cl_action', array( 'label' => __( 'Action', 'fieldwren' ), 'type' => \FWREN_BC\Controls::SELECT, 'default' => 'show', 'options' => array( 'show' => __( 'Show this container', 'fieldwren' ), 'hide' => __( 'Hide this container', 'fieldwren' ), ), 'condition' => array( 'cl_enabled' => 'yes' ), ) ); $block->add_control( 'cl_match', array( 'label' => __( 'When', 'fieldwren' ), 'type' => \FWREN_BC\Controls::SELECT, 'default' => 'all', 'options' => array( 'all' => __( 'All rules match', 'fieldwren' ), 'any' => __( 'Any rule matches', 'fieldwren' ), ), 'condition' => array( 'cl_enabled' => 'yes' ), ) ); $block->add_control( 'cl_rules', array( 'label' => __( 'Rules', 'fieldwren' ), 'type' => Field_Block::RULES, 'default' => array(), 'condition' => array( 'cl_enabled' => 'yes' ) ) ); $block->end_controls_section(); } /** Print the Container's condition on its wrapper (frontend; the editor draws it in JS). */ public static function container_render( $html, $parsed ) { $cond = Field_Block::conditions_of( (array) ( $parsed['attrs'] ?? array() ) ); if ( ! $cond || Renderer::is_editor() ) { return $html; } $json = esc_attr( wp_json_encode( $cond ) ); return preg_replace_callback( '/<([a-z]+)([^>]*?)class="([^"]*)"/i', static function ( $m ) use ( $json ) { return '<' . $m[1] . $m[2] . 'class="' . $m[3] . ' fwren-cond" data-fwren-cond="' . $json . '"'; }, $html, 1 ); } /** Parent restriction (Column only inside Columns). */ public static function block_args( $args, $block ) { if ( $block instanceof Field_Block && $block->get_parent() ) { $args['parent'] = $block->get_parent(); } return $args; } /** Load field classes once. */ public static function load() { if ( self::$loaded ) { return; } self::$loaded = true; require_once FWREN_PATH . 'fields/basic.php'; require_once FWREN_PATH . 'fields/choice.php'; require_once FWREN_PATH . 'fields/advanced.php'; require_once FWREN_PATH . 'fields/layout.php'; require_once FWREN_PATH . 'fields/extra.php'; $classes = array( 'Text_Field', 'Email_Field', 'Name_Field', 'Textarea_Field', 'Number_Field', 'Phone_Field', 'Url_Field', 'Hidden_Field', 'Password_Field', 'Select_Field', 'Radio_Field', 'Checkbox_Field', 'Country_Field', 'Rating_Field', 'Nps_Field', 'Range_Field', 'Grid_Field', 'Ranking_Field', 'Color_Field', 'Date_Field', 'File_Field', 'Image_Field', 'Signature_Field', 'Consent_Field', 'Calculation_Field', 'Address_Field', 'Repeater_Field', 'Columns_Block', 'Column_Block', 'Section_Block', 'Html_Block', 'Step_Block', 'Submit_Block', 'Captcha_Block', 'Save_Block', 'Shortcode_Block', 'Hook_Block', 'Switch_Field', 'Toggle_Field', 'Like_Field', 'Richtext_Field', 'Map_Field', 'Simple_Captcha_Field', 'Summary_Block', ); foreach ( $classes as $c ) { $fq = '\\FWREN\\Fields\\' . $c; self::add( new $fq() ); } do_action( 'fwren/register_fields', new class() { /** @param Field_Block $f Field. */ public function add( $f ) { Fields::add( $f ); } } ); } /** Add a field. */ public static function add( Field_Block $f ) { self::$types[ $f->get_field_type() ] = $f; self::$blocks[ $f->get_block_name() ] = $f; } /** Register with the framework (called on init by the framework). */ public static function register( $framework ) { self::load(); foreach ( self::$blocks as $f ) { $framework->register_block( $f ); } } /** @return Field_Block|null By field type. */ public static function get( $type ) { self::load(); return self::$types[ $type ] ?? null; } /** @return Field_Block|null By block name. */ public static function by_block( $name ) { self::load(); return self::$blocks[ $name ] ?? null; } /** @return string[] All form block names. */ public static function block_names() { self::load(); return array_keys( self::$blocks ); } /** @return array */ public static function all() { self::load(); return self::$types; } /** Inserter categories (first in the list on form screens). */ public static function categories( $cats, $context ) { $ours = array( array( 'slug' => 'fwren-fields', 'title' => __( 'Form – General Fields', 'fieldwren' ) ), array( 'slug' => 'fwren-choice', 'title' => __( 'Form – Choice Fields', 'fieldwren' ) ), array( 'slug' => 'fwren-advanced', 'title' => __( 'Form – Advanced Fields', 'fieldwren' ) ), array( 'slug' => 'fwren-payment', 'title' => __( 'Form – Payment Fields', 'fieldwren' ) ), array( 'slug' => 'fwren-layout', 'title' => __( 'Form – Layout', 'fieldwren' ) ), ); $slugs = wp_list_pluck( $cats, 'slug' ); $ours = array_values( array_filter( $ours, static function ( $c ) use ( $slugs ) { return ! in_array( $c['slug'], $slugs, true ); } ) ); $post = $context->post ?? null; return $post && Plugin::POST_TYPE === $post->post_type ? array_merge( $ours, $cats ) : array_merge( $cats, $ours ); } /** Attribute types of the custom controls. */ public static function attribute_type( $type, $control_type ) { if ( in_array( $control_type, array( Field_Block::OPTIONS, Field_Block::RULES ), true ) ) { return 'array'; } return $type; } }