Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions classes/controllers/FrmAppController.php
Original file line number Diff line number Diff line change
Expand Up @@ -904,6 +904,11 @@ private static function enqueue_builder_assets( $plugin_url, $version ) {

wp_enqueue_style( 'formidable-settings-components' );
wp_enqueue_script( 'formidable-settings-components' );

if ( FrmAppHelper::needs_pro_builder_compat() ) {
// Pro versions released before these builder features moved to Pro still need Lite's copy.
wp_enqueue_script( 'formidable_admin_pro_compat', $plugin_url . '/js/formidable_admin_pro_compat.js', array( 'formidable_admin' ), $version, true );
}
}

/**
Expand Down
170 changes: 170 additions & 0 deletions classes/controllers/FrmFieldsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,176 @@ public static function load_field_settings() {
);
}

/**
* Render a field's builder preview with the unsaved settings from its panel.
*
* The posted settings are applied to a copy of the saved field. Nothing is saved.
*
* @since x.x
*
* @return void
*/
public static function refresh_field_preview() {
FrmAppHelper::permission_check( 'frm_edit_forms' );
check_ajax_referer( 'frm_ajax', 'nonce' );

$field_id = FrmAppHelper::get_post_param( 'field_id', 0, 'absint' );
$field = $field_id ? FrmField::getOne( $field_id ) : null;

if ( ! is_object( $field ) ) {
wp_send_json_error();
return;
}

// Work on a copy, so the cached field keeps its saved settings.
$field = clone $field;
$field->field_options = is_array( $field->field_options ) ? $field->field_options : array();
$form_id = FrmAppHelper::get_post_param( 'form_id', 0, 'absint' );
$field->parent_form_id = $form_id ? $form_id : $field->form_id;

self::apply_preview_settings( $field, self::get_preview_settings( $field ) );

global $frm_vars;
$frm_vars['is_admin'] = true;
$_GET['page'] = 'formidable';

ob_start();
FrmFieldFactory::get_field_object( $field )->show_on_form_builder();
$html = ob_get_clean() . '<div class="clear"></div>';

wp_send_json_success( array( 'html' => $html ) );
}

/**
* Get the posted settings for a field preview, keyed by setting name.
*
* @since x.x
*
* @param object $field The saved field.
*
* @return array
*/
private static function get_preview_settings( $field ) {
$suffix = '_' . $field->id;
$suffix_length = strlen( $suffix );
$posted = FrmAppHelper::get_post_param( 'field_options', array(), '' );
$settings = array();

if ( is_array( $posted ) ) {
foreach ( $posted as $name => $value ) {
$name = (string) $name;

if ( strlen( $name ) > $suffix_length && str_ends_with( $name, $suffix ) ) {
$settings[ substr( $name, 0, -$suffix_length ) ] = $value;
}
}
}

// The default value input is not named like the other settings.
$default_value = FrmAppHelper::get_post_param( 'default_value' . $suffix, null, '' );

if ( null !== $default_value ) {
$settings['default_value'] = $default_value;
}

/**
* Filters which posted settings a field preview refresh may use.
*
* @since x.x
*
* @param array|null $allowed_keys Setting names without the field ID suffix, like 'size'. Defaults to the field's saved
* settings and the default settings for its type. Return null to allow every posted setting.
* @param object $field The saved field.
*/
$allowed_keys = apply_filters( 'frm_refresh_field_preview_settings', self::get_default_preview_setting_keys( $field ), $field );

if ( is_array( $allowed_keys ) ) {
$settings = array_intersect_key( $settings, array_flip( array_filter( $allowed_keys, 'is_string' ) ) );
}

return self::sanitize_preview_settings( $settings );
}

/**
* Get the settings a field preview may use when no filter changes them.
*
* These are the field columns, the field's saved settings and the default settings for its type, including
* those added with the frm_default_field_opts filters. Anything else posted with the request is left out.
*
* @since x.x
*
* @param object $field The saved field.
*
* @return array Setting names without the field ID suffix.
*/
private static function get_default_preview_setting_keys( $field ) {
$columns = array( 'name', 'description', 'required', 'options', 'default_value' );
$defaults = FrmFieldsHelper::get_default_field_options_from_field( clone $field );
$keys = array_merge( $columns, array_keys( $field->field_options ), array_keys( (array) $defaults ) );

return array_values( array_unique( array_map( 'strval', $keys ) ) );
}

/**
* Sanitize preview settings the way field settings are sanitized on save.
*
* @since x.x
*
* @param array $settings The posted settings.
*
* @return array
*/
private static function sanitize_preview_settings( $settings ) {
foreach ( $settings as $key => $value ) {
if ( is_array( $value ) ) {
$settings[ $key ] = self::sanitize_preview_settings( $value );
} elseif ( is_scalar( $value ) ) {
$settings[ $key ] = trim( FrmAppHelper::kses( (string) $value, 'all' ) );
} else {
unset( $settings[ $key ] );
}
}

return $settings;
}

/**
* Apply preview settings to a copy of a field.
*
* @since x.x
*
* @param object $field A copy of the saved field.
* @param array $settings The sanitized settings, keyed by setting name.
*
* @return void
*/
private static function apply_preview_settings( $field, $settings ) {
// These are field columns rather than field options.
$columns = array( 'name', 'description', 'required', 'options', 'default_value' );
// These would change which field is rendered, or how, so they are never previewed.
$ignored = array( 'id', 'type', 'field_key', 'field_order', 'form_id' );

foreach ( $settings as $key => $value ) {
$key = (string) $key;

if ( in_array( $key, $ignored, true ) ) {
continue;
}

if ( ! in_array( $key, $columns, true ) ) {
$field->field_options[ $key ] = $value;
continue;
}

if ( 'options' === $key && is_array( $value ) ) {
// The template for new choices is not a choice.
unset( $value['000'] );
}

$field->{$key} = $value;
}
}

