Completed
Pull Request — master (#101)
by Stephanie
03:05
created

FrmSimpleBlocksController::simple_form_render()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 1
dl 0
loc 11
rs 9.9
c 0
b 0
f 0
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
		);
30
31
		wp_localize_script( 'formidable-form-selector', 'formidable_form_selector', $script_vars );
32
		if ( function_exists( 'wp_set_script_translations' ) ) {
33
			wp_set_script_translations( 'formidable-form-selector', 'formidable' );
34
		}
35
36
		wp_enqueue_style(
37
			'formidable_block-editor-css',
38
			FrmAppHelper::plugin_url() . '/css/frm_blocks.css',
39
			array( 'wp-edit-blocks' ),
40
			$version
41
		);
42
	}
43
44
	/**
45
	 * Returns a filtered list of form options with the name as label and the id as value, sorted by label
46
	 *
47
	 * @return array
48
	 */
49
	private static function get_forms_options() {
50
		$forms = FrmForm::getAll(
51
			array(
52
				'is_template' => 0,
53
				'status'      => 'published',
54
				array(
55
					'or'               => 1,
56
					'parent_form_id'   => null,
57
					'parent_form_id <' => 1,
58
				),
59
			),
60
			'name'
61
		);
62
63
		return array_map( 'self::set_form_options', $forms );
64
	}
65
66
	/**
67
	 * Returns an array for a form with name as label and id as value
68
	 *
69
	 * @param $form
70
	 *
71
	 * @return array
72
	 */
73
	private static function set_form_options( $form ) {
74
		return array(
75
			'label' => $form->name,
76
			'value' => $form->id,
77
		);
78
	}
79
80
	/**
81
	 * Registers simple form block
82
	 */
83
	public static function register_simple_form_block() {
84
		if ( ! is_callable( 'register_block_type' ) ) {
85
			return;
86
		}
87
88
		if ( is_admin() ) {
89
			FrmStylesController::enqueue_css( 'register', true );
90
		}
91
92
		register_block_type(
93
			'formidable/simple-form',
94
			array(
95
				'attributes'      => array(
96
					'formId'      => array(
97
						'type' => 'string',
98
					),
99
					'title'       => array(
100
						'type' => 'string',
101
					),
102
					'description' => array(
103
						'type' => 'string',
104
					),
105
					'minimize'    => array(
106
						'type' => 'string',
107
					),
108
				),
109
				'editor_style'    => 'formidable',
110
				'editor_script'   => 'formidable-form-selector',
111
				'render_callback' => 'FrmSimpleBlocksController::simple_form_render',
112
113
			)
114
		);
115
	}
116
117
	/**
118
	 * Renders a form given the specified attributes.
119
	 *
120
	 * @param $attributes
121
	 *
122
	 * @return string
123
	 */
124
	public static function simple_form_render( $attributes ) {
125
		if ( ! isset( $attributes['formId'] ) ) {
126
			return '';
127
		}
128
129
		$params       = array_filter( $attributes );
130
		$params['id'] = $params['formId'];
131
		unset( $params['formId'] );
132
133
		return FrmFormsController::get_form_shortcode( $params );
134
	}
135
}
136