Last active
November 15, 2021 15:11
-
-
Save verygoodplugins/9678da825bd776cd6eff802ce625bdfc to your computer and use it in GitHub Desktop.
Prefix the WPF contact ID and tags meta keys with the current blog prefix
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 | |
/** | |
* When the user's contact ID or tags are saved, prefix the usermeta key with | |
* the blog prefix of the current blog. | |
* | |
* @param bool $check Whether or not to bypass the original database | |
* check. | |
* @param int $user_id The user ID. | |
* @param string $meta_key The meta key. | |
* @param mixed $meta_value The meta value. | |
* @return bool|null Whether or not to bypass the database check. | |
*/ | |
function wpf_multisite_update_metadata( $check, $user_id, $meta_key, $meta_value ) { | |
if ( ! function_exists( 'wp_fusion' ) ) { | |
return $check; | |
} | |
if ( wp_fusion()->crm->slug . '_contact_id' === $meta_key || wp_fusion()->crm->slug . '_tags' === $meta_key ) { | |
global $wpdb; | |
$meta_key = $wpdb->get_blog_prefix() . $meta_key; | |
// Appends the blog prefix, like wp_1_activecampaign_tags. | |
update_user_meta( $user_id, $meta_key, $meta_value ); | |
return true; // returning anything other than null stops the original key from being updated. | |
} | |
return $check; | |
} | |
add_filter( 'update_user_metadata', 'wpf_multisite_update_metadata', 10, 4 ); | |
/** | |
* When the user's contact ID or tags are loaded, prefix the usermeta key with | |
* the blog prefix of the current blog. | |
* | |
* @param bool $check Whether or not to bypass the original database check. | |
* @param int $user_id The user ID. | |
* @param string $meta_key The meta key. | |
* @param mixed $single Whether to return a single result or array. | |
* @return bool|null Whether or not to bypass the database check. | |
*/ | |
function wpf_multisite_get_metadata( $check, $user_id, $meta_key, $single ) { | |
if ( ! function_exists( 'wp_fusion' ) ) { | |
return $check; | |
} | |
if ( wp_fusion()->crm->slug . '_contact_id' === $meta_key || wp_fusion()->crm->slug . '_tags' === $meta_key ) { | |
global $wpdb; | |
$meta_key = $wpdb->get_blog_prefix() . $meta_key; | |
// Appends the blog prefix, like wp_1_activecampaign_tags. | |
$result = get_user_meta( $user_id, $meta_key, true ); | |
if ( empty( $result ) ) { | |
// If it's not found at the new key, maybe it's at the old one, so | |
// we'll let things proceed as before. | |
return null; | |
} | |
if ( $single ) { | |
$result = array( $result ); | |
} | |
return $result; // returning anything other than null stops the original key from being updated. | |
} | |
return $check; | |
} | |
add_filter( 'get_user_metadata', 'wpf_multisite_get_metadata', 10, 4 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment