PluginProbe
SureForms – Contact Form Builder, AI Forms, Payment Form, Survey & Quiz / 0.0.12
SureForms – Contact Form Builder, AI Forms, Payment Form, Survey & Quiz v0.0.12
2.12.8 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 All 98 releases
sureforms / inc / gutenberg-hooks.php

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

326 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 * 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
195 // Enqueue the code editor for the Custom CSS Editor in SureForms.
196 wp_enqueue_code_editor( [ 'type' => 'text/css' ] );
197 wp_enqueue_script( 'wp-theme-plugin-editor' );
198 wp_enqueue_style( 'wp-codemirror' );
199
200 wp_localize_script(
201 SRFM_SLUG . $form_editor_script,
202 SRFM_SLUG . '_block_data',
203 [
204 'plugin_url' => SRFM_URL,
205 'admin_email' => get_option( 'admin_email' ),
206 ]
207 );
208 }
209
210 /**
211 * Register all editor scripts.
212 *
213 * @return void
214 * @since 0.0.1
215 */
216 public function block_editor_assets() {
217 $all_screen_blocks = '-blocks';
218 $screen = get_current_screen();
219
220 $blocks_asset_path = SRFM_DIR . 'assets/build/blocks.asset.php';
221 $blocks_info = file_exists( $blocks_asset_path )
222 ? include $blocks_asset_path
223 : [
224 'dependencies' => [],
225 'version' => SRFM_VER,
226 ];
227 wp_enqueue_script( SRFM_SLUG . $all_screen_blocks, SRFM_URL . 'assets/build/blocks.js', $blocks_info['dependencies'], SRFM_VER, true );
228
229 $plugin_path = 'sureforms-pro/sureforms-pro.php';
230
231 wp_localize_script(
232 SRFM_SLUG . $all_screen_blocks,
233 SRFM_SLUG . '_block_data',
234 [
235 'template_picker_url' => admin_url( '/admin.php?page=add-new-form' ),
236 'plugin_url' => SRFM_URL,
237 'admin_email' => get_option( 'admin_email' ),
238 'post_url' => admin_url( 'post.php' ),
239 'current_screen' => $screen,
240 'smart_tags_array' => Smart_Tags::smart_tag_list(),
241 'smart_tags_array_email' => Smart_Tags::email_smart_tag_list(),
242 'srfm_form_markup_nonce' => wp_create_nonce( 'srfm_form_markup' ),
243 'get_form_markup_url' => 'sureforms/v1/generate-form-markup',
244 'is_pro_active' => defined( 'SRFM_PRO_VER' ),
245 'get_default_dynamic_block_option' => get_option( 'get_default_dynamic_block_option', Helper::default_dynamic_block_option() ),
246 'form_selector_nonce' => current_user_can( 'edit_posts' ) ? wp_create_nonce( 'wp_rest' ) : '',
247 'is_admin_user' => current_user_can( 'manage_options' ),
248 ]
249 );
250
251 // Localizing the field preview image links.
252 wp_localize_script(
253 SRFM_SLUG . $all_screen_blocks,
254 SRFM_SLUG . '_fields_preview',
255 apply_filters(
256 'srfm_block_preview_images',
257 [
258 'input_preview' => SRFM_URL . 'images/field-previews/input.svg',
259 'email_preview' => SRFM_URL . 'images/field-previews/email.svg',
260 'url_preview' => SRFM_URL . 'images/field-previews/url.svg',
261 'textarea_preview' => SRFM_URL . 'images/field-previews/textarea.svg',
262 'multi_choice_preview' => SRFM_URL . 'images/field-previews/multi-choice.svg',
263 'checkbox_preview' => SRFM_URL . 'images/field-previews/checkbox.svg',
264 'number_preview' => SRFM_URL . 'images/field-previews/number.svg',
265 'phone_preview' => SRFM_URL . 'images/field-previews/phone.svg',
266 'dropdown_preview' => SRFM_URL . 'images/field-previews/dropdown.svg',
267 'address_preview' => SRFM_URL . 'images/field-previews/address.svg',
268 'sureforms_preview' => SRFM_URL . 'images/field-previews/sureforms.svg',
269 ]
270 )
271 );
272
273 wp_localize_script(
274 SRFM_SLUG . $all_screen_blocks,
275 SRFM_SLUG . '_blocks_info',
276 [
277 'font_awesome_5_polyfill' => [],
278 'collapse_panels' => 'enabled',
279 'is_site_editor' => $screen ? $screen->id : null,
280 ]
281 );
282 }
283
284 /**
285 * This function generates slug for sureforms blocks.
286 * Generates slug only if slug attribute of block is empty.
287 * Ensures that all sureforms blocks have unique slugs.
288 *
289 * @param int $post_id current sureforms form post id.
290 * @param \WP_Post $post SureForms post object.
291 * @since 0.0.2
292 * @return void
293 */
294 public function update_field_slug( $post_id, $post ) {
295 $blocks = parse_blocks( $post->post_content );
296
297 if ( empty( $blocks ) ) {
298 return;
299 }
300
301 $updated = false;
302
303 /**
304 * List of slugs already taken by processed blocks.
305 * used to maintain uniqueness of slugs.
306 */
307 $slugs = [];
308
309 list( $blocks, $slugs, $updated ) = Helper::process_blocks( $blocks, $slugs, $updated );
310
311 if ( ! $updated ) {
312 return;
313 }
314
315 $post_content = addslashes( serialize_blocks( $blocks ) );
316
317 wp_update_post(
318 [
319 'ID' => $post_id,
320 'post_content' => $post_content,
321 ]
322 );
323 }
324
325 }
326