Skip to content

Instantly share code, notes, and snippets.

@goranefbl
Created July 14, 2024 21:07
Show Gist options
  • Select an option

  • Save goranefbl/30ff52d0758ab42852d9484721023d9e to your computer and use it in GitHub Desktop.

Select an option

Save goranefbl/30ff52d0758ab42852d9484721023d9e to your computer and use it in GitHub Desktop.
webcare plugin
<?php
function user_has_activation_key($user_id) {
global $wpdb;
$user = $wpdb->get_row($wpdb->prepare(
"SELECT user_activation_key FROM {$wpdb->users} WHERE ID = %d",
$user_id
));
return !empty($user->user_activation_key);
}
function newsletter_raf_link($user) {
$user = get_user_by('email', $user->email);
if (!$user) {
return home_url('/my-account/myreferrals/');
}
$user_id = $user->id;
// Check if the user has set a password
if (user_has_activation_key($user_id)) {
// Generate a magic link so user can set initial password.
$reset_key = get_password_reset_key( $user );
if ( ! is_wp_error( $reset_key ) ) {
$action = 'newaccount';
$set_password_url = wc_get_account_endpoint_url( 'lost-password' ) . "?action=$action&key=$reset_key&login=" . rawurlencode( $user->user_login ) . "&redirect_to=" . urlencode( home_url( '/my-account/myreferrals/' ) );
return $set_password_url;
} else {
// Handle error
return home_url('/my-account/myreferrals/');
}
} else {
// User has set a password
return home_url('/my-account/myreferrals/');
}
}
add_filter("newsletter_message", "filter_newsletter_message_raf_link", 10, 3);
function filter_newsletter_message_raf_link($message, $email, $user) {
$wsp_options = get_option('wsp_options');
if (isset($wsp_options['raf_code'])) {
$raf_code = $wsp_options['raf_code'];
if (is_object($message) && isset($message->body)) {
$raf_link = newsletter_raf_link($user);
$message->body = str_replace("{social_share}", $raf_link, $message->body);
}
}
return $message;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment