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-custom.php

class-convertkit-block-form-builder-field-custom.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-custom.php

218 lines 4.9 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 Text Field Block class.
4 *
5 * @package ConvertKit
6 * @author ConvertKit
7 */
8
9 /**
10 * Kit Form Builder Text Field Block for Gutenberg.
11 *
12 * @package ConvertKit
13 * @author ConvertKit
14 */
15 class ConvertKit_Block_Form_Builder_Field_Custom extends ConvertKit_Block_Form_Builder_Field {
16
17 /**
18 * Returns this block's programmatic name, excluding the convertkit- prefix.
19 *
20 * @since 3.0.0
21 *
22 * @return string
23 */
24 public function get_name() {
25
26 /**
27 * This will register as:
28 * - a Gutenberg block, with the name convertkit/form-builder-field-custom.
29 */
30 return 'form-builder-field-custom';
31
32 }
33
34 /**
35 * Returns this block's title.
36 *
37 * @since 3.1.1
38 */
39 public function get_title() {
40
41 return __( 'Kit Form Builder: Custom Field', 'convertkit' );
42
43 }
44
45 /**
46 * Returns this block's icon.
47 *
48 * @since 3.1.1
49 */
50 public function get_icon() {
51
52 return 'resources/backend/images/block-icon-form-builder-field-custom.svg';
53
54 }
55
56 /**
57 * Returns this block's Title, Icon, Categories, Keywords and properties.
58 *
59 * @since 3.0.0
60 *
61 * @return array
62 */
63 public function get_overview() {
64
65 return array(
66 'title' => $this->get_title(),
67 'description' => __( 'Adds a text field to the Kit Form Builder, whose value is stored in a Kit custom field.', 'convertkit' ),
68 'icon' => $this->get_icon(),
69 'category' => 'convertkit',
70 'keywords' => array(
71 __( 'ConvertKit', 'convertkit' ),
72 __( 'Kit', 'convertkit' ),
73 __( 'Custom', 'convertkit' ),
74 __( 'Field', 'convertkit' ),
75 ),
76
77 // Function to call when rendering.
78 'render_callback' => array( $this, 'render' ),
79
80 // Gutenberg: Block Icon in Editor.
81 'gutenberg_icon' => convertkit_get_file_contents( CONVERTKIT_PLUGIN_PATH . '/resources/backend/images/block-icon-form-builder-field-custom.svg' ),
82
83 // Gutenberg: Example image showing how this block looks when choosing it in Gutenberg.
84 'gutenberg_example_image' => CONVERTKIT_PLUGIN_URL . 'resources/backend/images/block-example-form-builder-field-custom.png',
85
86 'has_access_token' => true,
87 'has_resources' => true,
88 );
89
90 }
91
92 /**
93 * Returns this block's Attributes
94 *
95 * @since 3.0.0
96 *
97 * @return array
98 */
99 public function get_attributes() {
100
101 return array_merge(
102 parent::get_attributes(),
103 array(
104 'type' => array(
105 'type' => 'string',
106 'default' => 'text',
107 ),
108 'custom_field' => array(
109 'type' => 'string',
110 'default' => $this->get_default_value( 'custom_field' ),
111 ),
112 )
113 );
114
115 }
116
117 /**
118 * Returns this block's Fields
119 *
120 * @since 3.0.0
121 *
122 * @return bool|array
123 */
124 public function get_fields() {
125
126 // Get Kit Custom Fields.
127 $custom_fields = new ConvertKit_Resource_Custom_Fields( 'block_form_builder' );
128 $values = array();
129 if ( $custom_fields->exist() ) {
130 foreach ( $custom_fields->get() as $custom_field ) {
131 $values[ $custom_field['key'] ] = sanitize_text_field( $custom_field['label'] );
132 }
133 }
134
135 // Get fields from parent class.
136 return array_merge(
137 parent::get_fields(),
138 array(
139 'type' => array(
140 'label' => __( 'Type', 'convertkit' ),
141 'type' => 'select',
142 'description' => __( 'The type of field to display.', 'convertkit' ),
143 'values' => array(
144 'text' => __( 'Text', 'convertkit' ),
145 'textarea' => __( 'Textarea', 'convertkit' ),
146 'number' => __( 'Number', 'convertkit' ),
147 'url' => __( 'URL', 'convertkit' ),
148 ),
149 ),
150 'custom_field' => array(
151 'label' => __( 'Custom Field', 'convertkit' ),
152 'type' => 'select',
153 'description' => __( 'The Kit custom field to store this field\'s entered value.', 'convertkit' ),
154 'values' => $values,
155 ),
156 )
157 );
158
159 }
160
161 /**
162 * Returns this block's UI panels / sections.
163 *
164 * @since 3.0.0
165 *
166 * @return bool|array
167 */
168 public function get_panels() {
169
170 // Get Panels from parent class.
171 $panels = parent::get_panels();
172
173 // Add attributes to the panel.
174 $panels['general']['fields'][] = 'type';
175 $panels['general']['fields'][] = 'custom_field';
176
177 return $panels;
178
179 }
180
181 /**
182 * Returns this block's Default Values
183 *
184 * @since 3.0.0
185 *
186 * @return array
187 */
188 public function get_default_values() {
189
190 return array_merge(
191 array(
192 'type' => 'text',
193 'custom_field' => '',
194 ),
195 parent::get_default_values()
196 );
197
198 }
199
200 /**
201 * Returns the block's output, based on the supplied configuration attributes.
202 *
203 * @since 3.0.0
204 *
205 * @param array $atts Block Attributes.
206 * @return string Output
207 */
208 public function render( $atts ) {
209
210 $this->field_name = 'custom_fields][' . $atts['custom_field'];
211 $this->field_id = 'custom_fields_' . $atts['custom_field'];
212 $this->field_type = $atts['type'];
213 return parent::render( $atts );
214
215 }
216
217 }
218