PluginProbe
Kit (formerly ConvertKit) – Email Newsletter, Email Marketing, Membership, Subscribers and Landing Pages / 1.9.7.4
Kit (formerly ConvertKit) – Email Newsletter, Email Marketing, Membership, Subscribers and Landing Pages v1.9.7.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 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 1.9.7.4, at includes/integrations/elementor/class-convertkit-elementor-widget.php

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