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