PluginProbe
Kit (formerly ConvertKit) – Email Newsletter, Email Marketing, Membership, Subscribers and Landing Pages / 1.9.7.2
Kit (formerly ConvertKit) – Email Newsletter, Email Marketing, Membership, Subscribers and Landing Pages v1.9.7.2
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 2.2.9 2.3.0 All 197 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 1.9.7.2, at includes/blocks/class-convertkit-block-form.php

342 lines 9.4 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 // Register additional scripts for this Gutenberg Block.
31 add_action( 'convertkit_gutenberg_enqueue_scripts', array( $this, 'enqueue_scripts' ) );
32
33 // Register additional stylesheets for this Gutenberg Block.
34 add_action( 'convertkit_gutenberg_enqueue_styles', array( $this, 'enqueue_styles' ) );
35
36 }
37
38 /**
39 * Enqueues additional scripts for this Gutenberg Block.
40 *
41 * @since 1.9.6.5
42 */
43 public function enqueue_scripts() {
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 additional scripts for this Gutenberg Block.
51 *
52 * @since 1.9.6.9
53 */
54 public function enqueue_styles() {
55
56 wp_enqueue_style( 'convertkit-gutenberg-block-form', CONVERTKIT_PLUGIN_URL . '/resources/backend/css/gutenberg-block-form.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 mixed 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();
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 // Get Settings.
185 $settings = new ConvertKit_Settings();
186
187 return array(
188 'form' => array(
189 'label' => __( 'Form', 'convertkit' ),
190 'type' => 'select',
191 'values' => $forms,
192 'data' => array(
193 'forms' => $convertkit_forms->get(),
194 'api_key' => $settings->get_api_key(),
195 ),
196 ),
197 );
198
199 }
200
201 /**
202 * Returns this block's UI panels / sections.
203 *
204 * @since 1.9.6
205 *
206 * @return mixed bool | array
207 */
208 public function get_panels() {
209
210 // Bail if the request is not for the WordPress Administration or frontend editor.
211 if ( ! WP_ConvertKit()->is_admin_or_frontend_editor() ) {
212 return false;
213 }
214
215 return array(
216 'general' => array(
217 'label' => __( 'General', 'convertkit' ),
218 'fields' => array(
219 'form',
220 ),
221 ),
222 );
223
224 }
225
226 /**
227 * Returns this block's Default Values
228 *
229 * @since 1.9.6
230 *
231 * @return array
232 */
233 public function get_default_values() {
234
235 return array(
236 'form' => '',
237 'id' => '', // Backward compat.
238 );
239
240 }
241
242 /**
243 * Returns the block's output, based on the supplied configuration attributes.
244 *
245 * @since 1.9.6
246 *
247 * @param array $atts Block / Shortcode Attributes.
248 * @return string Output
249 */
250 public function render( $atts ) {
251
252 // Parse shortcode attributes, defining fallback defaults if required.
253 $atts = shortcode_atts(
254 $this->get_default_values(),
255 $this->sanitize_atts( $atts ),
256 $this->get_name()
257 );
258
259 // Setup Settings class.
260 $settings = new ConvertKit_Settings();
261
262 // Determine Form ID.
263 // 'id' attribute is for backward compat.
264 $form_id = 0;
265 if ( $atts['form'] > 0 ) {
266 $form_id = $atts['form'];
267 } elseif ( $atts['id'] > 0 ) {
268 $form_id = $atts['id'];
269 }
270
271 // If no Form ID specified, bail.
272 if ( ! $form_id ) {
273 if ( $settings->debug_enabled() ) {
274 return '<!-- No Form ID Specified -->';
275 }
276
277 return '';
278 }
279
280 // Get Form HTML.
281 $forms = new ConvertKit_Resource_Forms();
282 $form = $forms->get_html( $form_id );
283
284 // If an error occured, it might be that we're requesting a Form ID that exists in ConvertKit
285 // but does not yet exist in the Plugin's Form Resources.
286 // If so, refresh the Form Resources and try again.
287 if ( is_wp_error( $form ) ) {
288 // Refresh Forms from the API.
289 $forms->refresh();
290
291 // Get Form HTML again.
292 $form = $forms->get_html( $form_id );
293 }
294
295 // If an error still occured, the shortcode might be from the ConvertKit App for a Legacy Form ID
296 // These ConvertKit App shortcodes, for some reason, use a different Form ID than the one presented
297 // to us in the API.
298 // For example, a Legacy Form ID might be 470099, but the ConvertKit app says to use the shortcode [convertkit form=5281783]).
299 // In this instance, fetch the Form HTML without checking that the Form ID exists in the Form Resources.
300 if ( is_wp_error( $form ) ) {
301 // Initialize the API.
302 $api = new ConvertKit_API( $settings->get_api_key(), $settings->get_api_secret(), $settings->debug_enabled() );
303
304 // Return Legacy Form HTML from the API, which bypasses any internal Plugin check to see if the Form ID exists.
305 $form = $api->get_form_html( $form_id );
306 }
307
308 // Finally, if we still get an error, there's nothing more we can do. The Form ID isn't valid.
309 if ( is_wp_error( $form ) ) {
310 if ( $settings->debug_enabled() ) {
311 return '<!-- ' . $form->get_error_message() . ' -->';
312 }
313
314 return '';
315 }
316
317 /**
318 * Filter the block's content immediately before it is output.
319 *
320 * @since 1.9.6
321 *
322 * @param string $form ConvertKit Form HTML.
323 * @param array $atts Block Attributes.
324 */
325 $form = apply_filters( 'convertkit_block_form_render', $form, $atts );
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