|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Gravity Wiz // Gravity Forms // Draft Resume Change Notice |
| 4 | + * https://gravitywiz.com/ |
| 5 | + * |
| 6 | + * Use this snippet to display a notice when the user resumes a draft from a different location, browser or device. |
| 7 | + */ |
| 8 | +add_filter( 'gform_get_form_filter', function( $form_markup, $form ) { |
| 9 | + |
| 10 | + if ( empty( $_GET['gf_token'] ) ) { |
| 11 | + return $form_markup; |
| 12 | + } |
| 13 | + $token = sanitize_text_field( wp_unslash( $_GET['gf_token'] ) ); |
| 14 | + |
| 15 | + global $wpdb; |
| 16 | + $table = GFFormsModel::get_draft_submissions_table_name(); |
| 17 | + $draft = $wpdb->get_row( |
| 18 | + $wpdb->prepare( |
| 19 | + "SELECT form_id, ip, submission FROM {$table} WHERE uuid = %s", |
| 20 | + $token |
| 21 | + ) |
| 22 | + ); |
| 23 | + |
| 24 | + if ( ! $draft ) { |
| 25 | + return $form_markup; |
| 26 | + } |
| 27 | + |
| 28 | + if ( (int) $form['id'] !== (int) $draft->form_id ) { |
| 29 | + return $form_markup; |
| 30 | + } |
| 31 | + |
| 32 | + $submission_data = json_decode( $draft->submission, true ); |
| 33 | + $submission_data = is_array( $submission_data ) ? $submission_data : array(); |
| 34 | + |
| 35 | + $stored_user_agent = $submission_data['partial_entry']['user_agent'] ?? ''; |
| 36 | + $current_user_agent = $_SERVER['HTTP_USER_AGENT'] ?? ''; |
| 37 | + |
| 38 | + $stored_ip = $draft->ip ?? ''; |
| 39 | + $current_ip = GFFormsModel::get_ip(); |
| 40 | + |
| 41 | + $ip_changed = ( $stored_ip && $current_ip && $stored_ip !== $current_ip ); |
| 42 | + $browser_changed = ( $stored_user_agent && $current_user_agent && $stored_user_agent !== $current_user_agent ); |
| 43 | + |
| 44 | + if ( ! $ip_changed && ! $browser_changed ) { |
| 45 | + return $form_markup; |
| 46 | + } |
| 47 | + |
| 48 | + // Configure Messages |
| 49 | + $ip_changed_message = "🌍 Your location has changed since last editing this draft"; |
| 50 | + $browser_changed_message = "💻 Your browser or device has changed since last editing this draft"; |
| 51 | + $both_changed_message = "🔒 Your location and device have both changed since last editing this draft"; |
| 52 | + |
| 53 | + $message = $both_changed_message; |
| 54 | + if ( $ip_changed && ! $browser_changed ) { |
| 55 | + $message = $ip_changed_message; |
| 56 | + } elseif ( $browser_changed && ! $ip_changed ) { |
| 57 | + $message = $browser_changed_message; |
| 58 | + } |
| 59 | + |
| 60 | + $warning = '<div style="background:#fff3cd;border:1px solid #ffc107;padding:15px;margin-bottom:15px;">'; |
| 61 | + $warning .= '<strong style="color:#856404;">' . esc_html( $message ) . '</strong>'; |
| 62 | + $warning .= '</div>'; |
| 63 | + |
| 64 | + return $warning . $form_markup; |
| 65 | + |
| 66 | +}, 10, 2 ); |
0 commit comments