PluginProbe
Kit (formerly ConvertKit) – Email Newsletter, Email Marketing, Membership, Subscribers and Landing Pages / 2.3.2
Kit (formerly ConvertKit) – Email Newsletter, Email Marketing, Membership, Subscribers and Landing Pages v2.3.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 2.3.2 2.3.3 All 194 releases
convertkit / includes / blocks / class-convertkit-block-form-trigger.php

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

488 lines 13.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * ConvertKit Form Trigger Button Block class.
4 *
5 * @package ConvertKit
6 * @author ConvertKit
7 */
8
9 /**
10 * ConvertKit Form Trigger Button Block for Gutenberg and Shortcode.
11 *
12 * @package ConvertKit
13 * @author ConvertKit
14 */
15 class ConvertKit_Block_Form_Trigger extends ConvertKit_Block {
16
17 /**
18 * Constructor
19 *
20 * @since 2.2.0
21 */
22 public function __construct() {
23
24 // Register this as a shortcode in the ConvertKit Plugin.
25 add_filter( 'convertkit_shortcodes', array( $this, 'register' ) );
26
27 // Register this as a Gutenberg block in the ConvertKit Plugin.
28 add_filter( 'convertkit_blocks', array( $this, 'register' ) );
29
30 // Enqueue scripts and styles for this Gutenberg Block in the editor view.
31 add_action( 'convertkit_gutenberg_enqueue_scripts', array( $this, 'enqueue_scripts_editor' ) );
32
33 // Enqueue scripts and styles for this Gutenberg Block in the editor and frontend views.
34 add_action( 'convertkit_gutenberg_enqueue_styles_editor_and_frontend', array( $this, 'enqueue_styles' ) );
35
36 }
37
38 /**
39 * Enqueues scripts for this Gutenberg Block in the editor view.
40 *
41 * @since 2.2.0
42 */
43 public function enqueue_scripts_editor() {
44
45 wp_enqueue_script( 'convertkit-gutenberg-block-form-trigger', CONVERTKIT_PLUGIN_URL . 'resources/backend/js/gutenberg-block-form-trigger.js', array( 'convertkit-gutenberg' ), CONVERTKIT_PLUGIN_VERSION, true );
46
47 }
48
49 /**
50 * Enqueues styles for this Gutenberg Block in the editor and frontend views.
51 *
52 * @since 2.2.0
53 */
54 public function enqueue_styles() {
55
56 wp_enqueue_style( 'convertkit-button', CONVERTKIT_PLUGIN_URL . 'resources/frontend/css/button.css', array(), CONVERTKIT_PLUGIN_VERSION );
57
58 }
59
60 /**
61 * Returns this block's programmatic name, excluding the convertkit- prefix.
62 *
63 * @since 2.2.0
64 */
65 public function get_name() {
66
67 /**
68 * This will register as:
69 * - a shortcode, with the name [convertkit_formtrigger].
70 * - a Gutenberg block, with the name convertkit/formtrigger.
71 */
72 return 'formtrigger';
73
74 }
75
76 /**
77 * Returns this block's Title, Icon, Categories, Keywords and properties.
78 *
79 * @since 2.2.0
80 */
81 public function get_overview() {
82
83 $convertkit_forms = new ConvertKit_Resource_Forms( 'block_edit' );
84 $settings = new ConvertKit_Settings();
85
86 return array(
87 'title' => __( 'ConvertKit Form Trigger', 'convertkit' ),
88 'description' => __( 'Displays a modal, sticky bar or slide in form to display when the button is pressed.', 'convertkit' ),
89 'icon' => 'resources/backend/images/block-icon-formtrigger.svg',
90 'category' => 'convertkit',
91 'keywords' => array(
92 __( 'ConvertKit', 'convertkit' ),
93 __( 'Form', 'convertkit' ),
94 ),
95
96 // Function to call when rendering as a block or a shortcode on the frontend web site.
97 'render_callback' => array( $this, 'render' ),
98
99 // Shortcode: TinyMCE / QuickTags Modal Width and Height.
100 'modal' => array(
101 'width' => 500,
102 'height' => 352,
103 ),
104
105 // Shortcode: Include a closing [/shortcode] tag when using TinyMCE or QuickTag Modals.
106 'shortcode_include_closing_tag' => false,
107
108 // Gutenberg: Block Icon in Editor.
109 'gutenberg_icon' => convertkit_get_file_contents( CONVERTKIT_PLUGIN_PATH . '/resources/backend/images/block-icon-formtrigger.svg' ),
110
111 // Gutenberg: Example image showing how this block looks when choosing it in Gutenberg.
112 'gutenberg_example_image' => CONVERTKIT_PLUGIN_URL . 'resources/backend/images/block-example-formtrigger.png',
113
114 // Help descriptions, displayed when no API key / resources exist and this block/shortcode is added.
115 'no_api_key' => array(
116 'notice' => __( 'No API Key specified.', 'convertkit' ),
117 'link' => convertkit_get_setup_wizard_plugin_link(),
118 'link_text' => __( 'Click here to add your API Key.', 'convertkit' ),
119 ),
120 'no_resources' => array(
121 'notice' => __( 'No modal, sticky bar or slide in forms exist in ConvertKit.', 'convertkit' ),
122 'link' => convertkit_get_new_form_url(),
123 'link_text' => __( 'Click here to create a form.', 'convertkit' ),
124 ),
125 'gutenberg_help_description' => __( 'Select a Form using the Form option in the Gutenberg sidebar.', 'convertkit' ),
126
127 // Gutenberg: JS function to call when rendering the block preview in the Gutenberg editor.
128 // If not defined, render_callback above will be used.
129 'gutenberg_preview_render_callback' => 'convertKitGutenbergFormTriggerBlockRenderPreview',
130
131 // Whether an API Key exists in the Plugin, and are the required resources (non-inline forms) available.
132 // If no API Key is specified in the Plugin's settings, render the "No API Key" output.
133 'has_api_key' => $settings->has_api_key_and_secret(),
134 'has_resources' => $convertkit_forms->non_inline_exist(),
135 );
136
137 }
138
139 /**
140 * Returns this block's Attributes
141 *
142 * @since 2.2.0
143 */
144 public function get_attributes() {
145
146 return array(
147 // Block attributes.
148 'form' => array(
149 'type' => 'string',
150 'default' => $this->get_default_value( 'form' ),
151 ),
152 'text' => array(
153 'type' => 'string',
154 'default' => $this->get_default_value( 'text' ),
155 ),
156
157 // The below are built in Gutenberg attributes registered in get_supports().
158
159 // Color.
160 'backgroundColor' => array(
161 'type' => 'string',
162 ),
163 'textColor' => array(
164 'type' => 'string',
165 ),
166
167 // Typography.
168 'fontSize' => array(
169 'type' => 'string',
170 ),
171
172 // Spacing/Dimensions > Padding.
173 'style' => array(
174 'type' => 'object',
175 'visualizers' => array(
176 'type' => 'object',
177 'padding' => array(
178 'type' => 'object',
179 'top' => array(
180 'type' => 'boolean',
181 ),
182 'bottom' => array(
183 'type' => 'boolean',
184 ),
185 'left' => array(
186 'type' => 'boolean',
187 ),
188 'right' => array(
189 'type' => 'boolean',
190 ),
191 ),
192 ),
193 ),
194
195 // Always required for Gutenberg.
196 'is_gutenberg_example' => array(
197 'type' => 'boolean',
198 'default' => false,
199 ),
200 );
201
202 }
203
204 /**
205 * Returns this block's supported built-in Attributes.
206 *
207 * @since 2.2.0
208 *
209 * @return array Supports
210 */
211 public function get_supports() {
212
213 return array(
214 'className' => true,
215 'color' => array(
216 'background' => true,
217 'text' => true,
218 ),
219 'typography' => array(
220 'fontSize' => true,
221 ),
222 'spacing' => array(
223 'padding' => array(
224 'horizontal',
225 'vertical',
226 ),
227 ),
228 );
229
230 }
231
232 /**
233 * Returns this block's Fields
234 *
235 * @since 2.2.0
236 *
237 * @return bool|array
238 */
239 public function get_fields() {
240
241 // Bail if the request is not for the WordPress Administration or frontend editor.
242 if ( ! WP_ConvertKit()->is_admin_or_frontend_editor() ) {
243 return false;
244 }
245
246 // Get non-inline ConvertKit Forms.
247 $forms = array();
248 $convertkit_forms = new ConvertKit_Resource_Forms( 'block_edit' );
249 if ( $convertkit_forms->non_inline_exist() ) {
250 foreach ( $convertkit_forms->get_non_inline() as $form ) {
251 $forms[ absint( $form['id'] ) ] = sanitize_text_field( $form['name'] );
252 }
253 }
254
255 // Gutenberg's built-in fields (such as styling, padding etc) don't need to be defined here, as they'll be included
256 // automatically by Gutenberg.
257 return array(
258 'form' => array(
259 'label' => __( 'Form', 'convertkit' ),
260 'type' => 'select',
261 'values' => $forms,
262 'description' => __( 'The modal, sticky bar or slide in form to display when the button is pressed. To embed a form, use the ConvertKit Form block instead.', 'convertkit' ),
263 ),
264 'text' => array(
265 'label' => __( 'Button Text', 'convertkit' ),
266 'type' => 'text',
267 'description' => __( 'The text to display for the button.', 'convertkit' ),
268 ),
269
270 // These fields will only display on the shortcode, and are deliberately not registered in get_attributes(),
271 // because Gutenberg will register its own color pickers for link, background and text.
272 'background_color' => array(
273 'label' => __( 'Background color', 'convertkit' ),
274 'type' => 'color',
275 ),
276 'text_color' => array(
277 'label' => __( 'Text color', 'convertkit' ),
278 'type' => 'color',
279 ),
280 );
281
282 }
283
284 /**
285 * Returns this block's UI panels / sections.
286 *
287 * @since 2.2.0
288 *
289 * @return bool|array
290 */
291 public function get_panels() {
292
293 // Bail if the request is not for the WordPress Administration or frontend editor.
294 if ( ! WP_ConvertKit()->is_admin_or_frontend_editor() ) {
295 return false;
296 }
297
298 // Gutenberg's built-in fields (such as styling, padding etc) don't need to be defined here, as they'll be included
299 // automatically by Gutenberg.
300 return array(
301 'general' => array(
302 'label' => __( 'General', 'convertkit' ),
303 'fields' => array(
304 'form',
305 'text',
306 'background_color',
307 'text_color',
308 ),
309 ),
310 );
311
312 }
313
314 /**
315 * Returns this block's Default Values
316 *
317 * @since 2.2.0
318 *
319 * @return array
320 */
321 public function get_default_values() {
322
323 return array(
324 'form' => '',
325 'text' => __( 'Subscribe', 'convertkit' ),
326 'background_color' => '',
327 'text_color' => '',
328
329 // Built-in Gutenberg block attributes.
330 'backgroundColor' => '',
331 'textColor' => '',
332 'fontSize' => '',
333 'style' => array(
334 'visualizers' => array(
335 'padding' => array(
336 'top' => '',
337 'bottom' => '',
338 'left' => '',
339 'right' => '',
340 ),
341 ),
342 ),
343 );
344
345 }
346
347 /**
348 * Returns the block's output, based on the supplied configuration attributes.
349 *
350 * @since 2.2.0
351 *
352 * @param array $atts Block / Shortcode Attributes.
353 * @return string Output
354 */
355 public function render( $atts ) {
356
357 // Parse attributes, defining fallback defaults if required
358 // and moving some attributes (such as Gutenberg's styles), if defined.
359 $atts = $this->sanitize_and_declare_atts( $atts );
360
361 // Setup Settings class.
362 $settings = new ConvertKit_Settings();
363
364 // Build HTML.
365 $html = $this->get_html( $atts['form'], $atts['text'], $atts['_css_classes'], $atts['_css_styles'], $this->is_block_editor_request() );
366
367 // Bail if an error occured.
368 if ( is_wp_error( $html ) ) {
369 if ( $settings->debug_enabled() ) {
370 return '<!-- ' . $html->get_error_message() . ' -->';
371 }
372
373 return '';
374 }
375
376 /**
377 * Filter the block's content immediately before it is output.
378 *
379 * @since 2.2.0
380 *
381 * @param string $html ConvertKit Button HTML.
382 * @param array $atts Block Attributes.
383 */
384 $html = apply_filters( 'convertkit_block_form_trigger_render', $html, $atts );
385
386 return $html;
387
388 }
389
390 /**
391 * Returns the HTML button markup for the given Form ID.
392 *
393 * @since 2.0.0
394 *
395 * @param int $id Form ID.
396 * @param string $button_text Button Text.
397 * @param array $css_classes CSS classes to apply to link (typically included when using Gutenberg).
398 * @param array $css_styles CSS inline styles to apply to link (typically included when using Gutenberg).
399 * @param bool $return_as_span If true, returns a <span> instead of <a>. Useful for the block editor so that the element is interactible.
400 * @return WP_Error|string Button HTML
401 */
402 private function get_html( $id, $button_text, $css_classes = array(), $css_styles = array(), $return_as_span = false ) {
403
404 // Cast ID to integer.
405 $id = absint( $id );
406
407 // Load classes.
408 $convertkit_forms = new ConvertKit_Resource_Forms( 'render' );
409
410 // Get form.
411 $form = $convertkit_forms->get_by_id( $id );
412
413 // Bail if the form could not be found.
414 if ( ! $form ) {
415 return new WP_Error(
416 'convertkit_block_form_trigger_get_html',
417 sprintf(
418 /* translators: ConvertKit Form ID */
419 __( 'ConvertKit Form ID %s does not exist on ConvertKit.', 'convertkit' ),
420 $id
421 )
422 );
423 }
424
425 // Bail if no uid or embed_js properties exist.
426 if ( ! array_key_exists( 'uid', $form ) ) {
427 return new WP_Error(
428 'convertkit_block_form_trigger_get_html',
429 sprintf(
430 /* translators: ConvertKit Form ID */
431 __( 'ConvertKit Form ID %s has no uid property.', 'convertkit' ),
432 $id
433 )
434 );
435 }
436 if ( ! array_key_exists( 'embed_js', $form ) ) {
437 return new WP_Error(
438 'convertkit_block_form_trigger_get_html',
439 sprintf(
440 /* translators: ConvertKit Form ID */
441 __( 'ConvertKit Form ID %s has no embed_js property.', 'convertkit' ),
442 $id
443 )
444 );
445 }
446
447 // Build button HTML.
448 $html = '<div class="convertkit-button">';
449
450 if ( $return_as_span ) {
451 $html .= '<span';
452 } else {
453 $html .= '<a data-formkit-toggle="' . esc_attr( $form['uid'] ) . '" href="' . esc_url( $form['embed_url'] ) . '"';
454 }
455
456 $html .= ' class="wp-block-button__link ' . implode( ' ', map_deep( $css_classes, 'sanitize_html_class' ) ) . '" style="' . implode( ';', map_deep( $css_styles, 'esc_attr' ) ) . '">';
457 $html .= esc_html( $button_text );
458
459 if ( $return_as_span ) {
460 $html .= '</span>';
461 } else {
462 $html .= '</a>';
463 }
464
465 $html .= '</div>';
466
467 // Register the script, so it's only loaded once for this non-inline form across the entire page.
468 add_filter(
469 'convertkit_output_scripts_footer',
470 function ( $scripts ) use ( $form ) {
471
472 $scripts[] = array(
473 'async' => true,
474 'data-uid' => $form['uid'],
475 'src' => $form['embed_js'],
476 );
477
478 return $scripts;
479
480 }
481 );
482
483 // Return.
484 return $html;
485 }
486
487 }
488