| @@ -21,9 +21,10 @@ | ||
| 21 | 21 | true |
| 22 | 22 | ); |
| 23 | 23 | |
| 24 | 24 | $icon = apply_filters( 'frm_icon', 'svg' ); |
| 25 | - if ( 0 === strpos( $icon, 'data:image/svg+xml;base64,' ) ) { | |
| 25 | + | |
| 26 | + if ( str_starts_with( $icon, 'data:image/svg+xml;base64,' ) ) { | |
| 26 | 27 | $icon = ' ' . FrmAppHelper::get_menu_icon_class(); |
| 27 | 28 | } else { |
| 28 | 29 | $icon = str_replace( 'dashicons-', '', $icon ); |
| 29 | 30 | } |
| @@ -28,25 +29,62 @@ | ||
| 28 | 29 | $icon = str_replace( 'dashicons-', '', $icon ); |
| 29 | 30 | } |
| 30 | 31 | |
| 31 | 32 | $block_name = FrmAppHelper::get_menu_name(); |
| 33 | + | |
| 32 | 34 | if ( $block_name === 'Formidable' ) { |
| 33 | 35 | $block_name = 'Formidable Forms'; |
| 34 | 36 | } |
| 35 | 37 | |
| 38 | + $modal_addon = self::get_addon_info( 185013 ); | |
| 39 | + $charts_addon = self::get_addon_info( 28248560 ); | |
| 40 | + $views_addon = FrmAddonsController::get_addon( 'views' ); | |
| 41 | + $views_addon_info = self::get_addon_info( 28027505 ); | |
| 42 | + | |
| 36 | 43 | $script_vars = array( |
| 37 | - 'forms' => self::get_forms_options(), | |
| 38 | - 'icon' => $icon, | |
| 39 | - 'name' => $block_name, | |
| 40 | - 'link' => FrmAppHelper::admin_upgrade_link( 'block' ), | |
| 41 | - 'url' => FrmAppHelper::plugin_url(), | |
| 44 | + 'forms' => self::get_forms_options(), | |
| 45 | + 'icon' => $icon, | |
| 46 | + 'name' => $block_name, | |
| 47 | + 'link' => FrmAppHelper::admin_upgrade_link( 'block' ), | |
| 48 | + 'url' => FrmAppHelper::plugin_url(), | |
| 49 | + 'modalAddon' => array( | |
| 50 | + 'link' => is_array( $modal_addon ) && isset( $modal_addon['link'] ) ? FrmAppHelper::admin_upgrade_link( 'block', $modal_addon['link'] ) : '', | |
| 51 | + 'hasAccess' => is_array( $modal_addon ) && ! empty( $modal_addon['url'] ), | |
| 52 | + ), | |
| 53 | + 'viewsAddon' => array( | |
| 54 | + 'link' => is_array( $views_addon_info ) && isset( $views_addon_info['link'] ) ? FrmAppHelper::admin_upgrade_link( 'block', $views_addon_info['link'] ) : '', | |
| 55 | + 'hasAccess' => is_array( $views_addon_info ) && ! empty( $views_addon_info['url'] ), | |
| 56 | + 'url' => ! empty( $views_addon_info['url'] ) ? $views_addon_info['url'] : '', | |
| 57 | + 'installed' => is_array( $views_addon ) && 'installed' === $views_addon['status']['type'], | |
| 58 | + ), | |
| 59 | + 'chartsAddon' => array( | |
| 60 | + 'link' => is_array( $charts_addon ) && isset( $charts_addon['link'] ) ? FrmAppHelper::admin_upgrade_link( 'block', $charts_addon['link'] ) : '', | |
| 61 | + 'hasAccess' => is_array( $charts_addon ) && ! empty( $charts_addon['url'] ), | |
| 62 | + 'installed' => class_exists( 'FrmChartsAppController' ), | |
| 63 | + ), | |
| 42 | 64 | ); |
| 43 | 65 | |
| 44 | 66 | wp_localize_script( 'formidable-form-selector', 'formidable_form_selector', $script_vars ); |
| 67 | + | |
| 45 | 68 | if ( function_exists( 'wp_set_script_translations' ) ) { |
| 46 | 69 | wp_set_script_translations( 'formidable-form-selector', 'formidable' ); |
| 47 | 70 | } |
| 71 | + } | |
| 48 | 72 | |
| 73 | + /** | |
| 74 | + * Enqueue Formidable Simple Blocks' JS and CSS for the content. | |
| 75 | + * | |
| 76 | + * @since 6.26 | |
| 77 | + * | |
| 78 | + * @return void | |
| 79 | + */ | |
| 80 | + public static function block_assets() { | |
| 81 | + if ( ! is_admin() ) { | |
| 82 | + return; | |
| 83 | + } | |
| 84 | + | |
| 85 | + $version = FrmAppHelper::plugin_version(); | |
| 86 | + | |
| 49 | 87 | wp_enqueue_style( |
| 50 | 88 | 'formidable_block-editor-css', |
| 51 | 89 | FrmAppHelper::plugin_url() . '/css/frm_blocks.css', |
| 52 | 90 | array( 'wp-edit-blocks' ), |
| @@ -54,8 +92,28 @@ | ||
| 54 | 92 | ); |
| 55 | 93 | } |
| 56 | 94 | |
| 57 | 95 | /** |
| 96 | + * Gets addon info. | |
| 97 | + * | |
| 98 | + * @since 6.8 | |
| 99 | + * | |
| 100 | + * @param int $addon_id Addon ID. | |
| 101 | + * | |
| 102 | + * @return array|false | |
| 103 | + */ | |
| 104 | + private static function get_addon_info( $addon_id ) { | |
| 105 | + $api = new FrmFormApi(); | |
| 106 | + $addons = $api->get_api_info(); | |
| 107 | + | |
| 108 | + if ( ! is_array( $addons ) || ! array_key_exists( $addon_id, $addons ) ) { | |
| 109 | + return false; | |
| 110 | + } | |
| 111 | + | |
| 112 | + return $addons[ $addon_id ]; | |
| 113 | + } | |
| 114 | + | |
| 115 | + /** | |
| 58 | 116 | * Returns a filtered list of form options with the name as label and the id as value, sorted by label |
| 59 | 117 | * |
| 60 | 118 | * @return array |
| 61 | 119 | */ |
| @@ -78,15 +136,15 @@ | ||
| 78 | 136 | |
| 79 | 137 | /** |
| 80 | 138 | * Returns an array for a form with name as label and id as value |
| 81 | 139 | * |
| 82 | - * @param $form | |
| 140 | + * @param object $form | |
| 83 | 141 | * |
| 84 | 142 | * @return array |
| 85 | 143 | */ |
| 86 | 144 | private static function set_form_options( $form ) { |
| 87 | 145 | return array( |
| 88 | - 'label' => $form->name, | |
| 146 | + 'label' => FrmFormsHelper::edit_form_link_label( $form ), | |
| 89 | 147 | 'value' => $form->id, |
| 90 | 148 | ); |
| 91 | 149 | } |
| 92 | 150 | |
| @@ -135,8 +193,9 @@ | ||
| 135 | 193 | /** |
| 136 | 194 | * Renders a form given the specified attributes. |
| 137 | 195 | * |
| 138 | 196 | * @param array $attributes |
| 197 | + * | |
| 139 | 198 | * @return string |
| 140 | 199 | */ |
| 141 | 200 | public static function simple_form_render( $attributes ) { |
| 142 | 201 | if ( ! isset( $attributes['formId'] ) ) { |
| @@ -142,19 +201,38 @@ | ||
| 142 | 201 | if ( ! isset( $attributes['formId'] ) ) { |
| 143 | 202 | return ''; |
| 144 | 203 | } |
| 145 | 204 | |
| 205 | + ob_start(); | |
| 206 | + | |
| 146 | 207 | /** |
| 147 | 208 | * @since 5.5.2 |
| 209 | + * | |
| 148 | 210 | * @param array $attributes |
| 149 | 211 | */ |
| 150 | 212 | do_action( 'frm_before_simple_form_render', $attributes ); |
| 151 | 213 | |
| 214 | + $form = ob_get_clean(); | |
| 215 | + | |
| 216 | + if ( false === $form ) { | |
| 217 | + $form = ''; | |
| 218 | + } | |
| 219 | + | |
| 152 | 220 | $params = array_filter( $attributes ); |
| 153 | 221 | $params['id'] = $params['formId']; |
| 154 | 222 | unset( $params['formId'] ); |
| 155 | 223 | |
| 156 | - $form = FrmFormsController::get_form_shortcode( $params ); | |
| 224 | + // Still pass false for title and description options if nothing is set, | |
| 225 | + // So a default doesn't overwrite the block option. | |
| 226 | + $params['title'] = ! empty( $params['title'] ); | |
| 227 | + $params['description'] = ! empty( $params['description'] ); | |
| 228 | + | |
| 229 | + $form .= FrmFormsController::get_form_shortcode( $params ); | |
| 230 | + | |
| 231 | + if ( ! empty( $attributes['className'] ) ) { | |
| 232 | + $form = preg_replace( '/\bfrm_forms\b/', 'frm_forms ' . esc_attr( $attributes['className'] ), $form, 1 ); | |
| 233 | + } | |
| 234 | + | |
| 157 | 235 | return self::maybe_remove_fade_on_load_for_block_preview( $form ); |
| 158 | 236 | } |
| 159 | 237 | |
| 160 | 238 | /** |
| @@ -161,13 +239,14 @@ | ||
| 161 | 239 | * Remove fade on load when /wp-json/wp/v2/block-renderer/formidable/simple-form is called. |
| 162 | 240 | * With the class set, the form never appears in the form block preview. |
| 163 | 241 | * |
| 164 | 242 | * @param string $form |
| 243 | + * | |
| 165 | 244 | * @return string |
| 166 | 245 | */ |
| 167 | 246 | private static function maybe_remove_fade_on_load_for_block_preview( $form ) { |
| 168 | 247 | if ( is_callable( 'wp_is_json_request' ) && wp_is_json_request() ) { |
| 169 | - $form = str_replace( ' frm_logic_form ', ' ', $form ); | |
| 248 | + return str_replace( ' frm_logic_form ', ' ', $form ); | |
| 170 | 249 | } |
| 171 | 250 | return $form; |
| 172 | 251 | } |
| 173 | 252 | } |