public function bc_ajax_delete_video_or_playlist()
 {
     // call this to check if we are allowed to continue
     $this->bc_helper_check_ajax();
     $type = sanitize_key($_POST['type']);
     $type_msg = '';
     $id = BC_Utility::sanitize_id($_POST['id']);
     $existing_post = null;
     // get type brightcove-playlist or brightcove-video
     if (!in_array($type, array('playlists', 'videos'))) {
         wp_send_json_error(esc_html__('Type is not specified!', 'brightcove'));
     }
     // try to get the existing post based on id
     if ('playlists' === $type) {
         // get playlist
         $existing_post = $this->playlists->get_playlist_by_id($id);
         $type_msg = 'playlist';
     } elseif ($type === 'videos') {
         // find existing video
         $existing_post = $this->videos->get_video_by_id($id);
         $type_msg = 'video';
     } else {
         wp_send_json_error(esc_html__('Wrong type is specified!', 'brightcove'));
     }
     if (!is_a($existing_post, 'WP_Post')) {
         wp_send_json_error(esc_html__(ucfirst($type_msg) . ' doesn\'t exist', 'brightcove'));
     }
     global $bc_accounts;
     $hash = sanitize_text_field($_POST['account']);
     if (false === $bc_accounts->get_account_by_hash($hash)) {
         wp_send_json_error(__('No such account exists', 'brightcove'));
     }
     $bc_accounts->set_current_account($hash);
     // Remove from Brightcove
     $delete_status = false;
     $delete_message = '';
     if ($type === 'videos') {
         $delete_status = $this->cms_api->video_delete($id);
         if (!$delete_status) {
             // We were not able to delete video from Brightcove, so force a resync to get back our media object
             $this->videos->sync_videos();
             $delete_message = esc_html__('Unable to remove video from Brightcove!', 'brightcove');
         } else {
             $delete_message = esc_html__('Successfully deleted your video.', 'brightcove');
         }
     } elseif ($type === 'playlists') {
         $delete_status = $this->cms_api->playlist_delete($id);
         if (!$delete_status) {
             // We were not able to delete playlist from Brightcove, so force a resync to get back our media object
             $this->playlists->sync_playlists();
             $delete_message = esc_html__('Unable to remove playlist from Brightcove!', 'brightcove');
         } else {
             $delete_message = esc_html__('Successfully deleted your playlist.', 'brightcove');
         }
     }
     BC_Utility::clear_cached_api_requests('all');
     BC_Utility::clear_cached_api_requests($bc_accounts->get_account_id());
     $bc_accounts->restore_default_account();
     if ($delete_status) {
         $deleted_obj = BC_Utility::delete_object($existing_post->ID);
         if (!$deleted_obj || 0 === $deleted_obj) {
             $delete_message = esc_html__('Unable to remove ' . $type_msg . ' from WordPress!', 'brightcove');
             // We couldn't delete the post, lets try a sync and hopefully that clears it up for us.
             if ('videos' === $type) {
                 $this->videos->sync_videos();
             } else {
                 $this->playlists->sync_playlists();
             }
         }
     }
     if ($delete_status) {
         wp_send_json_success($delete_message);
     } else {
         wp_send_json_error($delete_message);
     }
 }
 /**
  * Provides the handler for saving/updating source data
  *
  * @return bool/WP_Error
  */
 public function save_account()
 {
     global $bc_accounts;
     if (!isset($_POST['brightcove-check_oauth'])) {
         return false;
     }
     if (!current_user_can('brightcove_manipulate_accounts')) {
         $error_message = esc_html__('You do not have permission to manage this account.', 'brightcove');
         BC_Logging::log(sprintf('ACCOUNT: %s', $error_message));
         $this->notices[] = array('message' => $error_message, 'type' => 'error');
         return new WP_Error('brightcove-account-manage-permissions', $error_message);
     }
     if (!wp_verify_nonce($_POST['brightcove-check_oauth'], '_brightcove_check_oauth_for_source')) {
         return false;
     }
     // Only go through the oAuth credential validation when we're adding a new account or editing the account's credentials (not default players etc)
     if ('create' === $_POST['source-action']) {
         $required_keys = array('brightcove-check_oauth', 'source-account-id', 'source-client-id', 'source-client-secret', 'source-name');
         foreach ($required_keys as $key) {
             if (!array_key_exists($key, $_POST)) {
                 return false;
             }
         }
         $account_id = BC_Utility::sanitize_id($_POST['source-account-id']);
         $client_id = sanitize_text_field($_POST['source-client-id']);
         $client_secret = BC_Utility::get_sanitized_client_secret($_POST['source-client-secret']);
         $account_name = sanitize_text_field(stripslashes_deep($_POST['source-name']));
         $set_default = isset($_POST['source-default-account']) && 'on' === $_POST['source-default-account'] ? 'default' : '';
         $hash = BC_Utility::get_hash_for_account(array('account_id' => $account_id, 'client_id' => $client_id, 'client_secret' => $client_secret));
         $account = $bc_accounts->get_account_by_hash($hash);
         if ($account) {
             // Account already exists
             $error_message = esc_html__('The Brightcove credentials provided already exist.', 'brightcove');
             BC_Logging::log(sprintf('BC ACCOUNTS: %s', $error_message));
             $this->notices[] = array('message' => $error_message, 'type' => 'error');
             return new WP_Error('bc-account-exists-error', $error_message);
         }
         if (!$bc_accounts->add_account($account_id, $client_id, $client_secret, $account_name, $set_default, false)) {
             $error_message = esc_html__('We could not authenticate your credentials with Brightcove', 'brightcove');
             BC_Logging::log(sprintf('BC OAUTH ERROR: %s', $error_message));
             $this->notices[] = array('message' => $error_message, 'type' => 'error');
             return new WP_Error('bc-oauth-error', $error_message);
         }
         BC_Utility::clear_cached_api_requests('all');
         $bc_accounts->set_current_account_by_id($account_id);
         $players = new BC_Players();
         $players->sync_players();
     }
     if ('update' === $_POST['source-action']) {
         if (isset($_POST['source-default-account']) && 'on' === $_POST['source-default-account']) {
             update_option('_brightcove_default_account', sanitize_text_field($_POST['hash']));
         }
     }
     // Deleting transient to allow syncing from the new account, otherwise we won't be able to sync it until this transient expires.
     delete_transient('brightcove_sync_videos');
     $this->notices[] = array('message' => sprintf('%s <a href="%s">%s</a>.', esc_html__('Congratulations! Your credentials have been authenticated. Return to', 'brightcove'), admin_url('admin.php?page=brightcove-sources '), esc_html__('Settings', 'brightcove')), 'type' => 'updated');
     return true;
 }