-
Notifications
You must be signed in to change notification settings - Fork 91
/
Copy pathgw-conditional-logic-entry-meta.php
248 lines (210 loc) · 7.59 KB
/
gw-conditional-logic-entry-meta.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
<?php
/**
* Gravity Wiz // Gravity Forms // Conditional Logic: Entry Meta
* https://gravitywiz.com/
*
* Supports all registered meta and as well as the "Payment Status" standard meta.
* Handles enabling conditional logic evaluation on send for GP Notification Scheduler when notification
* contains a conditional rule for "payment_status".
*
* Requires Gravity Forms 2.6.2.
*
* Plugin Name: GF Conditional Logic: Entry Meta
* Plugin URI: https://gravitywiz.com/
* Description: Use the entry meta in conditional logic (e.g. payment status, approval status, etc).
* Author: Gravity Wiz
* Version: 0.3.1
* Author URI: https://gravitywiz.com
*/
class GW_CL_Entry_Meta {
private static $instance;
public static function get_instance() {
if ( ! self::$instance ) {
self::$instance = new GW_CL_Entry_Meta();
}
return self::$instance;
}
private function __construct() {
if ( did_action( 'gform_loaded' ) ) {
$this->init();
} else {
add_action( 'gform_loaded', array( $this, 'init' ) );
}
}
public function init() {
add_action( 'admin_footer', array( $this, 'output_admin_script' ) );
add_filter( 'gform_rule_source_value', array( $this, 'set_rule_source_value' ), 10, 5 );
add_filter( 'gpns_evaluate_conditional_logic_on_send', array( $this, 'gpns_should_evaluate_conditional_logic_on_send' ), 10, 4 );
}
public function output_admin_script() {
if ( wp_script_is( 'gform_form_admin' ) && GFForms::get_page() !== 'form_editor' ) :
?>
<script>
const entryOptions = <?php echo json_encode( $this->get_conditional_logic_options() ); ?>;
gform.addFilter( 'gform_conditional_logic_fields', function( options, form, selectedFieldId ) {
for ( const property in entryOptions ) {
// Entry meta are already added in Notifications and Confirmations conditional logic but not in feeds.
// Let's just make sure that none of our entry meta options have been previously added.
if ( entryOptions.hasOwnProperty( property ) && ! options.find( opt => opt.value === entryOptions[ property ].value ) ) {
options.push( {
label: entryOptions[ property ].label,
value: entryOptions[ property ].value
} );
}
}
return options;
} );
gform.addFilter( 'gform_conditional_logic_operators', function( operators, objectType, fieldId ) {
if ( entryOptions.hasOwnProperty( fieldId ) ) {
operators = entryOptions[ fieldId ].operators;
}
return operators;
} );
gform.addFilter( 'gform_conditional_logic_values_input', function( str, objectType, ruleIndex, selectedFieldId, selectedValue ) {
if ( entryOptions.hasOwnProperty( selectedFieldId ) && entryOptions[ selectedFieldId ].choices ) {
let inputName = objectType + '_rule_value_' + ruleIndex;
str = GetRuleValuesDropDown( entryOptions[ selectedFieldId ].choices, objectType, ruleIndex, selectedValue, inputName );
}
return str;
} );
</script>
<?php
endif;
}
public function get_conditional_logic_options() {
$form_ids = 0;
// Scope entry meta to the current form for GP Email Users and other admin screens
if ( is_admin() ) {
if ( rgget( 'page' ) === 'gp-email-users' ) {
$form_ids = rgpost( '_gform_setting_form' );
if ( ! $form_ids ) {
$draft = gp_email_users()->get_draft();
$form_ids = $draft['form'];
}
}
if ( GFForms::get_page() && rgget( 'id' ) ) {
$form_ids = rgget( 'id' );
}
}
$options = array();
if ( ! empty( $form_ids ) ) {
$post_submission_conditional_logic_field_types = array(
'uid' => array(
'operators' => array(
'is' => 'is',
'isnot' => 'isNot',
'>' => 'greaterThan',
'<' => 'lessThan',
'contains' => 'contains',
'starts_with' => 'startsWith',
'ends_with' => 'endsWith',
),
),
);
$form = GFAPI::get_form( is_array( $form_ids ) ? $form_ids[0] : $form_ids );
if ( $form ) {
$fields = GFAPI::get_fields_by_type( $form, array_keys( $post_submission_conditional_logic_field_types ) );
foreach ( $fields as $field ) {
$options[ $field->id ] = array(
'label' => $field->label,
'value' => $field->id,
'operators' => rgars( $post_submission_conditional_logic_field_types, $field->type . '/operators', array() ),
);
}
}
}
$entry_meta = GFFormsModel::get_entry_meta( $form_ids );
$choices_by_key = array(
'is_approved' => array(
1 => esc_html__( 'Approved', 'gravityview' ),
2 => esc_html__( 'Disapproved', 'gravityview' ),
3 => esc_html__( 'Unapproved', 'gravityview' ),
),
);
foreach ( $entry_meta as $key => $meta ) {
$options[ $key ] = array(
'label' => $meta['label'],
'value' => $key,
'operators' => array(
'is' => 'is',
'isnot' => 'isNot',
),
);
$_choices = rgar( $choices_by_key, $key );
/*
* Use choices by key if exists, otherwise, attempt to pull from $meta/filter/choices, which contains
* array{ text: string, value: string }[]
*/
if ( empty( $_choices ) ) {
$filter_choices = rgars( $meta, 'filter/choices' );
if ( ! empty( $filter_choices ) ) {
$_choices = wp_list_pluck( $filter_choices, 'text', 'value' );
}
}
if ( ! empty( $_choices ) ) {
$choices = array();
foreach ( $_choices as $value => $text ) {
$choices[] = compact( 'text', 'value' );
}
$options[ $key ]['choices'] = $choices;
}
}
$options['payment_status'] = array(
'label' => esc_html__( 'Payment Status', 'gravityforms' ),
'value' => 'payment_status',
'choices' => $this->get_payment_status_choices(),
'operators' => array(
'is' => 'is',
'isnot' => 'isNot',
),
);
return $options;
}
public function get_payment_status_choices() {
$choices = array();
foreach ( GFCommon::get_entry_payment_statuses() as $text => $value ) {
$choices[] = compact( 'text', 'value' );
}
return $choices;
}
public function set_rule_source_value( $source_value, $rule, $form, $logic, $entry ) {
$keys = array_keys( $this->get_conditional_logic_options() );
$target = $rule['fieldId'];
if ( in_array( $target, $keys ) && $entry ) {
if ( in_array( $target, $this->get_runtime_entry_meta_keys(), true ) ) {
// Some payment add-ons do not update the runtime entry but do update the entry in the database.
// Fetch the latest from the database.
$entry = GFAPI::get_entry( $entry['id'] );
}
$source_value = rgar( $entry, $rule['fieldId'] );
}
return $source_value;
}
public function gpns_should_evaluate_conditional_logic_on_send( $eval_on_send, $form, $entry, $notifications ) {
foreach ( $notifications as $notification ) {
$_notification = gp_notification_schedule()->get_notification( $form, $notification['nid'] );
foreach ( rgars( $_notification, 'notification_conditional_logic_object/rules' ) as $rule ) {
if ( in_array( $rule['fieldId'], $this->get_runtime_entry_meta_keys(), true ) ) {
return true;
}
}
}
return $eval_on_send;
}
/**
* Get the keys for any entry meta that may be updated during a form submission.
*
* Since these are updated mid-submission, conditional logic in some contexts (like notifications triggered by a feed
* action) will be using a stale entry. Identifying these entry meta keys allows us to ensure special functionality
* to support them.
*
* @return mixed|void
*/
public function get_runtime_entry_meta_keys() {
return apply_filters( 'gwclem_runtime_entry_meta_keys', array( 'payment_status' ) );
}
}
function gw_cl_entry_meta() {
return GW_CL_Entry_Meta::get_instance();
}
gw_cl_entry_meta();