listPlaybackRestrictions()->getData(); foreach ($all_restrictions as $restriction) { try { $api_playback_restrictions->deletePlaybackRestriction($restriction->getId()); if ( defined( 'WP_DEBUG' ) && WP_DEBUG ) { error_log('Restriction deleted'); } } catch (Exception $e) { if ( defined( 'WP_DEBUG' ) && WP_DEBUG ) { error_log('Error deleting restriction'); } } } $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); if ( defined( 'WP_DEBUG' ) && WP_DEBUG ) { error_log('New restriction created'); } } catch (Exception $e) { if ( defined( 'WP_DEBUG' ) && WP_DEBUG ) { error_log('Error creating restriction'); } } } catch (Exception $e) { if ( defined( 'WP_DEBUG' ) && WP_DEBUG ) { error_log('Error with restrictions'); } } } else { wp_send_json_error(array('message' => 'Security verification failed')); return; } wp_die(); } /** * Display the content to Customize shortcode */ add_action( 'wp_ajax_muxvideo_display_modal_customize_shortcode', 'muxvideo_display_modal_customize_shortcode' ); function muxvideo_display_modal_customize_shortcode() { if (!isset($_POST['nonce']) || !wp_verify_nonce($_POST['nonce'], 'mux_token_nonce')) { wp_send_json_error(array('message' => 'Security verification failed')); return; } try { include_once MUXVIDEO_PLUGIN_DIR . '/views/template-parts/customize-shortcode.php'; wp_die(); } catch (\Throwable $th) { if ( defined( 'WP_DEBUG' ) && WP_DEBUG ) { error_log( "Error: muxvideo_display_modal_customize_shortcode"); } echo '
' . __('error_try_again', '2coders-integration-mux-video') . '
'; } } /** * Display the content to Customize shortcode */ add_action( 'wp_ajax_muxvideo_get_jwt_dinamically', 'muxvideo_get_jwt_dinamically' ); function muxvideo_get_jwt_dinamically() { if (!isset($_POST['nonce']) || !wp_verify_nonce($_POST['nonce'], 'mux_token_nonce')) { wp_send_json_error(array('message' => 'Security verification failed')); return; } $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) { if ( defined( 'WP_DEBUG' ) && WP_DEBUG ) { error_log( "Error: muxvideo_get_jwt_dinamically"); } echo '
' . __('error_try_again', '2coders-integration-mux-video') . '
'; } } 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' ); function muxvideo_load_more_assets() { if (!isset($_POST['nonce']) || !wp_verify_nonce($_POST['nonce'], 'mux_token_nonce')) { wp_send_json_error(array('message' => 'Security verification failed')); return; } if ( ! current_user_can( 'upload_files' ) ) { wp_die( __( 'You do not have permission to access this page.', '2coders-integration-mux-video' ) ); } 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' ); function muxvideo_delete_asset() { require_once MUXVIDEO_PLUGIN_DIR . '/includes/functions.php'; global $wpdb; if ( isset($_POST['nonce']) && wp_verify_nonce($_POST['nonce'], 'mux_token_nonce') ) { try { $assetsApi = muxvideo_api_client_init(); $assetsApi->deleteAsset( sanitize_text_field( $_REQUEST['asset_id'] ) ); global $wpdb; $wpdb->delete( $wpdb->prefix . 'mux_caption_queue', array( 'asset_id' => sanitize_text_field( $_REQUEST['asset_id'] ) ), array( '%s' ) ); } catch (Exception $e) { if ( defined( 'WP_DEBUG' ) && WP_DEBUG ) { error_log( 'Exception when calling AssetsApi->deleteAsset'); } echo '
' . __('error_delete', '2coders-integration-mux-video') . '
'; } wp_die(); } } /** * Create a new upload url to upload a new asset */ add_action( 'wp_ajax_muxvideo_reset_upload_url', 'muxvideo_reset_upload_url' ); function muxvideo_reset_upload_url() { if (!isset($_POST['nonce']) || !wp_verify_nonce($_POST['nonce'], 'mux_token_nonce')) { wp_send_json_error(array('message' => 'Security verification failed')); return; } require_once MUXVIDEO_PLUGIN_DIR . '/includes/functions.php'; $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'; $title = isset($_POST['title']) ? sanitize_text_field($_POST['title']) : ''; 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_policies" => $asset_privacy, "meta" => [ "title" => $title ], ]); $createUploadRequest = new MuxPhp\Models\CreateUploadRequest([ "timeout" => 3600, "new_asset_settings" => $createAssetRequest, "cors_origin" => "*" ]); $upload = $uploadsApi->createDirectUpload($createUploadRequest); $data = $upload->getData(); $new_upload_id = $data->getId(); // Iniciar proceso de actualización con reintento automático (empieza en intento 1) // muxvideo_update_asset_metadata_later($new_upload_id, $title, 1); wp_send_json([ 'data' => [ 'url' => $data->getUrl(), 'id' => $new_upload_id ] ]); } catch (Exception $e) { if ( defined( 'WP_DEBUG' ) && WP_DEBUG ) { error_log('Exception when creating upload'); } echo '
' . __('error_upload', '2coders-integration-mux-video') . '
'; } wp_die(); } add_action('wp_ajax_muxvideo_start_metadata_polling', 'muxvideo_start_metadata_polling'); function muxvideo_start_metadata_polling() { if (!isset($_POST['nonce']) || !wp_verify_nonce($_POST['nonce'], 'mux_token_nonce')) { wp_send_json_error(array('message' => 'Security verification failed')); return; } require_once MUXVIDEO_PLUGIN_DIR . '/includes/functions.php'; $upload_id = isset($_POST['upload_id']) ? sanitize_text_field($_POST['upload_id']) : ''; $title = isset($_POST['title']) ? sanitize_text_field($_POST['title']) : ''; $apiInstance = muxvideo_api_client_init(); $assets = $apiInstance->listAssets(25, 1, null, $upload_id); $asset = $assets->getData()[0]; $asset_id = $asset->getId(); $update_asset_request = [ 'meta' => [ 'title' => $title ] ]; $apiInstance->updateAsset($asset_id, $update_asset_request); if (!$upload_id || !$title) { wp_send_json_error(['message' => 'Faltan parámetros']); } muxvideo_update_asset_metadata_later($upload_id, $title, 1); wp_send_json_success(['message' => 'Intento de actualización programado.']); } /** * Refresh asset list dynamically */ add_action( 'wp_ajax_muxvideo_refresh_asset_list', 'muxvideo_refresh_asset_list' ); function muxvideo_refresh_asset_list() { if (!isset($_REQUEST['nonce']) || !wp_verify_nonce($_REQUEST['nonce'], 'mux_token_nonce')) { wp_send_json_error(array('message' => 'Security verification failed')); return; } try { muxvideo_display_assets_list(); wp_die(); } catch (\Exception $e) { if ( defined( 'WP_DEBUG' ) && WP_DEBUG ) { error_log( 'Exception when calling muxvideo_refresh_asset_list'); } echo '
' . __('error_displaying_list', '2coders-integration-mux-video') . '
'; } } /** * 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) { if ( defined( 'WP_DEBUG' ) && WP_DEBUG ) { error_log('Exception when calling AssetsApi->deleteTrack'); } wp_send_json_error(array( 'message' => __('caption_delete_error', '2coders-integration-mux-video'), )); } wp_die(); } add_action('wp_ajax_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'); function muxvideo_display_modal_manage_captions() { if (!isset($_POST['nonce']) || !wp_verify_nonce($_POST['nonce'], 'mux_token_nonce')) { wp_send_json_error(array('message' => 'Security verification failed')); return; } 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) { if ( defined( 'WP_DEBUG' ) && WP_DEBUG ) { error_log("Error: muxvideo_display_modal_manage_captions"); } echo '
' . __('error_try_again', '2coders-integration-mux-video') . '
'; } } /** * Ajax handler for loading asset captions */ add_action('wp_ajax_muxvideo_get_asset_captions', 'muxvideo_get_asset_captions'); function muxvideo_get_asset_captions() { if (!isset($_POST['nonce']) || !wp_verify_nonce($_POST['nonce'], 'mux_token_nonce')) { wp_send_json_error(array('message' => 'Security verification failed')); return; } 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) { if ( defined( 'WP_DEBUG' ) && WP_DEBUG ) { 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(__('An error occurred. Please try again.', 'text-domain')); } } add_action('wp_ajax_muxvideo_generate_captions', 'muxvideo_ajax_generate_captions'); function muxvideo_ajax_generate_captions() { if (!isset($_POST['nonce']) || !wp_verify_nonce($_POST['nonce'], 'mux_token_nonce')) { wp_send_json_error(array('message' => 'Security verification failed')); return; } 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(__('An error occurred. Please try again.', 'text-domain')); } wp_die(); } /** * AJAX handler for uploading custom captions */ function muxvideo_ajax_upload_captions() { if (!isset($_POST['nonce']) || !wp_verify_nonce($_POST['nonce'], 'mux_token_nonce')) { wp_send_json_error(array('message' => 'Security verification failed')); return; } if (!current_user_can('upload_files') || !current_user_can('manage_options')) { 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(__('An error occurred. Please try again.', 'text-domain')); return; } // Create custom directory for captions $upload_dir = wp_upload_dir(); $asset_id = preg_replace('/[^a-zA-Z0-9_-]/', '', $asset_id); $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(__('An error occurred. Please try again.', 'text-domain')); } } 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')); } } 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'; $title = isset($_POST['title']) ? sanitize_text_field($_POST['title']) : ''; // 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, "meta" => [ "title" => $title ] ]); // 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' ]); } } add_action('wp_ajax_muxvideo_update_asset_settings', 'muxvideo_update_asset_settings'); // CREATE TABLE IN THE DATABASE FOR MUX ASSETS add_action('wp_ajax_save_mux_asset', 'handle_save_mux_asset'); function handle_save_mux_asset() { if (!isset($_POST['nonce']) || !wp_verify_nonce($_POST['nonce'], 'mux_token_nonce')) { wp_send_json_error(array('message' => 'Security verification failed')); return; } require_once MUXVIDEO_PLUGIN_DIR . '/includes/functions.php'; if (!current_user_can('upload_files') || !current_user_can('manage_options')) { wp_send_json_error('Insufficient permissions'); } $title = isset($_POST['title']) ? sanitize_text_field($_POST['title']) : ''; if (empty($title)) { wp_send_json_error('Empty title.'); } if (isset($_FILES['file'])) { $file = $_FILES['file']; $finfo = finfo_open(FILEINFO_MIME_TYPE); $mime_type = finfo_file($finfo, $file['tmp_name']); finfo_close($finfo); $allowed_mimes = [ 'video/mp4','video/x-m4v','video/avi','video/quicktime','video/mpeg','video/x-ms-wmv', 'video/x-flv','video/webm','video/x-matroska','video/ogg','video/3gpp','video/3gpp2', 'audio/mpeg','audio/mp3','audio/wav','audio/ogg','audio/mp4','audio/flac','audio/amr', 'audio/x-m4a','audio/x-m4r' ]; if (!in_array($mime_type, $allowed_mimes)) { wp_send_json_error('Invalid file type.'); } $extension = strtolower(pathinfo($file['name'], PATHINFO_EXTENSION)); $allowed_extensions = ['mp4','m4v','avi','mov','mpeg','wmv','flv','webm','mkv','ogg','3gp','3g2', 'mp3','wav','flac','amr','m4a','m4r' ]; if (!in_array($extension, $allowed_extensions)) { wp_send_json_error('Invalid extension.'); } $filename = sanitize_file_name($file['name']); $upload = wp_handle_upload($file, ['test_form' => false]); if (isset($upload['error'])) { wp_send_json_error(__('An error occurred. Please try again.', 'text-domain')); } $file_url = $upload['url']; } $uploadsApi = muxvideo_api_client_init(); $data = $uploadsApi->listAssets(1, 1); $playback_restrictions_data = muxvideo_get_data_playback_restrictions(); $ID = ""; createMuxDatabaseTable(); foreach ($data->getData() as $asset) { $asset_data = muxvideo_get_asset_data($asset, $playback_restrictions_data); $ID .= $asset_data['id']; } insertMuxAsset($title, $ID, isset($file_url) ? $file_url : ''); wp_send_json_success([ 'message' => 'Asset saved!', 'title' => $title, 'mux_id' => $ID, 'file_url' => isset($file_url) ? $file_url : '' ]); } add_action('wp_ajax_save_mux_asset_title', 'handle_save_mux_asset_title'); function handle_save_mux_asset_title() { if (!isset($_POST['nonce']) || !wp_verify_nonce($_POST['nonce'], 'mux_token_nonce')) { wp_send_json_error(array('message' => 'Security verification failed')); return; } require_once MUXVIDEO_PLUGIN_DIR . '/includes/functions.php'; if (!current_user_can('upload_files') || !current_user_can('manage_options')) { wp_send_json_error('Insufficient permissions'); } if (empty($_POST['title']) || empty($_POST['mux_id'])) { wp_send_json_error('Uncomplete data.'); } $apiInstance = muxvideo_api_client_init(); $title = sanitize_text_field($_POST['title']); $mux_id = sanitize_text_field($_POST['mux_id']); global $wpdb; $table = $wpdb->prefix . 'mux_assets'; $exists = $wpdb->get_var( $wpdb->prepare("SELECT COUNT(*) FROM {$table} WHERE mux_id = %s", $mux_id) ); if ($exists) { updateMuxAssetTitle($title, $mux_id); } else { insertMuxAsset($title, $mux_id); } $update_asset_request = [ 'meta' => [ 'title' => $title ] ]; $apiInstance->updateAsset($mux_id, $update_asset_request); wp_send_json_success('Asset guardado'); } function createMuxDatabaseTable() { global $wpdb; $table = $wpdb->prefix . 'mux_assets'; $charset_collate = $wpdb->get_charset_collate(); $sql = "CREATE TABLE $table ( id mediumint(9) NOT NULL AUTO_INCREMENT, title varchar(255) NOT NULL, mux_id varchar(255) NOT NULL, PRIMARY KEY (id) ) $charset_collate;"; require_once(ABSPATH . 'wp-admin/includes/upgrade.php'); dbDelta($sql); } function insertMuxAsset($title, $mux_id) { global $wpdb; $table = $wpdb->prefix . 'mux_assets'; $wpdb->insert( $table, [ 'title' => sanitize_text_field($title), 'mux_id' => sanitize_text_field($mux_id), ], ['%s', '%s'] ); } function updateMuxAssetTitle($title, $mux_id) { global $wpdb; $table = $wpdb->prefix . 'mux_assets'; $wpdb->update( $table, [ 'title' => sanitize_text_field($title),], [ 'mux_id' => sanitize_text_field($mux_id),], ['%s'], ['%s'] ); }