| 1 |
<?php |
| 2 |
/** |
| 3 |
* WPBC BFB Pack: Text Field (Schema-driven Reference) |
| 4 |
* |
| 5 |
* Purpose: |
| 6 |
* - Minimal, crystal-clear example of a field pack using Schema/Factory for the Inspector. |
| 7 |
* - No WP templates; preview is rendered via pure JS renderer class. |
| 8 |
* - Reference implementation to clone for other simple fields. |
| 9 |
* |
| 10 |
* Contracts Used: |
| 11 |
* - Filter: wpbc_bfb_register_field_packs |
| 12 |
* - Action: wpbc_enqueue_js_field_pack |
| 13 |
* - Action: wpbc_bfb_palette_register_items (optional; used here to list Text in the palette) |
| 14 |
* |
| 15 |
* Notes: |
| 16 |
* - Keep schema defaults in sync with JS get_defaults(). |
| 17 |
* |
| 18 |
* File: ../includes/page-form-builder/field-packs/text/field-text.php |
| 19 |
* |
| 20 |
* @package Booking Calendar |
| 21 |
* @author wpdevelop, oplugins |
| 22 |
* @web-site https://wpbookingcalendar.com/ |
| 23 |
* @email info@wpbookingcalendar.com |
| 24 |
* |
| 25 |
* @since 11.0.0 |
| 26 |
* @modified 2025-09-02 |
| 27 |
* @version 1.0.1 |
| 28 |
*/ |
| 29 |
|
| 30 |
if ( ! defined( 'ABSPATH' ) ) { |
| 31 |
exit; |
| 32 |
} |
| 33 |
|
| 34 |
/** |
| 35 |
* Register the "text" field pack (Schema-driven inspector). |
| 36 |
* |
| 37 |
* @param array $packs Accumulated packs. |
| 38 |
* |
| 39 |
* @return array Modified packs with "text" field included. |
| 40 |
*/ |
| 41 |
function wpbc_bfb_register_field_packs__field_text_schema( $packs ) { |
| 42 |
|
| 43 |
$packs['text'] = array( |
| 44 |
'kind' => 'field', |
| 45 |
'type' => 'text', |
| 46 |
'label' => __( 'Text', 'booking' ), |
| 47 |
'icon' => 'wpbc_icn_text_fields', |
| 48 |
'usage_key' => 'text', // Optional: used by UsageLimitService if you set limits elsewhere. |
| 49 |
// === Schema: coercion/validation + defaults for Inspector/serialization ===. |
| 50 |
'schema' => array( |
| 51 |
'props' => array( |
| 52 |
'label' => array( 'type' => 'string', 'default' => __( 'Text', 'booking' ) ), |
| 53 |
'name' => array( 'type' => 'string', 'default' => '' ), |
| 54 |
'html_id' => array( 'type' => 'string', 'default' => '' ), |
| 55 |
'placeholder' => array( 'type' => 'string', 'default' => '' ), |
| 56 |
'required' => array( 'type' => 'boolean', 'default' => false ), |
| 57 |
'minlength' => array( 'type' => 'number', 'default' => null, 'min' => 0 ), |
| 58 |
'maxlength' => array( 'type' => 'number', 'default' => null, 'min' => 0 ), |
| 59 |
'pattern' => array( 'type' => 'string', 'default' => '' ), |
| 60 |
'cssclass' => array( 'type' => 'string', 'default' => '' ), |
| 61 |
'help' => array( 'type' => 'string', 'default' => '' ), |
| 62 |
'default_value' => array( 'type' => 'string', 'default' => '' ), |
| 63 |
), |
| 64 |
), |
| 65 |
// === Inspector UI (Factory-driven; no WP templates here) ===. |
| 66 |
'inspector_ui' => array( |
| 67 |
'title' => __( 'Single Line Text', 'booking' ) . '', // TODO remove it. |
| 68 |
'description' => __( 'Configure a single-line text input.', 'booking' ), |
| 69 |
'header_variant' => 'toolbar', |
| 70 |
'header_actions' => array( 'deselect', 'scrollto', 'move-up', 'move-down', 'duplicate', 'delete' ), |
| 71 |
'groups' => array( |
| 72 |
array( |
| 73 |
'key' => 'basic', |
| 74 |
'label' => __( 'Basic', 'booking' ), |
| 75 |
'open' => true, |
| 76 |
'controls' => array( |
| 77 |
array( 'type' => 'text', 'key' => 'label', 'label' => __( 'Label', 'booking' ) ), |
| 78 |
array( 'type' => 'text', 'key' => 'name', 'label' => __( 'Name', 'booking' ) ), |
| 79 |
array( 'type' => 'text', 'key' => 'placeholder', 'label' => __( 'Placeholder', 'booking' ) ), |
| 80 |
array( 'type' => 'checkbox', 'key' => 'required', 'label' => __( 'Required', 'booking' ) ), |
| 81 |
array( 'type' => 'text', 'key' => 'default_value', 'label' => __( 'Default value', 'booking' ) ), |
| 82 |
array( |
| 83 |
'type' => 'textarea', |
| 84 |
'key' => 'help', |
| 85 |
'label' => __( 'Help text', 'booking' ), |
| 86 |
'rows' => 3, |
| 87 |
), |
| 88 |
), |
| 89 |
), |
| 90 |
array( |
| 91 |
'key' => 'validation', |
| 92 |
'label' => __( 'Validation', 'booking' ), |
| 93 |
'controls' => array( |
| 94 |
array( 'type' => 'number', 'key' => 'minlength', 'label' => __( 'Min length', 'booking' ) ), |
| 95 |
array( 'type' => 'number', 'key' => 'maxlength', 'label' => __( 'Max length', 'booking' ) ), |
| 96 |
// array( 'type' => 'text', 'key' => 'pattern', 'label' => __( 'Pattern', 'booking' ) ), // Temporary comment pattern, it can be enabled in future updates. |
| 97 |
), |
| 98 |
), |
| 99 |
array( |
| 100 |
'key' => 'appearance', |
| 101 |
'label' => __( 'Appearance', 'booking' ), |
| 102 |
'controls' => array( |
| 103 |
array( 'type' => 'text', 'key' => 'cssclass', 'label' => __( 'CSS class', 'booking' ) ), |
| 104 |
array( 'type' => 'text', 'key' => 'html_id', 'label' => __( 'HTML ID', 'booking' ) ), |
| 105 |
), |
| 106 |
), |
| 107 |
), |
| 108 |
), |
| 109 |
|
| 110 |
// No templates_printer for Schema-driven variant. |
| 111 |
); |
| 112 |
|
| 113 |
return $packs; |
| 114 |
} |
| 115 |
add_filter( 'wpbc_bfb_register_field_packs', 'wpbc_bfb_register_field_packs__field_text_schema' ); |
| 116 |
|
| 117 |
|
| 118 |
/** |
| 119 |
* Enqueue the Text field pack JS for the Builder page. |
| 120 |
* |
| 121 |
* This hook is fired only on the Builder page (gated by is_builder_by_request()), |
| 122 |
* so we do not need an extra $page check here. |
| 123 |
* |
| 124 |
* @param string $page Current admin page slug. |
| 125 |
* |
| 126 |
* @return void |
| 127 |
*/ |
| 128 |
function wpbc_bfb_enqueue__field_text_schema_js( $page ) { |
| 129 |
|
| 130 |
wp_enqueue_script( 'wpbc-bfb_field_text_schema', wpbc_plugin_url( '/includes/page-form-builder/field-packs/text/_out/field-text.js' ), array( 'wpbc-bfb' ), WP_BK_VERSION_NUM, true ); |
| 131 |
} |
| 132 |
add_action( 'wpbc_enqueue_js_field_pack', 'wpbc_bfb_enqueue__field_text_schema_js', 10, 1 ); |
| 133 |
|
| 134 |
|
| 135 |
/** |
| 136 |
* (Optional) Register the "Text" palette item in "general/bottom". |
| 137 |
* |
| 138 |
* If your palette auto-builds from packs, you can remove this. |
| 139 |
* Keeping it here makes the reference self-contained and explicit. |
| 140 |
* |
| 141 |
* @param string $group Palette group: 'general' | 'advanced' | ... |
| 142 |
* @param string $position Position: 'top' | 'bottom' |
| 143 |
* |
| 144 |
* @return void |
| 145 |
*/ |
| 146 |
function wpbc_bfb_palette_register_items__text_schema( $group, $position ) { |
| 147 |
|
| 148 |
if ( 'standard' !== $group || 'bottom' !== $position ) { |
| 149 |
return; |
| 150 |
} |
| 151 |
?> |
| 152 |
<li class="wpbc_bfb__field" data-id="text" data-type="text" data-usage_key="text" data-label="<?php echo esc_attr( __( 'Text', 'booking' ) ); ?>" > |
| 153 |
<i class="menu_icon icon-1x wpbc-bi-input-cursor"></i> |
| 154 |
<span class="wpbc_bfb__field-label"><?php echo esc_html( __( 'Text', 'booking' ) ); ?></span> |
| 155 |
<span class="wpbc_bfb__field-type">text</span> |
| 156 |
</li> |
| 157 |
<?php |
| 158 |
} |
| 159 |
add_action( 'wpbc_bfb_palette_register_items', 'wpbc_bfb_palette_register_items__text_schema', 10, 2 ); |
| 160 |
|