Skip to content

Commit b70a49b

Browse files
authored
gpb-blocklist-modifier.php: Added new snippet.
1 parent 6b9e42a commit b70a49b

File tree

1 file changed

+68
-0
lines changed

1 file changed

+68
-0
lines changed
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
<?php
2+
/**
3+
* Gravity Forms // Gravity Perks // Add to Block List Modifier
4+
* https://gravitywiz.com/documentation/gravity-forms-blocklist/
5+
*
6+
* This snippet introduces a `:blocklist` merge tag modifier for Gravity Forms.
7+
* When applied to any field merge tag, it generates a secure URL that allows
8+
* users to easily add that field's value to WordPress's comment
9+
* disallowed list (formerly the "comment blacklist").
10+
*
11+
* Tip: This can be used to allow users to prevent their email from being submitted
12+
* on any GPB-enabled Gravity Form.
13+
*
14+
* Usage:
15+
*
16+
* 1. Use the merge tag with the `:blocklist` modifier in your Gravity Forms notifications
17+
* or confirmations. For example:
18+
*
19+
* `{Email:1:blocklist}`
20+
*
21+
* This will output a secure URL that, when visited, adds the email address to the
22+
* WordPress blocklist.
23+
*
24+
* 2. Users clicking the generated link will automatically append the field's value to the
25+
* WordPress disallowed comment list.
26+
*
27+
* Instructions:
28+
*
29+
* 1. Install the snippet.
30+
* https://gravitywiz.com/documentation/managing-snippets/#where-do-i-put-snippets
31+
*
32+
* 2. Use the `:blocklist` modifier on any field merge tag.
33+
*/
34+
add_filter( 'gform_merge_tag_filter', function ( $value, $merge_tag, $modifier, $field, $raw_value, $format ) {
35+
if ( $modifier !== 'blocklist' || empty( $raw_value ) || ! is_email( $raw_value ) ) {
36+
return $value;
37+
}
38+
39+
$blocklist_url = add_query_arg([
40+
'value' => $raw_value,
41+
'hash' => wp_hash( $raw_value ),
42+
], site_url( '/?gf_blocklist=1' ));
43+
44+
return esc_url( $blocklist_url );
45+
}, 10, 6 );
46+
47+
add_action( 'init', function () {
48+
if ( empty( $_GET['gf_blocklist'] ) || $_GET['gf_blocklist'] !== '1' ) {
49+
return;
50+
}
51+
52+
$value = rgget( 'value' );
53+
$hash = rgget( 'hash' );
54+
if ( empty( $value ) || $hash !== wp_hash( $value ) ) {
55+
wp_die( 'Invalid value.' );
56+
}
57+
58+
// Retrieve the current comment blocklist
59+
$blocklist = get_option( 'disallowed_keys', '' );
60+
61+
// Append the new email if it's not already in the list
62+
if ( stripos( $blocklist, $value ) === false ) {
63+
$blocklist .= PHP_EOL . $value;
64+
update_option( 'disallowed_keys', trim( $blocklist ) );
65+
}
66+
67+
wp_die( sprintf( '"%s" has been added to the blocklist.', $value ), 'Blocklist', ['response' => 200] );
68+
});

0 commit comments

Comments
 (0)