listPlaybackRestrictions()->getData(); foreach ($all_restrictions as $restriction) { try { $api_playback_restrictions->deletePlaybackRestriction($restriction->getId()); error_log('Restriction deleted: ' . $restriction->getId()); } catch (Exception $e) { error_log('Error deleting restriction: ' . $e->getMessage()); } } $request_data = isset($_POST['form_data']) ? stripslashes(sanitize_text_field($_POST['form_data'])) : ''; $update_referrer_domain_restriction_request = json_decode($request_data, true); $create_playback_restriction_request = ["referrer" => $update_referrer_domain_restriction_request]; try { $result = $api_playback_restrictions->createPlaybackRestriction($create_playback_restriction_request); error_log('New restriction created: ' . print_r($result, true)); } catch (Exception $e) { error_log('Error creating restriction: ' . $e->getMessage()); } } catch (Exception $e) { error_log('Error with restrictions: ' . $e->getMessage()); } wp_die(); } /** * Display the content to Customize shortcode */ add_action( 'wp_ajax_muxvideo_display_modal_customize_shortcode', 'muxvideo_display_modal_customize_shortcode' ); add_action( 'wp_ajax_nopriv_muxvideo_display_modal_customize_shortcode', 'muxvideo_display_modal_customize_shortcode' ); function muxvideo_display_modal_customize_shortcode() { try { include_once MUXVIDEO_PLUGIN_DIR . '/views/template-parts/customize-shortcode.php'; wp_die(); } catch (\Throwable $th) { error_log( "Error: muxvideo_display_modal_customize_shortcode " . $th->getMessage() ); } } /** * Display the content to Customize shortcode */ add_action( 'wp_ajax_muxvideo_get_jwt_dinamically', 'muxvideo_get_jwt_dinamically' ); add_action( 'wp_ajax_nopriv_muxvideo_get_jwt_dinamically', 'muxvideo_get_jwt_dinamically' ); function muxvideo_get_jwt_dinamically() { $json_string = isset( $_POST['formated_data'] ) ? wp_unslash( sanitize_text_field( $_POST['formated_data'] ) ) : ''; $asset_data = json_decode( sanitize_text_field( $json_string ), true ); $time = isset( $_POST['time'] ) ? absint( sanitize_text_field( $_POST['time'] ) ) : 0; if ( ! empty( $asset_data ) && is_array( $asset_data ) ) { try { $jwt_t = muxvideo_get_json_web_token( sanitize_text_field( $asset_data['playback_id'] ), sanitize_text_field( $asset_data['signing_id'] ), sanitize_text_field( $asset_data['signing_private_key'] ), 't', sanitize_text_field( $asset_data['playback_restriction_id'] ), $time ); wp_send_json( $jwt_t ); wp_die(); } catch (\Throwable $th) { error_log( "Error: muxvideo_get_jwt_dinamically " . $th->getMessage() ); } } else { wp_send_json_error( 'No valid data' ); wp_die(); } } /** * Function called from ajax to get next page of assets */ add_action( 'wp_ajax_muxvideo_load_more_assets', 'muxvideo_load_more_assets' ); add_action( 'wp_ajax_nopriv_muxvideo_load_more_assets', 'muxvideo_load_more_assets' ); function muxvideo_load_more_assets() { muxvideo_display_assets_list(); wp_die(); } /** * Create a delete request and remove the asset in Mux */ add_action( 'wp_ajax_muxvideo_delete_asset', 'muxvideo_delete_asset' ); add_action( 'wp_ajax_nopriv_muxvideo_delete_asset', 'muxvideo_delete_asset' ); function muxvideo_delete_asset() { require_once MUXVIDEO_PLUGIN_DIR . '/includes/functions.php'; global $wpdb; if(!isset($_REQUEST['nonce-form']) || !wp_verify_nonce($_REQUEST['nonce-form'], '_wpnonce')) { try { $assetsApi = muxvideo_api_client_init(); $assetsApi->deleteAsset( sanitize_text_field( $_REQUEST['asset_id'] ) ); $wpdb->delete( $wpdb->prefix . 'mux_caption_queue', array( 'asset_id' => sanitize_text_field( $_REQUEST['asset_id'] ) ), array( '%s' ) ); } catch (Exception $e) { error_log( 'Exception when calling AssetsApi->deleteAsset: '. $e->getMessage() ); } wp_die(); } } /** * Create a new upload url to upload a new asset */ add_action( 'wp_ajax_muxvideo_reset_upload_url', 'muxvideo_reset_upload_url' ); add_action( 'wp_ajax_nopriv_muxvideo_reset_upload_url', 'muxvideo_reset_upload_url' ); function muxvideo_reset_upload_url() { require_once MUXVIDEO_PLUGIN_DIR . '/includes/functions.php'; if(!isset($_REQUEST['nonce-form']) || !wp_verify_nonce($_REQUEST['nonce-form'], '_wpnonce')) { $asset_privacy = ( $_REQUEST['asset_privacy'] && $_REQUEST['asset_privacy'] == 'signed' ) ? [ MuxPhp\Models\PlaybackPolicy::SIGNED ] : [ MuxPhp\Models\PlaybackPolicy::_PUBLIC ]; $upload_id = isset($_REQUEST['upload_id']) ? sanitize_text_field($_REQUEST['upload_id']) : ''; $caption_type = isset($_REQUEST['caption_type']) ? sanitize_text_field($_REQUEST['caption_type']) : ''; $language_code = isset($_REQUEST['language_code']) ? sanitize_text_field($_REQUEST['language_code']) : ''; $closed_captions = isset($_REQUEST['closed_captions']) && $_REQUEST['closed_captions'] === 'true'; muxvideo_save_caption_request_handler($upload_id, $caption_type, $language_code, $closed_captions); try { $uploadsApi = muxvideo_api_client_init_upload(); $createAssetRequest = new MuxPhp\Models\CreateAssetRequest( [ "playback_policy" => $asset_privacy ] ); $createUploadRequest = new MuxPhp\Models\CreateUploadRequest( [ "timeout" => 3600, "new_asset_settings" => $createAssetRequest, "cors_origin" => "*" ] ); $upload = $uploadsApi->createDirectUpload( $createUploadRequest ); $data = $upload->getData(); $response = [ 'data' => [ 'url' => $data->getUrl(), 'id' => $data->getId() ] ]; wp_send_json( $response ); } catch (Exception $e) { error_log( 'Exception when calling AssetsApi->deleteAsset: '. $e->getMessage() ); } wp_die(); } } /** * Refresh asset list dynamically */ add_action( 'wp_ajax_muxvideo_refresh_asset_list', 'muxvideo_refresh_asset_list' ); add_action( 'wp_ajax_nopriv_muxvideo_refresh_asset_list', 'muxvideo_refresh_asset_list' ); function muxvideo_refresh_asset_list() { try { muxvideo_display_assets_list(); wp_die(); } catch (\Exception $e) { error_log( 'Exception when calling muxvideo_refresh_asset_list: '. $e->getMessage() ); } } /** * Display the delete caption modal content */ function muxvideo_display_modal_delete_caption() { if ( ! isset( $_POST['track_id'] ) || ! isset( $_POST['asset_id'] ) ) { wp_send_json_error( array( 'message' => __( 'delete_caption_error_missing_data', '2coders-integration-mux-video' ) ) ); } ob_start(); include_once MUXVIDEO_PLUGIN_DIR . '/views/template-parts/delete-caption-modal.php'; $content = ob_get_clean(); echo $content; wp_die(); } add_action( 'wp_ajax_muxvideo_display_modal_delete_caption', 'muxvideo_display_modal_delete_caption' ); /** * Delete a caption track */ function muxvideo_delete_caption_track() { if (!isset($_POST['track_id']) || !isset($_POST['asset_id'])) { wp_send_json_error(array('message' => __('delete_caption_error_missing_data', '2coders-integration-mux-video'))); return; } $track_id = sanitize_text_field($_POST['track_id']); $asset_id = sanitize_text_field($_POST['asset_id']); try { $assetsApi = muxvideo_api_client_init(); $assetsApi->deleteAssetTrack($asset_id, $track_id); wp_send_json_success(array( 'message' => __('caption_deleted_success', '2coders-integration-mux-video'), 'track_id' => $track_id )); } catch (\Exception $e) { error_log('Exception when calling AssetsApi->deleteTrack: ' . $e->getMessage()); wp_send_json_error(array( 'message' => __('caption_delete_error', '2coders-integration-mux-video'), 'error' => $e->getMessage() )); } wp_die(); } add_action('wp_ajax_muxvideo_delete_caption_track', 'muxvideo_delete_caption_track'); add_action('wp_ajax_nopriv_muxvideo_delete_caption_track', 'muxvideo_delete_caption_track'); /** * Display the content to manage captions */ add_action('wp_ajax_muxvideo_display_modal_manage_captions', 'muxvideo_display_modal_manage_captions'); add_action('wp_ajax_nopriv_muxvideo_display_modal_manage_captions', 'muxvideo_display_modal_manage_captions'); function muxvideo_display_modal_manage_captions() { try { $asset_id = isset($_POST['asset_id']) ? sanitize_text_field($_POST['asset_id']) : ''; $used_languages = isset($_POST['used_languages']) ? json_decode(sanitize_text_field($_POST['used_languages']), true) : []; if (empty($asset_id)) { wp_send_json_error(array('message' => __('captions_manager_error_missing_asset', '2coders-integration-mux-video'))); return; } include_once MUXVIDEO_PLUGIN_DIR . '/views/template-parts/customize-captions-modal.php'; wp_die(); } catch (\Throwable $th) { error_log("Error: muxvideo_display_modal_manage_captions " . $th->getMessage()); } } /** * Ajax handler for loading asset captions */ add_action('wp_ajax_muxvideo_get_asset_captions', 'muxvideo_get_asset_captions'); add_action('wp_ajax_nopriv_muxvideo_get_asset_captions', 'muxvideo_get_asset_captions'); function muxvideo_get_asset_captions() { if (!isset($_POST['asset_id'])) { wp_send_json_error(array('message' => __('Missing asset ID', '2coders-integration-mux-video'))); return; } $asset_id = sanitize_text_field($_POST['asset_id']); $upload_id = isset($_POST['upload_id']) ? sanitize_text_field($_POST['upload_id']) : ''; $caption_page = isset($_POST['caption_page']) ? intval($_POST['caption_page']) : 1; try { // Check for pending captions for this asset global $wpdb; $table_name = $wpdb->prefix . 'mux_caption_queue'; // Only proceed if we have an upload_id if (!empty($upload_id) && !empty($asset_id)) { // First check if the asset is ready try { $assetsApi = muxvideo_api_client_init(); $asset = $assetsApi->getAsset($asset_id); $asset_data = $asset->getData(); // If asset is not ready, just update the timestamp and exit if ($asset_data->getStatus() === 'ready') { $pending_requests = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM $table_name WHERE upload_id = %s AND status = %s ORDER BY created_at ASC", $upload_id, 'pending' ) ); // Update the asset_id for these requests if we have pending requests if (!empty($pending_requests)) { $wpdb->update( $table_name, array( 'asset_id' => $asset_id, 'updated_at' => current_time('mysql') ), array('upload_id' => $upload_id) ); // Process pending captions foreach ($pending_requests as $request) { $wpdb->update( $table_name, array( 'attempts' => $request->attempts + 1, 'updated_at' => current_time('mysql') ), array('id' => $request->id) ); $original_filename = isset($request->original_filename) ? $request->original_filename : ''; if ($request->caption_type === 'auto') { $result = muxvideo_generate_captions($asset_id, $request->language_code); if ($result['success']) { $wpdb->update( $table_name, array( 'status' => 'completed', 'updated_at' => current_time('mysql'), 'error_message' => null ), array('id' => $request->id) ); } else { $is_permanent_error = strpos($result['message'], 'already has auto-generated captions') !== false; $wpdb->update( $table_name, array( 'status' => $is_permanent_error ? 'failed' : 'pending', 'error_message' => $result['message'], 'updated_at' => current_time('mysql') ), array('id' => $request->id) ); } } else if ($request->caption_type === 'custom' && !empty($request->caption_file_url)) { $result = muxvideo_upload_captions( $asset_id, $request->language_code, $request->caption_file_url, (bool)$request->closed_captions, $original_filename ); if ($result['success']) { $wpdb->update( $table_name, array( 'status' => 'completed', 'updated_at' => current_time('mysql'), 'error_message' => null ), array('id' => $request->id) ); } else { $wpdb->update( $table_name, array( 'status' => 'pending', 'error_message' => $result['message'], 'updated_at' => current_time('mysql') ), array('id' => $request->id) ); } } } } } } catch (\Exception $e) { error_log('Error checking asset status: ' . $e->getMessage()); return; } } // Now get the asset data and display captions $assetsApi = muxvideo_api_client_init(); $asset = $assetsApi->getAsset($asset_id); $asset_data = $asset->getData(); if (!$asset) { echo '
' . esc_html__('error_asset_not_found', '2coders-integration-mux-video') . '
'; wp_die(); } $has_valid_captions = false; if (isset($asset_data['tracks']) && !empty($asset_data['tracks'])) { foreach ($asset_data['tracks'] as $track) { if ($track->getType() === 'text' && $track->getLanguageCode()) { $has_valid_captions = true; break; } } } if ($has_valid_captions) { // Set up variables for the template $all_languages = array_merge( muxvideo_get_auto_caption_languages()['stable'], muxvideo_get_auto_caption_languages()['beta'], muxvideo_get_custom_caption_languages() ); // Pass the caption_page to your template $_GET['caption_page'] = $caption_page; // Include the template file with the captions content ob_start(); include MUXVIDEO_PLUGIN_DIR . '/views/template-parts/asset-captions.php'; echo ob_get_clean(); } else { echo '
' . esc_html__('asset_list_no_captions', '2coders-integration-mux-video') . '
'; } wp_die(); } catch (\Throwable $th) { wp_send_json_error(array('message' => $th->getMessage())); } } add_action('wp_ajax_muxvideo_generate_captions', 'muxvideo_ajax_generate_captions'); add_action('wp_ajax_nopriv_muxvideo_generate_captions', 'muxvideo_ajax_generate_captions'); function muxvideo_ajax_generate_captions() { try { $asset_id = isset($_POST['asset_id']) ? sanitize_text_field($_POST['asset_id']) : ''; $language_code = isset($_POST['language_code']) ? sanitize_text_field($_POST['language_code']) : ''; if (empty($asset_id) || empty($language_code)) { wp_send_json_error(__('missing_required_parameters', '2coders-integration-mux-video')); return; } $result = muxvideo_generate_captions($asset_id, $language_code); if ($result === true || (is_array($result) && isset($result['success']) && $result['success'])) { wp_send_json_success($result); } else { $error_message = is_array($result) && isset($result['message']) ? $result['message'] : __('captions_generation_failed', '2coders-integration-mux-video'); wp_send_json_error($error_message); } } catch (\Exception $e) { wp_send_json_error($e->getMessage()); } wp_die(); } /** * AJAX handler for uploading custom captions */ function muxvideo_ajax_upload_captions() { if (!current_user_can('edit_posts')) { wp_send_json_error(__('captions_manager_error_permission_denied', 'mux-video-by-2coders')); } $asset_id = isset($_POST['asset_id']) ? sanitize_text_field($_POST['asset_id']) : ''; $language_code = isset($_POST['language_code']) ? sanitize_text_field($_POST['language_code']) : ''; $closed_captions = isset($_POST['closed_captions']) && $_POST['closed_captions'] === 'true'; if (empty($asset_id) || empty($language_code)) { wp_send_json_error(__('captions_manager_error_missing_parameters', 'mux-video-by-2coders')); } // Check if file was uploaded if (!isset($_FILES['caption_file']) || $_FILES['caption_file']['error'] !== UPLOAD_ERR_OK) { $error = isset($_FILES['caption_file']) ? $_FILES['caption_file']['error'] : __('captions_manager_error_no_file', 'mux-video-by-2coders'); wp_send_json_error(sprintf(__('captions_manager_error_upload_failed', 'mux-video-by-2coders'), $error)); } // Get the original filename and extension $original_filename = $_FILES['caption_file']['name']; $file_extension = strtolower(pathinfo($original_filename, PATHINFO_EXTENSION)); // Validate file extension if (!in_array($file_extension, ['srt', 'vtt'])) { wp_send_json_error(__('captions_manager_error_format', 'mux-video-by-2coders')); } $upload = wp_handle_upload($_FILES['caption_file'], array('test_form' => false)); if (isset($upload['error'])) { wp_send_json_error($upload['error']); return; } // Create custom directory for captions $upload_dir = wp_upload_dir(); $caption_dir = $upload_dir['basedir'] . '/mux-captions/' . $asset_id; if (!file_exists($caption_dir)) { wp_mkdir_p($caption_dir); } $unique_filename = $language_code . '.' . $file_extension; $file_path = $caption_dir . '/' . $unique_filename; // Move the file from temporary location to our custom directory if (copy($upload['file'], $file_path)) { // Delete the original temporary file @unlink($upload['file']); $caption_file_url = $upload_dir['baseurl'] . '/mux-captions/' . $asset_id . '/' . $unique_filename; // Upload to Mux $result = muxvideo_upload_captions($asset_id, $language_code, $caption_file_url, $closed_captions, $original_filename); if ($result['success']) { wp_send_json_success($result); } else { // Clean up the file if upload failed @unlink($file_path); wp_send_json_error($result['message']); } } else { wp_send_json_error('Error uploading captions', 'mux-video-by-2coders'); } } add_action('wp_ajax_muxvideo_upload_captions', 'muxvideo_ajax_upload_captions'); function update_mux_asset_callback() { if ( !isset($_POST['asset_id']) || !isset($_POST['mp4_support']) ) { wp_send_json_error(array('message' => 'Invalid parameters')); } $apiInstance = muxvideo_api_client_init(); $asset_id = sanitize_text_field($_POST['asset_id']); $mp4_support = sanitize_text_field($_POST['mp4_support']); $update_asset_request = array('mp4_support' => $mp4_support); try { $clean_mp4_support = array('mp4_support' => 'none'); $apiInstance->updateAssetMp4Support($asset_id, $clean_mp4_support); $result = $apiInstance->updateAssetMp4Support($asset_id, $update_asset_request); $result; wp_send_json_success(array('message' => 'Asset updated successfully')); } catch (Exception $e) { wp_send_json_error(array('message' => 'Error updating asset: ' . $e->getMessage())); } } add_action('wp_ajax_update_mux_asset', 'update_mux_asset_callback'); function muxvideo_update_asset_settings() { if (!isset($_POST['upload_id'])) { wp_send_json_error(['message' => 'Missing upload ID']); return; } try { $video_quality = isset($_POST['video_quality']) ? sanitize_text_field($_POST['video_quality']) : 'plus'; $mp4_support = isset($_POST['mp4_support']) ? sanitize_text_field($_POST['mp4_support']) : ''; $asset_privacy = isset($_POST['asset_privacy']) ? sanitize_text_field($_POST['asset_privacy']) : 'protected'; // Create asset settings $createAssetRequest = new MuxPhp\Models\CreateAssetRequest([ "playback_policy" => $asset_privacy === 'protected' ? [MuxPhp\Models\PlaybackPolicy::SIGNED] : [MuxPhp\Models\PlaybackPolicy::_PUBLIC], "mp4_support" => $mp4_support ?: 'none', "video_quality" => $video_quality ]); // Create upload request $createUploadRequest = new MuxPhp\Models\CreateUploadRequest([ "timeout" => 6000, "new_asset_settings" => $createAssetRequest, "cors_origin" => "*" ]); $uploadsApi = muxvideo_api_client_init_upload(); $upload = $uploadsApi->createDirectUpload($createUploadRequest); $data = $upload->getData(); wp_send_json_success([ 'url' => $data->getUrl(), 'id' => $data->getId() ]); } catch (Exception $e) { wp_send_json_error([ 'message' => 'Error creating upload: ' . $e->getMessage() ]); } } add_action('wp_ajax_muxvideo_update_asset_settings', 'muxvideo_update_asset_settings'); add_action('wp_ajax_nopriv_muxvideo_update_asset_settings', 'muxvideo_update_asset_settings');