Created
September 7, 2024 15:59
-
-
Save goranefbl/e45d9fc37b3c8530d73e2980d4469fd7 to your computer and use it in GitHub Desktop.
Save utm to user then order
This file contains 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
<?php | |
// Hook to save user meta when a user registers | |
add_action('user_register', 'save_source_tracker_user_meta', 10, 1); | |
function save_source_tracker_user_meta($user_id) { | |
$disabled = get_option('wpgens_st_disable'); | |
if ($disabled) { | |
return; | |
} | |
$last_touch = isset($_COOKIE['wpg_last']) ? json_decode(stripslashes($_COOKIE['wpg_last']), true) : null; | |
$first_touch = isset($_COOKIE['wpg_first']) ? json_decode(stripslashes($_COOKIE['wpg_first']), true) : null; | |
$unique_id = isset($_COOKIE['wpg_id']) ? sanitize_text_field($_COOKIE['wpg_id']) : null; | |
if (!$last_touch && !$first_touch) { | |
return; | |
} | |
$data_to_save = WPGens_ST\Includes\WPGens_ST_Config::getDataToSave(); | |
if ($unique_id) { | |
update_user_meta($user_id, '_wpg_id', $unique_id); | |
} | |
if ($last_touch) { | |
$last_touch = sanitize_array($last_touch); | |
update_user_meta($user_id, 'wpg_last', $last_touch); | |
foreach ($data_to_save as $key) { | |
if (isset($last_touch[$key]) && $last_touch[$key] !== '') { | |
update_user_meta($user_id, '_wpg_last_' . $key, $last_touch[$key]); | |
} | |
} | |
// Extract and save click identifiers from the URL | |
if (isset($last_touch['url'])) { | |
$url_components = parse_url($last_touch['url']); | |
if (isset($url_components['query'])) { | |
parse_str($url_components['query'], $params); | |
$click_ids = ['gclid', 'fbclid', 'msclkid']; | |
foreach ($click_ids as $id) { | |
if (isset($params[$id])) { | |
update_user_meta($user_id, '_wpg_last_' . $id, sanitize_text_field($params[$id])); | |
} | |
} | |
} | |
} | |
} | |
if ($first_touch) { | |
$first_touch = sanitize_array($first_touch); | |
update_user_meta($user_id, 'wpg_first', $first_touch); | |
foreach ($data_to_save as $key) { | |
if (isset($first_touch[$key]) && $first_touch[$key] !== '') { | |
update_user_meta($user_id, '_wpg_first_' . $key, $first_touch[$key]); | |
} | |
} | |
// Extract and save click identifiers from the URL | |
if (isset($first_touch['url'])) { | |
$url_components = parse_url($first_touch['url']); | |
if (isset($url_components['query'])) { | |
parse_str($url_components['query'], $params); | |
$click_ids = ['gclid', 'fbclid', 'msclkid']; | |
foreach ($click_ids as $id) { | |
if (isset($params[$id])) { | |
update_user_meta($user_id, '_wpg_first_' . $id, sanitize_text_field($params[$id])); | |
} | |
} | |
} | |
} | |
} | |
} | |
// Hook to transfer user meta to order meta when an order is created | |
add_action('woocommerce_new_order', 'transfer_source_tracker_to_order', 10, 2); | |
function transfer_source_tracker_to_order($order_id, $order) { | |
$user_id = $order->get_user_id(); | |
if (!$user_id) { | |
return; | |
} | |
$meta_keys = [ | |
'_wpg_id', | |
'wpg_last', | |
'wpg_first', | |
]; | |
$data_to_save = WPGens_ST\Includes\WPGens_ST_Config::getDataToSave(); | |
foreach ($data_to_save as $key) { | |
$meta_keys[] = '_wpg_last_' . $key; | |
$meta_keys[] = '_wpg_first_' . $key; | |
} | |
$click_ids = ['gclid', 'fbclid', 'msclkid']; | |
foreach ($click_ids as $id) { | |
$meta_keys[] = '_wpg_last_' . $id; | |
$meta_keys[] = '_wpg_first_' . $id; | |
} | |
foreach ($meta_keys as $key) { | |
$value = get_user_meta($user_id, $key, true); | |
if ($value) { | |
$order->update_meta_data($key, $value); | |
} | |
} | |
$order->save(); | |
} | |
// Helper function to sanitize arrays | |
function sanitize_array($array) { | |
foreach ($array as $key => &$value) { | |
if (is_array($value)) { | |
$value = sanitize_array($value); | |
} else { | |
$value = sanitize_text_field($value); | |
} | |
} | |
return $array; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment