Skip to content

Commit

Permalink
fix: return parsed post data in response
Browse files Browse the repository at this point in the history
  • Loading branch information
pranvinit committed Nov 6, 2024
1 parent fca3de1 commit f35e064
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 8 deletions.
24 changes: 24 additions & 0 deletions admin/rt-transcoder-handler.php
Original file line number Diff line number Diff line change
Expand Up @@ -1644,4 +1644,28 @@ public function filter_transcoder_response() {

return $post_array;
}

/**
* Sanitize transcoder post respopnse array for JSON.
*
* @param WP_REST_Request $request Request object.
*
* @return array $post_array Sanitized post array.
*/
public function filter_transcoder_response_json( $request ) {
return array(
'job_id' => sanitize_text_field( wp_unslash( $request->get_param( 'job_id' ) ) ),
'job_type' => sanitize_text_field( wp_unslash( $request->get_param( 'job_type' ) ) ),
'job_for' => sanitize_text_field( wp_unslash( $request->get_param( 'job_for' ) ) ),
'format' => sanitize_text_field( wp_unslash( $request->get_param( 'format' ) ) ),
'download_url' => esc_url_raw( $request->get_param( 'download_url' ) ),
'file_name' => sanitize_text_field( wp_unslash( $request->get_param( 'file_name' ) ) ),
'thumb_count' => absint( $request->get_param( 'thumb_count' ) ),
'status' => sanitize_text_field( wp_unslash( $request->get_param( 'status' ) ) ),
'files' => array_map( 'esc_url_raw', (array) $request->get_param( 'files' ) ),
'file_status' => sanitize_text_field( wp_unslash( $request->get_param( 'file_status' ) ) ),
'thumbnail' => array_map( 'esc_url_raw', (array) $request->get_param( 'thumbnail' ) ),
'error_msg' => sanitize_text_field( wp_unslash( $request->get_param( 'error_msg' ) ) ),
);
}
}
16 changes: 8 additions & 8 deletions admin/rt-transcoder-rest-routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -253,15 +253,15 @@ public function handle_callback( WP_REST_Request $request ) {

if ( ! empty( $id ) && is_numeric( $id ) ) {
$attachment_id = $id;
$post_array = $this->rt_transcoder_handler->filter_transcoder_response();
$post_array = $this->rt_transcoder_handler->filter_transcoder_response_json( $request );
$post_array['post_id'] = $attachment_id;

if ( $has_thumbs && ! empty( $post_array['thumbnail'] ) ) {
$thumbnail = $this->rt_transcoder_handler->add_media_thumbnails( $post_array );
}

if ( isset( $format ) && 'thumbnail' === $format ) {
return new WP_REST_Response( 'Done', 200 );
return new WP_REST_Response( $post_array, 200 );
}

if ( ! empty( $post_array['files'] ) ) {
Expand All @@ -275,7 +275,7 @@ public function handle_callback( WP_REST_Request $request ) {

if ( $flag && $mail ) {
$subject = 'Transcoding: Download Failed';
$message = '<p><a href="' . rtt_get_edit_post_link( $attachment_id ) . '">Media</a> was successfully encoded but there was an error while downloading:</p><p><code>' . $flag . '</code></p>';
$message = '<p><a href="' . esc_url( rtt_get_edit_post_link( $attachment_id ) ) . '">' . esc_html__( 'Media', 'transcoder' ) . '</a> ' . esc_html__( ' was successfully encoded but there was an error while downloading:', 'transcoder' ) . '</p><p><code>' . esc_html( $flag ) . '</code></p>';
$users = get_users( array( 'role' => 'administrator' ) );
if ( $users ) {
$admin_email_ids = array();
Expand All @@ -288,7 +288,7 @@ public function handle_callback( WP_REST_Request $request ) {
}
return new WP_Error( 'transcoder_error', $flag, array( 'status' => 500 ) );
} else {
return new WP_REST_Response( 'Done', 200 );
return new WP_REST_Response( $post_array, 200 );
}
}
} else {
Expand Down Expand Up @@ -322,15 +322,15 @@ public function handle_callback( WP_REST_Request $request ) {
$attachment_id = $media[0]->media_id;

$post_array = $this->rt_transcoder
->filter_transcoder_response();
->filter_transcoder_response_json( $request );
$post_array['post_id'] = $attachment_id;

if ( $has_thumbs ) {
$this->rt_transcoder_handler->add_media_thumbnails( $post_array );
}

if ( isset( $format ) && 'thumbnail' === $format ) {
return new WP_REST_Response( 'Done', 200 );
return new WP_REST_Response( $post_array, 200 );
}

if ( ! empty( $post_array['files'] ) ) {
Expand All @@ -344,7 +344,7 @@ public function handle_callback( WP_REST_Request $request ) {

if ( $flag && $mail ) {
$subject = 'Transcoding: Download Failed';
$message = '<p><a href="' . rtt_get_edit_post_link( $attachment_id ) . '">Media</a> was successfully transcoded but there was an error while downloading:</p><p><code>' . $flag . '</code></p><p>';
$message = '<p><a href="' . esc_url( rtt_get_edit_post_link( $attachment_id ) ) . '">' . esc_html__( 'Media', 'transcoder' ) . '</a> ' . esc_html__( ' was successfully transcoded but there was an error while downloading:', 'transcoder' ) . '</p><p><code>' . esc_html( $flag ) . '</code></p><p>';
$users = get_users( array( 'role' => 'administrator' ) );
if ( $users ) {
$admin_email_ids = array();
Expand All @@ -358,7 +358,7 @@ public function handle_callback( WP_REST_Request $request ) {
return new WP_Error( 'transcoder_error', $flag, array( 'status' => 500 ) );

} else {
return new WP_REST_Response( 'Done', 200 );
return new WP_REST_Response( $post_array, 200 );
}
}
}
Expand Down

0 comments on commit f35e064

Please sign in to comment.