PluginProbe
Kit (formerly ConvertKit) – Email Newsletter, Email Marketing, Membership, Subscribers and Landing Pages / 3.4.3
Kit (formerly ConvertKit) – Email Newsletter, Email Marketing, Membership, Subscribers and Landing Pages v3.4.3
3.4.3 3.4.2 3.4.1 3.4.0 3.3.9 3.3.8 3.3.7 3.3.6 3.3.5 3.3.4 3.3.3 3.3.2 3.3.1 2.2.0 2.2.1 2.2.2 2.2.3 2.2.4 2.2.5 2.2.6 2.2.7 2.2.8 2.2.9 2.3.0 2.3.1 All 196 releases
convertkit / includes / blocks / class-convertkit-block-form-builder-field.php

class-convertkit-block-form-builder-field.php in Kit (formerly ConvertKit) – Email Newsletter, Email Marketing, Membership, Subscribers and Landing Pages 3.4.3, at includes/blocks/class-convertkit-block-form-builder-field.php

344 lines 7.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Kit Form Builder Field Block class.
4 *
5 * @package ConvertKit
6 * @author ConvertKit
7 */
8
9 /**
10 * Kit Form Builder Field Block for Gutenberg.
11 *
12 * This isn't a block itself, but is used as a base class for other field blocks, such as
13 * ConvertKit_Block_Form_Builder_Field_Email and ConvertKit_Block_Form_Builder_Field_Text.
14 *
15 * @package ConvertKit
16 * @author ConvertKit
17 */
18 class ConvertKit_Block_Form_Builder_Field extends ConvertKit_Block {
19
20 /**
21 * The field name.
22 *
23 * @since 3.0.0
24 *
25 * @var string
26 */
27 public $field_name;
28
29 /**
30 * The field ID.
31 *
32 * @since 3.0.0
33 *
34 * @var string
35 */
36 public $field_id;
37
38 /**
39 * The type of field to render.
40 *
41 * @since 3.0.0
42 *
43 * @var string
44 */
45 public $field_type = 'text';
46
47 /**
48 * Whether the field is required.
49 *
50 * @since 3.0.0
51 *
52 * @var bool
53 */
54 private $field_required = false;
55
56 /**
57 * Constructor
58 *
59 * @since 3.0.0
60 */
61 public function __construct() {
62
63 // Register this as a Gutenberg block in the Kit Plugin.
64 add_filter( 'convertkit_blocks', array( $this, 'register' ) );
65
66 // Enqueue styles for this Gutenberg Block in the editor view.
67 add_action( 'convertkit_gutenberg_enqueue_styles', array( $this, 'enqueue_styles_editor' ) );
68
69 // Enqueue scripts and styles for this Gutenberg Block in the editor and frontend views.
70 add_action( 'convertkit_gutenberg_enqueue_styles_editor_and_frontend', array( $this, 'enqueue_styles' ) );
71
72 }
73
74 /**
75 * Enqueues styles for this Gutenberg Block in the editor view.
76 *
77 * @since 3.0.0
78 */
79 public function enqueue_styles_editor() {
80
81 wp_enqueue_style( 'convertkit-gutenberg', CONVERTKIT_PLUGIN_URL . 'resources/backend/css/gutenberg.css', array( 'wp-edit-blocks' ), CONVERTKIT_PLUGIN_VERSION );
82
83 }
84
85 /**
86 * Enqueues styles for this Gutenberg Block in the editor and frontend views.
87 *
88 * @since 2.3.3
89 */
90 public function enqueue_styles() {
91
92 convertkit_enqueue_frontend_css();
93
94 }
95
96 /**
97 * Returns this block's programmatic name, excluding the convertkit- prefix.
98 *
99 * @since 3.0.0
100 *
101 * @return string
102 */
103 public function get_name() {
104
105 /**
106 * This will register as:
107 * - a Gutenberg block, with the name convertkit/form-builder-field.
108 */
109 return 'form-builder-field';
110
111 }
112
113 /**
114 * Returns this block's title.
115 *
116 * @since 3.1.1
117 */
118 public function get_title() {
119
120 return __( 'Kit Form Builder: Field', 'convertkit' );
121
122 }
123
124 /**
125 * Returns this block's icon.
126 *
127 * @since 3.1.1
128 */
129 public function get_icon() {
130
131 return 'resources/backend/images/block-icon-form-builder-field.svg';
132
133 }
134
135 /**
136 * Returns this block's Attributes
137 *
138 * @since 3.0.0
139 *
140 * @return array
141 */
142 public function get_attributes() {
143
144 return array(
145 // Block attributes.
146 'label' => array(
147 'type' => 'string',
148 'default' => $this->get_default_value( 'label' ),
149 ),
150 'required' => array(
151 'type' => 'boolean',
152 'default' => $this->get_default_value( 'required' ),
153 ),
154
155 // get_supports() style, color and typography attributes.
156 'align' => array(
157 'type' => 'string',
158 ),
159 'style' => array(
160 'type' => 'object',
161 ),
162 'backgroundColor' => array(
163 'type' => 'string',
164 ),
165 'textColor' => array(
166 'type' => 'string',
167 ),
168 'fontSize' => array(
169 'type' => 'string',
170 ),
171
172 // Always required for Gutenberg.
173 'is_gutenberg_example' => array(
174 'type' => 'boolean',
175 'default' => false,
176 ),
177 );
178
179 }
180
181 /**
182 * Returns this block's supported built-in Attributes.
183 *
184 * @since 3.0.0
185 *
186 * @return array Supports
187 */
188 public function get_supports() {
189
190 return array(
191 'align' => true,
192 'className' => true,
193 'color' => array(
194 'link' => true,
195 'background' => true,
196 'text' => true,
197 ),
198 'typography' => array(
199 'fontSize' => true,
200 'lineHeight' => true,
201 ),
202 'spacing' => array(
203 'margin' => true,
204 'padding' => true,
205 ),
206 );
207
208 }
209
210 /**
211 * Returns this block's Fields
212 *
213 * @since 3.0.0
214 *
215 * @return bool|array
216 */
217 public function get_fields() {
218
219 return array(
220 'label' => array(
221 'label' => __( 'Label', 'convertkit' ),
222 'type' => 'text',
223 'description' => __( 'The field label.', 'convertkit' ),
224 ),
225 'required' => array(
226 'label' => __( 'Required', 'convertkit' ),
227 'type' => 'toggle',
228 'description' => __( 'Whether the field is required.', 'convertkit' ),
229 ),
230 );
231
232 }
233
234 /**
235 * Returns this block's UI panels / sections.
236 *
237 * @since 3.0.0
238 *
239 * @return bool|array
240 */
241 public function get_panels() {
242
243 return array(
244 'general' => array(
245 'label' => __( 'General', 'convertkit' ),
246 'fields' => array(
247 'label',
248 'required',
249 ),
250 ),
251 );
252
253 }
254
255 /**
256 * Returns this block's Default Values
257 *
258 * @since 3.0.0
259 *
260 * @return array
261 */
262 public function get_default_values() {
263
264 return array(
265 'label' => '',
266 'required' => true,
267
268 // Built-in Gutenberg block attributes.
269 'style' => '',
270 'backgroundColor' => '',
271 'textColor' => '',
272 );
273
274 }
275
276 /**
277 * Returns the block's output, based on the supplied configuration attributes.
278 *
279 * @since 3.0.0
280 *
281 * @param array $atts Block Attributes.
282 * @return string Output
283 */
284 public function render( $atts ) {
285
286 // Parse attributes, defining fallback defaults if required
287 // and moving some attributes (such as Gutenberg's styles), if defined.
288 $atts = $this->sanitize_and_declare_atts( $atts );
289
290 // Get CSS classes and styles.
291 $css_classes = $this->get_css_classes( array( 'wp-block-convertkit-form-builder-field', 'convertkit-form-builder-field' ) );
292 $css_styles = $this->get_css_styles( $atts );
293
294 // Determine if the field is required.
295 $field_required = $this->field_required ? true : ( $atts['required'] ? true : false );
296
297 // Build input / textarea.
298 switch ( $this->field_type ) {
299 case 'textarea':
300 $field = sprintf(
301 '<textarea id="%s" name="convertkit[%s]" %s></textarea>',
302 esc_attr( sanitize_title( $this->field_id ) ),
303 esc_attr( $this->field_name ),
304 $field_required ? ' required' : ''
305 );
306 break;
307 default:
308 $field = sprintf(
309 '<input type="%s" id="%s" name="convertkit[%s]" %s />',
310 esc_attr( $this->field_type ),
311 esc_attr( sanitize_title( $this->field_id ) ),
312 esc_attr( $this->field_name ),
313 $field_required ? ' required' : ''
314 );
315 break;
316 }
317
318 // Build field HTML.
319 $html = sprintf(
320 '<div class="%s" style="%s"><label for="%s">%s%s</label>%s</div>',
321 implode( ' ', map_deep( $css_classes, 'sanitize_html_class' ) ),
322 implode( ';', map_deep( $css_styles, 'esc_attr' ) ),
323 esc_attr( sanitize_title( $this->field_id ) ),
324 esc_html( $atts['label'] ),
325 ( $field_required ? ' <span class="convertkit-form-builder-field-required">*</span>' : '' ),
326 $field
327 );
328
329 /**
330 * Filter the block's content immediately before it is output.
331 *
332 * @since 3.0.0
333 *
334 * @param string $html Field HTML.
335 * @param array $atts Block Attributes.
336 */
337 $html = apply_filters( 'convertkit_block_form_builder_field_render', $html, $atts );
338
339 return $html;
340
341 }
342
343 }
344