id = 'exchange_rates';
$this->desc = __( 'Exchange Rates', 'currency-switcher-woocommerce' );
parent::__construct();
add_action( 'woocommerce_admin_field_alg_exchange_rate', array( $this, 'output_settings_button' ) );
add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_script' ) );
add_action( 'wp_ajax_' . 'alg_wc_cs_get_exchange_rate', array( $this, 'get_exchange_rate_ajax' ) );
add_action( 'wp_ajax_nopriv_' . 'alg_wc_cs_get_exchange_rate', array( $this, 'get_exchange_rate_ajax' ) );
add_action( 'admin_init', array( $this, 'process_buttons' ) );
}
/**
* get_exchange_rate_ajax.
*
* @version 2.14.0
* @since 2.2.0
*/
function get_exchange_rate_ajax() {
$currency_from = sanitize_text_field( $_POST['alg_currency_from'] );
$currency_to = sanitize_text_field( $_POST['alg_currency_to'] );
$server_override = $_POST['wpw_currency_server'] ? sanitize_text_field( $_POST['wpw_currency_server'] ) : false;
echo alg_wc_cs_get_exchange_rate( $currency_from, $currency_to, $server_override );
die();
}
/**
* enqueue_script.
*
* @version 2.15.0
* @since 1.0.0
*/
function enqueue_script() {
global $pagenow;
// check we are on the settings page
if (
$pagenow === 'admin.php'
&& isset( $_REQUEST['tab'] ) && $_REQUEST['tab'] === 'alg_wc_currency_switcher'
&& isset( $_REQUEST['section'] ) && $_REQUEST['section'] === 'exchange_rates'
) {
wp_enqueue_script( 'alg-exchange-rates', plugin_dir_url( __FILE__ ) . 'js/alg_exchange_rates.js', array( 'jquery' ), alg_wc_currency_switcher_plugin()->version, true );
wp_localize_script( 'alg-exchange-rates', 'ajax_object', array(
'ajax_url' => admin_url( 'admin-ajax.php' ),
) );
}
}
/**
* output_settings_button.
*
* @version 2.0.0
* @since 1.0.0
*/
function output_settings_button( $value ) {
$value['type'] = 'number';
$option_value = get_option( $value['id'], $value['default'] );
// Custom attribute handling
$custom_attributes = array();
if ( ! empty( $value['custom_attributes'] ) && is_array( $value['custom_attributes'] ) ) {
foreach ( $value['custom_attributes'] as $attribute => $attribute_value ) {
$custom_attributes[] = esc_attr( $attribute ) . '="' . esc_attr( $attribute_value ) . '"';
}
}
$custom_attributes_button = array();
if ( ! empty( $value['custom_attributes_button'] ) && is_array( $value['custom_attributes_button'] ) ) {
foreach ( $value['custom_attributes_button'] as $attribute => $attribute_value ) {
$custom_attributes_button[] = esc_attr( $attribute ) . '="' . esc_attr( $attribute_value ) . '"';
}
}
$tip = '';
$description = '';
?>
|
/>
/>
|
60 ) {
$desc = sprintf( __( '%s till next update.', 'currency-switcher-woocommerce' ), human_time_diff( 0, $scheduled_time_diff ) );
} elseif ( $scheduled_time_diff > 0 ) {
$desc = sprintf( __( '%s seconds till next update.', 'currency-switcher-woocommerce' ), $scheduled_time_diff );
}
}
}
$settings = array_merge( $settings, array(
array(
'title' => __( 'Exchange Rates', 'currency-switcher-woocommerce' ),
'type' => 'alg_title',
'desc' => $desc,
'id' => 'alg_wc_currency_switcher_exchange_rates_options',
'buttons' => array(
array(
'id' => 'alg_update_all_exchange_rates',
'link' => add_query_arg( 'alg_update_all_exchange_rates', '1' ),
'title' => __( 'Update all exchange rates now', 'currency-switcher-woocommerce' )
),
array(
'id' => 'alg_reset_all_exchange_rates',
'link' => add_query_arg( 'alg_reset_all_exchange_rates', '1' ),
'title' => __( 'Reset all rates', 'currency-switcher-woocommerce' )
),
),
),
array_merge(
array(
'title' => __( 'Exchange rates updates', 'currency-switcher-woocommerce' ),
'id' => 'alg_currency_switcher_exchange_rate_update',
'default' => 'manual',
'type' => 'select',
'class' => 'wc-enhanced-select',
'options' => array_merge(
array(
'manual' => __( 'Enter Rates Manually', 'currency-switcher-woocommerce' ),
'weekly' => __( 'Update Automatically Weekly', 'currency-switcher-woocommerce' ),
),
( get_option( 'wpwham_currency_switcher_version' ) === 'legacy' ? array(
'daily' => __( 'Update Automatically Daily', 'currency-switcher-woocommerce' ),
'twicedaily' => __( 'Update Automatically Twice Daily', 'currency-switcher-woocommerce' ),
'hourly' => __( 'Update Automatically Hourly', 'currency-switcher-woocommerce' ),
'minutely' => __( 'Update Automatically Every Minute', 'currency-switcher-woocommerce' ),
) : array() )
),
),
( get_option( 'wpwham_currency_switcher_version' ) !== 'legacy' ? array(
'desc' => apply_filters( 'alg_wc_currency_switcher_plugin_option', sprintf(
__( 'For faster updates, including daily, hourly, or minute-to-minute, you will need %s plugin.', 'currency-switcher-woocommerce' ),
'' .
__( 'Currency Switcher for WooCommerce Pro', 'currency-switcher-woocommerce' ) . '' ), 'settings' ),
) : array() )
),
array(
'title' => __( 'Exchange rates server', 'currency-switcher-woocommerce' ),
'id' => 'alg_currency_switcher_exchange_rate_server',
'default' => 'ecb',
'type' => 'select',
'class' => 'alg-currency-switcher-exchange-rate-server wc-enhanced-select',
'options' => alg_wc_cs_get_exchange_rates_servers(),
),
array(
'title' => __( 'Free Currency Converter API Key', 'currency-switcher-woocommerce' ),
'desc' => sprintf(
__( 'Free Currency Converter now requires an API key. Get your key at %s', 'currency-switcher-woocommerce' ),
'https://free.currencyconverterapi.com/free-api-key'
),
'type' => 'text',
'id' => 'wpw_cs_fcc_api_key',
),
array(
'title' => __( 'CoinMarketCap API Key', 'currency-switcher-woocommerce' ),
'desc' => sprintf(
__( 'CoinMarketCap now requires an API key. Get your key at %s', 'currency-switcher-woocommerce' ),
'https://coinmarketcap.com/api/pricing/'
),
'type' => 'text',
'id' => 'wpw_currency_switcher_coinmarketcap_api_key',
),
array(
'title' => __( 'Currencyapi.com API Key', 'currency-switcher-woocommerce' ),
'desc' => sprintf(
__( 'Currencyapi.com requires an API key. Get your key at %s', 'currency-switcher-woocommerce' ),
'https://currencyapi.com/pricing/'
),
'type' => 'text',
'id' => 'wpw_currency_switcher_currencyapi_api_key',
),
array(
'title' => __( 'Exchange rates offset', 'currency-switcher-woocommerce' ),
'desc' => __( 'percent', 'currency-switcher-woocommerce' ),
'id' => 'alg_currency_switcher_exchange_rate_offset',
'default' => 0,
'type' => 'number',
'custom_attributes' => array( 'step' => '0.000001' ),
),
) );
foreach ( alg_get_enabled_currencies( false ) as $i => $currency_to ) {
if ( $currency_from != $currency_to ) {
$settings = array_merge( $settings, array(
array(
'title' => '#' . ( $i + 1 ) . ' ' . $currency_from . '/' . $currency_to . ' (' . $all_currencies[ $currency_to ] . ')',
'id' => 'alg_currency_switcher_exchange_rate_' . $currency_from . '_' . $currency_to,
'default' => 1,
'type' => 'alg_exchange_rate',
'custom_attributes' => array( 'step' => 'any', 'min' => '0' ),
'custom_attributes_button' => array( 'currency_from' => $currency_from, 'currency_to' => $currency_to, 'exchange_rates_field_id' => 'alg_currency_switcher_exchange_rate_' . $currency_from . '_' . $currency_to ),
'css' => 'width:100px;',
'value' => $currency_from . '/' . $currency_to,
'value_title' => sprintf( __( 'Grab %s rate', 'currency-switcher-woocommerce' ), $currency_from . '/' . $currency_to ),
),
array(
'desc_tip' => __( 'Exchange Rates Server', 'currency-switcher-woocommerce' ),
'id' => 'alg_currency_switcher_exchange_rate_server_' . $currency_from . '_' . $currency_to,
'default' => 'default',
'type' => 'select',
'class' => 'alg-currency-switcher-exchange-rate-server wc-enhanced-select',
'options' => array_replace(
array(
'default' => __( 'Default server', 'currency-switcher-woocommerce' ) .
' (' . alg_wc_cs_get_exchange_rates_server_title( get_option( 'alg_currency_switcher_exchange_rate_server', 'ecb' ) ) . ')',
),
alg_wc_cs_get_exchange_rates_servers()
),
),
array(
'desc_tip' => __( 'Exchange Rates Offset', 'currency-switcher-woocommerce' ),
'id' => 'alg_currency_switcher_exchange_rate_offset_type_' . $currency_from . '_' . $currency_to,
'default' => 'default',
'type' => 'select',
'class' => 'wc-enhanced-select',
'options' => array(
'default' => __( 'Default offset', 'currency-switcher-woocommerce' ) .
' (' . get_option( 'alg_currency_switcher_exchange_rate_offset', 0 ) . '%)',
'custom' => __( 'Custom offset', 'currency-switcher-woocommerce' ) .
' (' . get_option( 'alg_currency_switcher_exchange_rate_offset_' . $currency_from . '_' . $currency_to, 0 ) . '%)',
),
),
array(
'desc_tip' => __( 'Custom Offset', 'currency-switcher-woocommerce' ),
'desc' => __( 'percent', 'currency-switcher-woocommerce' ),
'id' => 'alg_currency_switcher_exchange_rate_offset_' . $currency_from . '_' . $currency_to,
'default' => 0,
'type' => 'number',
'custom_attributes' => array( 'step' => '0.000001' ),
),
) );
}
}
$settings = array_merge( $settings, array(
array(
'type' => 'sectionend',
'id' => 'alg_wc_currency_switcher_exchange_rates_options',
),
) );
return $settings;
}
}
endif;
return new Alg_WC_Currency_Switcher_Settings_Exchange_Rates();