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
112 changes: 7 additions & 105 deletions classes/helpers/FrmFieldsHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -982,63 +982,10 @@ public static function get_term_link( $tax_id ) {
* @return bool
*/
public static function value_meets_condition( $observed_value, $cond, $hide_opt ) {
$hide_opt = self::get_value_for_comparison( $hide_opt );
$observed_value = self::get_value_for_comparison( $observed_value );

if ( is_array( $observed_value ) ) {
return self::array_value_condition( $observed_value, $cond, $hide_opt );
}

$m = false;

if ( $cond === '==' ) {
$m = $observed_value == $hide_opt; // phpcs:ignore Universal.Operators.StrictComparisons
} elseif ( $cond === '!=' ) {
$m = $observed_value != $hide_opt; // phpcs:ignore Universal.Operators.StrictComparisons
} elseif ( $cond === '>' ) {
$m = $observed_value > $hide_opt;
} elseif ( $cond === '>=' ) {
$m = $observed_value >= $hide_opt;
} elseif ( $cond === '<' ) {
$m = $observed_value < $hide_opt;
} elseif ( $cond === '<=' ) {
$m = $observed_value <= $hide_opt;
} elseif ( $cond === 'LIKE' || $cond === 'not LIKE' ) {
$m = stripos( $observed_value, $hide_opt );
$m = $cond === 'not LIKE' ? $m === false : $m !== false;
} elseif ( $cond === '%LIKE' ) {
// Ends with
$length = strlen( $hide_opt );
$substr = substr( $observed_value, strlen( $observed_value ) - $length );
$m = 0 === strcasecmp( $substr, $hide_opt );
} elseif ( 'LIKE%' === $cond ) {
// Starts with
$length = strlen( $hide_opt );
$substr = substr( $observed_value, 0, $length );
$m = 0 === strcasecmp( $substr, $hide_opt );
}//end if

return $m;
}

/**
* Trim and sanitize the values
*
* @since 2.05
*
* @param array|string $value
*
* @return array|string
*/
private static function get_value_for_comparison( $value ) {
// Remove white space from hide_opt
if ( ! is_array( $value ) ) {
$value = trim( $value );
if ( is_callable( 'FrmProConditionalLogicHelper::value_meets_condition' ) ) {
return FrmProConditionalLogicHelper::value_meets_condition( $observed_value, $cond, $hide_opt );
}

FrmAppHelper::sanitize_value( 'wp_kses_post', $value );

return $value;
return FrmLegacyConditionalLogicHelper::value_meets_condition( $observed_value, $cond, $hide_opt );
}

/**
Expand All @@ -1049,55 +996,10 @@ private static function get_value_for_comparison( $value ) {
* @return bool
*/
public static function array_value_condition( $observed_value, $cond, $hide_opt ) {
$m = false;

if ( $cond === '==' ) {
if ( is_array( $hide_opt ) ) {
$m = array_intersect( $hide_opt, $observed_value );
$m = $m !== array();
} else {
// phpcs:ignore WordPress.PHP.StrictInArray.MissingTrueStrict
$m = in_array( $hide_opt, $observed_value );
}
} elseif ( $cond === '!=' ) {
// phpcs:ignore WordPress.PHP.StrictInArray.MissingTrueStrict
$m = ! in_array( $hide_opt, $observed_value );
} elseif ( $cond === '>' ) {
$m = min( $observed_value ) > $hide_opt;
} elseif ( $cond === '<' ) {
$m = max( $observed_value ) < $hide_opt;
} elseif ( $cond === 'LIKE' || $cond === 'not LIKE' ) {
foreach ( $observed_value as $ob ) {
$m = strpos( $ob, $hide_opt );

if ( $m !== false ) {
$m = true;
break;
}
}

if ( $cond === 'not LIKE' ) {
$m = $m === false;
}
} elseif ( $cond === '%LIKE' ) {
// Ends with
foreach ( $observed_value as $ob ) {
if ( $hide_opt === substr( $ob, strlen( $ob ) - strlen( $hide_opt ) ) ) {
$m = true;
break;
}
}
} elseif ( $cond === 'LIKE%' ) {
// Starts with
foreach ( $observed_value as $ob ) {
if ( str_starts_with( $ob, $hide_opt ) ) {
$m = true;
break;
}
}
}//end if

return $m;
if ( is_callable( 'FrmProConditionalLogicHelper::array_value_condition' ) ) {
return FrmProConditionalLogicHelper::array_value_condition( $observed_value, $cond, $hide_opt );
}
return FrmLegacyConditionalLogicHelper::array_value_condition( $observed_value, $cond, $hide_opt );
}

/**
Expand Down
137 changes: 137 additions & 0 deletions classes/helpers/FrmLegacyConditionalLogicHelper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
<?php

if ( ! defined( 'ABSPATH' ) ) {
die( 'You are not allowed to call this page directly.' );
}

/**
* Compatibility for callers of the field comparison API when Pro does not provide it.
*/
class FrmLegacyConditionalLogicHelper {

/**
* @param array|string $observed_value
* @param string $cond
* @param array|string $hide_opt
*
* @return bool
*/
public static function value_meets_condition( $observed_value, $cond, $hide_opt ) {
$hide_opt = self::get_value_for_comparison( $hide_opt );
$observed_value = self::get_value_for_comparison( $observed_value );

if ( is_array( $observed_value ) ) {
return self::array_value_condition( $observed_value, $cond, $hide_opt );
}

$m = false;

if ( $cond === '==' ) {
$m = $observed_value == $hide_opt; // phpcs:ignore Universal.Operators.StrictComparisons
} elseif ( $cond === '!=' ) {
$m = $observed_value != $hide_opt; // phpcs:ignore Universal.Operators.StrictComparisons
} elseif ( $cond === '>' ) {
$m = $observed_value > $hide_opt;
} elseif ( $cond === '>=' ) {
$m = $observed_value >= $hide_opt;
} elseif ( $cond === '<' ) {
$m = $observed_value < $hide_opt;
} elseif ( $cond === '<=' ) {
$m = $observed_value <= $hide_opt;
} elseif ( $cond === 'LIKE' || $cond === 'not LIKE' ) {
$m = stripos( $observed_value, $hide_opt );
$m = $cond === 'not LIKE' ? $m === false : $m !== false;
} elseif ( $cond === '%LIKE' ) {
// Ends with
$length = strlen( $hide_opt );
$substr = substr( $observed_value, strlen( $observed_value ) - $length );
$m = 0 === strcasecmp( $substr, $hide_opt );
} elseif ( 'LIKE%' === $cond ) {
// Starts with
$length = strlen( $hide_opt );
$substr = substr( $observed_value, 0, $length );
$m = 0 === strcasecmp( $substr, $hide_opt );
}//end if

return $m;
}

/**
* Trim and sanitize the values
*
* @since 2.05
*
* @param array|string $value
*
* @return array|string
*/
private static function get_value_for_comparison( $value ) {
// Remove white space from hide_opt
if ( ! is_array( $value ) ) {
$value = trim( $value );
}

FrmAppHelper::sanitize_value( 'wp_kses_post', $value );

return $value;
}

/**
* @param array $observed_value
* @param string $cond
* @param array|string $hide_opt
*
* @return bool
*/
public static function array_value_condition( $observed_value, $cond, $hide_opt ) {
$m = false;

if ( $cond === '==' ) {
if ( is_array( $hide_opt ) ) {
$m = array_intersect( $hide_opt, $observed_value );
$m = $m !== array();
} else {
// phpcs:ignore WordPress.PHP.StrictInArray.MissingTrueStrict
$m = in_array( $hide_opt, $observed_value );
}
} elseif ( $cond === '!=' ) {
// phpcs:ignore WordPress.PHP.StrictInArray.MissingTrueStrict
$m = ! in_array( $hide_opt, $observed_value );
} elseif ( $cond === '>' ) {
$m = min( $observed_value ) > $hide_opt;
} elseif ( $cond === '<' ) {
$m = max( $observed_value ) < $hide_opt;
} elseif ( $cond === 'LIKE' || $cond === 'not LIKE' ) {
foreach ( $observed_value as $ob ) {
$m = strpos( $ob, $hide_opt );

if ( $m !== false ) {
$m = true;
break;
}
}

if ( $cond === 'not LIKE' ) {
$m = $m === false;
}
} elseif ( $cond === '%LIKE' ) {
// Ends with
foreach ( $observed_value as $ob ) {
if ( $hide_opt === substr( $ob, strlen( $ob ) - strlen( $hide_opt ) ) ) {
$m = true;
break;
}
}
} elseif ( $cond === 'LIKE%' ) {
// Starts with
foreach ( $observed_value as $ob ) {
if ( str_starts_with( $ob, $hide_opt ) ) {
$m = true;
break;
}
}
}//end if

return $m;
}
}
2 changes: 1 addition & 1 deletion js/formidable-form-settings.js

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions js/formidable_admin.js

Large diffs are not rendered by default.

16 changes: 8 additions & 8 deletions js/formidable_builder.js

Large diffs are not rendered by default.

9 changes: 7 additions & 2 deletions js/src/admin/admin.js
Original file line number Diff line number Diff line change
Expand Up @@ -2960,7 +2960,12 @@ window.frmCheckAllLevel = ( checked, n, level ) => {
$kids.children( `input[name^="${ n }"]` ).prop( 'checked', Boolean( checked ) );
};

window.frmGetFieldValues = ( fieldId, cur, rowNumber, fieldType, htmlName, callback ) => {
window.frmGetFieldValues = ( ...args ) => {
const load = wp.hooks.applyFilters( 'frm_load_conditional_logic_values', fallbackLoadLogicValues );
load( ...args );
};

function fallbackLoadLogicValues( fieldId, cur, rowNumber, fieldType, htmlName, callback ) {
if ( ! fieldId ) {
return;
}
Expand All @@ -2979,4 +2984,4 @@ window.frmGetFieldValues = ( fieldId, cur, rowNumber, fieldType, htmlName, callb
}
}
} );
};
}
17 changes: 16 additions & 1 deletion js/src/admin/builder.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,22 @@ function createBuilder( shared ) {
builder.fieldActions = require( './builder/fieldActions' )( shared, builder );
builder.fieldOptions = require( './builder/fieldOptions' )( shared, builder );
builder.fieldGroups = require( './builder/fieldGroups' )( shared, builder );
builder.conditionalLogic = require( './builder/conditionalLogic' )( shared, builder );
const fallbackLogic = require( './builder/conditionalLogic' )( shared, builder );
let logicFactory;
let logicHandlers = fallbackLogic;

// Resolve at event time because Pro can load after Lite binds its listeners.
builder.conditionalLogic = {};
for ( const method of Object.keys( fallbackLogic ) ) {
builder.conditionalLogic[ method ] = function( ...args ) {
const factory = wp.hooks.applyFilters( 'frm_builder_conditional_logic_factory', null );
if ( factory !== logicFactory ) {
logicFactory = factory;
logicHandlers = factory ? { ...fallbackLogic, ...factory( shared, builder, materializeAllFieldSettings ) } : fallbackLogic;
}
return logicHandlers[ method ].apply( this, args );
};
Comment on lines +75 to +82

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function declared in a loop contains unsafe references to variable(s) 'logicFactory', 'logicHandlers'


Writing functions within loops tends to result in errors due to the way the function creates a closure around the loop. For example:

}
builder.fieldPreview = require( './builder/fieldPreview' )( shared, builder );
builder.fieldEditor = require( './builder/fieldEditor' )( shared, builder );

Expand Down
2 changes: 2 additions & 0 deletions js/src/admin/builder/conditionalLogic.js
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,7 @@ module.exports = function createConditionalLogic( {

if ( this.value === '' ) {
fieldSelect.options.length = 1;
wp.hooks.doAction( 'frm_builder_source_fields_changed', fieldID, [] );
} else {
const formID = this.value;
jQuery.ajax( {
Expand All @@ -316,6 +317,7 @@ module.exports = function createConditionalLogic( {
},
success( fields ) {
fieldSelect.innerHTML = fields;
wp.hooks.doAction( 'frm_builder_source_fields_changed', fieldID, fieldSelect.options );
}
} );
}
Expand Down
Loading