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 '