diff --git a/plugins/wporg-5ftf/includes/pledge-log.php b/plugins/wporg-5ftf/includes/pledge-log.php new file mode 100644 index 0000000..1a7f643 --- /dev/null +++ b/plugins/wporg-5ftf/includes/pledge-log.php @@ -0,0 +1,165 @@ +ID ); + + require FiveForTheFuture\get_views_path() . 'log.php'; +} + +/** + * Defaults for a log entry. + * + * @return array + */ +function get_log_entry_template() { + return array( + 'timestamp' => time(), + 'message' => '', + 'data' => array(), + 'user_id' => 0, + ); +} + +/** + * Get a time-sorted array of log entries for a particular pledge. + * + * @param int $pledge_id + * + * @return array + */ +function get_pledge_log( $pledge_id ) { + $log = get_post_meta( $pledge_id, LOG_META_KEY, false ); + + if ( ! $log ) { + return array(); + } + + usort( $log, function( $a, $b ) { + if ( $a['timestamp'] === $b['timestamp'] ) { + return 0; + } + + return ( $a['timestamp'] < $b['timestamp'] ) ? -1 : 1; + } ); + + return $log; +} + +/** + * Add a new log entry for a particular pledge. + * + * @param int $pledge_id + * @param string $message + * @param array $data + * @param int $user_id + * + * @return void + */ +function add_log_entry( $pledge_id, $message, array $data, $user_id ) { + $entry = get_log_entry_template(); + + $entry['message'] = $message; + $entry['data'] = $data; + $entry['user_id'] = $user_id; + + add_post_meta( $pledge_id, LOG_META_KEY, $entry, false ); +} + +/** + * Record logs for events when saving a post. + * + * Hooked to "save_post_{$post->post_type}". + * + * @param int $post_ID Post ID. + * @param WP_Post $post Unused. Post object. + * @param bool $update Whether this is an existing post being updated or not. + * + * @return void + */ +function capture_save_post( $post_ID, $post, $update ) { + if ( false === $update ) { + add_log_entry( + $post_ID, + sprintf( + 'Pledge created. Status set to %s.', + esc_html( get_post_status( $post_ID ) ) + ), + PledgeForm\get_form_submission(), + get_current_user_id() + ); + } +} + +/** + * Record logs for events when postmeta values change. + * + * @param int $meta_id Unused. ID of updated metadata entry. + * @param int $object_id Post ID. + * @param string $meta_key Meta key. + * @param mixed $meta_value Meta value. + * + * @return void + */ +function capture_updated_postmeta( $meta_id, $object_id, $meta_key, $meta_value ) { + $post_type = get_post_type( $object_id ); + + if ( Pledge\CPT_ID !== $post_type ) { + return; + } + + $valid_keys = array_keys( PledgeMeta\get_pledge_meta_config( 'user_input' ) ); + + if ( in_array( $meta_key, $valid_keys, true ) ) { + add_log_entry( + $object_id, + sprintf( + 'Changed %1$scode> to %2$s.', + esc_html( $meta_key ), + esc_html( $meta_value ) + ), + array( + $meta_key => $meta_value, + ), + get_current_user_id() + ); + } +} diff --git a/plugins/wporg-5ftf/index.php b/plugins/wporg-5ftf/index.php index 54e1d28..be8c836 100755 --- a/plugins/wporg-5ftf/index.php +++ b/plugins/wporg-5ftf/index.php @@ -30,6 +30,7 @@ function load() { require_once get_includes_path() . 'pledge-form.php'; require_once get_includes_path() . 'directory.php'; require_once get_includes_path() . 'xprofile.php'; + require_once get_includes_path() . 'pledge-log.php'; } /** diff --git a/plugins/wporg-5ftf/views/log.php b/plugins/wporg-5ftf/views/log.php new file mode 100644 index 0000000..57d3fcb --- /dev/null +++ b/plugins/wporg-5ftf/views/log.php @@ -0,0 +1,53 @@ + + +
+ + + + + + + + + + + + + + + + + + + +
DateEntryUser
+ + +
+ + + + +
+ +
+
+ + user_login ); ?> + +
+ + + +

+ There are no log entries. +

+ + +