PluginProbe
Booking Calendar / 11.0
Booking Calendar v11.0
11.9 11.8.4 11.8.3 11.8.2 11.8.1 11.8 11.7 11.6.1 11.6 11.5 11.4.3 11.4.2 11.4.1 11.4 11.3 11.2.1 11.2 11.1 11.0 10.15.7 10.15.6 10.1.3 10.10 10.10.1 10.10.2 All 205 releases
booking / includes / page-form-builder / field-packs / text-firstname / field-text-firstname-preset.php

field-text-firstname-preset.php in Booking Calendar 11.0, at includes/page-form-builder/field-packs/text-firstname/field-text-firstname-preset.php

164 lines 5.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * WPBC BFB Preset: First Name (prefilled Text field)
4 *
5 * Purpose:
6 * - Adds a ready-to-use “First Name” field to the palette, prefilled with specific properties.
7 * - Reuses the existing Text field pack renderer/templates (type = "text"); no extra JS required.
8 *
9 * How it works:
10 * - The palette <li> carries data-* attributes mirroring field props.
11 * - On drop, the Builder merges these attributes into the new field’s data object.
12 *
13 * The preset’s palette item declares data-type="text".
14 * That means the exact same renderer class is used: WPBC_BFB_Field_Text from your Text pack (_out/field-text-wptpl.js).
15 * It also uses the same wp.template() blocks the Text pack prints:
16 * Preview: tmpl-wpbc-bfb-field-text
17 * Inspector: tmpl-wpbc-bfb-inspector-text
18 * Result: no new JS renderer, no duplicate templates—just reuse.
19 *
20 * On drop, the Builder takes the <li> dataset from the palette (your preset) and merges it into the new field’s data object.
21 * That data object is normalized against the Text pack defaults (get_defaults() in WPBC_BFB_Field_Text) and the Text PHP schema in field-text-wptpl.php.
22 * Your preset simply provides initial values for those same Text props:
23 * label -> “First Name”
24 * name -> firstname
25 * placeholder -> Example: "John"
26 * required -> 1
27 * help -> Enter your first name.
28 * minlength -> 5
29 * maxlength -> 10
30 * pattern -> aaa (passed straight into the input’s pattern attribute)
31 * cssclass -> firstname myname
32 * html_id -> my-name
33 * min_width -> 8em (layout hint stored at wrapper; the Text pack happily ignores it, but the Builder UI uses it)
34 *
35 * File:
36 * - /includes/page-form-builder/field-packs/text-firstname/field-text-firstname-preset.php
37 *
38 * @package Booking Calendar
39 * @author wpdevelop, oplugins
40 * @since 11.0.0
41 * @modified 2025-10-19
42 * @version 1.0.0
43 */
44
45 if ( ! defined( 'ABSPATH' ) ) {
46 exit;
47 }
48
49 /**
50 * Class-like wrapper (snake_case) for the “First Name” prefilled Text preset.
51 *
52 * Notes:
53 * - Uses WordPress coding standards and snake_case for class, methods, functions, variables.
54 * - Prefilled values match the user’s requested structure (where applicable in the Builder).
55 */
56 class wpbc_bfb_preset_text_firstname {
57
58 /**
59 * Bootstrap — attach hooks.
60 *
61 * @return void
62 */
63 public static function init() {
64 add_action( 'wpbc_bfb_palette_register_items', array( __CLASS__, 'palette_register_items' ), 10, 2 );
65 }
66
67 /**
68 * Return the prefilled props for the First Name preset.
69 *
70 * Keys correspond to Text field pack properties (and optional field-level width).
71 * Filterable via 'wpbc_bfb_preset_text_firstname_props'.
72 *
73 * @return array Prefilled properties for the field data object.
74 */
75 public static function get_preset_props() {
76 $props = array(
77 // Renderer/type contract.
78 'type' => 'text',
79
80 // Visual / UX.
81 'min_width' => '8em',
82 'label' => __( 'First Name', 'booking' ),
83 'placeholder'=> 'Example: "John"',
84 'required' => 1,
85 'help' => __( 'Enter your first name.', 'booking' ),
86
87 // Validation
88 // 'minlength' => '',
89 // 'maxlength' => '',
90 // 'pattern' => '', // keep as provided; adjust pattern to your real needs later
91
92 // Naming / CSS
93 'name' => 'firstname',
94 'cssclass' => 'firstname',
95 // 'html_id' => '',
96
97 // Optional legacy/internal hints (not always used by all renderers)
98 // 'default_value' => '',
99 // 'id' => 'input-text', // internal field id is usually auto-managed; included here only if your core honors it
100 );
101
102 /**
103 * Filter: allow customizing preset props at runtime.
104 *
105 * @param array $props
106 */
107 return apply_filters( 'wpbc_bfb_preset_text_firstname_props', $props );
108 }
109
110 /**
111 * Register the prefilled “First Name” item in the Builder palette.
112 *
113 * Reuses the Text field pack by setting data-type="text" and passing all prefilled values
114 * via data-* attributes (merged by the Builder on drop).
115 *
116 * @param string $group Palette group slug (e.g., 'standard').
117 * @param string $position Palette position (e.g., 'top' or 'bottom').
118 *
119 * @return void
120 */
121 public static function palette_register_items( $group, $position ) {
122 // Adjust target zone as needed.
123 if ( 'essentials' !== $group || 'top' !== $position ) {
124 return;
125 }
126
127 $props = self::get_preset_props();
128
129 // Safety esc for attributes.
130 $esc = function ( $v ) {
131 return is_scalar( $v ) ? $v : wp_json_encode( $v );
132 };
133 ?>
134 <li class="wpbc_bfb__field"
135 data-id="text-firstname"
136 data-type="text"
137 data-usage_key="text"
138 data-label="<?php echo esc_attr( $esc( $props['label'] ) ); ?>"
139
140 <?php // Prefilled props (merged into the new field’s data on drop). ?>
141 data-name="<?php echo esc_attr( $esc( $props['name'] ) ); ?>"
142 data-placeholder="<?php echo esc_attr( $esc( $props['placeholder'] ) ); ?>"
143 data-required="<?php echo esc_attr( $esc( $props['required'] ) ); ?>"
144 data-help="<?php echo esc_attr( $esc( $props['help'] ) ); ?>"
145 <?php /*
146 data-minlength="<?php echo esc_attr( $esc( $props['minlength'] ) ); ?>"
147 data-maxlength="<?php echo esc_attr( $esc( $props['maxlength'] ) ); ?>"
148 data-pattern="<?php echo esc_attr( $esc( $props['pattern'] ) ); ?>"
149 data-html_id="<?php echo esc_attr( $esc( $props['html_id'] ) ); ?>"
150 */ ?>
151 data-cssclass="<?php echo esc_attr( $esc( $props['cssclass'] ) ); ?>"
152 data-min_width="<?php echo esc_attr( $esc( $props['min_width'] ) ); ?>"
153 >
154 <i class="menu_icon icon-1x wpbc-bi-person"></i>
155 <span class="wpbc_bfb__field-label"><?php echo esc_html( $props['label'] ); ?></span>
156 <span class="wpbc_bfb__field-type">firstname | text</span>
157 </li>
158 <?php
159 }
160 }
161
162 // Initialize preset.
163 wpbc_bfb_preset_text_firstname::init();
164