/**
* Omit definitions the browser has already received, including from the initial page.
*
Expand Down
1 change: 1 addition & 0 deletions classes/controllers/FrmHooksController.php
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,7 @@ public static function load_ajax_hooks() {
// Fields Controller.
add_action( 'wp_ajax_frm_load_field', 'FrmFieldsController::load_field' );
add_action( 'wp_ajax_frm_load_field_settings', 'FrmFieldsController::load_field_settings' );
add_action( 'wp_ajax_frm_refresh_field_preview', 'FrmFieldsController::refresh_field_preview' );
add_action( 'wp_ajax_frm_insert_field', 'FrmFieldsController::create' );
add_action( 'wp_ajax_frm_duplicate_field', 'FrmFieldsController::duplicate' );
add_action( 'wp_ajax_frm_delete_field', 'FrmFieldsController::destroy' );
Expand Down
64 changes: 64 additions & 0 deletions classes/helpers/FrmAppHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,23 @@

class FrmAppHelper {

/**
* Builder feature keys whose code moved from Lite to Pro.
*
* @since x.x
*
* @var string[]
*/
const PRO_BUILDER_FEATURES = array(
'calc_box',
'page_section_chrome',
'lookup_settings',
'repeater_settings',
'confirmation_field',
'field_logic_rows',
'logic_option_sync',
);

/**
* Version of the database we are moving to.
*
Expand Down Expand Up @@ -402,6 +419,41 @@ public static function pro_is_installed() {
return (bool) apply_filters( 'frm_pro_installed', false );
}

/**
* Get the builder features that Pro registers its own code for.
*
* @since x.x
*
* @return string[]
*/
public static function get_pro_builder_features() {
/**
* Lets Pro list the builder features it handles, so Lite skips its own copy.
*
* @since x.x
*
* @param string[] $features Builder feature keys, like 'calc_box'.
*/
$features = apply_filters( 'frm_pro_builder_features', array() );

return is_array( $features ) ? array_values( array_filter( $features, 'is_string' ) ) : array();
}

/**
* Check if Lite needs to load its copy of builder features for an older Pro version.
*
* @since x.x
*
* @return bool
*/
public static function needs_pro_builder_compat() {
if ( ! self::pro_is_installed() ) {
return false;
}

return (bool) array_diff( self::PRO_BUILDER_FEATURES, self::get_pro_builder_features() );
}

/**
* Check if the Pro plugin is installed, whether authorized or not.
*
Expand Down Expand Up @@ -442,6 +494,17 @@ public static function is_form_builder_page( $check_for_settings = true ) {
return self::is_admin_page( 'formidable' ) && in_array( $action, $check_actions, true );
}

/**
* Check if the current page is a form's settings page.
*
* @since x.x
*
* @return bool
*/
public static function is_form_settings_page() {
return self::is_admin_page( 'formidable' ) && 'settings' === self::simple_get( 'frm_action', 'sanitize_title' );
}

/**
* @return bool
*/
Expand Down Expand Up @@ -3935,6 +3998,7 @@ public static function load_admin_wide_js( $load = true ) {
'loading' => __( 'Loading&hellip;', 'formidable' ),
'nonce' => wp_create_nonce( 'frm_ajax' ),
'proIncludesSliderJs' => is_callable( 'FrmProFormsHelper::prepare_custom_currency' ),
'proBuilderFeatures' => self::get_pro_builder_features(),
'inboxSlideIn' => FrmInbox::get_inbox_slide_in_value_for_js(),
'utmMedium' => self::get_utm_medium(),
);
Expand Down
17 changes: 13 additions & 4 deletions classes/views/frm-fields/back-end/pixels-wide.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,22 @@
</label>

<?php
$field_size_attrs = array(
'id' => 'field_options_size_' . $field['id'],
'name' => 'field_options[size_' . $field['id'] . ']',
);

if ( 'textarea' === $field['type'] ) {
// Size the textarea preview as the setting changes. A number gets px, like FrmFieldTextarea::show_on_form_builder.
// Single line fields stay unsized in the builder on purpose.
$field_size_attrs['data-frmpreview'] = '#field_' . $field['id'] . '_inner_container .frm_form_fields textarea';
$field_size_attrs['data-frmpreview-mode'] = 'style:width:px';
}

FrmHtmlHelper::echo_unit_input(
array(
'value' => $field['size'],
'field_attrs' => array(
'id' => 'field_options_size_' . $field['id'],
'name' => 'field_options[size_' . $field['id'] . ']',
),
'field_attrs' => $field_size_attrs,
)
);
?>
Expand Down
6 changes: 6 additions & 0 deletions css/custom_theme.css.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,12 @@
display:none;
}

/* Not !important, so an inline display from jQuery .show() still wins. */
.frm_wrap [hidden],
.with_frm_style [hidden]{
display:none;
}

.with_frm_style,
.with_frm_style form,
.with_frm_style .frm-show-form div.frm_description p{
Expand Down
2 changes: 1 addition & 1 deletion css/frm_admin.css

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion css/frm_testing_mode.css

Large diffs are not rendered by default.

Loading
Loading