-
Notifications
You must be signed in to change notification settings - Fork 92
gw-draft-resume-change-notice.php: Added new snippet that displays a notice when a user resumes draft from a new IP/UA.
#1164
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
Show all changes
6 commits
Select commit
Hold shift + click to select a range
c84ab52
`gw-draft-resume-change-notice.php`: Added new snippet.
SebastianWiz 3018ec3
`gw-draft-resume-change-notice.php`: Added new snippet that displays …
SebastianWiz ba8fb22
`gw-draft-resume-change-notice.php`: Added new snippet that displays …
SebastianWiz aad73a5
`gw-draft-resume-change-notice.php`: Added new snippet that displays …
SebastianWiz 42a33c0
`gw-draft-resume-change-notice.php`: Added new snippet that displays …
SebastianWiz ea46c47
`gw-draft-resume-change-notice.php`: Added new snippet that displays …
SebastianWiz 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
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
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,71 @@ | ||
| <?php | ||
| /** | ||
| * Gravity Wiz // Gravity Forms // Draft Resume Change Notice | ||
| * https://gravitywiz.com/ | ||
| * | ||
| * Use this snippet to display a notice when the user resumes draft from a different location, browser or device. | ||
| */ | ||
| add_filter( 'gform_get_form_filter', function( $form_markup, $form ) { | ||
|
|
||
| if ( empty( $_GET['gf_token'] ) ) { | ||
| return $form_markup; | ||
| } | ||
| $token = sanitize_text_field( wp_unslash( $_GET['gf_token'] ) ); | ||
|
|
||
| global $wpdb; | ||
| $table = GFFormsModel::get_draft_submissions_table_name(); | ||
|
|
||
| $draft = $wpdb->get_row( | ||
| // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared, WordPress.DB.PreparedSQLPlaceholders.ReplacementsWrongNumber | ||
| $wpdb->prepare( | ||
| sprintf( | ||
| 'SELECT form_id, ip, submission FROM `%s` WHERE uuid = %%s', | ||
| esc_sql( $table ) | ||
| ), | ||
| $token | ||
| ) | ||
| ); | ||
|
|
||
| if ( ! $draft ) { | ||
| return $form_markup; | ||
| } | ||
|
|
||
| if ( (int) $form['id'] !== (int) $draft->form_id ) { | ||
| return $form_markup; | ||
| } | ||
|
|
||
| $submission_data = json_decode( $draft->submission, true ); | ||
| $submission_data = is_array( $submission_data ) ? $submission_data : array(); | ||
|
|
||
| $stored_user_agent = $submission_data['partial_entry']['user_agent'] ?? ''; | ||
| $current_user_agent = $_SERVER['HTTP_USER_AGENT'] ?? ''; | ||
|
|
||
| $stored_ip = $draft->ip ?? ''; | ||
| $current_ip = GFFormsModel::get_ip(); | ||
|
|
||
| $ip_changed = ( $stored_ip && $current_ip && $stored_ip !== $current_ip ); | ||
| $browser_changed = ( $stored_user_agent && $current_user_agent && $stored_user_agent !== $current_user_agent ); | ||
|
|
||
| if ( ! $ip_changed && ! $browser_changed ) { | ||
| return $form_markup; | ||
| } | ||
|
|
||
| // Configure Messages | ||
| $ip_changed_message = '🌍 Your location has changed since last editing this draft'; | ||
| $browser_changed_message = '💻 Your browser or device has changed since last editing this draft'; | ||
| $both_changed_message = '🔒 Your location AND device have both changed since last editing this draft'; | ||
|
|
||
| $message = $both_changed_message; | ||
| if ( $ip_changed && ! $browser_changed ) { | ||
| $message = $ip_changed_message; | ||
| } elseif ( $browser_changed && ! $ip_changed ) { | ||
| $message = $browser_changed_message; | ||
| } | ||
|
|
||
| $warning = '<div style="background:#fff3cd;border:1px solid #ffc107;padding:15px;margin-bottom:15px;">'; | ||
| $warning .= '<strong style="color:#856404;">' . esc_html( $message ) . '</strong>'; | ||
| $warning .= '</div>'; | ||
|
|
||
| return $warning . $form_markup; | ||
|
|
||
| }, 10, 2 ); | ||
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.
Consider sanitizing the current User-Agent.
While the current User-Agent is only used for comparison and not stored or displayed, sanitizing external input is a best practice. Consider using
sanitize_text_field()for consistency.Apply this diff:
📝 Committable suggestion
🤖 Prompt for AI Agents