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

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