php 重力Wiz //重力形式//货币转换器
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了php 重力Wiz //重力形式//货币转换器相关的知识,希望对你有一定的参考价值。
<?php
/**
* Gravity Wiz // Gravity Forms // Currency Converter
*
* Enter prices in default currency and display prices in another currency.
*
* @version 0.2
* @author David Smith <david@gravitywiz.com>
* @license GPL-2.0+
* @link http://gravitywiz.com/
*
* Plugin Name: Gravity Forms Currency Converter
* Plugin URI: http://gravitywiz.com/
* Description: Enter prices in default currency and display prices in another currency.
* Author: Gravity Wiz
* Version: 0.2
* Author URI: http://gravitywiz.com
*/
class GW_Currency_Converter {
protected static $is_script_output = false;
public function __construct( $args = array() ) {
// set our default arguments, parse against the provided arguments, and store for use throughout the class
$this->_args = wp_parse_args( $args, array(
'app_id' => '',
) );
// do version check in the init to make sure if GF is going to be loaded, it is already loaded
add_action( 'init', array( $this, 'init' ) );
}
public function init() {
// make sure we're running the required minimum version of Gravity Forms
if( ! property_exists( 'GFCommon', 'version' ) || ! version_compare( GFCommon::$version, '1.8', '>=' ) ) {
return;
}
add_filter( 'gform_tooltips', array( $this, 'add_tooltips' ) );
add_action( 'gform_form_settings', array( $this, 'add_form_setting' ), 10, 2 );
add_action( 'gform_pre_form_settings_save', array( $this, 'save_form_settings' ) );
add_filter( 'gform_pre_render', array( $this, 'load_form_script' ), 10, 2 );
add_filter( 'gform_register_init_scripts', array( $this, 'add_init_script' ), 10, 2 );
add_filter( 'gform_enqueue_scripts', array( $this, 'add_currency_global' ) );
//add_filter( 'gform_pre_render', array( $this, 'prepare_form' ), 10, 2 );
}
public function add_tooltips( $tooltips ) {
$tooltips['gwcc_convert_currency'] = sprintf(
'<h6>%s</h6> %s',
__( 'Convert Currency on Render' ),
sprintf( __( 'Enable this option to convert the currency from your configured currency (%s) to the selected currency on render.' ), GFCommon::get_currency() )
);
return $tooltips;
}
public function add_form_setting( $form_settings, $form ) {
ob_start();
$current_currency = rgar( $form, 'gwccConvertCurrency' );
?>
<tr>
<th>
<label for="gwcc_convert_currency"><?php printf( '%s %s', __( 'Convert Currency on Render' ), gform_tooltip( 'gwcc_convert_currency', '', true ) ); ?></label>
</th>
<td>
<select id="gwcc_convert_currency" name="gwcc_convert_currency">
<option value=""><?php _e( 'No conversion.', 'gw-currency-converter' ); ?></option>
<?php foreach ( RGCurrency::get_currencies() as $code => $currency ): ?>
<option value="<?php echo esc_attr( $code ) ?>" <?php selected( $current_currency, $code ); ?>><?php echo esc_html( $currency['name'] ) ?></option>
<?php endforeach; ?>
</select>
</td>
</tr>
<?php
$form_settings['Form Options']['gwcc_convert_currency'] = ob_get_clean();
return $form_settings;
}
public function save_form_settings( $form ) {
if( isset( $_POST['gwcc_convert_currency'] ) ) {
$form['gwccConvertCurrency'] = $_POST['gwcc_convert_currency'];
}
return $form;
}
public function load_form_script( $form, $is_ajax_enabled ) {
// 1 - Make sure it's the right form.
// 2 - Make sure we haven't already output the script.
// 3 - Make sure we don't output the script if any 3rd party code is running 'gform_pre_render' prematurely.
$did_header = did_action( 'wp_head' ) || rgget( 'gf_page' ) == 'preview';
if( $this->is_applicable_form( $form ) && ! self::$is_script_output && ! $this->is_ajax_submission( $form['id'], $is_ajax_enabled ) && $did_header ) {
$this->output_script();
}
return $form;
}
public function is_ajax_submission( $form_id, $is_ajax_enabled ) {
return isset( GFFormDisplay::$submission[ $form_id ] ) && $is_ajax_enabled;
}
public function output_script() {
?>
<script type="text/javascript">
( function( $ ) {
window.GWCurrencyConverter = function( args ) {
var self = this;
// copy all args to current object: (list expected props)
for( prop in args ) {
if( args.hasOwnProperty( prop ) )
self[prop] = args[prop];
}
self.init = function() {
// do the magic
};
GWCurrencyConverter.convert = function( value, currencyCode ) {
var targetRate = rgars( GWCurrencyConverterData.rates.rates, currencyCode ),
converted = value * targetRate;
return converted;
};
self.init();
};
window.origGformFormatMoney = window.gformFormatMoney;
window.gformFormatMoney = function( text, isNumeric ) {
if( ! gf_global.gf_currency_config ) {
return text;
}
// If number has not already been cleaned, use global currency to clean the number.
if( ! isNumeric ) {
var currency = new Currency( gf_global.gf_currency_config );
text = gformCleanNumber( number, currency.currency['symbol_right'], currency.currency['symbol_left'], currency.currency['decimal_separator'] );
}
text = GWCurrencyConverter.convert( text, GWCurrencyConverterData.currencyCode );
currency = new Currency( GWCurrencyConverterData.currency );
return currency.toMoney( text, isNumeric );
}
} )( jQuery );
</script>
<?php
self::$is_script_output = true;
}
public function add_currency_global( $form ) {
if( $this->is_applicable_form( $form ) ) {
wp_localize_script( 'gform_gravityforms', 'GWCurrencyConverterData', array(
'currencyCode' => $form['gwccConvertCurrency'],
'currency' => RGCurrency::get_currency( $form['gwccConvertCurrency'] ),
'rates' => $this->get_conversion_rates(),
) );
}
}
public function add_init_script( $form ) {
if( ! $this->is_applicable_form( $form ) ) {
return;
}
$args = array( );
$script = 'new GWCurrencyConverter( ' . json_encode( $args ) . ' );';
$slug = implode( '_', array( 'gw_currency_converter', $form['id'] ) );
GFFormDisplay::add_init_script( $form['id'], $slug, GFFormDisplay::ON_PAGE_RENDER, $script );
}
public function get_conversion_rates() {
$rates = get_transient( 'gwcc_rates' );
if( $rates !== false ) {
return $rates;
}
$url = 'https://openexchangerates.org/api/latest.json?app_id=' . $this->_args['app_id'];
$response = wp_remote_get( $url );
if( wp_remote_retrieve_response_code( $response ) == 200 ) {
$rates = json_decode( wp_remote_retrieve_body( $response ) );
set_transient( 'gwcc_rates', $rates, 60 * 60 * 24 );
}
return $rates;
}
public function is_applicable_form( $form ) {
return rgar( $form, 'gwccConvertCurrency' );
}
}
# Configuration
new GW_Currency_Converter( array(
'app_id' => '1c65801c9d1e40489c7601aee1252269'
) );
以上是关于php 重力Wiz //重力形式//货币转换器的主要内容,如果未能解决你的问题,请参考以下文章
php 重力Wiz //重力形式//修改自定义字段的日期格式
php 重力Wiz //重力形式//修改条目中的日期字段格式