Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion admin/class-rtgodam-transcoder-handler.php
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,11 @@ public function wp_media_transcoding( $wp_metadata, $attachment_id, $autoformat
}
}

// Skip transcoding and re-transcoding for images.
if ( preg_match( '/image/i', $wp_metadata['mime_type'], $type_array ) ) {
return $wp_metadata;
}

if ( empty( $wp_metadata['mime_type'] ) ) {
return $wp_metadata;
}
Expand Down Expand Up @@ -278,7 +283,6 @@ public function wp_media_transcoding( $wp_metadata, $attachment_id, $autoformat
$type = strtolower( $type_arry[ count( $type_arry ) - 1 ] );
$extension = pathinfo( $path, PATHINFO_EXTENSION );
$not_allowed_type = array();
preg_match( '/video|audio/i', $metadata['mime_type'], $type_array );

if ( (
preg_match( '/video|audio/i', $metadata['mime_type'], $type_array ) ||
Expand Down
47 changes: 42 additions & 5 deletions inc/classes/class-media-library-ajax.php
Original file line number Diff line number Diff line change
Expand Up @@ -148,16 +148,51 @@ private function get_mime_type_for_job_type( $job_type, $mime_type ) {
/**
* Upload media to the Frappe backend.
*
* @param int $attachment_id Attachment ID.
* @param int $attachment_id Attachment ID.
* @param bool $retranscode Whether this is a retranscode request.
* @return void
*/
public function upload_media_to_frappe_backend( $attachment_id ) {
public function upload_media_to_frappe_backend( $attachment_id, $retranscode = false ) {
// Check if local development environment.
if ( rtgodam_is_local_environment() ) {
return;
}

// Only if attachment type if image.
/**
* Filter to allow external developers to disable automatic transcoding on upload.
* This allows users to have manual control over when media files get transcoded.
*
* Note: This filter only applies to automatic uploads. Manual retranscoding requests
* (via bulk actions, tools page, etc.) will always proceed regardless of this setting.
* Form integrations will also use this filter to disable transcoding for form uploads.
*
* Example usage:
* add_filter( 'godam_auto_transcode_on_upload', '__return_false' ); // Disable globally
*
* @since n.e.x.t
*
* @param bool $auto_transcode_on_upload Whether to automatically transcode on upload. Default true.
*/
if ( ! $retranscode ) {
$auto_transcode_on_upload = apply_filters( 'godam_auto_transcode_on_upload', true );

if ( ! $auto_transcode_on_upload ) {
return;
}
}

$transcoding_job_id = get_post_meta( $attachment_id, 'rtgodam_transcoding_job_id', true );

// Check virtual media status for transcoding requests.
$godam_original_id = get_post_meta( $attachment_id, '_godam_original_id', true );
$is_virtual_media = ! empty( $godam_original_id );

// Skip transcoding for virtual media.
if ( $is_virtual_media ) {
return;
}

// Only if attachment type is image.
if ( 'image' !== substr( get_post_mime_type( $attachment_id ), 0, 5 ) ) {
return;
}
Expand All @@ -168,7 +203,7 @@ public function upload_media_to_frappe_backend( $attachment_id ) {
return;
}

$api_url = RTGODAM_API_BASE . '/api/resource/Transcoder Job';
$api_url = RTGODAM_API_BASE . '/api/resource/Transcoder Job' . ( empty( $transcoding_job_id ) ? '' : '/' . $transcoding_job_id );

$attachment_url = wp_get_attachment_url( $attachment_id );

Expand Down Expand Up @@ -208,6 +243,7 @@ public function upload_media_to_frappe_backend( $attachment_id ) {

// Request params.
$params = array(
'retranscode' => empty( $transcoding_job_id ) ? 0 : 1,
'api_token' => $api_key,
'job_type' => 'image',
'job_for' => 'wp-media',
Expand All @@ -222,9 +258,10 @@ public function upload_media_to_frappe_backend( $attachment_id ) {
'public' => 1,
);

$upload_media = wp_remote_post(
$upload_media = wp_remote_request(
$api_url,
array(
'method' => empty( $transcoding_job_id ) ? 'POST' : 'PUT',
'body' => wp_json_encode( $params ),
'headers' => array(
'Content-Type' => 'application/json',
Expand Down
13 changes: 10 additions & 3 deletions inc/classes/rest-api/class-transcoding.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@

namespace RTGODAM\Inc\REST_API;

use RTGODAM\Inc\Media_Library_Ajax;

defined( 'ABSPATH' ) || exit;

/**
Expand Down Expand Up @@ -533,7 +535,7 @@ public function retranscode_media( \WP_REST_Request $request ) {
if ( ! empty( $godam_original_id ) ) {
$message = sprintf(
// translators: 1: Attachment title, 2: Attachment ID.
__( '%1$s (ID %2$d) is virtual media from GoDAM Central. Please retranscode this video on GoDAM Central.', 'godam' ),
__( '%1$s (ID %2$d) is virtual media from GoDAM Central. Please retranscode this media on GoDAM Central.', 'godam' ),
esc_html( $title ),
absint( $attachment_id )
);
Expand Down Expand Up @@ -603,8 +605,13 @@ public function retranscode_media( \WP_REST_Request $request ) {
$wp_metadata['mime_type'] = $mime_type;

// Retranscode the media.
$transcoder = new \RTGODAM_Transcoder_Handler( true );
$transcoder->wp_media_transcoding( $wp_metadata, $attachment_id, true, true );
if ( preg_match( '/image/i', $mime_type ) ) {
$transcoder = Media_Library_Ajax::get_instance();
$transcoder->upload_media_to_frappe_backend( $attachment_id, true );
} else {
$transcoder = new \RTGODAM_Transcoder_Handler( true );
$transcoder->wp_media_transcoding( $wp_metadata, $attachment_id, true, true );
}

// Check if the transcoding job ID is set.
$is_sent = get_post_meta( $attachment_id, 'rtgodam_transcoding_job_id', true );
Expand Down
Loading