Log: Add a type prop to entries

This commit is contained in:
Corey McKrill 2019-10-28 11:51:22 -07:00
parent 0b80eb75d2
commit a41ecd7665
No known key found for this signature in database
GPG key ID: C2C0746F7BF17E38

View file

@ -50,11 +50,18 @@ function render_log_meta_box( $pledge ) {
/** /**
* Defaults for a log entry. * Defaults for a log entry.
* *
* @return array * @return array {
* @type int $timestamp Time of the event.
* @type string $type The type of event. A snake_case or kebab-case string.
* @type string $message Description of the event.
* @type array $data Details and data related to the event.
* @type int $user_id The ID of the logged in user who triggered the event, if applicable.
* }
*/ */
function get_log_entry_template() { function get_log_entry_template() {
return array( return array(
'timestamp' => time(), 'timestamp' => time(),
'type' => '',
'message' => '', 'message' => '',
'data' => array(), 'data' => array(),
'user_id' => 0, 'user_id' => 0,
@ -90,15 +97,17 @@ function get_pledge_log( $pledge_id ) {
* Add a new log entry for a particular pledge. * Add a new log entry for a particular pledge.
* *
* @param int $pledge_id * @param int $pledge_id
* @param string $type
* @param string $message * @param string $message
* @param array $data * @param array $data
* @param int $user_id * @param int $user_id
* *
* @return void * @return void
*/ */
function add_log_entry( $pledge_id, $message, array $data = array(), $user_id = 0 ) { function add_log_entry( $pledge_id, $type, $message, array $data = array(), $user_id = 0 ) {
$entry = get_log_entry_template(); $entry = get_log_entry_template();
$entry['type'] = $type;
$entry['message'] = $message; $entry['message'] = $message;
$entry['data'] = $data; $entry['data'] = $data;
$entry['user_id'] = $user_id; $entry['user_id'] = $user_id;
@ -121,6 +130,7 @@ function capture_save_post( $post_ID, $post, $update ) {
if ( false === $update ) { if ( false === $update ) {
add_log_entry( add_log_entry(
$post_ID, $post_ID,
'pledge_created',
sprintf( sprintf(
'Pledge created. Status set to <code>%s</code>.', 'Pledge created. Status set to <code>%s</code>.',
esc_html( get_post_status( $post_ID ) ) esc_html( get_post_status( $post_ID ) )
@ -154,6 +164,7 @@ function capture_updated_postmeta( $meta_id, $object_id, $meta_key, $meta_value
if ( in_array( $trimmed_meta_key, $valid_keys, true ) ) { if ( in_array( $trimmed_meta_key, $valid_keys, true ) ) {
add_log_entry( add_log_entry(
$object_id, $object_id,
'pledge_data_changed',
sprintf( sprintf(
'Changed <code>%1$s</code> to <code>%2$s</code>.', 'Changed <code>%1$s</code> to <code>%2$s</code>.',
esc_html( $trimmed_meta_key ), esc_html( $trimmed_meta_key ),
@ -189,6 +200,7 @@ function capture_added_post_meta( $meta_id, $object_id, $meta_key, $meta_value )
if ( true === $meta_value ) { if ( true === $meta_value ) {
add_log_entry( add_log_entry(
$object_id, $object_id,
'pledge_email_confirmed',
'Pledge email address confirmed.', 'Pledge email address confirmed.',
array( array(
'email' => get_post_meta( $object_id, PledgeMeta\META_PREFIX . 'org-pledge-email', true ) 'email' => get_post_meta( $object_id, PledgeMeta\META_PREFIX . 'org-pledge-email', true )
@ -212,6 +224,7 @@ function capture_transition_post_status( $new_status, $old_status, WP_Post $post
add_log_entry( add_log_entry(
$post->ID, $post->ID,
'pledge_status_changed',
sprintf( sprintf(
'Pledge status changed from <code>%1$s</code> to <code>%2$s</code>.', 'Pledge status changed from <code>%1$s</code> to <code>%2$s</code>.',
esc_html( $old_status ), esc_html( $old_status ),