Shows how to add an email template tag that shows all the changes made on a Participants Database record update.
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 Edit Log Tag | |
* Description: provides a custom email tag that shows which fields were updated | |
* Version: 1.0 | |
* | |
*/ | |
class PDb_Record_Edit_Log_Tag { | |
/** | |
* @var array the pre-edit record | |
*/ | |
private $current_record; | |
/** | |
* @var array the change log | |
* | |
* as 'fieldname' => array( | |
* 'title' => $field_title, | |
* 'old_value' => $current_value, | |
* 'new_value' => $updated_value | |
* ) | |
*/ | |
private $change_log = array(); | |
/** | |
* sets up the filters/actions | |
*/ | |
public function __construct() | |
{ | |
add_filter( 'pdb-before_submit_update', array( $this, 'capture_stored_record' ), 1 ); | |
add_action( 'pdb-after_submit_update', array( $this, 'build_log_data' ), 1 ); | |
add_filter( 'pdb-template_email_tag_map', array( $this, 'add_edit_log_tag' ), 20, 2 ); | |
} | |
/** | |
* captures the stored record before updating | |
* | |
* @param array $post the posted data | |
* @return array | |
*/ | |
public function capture_stored_record( $post ) | |
{ | |
$this->current_record = Participants_Db::get_participant( filter_var( $post['id'], FILTER_SANITIZE_NUMBER_INT ) ); | |
return $post; | |
} | |
/** | |
* builds the change log data | |
* | |
* @param array $submission the submitted data | |
*/ | |
public function build_log_data( $submission ) | |
{ | |
$this->log_data($submission); | |
} | |
/** | |
* adds the change log tag to the tag list | |
* | |
* @param array $tag_list | |
* @param string $context | |
* @return array | |
*/ | |
public function add_edit_log_tag( $tag_list, $context ) { | |
$tag_list['change_log'] = $this->printable_log(); | |
return $tag_list; | |
} | |
/** | |
* provides the log data | |
* | |
* @param array $submission the submitted record data | |
*/ | |
private function log_data( $submission ) | |
{ | |
foreach ( $submission as $fieldname => $value ) { | |
if ( isset( $this->current_record[$fieldname] ) && $value != $this->current_record[$fieldname] ) { | |
$field = new PDb_Form_Field_Def( $fieldname ); | |
$this->change_log[$fieldname] = array( | |
'title' => $field->title(), | |
'old_value' => $this->display_string( $this->current_record[$fieldname] ), | |
'new_value' => $this->display_string( $value ), | |
); | |
} | |
} | |
} | |
/** | |
* provides the change log in printable form | |
* | |
* @return string HTML | |
*/ | |
private function printable_log() | |
{ | |
$print = array(); | |
if ( count( $this->change_log ) > 0 ) { | |
// set up the table header | |
$print[] = '<table>'; | |
$print[] = '<tr><th>Field</th><th>Old Value</th><th>New Value</th></tr>'; | |
foreach( $this->change_log as $line ) { | |
$print[] = vsprintf( '<tr><td>%s</td><td>%s</td><td>%s</td></tr>', $line ); | |
} | |
$print[] = '</table>'; | |
} | |
return implode( PHP_EOL, $print ); | |
} | |
/** | |
* converts an array to a string if needed | |
* | |
* @param array|string $input | |
* @return string output | |
*/ | |
private function display_string( $input ) | |
{ | |
$output = $input; | |
if ( is_array( maybe_unserialize( $input ) ) ) { | |
$output = implode( ', ', maybe_unserialize( $input ) ); | |
} | |
return $output; | |
} | |
} | |
new PDb_Record_Edit_Log_Tag(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
You can include this tag in your email template with:
[change_log]
It will show a table listing all the changes made to a record when updated.