Viewing 5 posts - 1 through 5 (of 5 total)
  • Author
    Posts
  • #1183955

    Hi,

    We have a possibly theme-related problem. We’re having issues with confirmation mail that is sent after registration. On site, we don’t need any user interaction, no likes, comments, no user posts. Reason for registration is solely for license key generation that is used in our software, that is taken care of. We use wp-members for the registration form.

    To be as brief as possible, when the user registers, he gets an email containing [link1] It used to work, but we updated theme after several versions of not updating it and now the link in emails comes like this [link2]. Weirdly enough, if you click on that link, you get logged in as a random user, so it seems that the key generation has been messed up in the update. We looked at code and issue seems to be here:

    /var/www/html/wp-content/themes/enfold-child/functions.php

    
    /* Customize the login process - send activation link instead of password */function my_activation_key_settings() {
        $settings = array(
            'email_text'      => 'Please activate your account: ',
            'return_url'      => 'https://frinx.io/user-account-2',
            'send_welcome'    => true,
            'show_success'    => true,
            'success_message' => 'Thank you for activating your account.',
            'send_notify'     => true,
        );
        return $settings;
    }/**
     * Create an activation key for the
     * user at registration.
     */
    add_action( 'wpmem_post_register_data', 'my_generate_key' );
    function my_generate_key( $fields ) {
         // Generate a random key.
        $key = sha1( rand() );
        echo $key;
        // Save this for the new user account.
        add_user_meta( $fields['ID'], 'activation_key', $key, true );
    }/**
     * Include the activation key in the new user
     * registration email as an activation link.
     */
    add_filter( 'wpmem_email_filter', 'my_add_key_to_email', 10, 3 );
    function my_add_key_to_email( $arr, $wpmem_fields, $field_data ) {
        $settings = my_activation_key_settings();
        $url = trailingslashit( $settings['return_url'] );
        // Only do this for new registrations.
        if ( $arr['toggle'] == 'newmod' ) {
            // Get the stored key.
            $key = get_user_meta( $arr['user_id'], 'activation_key', true );
            $name = $arr['user_login'];        // Add text and link to the email body.
            $greeting = "Hi ${name}! \n" . "Thank you for your interest in FRINX and for registering on our web page.\n\n";        // Concatenate so that the activation link is at the beginning of the activation email.
              $sprava = $greeting . $settings['email_text'] . add_query_arg( 'activate=', $key, $url ) . "\n\n" . $arr['body'];
            $arr['body'] = $sprava;
            echo "arr" . $sprava;
            echo "arr body" . $arr['body'];
        }    return $arr;
    }/**
     * Check for an activation key and if one exists,
     * validate and log in user.
     */
    add_action( 'template_redirect', 'my_validate_key' );
    function my_validate_key(){
        $settings = my_activation_key_settings();
        // Check for activation key.
        if ( isset( $_GET['activate'] ) ) {
            // Get the user account the key is for.
            $users = get_users( array(
                'meta_key'    => 'activation_key',
                'meta_value'  => $_GET['activate'],
                'number'      => 1,
                'count_total' => false
            ) );
            if ( $users ) {
                foreach( $users as $user ) {
                    // The provided activation key was valid, log in.
                    wp_set_auth_cookie( $user->ID, true );
                    wp_set_current_user( $user->ID );                // Delete activation_key meta and set active.
                    delete_user_meta( $user->ID, 'activation_key' );
                    update_user_meta( $user->ID, 'active', '1' );                if ( $settings['send_welcome'] ) {
                        // Send a welcome email
                        include_once( WPMEM_PATH . 'inc/email.php' );
                        wpmem_inc_regemail( $user->ID, '', 2 );
                    }
                    if ( $settings['send_notify'] ) {
                        // Send a welcome email
                        global $wpmem;
                        include_once( WPMEM_PATH . 'inc/email.php' );
                        wpmem_notify_admin( $user->ID, $wpmem->fields );
                    }
                    break;
                }
            }
        }
    }
    

    the problem occures mostly when getting key from metadata tried to debug if metadata was added but this piece of code works fine

    
        add_user_meta( $fields['ID'], 'activation_key', $key, true);
    

    but after retrieving key using

    
    $key = get_user_meta( $arr['user_id'],'activation_key, true)
    

    the key is not returned

    when trying to use diffrent kind of functions to retrieve the array of metadata then critical error occure so it was hard to say what went wrong or try to do it another way

    Function add_user_meta is stored correctly(due to documentation function is returning meta_id), but when we are retrieving user metadata under the parameters and user_id how we stored it , empty field is being returned. The activation key is not stored properly in user metadata, so it is lost in between its creating and outputting function.

    Thanks for any tips

    • This topic was modified 5 years, 3 months ago by Frinxio. Reason: additional info
    #1185511

    Hey Frinxio,

    Thank you for the inquiry.

    The links above (link1, link2) are not available. Please add it again so that we can understand what you’re referring to. Have you tried enabling the debug mode?

    add_user_meta( $fields[‘ID’], ‘activation_key’, $key, true);
    but after retrieving key using
    $key = get_user_meta( $arr[‘user_id’],’activation_key, true)

    Are the values of the $fields[‘ID’] and $arr[‘user_id’] the same?

    UPDATE: We missed the links in the private field.

    Best regards,
    Ismael

    #1185661

    Hi Ismael ,
    yes , values of the $fields[‘ID’] and $arr[‘user_id’] are the same thats what i ve checked in the first place,
    later I found out that function of my_generate_key() is executed after my_add_key_to_email() despite the hooks that are declared , so the key is generated after the email is sent and it is not available in user metadata

    #1185747

    i also fix of the notices in debug mode but it didn’t help my problem in any way , the function is still exectued first

    • This reply was modified 5 years, 3 months ago by Frinxio.
    #1186582

    Hi,

    Thank you for the update.

    later I found out that function of my_generate_key() is executed after my_add_key_to_email()

    Have you tried adjusting the priority of the hooks so that the function my_generate_key is executed before my_add_key_to_email?

    // https://developer.wordpress.org/plugins/hooks/actions/#priority

    Set the priority value of the my_generate_key to 10 and the other function to 20.

    Best regards,
    Ismael

Viewing 5 posts - 1 through 5 (of 5 total)
  • You must be logged in to reply to this topic.