PluginProbe
SureForms – Contact Form Builder, AI Forms, Payment Form, Survey & Quiz / 1.6.2
SureForms – Contact Form Builder, AI Forms, Payment Form, Survey & Quiz v1.6.2
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
369 lines 11.3 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_action( 'init', [ $this, 'register_block_patterns' ], 9 );
53 add_filter( 'allowed_block_types_all', [ $this, 'disable_forms_wrapper_block' ], 10, 2 );
54 add_action( 'save_post_sureforms_form', [ $this, 'update_field_slug' ], 10, 2 );
55 add_action( 'load-post.php', [ $this, 'maybe_migrate_form_stylings' ] );
56 }
57
58 /**
59 * Disable Sureforms_Form Block and allowed only sureforms block inside Sureform CPT editor.
60 *
61 * @param bool|array<string> $allowed_block_types Array of block types.
62 * @param \WP_Block_Editor_Context $editor_context The current block editor context.
63 * @return array<mixed>|bool
64 * @since 0.0.1
65 */
66 public function disable_forms_wrapper_block( $allowed_block_types, $editor_context ) {
67 if ( ! empty( $editor_context->post->post_type ) && 'sureforms_form' === $editor_context->post->post_type ) {
68 $allow_block_types = [
69 'srfm/input',
70 'srfm/email',
71 'srfm/textarea',
72 'srfm/number',
73 'srfm/checkbox',
74 'srfm/gdpr',
75 'srfm/phone',
76 'srfm/address',
77 'srfm/dropdown',
78 'srfm/multi-choice',
79 'srfm/url',
80 'srfm/separator',
81 'srfm/icon',
82 'srfm/image',
83 'srfm/advanced-heading',
84 'srfm/inline-button',
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 * Register our block patterns.
126 *
127 * @return void
128 * @since 0.0.1
129 */
130 public function register_block_patterns() {
131 // Apply filters to the patterns.
132 $this->patterns = apply_filters( 'srfm_block_patterns', $this->patterns );
133
134 // Iterate over each block pattern.
135 foreach ( $this->patterns as $block_pattern ) {
136 // Attempt to register block pattern from the main directory.
137 if ( ! $this->register_block_pattern_from_directory( $block_pattern, plugin_dir_path( SRFM_FILE ) . 'templates/forms/' ) ) {
138 // If unsuccessful, attempt to register block pattern from the pro directory.
139 if ( defined( 'SRFM_PRO_VER' ) && defined( 'SRFM_PRO_DIR' ) ) {
140 $this->register_block_pattern_from_directory( $block_pattern, SRFM_PRO_DIR . 'templates/forms/' );
141 }
142 }
143 }
144 }
145
146 /**
147 * Add Form Editor Scripts.
148 *
149 * @return void
150 * @since 0.0.1
151 */
152 public function form_editor_screen_assets() {
153 $form_editor_script = '-formEditor';
154
155 $screen = get_current_screen();
156 $post_types = [ SRFM_FORMS_POST_TYPE ];
157
158 if ( is_null( $screen ) || ! in_array( $screen->post_type, $post_types, true ) ) {
159 return;
160 }
161
162 $script_asset_path = SRFM_DIR . 'assets/build/formEditor.asset.php';
163 $script_info = file_exists( $script_asset_path )
164 ? include $script_asset_path
165 : [
166 'dependencies' => [],
167 'version' => SRFM_VER,
168 ];
169
170 wp_enqueue_script( SRFM_SLUG . $form_editor_script, SRFM_URL . 'assets/build/formEditor.js', $script_info['dependencies'], $script_info['version'], true );
171 wp_localize_script( SRFM_SLUG . $form_editor_script, 'scIcons', [ 'path' => SRFM_URL . 'assets/build/icon-assets' ] );
172
173 // Enqueue the code editor for the Custom CSS Editor in SureForms.
174 wp_enqueue_code_editor( [ 'type' => 'text/css' ] );
175 wp_enqueue_script( 'wp-theme-plugin-editor' );
176 wp_enqueue_style( 'wp-codemirror' );
177
178 wp_localize_script(
179 SRFM_SLUG . $form_editor_script,
180 SRFM_SLUG . '_block_data',
181 [
182 'plugin_url' => SRFM_URL,
183 'admin_email' => get_option( 'admin_email' ),
184 'pro_plugin_name' => defined( 'SRFM_PRO_VER' ) && defined( 'SRFM_PRO_PRODUCT' ) ? SRFM_PRO_PRODUCT : 'free',
185 ]
186 );
187
188 Helper::register_script_translations( SRFM_SLUG . $form_editor_script );
189 }
190
191 /**
192 * Register all editor scripts.
193 *
194 * @return void
195 * @since 0.0.1
196 */
197 public function block_editor_assets() {
198 $all_screen_blocks = '-blocks';
199 $screen = get_current_screen();
200
201 $blocks_asset_path = SRFM_DIR . 'assets/build/blocks.asset.php';
202 $blocks_info = file_exists( $blocks_asset_path )
203 ? include $blocks_asset_path
204 : [
205 'dependencies' => [],
206 'version' => SRFM_VER,
207 ];
208 wp_enqueue_script( SRFM_SLUG . $all_screen_blocks, SRFM_URL . 'assets/build/blocks.js', $blocks_info['dependencies'], SRFM_VER, true );
209
210 Helper::register_script_translations( SRFM_SLUG . $all_screen_blocks );
211
212 wp_localize_script(
213 SRFM_SLUG . $all_screen_blocks,
214 SRFM_SLUG . '_block_data',
215 [
216 'template_picker_url' => admin_url( '/admin.php?page=add-new-form' ),
217 'plugin_url' => SRFM_URL,
218 'admin_email' => get_option( 'admin_email' ),
219 'post_url' => admin_url( 'post.php' ),
220 'current_screen' => $screen,
221 'smart_tags_array' => Smart_Tags::smart_tag_list(),
222 'smart_tags_array_email' => Smart_Tags::email_smart_tag_list(),
223 'srfm_form_markup_nonce' => wp_create_nonce( 'srfm_form_markup' ),
224 'get_form_markup_url' => 'sureforms/v1/generate-form-markup',
225 'is_pro_active' => defined( 'SRFM_PRO_VER' ),
226 'srfm_default_dynamic_block_option' => get_option( 'srfm_default_dynamic_block_option', Helper::default_dynamic_block_option() ),
227 'form_selector_nonce' => current_user_can( 'edit_posts' ) ? wp_create_nonce( 'wp_rest' ) : '',
228 'is_admin_user' => current_user_can( 'manage_options' ),
229 'site_url' => wp_parse_url( esc_url( get_site_url() ), PHP_URL_HOST ),
230 'is_suremails_active' => is_plugin_active( 'suremails/suremails.php' ),
231 ]
232 );
233
234 // Localizing the field preview image links.
235 wp_localize_script(
236 SRFM_SLUG . $all_screen_blocks,
237 SRFM_SLUG . '_fields_preview',
238 apply_filters(
239 'srfm_block_preview_images',
240 [
241 'input_preview' => SRFM_URL . 'images/field-previews/input.svg',
242 'email_preview' => SRFM_URL . 'images/field-previews/email.svg',
243 'url_preview' => SRFM_URL . 'images/field-previews/url.svg',
244 'textarea_preview' => SRFM_URL . 'images/field-previews/textarea.svg',
245 'multi_choice_preview' => SRFM_URL . 'images/field-previews/multi-choice.svg',
246 'checkbox_preview' => SRFM_URL . 'images/field-previews/checkbox.svg',
247 'number_preview' => SRFM_URL . 'images/field-previews/number.svg',
248 'phone_preview' => SRFM_URL . 'images/field-previews/phone.svg',
249 'dropdown_preview' => SRFM_URL . 'images/field-previews/dropdown.svg',
250 'address_preview' => SRFM_URL . 'images/field-previews/address.svg',
251 'sureforms_preview' => SRFM_URL . 'images/field-previews/sureforms.svg',
252 ]
253 )
254 );
255
256 wp_localize_script(
257 SRFM_SLUG . $all_screen_blocks,
258 SRFM_SLUG . '_blocks_info',
259 [
260 'font_awesome_5_polyfill' => [],
261 'collapse_panels' => 'enabled',
262 'is_site_editor' => $screen ? $screen->id : null,
263 ]
264 );
265 }
266
267 /**
268 * This function generates slug for sureforms blocks.
269 * Generates slug only if slug attribute of block is empty.
270 * Ensures that all sureforms blocks have unique slugs.
271 *
272 * @param int $post_id current sureforms form post id.
273 * @param \WP_Post $post SureForms post object.
274 * @since 0.0.2
275 * @return void
276 */
277 public function update_field_slug( $post_id, $post ) {
278 $blocks = parse_blocks( $post->post_content );
279
280 if ( empty( $blocks ) ) {
281 return;
282 }
283
284 $updated = false;
285
286 /**
287 * List of slugs already taken by processed blocks.
288 * used to maintain uniqueness of slugs.
289 */
290 $slugs = [];
291
292 [ $blocks, $slugs, $updated ] = Helper::process_blocks( $blocks, $slugs, $updated );
293
294 if ( ! $updated ) {
295 return;
296 }
297
298 $post_content = addslashes( serialize_blocks( $blocks ) );
299
300 wp_update_post(
301 [
302 'ID' => $post_id,
303 'post_content' => $post_content,
304 ]
305 );
306 }
307
308 /**
309 * Migrate the background type and associated values from
310 * instant form settings to form styling meta.
311 *
312 * @since 1.4.4
313 * @return void
314 */
315 public function maybe_migrate_form_stylings() {
316 $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.
317 if ( empty( $post_id ) || ! current_user_can( 'edit_post', $post_id ) ) {
318 return;
319 }
320
321 $post_type = get_post_type( $post_id );
322 if ( SRFM_FORMS_POST_TYPE !== $post_type ) {
323 return;
324 }
325
326 $instant_form_settings = get_post_meta( $post_id, '_srfm_instant_form_settings', true );
327 if ( ! is_array( $instant_form_settings ) ) {
328 return;
329 }
330
331 $form_styling = get_post_meta( $post_id, '_srfm_forms_styling', true );
332 if ( ! is_array( $form_styling ) ) {
333 $form_styling = [];
334 }
335
336 $migrated = false;
337 $keys = [ 'bg_type', 'bg_color', 'bg_image', 'bg_image_id' ];
338
339 foreach ( $keys as $key ) {
340 if ( ! isset( $form_styling[ $key ] ) && isset( $instant_form_settings[ $key ] ) ) {
341 $form_styling[ $key ] = $instant_form_settings[ $key ];
342 $migrated = true;
343 }
344 }
345 if ( $migrated ) {
346 update_post_meta( $post_id, '_srfm_forms_styling', $form_styling );
347 }
348 }
349
350 /**
351 * Register block pattern from the specified directory.
352 *
353 * @param string|mixed $block_pattern The block pattern name.
354 * @param string $directory The directory path.
355 * @since 0.0.2
356 * @return bool True if the block pattern was registered, false otherwise.
357 */
358 private function register_block_pattern_from_directory( $block_pattern, $directory ) {
359 $pattern_file = $directory . $block_pattern . '.php';
360
361 if ( is_readable( $pattern_file ) ) {
362 register_block_pattern( 'srfm/' . $block_pattern, require $pattern_file );
363 return true;
364 }
365
366 return false;
367 }
368 }
369