Jump to content

Membership ID generation


dougdavis22

Recommended Posts

Hi all. I have some code that was provided to me my a Wordpress plugin developer that generate membership IDs for members in a database. However, I need to change the format of the membership ID as I don’t want it to just be 4 digits. The current code is as follows:

<?php

/**
 * Plugin Name: PDB Generate Member ID
 * Description: provides a unique ID for new signups and new records in Participants Database
 * Version: 2.0
 */
class pdb_generate_member_id {

  /**
   * @var string name of the member id field
   */
  private $id_field = 'member_id';

  /**
   * @var string the starting member ID value
   */
  private $start_value = 1000;

  /**
   * initializes the plugin
   */
  public function __construct()
  {
    // this is used when a signup form is submitted
    add_filter( 'pdb-after_submit_signup', array($this, 'maybe_save_member_id') );
    
    // this is used when a new record is added from the backend
    add_filter( 'pdb-after_submit_add', array($this, 'maybe_save_member_id') );

    // this is used when a new record is added from the backend
    add_filter( 'pdb-after_submit_update', array($this, 'maybe_save_member_id') );
  }
  
  /**
   * saves the new member ID
   * 
   * this is called after the record is added or updated
   * 
   * @param array $record the submitted data
   */
  public function maybe_save_member_id( $record )
  {
    if ( $this->record_needs_id( $record ) ) {
      $this->store_member_id( $record['id'], $this->generate_new_member_id() );
    }
  }
  
  /**
   * checks the record to see if it needs a member ID assigned
   * 
   * @param array $record the incoming record values
   * @return bool true if the record needs an ID
   */
  private function record_needs_id( $record )
  {
    $saved_record = Participants_Db::get_participant( $record['id'] );
    $field_def = Participants_Db::$fields[$this->id_field];
    
    return ! isset( $saved_record[$this->id_field] ) || empty( $saved_record[$this->id_field] ) || $saved_record[$this->id_field] === $field_def->default_value();
  }
  
  /**
   * saves the member ID to the database
   * 
   * @global wpdb $spdb
   * 
   * @param int $record_id
   * @param string $id the member ID to save
   */
  private function store_member_id( $record_id, $id )
  {
    global $wpdb;
    $wpdb->update( Participants_Db::$participants_table, array( $this->id_field => $id ), array( 'id' => $record_id ) );
  }
  
  

  /**
   * generates the new ID
   * 
   * 
   * @return string the new member ID
   */
  private function generate_new_member_id()
  {

    $last_id = $this->last_saved_member_id();

    if ( !$last_id ) {
      // no ids have been saved yet, so we start here
      $last_id = $this->start_value;
    }

    /*
     *  we start the process with the previous ID
     */
    $new_id = $last_id;

    /*
     * this will keep looping as long as the ID we are trying is not unique
     * 
     * as soon as we try a unique one, it will break out of the loop
     */
    while ( !$this->member_id_is_unique( $new_id ) ) {

      /*
       * this is where we generate the new ID
       * 
       * you could make up your own formula here, for this example we simply increment the value
       */
      $new_id = $new_id + 1;
    }
    
    return $new_id;
  }

  /**
   * provides the last entered ID
   * 
   * @global wpdb $wpdb
   */
  private function last_saved_member_id()
  {
    global $wpdb;
    $sql = 'SELECT `' . $this->id_field . '` FROM ' . Participants_Db::$participants_table . ' ORDER BY `date_recorded` DESC LIMIT 1';
    return $wpdb->get_var( $sql );
  }

  /**
   * checks for a duplicate ID
   * 
   * @global wodb $wpdb
   * @param string $member_id the id value
   * @return bool true if the value is unique
   */
  private function member_id_is_unique( $member_id )
  {
    global $wpdb;
    $sql = 'SELECT `id` FROM ' . Participants_Db::$participants_table . ' WHERE ' . $this->id_field . ' = "%s"';
    $result = $wpdb->get_col( $wpdb->prepare( $sql, $member_id ) );
    return $wpdb->num_rows === 0;
  }

}

new pdb_generate_member_id(); // instantiates the class

What I’m after is a member_id that is comprised of the first 4 characters from the last_name field and then add the 4-digit code to that. Apparently it’s easy to do, but the developer won’t do it for me. He did however offer this advice:

Quote

When the maybe_save_member_id method is called by the filter, the contents of the record is passed in. You can pass that info along to the generate_new_member_id method so you can use that info to construct your ID.

Any help really, really appreciated. Thanks, Doug.

Link to comment
Share on other sites

Every time I see WordPress code I have to take a shower to wash all the disgusting off me.

The easiest way to accomplish what you want is to not do that.
If you want the member ID to look like four letters from the last name (what if their last name is less than that?) and the number then show the last four letters and the number. Make a function or class or whatever that can take a member and their ID and give you the string you want.

But here's the thing. You cannot use that last four + ID as a real identifier. It will cause lots of problems. You can show it to people but under the hood you should always deal with just the member ID.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.