PluginProbe ʕ •ᴥ•ʔ
SureForms – Contact Form Builder, AI Forms, Payment Form, Survey & Quiz / 2.12.1
SureForms – Contact Form Builder, AI Forms, Payment Form, Survey & Quiz v2.12.1
2.12.6 2.12.5 2.12.4 2.12.3 2.12.2 2.12.1 2.12.0 2.11.1 2.11.0 2.10.1 2.10.0 2.9.1 2.9.0 2.8.2 2.8.1 2.7.0 2.7.1 2.8.0 trunk 0.0.10 0.0.11 0.0.12 0.0.13 0.0.2 0.0.3 0.0.4 0.0.5 0.0.6 0.0.7 0.0.8 0.0.9 1.0.0 1.0.1 1.0.2 1.0.3 1.0.4 1.0.5 1.0.6 1.0.7 1.1.0 1.1.1 1.1.2 1.10.0 1.10.1 1.11.0 1.12.0 1.12.1 1.12.2 1.12.3 1.13.0 1.13.1 1.13.2 1.2.0 1.2.1 1.2.2 1.2.3 1.2.4 1.2.5 1.3.0 1.3.1 1.3.2 1.4.0 1.4.1 1.4.2 1.4.3 1.4.4 1.4.5 1.5.0 1.5.1 1.6.0 1.6.1 1.6.2 1.6.3 1.6.4 1.6.5 1.7.0 1.7.1 1.7.2 1.7.3 1.7.4 1.8.0 1.9.0 1.9.1 2.0.0 2.0.1 2.0.2 2.1.0 2.1.1 2.2.0 2.2.1 2.2.2 2.3.0 2.4.0 2.5.0 2.5.2 2.6.0
sureforms / inc / gutenberg-hooks.php
sureforms / inc Last commit date
abilities 1 month ago admin 3 months ago ai-form-builder 1 month ago blocks 2 months ago compatibility 2 months ago database 2 months ago email 5 months ago fields 2 months ago global-settings 1 month ago lib 1 month ago migrator 2 months ago page-builders 2 months ago payments 1 month ago single-form-settings 2 months ago traits 2 months ago activator.php 1 year ago admin-ajax.php 2 months ago background-process.php 9 months ago create-new-form.php 3 months ago duplicate-form.php 3 months ago entries.php 2 months ago events-scheduler.php 2 years ago export.php 3 months ago field-validation.php 1 month ago form-restriction.php 2 months ago form-styling.php 5 months ago form-submit.php 2 months ago forms-data.php 5 months ago frontend-assets.php 1 month ago generate-form-markup.php 2 months ago gutenberg-hooks.php 2 months ago helper.php 2 months ago learn.php 4 months ago onboarding.php 2 months ago post-types.php 2 months ago rest-api.php 1 month ago smart-tags.php 4 months ago submit-token.php 5 months ago translatable.php 1 month ago updater-callbacks.php 1 year ago updater.php 1 year ago
gutenberg-hooks.php
347 lines
1 <?php
2 /**
3 * Gutenberg Hooks Manager Class.
4 *
5 * @package sureforms.
6 */
7
8 namespace SRFM\Inc;
9
10 use SRFM\Inc\Traits\Get_Instance;
11
12 if ( ! defined( 'ABSPATH' ) ) {
13 exit; // Exit if accessed directly.
14 }
15
16 /**
17 * Gutenberg hooks handler class.
18 *
19 * @since 0.0.1
20 */
21 class Gutenberg_Hooks {
22 use Get_Instance;
23 /**
24 * Block patterns to register.
25 *
26 * @var array<mixed>
27 */
28 protected $patterns = [];
29
30 /**
31 * Class constructor.
32 *
33 * @return void
34 * @since 0.0.1
35 */
36 public function __construct() {
37 // Setting Form default patterns.
38 $this->patterns = [
39 'blank-form',
40 'contact-form',
41 'newsletter-form',
42 'support-form',
43 'feedback-form',
44 'event-rsvp-form',
45 'subscription-form',
46 ];
47
48 // Initializing hooks.
49 add_action( 'enqueue_block_editor_assets', [ $this, 'form_editor_screen_assets' ] );
50 add_action( 'enqueue_block_editor_assets', [ $this, 'block_editor_assets' ] );
51 add_filter( 'block_categories_all', [ $this, 'register_block_categories' ], 10, 2 );
52 add_filter( 'allowed_block_types_all', [ $this, 'disable_forms_wrapper_block' ], 10, 2 );
53 add_action( 'save_post_sureforms_form', [ $this, 'update_field_slug' ], 10, 2 );
54 add_action( 'load-post.php', [ $this, 'maybe_migrate_form_stylings' ] );
55 }
56
57 /**
58 * Disable Sureforms_Form Block and allowed only sureforms block inside Sureform CPT editor.
59 *
60 * @param bool|array<string> $allowed_block_types Array of block types.
61 * @param \WP_Block_Editor_Context $editor_context The current block editor context.
62 * @return array<mixed>|bool
63 * @since 0.0.1
64 */
65 public function disable_forms_wrapper_block( $allowed_block_types, $editor_context ) {
66 if ( ! empty( $editor_context->post->post_type ) && 'sureforms_form' === $editor_context->post->post_type ) {
67 $allow_block_types = [
68 'srfm/input',
69 'srfm/email',
70 'srfm/textarea',
71 'srfm/number',
72 'srfm/checkbox',
73 'srfm/gdpr',
74 'srfm/phone',
75 'srfm/address',
76 'srfm/dropdown',
77 'srfm/multi-choice',
78 'srfm/url',
79 'srfm/separator',
80 'srfm/icon',
81 'srfm/image',
82 'srfm/advanced-heading',
83 'srfm/inline-button',
84 'srfm/payment',
85 ];
86 // Apply a filter to the $allow_block_types types array.
87 return apply_filters( 'srfm_allowed_block_types', $allow_block_types, $editor_context );
88 }
89
90 // Return the default $allowed_block_types value.
91 return $allowed_block_types;
92 }
93
94 /**
95 * Register our custom block category.
96 *
97 * @param array<mixed> $categories Array of categories.
98 * @param \WP_Block_Editor_Context $block_editor_context The current block editor context.
99 * @return array<mixed>
100 * @since 0.0.1
101 */
102 public function register_block_categories( $categories, $block_editor_context ) {
103
104 $post_type = $block_editor_context->post->post_type ?? '';
105
106 if ( $post_type && SRFM_FORMS_POST_TYPE === $post_type ) {
107 $title = esc_html__( 'General Fields', 'sureforms' );
108 } else {
109 $title = esc_html__( 'SureForms', 'sureforms' );
110 }
111
112 $custom_categories = [
113 [
114 'slug' => 'sureforms',
115 'title' => $title,
116 ],
117 [
118 'slug' => 'sureforms-pro',
119 'title' => esc_html__( 'Advanced Fields', 'sureforms' ),
120 ],
121 ];
122
123 return array_merge( $custom_categories, $categories );
124 }
125
126 /**
127 * Add Form Editor Scripts.
128 *
129 * @return void
130 * @since 0.0.1
131 */
132 public function form_editor_screen_assets() {
133 $form_editor_script = '-formEditor';
134
135 $screen = get_current_screen();
136 $post_types = [ SRFM_FORMS_POST_TYPE ];
137
138 if ( is_null( $screen ) || ! in_array( $screen->post_type, $post_types, true ) ) {
139 return;
140 }
141
142 $script_asset_path = SRFM_DIR . 'assets/build/formEditor.asset.php';
143 $script_info = file_exists( $script_asset_path )
144 ? include $script_asset_path
145 : [
146 'dependencies' => [],
147 'version' => SRFM_VER,
148 ];
149
150 wp_enqueue_script( SRFM_SLUG . $form_editor_script, SRFM_URL . 'assets/build/formEditor.js', $script_info['dependencies'], $script_info['version'], true );
151 wp_localize_script( SRFM_SLUG . $form_editor_script, 'scIcons', [ 'path' => SRFM_URL . 'assets/build/icon-assets' ] );
152
153 // Enqueue the code editor for the Custom CSS Editor in SureForms.
154 wp_enqueue_code_editor( [ 'type' => 'text/css' ] );
155 wp_enqueue_script( 'wp-theme-plugin-editor' );
156 wp_enqueue_style( 'wp-codemirror' );
157
158 wp_localize_script(
159 SRFM_SLUG . $form_editor_script,
160 SRFM_SLUG . '_block_data',
161 [
162 'plugin_url' => SRFM_URL,
163 'admin_email' => get_option( 'admin_email' ),
164 'pro_plugin_name' => defined( 'SRFM_PRO_VER' ) && defined( 'SRFM_PRO_PRODUCT' ) ? SRFM_PRO_PRODUCT : 'free',
165 ]
166 );
167
168 Helper::register_script_translations( SRFM_SLUG . $form_editor_script );
169 }
170
171 /**
172 * Register all editor scripts.
173 *
174 * @return void
175 * @since 0.0.1
176 */
177 public function block_editor_assets() {
178 $all_screen_blocks = '-blocks';
179 $screen = get_current_screen();
180
181 $blocks_asset_path = SRFM_DIR . 'assets/build/blocks.asset.php';
182 $blocks_info = file_exists( $blocks_asset_path )
183 ? include $blocks_asset_path
184 : [
185 'dependencies' => [],
186 'version' => SRFM_VER,
187 ];
188 wp_enqueue_script( SRFM_SLUG . $all_screen_blocks, SRFM_URL . 'assets/build/blocks.js', $blocks_info['dependencies'], SRFM_VER, true );
189
190 Helper::register_script_translations( SRFM_SLUG . $all_screen_blocks );
191
192 $site_url = Helper::get_string_value( wp_parse_url( esc_url( get_site_url() ), PHP_URL_HOST ) );
193 $site_url = preg_replace( '/^www\./', '', $site_url );
194
195 wp_localize_script(
196 SRFM_SLUG . $all_screen_blocks,
197 SRFM_SLUG . '_block_data',
198 [
199 'template_picker_url' => admin_url( '/admin.php?page=add-new-form' ),
200 'plugin_url' => SRFM_URL,
201 'admin_email' => get_option( 'admin_email' ),
202 'post_url' => admin_url( 'post.php' ),
203 'current_screen' => $screen,
204 'smart_tags_array' => Smart_Tags::smart_tag_list(),
205 'smart_tags_array_email' => Smart_Tags::email_smart_tag_list(),
206 'srfm_form_markup_nonce' => wp_create_nonce( 'srfm_form_markup' ),
207 'get_form_markup_url' => 'sureforms/v1/generate-form-markup',
208 'is_pro_active' => Helper::has_pro(),
209 'srfm_default_dynamic_block_option' => wp_parse_args( Helper::get_array_value( get_option( 'srfm_default_dynamic_block_option', [] ) ), Helper::default_dynamic_block_option() ),
210 'form_selector_nonce' => Helper::current_user_can( 'edit_posts' ) ? wp_create_nonce( 'wp_rest' ) : '',
211 'is_admin_user' => Helper::current_user_can(),
212 'site_url' => $site_url,
213 'is_suremails_active' => is_plugin_active( 'suremails/suremails.php' ),
214 'upgrade_url' => add_query_arg(
215 [ 'utm_medium' => 'embed_styling_upgrade' ],
216 Helper::get_sureforms_website_url( 'pricing' )
217 ),
218 'default_translations' => [
219 'gdpr_label' => __( 'I consent to have this website store my submitted information so they can respond to my inquiry.', 'sureforms' ),
220 'dropdown_placeholder' => __( 'Select an option', 'sureforms' ),
221 ],
222 ]
223 );
224
225 // Localizing the field preview image links.
226 wp_localize_script(
227 SRFM_SLUG . $all_screen_blocks,
228 SRFM_SLUG . '_fields_preview',
229 apply_filters(
230 'srfm_block_preview_images',
231 [
232 'input_preview' => SRFM_URL . 'images/field-previews/input.svg',
233 'email_preview' => SRFM_URL . 'images/field-previews/email.svg',
234 'url_preview' => SRFM_URL . 'images/field-previews/url.svg',
235 'textarea_preview' => SRFM_URL . 'images/field-previews/textarea.svg',
236 'multi_choice_preview' => SRFM_URL . 'images/field-previews/multi-choice.svg',
237 'checkbox_preview' => SRFM_URL . 'images/field-previews/checkbox.svg',
238 'number_preview' => SRFM_URL . 'images/field-previews/number.svg',
239 'phone_preview' => SRFM_URL . 'images/field-previews/phone.svg',
240 'dropdown_preview' => SRFM_URL . 'images/field-previews/dropdown.svg',
241 'address_preview' => SRFM_URL . 'images/field-previews/address.svg',
242 'sureforms_preview' => SRFM_URL . 'images/field-previews/sureforms.svg',
243 'payment_preview' => SRFM_URL . 'images/field-previews/payment.svg',
244 ]
245 )
246 );
247
248 wp_localize_script(
249 SRFM_SLUG . $all_screen_blocks,
250 SRFM_SLUG . '_blocks_info',
251 [
252 'font_awesome_5_polyfill' => [],
253 'collapse_panels' => 'enabled',
254 'is_site_editor' => $screen ? $screen->id : null,
255 ]
256 );
257 }
258
259 /**
260 * This function generates slug for sureforms blocks.
261 * Generates slug only if slug attribute of block is empty.
262 * Ensures that all sureforms blocks have unique slugs.
263 *
264 * @param int $post_id current sureforms form post id.
265 * @param \WP_Post $post SureForms post object.
266 * @since 0.0.2
267 * @return void
268 */
269 public function update_field_slug( $post_id, $post ) {
270 $blocks = parse_blocks( $post->post_content );
271
272 if ( empty( $blocks ) ) {
273 return;
274 }
275
276 $updated = false;
277
278 /**
279 * List of slugs already taken by processed blocks.
280 * used to maintain uniqueness of slugs.
281 */
282 $slugs = [];
283
284 [ $blocks, $slugs, $updated ] = Helper::process_blocks( $blocks, $slugs, $updated );
285
286 // Process and store block configurations for form fields.
287 Field_Validation::add_block_config( $blocks, $post_id );
288
289 if ( ! $updated ) {
290 return;
291 }
292
293 $post_content = serialize_blocks( $blocks );
294
295 // Use wp_slash() to preserve unicode escapes (like \u003c for <) in block attributes.
296 // Without this, wp_update_post() calls wp_unslash() which corrupts these escapes.
297 wp_update_post(
298 [
299 'ID' => $post_id,
300 'post_content' => wp_slash( $post_content ),
301 ]
302 );
303 }
304
305 /**
306 * Migrate the background type and associated values from
307 * instant form settings to form styling meta.
308 *
309 * @since 1.4.4
310 * @return void
311 */
312 public function maybe_migrate_form_stylings() {
313 $post_id = isset( $_GET['post'] ) ? Helper::get_integer_value( sanitize_text_field( wp_unslash( $_GET['post'] ) ) ) : ''; // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- $_GET['post'] does not provide nonce.
314 if ( empty( $post_id ) || ! Helper::current_user_can() ) {
315 return;
316 }
317
318 $post_type = get_post_type( $post_id );
319 if ( SRFM_FORMS_POST_TYPE !== $post_type ) {
320 return;
321 }
322
323 $instant_form_settings = get_post_meta( $post_id, '_srfm_instant_form_settings', true );
324 if ( ! is_array( $instant_form_settings ) ) {
325 return;
326 }
327
328 $form_styling = get_post_meta( $post_id, '_srfm_forms_styling', true );
329 if ( ! is_array( $form_styling ) ) {
330 $form_styling = [];
331 }
332
333 $migrated = false;
334 $keys = [ 'bg_type', 'bg_color', 'bg_image', 'bg_image_id' ];
335
336 foreach ( $keys as $key ) {
337 if ( ! isset( $form_styling[ $key ] ) && isset( $instant_form_settings[ $key ] ) ) {
338 $form_styling[ $key ] = $instant_form_settings[ $key ];
339 $migrated = true;
340 }
341 }
342 if ( $migrated ) {
343 update_post_meta( $post_id, '_srfm_forms_styling', $form_styling );
344 }
345 }
346 }
347