Demonstrates a way to generate and provide a unique ID for new signups and new records in Participants Database
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Plugin Name: PDB Generate Member ID | |
* Description: provides a unique ID for new signups and new records in Participants Database | |
* Version: 2.1 | |
*/ | |
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 ) ); | |
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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment