From 101f5772ef220083e7987919e9bd9db111291f22 Mon Sep 17 00:00:00 2001 From: Burhan Nasir Date: Wed, 4 Sep 2024 17:36:27 +0500 Subject: [PATCH] Fix: Save history isn't working (#404) --- assets/js/src/views/video-edit.js | 6 ++++- .../admin/api/class-bc-admin-media-api.php | 25 ++++++------------- 2 files changed, 13 insertions(+), 18 deletions(-) diff --git a/assets/js/src/views/video-edit.js b/assets/js/src/views/video-edit.js index 711aca7..1acf745 100644 --- a/assets/js/src/views/video-edit.js +++ b/assets/js/src/views/video-edit.js @@ -414,6 +414,10 @@ var VideoEditView = BrightcoveView.extend({ this.model.set('custom_fields', custom); this.model.set('custom', custom_fields); + const history = this.el.querySelector('.brightcove-change-history').value; + const historyJson = JSON.stringify(history.split('\n').map((line) => line.trim())); + this.model.set('_change_history', historyJson); + this.model .save() .done(function () { @@ -572,7 +576,7 @@ var VideoEditView = BrightcoveView.extend({ history = JSON.parse(history); _.each(history, function (item) { - historyStr += item.user + ' - ' + item.time + '\n'; + historyStr += item + '\n'; }); if (historyStr !== '') { diff --git a/includes/admin/api/class-bc-admin-media-api.php b/includes/admin/api/class-bc-admin-media-api.php index 0c1d9c0..ac47558 100644 --- a/includes/admin/api/class-bc-admin-media-api.php +++ b/includes/admin/api/class-bc-admin-media-api.php @@ -204,21 +204,12 @@ public function bc_ajax_update_video_or_playlist() { // Build out history if it's supported if ( $use_history ) { - $history = null; - if ( isset( $_POST['history'] ) ) { - $raw = wp_unslash( $_POST['history'] ); - $history = json_decode( $raw, true ); - } - if ( null === $history ) { - $history = array(); - } + $history = isset( $_POST['history'] ) ? json_decode( wp_unslash( $_POST['history'] ), true ) : []; - $user = wp_get_current_user(); - $history[] = array( - 'user' => $user->user_login, - 'time' => gmdate( 'Y-m-d H:i:s', time() ), - ); + $user = wp_get_current_user(); + $history = array_merge( array( sprintf( '%s - %s', $user->user_login, wp_date( 'Y-m-d H:i:s' ) ) ), $history ); + $history = array_filter( $history ); $custom['_change_history'] = wp_json_encode( $history ); } @@ -589,7 +580,7 @@ public function brightcove_media_query() { // Get a list of videos. - for ( $i = 0; $i < $tries; $i ++ ) { + for ( $i = 0; $i < $tries; $i++ ) { $results = $this->cms_api->video_list( $posts_per_page, $posts_per_page * ( $page - 1 ), $query_string, $bc_video_sort_field, true, $folder_id ); if ( ! is_wp_error( $results ) ) { @@ -634,7 +625,7 @@ public function brightcove_media_query() { $bc_accounts->set_current_account_by_id( $account_id ); - for ( $i = 0; $i < $tries; $i ++ ) { + for ( $i = 0; $i < $tries; $i++ ) { $results = $this->cms_api->playlist_list( $query ); if ( ! is_wp_error( $results ) ) { @@ -654,7 +645,7 @@ public function brightcove_media_query() { $bc_accounts->set_current_account_by_id( $account_id ); - for ( $i = 0; $i < $tries; $i ++ ) { + for ( $i = 0; $i < $tries; $i++ ) { $results = $this->experiences_api->get_experiences_by_account_id( $account_id ); if ( ! is_wp_error( $results ) ) { @@ -672,7 +663,7 @@ public function brightcove_media_query() { } // Get a list of available custom fields - for ( $i = 0; $i < $tries; $i ++ ) { + for ( $i = 0; $i < $tries; $i++ ) { $fields = $this->cms_api->video_fields(); if ( ! is_wp_error( $fields ) ) {