Skip to content

Instantly share code, notes, and snippets.

@xnau
Last active October 10, 2023 07:00
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save xnau/2409c27840f89813115a85db3a8ed0b4 to your computer and use it in GitHub Desktop.
Save xnau/2409c27840f89813115a85db3a8ed0b4 to your computer and use it in GitHub Desktop.
Demonstrates a way to generate and provide a unique ID for new signups and new records in Participants Database
<?php
/**
* Plugin Name: PDB Generate Member ID
* Description: provides a unique ID for new signups and new records in Participants Database
* Version: 2.2
*/
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 record that has no defined ID is updated
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 )
{
if ( ! isset( $record['id'] ) )
{
return false;
}
$saved_record = Participants_Db::get_participant( $record['id'] );
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 $member_id the member ID to save
*/
private function store_member_id( $record_id, $member_id )
{
global $wpdb;
$wpdb->update( Participants_Db::$participants_table, array( $this->id_field => $member_id ), array( 'id' => $record_id ) );
PDb_Participant_Cache::clear_cache( $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
@xnau
Copy link
Author

xnau commented Oct 10, 2023

Be sure to edit the line where the name of the member ID field is defined, this must match the name of the field you will be using.

If you want to use this plugin to generate a random identifier (which could be a kind of password) you can change the code in the generate_new_member_id() method at line 117. In the example, this is simply incremented from the last one, you could instead generate a random string of some kind. The code checks to make sure it is unique.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment