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

143 lines 3.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 class FrmSimpleBlocksController {
4
5 /**
6 * Enqueue Formidable Simple Blocks' js and CSS for editor in admin.
7 */
8 public static function block_editor_assets() {
9 $version = FrmAppHelper::plugin_version();
10
11 wp_register_script(
12 'formidable-form-selector',
13 FrmAppHelper::plugin_url() . '/js/formidable_blocks.js',
14 array( 'wp-blocks', 'wp-i18n', 'wp-element', 'wp-components', 'wp-editor' ),
15 $version,
16 true
17 );
18
19 $icon = str_replace( 'dashicons-', '', apply_filters( 'frm_icon', 'svg' ) );
20 $block_name = FrmAppHelper::get_menu_name();
21 if ( $block_name === 'Formidable' ) {
22 $block_name = 'Formidable Forms';
23 }
24
25 $script_vars = array(
26 'forms' => self::get_forms_options(),
27 'icon' => $icon,
28 'name' => $block_name,
29 'link' => FrmAppHelper::admin_upgrade_link( 'block' ),
30 'url' => FrmAppHelper::plugin_url(),
31 );
32
33 wp_localize_script( 'formidable-form-selector', 'formidable_form_selector', $script_vars );
34 if ( function_exists( 'wp_set_script_translations' ) ) {
35 wp_set_script_translations( 'formidable-form-selector', 'formidable' );
36 }
37
38 wp_enqueue_style(
39 'formidable_block-editor-css',
40 FrmAppHelper::plugin_url() . '/css/frm_blocks.css',
41 array( 'wp-edit-blocks' ),
42 $version
43 );
44 }
45
46 /**
47 * Returns a filtered list of form options with the name as label and the id as value, sorted by label
48 *
49 * @return array
50 */
51 private static function get_forms_options() {
52 $forms = FrmForm::getAll(
53 array(
54 'is_template' => 0,
55 'status' => 'published',
56 array(
57 'or' => 1,
58 'parent_form_id' => null,
59 'parent_form_id <' => 1,
60 ),
61 ),
62 'name'
63 );
64
65 return array_map( 'FrmSimpleBlocksController::set_form_options', $forms );
66 }
67
68 /**
69 * Returns an array for a form with name as label and id as value
70 *
71 * @param $form
72 *
73 * @return array
74 */
75 private static function set_form_options( $form ) {
76 return array(
77 'label' => $form->name,
78 'value' => $form->id,
79 );
80 }
81
82 /**
83 * Registers simple form block
84 */
85 public static function register_simple_form_block() {
86 if ( ! is_callable( 'register_block_type' ) ) {
87 return;
88 }
89
90 if ( is_admin() ) {
91 FrmStylesController::enqueue_css( 'register', true );
92 }
93
94 register_block_type(
95 'formidable/simple-form',
96 array(
97 'attributes' => array(
98 'formId' => array(
99 'type' => 'string',
100 ),
101 'title' => array(
102 'type' => 'string',
103 ),
104 'description' => array(
105 'type' => 'string',
106 ),
107 'minimize' => array(
108 'type' => 'string',
109 ),
110 'className' => array(
111 'type' => 'string',
112 ),
113 ),
114 'editor_style' => 'formidable',
115 'editor_script' => 'formidable-form-selector',
116 'render_callback' => 'FrmSimpleBlocksController::simple_form_render',
117
118 )
119 );
120 }
121
122 /**
123 * Renders a form given the specified attributes.
124 *
125 * @param $attributes
126 *
127 * @return string
128 */
129 public static function simple_form_render( $attributes ) {
130 if ( ! isset( $attributes['formId'] ) ) {
131 return '';
132 }
133
134 $params = array_filter( $attributes );
135 $params['id'] = $params['formId'];
136 unset( $params['formId'] );
137
138 $form = FrmFormsController::get_form_shortcode( $params );
139
140 return str_replace( ' frm_logic_form ', ' ', $form ); // prevent the form from hiding
141 }
142 }
143