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

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