PluginProbe
SureForms – Contact Form Builder, AI Forms, Payment Form, Survey & Quiz / 2.1.0
SureForms – Contact Form Builder, AI Forms, Payment Form, Survey & Quiz v2.1.0
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 All 96 releases
sureforms / inc / gutenberg-hooks.php

gutenberg-hooks.php in SureForms – Contact Form Builder, AI Forms, Payment Form, Survey & Quiz 2.1.0, at inc/gutenberg-hooks.php

335 lines 10.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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, 1 );
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 * @return array<mixed>
99 * @since 0.0.1
100 */
101 public function register_block_categories( $categories ) {
102 $screen = get_current_screen();
103
104 if ( $screen && SRFM_FORMS_POST_TYPE === $screen->post_type ) {
105 $title = esc_html__( 'General Fields', 'sureforms' );
106 } else {
107 $title = esc_html__( 'SureForms', 'sureforms' );
108 }
109
110 $custom_categories = [
111 [
112 'slug' => 'sureforms',
113 'title' => $title,
114 ],
115 [
116 'slug' => 'sureforms-pro',
117 'title' => esc_html__( 'Advanced Fields', 'sureforms' ),
118 ],
119 ];
120
121 return array_merge( $custom_categories, $categories );
122 }
123
124 /**
125 * Add Form Editor Scripts.
126 *
127 * @return void
128 * @since 0.0.1
129 */
130 public function form_editor_screen_assets() {
131 $form_editor_script = '-formEditor';
132
133 $screen = get_current_screen();
134 $post_types = [ SRFM_FORMS_POST_TYPE ];
135
136 if ( is_null( $screen ) || ! in_array( $screen->post_type, $post_types, true ) ) {
137 return;
138 }
139
140 $script_asset_path = SRFM_DIR . 'assets/build/formEditor.asset.php';
141 $script_info = file_exists( $script_asset_path )
142 ? include $script_asset_path
143 : [
144 'dependencies' => [],
145 'version' => SRFM_VER,
146 ];
147
148 wp_enqueue_script( SRFM_SLUG . $form_editor_script, SRFM_URL . 'assets/build/formEditor.js', $script_info['dependencies'], $script_info['version'], true );
149 wp_localize_script( SRFM_SLUG . $form_editor_script, 'scIcons', [ 'path' => SRFM_URL . 'assets/build/icon-assets' ] );
150
151 // Enqueue the code editor for the Custom CSS Editor in SureForms.
152 wp_enqueue_code_editor( [ 'type' => 'text/css' ] );
153 wp_enqueue_script( 'wp-theme-plugin-editor' );
154 wp_enqueue_style( 'wp-codemirror' );
155
156 wp_localize_script(
157 SRFM_SLUG . $form_editor_script,
158 SRFM_SLUG . '_block_data',
159 [
160 'plugin_url' => SRFM_URL,
161 'admin_email' => get_option( 'admin_email' ),
162 'pro_plugin_name' => defined( 'SRFM_PRO_VER' ) && defined( 'SRFM_PRO_PRODUCT' ) ? SRFM_PRO_PRODUCT : 'free',
163 ]
164 );
165
166 Helper::register_script_translations( SRFM_SLUG . $form_editor_script );
167 }
168
169 /**
170 * Register all editor scripts.
171 *
172 * @return void
173 * @since 0.0.1
174 */
175 public function block_editor_assets() {
176 $all_screen_blocks = '-blocks';
177 $screen = get_current_screen();
178
179 $blocks_asset_path = SRFM_DIR . 'assets/build/blocks.asset.php';
180 $blocks_info = file_exists( $blocks_asset_path )
181 ? include $blocks_asset_path
182 : [
183 'dependencies' => [],
184 'version' => SRFM_VER,
185 ];
186 wp_enqueue_script( SRFM_SLUG . $all_screen_blocks, SRFM_URL . 'assets/build/blocks.js', $blocks_info['dependencies'], SRFM_VER, true );
187
188 Helper::register_script_translations( SRFM_SLUG . $all_screen_blocks );
189
190 $site_url = Helper::get_string_value( wp_parse_url( esc_url( get_site_url() ), PHP_URL_HOST ) );
191 $site_url = preg_replace( '/^www\./', '', $site_url );
192
193 wp_localize_script(
194 SRFM_SLUG . $all_screen_blocks,
195 SRFM_SLUG . '_block_data',
196 [
197 'template_picker_url' => admin_url( '/admin.php?page=add-new-form' ),
198 'plugin_url' => SRFM_URL,
199 'admin_email' => get_option( 'admin_email' ),
200 'post_url' => admin_url( 'post.php' ),
201 'current_screen' => $screen,
202 'smart_tags_array' => Smart_Tags::smart_tag_list(),
203 'smart_tags_array_email' => Smart_Tags::email_smart_tag_list(),
204 'srfm_form_markup_nonce' => wp_create_nonce( 'srfm_form_markup' ),
205 'get_form_markup_url' => 'sureforms/v1/generate-form-markup',
206 'is_pro_active' => Helper::has_pro(),
207 'srfm_default_dynamic_block_option' => get_option( 'srfm_default_dynamic_block_option', Helper::default_dynamic_block_option() ),
208 'form_selector_nonce' => Helper::current_user_can( 'edit_posts' ) ? wp_create_nonce( 'wp_rest' ) : '',
209 'is_admin_user' => Helper::current_user_can(),
210 'site_url' => $site_url,
211 'is_suremails_active' => is_plugin_active( 'suremails/suremails.php' ),
212 ]
213 );
214
215 // Localizing the field preview image links.
216 wp_localize_script(
217 SRFM_SLUG . $all_screen_blocks,
218 SRFM_SLUG . '_fields_preview',
219 apply_filters(
220 'srfm_block_preview_images',
221 [
222 'input_preview' => SRFM_URL . 'images/field-previews/input.svg',
223 'email_preview' => SRFM_URL . 'images/field-previews/email.svg',
224 'url_preview' => SRFM_URL . 'images/field-previews/url.svg',
225 'textarea_preview' => SRFM_URL . 'images/field-previews/textarea.svg',
226 'multi_choice_preview' => SRFM_URL . 'images/field-previews/multi-choice.svg',
227 'checkbox_preview' => SRFM_URL . 'images/field-previews/checkbox.svg',
228 'number_preview' => SRFM_URL . 'images/field-previews/number.svg',
229 'phone_preview' => SRFM_URL . 'images/field-previews/phone.svg',
230 'dropdown_preview' => SRFM_URL . 'images/field-previews/dropdown.svg',
231 'address_preview' => SRFM_URL . 'images/field-previews/address.svg',
232 'sureforms_preview' => SRFM_URL . 'images/field-previews/sureforms.svg',
233 'payment_preview' => SRFM_URL . 'images/field-previews/payment.svg',
234 ]
235 )
236 );
237
238 wp_localize_script(
239 SRFM_SLUG . $all_screen_blocks,
240 SRFM_SLUG . '_blocks_info',
241 [
242 'font_awesome_5_polyfill' => [],
243 'collapse_panels' => 'enabled',
244 'is_site_editor' => $screen ? $screen->id : null,
245 ]
246 );
247 }
248
249 /**
250 * This function generates slug for sureforms blocks.
251 * Generates slug only if slug attribute of block is empty.
252 * Ensures that all sureforms blocks have unique slugs.
253 *
254 * @param int $post_id current sureforms form post id.
255 * @param \WP_Post $post SureForms post object.
256 * @since 0.0.2
257 * @return void
258 */
259 public function update_field_slug( $post_id, $post ) {
260 $blocks = parse_blocks( $post->post_content );
261
262 if ( empty( $blocks ) ) {
263 return;
264 }
265
266 $updated = false;
267
268 /**
269 * List of slugs already taken by processed blocks.
270 * used to maintain uniqueness of slugs.
271 */
272 $slugs = [];
273
274 [ $blocks, $slugs, $updated ] = Helper::process_blocks( $blocks, $slugs, $updated );
275
276 // Process and store block configurations for form fields.
277 Field_Validation::add_block_config( $blocks, $post_id );
278
279 if ( ! $updated ) {
280 return;
281 }
282
283 $post_content = addslashes( serialize_blocks( $blocks ) );
284
285 wp_update_post(
286 [
287 'ID' => $post_id,
288 'post_content' => $post_content,
289 ]
290 );
291 }
292
293 /**
294 * Migrate the background type and associated values from
295 * instant form settings to form styling meta.
296 *
297 * @since 1.4.4
298 * @return void
299 */
300 public function maybe_migrate_form_stylings() {
301 $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.
302 if ( empty( $post_id ) || ! Helper::current_user_can() ) {
303 return;
304 }
305
306 $post_type = get_post_type( $post_id );
307 if ( SRFM_FORMS_POST_TYPE !== $post_type ) {
308 return;
309 }
310
311 $instant_form_settings = get_post_meta( $post_id, '_srfm_instant_form_settings', true );
312 if ( ! is_array( $instant_form_settings ) ) {
313 return;
314 }
315
316 $form_styling = get_post_meta( $post_id, '_srfm_forms_styling', true );
317 if ( ! is_array( $form_styling ) ) {
318 $form_styling = [];
319 }
320
321 $migrated = false;
322 $keys = [ 'bg_type', 'bg_color', 'bg_image', 'bg_image_id' ];
323
324 foreach ( $keys as $key ) {
325 if ( ! isset( $form_styling[ $key ] ) && isset( $instant_form_settings[ $key ] ) ) {
326 $form_styling[ $key ] = $instant_form_settings[ $key ];
327 $migrated = true;
328 }
329 }
330 if ( $migrated ) {
331 update_post_meta( $post_id, '_srfm_forms_styling', $form_styling );
332 }
333 }
334 }
335