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 / integrations / elementor / class-convertkit-elementor-widget.php

class-convertkit-elementor-widget.php in Kit (formerly ConvertKit) – Email Newsletter, Email Marketing, Membership, Subscribers and Landing Pages 3.4.3, at includes/integrations/elementor/class-convertkit-elementor-widget.php

317 lines 6.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * ConvertKit Elementor Widget class.
4 *
5 * @package ConvertKit
6 * @author ConvertKit
7 */
8
9 /**
10 * Extend this class for a specific block; see class-convertkit-elementor-widget-form.php
11 * for an example.
12 *
13 * @package ConvertKit
14 * @author ConvertKit
15 */
16 class ConvertKit_Elementor_Widget extends Elementor\Widget_Base {
17
18 /**
19 * Holds the block's properties
20 *
21 * @since 1.9.7.2
22 *
23 * @var WP_Error|array
24 */
25 private $block;
26
27 /**
28 * The module's slug. Must be different from the block's name i.e. cannot be convertkit-form.
29 *
30 * @since 1.9.7.2
31 *
32 * @var string
33 */
34 public $slug = '';
35
36 /**
37 * Defines the Widget Name
38 *
39 * @since 1.9.7.2
40 *
41 * @return string
42 */
43 public function get_name() {
44
45 return $this->slug;
46
47 }
48
49 /**
50 * Defines the Block Name, which is the slug excluding the
51 * `convertkit-elementor-` prefix.
52 *
53 * @since 1.9.7.2
54 *
55 * @return string
56 */
57 public function get_block_name() {
58
59 return str_replace( 'convertkit-elementor-', '', $this->slug );
60
61 }
62
63 /**
64 * Defines the Widget Title
65 *
66 * @since 1.9.7.2
67 *
68 * @return string
69 */
70 public function get_title() {
71
72 // Get block.
73 $this->block = $this->get_block();
74
75 // Bail if the block could not be found.
76 if ( is_wp_error( $this->block ) ) {
77 return $this->block->get_error_message();
78 }
79
80 // Return block's title.
81 return $this->block['title'];
82
83 }
84
85 /**
86 * Defines the Widget Icon
87 *
88 * @since 1.9.7.2
89 *
90 * @return string
91 */
92 public function get_icon() {
93
94 return 'eicon-convertkit-' . $this->get_block_name();
95
96 }
97
98 /**
99 * Defines the Widget Categories
100 *
101 * @since 1.9.7.2
102 *
103 * @return array
104 */
105 public function get_categories() {
106
107 return array( 'convertkit' );
108
109 }
110
111 /**
112 * Defines the fields for this Widget
113 *
114 * @since 1.9.7.2
115 */
116 protected function register_controls() {
117
118 // Bail if the request is not for the WordPress Administration or frontend editor.
119 if ( ! WP_ConvertKit()->is_admin_or_frontend_editor() ) {
120 return;
121 }
122
123 // Get block.
124 $this->block = $this->get_block();
125
126 // Bail if the block could not be found.
127 if ( is_wp_error( $this->block ) ) {
128 return;
129 }
130
131 // Iterate through panels, building a section for each.
132 foreach ( $this->block['panels'] as $panel_name => $panel_properties ) {
133 // Start section.
134 $this->start_controls_section(
135 // Deliberately prefix, as if a tab and field have the same name, it won't render.
136 'section_' . $panel_name,
137 array(
138 'label' => $panel_properties['label'],
139 'tab' => Elementor\Controls_Manager::TAB_CONTENT,
140 )
141 );
142
143 // Add controls to this section.
144 foreach ( $panel_properties['fields'] as $field_name ) {
145 // Get field.
146 $field = $this->block['fields'][ $field_name ];
147
148 // Get Elementor Control for this field.
149 $control = $this->get_field_control_args( $field );
150
151 // Finally, register the control for this field.
152 $this->add_control( $field_name, $control );
153 }
154
155 // Close the section.
156 $this->end_controls_section();
157
158 }
159
160 }
161
162 /**
163 * Returns the given field's control arguments, so that the field can be registered
164 * as an Elementor Control.
165 *
166 * @since 1.9.7.2
167 *
168 * @param array $field Block Field.
169 * @return array Elementor Control Arguments, compatible with add_control()
170 */
171 private function get_field_control_args( $field ) {
172
173 // Start building control.
174 $control = array(
175 'default' => ( isset( $field['default_value'] ) ? $field['default_value'] : '' ),
176 'label' => $field['label'],
177 'placeholder' => ( isset( $field['placeholder'] ) ? $field['placeholder'] : '' ),
178 'desc' => ( isset( $field['description'] ) ? $field['description'] : '' ),
179 );
180
181 // Add conditional field, if defined.
182 if ( isset( $field['display_if'] ) ) {
183 $control = array_merge(
184 $control,
185 array(
186 'condition' => array(
187 $field['display_if']['key'] => $field['display_if']['value'] ? 'yes' : 'no',
188 ),
189 )
190 );
191 }
192
193 // Add control depending on the field type.
194 switch ( $field['type'] ) {
195 /**
196 * Select
197 */
198 case 'resource':
199 case 'select':
200 $control = array_merge(
201 $control,
202 array(
203 'type' => Elementor\Controls_Manager::SELECT,
204 'options' => $field['values'],
205 )
206 );
207 break;
208
209 /**
210 * Number
211 */
212 case 'number':
213 $control = array_merge(
214 $control,
215 array(
216 'type' => Elementor\Controls_Manager::NUMBER,
217 'min' => $field['min'],
218 'max' => $field['max'],
219 'step' => $field['step'],
220 )
221 );
222 break;
223
224 /**
225 * Toggle
226 */
227 case 'toggle':
228 $control = array_merge(
229 $control,
230 array(
231 'type' => Elementor\Controls_Manager::SWITCHER,
232 )
233 );
234 break;
235
236 /**
237 * Color Picker
238 */
239 case 'color':
240 $control = array_merge(
241 $control,
242 array(
243 'type' => Elementor\Controls_Manager::COLOR,
244 )
245 );
246 break;
247
248 default:
249 $control = array_merge(
250 $control,
251 array(
252 'type' => Elementor\Controls_Manager::TEXT,
253 )
254 );
255 break;
256
257 }
258
259 return $control;
260
261 }
262
263 /**
264 * Renders the block.
265 *
266 * @since 1.9.7.2
267 */
268 protected function render() {
269
270 // Bail if the block could not be found.
271 if ( is_wp_error( $this->block ) ) {
272 return $this->block->get_error_message();
273 }
274
275 // Render using Block class' render() function.
276 // Output is already escaped in render() function.
277 echo WP_ConvertKit()->get_class( 'blocks_convertkit_' . $this->get_block_name() )->render( $this->get_settings_for_display() ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
278
279 }
280
281 /**
282 * Return the block for the Elementor Widget.
283 *
284 * @since 1.9.7.2
285 *
286 * @return WP_Error|array
287 */
288 private function get_block() {
289
290 // Get blocks.
291 $blocks = convertkit_get_blocks();
292
293 // Bail if no blocks are available.
294 if ( ! count( $blocks ) ) {
295 return new WP_Error( 'convertkit_elementor_widget_get_block_error', __( 'No blocks are registered. Register blocks using the `convertkit_blocks` filter.', 'convertkit' ) );
296 }
297
298 // Bail if block doesn't exist.
299 if ( ! array_key_exists( $this->get_block_name(), $blocks ) ) {
300 return new WP_Error(
301 'convertkit_elementor_widget_get_block_error',
302 sprintf(
303 /* translators: %1$s: Block name, %2$s: Elementor Widget name */
304 __( 'Block %1$s is not registered. Register using the `convertkit_blocks` filter, and ensure the Elementor Widget for this block has its `slug` property set to %2$s.', 'convertkit' ),
305 $this->get_block_name(),
306 $this->slug
307 )
308 );
309 }
310
311 // Return block.
312 return $blocks[ $this->get_block_name() ];
313
314 }
315
316 }
317