PluginProbe
Kit (formerly ConvertKit) – Email Newsletter, Email Marketing, Membership, Subscribers and Landing Pages / 2.1.2
Kit (formerly ConvertKit) – Email Newsletter, Email Marketing, Membership, Subscribers and Landing Pages v2.1.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.php

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

342 lines 9.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * ConvertKit Form Block class.
4 *
5 * @package ConvertKit
6 * @author ConvertKit
7 */
8
9 /**
10 * ConvertKit Form Block for Gutenberg and Shortcode.
11 *
12 * @package ConvertKit
13 * @author ConvertKit
14 */
15 class ConvertKit_Block_Form extends ConvertKit_Block {
16
17 /**
18 * Constructor
19 *
20 * @since 1.9.6
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 for this Gutenberg Block in the editor view.
31 add_action( 'convertkit_gutenberg_enqueue_scripts', array( $this, 'enqueue_scripts_editor' ) );
32
33 // Enqueue styles for this Gutenberg Block in the editor view.
34 add_action( 'convertkit_gutenberg_enqueue_styles', array( $this, 'enqueue_styles_editor' ) );
35
36 }
37
38 /**
39 * Enqueues scripts for this Gutenberg Block in the editor view.
40 *
41 * @since 1.9.6.5
42 */
43 public function enqueue_scripts_editor() {
44
45 wp_enqueue_script( 'convertkit-gutenberg-block-form', CONVERTKIT_PLUGIN_URL . 'resources/backend/js/gutenberg-block-form.js', array( 'convertkit-gutenberg' ), CONVERTKIT_PLUGIN_VERSION, true );
46
47 }
48
49 /**
50 * Enqueues styles for this Gutenberg Block in the editor view.
51 *
52 * @since 1.9.6.9
53 */
54 public function enqueue_styles_editor() {
55
56 wp_enqueue_style( 'convertkit-gutenberg', CONVERTKIT_PLUGIN_URL . 'resources/backend/css/gutenberg.css', array( 'wp-edit-blocks' ), CONVERTKIT_PLUGIN_VERSION );
57
58 }
59
60 /**
61 * Returns this block's programmatic name, excluding the convertkit- prefix.
62 *
63 * @since 1.9.6
64 *
65 * @return string
66 */
67 public function get_name() {
68
69 /**
70 * This will register as:
71 * - a shortcode, with the name [convertkit_form].
72 * - a shortcode, with the name [convertkit], for backward compat.
73 * - a Gutenberg block, with the name convertkit/form.
74 */
75 return 'form';
76
77 }
78
79 /**
80 * Returns this block's Title, Icon, Categories, Keywords and properties.
81 *
82 * @since 1.9.6
83 *
84 * @return array
85 */
86 public function get_overview() {
87
88 return array(
89 'title' => __( 'ConvertKit Form', 'convertkit' ),
90 'description' => __( 'Displays a ConvertKit Form.', 'convertkit' ),
91 'icon' => 'resources/backend/images/block-icon-form.png',
92 'category' => 'convertkit',
93 'keywords' => array(
94 __( 'ConvertKit', 'convertkit' ),
95 __( 'Form', 'convertkit' ),
96 ),
97
98 // Function to call when rendering as a block or a shortcode on the frontend web site.
99 'render_callback' => array( $this, 'render' ),
100
101 // Shortcode: TinyMCE / QuickTags Modal Width and Height.
102 'modal' => array(
103 'width' => 500,
104 'height' => 100,
105 ),
106
107 // Shortcode: Include a closing [/shortcode] tag when using TinyMCE or QuickTag Modals.
108 'shortcode_include_closing_tag' => false,
109
110 // Gutenberg: Block Icon in Editor.
111 'gutenberg_icon' => file_get_contents( CONVERTKIT_PLUGIN_PATH . '/resources/backend/images/block-icon-form.svg' ), /* phpcs:ignore */
112
113 // Gutenberg: Example image showing how this block looks when choosing it in Gutenberg.
114 'gutenberg_example_image' => CONVERTKIT_PLUGIN_URL . 'resources/backend/images/block-example-form.png',
115
116 // Gutenberg: Help description, displayed when no settings defined for a newly added Block.
117 'gutenberg_help_description' => __( 'Select a Form using the Form option in the Gutenberg sidebar.', 'convertkit' ),
118
119 // Gutenberg: JS function to call when rendering the block preview in the Gutenberg editor.
120 // If not defined, render_callback above will be used.
121 'gutenberg_preview_render_callback' => 'convertKitGutenbergFormBlockRenderPreview',
122
123 // General: Any other strings for use in JS that need to support translation / i18n.
124 'i18n' => array(
125 /* translators: Form name in ConvertKit */
126 'gutenberg_form_modal' => __( 'Modal form "%s" selected. View on the frontend site to see the modal form.', 'convertkit' ),
127
128 /* translators: Form name in ConvertKit */
129 'gutenberg_form_slide_in' => __( 'Slide in form "%s" selected. View on the frontend site to see the slide in form.', 'convertkit' ),
130
131 /* translators: Form name in ConvertKit */
132 'gutenberg_form_sticky_bar' => __( 'Sticky bar form "%s" selected. View on the frontend site to see the sticky bar form.', 'convertkit' ),
133 ),
134 );
135
136 }
137
138 /**
139 * Returns this block's Attributes
140 *
141 * @since 1.9.6.5
142 *
143 * @return array
144 */
145 public function get_attributes() {
146
147 return array(
148 'form' => array(
149 'type' => 'string',
150 ),
151
152 // Always required for Gutenberg.
153 'is_gutenberg_example' => array(
154 'type' => 'boolean',
155 'default' => false,
156 ),
157 );
158
159 }
160
161 /**
162 * Returns this block's Fields
163 *
164 * @since 1.9.6
165 *
166 * @return bool|array
167 */
168 public function get_fields() {
169
170 // Bail if the request is not for the WordPress Administration or frontend editor.
171 if ( ! WP_ConvertKit()->is_admin_or_frontend_editor() ) {
172 return false;
173 }
174
175 // Get ConvertKit Forms.
176 $forms = array();
177 $convertkit_forms = new ConvertKit_Resource_Forms( 'block_edit' );
178 if ( $convertkit_forms->exist() ) {
179 foreach ( $convertkit_forms->get() as $form ) {
180 $forms[ absint( $form['id'] ) ] = sanitize_text_field( $form['name'] );
181 }
182 }
183
184 return array(
185 'form' => array(
186 'label' => __( 'Form', 'convertkit' ),
187 'type' => 'select',
188 'values' => $forms,
189 'data' => array(
190 // Used by resources/backend/js/gutenberg-block-form.js to determine the selected form's format
191 // (modal, slide in, sticky bar) and output a message in the block editor for the preview to explain
192 // why some formats cannot be previewed.
193 'forms' => ( $convertkit_forms->exist() ? $convertkit_forms->get() : array() ),
194 ),
195 ),
196 );
197
198 }
199
200 /**
201 * Returns this block's UI panels / sections.
202 *
203 * @since 1.9.6
204 *
205 * @return bool|array
206 */
207 public function get_panels() {
208
209 // Bail if the request is not for the WordPress Administration or frontend editor.
210 if ( ! WP_ConvertKit()->is_admin_or_frontend_editor() ) {
211 return false;
212 }
213
214 return array(
215 'general' => array(
216 'label' => __( 'General', 'convertkit' ),
217 'fields' => array(
218 'form',
219 ),
220 ),
221 );
222
223 }
224
225 /**
226 * Returns this block's Default Values
227 *
228 * @since 1.9.6
229 *
230 * @return array
231 */
232 public function get_default_values() {
233
234 return array(
235 'form' => '',
236 'id' => '', // Backward compat.
237 );
238
239 }
240
241 /**
242 * Returns the block's output, based on the supplied configuration attributes.
243 *
244 * @since 1.9.6
245 *
246 * @param array $atts Block / Shortcode Attributes.
247 * @return string Output
248 */
249 public function render( $atts ) {
250
251 // Parse shortcode attributes, defining fallback defaults if required.
252 $atts = shortcode_atts(
253 $this->get_default_values(),
254 $this->sanitize_atts( $atts ),
255 $this->get_name()
256 );
257
258 // Setup Settings class.
259 $settings = new ConvertKit_Settings();
260
261 // Determine Form ID.
262 // 'id' attribute is for backward compat.
263 $form_id = 0;
264 if ( $atts['form'] > 0 ) {
265 $form_id = $atts['form'];
266 } elseif ( $atts['id'] > 0 ) {
267 $form_id = $atts['id'];
268 }
269
270 // If no Form ID specified, bail.
271 if ( ! $form_id ) {
272 if ( $settings->debug_enabled() ) {
273 return '<!-- No Form ID Specified -->';
274 }
275
276 return '';
277 }
278
279 // Get Form HTML.
280 $forms = new ConvertKit_Resource_Forms( 'output_form' );
281 $form = $forms->get_html( $form_id );
282
283 // If an error occured, it might be that we're requesting a Form ID that exists in ConvertKit
284 // but does not yet exist in the Plugin's Form Resources.
285 // If so, refresh the Form Resources and try again.
286 if ( is_wp_error( $form ) ) {
287 // Refresh Forms from the API.
288 $forms->refresh();
289
290 // Get Form HTML again.
291 $form = $forms->get_html( $form_id );
292 }
293
294 // If an error still occured, the shortcode might be from the ConvertKit App for a Legacy Form ID
295 // These ConvertKit App shortcodes, for some reason, use a different Form ID than the one presented
296 // to us in the API.
297 // For example, a Legacy Form ID might be 470099, but the ConvertKit app says to use the shortcode [convertkit form=5281783]).
298 // In this instance, fetch the Form HTML without checking that the Form ID exists in the Form Resources.
299 if ( is_wp_error( $form ) ) {
300 // Initialize the API.
301 $api = new ConvertKit_API( $settings->get_api_key(), $settings->get_api_secret(), $settings->debug_enabled(), 'output_form' );
302
303 // Return Legacy Form HTML from the API, which bypasses any internal Plugin check to see if the Form ID exists.
304 $form = $api->get_form_html( $form_id );
305 }
306
307 // Finally, if we still get an error, there's nothing more we can do. The Form ID isn't valid.
308 if ( is_wp_error( $form ) ) {
309 if ( $settings->debug_enabled() ) {
310 return '<!-- ' . $form->get_error_message() . ' -->';
311 }
312
313 return '';
314 }
315
316 /**
317 * Filter the block's content immediately before it is output.
318 *
319 * @since 1.9.6
320 *
321 * @param string $form ConvertKit Form HTML.
322 * @param array $atts Block Attributes.
323 * @param int $form_id Form ID.
324 */
325 $form = apply_filters( 'convertkit_block_form_render', $form, $atts, $form_id );
326
327 /**
328 * Backward compat. filter for < 1.9.6. Filter the block's content immediately before it is output.
329 *
330 * @since 1.0.0
331 *
332 * @param string $form ConvertKit Form HTML.
333 * @param array $atts Block Attributes.
334 */
335 $form = apply_filters( 'wp_convertkit_get_form_embed', $form, $atts );
336
337 return $form;
338
339 }
340
341 }
342