-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdom-currency-converter.php
204 lines (180 loc) · 7.19 KB
/
dom-currency-converter.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
192
193
194
195
196
197
198
199
200
201
202
203
204
<?php
/*
Plugin Name: DOM Currency Converter
Plugin URI: https://hassam.dev/
Description: This plugin will change the USD symbol/value with AED symbol/value according to current rate of dollar. This plugin is for eagle booking.
Version: 0.0.1
Author: M Hassam
Author URI: https://hassam.dev/
License: GPL2
*/
/**
* create log file
*
*/
// Define the path to the log file
$log_file = plugin_dir_path(__FILE__) . 'logs.txt';
if (!file_exists($log_file)) {
$log_file_path = plugin_dir_path( __FILE__ ) . 'logs.txt';
$log_file_open = fopen($log_file_path, "w");
fwrite($log_file_open, "---------------\n");
// Close the file
fclose($log_file_open);
chmod($log_file_path, 0777);
}
function dom_currency_converter_activate() {
// create a new log file in the plugin directory
$log_file_path = plugin_dir_path( __FILE__ ) . 'logs.txt';
$log_file_open = fopen($log_file_path, "w");
// Add a string to the file
fwrite($log_file_open, "---------------\n");
// Close the file
fclose($log_file_open);
chmod($log_file_path, 0777);
if (!chmod($log_file_path, 0777)) {
error_log("Failed to assign permissions to file: [logs.txt]" . "\n", 3, $log_file_path);
} else {
error_log("Permissions assigned successfully to file: [logs.txt]" . "\n", 3, $log_file_path);
}
// Create a txt file, and from that file we read converted currency value
error_log('Let\'s create new [openexchangeratesorg-aed.txt] file!' . "\n", 3, $log_file_path);
$filepath = plugin_dir_path(__FILE__) . 'openexchangeratesorg-aed.txt';
// Assign read and write permissions to the file
$file = fopen($filepath, "w");
// Add a string to the file
fwrite($file, $rate_aed ?? 3.6725);
// Close the file
fclose($file);
chmod($filepath, 0777);
if (!chmod($filepath, 0777)) {
error_log("Failed to assign permissions to file: {$filepath}" . "\n", 3, $log_file_path);
} else {
error_log("Permissions assigned successfully to file: {$filepath}" . "\n", 3, $log_file_path);
}
// create cronjob
if (!wp_next_scheduled('dom_currency_converter_cron')) {
wp_schedule_event(time(), 'myplugin_minute', 'dom_currency_converter_cron');
}
}
function dom_currency_converter_deactivate() {
// Remove cron job on plugin deactivation
wp_clear_scheduled_hook('dom_currency_converter_cron');
// empty the contents of the file
$file_path = plugin_dir_path( __FILE__ ) . 'logs.txt';
$file = fopen( $file_path, 'a' );
fwrite( $file, "\n--------------------\nplugin deactivate.\n" );
fclose( $file );
}
register_activation_hook( __FILE__, 'dom_currency_converter_activate' );
register_deactivation_hook( __FILE__, 'dom_currency_converter_deactivate' );
// END log file
/**
* create a cronjob
*
*/
// Define the function that will run when the cron job is triggered
add_action('dom_currency_converter_cron', 'dom_currency_converter_run_cron');
function dom_currency_converter_run_cron()
{
$log_file_path = plugin_dir_path(__FILE__) . 'logs.txt';
// Your code goes here
error_log("cronjob triggered!" . "\n", 3, $log_file_path);
fetchCurrencyFromApi();
}
// Add custom interval of 1 minute
add_filter('cron_schedules', 'dom_currency_converter_cron_interval');
function dom_currency_converter_cron_interval($schedules)
{
$schedules['myplugin_minute'] = array(
'interval' => 86400, // 24 hours in seconds
'display' => esc_html__('Every 24 Hours')
);
return $schedules;
}
/**
* email: [email protected]
*
* https://openexchangerates.org
* https://openexchangerates.org/account/app-ids
*
*
* Code by hassam.dev
*/
// CRON job to check USD price on every morning
function fetchCurrencyFromApi($cronjob = null)
{
$log_file_path = plugin_dir_path(__FILE__) . 'logs.txt';
try {
$disclaimer = '';
$license = '';
$timestamp = '';
$base = '';
$rates = [];
$rate_aed = 3.6725; //Since 1997, the dirham has been pegged to the US dollar at the rate of 1 USD = 3.6725 AED.
error_log('fetchCurrencyFromApi triggered' . "\n", 3, $log_file_path);
//if (!is_null($cronjob) && $cronjob == 'true') {
$app_id = '';
$symbols = 'AED';
$base = 'USD';
$show_alternative = false;
$prettyprint = false;
$convert_amount = 1; // 1 USD is equal to AED?
$oxr_url = "https://openexchangerates.org/api/latest.json?app_id={$app_id}&base={$base}&symbols={$symbols}&prettyprint={$prettyprint}&show_alternative={$show_alternative}";
// Open CURL session:
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $oxr_url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// Get the data:
$json = curl_exec($ch);
if (curl_errno($ch)) {
error_log('Error: ' . curl_error($ch) . "\n", 3, $log_file_path);
} else {
// Decode JSON response:
$oxr_latest = json_decode($json, true);
//foreach ($oxr_latest as $item) {
$disclaimer = $oxr_latest['disclaimer'] ?? '';
$license = $oxr_latest['license'] ?? '';
$timestamp = $oxr_latest['timestamp'] ?? '';
$base = $oxr_latest['base'] ?? '';
$rates = $oxr_latest['rates'] ?? [];
$rate_aed = $rates['AED'] ?? 3.6725;
//}
error_log('Price updated' . "\n", 3, $log_file_path);
}
curl_close($ch);
//}
// Create a text file
$file_path = plugin_dir_path(__FILE__) . 'openexchangeratesorg-aed.txt';
$file = fopen($file_path, "w");
// Add a string to the file
fwrite($file, $rate_aed);
// Close the file
fclose($file);
// Assign read and write permissions to the file
$filename = "openexchangeratesorg-aed.txt";
$filepath = plugin_dir_path(__FILE__) . $filename;
if (file_exists($filepath)) {
error_log('The [openexchangeratesorg-aed.txt] file exists!' . "\n", 3, $log_file_path);
chmod($file_path, 0777);
} else {
error_log('The [openexchangeratesorg-aed.txt] file does not exist!' . "\n", 3, $log_file_path);
}
if (!chmod($filepath, 0777)) {
error_log("Failed to assign permissions to file: {$filepath}" . "\n", 3, $log_file_path);
} else {
error_log("Permissions assigned successfully to file: {$filepath}" . "\n", 3, $log_file_path);
}
} catch (Exception $e) {
error_log("An error occurred: {$e->getMessage()}" . "\n", 3, $log_file_path);
}
}
function dom_currency_converter_enqueue_scripts()
{
// Enqueue the JavaScript file
wp_enqueue_script( 'dom-currency-converter-script', plugins_url( 'dom-currency-converter.js', __FILE__ ), array(), '1.0.0', true );
wp_localize_script( 'dom-currency-converter-script', 'openexchangeratesorg_data', array(
//'openexchangeratesorg_aed_file_path' => plugin_dir_path( __FILE__ ) . 'openexchangeratesorg-aed.txt',
'openexchangeratesorg_aed_file_path' => str_replace( ABSPATH, '/', plugin_dir_path( __FILE__ ) ) . 'openexchangeratesorg-aed.txt',
));
}
add_action('wp_enqueue_scripts', 'dom_currency_converter_enqueue_scripts');