public function trigger_background_fetch()
 {
     $this->set_time_limit();
     $videos = new BC_Videos();
     $videos->sync_videos();
     $playlists = new BC_Playlists();
     $playlists->sync_playlists();
 }
 /**
  * Update a video or a playlist via ajax
  *
  * @global BC_Accounts $bc_accounts
  */
 public function bc_ajax_update_video_or_playlist()
 {
     global $bc_accounts;
     // Call this to check if we are allowed to continue..
     $this->bc_helper_check_ajax();
     $type_msg = '';
     $updated_data = array();
     // Check if playlist or video data was sent.
     $fields = array('long_description', 'description', 'name', 'playlist_id', 'video_id', 'tags', 'width', 'height');
     foreach ($fields as $field) {
         $updated_data[$field] = isset($_POST[$field]) ? sanitize_text_field($_POST[$field]) : '';
     }
     // Only Playlists have playlist_videos. We only do this if we're updating playlists.
     if (isset($_POST['playlist_videos'])) {
         $playlist_videos = array();
         foreach ($_POST['playlist_videos'] as $video_id) {
             $playlist_videos[] = BC_Utility::sanitize_id($video_id);
         }
         $updated_data['playlist_videos'] = $playlist_videos;
     }
     // Only Videos have a video_id so we only do this when updating a video.
     if (isset($_POST['video_id'])) {
         $updated_data['video_id'] = BC_Utility::sanitize_id($_POST['video_id']);
     }
     // If custom fields are sent, be sure to sanitize them separately
     if (isset($_POST['custom_fields'])) {
         $custom = array();
         foreach ($_POST['custom_fields'] as $id => $value) {
             $id = sanitize_text_field($id);
             $value = sanitize_text_field($value);
             if (!empty($id) && !empty($value)) {
                 $custom[$id] = $value;
             }
         }
         // Get a list of available custom fields
         $fields = $this->cms_api->video_fields();
         $use_history = false;
         foreach ($fields['custom_fields'] as $item) {
             if ('_change_history' == $item['id']) {
                 $use_history = true;
                 break;
             }
         }
         // Build out history if it's supported
         if ($use_history) {
             $history = null;
             if (isset($_POST['history'])) {
                 $raw = wp_unslash($_POST['history']);
                 $history = json_decode($raw, true);
             }
             if (null === $history) {
                 $history = array();
             }
             $user = wp_get_current_user();
             $history[] = array('user' => $user->user_login, 'time' => date('Y-m-d H:i:s', time()));
             $custom['_change_history'] = json_encode($history);
         }
         $updated_data['custom_fields'] = $custom;
     }
     $updated_data['update-playlist-metadata'] = sanitize_text_field($_POST['nonce']);
     $status = false;
     if (!in_array($_POST['type'], array('playlists', 'videos'))) {
         wp_send_json_error(__('Type is not specified', 'brightcove'));
     }
     $hash = $_POST['account'];
     if (false === $bc_accounts->get_account_by_hash($hash)) {
         wp_send_json_error(__('No such account exists', 'brightcove'));
     }
     $updated_data['account'] = $hash;
     if ('playlists' === $_POST['type']) {
         $updated_data['playlist-name'] = $updated_data['name'];
         $status = $this->playlists->update_bc_playlist($updated_data);
         $type_msg = 'playlist';
     } elseif ('videos' === $_POST['type']) {
         $status = $this->videos->update_bc_video($updated_data);
         $type_msg = 'video';
         // Maybe update poster
         if (isset($_POST['poster'])) {
             $poster_data = json_decode(wp_unslash($_POST['poster']), true);
             if ($poster_data) {
                 // Maybe update poster
                 $this->ajax_poster_upload($hash, $updated_data['video_id'], $poster_data['url'], $poster_data['width'], $poster_data['height']);
             }
         }
         // Maybe update thumbnail
         if (isset($_POST['thumbnail'])) {
             $thumb_data = json_decode(wp_unslash($_POST['thumbnail']), true);
             if ($thumb_data) {
                 // Maybe update poster
                 $this->ajax_thumb_upload($hash, $updated_data['video_id'], $thumb_data['url'], $thumb_data['width'], $thumb_data['height']);
             }
         }
         if (isset($_POST['captions'])) {
             // Maybe update captions
             $this->ajax_caption_upload($hash, $updated_data['video_id'], $_POST['captions']);
         }
     }
     BC_Utility::delete_cache_item('*');
     $bc_accounts->restore_default_account();
     BC_Utility::delete_cache_item('*');
     // Clear the cache of video lists retrieved.
     if (true === $status) {
         wp_send_json_success(esc_html__('Successfully updated ', 'brightcove') . esc_html($type_msg));
     } elseif (is_wp_error($status)) {
         wp_send_json_error(esc_html__('Failed to sync with WordPress!', 'brightcove'));
     } else {
         wp_send_json_error(esc_html__('Failed to update ', 'brightcove') . esc_html($type_msg) . '!');
     }
 }