PluginProbe
Formidable Forms – WordPress Form Builder for Contact Forms, Calculators, Quizzes & More / 6.34
Formidable Forms – WordPress Form Builder for Contact Forms, Calculators, Quizzes & More v6.34
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.34, at classes/controllers/FrmSimpleBlocksController.php

253 lines 6.5 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
26 if ( str_starts_with( $icon, 'data:image/svg+xml;base64,' ) ) {
27 $icon = ' ' . FrmAppHelper::get_menu_icon_class();
28 } else {
29 $icon = str_replace( 'dashicons-', '', $icon );
30 }
31
32 $block_name = FrmAppHelper::get_menu_name();
33
34 if ( $block_name === 'Formidable' ) {
35 $block_name = 'Formidable Forms';
36 }
37
38 $modal_addon = self::get_addon_info( 185013 );
39 $charts_addon = self::get_addon_info( 28248560 );
40 $views_addon = FrmAddonsController::get_addon( 'views' );
41 $views_addon_info = self::get_addon_info( 28027505 );
42
43 $script_vars = array(
44 'forms' => self::get_forms_options(),
45 'icon' => $icon,
46 'name' => $block_name,
47 'link' => FrmAppHelper::admin_upgrade_link( 'block' ),
48 'url' => FrmAppHelper::plugin_url(),
49 'modalAddon' => array(
50 'link' => is_array( $modal_addon ) && isset( $modal_addon['link'] ) ? FrmAppHelper::admin_upgrade_link( 'block', $modal_addon['link'] ) : '',
51 'hasAccess' => is_array( $modal_addon ) && ! empty( $modal_addon['url'] ),
52 ),
53 'viewsAddon' => array(
54 'link' => is_array( $views_addon_info ) && isset( $views_addon_info['link'] ) ? FrmAppHelper::admin_upgrade_link( 'block', $views_addon_info['link'] ) : '',
55 'hasAccess' => is_array( $views_addon_info ) && ! empty( $views_addon_info['url'] ),
56 'url' => ! empty( $views_addon_info['url'] ) ? $views_addon_info['url'] : '',
57 'installed' => is_array( $views_addon ) && 'installed' === $views_addon['status']['type'],
58 ),
59 'chartsAddon' => array(
60 'link' => is_array( $charts_addon ) && isset( $charts_addon['link'] ) ? FrmAppHelper::admin_upgrade_link( 'block', $charts_addon['link'] ) : '',
61 'hasAccess' => is_array( $charts_addon ) && ! empty( $charts_addon['url'] ),
62 'installed' => class_exists( 'FrmChartsAppController' ),
63 ),
64 );
65
66 wp_localize_script( 'formidable-form-selector', 'formidable_form_selector', $script_vars );
67
68 if ( function_exists( 'wp_set_script_translations' ) ) {
69 wp_set_script_translations( 'formidable-form-selector', 'formidable' );
70 }
71 }
72
73 /**
74 * Enqueue Formidable Simple Blocks' JS and CSS for the content.
75 *
76 * @since 6.26
77 *
78 * @return void
79 */
80 public static function block_assets() {
81 if ( ! is_admin() ) {
82 return;
83 }
84
85 $version = FrmAppHelper::plugin_version();
86
87 wp_enqueue_style(
88 'formidable_block-editor-css',
89 FrmAppHelper::plugin_url() . '/css/frm_blocks.css',
90 array( 'wp-edit-blocks' ),
91 $version
92 );
93 }
94
95 /**
96 * Gets addon info.
97 *
98 * @since 6.8
99 *
100 * @param int $addon_id Addon ID.
101 *
102 * @return array|false
103 */
104 private static function get_addon_info( $addon_id ) {
105 $api = new FrmFormApi();
106 $addons = $api->get_api_info();
107
108 if ( ! is_array( $addons ) || ! array_key_exists( $addon_id, $addons ) ) {
109 return false;
110 }
111
112 return $addons[ $addon_id ];
113 }
114
115 /**
116 * Returns a filtered list of form options with the name as label and the id as value, sorted by label
117 *
118 * @return array
119 */
120 private static function get_forms_options() {
121 $forms = FrmForm::getAll(
122 array(
123 'is_template' => 0,
124 'status' => 'published',
125 array(
126 'or' => 1,
127 'parent_form_id' => null,
128 'parent_form_id <' => 1,
129 ),
130 ),
131 'name'
132 );
133
134 return array_map( 'FrmSimpleBlocksController::set_form_options', $forms );
135 }
136
137 /**
138 * Returns an array for a form with name as label and id as value
139 *
140 * @param object $form
141 *
142 * @return array
143 */
144 private static function set_form_options( $form ) {
145 return array(
146 'label' => FrmFormsHelper::edit_form_link_label( $form ),
147 'value' => $form->id,
148 );
149 }
150
151 /**
152 * Registers simple form block
153 *
154 * @return void
155 */
156 public static function register_simple_form_block() {
157 if ( ! is_callable( 'register_block_type' ) ) {
158 return;
159 }
160
161 if ( is_admin() ) {
162 FrmStylesController::enqueue_css( 'register', true );
163 }
164
165 register_block_type(
166 'formidable/simple-form',
167 array(
168 'attributes' => array(
169 'formId' => array(
170 'type' => 'string',
171 ),
172 'title' => array(
173 'type' => 'string',
174 ),
175 'description' => array(
176 'type' => 'string',
177 ),
178 'minimize' => array(
179 'type' => 'string',
180 ),
181 'className' => array(
182 'type' => 'string',
183 ),
184 ),
185 'editor_style' => 'formidable',
186 'editor_script' => 'formidable-form-selector',
187 'render_callback' => 'FrmSimpleBlocksController::simple_form_render',
188
189 )
190 );
191 }
192
193 /**
194 * Renders a form given the specified attributes.
195 *
196 * @param array $attributes
197 *
198 * @return string
199 */
200 public static function simple_form_render( $attributes ) {
201 if ( ! isset( $attributes['formId'] ) ) {
202 return '';
203 }
204
205 ob_start();
206
207 /**
208 * @since 5.5.2
209 *
210 * @param array $attributes
211 */
212 do_action( 'frm_before_simple_form_render', $attributes );
213
214 $form = ob_get_clean();
215
216 if ( false === $form ) {
217 $form = '';
218 }
219
220 $params = array_filter( $attributes );
221 $params['id'] = $params['formId'];
222 unset( $params['formId'] );
223
224 // Still pass false for title and description options if nothing is set,
225 // So a default doesn't overwrite the block option.
226 $params['title'] = ! empty( $params['title'] );
227 $params['description'] = ! empty( $params['description'] );
228
229 $form .= FrmFormsController::get_form_shortcode( $params );
230
231 if ( ! empty( $attributes['className'] ) ) {
232 $form = preg_replace( '/\bfrm_forms\b/', 'frm_forms ' . esc_attr( $attributes['className'] ), $form, 1 );
233 }
234
235 return self::maybe_remove_fade_on_load_for_block_preview( $form );
236 }
237
238 /**
239 * Remove fade on load when /wp-json/wp/v2/block-renderer/formidable/simple-form is called.
240 * With the class set, the form never appears in the form block preview.
241 *
242 * @param string $form
243 *
244 * @return string
245 */
246 private static function maybe_remove_fade_on_load_for_block_preview( $form ) {
247 if ( is_callable( 'wp_is_json_request' ) && wp_is_json_request() ) {
248 return str_replace( ' frm_logic_form ', ' ', $form );
249 }
250 return $form;
251 }
252 }
253