forked from ilanco/gravity-forms-multi-currency
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgravity-forms-multi-currency.php
188 lines (148 loc) · 4.73 KB
/
gravity-forms-multi-currency.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
<?php
/**
* Plugin Name: Gravity Forms Multi Currency
* Plugin URI: /~https://github.com/ilanco/gravity-forms-multi-currency
* Description: Per form currency for Gravity Forms.
* Version: 1.7.2
* Author: Ilan Cohen <ilanco@gmail.com>
* Author URI: /~https://github.com/ilanco
*/
if (defined('WP_DEBUG') && (WP_DEBUG == true)) {
error_reporting(E_ALL);
}
// don't load directly
if (!defined('ABSPATH'))
die(false);
define('GF_MC_VERSION', '1.7.2');
define('GF_MC_MAINFILE', __FILE__);
add_action('init', array('GFMultiCurrency', 'init'), 9);
class GFMultiCurrency
{
private static $instance;
private $currency;
private $gf_mc_debug;
private function __construct()
{
if (!$this->is_gravityforms_supported()) {
return false;
}
add_action('wp', array(&$this, 'form_process'), 8);
add_filter('gform_currency', array(&$this, 'form_currency'));
if (is_admin()) {
add_action('gform_admin_pre_render', array(&$this, 'admin_pre_render'));
add_filter('gform_form_settings', array(&$this, 'custom_form_settings'), 10, 2);
add_filter('gform_pre_form_settings_save', array(&$this, 'save_custom_form_settings'));
add_action('gform_editor_js', array(&$this, 'admin_editor_js'));
add_action('gform_entry_detail_content_before', array(&$this, 'admin_entry_detail'), 10, 2);
}
else {
add_filter('gform_pre_render', array(&$this, 'pre_render'));
}
}
public static function init()
{
if (!self::$instance) {
self::$instance = new GFMultiCurrency();
}
return self::$instance;
}
public function form_process()
{
// asp: setup way to dump the form_info; probably a better way to do this
$gf_mc_debug = 0;
$form_id = isset($_POST["gform_submit"]) ? $_POST["gform_submit"] : 0;
if ($form_id) {
// asp: use new GFAPI class returns an array
$form_info = GFAPI::get_form( $form_id );
if ( $gf_mc_debug ) {
echo "form_id: $form_id<br/>";
echo 'is_active:' . $form_info['is_active'] . '<br/>';
echo '<pre>' . var_export($form_info, true) . '</pre>';
}
// asp: since the result is an array check for
// asp: is_active using array notation
$is_valid_form = $form_info && $form_info['is_active'];
if ($is_valid_form) {
$form = GFAPI::get_form( $form_id );
if (isset($form['currency']) && $form['currency']) {
$this->currency = $form['currency'];
}
}
}
}
public function form_currency($currency)
{
if ($this->currency) {
$currency = $this->currency;
}
return $currency;
}
public function admin_pre_render($form)
{
if (isset($form['currency']) && $form['currency']) {
$this->currency = $form['currency'];
}
return $form;
}
public function custom_form_settings($settings, $form)
{
ob_start();
include 'tpl/custom_form_settings.php';
$settings['Form Basics']['form_currency_setting'] = ob_get_contents();
ob_end_clean();
return $settings;
}
public function save_custom_form_settings($form)
{
$form['currency'] = rgpost('form_currency');
return $form;
}
public function admin_editor_js()
{
?>
<script type='text/javascript'>
jQuery(function($) {
$("#form_currency").change(function() {
form.currency = this.value;
});
$("#form_currency").val(form.currency);
});
</script>
<?php
}
public function admin_entry_detail($form, $lead)
{
if (isset($form['currency']) && $form['currency']) {
$this->currency = $form['currency'];
}
return $form;
}
public function pre_render($form)
{
if (isset($form['currency']) && $form['currency']) {
$this->currency = $form['currency'];
}
return $form;
}
protected function gf_get_default_currency()
{
$currency = get_option("rg_gforms_currency");
$currency = empty($currency) ? "USD" : $currency;
return $currency;
}
private function is_gravityforms_supported()
{
if (class_exists("GFCommon")) {
return true;
}
return false;
}
private function set_currency($form_id, $currency)
{
$this->currency[$form_id] = $currency;
}
private function get_currency($form_id)
{
return $this->currency[$form_id];
}
}