Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature: add REST endpint for ffmpeg server callback handler #293

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
3 changes: 2 additions & 1 deletion admin/rt-transcoder-handler.php
Original file line number Diff line number Diff line change
Expand Up @@ -1111,6 +1111,7 @@ public function get_post_id_by_meta_key_and_value( $key, $value ) {
* @since 1.0.0
*/
public function handle_callback() {
// TODO: Refactor this function into REST API.
require_once ABSPATH . 'wp-admin/includes/image.php';

$job_id = transcoder_filter_input( INPUT_POST, 'job_id', FILTER_SANITIZE_FULL_SPECIAL_CHARS );
Expand Down Expand Up @@ -1610,7 +1611,7 @@ class_exists( 'ImagickPixel', false ) &&
/**
* Sanitize transcoder post respopnse array.
*/
private function filter_transcoder_response() {
public function filter_transcoder_response() {
$post_var = $_POST; // phpcs:ignore WordPress.Security.NonceVerification.Missing

$filter_post_args = array(
Expand Down
222 changes: 222 additions & 0 deletions admin/rt-transcoder-rest-routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,25 @@ class Transcoder_Rest_Routes extends WP_REST_Controller {
*/
public $namespace_prefix = 'transcoder/v';

/**
* RT Transcoder Handler object.
*
* @var RT_Transcoder_Handler
*/
public $rt_transcoder_handler;

/**
* Constructor
*
* @since 1.0.0
*
* @access public
* @return void
*/
public function __construct() {
$this->rt_transcoder_handler = new RT_Transcoder_Handler( true );
}

/**
* Function to register routes.
*/
Expand All @@ -50,6 +69,54 @@ public function register_routes() {
'permission_callback' => '__return_true',
)
);

// Register `transcoder-callback` route to handle callback request by the FFMPEG transcoding server.
register_rest_route(
$this->namespace_prefix . $this->version,
'/transcoder-callback',
array(
'methods' => WP_REST_Server::CREATABLE,
'callback' => array( $this, 'handle_callback' ),
'permission_callback' => '__return_true',
'args' => array(
'job_id' => array(
pranvinit marked this conversation as resolved.
Show resolved Hide resolved
'required' => true,
'type' => 'string',
'sanitize_callback' => 'sanitize_text_field',
),
'file_status' => array(
pranvinit marked this conversation as resolved.
Show resolved Hide resolved
'required' => true,
'type' => 'string',
'sanitize_callback' => 'sanitize_text_field',
),
'error_msg' => array(
pranvinit marked this conversation as resolved.
Show resolved Hide resolved
'required' => false,
'type' => 'string',
'sanitize_callback' => 'sanitize_text_field',
),
'job_for' => array(
pranvinit marked this conversation as resolved.
Show resolved Hide resolved
'required' => false,
'type' => 'string',
'sanitize_callback' => 'sanitize_text_field',
),
'thumbnail' => array(
pranvinit marked this conversation as resolved.
Show resolved Hide resolved
'required' => false,
'type' => 'string',
'sanitize_callback' => 'sanitize_text_field',
),
'format' => array(
pranvinit marked this conversation as resolved.
Show resolved Hide resolved
'required' => false,
'type' => 'string',
'sanitize_callback' => 'sanitize_text_field',
),
'job_manager_form' => array(
'required' => false,
'type' => 'string',
'sanitize_callback' => 'sanitize_text_field',
),
),
)
);
}

/**
Expand Down Expand Up @@ -152,4 +219,159 @@ private function get_media_data_by_id( $media_id ) {
);
}

/**
* Function to handle the callback request by the FFMPEG transcoding server.
*
* @param WP_REST_Request $request Object of WP_REST_Request.
*
* @return WP_Error|WP_REST_Response REST API response.
*/
public function handle_callback( WP_REST_Request $request ) {

$job_id = sanitize_text_field( wp_unslash( $request->get_param( 'job_id' ) ) );
$file_status = sanitize_text_field( wp_unslash( $request->get_param( 'file_status' ) ) );
$error_msg = sanitize_text_field( wp_unslash( $request->get_param( 'error_msg' ) ) );
$job_for = sanitize_text_field( wp_unslash( $request->get_param( 'job_for' ) ) );
$thumbnail = sanitize_text_field( wp_unslash( $request->get_param( 'thumbnail' ) ) );
$format = sanitize_text_field( wp_unslash( $request->get_param( 'format' ) ) );

if ( ! empty( $job_id ) && ! empty( $file_status ) && ( 'error' === $file_status ) ) {
$this->rt_transcoder_handler->nofity_transcoding_failed( $job_id, $error_msg );
return new WP_Error( 'transcoder_error', 'Something went wrong. Invalid post request.', array( 'status' => 400 ) );
}

$mail = defined( 'RT_TRANSCODER_NO_MAIL' ) ? false : true;

$attachment_id = '';

if ( isset( $job_for ) && ( 'wp-media' === $job_for ) ) {
if ( isset( $job_id ) ) {
$has_thumbs = isset( $thumbnail ) ? true : false;
$flag = false;

$id = $this->rt_transcoder_handler->get_post_id_by_meta_key_and_value( '_rt_transcoding_job_id', $job_id );

if ( ! empty( $id ) && is_numeric( $id ) ) {
$attachment_id = $id;
$post_array = $this->rt_transcoder_handler->filter_transcoder_response();
$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 );
}

if ( ! empty( $post_array['files'] ) ) {
$this->rt_transcoder_handler->add_transcoded_files( $post_array['files'], $attachment_id, $job_for );
}
} else {
$flag = 'Something went wrong. The required attachment id does not exists. It must have been deleted.';
}

$this->rt_transcoder_handler->update_usage( $this->rt_transcoder_handler->api_key );

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>';
$users = get_users( array( 'role' => 'administrator' ) );
if ( $users ) {
$admin_email_ids = array();
foreach ( $users as $user ) {
$admin_email_ids[] = $user->user_email;
}
add_filter( 'wp_mail_content_type', array( $this->rt_transcoder_handler, 'wp_mail_content_type' ) );
wp_mail( $admin_email_ids, $subject, $message ); // phpcs:ignore WordPressVIPMinimum.Functions.RestrictedFunctions.wp_mail_wp_mail
remove_filter( 'wp_mail_content_type', array( $this->rt_transcoder_handler, 'wp_mail_content_type' ) );
}
return new WP_Error( 'transcoder_error', $flag, array( 'status' => 500 ) );
} else {
return new WP_REST_Response( 'Done', 200 );
}
}
} else {

// To check if request is sumitted from the WP Job Manager plugin ( https://wordpress.org/plugins/wp-job-manager/ ).
$job_manager_form = sanitize_text_field( wp_unslash( $request->get_param( 'job_manager_form' ) ) );

if ( isset( $job_id ) && ! empty( $job_id ) && class_exists( 'RTDBModel' ) && empty( $job_manager_form ) ) {

$has_thumbs = isset( $thumbnail ) ? true : false;
$flag = false;
$model = new RTDBModel( 'rtm_media_meta', false, 10, true );

$meta_details = $model->get(
array(
'meta_value' => $job_id, // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_value
'meta_key' => 'rtmedia-transcoding-job-id', // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_key
)
);

if ( ! isset( $meta_details[0] ) ) {
$id = $this->rt_transcoder_handler->get_post_id_by_meta_key_and_value( '_rt_transcoding_job_id', $job_id );
} else {
$id = $meta_details[0]->media_id;
}

if ( isset( $id ) && is_numeric( $id ) ) {
$model = new RTMediaModel();
$media = $model->get_media( array( 'media_id' => $id ), 0, 1 );
$this->media_author = $media[0]->media_author;
$attachment_id = $media[0]->media_id;

$post_array = $this->rt_transcoder
->filter_transcoder_response();
$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 );
}

if ( ! empty( $post_array['files'] ) ) {
$this->rt_transcoder_handler->add_transcoded_files( $post_array['files'], $attachment_id, $job_for );
}
} else {
$flag = 'Something went wrong. The required attachment id does not exists. It must have been deleted.';
}

$this->rt_transcoder_handler->update_usage( $this->rt_transcoder_handler->api_key );

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>';
$users = get_users( array( 'role' => 'administrator' ) );
if ( $users ) {
$admin_email_ids = array();
foreach ( $users as $user ) {
$admin_email_ids[] = $user->user_email;
}
add_filter( 'wp_mail_content_type', array( $this->rt_transcoder_handler, 'wp_mail_content_type' ) );
wp_mail( $admin_email_ids, $subject, $message ); // phpcs:ignore WordPressVIPMinimum.Functions.RestrictedFunctions.wp_mail_wp_mail
remove_filter( 'wp_mail_content_type', array( $this->rt_transcoder_handler, 'wp_mail_content_type' ) );
}
return new WP_Error( 'transcoder_error', $flag, array( 'status' => 500 ) );

} else {
return new WP_REST_Response( 'Done', 200 );
}
}
}

/**
* Allow users/plugins to perform action after response received from the transcoder is
* processed
*
* @since 1.0.9
*
* @param number $attachment_id Attachment ID for which the callback has sent from the transcoder
* @param number $job_id The transcoding job ID
*/
do_action( 'rtt_handle_callback_finished', $attachment_id, $job_id );
}
}
Loading