Skip to content
Open
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
12 changes: 10 additions & 2 deletions bin/build-plugin.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ set -e

# Change to the expected directory.
cd "$(dirname "$0")"
lite_dir="$(cd .. && pwd)"
plugin="$1"
cd ../../$plugin

Expand Down Expand Up @@ -63,8 +64,15 @@ fi
status "Installing dependencies..."
npm install
status "Generating build..."
# Skip the webpack cache so the release is built from scratch.
npm run build -- --no-cache
# Drop the webpack cache so the release is built from scratch. Every add-on runs this script,
# and not every build script accepts a --no-cache flag, so the cache is removed instead.
rm -rf node_modules/.cache/webpack
npm run build
# Add-ons minimize with Lite's closure compiler, so make sure Lite has its dependencies.
if [ ! -x "$lite_dir/node_modules/.bin/google-closure-compiler" ]; then
status "Installing Lite dependencies for the JS minimizer..."
( cd "$lite_dir" && npm ci )
fi
status "Minimizing JS..."
npm run minimize

Expand Down
12 changes: 12 additions & 0 deletions classes/models/FrmEntryMeta.php
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,18 @@ private static function update_entry_meta_if_changed( $entry_id, $field_id, $met

if ( is_string( $meta_value ) && null !== $previous_hash && md5( $meta_value ) === $previous_hash ) {
// Writing the same value again would only cost a query and trigger the meta change hooks.
/**
* Fires when an entry meta update is skipped because the stored value already matches.
*
* The frm_update_entry_meta filter has already run for this value, so a listener that
* keeps state from that filter can finish here, as it would after a write.
*
* @since x.x
*
* @param int $entry_id Entry ID.
* @param int $field_id Field ID.
*/
do_action( 'frm_entry_meta_unchanged', $entry_id, $field_id );
return 0;
}

Expand Down
53 changes: 43 additions & 10 deletions classes/models/FrmField.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,13 @@ class FrmField {
public static $transient_size = 200;

/**
* Cached field lists already loaded in this request, keyed by form ID.
* Cached field lists already loaded in this request, keyed by blog ID and form ID.
*
* Each list includes repeater and embedded fields. Reading the transient again would unserialize it again.
*
* @since x.x
*
* @var array<int, array{fields: array, sources: array<int, int>}>
* @var array<string, array{fields: array, sources: array<int, int>}>
*/
private static $request_cache = array();

Expand Down Expand Up @@ -1452,18 +1452,23 @@ private static function prepare_options( &$results ) {
* @return array{fields: array, sources: array<int, int>}|false False when there is no cached list and it was not built.
*/
private static function get_cached_fields( $form_id, $build ) {
if ( isset( self::$request_cache[ $form_id ] ) ) {
return self::$request_cache[ $form_id ];
// The current blog is part of the key, since a switch_to_blog() call can reuse a form ID.
$cache_key = get_current_blog_id() . ':' . $form_id;

if ( isset( self::$request_cache[ $cache_key ] ) ) {
return self::$request_cache[ $cache_key ];
}

$base_name = 'frm_form_fields_' . $form_id . 'includeinclude';
$fields = array();
$complete = self::get_next_transient( $fields, $base_name );
$sources = $complete && $fields ? get_transient( 'frm_form_fields_' . $form_id . 'includesources' ) : false;
$saved = $complete && $fields ? get_transient( 'frm_form_fields_' . $form_id . 'includesources' ) : false;

if ( is_array( $sources ) ) {
self::$request_cache[ $form_id ] = compact( 'fields', 'sources' );
return self::$request_cache[ $form_id ];
// Two requests can build the list at once, so the sources only count when they were saved with these fields.
if ( is_array( $saved ) && isset( $saved['signature'], $saved['sources'] ) && $saved['signature'] === self::get_field_list_signature( $fields ) ) {
$sources = $saved['sources'];
self::$request_cache[ $cache_key ] = compact( 'fields', 'sources' );
return self::$request_cache[ $cache_key ];
}

if ( ! $build || isset( self::$building_cache[ $form_id ] ) ) {
Expand All @@ -1476,7 +1481,14 @@ private static function get_cached_fields( $form_id, $build ) {

if ( $cache['fields'] ) {
// Save the sources first. The list is only read once its first chunk exists.
set_transient( 'frm_form_fields_' . $form_id . 'includesources', $cache['sources'], 60 * 60 * 6 );
set_transient(
'frm_form_fields_' . $form_id . 'includesources',
array(
'signature' => self::get_field_list_signature( $cache['fields'] ),
'sources' => $cache['sources'],
),
60 * 60 * 6
);
self::set_field_transient(
$cache['fields'],
$form_id,
Expand All @@ -1488,11 +1500,32 @@ private static function get_cached_fields( $form_id, $build ) {
);
}

self::$request_cache[ $form_id ] = $cache;
self::$request_cache[ $cache_key ] = $cache;

return $cache;
}

/**
* Identify a cached field list by the ID and form of each field, in order.
*
* The sources are saved apart from the fields, so this ties them to the list they were built with.
*
* @since x.x
*
* @param array $fields Field objects in list order.
*
* @return string
*/
private static function get_field_list_signature( $fields ) {
$parts = array();

foreach ( $fields as $field ) {
$parts[] = $field->id . ':' . $field->form_id;
}

return md5( implode( ',', $parts ) );
}

/**
* Query every field in a form and note where each one comes from.
*
Expand Down
13 changes: 12 additions & 1 deletion js/formidable_admin.js

Large diffs are not rendered by default.

113 changes: 105 additions & 8 deletions js/src/admin/admin.js
Original file line number Diff line number Diff line change
Expand Up @@ -2773,6 +2773,8 @@ window.frmAdminBuildJS = function() {
const fieldsAwaitingSettings = new Set();
// Fields whose settings failed to load during the last save attempt.
const fieldsFailedOnSave = new Set();
// Logic changes, in order, still to apply to fields whose settings failed to load.
const pendingLogicUpdates = new Map();

if ( thisFormId && browserPreviewFieldIds.length ) {
queueFieldSettings( {
Expand Down Expand Up @@ -3132,12 +3134,58 @@ window.frmAdminBuildJS = function() {
*
* @since x.x
* @param {string} fieldId The field whose choices changed.
* @param {Function} update Reads and changes the loaded logic rows.
* @param {Function} update Reads and changes the loaded logic rows. Gets a field ID when only that field's rows should change.
* @return {void}
*/
function whenLogicSettingsLoaded( fieldId, update ) {
const logicFieldIds = getFieldsWithLogicOn( fieldId ).map( String );
// Saving waits for these, and they move into the save form when they arrive. See handleLoadedFieldSettings.
whenFieldSettingsLoaded( getFieldsWithLogicOn( fieldId ), fieldsAwaitingSettings, update );
whenFieldSettingsLoaded( logicFieldIds, fieldsAwaitingSettings, () => {
update();
// A field whose settings failed to load still has the old logic, so change it again once they arrive.
logicFieldIds.filter( isFieldSettingsPending ).forEach( logicFieldId => {
const updates = pendingLogicUpdates.get( logicFieldId ) || [];
updates.push( update );
pendingLogicUpdates.set( logicFieldId, updates );
} );
} );
}

/**
* Apply the logic changes made while these fields' settings failed to load.
*
* @since x.x
* @param {string[]} fieldIds The fields whose settings were just cached.
* @return {void}
*/
function runPendingLogicUpdates( fieldIds ) {
fieldIds.forEach( fieldId => {
const updates = pendingLogicUpdates.get( fieldId );
if ( ! updates ) {
return;
}

pendingLogicUpdates.delete( fieldId );
updates.forEach( update => update( fieldId ) );
} );
}

/**
* Run a read of other fields' settings again once the pending ones arrive.
*
* Settings far from the viewport load only when needed, so a read made before they arrive
* finds no inputs. The refresh runs once after the requests settle, even when one failed.
*
* @since x.x
* @param {string[]} fieldIds The fields whose settings were read.
* @param {Function} refresh Reads the settings again.
* @return {void}
*/
function refreshWhenFieldSettingsLoad( fieldIds, refresh ) {
const pendingFieldIds = fieldIds.map( String ).filter( isFieldSettingsPending );
if ( pendingFieldIds.length ) {
whenFieldSettingsLoaded( pendingFieldIds, fieldsAwaitingSettings, refresh );
}
}

/**
Expand Down Expand Up @@ -3200,6 +3248,8 @@ window.frmAdminBuildJS = function() {

moveFieldSettings( ensureFieldSettings( fieldId ) );
} );

runPendingLogicUpdates( fieldIds );
}

/**
Expand Down Expand Up @@ -4322,10 +4372,11 @@ window.frmAdminBuildJS = function() {
/**
* Populates the calculation field list.
*
* @param {HTMLElement} element The calculation box element.
* @param {boolean} force Whether to force the refresh.
* @param {HTMLElement} element The calculation box element.
* @param {boolean} force Whether to force the refresh.
* @param {boolean} settingsLoaded Whether this is the refresh after pending field settings arrived.
*/
function popCalcFields( element, force ) {
function popCalcFields( element, force, settingsLoaded = false ) {
const settingsBox = jQuery( element ).closest( '.frm-single-settings' );
const calc = settingsBox.find( '.frm-calc-field' );

Expand Down Expand Up @@ -4361,25 +4412,41 @@ window.frmAdminBuildJS = function() {
const fields = wp.hooks.applyFilters( 'frm_calc_field_list', getFieldList(), { box, codeList, isSummary } );
const list = document.getElementById( `frm-calc-list-${ fieldId }` );
list.innerHTML = '';
const checkedFieldIds = [];

for ( let i = 0; i < fields.length; i++ ) {
if ( fields[ i ].fieldId === fieldId ) {
// A field can't be used in its own calculation, since that would create a loop.
continue;
}

if ( exclude?.includes( fields[ i ].fieldType ) ||
( excludedOpts.length && hasExcludedOption( fields[ i ], excludedOpts ) ) ) {
if ( exclude?.includes( fields[ i ].fieldType ) ) {
continue;
}

if ( excludedOpts.length ) {
checkedFieldIds.push( fields[ i ].fieldId );
if ( hasExcludedOption( fields[ i ], excludedOpts ) ) {
continue;
}
}

addCalcFieldLiToList( list, fieldId, fields[ i ].fieldId, fields[ i ].fieldName, fields[ i ].fieldType );

if ( ! isSummary ) {
// The summary field list is a list of fields to exclude, not a list of shortcodes, so field parts don't belong in it.
addFieldPartShortcodes( fields[ i ], fieldId, list );
}
}

if ( ! settingsLoaded ) {
// Fields whose settings haven't loaded can't be checked for excluded options yet.
refreshWhenFieldSettingsLoad( checkedFieldIds, () => {
if ( element.isConnected ) {
popCalcFields( element, force, true );
}
} );
}
}

/**
Expand Down Expand Up @@ -6920,7 +6987,7 @@ window.frmAdminBuildJS = function() {
optionMap[ fieldId ][ originalValue ].value = newValue;
}

whenLogicSettingsLoaded( fieldId, () => {
whenLogicSettingsLoaded( fieldId, onlyLogicFieldId => {
materializeAllFieldSettings( prepareLoadedFieldMarkup );
const fieldIds = [];
const rows = builderPage.querySelectorAll( '.frm_logic_row' );
Expand All @@ -6934,6 +7001,11 @@ window.frmAdminBuildJS = function() {
}

const logicId = row.id.split( '_' )[ 2 ];
if ( onlyLogicFieldId && logicId !== onlyLogicFieldId ) {
// Other rows already have this change.
continue;
}

const valueSelect = row.querySelector( `select[name="field_options[hide_opt_${ logicId }][]"]` );

let optionMatches;
Expand Down Expand Up @@ -7977,6 +8049,10 @@ window.frmAdminBuildJS = function() {
}
const { sourceID } = atts;
ensureFieldSettings( sourceID );
if ( ! atts.settingsLoaded ) {
// A source field far from the viewport has no option inputs until its settings arrive.
refreshWhenFieldSettingsLoad( [ sourceID ], () => refillDropdownOpts( field, atts ) );
}
const { placeholder } = atts;
const isProduct = isProductField( sourceID );
const showOther = atts.other;
Expand Down Expand Up @@ -8014,6 +8090,27 @@ window.frmAdminBuildJS = function() {
}
}

/**
* Fill a dropdown again once its source field's settings have loaded, keeping the chosen value.
*
* @since x.x
* @param {HTMLElement} field The dropdown to fill.
* @param {Object} atts The options fillDropdownOpts was first called with.
* @return {void}
*/
function refillDropdownOpts( field, atts ) {
if ( ! field.isConnected ) {
return;
}

const { value } = field;
fillDropdownOpts( field, { ...atts, settingsLoaded: true } );

if ( value && field.options && Array.from( field.options ).some( option => option.value === value ) ) {
field.value = value;
}
}

function addBlankSelectOption( field, placeholder ) {
const opt = document.createElement( 'option' );
const { firstChild } = field;
Expand Down
Loading
Loading