Skip to content
66 changes: 66 additions & 0 deletions gravity-forms/gw-draft-resume-change-notice.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?php
/**
* Gravity Wiz // Gravity Forms // Draft Resume Change Notice
* https://gravitywiz.com/
*
* Use this snippet to display a notice when the user resumes a 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'] ) );

Check failure on line 13 in gravity-forms/gw-draft-resume-change-notice.php

View workflow job for this annotation

GitHub Actions / PHPCS (Files Changed)

Tabs must be used to indent lines; spaces are not allowed

global $wpdb;
$table = GFFormsModel::get_draft_submissions_table_name();
$draft = $wpdb->get_row(
$wpdb->prepare(
"SELECT form_id, ip, submission FROM {$table} WHERE uuid = %s",

Check failure on line 19 in gravity-forms/gw-draft-resume-change-notice.php

View workflow job for this annotation

GitHub Actions / PHPCS (Files Changed)

Use placeholders and $wpdb-&gt;prepare(); found interpolated variable $table at &quot;SELECT form_id, ip, submission FROM {$table} WHERE uuid = %s&quot;
$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'] ?? '';

Check warning on line 35 in gravity-forms/gw-draft-resume-change-notice.php

View workflow job for this annotation

GitHub Actions / PHPCS (Files Changed)

Equals sign not aligned with surrounding assignments; expected 2 spaces but found 1 space
$current_user_agent = $_SERVER['HTTP_USER_AGENT'] ?? '';

$stored_ip = $draft->ip ?? '';

Check warning on line 38 in gravity-forms/gw-draft-resume-change-notice.php

View workflow job for this annotation

GitHub Actions / PHPCS (Files Changed)

Equals sign not aligned with surrounding assignments; expected 2 spaces but found 3 spaces
$current_ip = GFFormsModel::get_ip();

Check warning on line 39 in gravity-forms/gw-draft-resume-change-notice.php

View workflow job for this annotation

GitHub Actions / PHPCS (Files Changed)

Equals sign not aligned with surrounding assignments; expected 1 space but found 2 spaces

$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";

Check failure on line 49 in gravity-forms/gw-draft-resume-change-notice.php

View workflow job for this annotation

GitHub Actions / PHPCS (Files Changed)

String &quot;&#x1F30D; Your location has changed since last editing this draft&quot; does not require double quotes; use single quotes instead
$browser_changed_message = "💻 Your browser or device has changed since last editing this draft";

Check failure on line 50 in gravity-forms/gw-draft-resume-change-notice.php

View workflow job for this annotation

GitHub Actions / PHPCS (Files Changed)

String &quot;&#x1F4BB; Your browser or device has changed since last editing this draft&quot; does not require double quotes; use single quotes instead
$both_changed_message = "🔒 Your location and device have both changed since last editing this draft";

Check failure on line 51 in gravity-forms/gw-draft-resume-change-notice.php

View workflow job for this annotation

GitHub Actions / PHPCS (Files Changed)

String &quot;&#x1F512; Your location and device have both changed since last editing this draft&quot; does not require double quotes; use single quotes instead

$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 );
Loading