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

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