| 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( 'FrmSimpleBlocksController::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 |
'className' => array( |
| 109 |
'type' => 'string', |
| 110 |
), |
| 111 |
), |
| 112 |
'editor_style' => 'formidable', |
| 113 |
'editor_script' => 'formidable-form-selector', |
| 114 |
'render_callback' => 'FrmSimpleBlocksController::simple_form_render', |
| 115 |
|
| 116 |
) |
| 117 |
); |
| 118 |
} |
| 119 |
|
| 120 |
/** |
| 121 |
* Renders a form given the specified attributes. |
| 122 |
* |
| 123 |
* @param $attributes |
| 124 |
* |
| 125 |
* @return string |
| 126 |
*/ |
| 127 |
public static function simple_form_render( $attributes ) { |
| 128 |
if ( ! isset( $attributes['formId'] ) ) { |
| 129 |
return ''; |
| 130 |
} |
| 131 |
|
| 132 |
$params = array_filter( $attributes ); |
| 133 |
$params['id'] = $params['formId']; |
| 134 |
unset( $params['formId'] ); |
| 135 |
|
| 136 |
$form = FrmFormsController::get_form_shortcode( $params ); |
| 137 |
return str_replace( ' frm_logic_form ', ' ', $form ); // prevent the form from hiding |
| 138 |
} |
| 139 |
} |
| 140 |
|