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