Last active
October 19, 2021 02:05
-
-
Save xnau/40ff2abd35fb856e3d06dbb4bdbc7169 to your computer and use it in GitHub Desktop.
Shows how to set up a switch where a user can delete their own record
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 Record Delete Switch | |
* Description: Gives users the ability to delete their own record | |
* Version: 1.3 | |
*/ | |
class PDb_Record_Delete_Switch { | |
/** | |
* @var string name of the delete field | |
* | |
* change this to match the field you are using to let the user delete their | |
* record. It should be a simple checkbox with a single value: yes | |
*/ | |
private $delete_switch_field_name = 'delete_record'; | |
/** | |
* @var array the participant record | |
*/ | |
private $participant_record; | |
/** | |
* @var string name of the transient for saving the feedback message | |
*/ | |
const message = 'pdb-user_delete'; | |
/** | |
* sets up the filter | |
*/ | |
public function __construct() | |
{ | |
// this gives us the record after it has been updated | |
add_action( 'pdb-after_submit_update', array($this, 'check_for_delete') ); | |
// register an event for this so that a confirmation email may be sent | |
add_filter( 'pdb-register_global_event', array($this, 'register_events'), 30 ); | |
add_action( 'pdb-shortcode_set', array( $this, 'check_feedback' ) ); | |
} | |
/** | |
* checks the submission for the delete signal | |
* | |
* @param array $participant_record the record values | |
* | |
*/ | |
public function check_for_delete( $participant_record ) | |
{ | |
if ( !is_admin() ) { // this can't be done from the admin | |
if ( isset( $participant_record[$this->delete_switch_field_name] ) && $participant_record[$this->delete_switch_field_name] === 'yes' ) { | |
$this->participant_record = $participant_record; | |
$this->process_deletion(); | |
} | |
} | |
} | |
/** | |
* processes the record deletion | |
* | |
* @global wpdb $wpdb | |
*/ | |
private function process_deletion() | |
{ | |
global $wpdb; | |
$sql = 'DELETE FROM ' . Participants_Db::$participants_table . ' WHERE `id` = %d'; | |
$success = $wpdb->query( $wpdb->prepare( $sql, $this->participant_record['id'] ) ); | |
// if we're debugging, dump the query to the log | |
Participants_Db::debug_log( __METHOD__ . ' deleting a record with query: ' . $wpdb->last_query ); | |
if ( $success > 0 ) { | |
Participants_Db::$session->clear('pdbid'); | |
PDb_Participant_Cache::clear_cache($this->participant_record['id']); | |
/* | |
* store the feedback message to show when the page refreshes | |
*/ | |
set_transient( self::message, 'Your record has been deleted.' ); | |
/** | |
* trigger the deletion action | |
* | |
* @action pdb-record-delete-switch_record_deleted | |
* @param array the record data from the deleted record | |
*/ | |
do_action( 'pdb-record-delete-switch_record_deleted', $this->participant_record ); | |
} | |
} | |
/** | |
* registers the deletion event | |
* | |
* @param array $evetns | |
* @return array as $tag => $title | |
*/ | |
public function register_events( $events ) | |
{ | |
$events['pdb-record-delete-switch_record_deleted'] = __( 'User Record Delete' ); | |
return $events; | |
} | |
/** | |
* checks for the feedback message | |
* | |
* here, we look for the stored feedback message and show it if it is found | |
* | |
* @param PDb_Record $record | |
*/ | |
public function check_feedback( $record ) | |
{ | |
$message = get_transient( self::message ); | |
if ( $message ) { | |
add_filter( 'pdb-no_record_error_message_setting_value', function () use ( $message ) { | |
return $message; | |
}, 50 ); | |
add_filter( 'pdb-update_thanks_message', function () use ( $message ) { | |
return $message; | |
}, 50 ); | |
delete_transient( self::message ); | |
} | |
} | |
} | |
new PDb_Record_Delete_Switch(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment