/** * Handle an uploaded caption file and associate it with the specified video * * @global BC_Accounts $bc_accounts * * @param string $account_hash * @param int $video_id * @param array $raw_captions */ public function ajax_caption_upload($account_hash, $video_id, $raw_captions) { global $bc_accounts; // Set up the account to which we're pushing data $account = $bc_accounts->set_current_account($account_hash); if (false === $account) { $bc_accounts->restore_default_account(); return; } // Sanitize our passed data $video_id = BC_Utility::sanitize_id($video_id); $captions = array(); foreach ($raw_captions as $caption) { // Validate required fields if (!isset($caption['source']) || !isset($caption['language'])) { continue; } $url = esc_url($caption['source']); $lang = sanitize_text_field($caption['language']); if (empty($url) || empty($lang)) { continue; // Attachment has no URL, fail } $label = isset($caption['label']) ? sanitize_text_field($caption['label']) : ''; $source = parse_url($caption['source']); if (0 === strpos($source['host'], 'brightcove')) { // If the hostname starts with "brightcove," assume this media has already been ingested continue; } $captions[] = new BC_Text_Track($url, $lang, 'captions', $label); } if (empty($captions)) { return; // After sanitization, we have no valid captions } // Push the captions to Brightcove $this->cms_api->text_track_upload($video_id, $captions); // Restore our global, default account $bc_accounts->restore_default_account(); }