PluginProbe
Formidable Forms – WordPress Form Builder for Contact Forms, Calculators, Quizzes & More / 6.3
Formidable Forms – WordPress Form Builder for Contact Forms, Calculators, Quizzes & More v6.3
6.35 6.34 6.33.1 6.33 6.32.1 6.32 6.31 6.25 6.25.1 6.26 6.26.1 6.27 6.28 6.29 6.3 6.3.1 6.3.2 6.30 6.4 6.4.1 6.4.2 6.5 6.5.1 6.5.2 6.5.3 All 141 releases
formidable / classes / controllers / FrmSimpleBlocksController.php

FrmSimpleBlocksController.php in Formidable Forms – WordPress Form Builder for Contact Forms, Calculators, Quizzes & More 6.3, at classes/controllers/FrmSimpleBlocksController.php

174 lines 4.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 if ( ! defined( 'ABSPATH' ) ) {
3 die( 'You are not allowed to call this page directly.' );
4 }
5
6 class FrmSimpleBlocksController {
7
8 /**
9 * Enqueue Formidable Simple Blocks' js and CSS for editor in admin.
10 *
11 * @return void
12 */
13 public static function block_editor_assets() {
14 $version = FrmAppHelper::plugin_version();
15
16 wp_register_script(
17 'formidable-form-selector',
18 FrmAppHelper::plugin_url() . '/js/formidable_blocks.js',
19 array( 'wp-blocks', 'wp-i18n', 'wp-element', 'wp-components', 'wp-block-editor' ),
20 $version,
21 true
22 );
23
24 $icon = apply_filters( 'frm_icon', 'svg' );
25 if ( 0 === strpos( $icon, 'data:image/svg+xml;base64,' ) ) {
26 $icon = ' ' . FrmAppHelper::get_menu_icon_class();
27 } else {
28 $icon = str_replace( 'dashicons-', '', $icon );
29 }
30
31 $block_name = FrmAppHelper::get_menu_name();
32 if ( $block_name === 'Formidable' ) {
33 $block_name = 'Formidable Forms';
34 }
35
36 $script_vars = array(
37 'forms' => self::get_forms_options(),
38 'icon' => $icon,
39 'name' => $block_name,
40 'link' => FrmAppHelper::admin_upgrade_link( 'block' ),
41 'url' => FrmAppHelper::plugin_url(),
42 );
43
44 wp_localize_script( 'formidable-form-selector', 'formidable_form_selector', $script_vars );
45 if ( function_exists( 'wp_set_script_translations' ) ) {
46 wp_set_script_translations( 'formidable-form-selector', 'formidable' );
47 }
48
49 wp_enqueue_style(
50 'formidable_block-editor-css',
51 FrmAppHelper::plugin_url() . '/css/frm_blocks.css',
52 array( 'wp-edit-blocks' ),
53 $version
54 );
55 }
56
57 /**
58 * Returns a filtered list of form options with the name as label and the id as value, sorted by label
59 *
60 * @return array
61 */
62 private static function get_forms_options() {
63 $forms = FrmForm::getAll(
64 array(
65 'is_template' => 0,
66 'status' => 'published',
67 array(
68 'or' => 1,
69 'parent_form_id' => null,
70 'parent_form_id <' => 1,
71 ),
72 ),
73 'name'
74 );
75
76 return array_map( 'FrmSimpleBlocksController::set_form_options', $forms );
77 }
78
79 /**
80 * Returns an array for a form with name as label and id as value
81 *
82 * @param $form
83 *
84 * @return array
85 */
86 private static function set_form_options( $form ) {
87 return array(
88 'label' => $form->name,
89 'value' => $form->id,
90 );
91 }
92
93 /**
94 * Registers simple form block
95 *
96 * @return void
97 */
98 public static function register_simple_form_block() {
99 if ( ! is_callable( 'register_block_type' ) ) {
100 return;
101 }
102
103 if ( is_admin() ) {
104 FrmStylesController::enqueue_css( 'register', true );
105 }
106
107 register_block_type(
108 'formidable/simple-form',
109 array(
110 'attributes' => array(
111 'formId' => array(
112 'type' => 'string',
113 ),
114 'title' => array(
115 'type' => 'string',
116 ),
117 'description' => array(
118 'type' => 'string',
119 ),
120 'minimize' => array(
121 'type' => 'string',
122 ),
123 'className' => array(
124 'type' => 'string',
125 ),
126 ),
127 'editor_style' => 'formidable',
128 'editor_script' => 'formidable-form-selector',
129 'render_callback' => 'FrmSimpleBlocksController::simple_form_render',
130
131 )
132 );
133 }
134
135 /**
136 * Renders a form given the specified attributes.
137 *
138 * @param array $attributes
139 * @return string
140 */
141 public static function simple_form_render( $attributes ) {
142 if ( ! isset( $attributes['formId'] ) ) {
143 return '';
144 }
145
146 /**
147 * @since 5.5.2
148 * @param array $attributes
149 */
150 do_action( 'frm_before_simple_form_render', $attributes );
151
152 $params = array_filter( $attributes );
153 $params['id'] = $params['formId'];
154 unset( $params['formId'] );
155
156 $form = FrmFormsController::get_form_shortcode( $params );
157 return self::maybe_remove_fade_on_load_for_block_preview( $form );
158 }
159
160 /**
161 * Remove fade on load when /wp-json/wp/v2/block-renderer/formidable/simple-form is called.
162 * With the class set, the form never appears in the form block preview.
163 *
164 * @param string $form
165 * @return string
166 */
167 private static function maybe_remove_fade_on_load_for_block_preview( $form ) {
168 if ( is_callable( 'wp_is_json_request' ) && wp_is_json_request() ) {
169 $form = str_replace( ' frm_logic_form ', ' ', $form );
170 }
171 return $form;
172 }
173 }
174