PluginProbe
SureForms – Contact Form Builder, AI Forms, Payment Form, Survey & Quiz / 0.0.13
SureForms – Contact Form Builder, AI Forms, Payment Form, Survey & Quiz v0.0.13
2.12.7 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 All 97 releases
sureforms / inc / gutenberg-hooks.php

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

327 lines 9.5 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 Spec_Gb_Helper;
11 use SRFM\Inc\Traits\Get_Instance;
12 use SRFM\Inc\Smart_Tags;
13 use SRFM\Inc\Helper;
14
15 if ( ! defined( 'ABSPATH' ) ) {
16 exit; // Exit if accessed directly.
17 }
18
19 /**
20 * Gutenberg hooks handler class.
21 *
22 * @since 0.0.1
23 */
24 class Gutenberg_Hooks {
25
26 /**
27 * Block patterns to register.
28 *
29 * @var array<mixed>
30 */
31 protected $patterns = [];
32
33 use Get_Instance;
34
35 /**
36 * Class constructor.
37 *
38 * @return void
39 * @since 0.0.1
40 */
41 public function __construct() {
42 // Setting Form default patterns.
43 $this->patterns = [
44 'blank-form',
45 'contact-form',
46 'newsletter-form',
47 'support-form',
48 'feedback-form',
49 'event-rsvp-form',
50 'subscription-form',
51 ];
52
53 // Initializing hooks.
54 add_action( 'enqueue_block_editor_assets', [ $this, 'form_editor_screen_assets' ] );
55 add_action( 'enqueue_block_editor_assets', [ $this, 'block_editor_assets' ] );
56 add_filter( 'block_categories_all', [ $this, 'register_block_categories' ], 10, 1 );
57 add_action( 'init', [ $this, 'register_block_patterns' ], 9 );
58 add_filter( 'allowed_block_types_all', [ $this, 'disable_forms_wrapper_block' ], 10, 2 );
59 add_action( 'save_post_sureforms_form', [ $this, 'update_field_slug' ], 10, 2 );
60 }
61
62 /**
63 * Disable Sureforms_Form Block and allowed only sureforms block inside Sureform CPT editor.
64 *
65 * @param bool|string[] $allowed_block_types Array of block types.
66 * @param \WP_Block_Editor_Context $editor_context The current block editor context.
67 * @return array<mixed>|void
68 * @since 0.0.1
69 */
70 public function disable_forms_wrapper_block( $allowed_block_types, $editor_context ) {
71 if ( ! empty( $editor_context->post->post_type ) && 'sureforms_form' === $editor_context->post->post_type ) {
72 $allow_block_types = [
73 'srfm/input',
74 'srfm/email',
75 'srfm/textarea',
76 'srfm/number',
77 'srfm/checkbox',
78 'srfm/gdpr',
79 'srfm/phone',
80 'srfm/address',
81 'srfm/dropdown',
82 'srfm/multi-choice',
83 'srfm/url',
84 'srfm/separator',
85 'srfm/icon',
86 'srfm/image',
87 'srfm/advanced-heading',
88 'srfm/inline-button',
89 ];
90 // Apply a filter to the $allow_block_types types array.
91 $allow_block_types = apply_filters( 'srfm_allowed_block_types', $allow_block_types, $editor_context );
92 return $allow_block_types;
93 }
94 }
95
96 /**
97 * Register our custom block category.
98 *
99 * @param array<mixed> $categories Array of categories.
100 * @return array<mixed>
101 * @since 0.0.1
102 */
103 public function register_block_categories( $categories ) {
104 $screen = get_current_screen();
105
106 if ( $screen && SRFM_FORMS_POST_TYPE === $screen->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 /**
128 * Register our block patterns.
129 *
130 * @return void
131 * @since 0.0.1
132 */
133 public function register_block_patterns() {
134 // Apply filters to the patterns.
135 $this->patterns = apply_filters( 'srfm_block_patterns', $this->patterns );
136
137 // Iterate over each block pattern.
138 foreach ( $this->patterns as $block_pattern ) {
139 // Attempt to register block pattern from the main directory.
140 if ( ! $this->register_block_pattern_from_directory( $block_pattern, plugin_dir_path( SRFM_FILE ) . 'templates/forms/' ) ) {
141 // If unsuccessful, attempt to register block pattern from the pro directory.
142 if ( defined( 'SRFM_PRO_VER' ) && defined( 'SRFM_PRO_DIR' ) ) {
143 $this->register_block_pattern_from_directory( $block_pattern, SRFM_PRO_DIR . 'templates/forms/' );
144 }
145 }
146 }
147 }
148
149 /**
150 * Register block pattern from the specified directory.
151 *
152 * @param string|mixed $block_pattern The block pattern name.
153 * @param string $directory The directory path.
154 * @since 0.0.2
155 * @return bool True if the block pattern was registered, false otherwise.
156 */
157 private function register_block_pattern_from_directory( $block_pattern, $directory ) {
158 $pattern_file = $directory . $block_pattern . '.php';
159
160 if ( is_readable( $pattern_file ) ) {
161 register_block_pattern( 'srfm/' . $block_pattern, require $pattern_file );
162 return true;
163 }
164
165 return false;
166 }
167
168
169 /**
170 * Add Form Editor Scripts.
171 *
172 * @return void
173 * @since 0.0.1
174 */
175 public function form_editor_screen_assets() {
176 $form_editor_script = '-formEditor';
177
178 $screen = get_current_screen();
179 $post_types = [ SRFM_FORMS_POST_TYPE ];
180
181 if ( is_null( $screen ) || ! in_array( $screen->post_type, $post_types, true ) ) {
182 return;
183 }
184
185 $script_asset_path = SRFM_DIR . 'assets/build/formEditor.asset.php';
186 $script_info = file_exists( $script_asset_path )
187 ? include $script_asset_path
188 : [
189 'dependencies' => [],
190 'version' => SRFM_VER,
191 ];
192
193 wp_enqueue_script( SRFM_SLUG . $form_editor_script, SRFM_URL . 'assets/build/formEditor.js', $script_info['dependencies'], $script_info['version'], true );
194 wp_localize_script( SRFM_SLUG . $form_editor_script, 'scIcons', [ 'path' => SRFM_URL . 'assets/build/icon-assets' ] );
195
196 // Enqueue the code editor for the Custom CSS Editor in SureForms.
197 wp_enqueue_code_editor( [ 'type' => 'text/css' ] );
198 wp_enqueue_script( 'wp-theme-plugin-editor' );
199 wp_enqueue_style( 'wp-codemirror' );
200
201 wp_localize_script(
202 SRFM_SLUG . $form_editor_script,
203 SRFM_SLUG . '_block_data',
204 [
205 'plugin_url' => SRFM_URL,
206 'admin_email' => get_option( 'admin_email' ),
207 ]
208 );
209 }
210
211 /**
212 * Register all editor scripts.
213 *
214 * @return void
215 * @since 0.0.1
216 */
217 public function block_editor_assets() {
218 $all_screen_blocks = '-blocks';
219 $screen = get_current_screen();
220
221 $blocks_asset_path = SRFM_DIR . 'assets/build/blocks.asset.php';
222 $blocks_info = file_exists( $blocks_asset_path )
223 ? include $blocks_asset_path
224 : [
225 'dependencies' => [],
226 'version' => SRFM_VER,
227 ];
228 wp_enqueue_script( SRFM_SLUG . $all_screen_blocks, SRFM_URL . 'assets/build/blocks.js', $blocks_info['dependencies'], SRFM_VER, true );
229
230 $plugin_path = 'sureforms-pro/sureforms-pro.php';
231
232 wp_localize_script(
233 SRFM_SLUG . $all_screen_blocks,
234 SRFM_SLUG . '_block_data',
235 [
236 'template_picker_url' => admin_url( '/admin.php?page=add-new-form' ),
237 'plugin_url' => SRFM_URL,
238 'admin_email' => get_option( 'admin_email' ),
239 'post_url' => admin_url( 'post.php' ),
240 'current_screen' => $screen,
241 'smart_tags_array' => Smart_Tags::smart_tag_list(),
242 'smart_tags_array_email' => Smart_Tags::email_smart_tag_list(),
243 'srfm_form_markup_nonce' => wp_create_nonce( 'srfm_form_markup' ),
244 'get_form_markup_url' => 'sureforms/v1/generate-form-markup',
245 'is_pro_active' => defined( 'SRFM_PRO_VER' ),
246 'get_default_dynamic_block_option' => get_option( 'get_default_dynamic_block_option', Helper::default_dynamic_block_option() ),
247 'form_selector_nonce' => current_user_can( 'edit_posts' ) ? wp_create_nonce( 'wp_rest' ) : '',
248 'is_admin_user' => current_user_can( 'manage_options' ),
249 ]
250 );
251
252 // Localizing the field preview image links.
253 wp_localize_script(
254 SRFM_SLUG . $all_screen_blocks,
255 SRFM_SLUG . '_fields_preview',
256 apply_filters(
257 'srfm_block_preview_images',
258 [
259 'input_preview' => SRFM_URL . 'images/field-previews/input.svg',
260 'email_preview' => SRFM_URL . 'images/field-previews/email.svg',
261 'url_preview' => SRFM_URL . 'images/field-previews/url.svg',
262 'textarea_preview' => SRFM_URL . 'images/field-previews/textarea.svg',
263 'multi_choice_preview' => SRFM_URL . 'images/field-previews/multi-choice.svg',
264 'checkbox_preview' => SRFM_URL . 'images/field-previews/checkbox.svg',
265 'number_preview' => SRFM_URL . 'images/field-previews/number.svg',
266 'phone_preview' => SRFM_URL . 'images/field-previews/phone.svg',
267 'dropdown_preview' => SRFM_URL . 'images/field-previews/dropdown.svg',
268 'address_preview' => SRFM_URL . 'images/field-previews/address.svg',
269 'sureforms_preview' => SRFM_URL . 'images/field-previews/sureforms.svg',
270 ]
271 )
272 );
273
274 wp_localize_script(
275 SRFM_SLUG . $all_screen_blocks,
276 SRFM_SLUG . '_blocks_info',
277 [
278 'font_awesome_5_polyfill' => [],
279 'collapse_panels' => 'enabled',
280 'is_site_editor' => $screen ? $screen->id : null,
281 ]
282 );
283 }
284
285 /**
286 * This function generates slug for sureforms blocks.
287 * Generates slug only if slug attribute of block is empty.
288 * Ensures that all sureforms blocks have unique slugs.
289 *
290 * @param int $post_id current sureforms form post id.
291 * @param \WP_Post $post SureForms post object.
292 * @since 0.0.2
293 * @return void
294 */
295 public function update_field_slug( $post_id, $post ) {
296 $blocks = parse_blocks( $post->post_content );
297
298 if ( empty( $blocks ) ) {
299 return;
300 }
301
302 $updated = false;
303
304 /**
305 * List of slugs already taken by processed blocks.
306 * used to maintain uniqueness of slugs.
307 */
308 $slugs = [];
309
310 list( $blocks, $slugs, $updated ) = Helper::process_blocks( $blocks, $slugs, $updated );
311
312 if ( ! $updated ) {
313 return;
314 }
315
316 $post_content = addslashes( serialize_blocks( $blocks ) );
317
318 wp_update_post(
319 [
320 'ID' => $post_id,
321 'post_content' => $post_content,
322 ]
323 );
324 }
325
326 }
327