public function delete_account($hash) { if (!BC_Accounts::get_account_by_hash($hash)) { return new WP_Error('brightcove-account-not-configured', esc_html__('The specified Brightcove Account has not been configured in WordPress', 'brightcove')); } $all_accounts = $this->get_all_accounts(); $account_id = $all_accounts[$hash]['account_id']; $account_id_in_accounts_list = false; foreach ($all_accounts as $account) { if ($account_id === $account['account_id']) { $account_id_in_accounts_list = true; } } if ($account_id_in_accounts_list) { //only run deletion for the provided account ID if it is actually an active account. BC_Utility::remove_all_media_objects_for_account_id($account_id); } unset($all_accounts[$hash]); if ($hash === get_option('_brightcove_default_account')) { if (!empty($all_accounts)) { $remaining_accounts = $all_accounts; // The deleted account was the default. Now set the default to be the first account in the remaining list, if there is a remaining account $default_account = array_shift($remaining_accounts); $new_default_account = $default_account['hash']; } else { // Set default account to false instead of delete $new_default_account = false; } update_option('_brightcove_default_account', $new_default_account); } update_option($this->options_key, $all_accounts); return true; }