-
-
Save spivurno/6584835 to your computer and use it in GitHub Desktop.
<?php | |
/** | |
Plugin Name: Gravity Forms Cookies | |
Plugin URI: http://ounceoftalent.com/ | |
Description: Saves the query string parameters from a users visit to a cookie and allows these values to be populated via Gravity Forms' Dynamic Population feature. | |
Version: 1.5 | |
Author: David Smith | |
Author URI: http://ounceoftalent.com | |
License: GPL2 | |
*/ | |
class GFCookie { | |
public static $cookie_prefix = 'gfc_'; | |
public static $session = false; | |
public static $fresh_cookies = array(); | |
public static function init() { | |
//GFCookie::delete_cookies(); exit; | |
if(!is_admin()) { | |
add_action('parse_request', array('GFCookie', 'save_cookie'), 9); | |
add_action('gform_pre_render', array('GFCookie', 'load_cookie')); | |
} | |
add_shortcode('gf_cookied_link', array('GFCookie', 'cookied_link_shortcode')); | |
} | |
public static function save_cookie() { | |
// if cookies are not enabled, default to session use | |
// start the session regardless of $_GET being empty as session may have been previously populated | |
if(!self::cookies_enabled()) { | |
session_start(); | |
} | |
// don't do anything with cookie if there is no data | |
if(!empty($_GET)) | |
self::save_get_data(); | |
self::save_server_data(); | |
} | |
public static function load_cookie($form = null) { | |
$cookie = self::get_cookie_data(false); | |
foreach($cookie as $key => $value) { | |
// skip cookies that were not added by us | |
if(strpos($key, self::$cookie_prefix) === false) | |
continue; | |
// load cookie value into $_GET for population into Gravity Forms | |
$key = str_replace(self::$cookie_prefix, '', $key); | |
$_GET[$key] = $value; | |
} | |
// if gfc referrer is set, find {referer} merge tag and replace | |
if(self::get_cookie_data('HTTP_REFERER')) { | |
foreach($form['fields'] as &$field) { | |
if(strpos(rgar($field, 'defaultValue'), '{referer}') !== false) | |
$field['defaultValue'] = str_replace('{referer}', self::get_cookie_data('HTTP_REFERER'), $field['defaultValue']); | |
} | |
} | |
return $form; | |
} | |
public static function get_cookie_data($key = false) { | |
$session = isset($_SESSION) && is_array($_SESSION) ? $_SESSION : array(); | |
$cookie = self::cookies_enabled() ? $_COOKIE : $session; | |
// cookies that have not yet been sent through the header are merged with existing cookies | |
$cookie = array_merge($cookie, self::$fresh_cookies); | |
if($key === false) | |
return $cookie; | |
$gfc_key = self::$cookie_prefix . $key; | |
return rgar($cookie, $gfc_key); | |
} | |
public static function save_get_data() { | |
$reserved_words = array('attachment_id', 'author', 'author_name', 'calendar', 'cat', 'category', 'category__and', 'category__in', 'category__not_in', 'category_name', 'comments_per_page', 'comments_popup', 'cpage', 'day', 'debug', 'error', 'exact', 'feed', 'hour', 'link_category', 'm', 'minute', 'monthnum', 'more', 'name', 'nav_menu', 'nopaging', 'offset', 'order', 'orderby', 'p', 'page', 'page_id', 'paged', 'pagename', 'pb', 'perm', 'post', 'post__in', 'post__not_in', 'post_format', 'post_mime_type', 'post_status', 'post_tag', 'post_type', 'posts', 'posts_per_archive_page', 'posts_per_page', 'preview', 'robots', 's', 'search', 'second', 'sentence', 'showposts', 'static', 'subpost', 'subpost_id', 'tag', 'tag__and', 'tag__in', 'tag__not_in', 'tag_id', 'tag_slug__and', 'tag_slug__in', 'taxonomy', 'tb', 'term', 'type', 'w', 'withcomments', 'withoutcomments', 'year', 'DBGSESSID'); | |
foreach($_GET as $key => $value) { | |
if(!in_array($key, $reserved_words)) { | |
self::save_data($key, $value); | |
} | |
} | |
} | |
public static function save_server_data() { | |
$server_keys = array('HTTP_REFERER'); | |
foreach($_SERVER as $key => $value) { | |
if(in_array($key, $server_keys)) | |
self::save_data($key, $value); | |
} | |
} | |
public static function save_data($key, $value) { | |
$gfc_key = self::$cookie_prefix . $key; | |
if(self::cookies_enabled()) { | |
if(!rgar($_COOKIE, $gfc_key)) { | |
setcookie($gfc_key, $value, time() + 60*60*24*60, '/'); | |
self::$fresh_cookies[$gfc_key] = $value; | |
} | |
} else { | |
if(!rgar($_SESSION, $gfc_key)) { | |
$_SESSION[$gfc_key] = $value; | |
self::$fresh_cookies[$gfc_key] = $value; | |
} | |
} | |
} | |
/** | |
* WP uses cookies by default so if $_COOKIES global is empty, cookies are likely not enabled | |
* | |
*/ | |
public static function cookies_enabled() { | |
return !empty($_COOKIE); | |
} | |
public static function delete_cookies() { | |
foreach($_COOKIE as $key => $value) { | |
if(strpos($key, self::$cookie_prefix) !== false) { | |
self::delete_cookie($key); | |
} | |
} | |
} | |
public static function delete_cookie($key) { | |
setcookie($key, null, time() - 3600, '/'); | |
} | |
public static function cookied_link_shortcode($atts) { | |
extract( shortcode_atts( array( | |
'url' => false | |
), $atts ) ); | |
return !$url ? '' : gf_get_cookied_link($url); | |
} | |
} | |
GFCookie::init(); | |
if(!function_exists('gf_get_cookied_link')) { | |
function gf_get_cookied_link($url) { | |
$cookies = GFCookie::get_cookie_data(); | |
$gfc_cookies = array(); | |
foreach($cookies as $key => $value) { | |
if(strpos($key, GFCookie::$cookie_prefix) !== false) { | |
$key = str_replace('gfc_', '', $key); | |
$gfc_cookies[$key] = str_replace(array('http://', 'https://'), '', $value); | |
} | |
} | |
return add_query_arg($gfc_cookies, $url); | |
} | |
} |
Would love some basic direction on how to implement this.
Thank, works almost great out of the box! Saves me a lot of coding.
Found one issue: the $_COOKIE can be empty, not because cookies are disabled, but because the user is new and has received no cookies. The snippet starts a session and then fails to load from the session, because $_COOKIE is populated in the second page request. Then cookies are used instead of the session. The saved values in the session are thus not read...
Quick fix, just have cookies_enabled() return true. (if you know cookies are enabled on the server, which usually is)
Would love some basic direction on how to implement this.
Had some difficulty finding the easy way myself. Tip: caching can skip the execution of the snippet. So make sure there's no caching when URLs have GET params.
With FTP, create a new folder in the plugins folder (wp-includes/plugins/) and upload the snippet. The snippet appears in the plugin list in de WP admin. Activate it. When you create a folder, check the dynamic population and enter the query param that you need.
@mann1234 Glad you found this useful!
Really useful. Thanks!!!
@Cometeelcoco Glad to hear it! What'd you use it for?
Hi David. Can you tell me how to use this code?
@michaelcomceptum What are you trying to do? That might be a better place to start.
@Cometeelcoco Glad to hear it! What'd you use it for?
For Public relations people who refer people to my page and it was necessary that with a referral link a cookie would remain on the pc in case they later decided to make the purchase. Thank you very much for this great code. It also continues to work in Gravity Forms 2.5 in case anyone doubted it. A big hug.
@Cometeelcoco Fantastic!
What type of cache exclusion do you recommend for implementing this on a wpengine hosted website? This seems to only work when you're logged during my testing.
@adonnan Does WP Engine support exempting any cookie with a prefix? If so, exempting any cookie with the "gfc_" prefix should do the trick.
@spivurno That did the trick, thanks! I've been trying to figure that one out for a while.
Glad to help, @adonnan!
@taylor-onfido My pleasure!
Wow David, 10 years ago and still found useful your creations, like this one. One thing: how can implement a way to update the cookies created by query string when a new query string is created? I saw the part //GFCookie::delete_cookies(); exit; at beginning but how to use this function to clear the cokies created? Kind regards
@amar8105alva Glad you're still finding this useful!
You can call GFCookie::delete_cookies() anytime before content is being rendered to the screen to delete all cookies created by GFCookie. For example, you could do something like:
add_action( 'init', function() {
GFCookie::delete_cookies();
}, 9 );
@alexphelps: Refer to the Gravity Forms Documentation - https://www.gravityhelp.com/documentation/article/using-dynamic-population/