Custom Protect Plugin: Auto-Generate a Custom Field after registration

Discussion in 'Customization & add-ons' started by rzen, Dec 15, 2010.

  1. rzen

    rzen New Member

    Joined:
    Aug 7, 2010
    Messages:
    6
    Hello! I'm trying to write a simple plugin that will populate a custom field (named "license") based on input from another field (e-mail). I've created the field within aMember Admin and created the plugin by duplicating the sample plugin.

    Here is an example of my function hooked into user updates:
    PHP:
    // this function will be called when a member profile is updated. 
    function serialgen_updated($member_id$oldmember$newmember) {
        global 
    $db;
        
    $u $db->get_user($member_id);
          if (!
    $u['data']['license']) { $u['data']['license'] = md5($u['email']); }
        
    $db->update_user($u['member_id'], $u);
    }
    setup_plugin_hook('subscription_updated''serialgen_updated');
    I have a similar function for new users and DB rebuilds.

    I unfortunately face two problems with this: 1) Fatal error: Allowed memory size of ### bytes exhausted. (no matter how much i've set for the cap). 2) no matter what I set $u['data']['license'] to it always comes back empty in the administration.

    Ass soon as i drop the $db->update_user() line the error goes away, but then I'm obviously not returning any updates.

    I'm open to any and all suggestions. Thanks!
  2. alexander

    alexander Administrator Staff Member

    Joined:
    Jan 8, 2003
    Messages:
    6,279
    This will not work because your plugin hook will be executed from $db->update_user so there will be endless loop.
    You should move your code to finish_waiting_payment hook. So it will be executed only after signup.
  3. rzen

    rzen New Member

    Joined:
    Aug 7, 2010
    Messages:
    6
    Alexander, thanks for the quick reply! Changing hooks worked like a charm.

    I also discovered that i needed to change from $u[data][license] to just $u[license] and that the vars passed through the function at this new hook were $payment_id and $member_id. So below is the working function:

    PHP:
    // this function will be called when a member payment has been made. 
    function serialgen_updated($payment_id$member_id) {
        global 
    $db;
        
    $u $db->get_user($member_id);
         if (
    $u['license'] == '') { $u['license'] = md5($u['email']); }
        
    $db->update_user($u['member_id'], $u);
    }
    setup_plugin_hook('finish_waiting_payment''serialgen_updated');
    Now all I need is the proper way to hook this into subscription_rebuild so that I can provide all my users with keys in one swift motion. Below is what I have, which is obviously incorrect.

    PHP:
    // Called when database is rebuilt
    function serialgen_rebuild(&$members) {
        global 
    $db;
        
        foreach( 
    $members as $member ) {
            
    $u $db->get_user($member['member_id']);
            if ( 
    $u['license'] == '' || !$u['license'] ) { $u['license'] = md5($u['email']); }
            
    $db->update_user($u['member_id'], $u);
        }
    }
    setup_plugin_hook('subscription_rebuild''serialgen_rebuild');
    Any insight would be most appreciated!
  4. alexander

    alexander Administrator Staff Member

    Joined:
    Jan 8, 2003
    Messages:
    6,279
    Why you think that it is not correct?
    That code should work without a problem.
  5. rzen

    rzen New Member

    Joined:
    Aug 7, 2010
    Messages:
    6
    I think it's not correct because it doesn't work :)

    Could you look it over a bit more closely and help me see if I'm making any rookie mistakes? Or tell me if there is some way I can control the output during a db rebuild and see some vardump's along the way?
  6. alexander

    alexander Administrator Staff Member

    Joined:
    Jan 8, 2003
    Messages:
    6,279
    I don't see any errors, but remember that code will be executed for active users only.
    If you want to run it for all users, better to not use rebuild hook and just create standalone script that will update database directly.
    I regards to output, install firebug into firefox, you will be able to check what is being sent and received by ajax requests. So will be able to place debug info.
  7. rzen

    rzen New Member

    Joined:
    Aug 7, 2010
    Messages:
    6
    Well, I don't know what it is... I temporarily modded /admin/rebuild.php's Rebuild function directly, ran a rebuild, and all my users now have licenses. Maybe it has to do with the data that's coming in via the $members variable? My suspicion is that it's not actually coming through with 'member_id' in the array. Could you confirm?

    At any rate, I accomplished what I needed and now my plugin only really needs to hook into finish_waiting_payment.
  8. alexander

    alexander Administrator Staff Member

    Joined:
    Jan 8, 2003
    Messages:
    6,279
    Well you right, I forgot. $members array does not have member_id. It have only login and array of active products.
  9. endortrails

    endortrails Guest

    Joined:
    Feb 2, 2009
    Messages:
    14
    Unable to generate license key fields for existing users

    I'm in need of something similar as rzen above. I need to generate license keys for software activation after a user signs up for a paid product. We have successfully added a new license key for each NEW account, but, we have been unable to generate license keys for EXISTING accounts using the subscription_rebuild hook nor have we been able to DELETE license keys using the subscription_deleted hook. Here is our code:
    PHP:
    // Generate a unique KEY for each user who has purchased a subscription
    // Called when database is rebuilt 
    function gpp_member_api_key_rebuild(&$members) { 
        global 
    $db
        
        foreach( 
    $members as $user ) { 
            
    $u $db->get_user($user['member_id']); 
            if ( 
    $u[data]['license'] == '' || !$u[data]['license'] ) { $u[data]['license'] = md5($u['email']); } 
            
    $db->update_user($u['member_id'], $u); 
        } 

    setup_plugin_hook('subscription_rebuild''gpp_member_api_key_rebuild');

    // Deletes the unique KEY if a user doesn't have an active subscription
    function gpp_member_api_key_deleted($payment_id$member_id) {
        global 
    $db;
        
    $u $db->get_user($member_id); 
        if (
    $u[data]['license'] != '') { $u[data]['license'] == ''; }
        
    $db->update_user($u['member_id'], $u); 
    }
    setup_plugin_hook('subscription_deleted''gpp_member_api_key_deleted');  
    I've corresponded with Anton in HelpDesk, but none of his suggestions have worked thus far. This is the very last step we need to complete before we can release our new products. The HelpDesk reference number is: YAR-27155-674

    Any idea how we can get this working?
  10. maharzan

    maharzan New Member

    Joined:
    Mar 31, 2011
    Messages:
    2
    There is no member_id in subscription_rebuild. Here is a script that I made this work. This is generic and applies to all members. It will need to filter particular product like paid members, etc.

    PHP:
    function my_member_api_key_rebuild($members){
        global 
    $db;
        foreach (
    $members as $login => $rec) {
           
    $u $db->users_find_by_string($login,'login',1);
           
    $u $u[0];
           if (
    $u[data]['license'] == '' || !$u[data]['license']) { $u[data]['license'] = md5($u['email']); }
           
    $db->update_user($u['member_id'], $u);
        }
    }
    setup_plugin_hook('subscription_rebuild''my_member_api_key_rebuild');
    Hope this helps all who are looking for a solution. The aMember Pro documentation really poor as it doesn't give examples on how to use the hooks.
  11. maharzan

    maharzan New Member

    Joined:
    Mar 31, 2011
    Messages:
    2
    and heres the subscription_deleted code

    PHP:
    function my_member_api_key_deleted($member_id,$product_id,$member) {
        global 
    $db;
        
    $member['data']['license'] = ''
        
    $db->update_user($member['member_id'], $member); 
    }
    setup_plugin_hook('subscription_deleted''my_member_api_key_deleted'); 
    Again, this is generic.. will need to check conditions such as kind of products..

Share This Page