This repository has been archived by the owner on Aug 11, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
paysubs-encryption.php
executable file
·250 lines (185 loc) · 7.8 KB
/
paysubs-encryption.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
249
250
<?php
/**
* Gravity Forms Post Content Merge Tags
*
* Adds support for using Gravity Form merge tags in your post content. This functionality requires that the entry ID is
* is passed to the post via the "eid" parameter.
*
* Setup your confirmation page (requires GFv1.8) or confirmation URL "Redirect Query String" setting to
* include this parameter: 'eid={entry_id}'. You can then use any entry-based merge tag in your post content.
*
*/
if ( !class_exists( 'GW_Post_Content_Merge_Tags' ) ) {
class GW_Post_Content_Merge_Tags
{
public static $_entry = null;
private static $instance = null;
public static function get_instance( $args = array() )
{
if ( self::$instance == null ) {
self::$instance = new self( $args );
}
return self::$instance;
}
public function __construct( $args )
{
if ( !class_exists( 'GFForms' ) ) {
return;
}
$this->_args = wp_parse_args( $args, array(
'auto_append_eid' => false, // true, false or array of form IDs
'encrypt_eid' => false,
) );
add_filter( 'the_content', array( $this, 'replace_merge_tags' ), 1 );
add_filter( 'gform_replace_merge_tags', array( $this, 'replace_encrypt_entry_id_merge_tag' ), 10, 3 );
if ( !empty( $this->_args['auto_append_eid'] ) ) {
add_filter( 'gform_confirmation', array( $this, 'append_eid_parameter' ), 20, 3 );
}
}
public function replace_merge_tags( $post_content )
{
$entry = $this->get_entry();
if ( !$entry ) {
return $post_content;
}
$form = GFFormsModel::get_form_meta( $entry['form_id'] );
$post_content = $this->replace_field_label_merge_tags( $post_content, $form );
$post_content = GFCommon::replace_variables( $post_content, $form, $entry, false, false, false );
return $post_content;
}
public function replace_field_label_merge_tags( $text, $form )
{
preg_match_all( '/{([^:]+?)}/', $text, $matches, PREG_SET_ORDER );
if ( empty( $matches ) ) {
return $text;
}
foreach ( $matches as $match ) {
list( $search, $field_label ) = $match;
foreach ( $form['fields'] as $field ) {
$full_input_id = false;
$matches_admin_label = rgar( $field, 'adminLabel' ) == $field_label;
$matches_field_label = false;
if ( is_array( $field['inputs'] ) ) {
foreach ( $field['inputs'] as $input ) {
if ( GFFormsModel::get_label( $field, $input['id'] ) == $field_label ) {
$matches_field_label = true;
$input_id = $input['id'];
break;
}
}
} else {
$matches_field_label = GFFormsModel::get_label( $field ) == $field_label;
$input_id = $field['id'];
}
if ( !$matches_admin_label && !$matches_field_label ) {
continue;
}
$replace = sprintf( '{%s:%s}', $field_label, (string) $input_id );
$text = str_replace( $search, $replace, $text );
break;
}
}
return $text;
}
public function replace_encrypt_entry_id_merge_tag( $text, $form, $entry )
{
if ( strpos( $text, '{encrypted_entry_id}' ) === false ) {
return $text;
}
// $entry is not always a "full" entry
$entry_id = rgar( $entry, 'id' );
if ( $entry_id ) {
$entry_id = $this->prepare_eid( $entry['id'], true );
}
return str_replace( '{encrypted_entry_id}', $entry_id, $text );
}
public function append_eid_parameter( $confirmation, $form, $entry )
{
$is_ajax_redirect = is_string( $confirmation ) && strpos( $confirmation, 'gformRedirect' );
$is_redirect = is_array( $confirmation ) && isset( $confirmation['redirect'] );
if ( !$this->is_auto_eid_enabled( $form ) || !( $is_ajax_redirect || $is_redirect ) ) {
return $confirmation;
}
$eid = $this->prepare_eid( $entry['id'] );
if ( $is_ajax_redirect ) {
preg_match_all( '/gformRedirect.+?(http.+?)(?=\'|")/', $confirmation, $matches, PREG_SET_ORDER );
list( $full_match, $url ) = $matches[0];
$redirect_url = add_query_arg( array( 'eid' => $eid ), $url );
$confirmation = str_replace( $url, $redirect_url, $confirmation );
} else {
$redirect_url = add_query_arg( array( 'eid' => $eid ), $confirmation['redirect'] );
$confirmation['redirect'] = $redirect_url;
}
return $confirmation;
}
public function prepare_eid( $entry_id, $force_encrypt = false )
{
$eid = $entry_id;
$do_encrypt = $force_encrypt || $this->_args['encrypt_eid'];
if ( $do_encrypt && is_callable( array( 'GFCommon', 'encrypt' ) ) ) {
// $eid = ( GFCommon::encrypt( $eid ) );
}
return $eid;
}
public function get_entry()
{
if ( !self::$_entry ) {
$entry_id = $this->get_entry_id();
if ( !$entry_id ) {
return false;
}
$entry = GFFormsModel::get_lead( $entry_id );
if ( empty( $entry ) ) {
return false;
}
self::$_entry = $entry;
}
return self::$_entry;
}
public function get_entry_id()
{
$entry_id = rgget( 'eid' );
if ( $entry_id ) {
return $this->maybe_decrypt_entry_id( $entry_id );
}
$post = get_post();
if ( $post ) {
$entry_id = get_post_meta( $post->ID, '_gform-entry-id', true );
}
return $entry_id ? $entry_id : false;
}
public function maybe_decrypt_entry_id( $entry_id )
{
// if encryption is enabled, 'eid' parameter MUST be encrypted
$do_encrypt = $this->_args['encrypt_eid'];
if ( !$entry_id ) {
return null;
} else if ( !$do_encrypt && is_numeric( $entry_id ) && intval( $entry_id ) > 0 ) {
return $entry_id;
} else {
if ( is_callable( array( 'GFCommon', 'decrypt' ) ) ) {
$entry_id = PaySubs_encryption( $entry_id, 'd' );
}
return intval( $entry_id );
}
}
public function is_auto_eid_enabled( $form )
{
$auto_append_eid = $this->_args['auto_append_eid'];
if ( is_bool( $auto_append_eid ) && $auto_append_eid === true ) {
return true;
}
if ( is_array( $auto_append_eid ) && in_array( $form['id'], $auto_append_eid ) ) {
return true;
}
return false;
}
}
}
if ( !function_exists( 'gw_post_content_merge_tags' ) ) {
function gw_post_content_merge_tags( $args = array() )
{
return GW_Post_Content_Merge_Tags::get_instance( $args );
}
}
gw_post_content_merge_tags();