-
Notifications
You must be signed in to change notification settings - Fork 91
gw-conditional-logic-operator-does-not-contain.php
: Added snippet for does not contain conditional logic comparison.
#1073
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
172 changes: 172 additions & 0 deletions
172
gravity-forms/gw-conditional-logic-operator-does-not-contain.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,172 @@ | ||
<?php | ||
/** | ||
* Gravity Wiz // Gravity Forms // Conditional Logic Operator: "Does Not Contain" | ||
* | ||
* Instruction Video: https://www.loom.com/share/8e1b27ec47b341dbb4f0da2bec6a960b | ||
* | ||
* Check if a source field value does NOT contain a specific substring using the "does not contain" conditional logic operator. | ||
* | ||
* Plugin Name: GF Conditional Logic Operator: "Does Not Contain" | ||
* Plugin URI: https://gravitywiz.com/ | ||
* Description: Adds support for the "does not contain" conditional logic operator in Gravity Forms. | ||
* Author: Gravity Wiz | ||
* Version: 1.0 | ||
* Author URI: https://gravitywiz.com | ||
*/ | ||
class GF_CLO_Does_Not_Contain { | ||
|
||
public function __construct() { | ||
add_action( 'init', array( $this, 'init' ) ); | ||
} | ||
|
||
public function init() { | ||
|
||
add_filter( 'admin_footer', array( $this, 'output_admin_inline_script' ) ); | ||
add_filter( 'gform_is_valid_conditional_logic_operator', array( $this, 'whitelist_operator' ), 10, 2 ); | ||
add_filter( 'gpeu_field_filters_from_conditional_logic', array( $this, 'convert_conditional_logic_to_field_filters' ) ); | ||
|
||
add_filter( 'gform_pre_render', array( $this, 'load_form_script' ), 10, 2 ); | ||
add_filter( 'gform_register_init_scripts', array( $this, 'add_init_script' ), 10, 2 ); | ||
add_filter( 'gform_is_value_match', array( $this, 'evaluate_operator' ), 10, 6 ); | ||
|
||
} | ||
|
||
public function output_admin_inline_script() { | ||
if ( ! GFForms::get_page() && ! is_admin() && ! in_array( rgget( 'page' ), array( 'gp-email-users' ) ) ) { | ||
return; | ||
} | ||
?> | ||
<script> | ||
if ( window.gf_vars ) { | ||
gf_vars['does_not_contain'] = '<?php esc_html_e( 'does not contain' ); ?>'; | ||
} | ||
|
||
gform.addFilter( 'gform_conditional_logic_operators', function( operators ) { | ||
operators.does_not_contain = 'does not contain'; | ||
return operators; | ||
} ); | ||
</script> | ||
<?php | ||
} | ||
|
||
public function load_form_script( $form, $is_ajax_enabled ) { | ||
|
||
if ( $this->is_applicable_form( $form ) && ! has_action( 'wp_footer', array( $this, 'output_script' ) ) ) { | ||
add_action( 'wp_footer', array( $this, 'output_script' ) ); | ||
add_action( 'gform_preview_footer', array( $this, 'output_script' ) ); | ||
} | ||
|
||
return $form; | ||
} | ||
|
||
public function output_script() { | ||
?> | ||
<script type="text/javascript"> | ||
( function( $ ) { | ||
|
||
window.GWCLODoesNotContain = function( args ) { | ||
|
||
var self = this; | ||
|
||
for ( var prop in args ) { | ||
if ( args.hasOwnProperty( prop ) ) { | ||
self[ prop ] = args[ prop ]; | ||
} | ||
} | ||
|
||
self.init = function() { | ||
gform.addFilter( 'gform_is_value_match', function( isMatch, formId, rule ) { | ||
|
||
if ( rule.operator !== 'does_not_contain' ) { | ||
return isMatch; | ||
} | ||
|
||
var fieldValue = ''; | ||
var $field = $( '#input_' + formId + '_' + rule.fieldId ); | ||
|
||
// Handle different field types | ||
if ( $field.is(':checkbox') || $field.is(':radio') ) { | ||
fieldValue = $field.filter(':checked').map(function() { | ||
return this.value; | ||
}).get().join(','); | ||
} else if ( $field.is('select[multiple]') ) { | ||
fieldValue = $field.val() ? $field.val().join(',') : ''; | ||
} else { | ||
fieldValue = $field.val() || ''; | ||
} | ||
isMatch = typeof fieldValue === 'string' && fieldValue.indexOf( rule.value ) === -1; | ||
|
||
return isMatch; | ||
} ); | ||
}; | ||
|
||
self.init(); | ||
|
||
} | ||
|
||
} )( jQuery ); | ||
</script> | ||
<?php | ||
} | ||
|
||
public function add_init_script( $form ) { | ||
|
||
if ( ! $this->is_applicable_form( $form ) ) { | ||
return; | ||
} | ||
|
||
$script = 'new GWCLODoesNotContain();'; | ||
$slug = implode( '_', array( 'gwclo_does_not_contain', $form['id'] ) ); | ||
|
||
GFFormDisplay::add_init_script( $form['id'], $slug, GFFormDisplay::ON_PAGE_RENDER, $script ); | ||
} | ||
|
||
public function whitelist_operator( $is_valid, $operator ) { | ||
if ( $operator === 'does_not_contain' ) { | ||
$is_valid = true; | ||
} | ||
return $is_valid; | ||
} | ||
|
||
public function evaluate_operator( $is_match, $field_value, $target_value, $operation, $source_field, $rule ) { | ||
|
||
if ( $rule['operator'] !== 'does_not_contain' || rgar( $rule, 'gwclodncEvaluatingOperator' ) ) { | ||
return $is_match; | ||
} | ||
|
||
$rule['gwclodncEvaluatingOperator'] = true; | ||
|
||
// If the field contains the target value, it's not a match. | ||
$is_match = strpos( $field_value, $target_value ) === false; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Would it make sense to make this check case insensitive, in the JS check and in |
||
|
||
$rule['gwclodncEvaluatingOperator'] = false; | ||
|
||
return $is_match; | ||
} | ||
|
||
public function convert_conditional_logic_to_field_filters( $field_filters ) { | ||
|
||
foreach ( $field_filters as &$field_filter ) { | ||
if ( ! is_array( $field_filter ) ) { | ||
continue; | ||
} | ||
|
||
switch ( $field_filter['operator'] ) { | ||
case 'does_not_contain': | ||
$field_filter['operator'] = 'NOT LIKE'; | ||
$field_filter['value'] = '%' . $field_filter['value'] . '%'; | ||
break; | ||
} | ||
} | ||
|
||
return $field_filters; | ||
} | ||
|
||
public function is_applicable_form( $form ) { | ||
return GFFormDisplay::has_conditional_logic( $form ); | ||
} | ||
} | ||
|
||
# Configuration | ||
|
||
new GF_CLO_Does_Not_Contain(); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Might be worth setting
$text = esc_html_e( 'does not contain' );
in a variable above in PHP, then using here and above on line 41. Or using the translation function here as well