-
Notifications
You must be signed in to change notification settings - Fork 91
/
Copy pathgpb-blocklist-modifier.php
68 lines (61 loc) · 2.21 KB
/
gpb-blocklist-modifier.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
<?php
/**
* Gravity Forms // Gravity Perks // Modifier: Add to Blocklist
* https://gravitywiz.com/documentation/gravity-forms-blocklist/
*
* This snippet introduces a `:blocklist` merge tag modifier for Gravity Forms.
* When applied to any field merge tag, it generates a secure URL that allows
* users to easily add that field's value to WordPress's comment
* disallowed list (formerly the "comment blacklist").
*
* Tip: This can be used to allow users to prevent their email from being submitted
* on any GPB-enabled Gravity Form.
*
* Usage:
*
* 1. Use the merge tag with the `:blocklist` modifier in your Gravity Forms notifications
* or confirmations. For example:
*
* `{Email:1:blocklist}`
*
* This will output a secure URL that, when visited, adds the email address to the
* WordPress blocklist.
*
* 2. Users clicking the generated link will automatically append the field's value to the
* WordPress disallowed comment list.
*
* Instructions:
*
* 1. Install the snippet.
* https://gravitywiz.com/documentation/managing-snippets/#where-do-i-put-snippets
*
* 2. Use the `:blocklist` modifier on any field merge tag.
*/
add_filter( 'gform_merge_tag_filter', function ( $value, $merge_tag, $modifier, $field, $raw_value, $format ) {
if ( $modifier !== 'blocklist' || empty( $raw_value ) || ! is_email( $raw_value ) ) {
return $value;
}
$blocklist_url = add_query_arg([
'value' => $raw_value,
'hash' => wp_hash( $raw_value ),
], site_url( '/?gf_blocklist=1' ));
return esc_url( $blocklist_url );
}, 10, 6 );
add_action( 'init', function () {
if ( empty( $_GET['gf_blocklist'] ) || $_GET['gf_blocklist'] !== '1' ) {
return;
}
$value = rgget( 'value' );
$hash = rgget( 'hash' );
if ( empty( $value ) || $hash !== wp_hash( $value ) ) {
wp_die( 'Invalid value.' );
}
// Retrieve the current comment blocklist
$blocklist = get_option( 'disallowed_keys', '' );
// Append the new email if it's not already in the list
if ( stripos( $blocklist, $value ) === false ) {
$blocklist .= PHP_EOL . $value;
update_option( 'disallowed_keys', trim( $blocklist ) );
}
wp_die( sprintf( '"%s" has been added to the blocklist.', $value ), 'Blocklist', ['response' => 200] );
});