PluginProbe
Kit (formerly ConvertKit) – Email Newsletter, Email Marketing, Membership, Subscribers and Landing Pages / 2.7.7
Kit (formerly ConvertKit) – Email Newsletter, Email Marketing, Membership, Subscribers and Landing Pages v2.7.7
3.4.6 3.4.5 3.4.4 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 All 199 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 2.7.7, at includes/integrations/elementor/class-convertkit-elementor-widget.php

305 lines 6.3 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 control depending on the field type.
182 switch ( $field['type'] ) {
183 /**
184 * Select
185 */
186 case 'resource':
187 case 'select':
188 $control = array_merge(
189 $control,
190 array(
191 'type' => Elementor\Controls_Manager::SELECT,
192 'options' => $field['values'],
193 )
194 );
195 break;
196
197 /**
198 * Number
199 */
200 case 'number':
201 $control = array_merge(
202 $control,
203 array(
204 'type' => Elementor\Controls_Manager::NUMBER,
205 'min' => $field['min'],
206 'max' => $field['max'],
207 'step' => $field['step'],
208 )
209 );
210 break;
211
212 /**
213 * Toggle
214 */
215 case 'toggle':
216 $control = array_merge(
217 $control,
218 array(
219 'type' => Elementor\Controls_Manager::SWITCHER,
220 )
221 );
222 break;
223
224 /**
225 * Color Picker
226 */
227 case 'color':
228 $control = array_merge(
229 $control,
230 array(
231 'type' => Elementor\Controls_Manager::COLOR,
232 )
233 );
234 break;
235
236 default:
237 $control = array_merge(
238 $control,
239 array(
240 'type' => Elementor\Controls_Manager::TEXT,
241 )
242 );
243 break;
244
245 }
246
247 return $control;
248
249 }
250
251 /**
252 * Renders the block.
253 *
254 * @since 1.9.7.2
255 */
256 protected function render() {
257
258 // Bail if the block could not be found.
259 if ( is_wp_error( $this->block ) ) {
260 return $this->block->get_error_message();
261 }
262
263 // Render using Block class' render() function.
264 // Output is already escaped in render() function.
265 echo WP_ConvertKit()->get_class( 'blocks_convertkit_' . $this->get_block_name() )->render( $this->get_settings_for_display() ); // phpcs:ignore WordPress.Security.EscapeOutput
266
267 }
268
269 /**
270 * Return the block for the Elementor Widget.
271 *
272 * @since 1.9.7.2
273 *
274 * @return WP_Error|array
275 */
276 private function get_block() {
277
278 // Get blocks.
279 $blocks = convertkit_get_blocks();
280
281 // Bail if no blocks are available.
282 if ( ! count( $blocks ) ) {
283 return new WP_Error( 'convertkit_elementor_widget_get_block_error', __( 'No blocks are registered. Register blocks using the `convertkit_blocks` filter.', 'convertkit' ) );
284 }
285
286 // Bail if block doesn't exist.
287 if ( ! array_key_exists( $this->get_block_name(), $blocks ) ) {
288 return new WP_Error(
289 'convertkit_elementor_widget_get_block_error',
290 sprintf(
291 /* translators: %1$s: Block name, %2$s: Elementor Widget name */
292 __( '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' ),
293 $this->get_block_name(),
294 $this->slug
295 )
296 );
297 }
298
299 // Return block.
300 return $blocks[ $this->get_block_name() ];
301
302 }
303
304 }
305