/**
  * Plugin Setup (On Activation)
  *
  * Does the initial setup,
  * stest default values for the plugin options.
  *
  * @package Easy Digital Downloads - Currency Converter
  * @since 1.0.0
  **/
 function edd_currency_install()
 {
     global $wpdb, $user_ID, $edd_options;
     //register post type
     edd_currency_reg_create_post_type();
     //IMP Call of Function
     //Need to call when custom post type is being used in plugin
     flush_rewrite_rules();
     //get option for when plugin is activating first time
     $edd_currency_set_option = get_option('edd_currency_set_option');
     //check plugin version option
     if (empty($edd_currency_set_option)) {
         $edd_page = array('post_type' => 'page', 'post_status' => 'publish', 'post_title' => __('Select Currency', 'eddcurrency'), 'post_content' => '[edd_select_currency][/edd_select_currency]', 'post_author' => 1, 'menu_order' => 0, 'comment_status' => 'closed');
         //create main page for plugin
         $edd_currency_page_id = wp_insert_post($edd_page);
         //update currency page option
         update_option('edd_currency_select_currency_page', $edd_currency_page_id);
         $edd_currencies = edd_get_currencies();
         $edd_currency_order_count = 0;
         $edd_currency_orders = array();
         foreach ($edd_currencies as $currency_code => $currency_value) {
             $edd_currency_order_count++;
             $currency_arr = array('post_title' => $currency_code, 'post_content' => $currency_value, 'post_status' => 'publish', 'post_author' => 1, 'menu_order' => $edd_currency_order_count, 'post_type' => EDD_CURRENCY_POST_TYPE);
             //create currency
             $edd_currency_id = wp_insert_post($currency_arr);
             if (!empty($edd_currency_id)) {
                 //check inserted currency id
                 $edd_currency_symbol = edd_currency_get_symbol($currency_code);
                 update_post_meta($edd_currency_id, '_edd_currency_symbol', $edd_currency_symbol);
                 // store cuurency id and order no
                 $edd_currency_orders[$edd_currency_id] = $edd_currency_order_count;
             }
         }
         //update sort order
         update_option('edd_currency_sort_order', $edd_currency_orders);
         //update sort order count
         update_option('edd_currency_sort_order_count', $edd_currency_order_count);
         //update plugin version to option
         update_option('edd_currency_set_option', '1.0');
     }
     //end if to check set option is empty or not
     //check currency set option is equal to 1.0 or not
     if ($edd_currency_set_option == '1.0') {
         //future code will here
     }
     //end if
     /*************** Default Options Saving to Options of EDD Start ***************/
     //create currency page option default
     $currency_page = get_option('edd_currency_select_currency_page');
     $udpopt = false;
     //check exchange rates method is not set
     if (!isset($edd_options['exchange_rates_method'])) {
         $edd_options['exchange_rates_method'] = 'open_exchange';
         $udpopt = true;
     }
     //end if
     //check select currency page is not set
     if (!isset($edd_options['select_currency'])) {
         $edd_options['select_currency'] = $currency_page;
         $udpopt = true;
     }
     //end if
     //check append code enabled is not set
     if (!isset($edd_options['append_code'])) {
         $edd_options['append_code'] = '';
         $udpopt = true;
     }
     //end if
     //check append code enabled is not set
     if (!isset($edd_options['curr_code_position'])) {
         $edd_options['curr_code_position'] = 'after';
         $udpopt = true;
     }
     //end if
     //check replace base currency is not set
     if (!isset($edd_options['replace_base_currency'])) {
         $edd_options['replace_base_currency'] = '';
         $udpopt = true;
     }
     //end if
     //check display cart notification is not set
     if (!isset($edd_options['display_cart_notification'])) {
         $edd_options['display_cart_notification'] = 'yes';
         $udpopt = true;
     }
     //end if
     //check base currency is not set
     if (!isset($edd_options['curr_base_currency'])) {
         $edd_options['curr_base_currency'] = edd_get_currency();
         $udpopt = true;
     }
     //end if
     //check exchange app id is not set
     if (!isset($edd_options['exchange_app_id'])) {
         $edd_options['exchange_app_id'] = '';
         $udpopt = true;
     }
     //end if
     //check exchange rates cache is not set
     if (!isset($edd_options['exchange_rates_cache'])) {
         $edd_options['exchange_rates_cache'] = '60';
         $udpopt = true;
     }
     //end if
     //check currency detection is not set
     if (!isset($edd_options['currency_detection'])) {
         $edd_options['currency_detection'] = 'no';
         $udpopt = true;
     }
     //end if
     //check prompt user detection is not set
     if (!isset($edd_options['prompt_user_detection'])) {
         $edd_options['prompt_user_detection'] = 'no';
         $udpopt = true;
     }
     //end if
     //check need to update the defaults value to options
     if ($udpopt == true) {
         // if any of the settings need to be updated
         update_option('edd_settings', $edd_options);
     }
     /*************** Default Options Saving to Options of EDD End ***************/
 }
 /**
  * Change Currency After
  * 
  * Handles to change currency After
  * 
  * @package Easy Digital Downloads - Currency Converter
  * @since 1.0.0
  */
 public function edd_currency_change_after_format($formatted, $currency, $price)
 {
     global $edd_options;
     $stored_currency = edd_currency_get_stored_currency();
     $exchange_rates = edd_currency_get_exchange_rates();
     $cur_symbol = edd_currency_get_symbol($currency);
     $new_formatted = $this->edd_currency_append_code($cur_symbol, $price, $currency, $formatted);
     //Check Our Cookie is set
     if (!empty($price) && $stored_currency != $currency && $this->model->edd_currency_check_open_exchange() && isset($exchange_rates[$stored_currency])) {
         $new_symbol = edd_currency_get_symbol($stored_currency);
         $new_price = edd_currency_get_converted_price($price);
         // only 1 line change , we can make this functions ( edd_currency_change_after_format and edd_currency_change_before_format) common in future
         $new_price_formt = $new_price . $new_symbol;
         $new_changed_format = $this->edd_currency_append_code($new_symbol, $new_price, $stored_currency, $new_price_formt);
         //Check Replace Base Currency settings is checked
         if (isset($edd_options['replace_base_currency']) && $edd_options['replace_base_currency'] == '1') {
             $new_formatted = $new_changed_format;
         } else {
             $new_formatted .= ' ( ' . $new_changed_format . ' ) ';
         }
     }
     return $new_formatted;
 }