-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwp-dolarizate.php
191 lines (150 loc) · 5.41 KB
/
wp-dolarizate.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
188
189
190
191
<?php
/**
* Plugin Name: WP Dolarizate
* Plugin URI: https://github.com/PecceG2/wp-dolarizate/
* Description: WP Dolarizate is a WordPress plugin for automatically currency exchange and display with Visual Composer.
* Version: 0.1
* Author: Giuliano Peccetto
* Author URI: https://www.pecceg2.com/
*/
/*########################################################################*/
// SETTINGS PAGE //
/*########################################################################*/
require_once dirname( __FILE__ ) .'/config-page.php';
add_action('admin_menu', 'create_settings_page');
function create_settings_page() {
add_menu_page('WP Dolarizate', 'WP Dolarizate', 'administrator', __FILE__, 'settings_page' , plugins_url('/icon.png', __FILE__) );
add_action( 'admin_init', 'register_settings' );
}
add_action('admin_post_wpd_update_config', 'save_settings_page');
function save_settings_page() {
update_option('api_dolar', $_POST['api_dolar']);
update_option('update_time', $_POST['update_time']);
update_option('dolar_type', $_POST['dolar_type']);
update_option('dolar_type_cv', $_POST['dolar_type_cv']);
$dolar_valor_manual = $_POST['valor_dolar_manual'];
switch($_POST['api_dolar']){
case 0:
//DolarSI
currencyUpdate_DolarSI();
break;
case 1:
//BCRA
break;
case 2:
//BNA
break;
case 3:
//XEC
break;
case 4:
// Manual
currencyUpdate_Manual($dolar_valor_manual);
break;
}
wp_redirect($_SERVER["HTTP_REFERER"], 302, 'WordPress');
}
function register_settings() {
register_setting('wp-dolarizate-settings', 'api_dolar');
register_setting('wp-dolarizate-settings', 'update_time');
register_setting('wp-dolarizate-settings', 'dolar_type');
register_setting('wp-dolarizate-settings', 'dolar_type_cv');
register_setting('wp-dolarizate-settings', 'dolar_value');
}
/*########################################################################*/
// END SETTINGS PAGE //
/*########################################################################*/
/*########################################################################*/
// CRON JOBS //
/*########################################################################*/
function currencyUpdate_DolarSI(){
$content = getCURL("https://www.dolarsi.com/api/api.php?type=valoresprincipales", true);
switch(get_option("dolar_type_cv")){
case "dolarCompra":
$dolVal = $content[get_option("dolar_type")]->casa->compra;
break;
case "dolarVenta":
$dolVal = $content[get_option("dolar_type")]->casa->venta;
break;
case "dolarAgencia":
$dolVal = $content[get_option("dolar_type")]->casa->agencia;
break;
default:
$dolVal = 0;
break;
}
update_option('dolar_value', $dolVal);
return($content);
}
function currencyUpdate_EstadisticasBCRA(){
}
function currencyUpdate_BNAOficial(){
}
function currencyUpdate_XE(){
}
function currencyUpdate_Manual($valor_usd_manual){
update_option('dolar_value', $valor_usd_manual);
}
/*########################################################################*/
// END CRON JOBS //
/*########################################################################*/
/*########################################################################*/
// SHORTCODES //
/*########################################################################*/
function sc_dolarizate_print($atts){
$default = array(
'valor' => '0',
);
$attr = shortcode_atts($default, $atts);
return floatval($attr['valor'])*floatval(get_option('dolar_value'));
}
add_shortcode('wp-dolarizate', 'sc_dolarizate_print');
// Fix Visual Composer addons/shortcodes runtime error.
function sc_dolarizate_print_forced($content){
$data = getAllValues($content);
if(!empty($data)){
$i = 0;
foreach($data['value'] as $key=>$value){
$content = preg_replace('#\[wp\-dolarizate valor\=\'\d+\'\]#', $data['value'][$i], $content, 1);
$i++;
}
}
return $content;
}
add_filter('the_content', 'sc_dolarizate_print_forced', 12);
/*########################################################################*/
// END SHORTCODES //
/*########################################################################*/
/*########################################################################*/
// OTHER FUNCTIONS //
/*########################################################################*/
function getCURL($url, $isJSON){
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_BINARYTRANSFER, true);
if($isJSON){
$content = json_decode(curl_exec($ch));
}else{
$content = curl_exec($ch);
}
curl_close($ch);
return($content);
}
function getAllValues($content){
$offset = 0;
$allpos = array();
while(($pos = strpos($content, "[wp-dolarizate", $offset)) !== FALSE){
$tmpLast = strpos($content, "']", $pos);
$allpos['startposition'][] = $pos;
$allpos['endposition'][] = $tmpLast;
$allpos['value'][] = floatval(substr($content, $pos+22, $tmpLast-$pos-22))*floatval(get_option('dolar_value'));
$tmp_content = substr($content, $pos);
//$content = str_replace("[wp-dolarizate valor='116']", ""
$offset = $tmpLast; //Evite infinite loop
}
return $allpos;
}
/*########################################################################*/
// END OTHER FUNCTIONS //
/*########################################################################*/
?>