forked from jtl-software/connector-woocommerce3
-
Notifications
You must be signed in to change notification settings - Fork 0
/
woo-jtl-connector.php
352 lines (304 loc) · 10 KB
/
woo-jtl-connector.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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
<?php //phpcs:ignore PSR1.Files.SideEffects.FoundWithSymbols
/**
* Plugin Name: WooCommerce JTL-Connector
* Description: Connect your woocommerce-shop with JTL-Wawi, the free multichannel-erp for mail order business.
* Version: 1.39.8
* Requires PHP: 7.2
* WC tested up to: 6.3
* Author: JTL-Software GmbH
* Author URI: http://www.jtl-software.de
* License: GPL3
* License URI: http://www.gnu.org/licenses/lgpl-3.0.html
* Requires at least WooCommerce: 3.4.7
* Text Domain: woo-jtl-connector
*
* @author JTL-Software-GmbH <[email protected]>
*/
define('JTLWCC_TEXT_DOMAIN', 'woo-jtl-connector');
define('JTLWCC_WOOCOMMERCE_PLUGIN_FILE', 'woocommerce/woocommerce.php');
define('JTLWCC_DS', DIRECTORY_SEPARATOR);
define('JTLWCC_CONNECTOR_DIR', __DIR__);
define('JTLWCC_EXT_CONNECTOR_PLUGIN_DIR', dirname(__DIR__) . '/' . JTLWCC_TEXT_DOMAIN . '-custom-plugins');
define('JTLWCC_CONNECTOR_DIR_URL', WP_PLUGIN_URL . JTLWCC_DS . JTLWCC_TEXT_DOMAIN);
define('CONNECTOR_DIR', __DIR__); // NEED CONNECTOR CORE CHANGES
define('JTLWCC_INCLUDES_DIR', plugin_dir_path(__FILE__) . 'includes' . JTLWCC_DS);
if (!defined('ABSPATH')) {
exit;
}
require_once ABSPATH . '/wp-admin/includes/plugin.php';
try {
if (file_exists(JTLWCC_CONNECTOR_DIR . '/connector.phar')) {
if (is_writable(sys_get_temp_dir())) {
$loader = require('phar://' . JTLWCC_CONNECTOR_DIR . '/connector.phar/vendor/autoload.php');
$loader->add('', JTLWCC_CONNECTOR_DIR . '/plugins');
if (is_dir(JTLWCC_EXT_CONNECTOR_PLUGIN_DIR)) {
$loader->add('', JTLWCC_EXT_CONNECTOR_PLUGIN_DIR);
}
}
} else {
$loader = require(JTLWCC_CONNECTOR_DIR . '/vendor/autoload.php');
$loader->add('', JTLWCC_CONNECTOR_DIR . '/plugins');
if (is_dir(JTLWCC_EXT_CONNECTOR_PLUGIN_DIR)) {
$loader->add('', JTLWCC_EXT_CONNECTOR_PLUGIN_DIR);
}
}
} catch (\Exception $e) {
}
add_action('init', 'jtlwcc_load_internationalization');
add_action('plugins_loaded', 'jtlwcc_validate_plugins');
if (jtlwcc_rewriting_disabled()) {
jtlwcc_deactivate_plugin();
add_action('admin_notices', 'jtlwcc_rewriting_not_activated');
} else {
require_once JTLWCC_INCLUDES_DIR . 'JtlConnector.php';
require_once JTLWCC_INCLUDES_DIR . 'JtlConnectorAdmin.php';
register_activation_hook(__FILE__, [
'JtlConnectorAdmin',
'plugin_activation',
]);
register_deactivation_hook(__FILE__, [
'JtlConnectorAdmin',
'plugin_deactivation',
]);
add_action('parse_request', 'JtlConnector::capture_request', 1);
if (is_admin()) {
add_action('init', [
'JtlConnectorAdmin',
'init',
]);
add_filter('plugin_action_links_' . plugin_basename(__FILE__), [
'JtlConnectorAdmin',
'settings_link',
]);
add_action('admin_footer', 'woo_jtl_connector_settings_javascript', PHP_INT_MAX);
add_action('wp_ajax_downloadJTLLogs', 'downloadJTLLogs', PHP_INT_MAX);
add_action('wp_ajax_clearJTLLogs', 'clearJTLLogs', PHP_INT_MAX);
}
}
function woo_jtl_connector_settings_javascript()
{
?>
<script type="text/javascript">
jQuery(document).ready(($) => {
//console.log('Script Loaded ');
$("#downloadLogBtn").click(
() => {
let data = {
'action': 'downloadJTLLogs',
};
jQuery.ajax(
{
url: ajaxurl,
type: 'POST',
data: data,
success: (response) => {
console.log(response);
window.location.href = response;
},
error: (response) => {
response = JSON.parse(response.responseText);
alert(response.message);
}
}
);
}
);
$("#clearLogBtn").click(
() => {
let result = confirm("Are you sure you want a reset?");
if (result) {
let data = {
'action': 'clearJTLLogs',
};
jQuery.ajax(
{
url: ajaxurl,
type: 'POST',
data: data,
success: (response) => {
//console.log(response);
},
}
);
}
}
);
});
</script>
<?php
}
function downloadJTLLogs()
{
$logDir = CONNECTOR_DIR . '/logs';
$zip_file = CONNECTOR_DIR . '/tmp/connector_logs.zip';
$url = get_site_url() . '/wp-content/plugins/woo-jtl-connector/tmp/connector_logs.zip';
// Get real path for our folder
$rootPath = $logDir;
// Initialize archive object
$zip = new ZipArchive();
$zip->open($zip_file, ZipArchive::CREATE | ZipArchive::OVERWRITE);
// Create recursive directory iterator
/** @var SplFileInfo[] $files */
$files = new RecursiveIteratorIterator(
new RecursiveDirectoryIterator($rootPath),
RecursiveIteratorIterator::LEAVES_ONLY
);
$filesCounter = 0;
foreach ($files as $name => $file) {
if ($file->getFilename() === '.gitkeep') {
continue;
}
// Skip directories (they would be added automatically)
if (!$file->isDir()) {
// Get real and relative path for current file
$filePath = $file->getRealPath();
$relativePath = substr($filePath, strlen($rootPath) + 1);
// Add current file to archive
$zip->addFile($filePath, $relativePath);
$filesCounter++;
}
}
// Zip archive will be created only after closing object
$zip->close();
header('Content-Type: application/json; charset=UTF-8');
if ($filesCounter > 0) {
print json_encode($url);
} else {
header('HTTP/1.1 451 Internal Server Booboo');
die(json_encode([
'message' => 'Keine Logs Vorhanden!',
'code' => 451,
]));
}
wp_die();
//self::display_page();
}
function clearJTLLogs()
{
$logDir = CONNECTOR_DIR . '/logs';
$zip_file = CONNECTOR_DIR . '/tmp/connector_logs.zip';
if (file_exists($zip_file)) {
unlink($zip_file);
}
$files = new RecursiveIteratorIterator(
new RecursiveDirectoryIterator($logDir),
RecursiveIteratorIterator::LEAVES_ONLY
);
foreach ($files as $name => $file) {
if ($file->getFilename() === '.gitkeep') {
continue;
}
if (!$file->isDir()) {
$filePath = $file->getRealPath();
if (file_exists($filePath)) {
unlink($filePath);
}
}
}
echo 'success';
wp_die();
}
/**
* Register the languages folder thus the DE and CH German translations are available based on the WP setting.
*/
function jtlwcc_load_internationalization()
{
load_plugin_textdomain(JTLWCC_TEXT_DOMAIN, false, basename(dirname(__FILE__)) . '/languages');
}
/**
* Check the status of WC, connector and the WC version.
*/
function jtlwcc_validate_plugins()
{
if (jtlwcc_woocommerce_deactivated() && jtlwcc_connector_activated()) {
add_action('admin_notices', 'jtlwcc_woocommerce_not_activated');
} elseif (version_compare(jtlwcc_get_woocommerce_version(), '3.0', '<')) {
jtlwcc_deactivate_plugin();
add_action('admin_notices', 'jtlwcc_wrong_woocommerce_version');
}
}
/**
* Deactivate the connector.
*/
function jtlwcc_deactivate_plugin()
{
deactivate_plugins(__FILE__);
}
/**
* Check if the required WC is deactivated.
*
* @return bool
*/
function jtlwcc_woocommerce_deactivated()
{
return !in_array(
JTLWCC_WOOCOMMERCE_PLUGIN_FILE,
apply_filters('active_plugins', get_option('active_plugins'))
);
}
/**
* Redirect action
*/
function woo_jtl_connector_menu_link()
{
$link = 'admin.php?page=wc-settings&tab=woo-jtl-connector';
wp_redirect($link, 301);
exit;
}
/**
* Check if the connector is activated.
*
* @return bool
*/
function jtlwcc_connector_activated()
{
return in_array(
'woo-jtl-connector/woo-jtl-connector.php',
apply_filters('active_plugins', get_option('active_plugins'))
);
}
/**
* Read out the WC version from the plugin file.
*
* @return string The WC version.
*/
function jtlwcc_get_woocommerce_version()
{
$plugin = get_plugin_data(WP_PLUGIN_DIR . '/' . JTLWCC_WOOCOMMERCE_PLUGIN_FILE);
return isset($plugin['Version']) ? $plugin['Version'] : '0';
}
/**
* Without rewriting a URL like jtlconnector cannot be used.
*/
function jtlwcc_rewriting_disabled()
{
$permalink_structure = \get_option('permalink_structure');
return empty($permalink_structure);
}
function jtlwcc_woocommerce_not_activated()
{
jtlwcc_show_wordpress_error(
__('Activate WooCommerce in order to use the JTL-Connector.', JTLWCC_TEXT_DOMAIN),
true
);
}
function jtlwcc_wrong_woocommerce_version()
{
jtlwcc_show_wordpress_error(__('At least WooCommerce 3.0 has to be installed.', JTLWCC_TEXT_DOMAIN));
}
function jtlwcc_rewriting_not_activated()
{
jtlwcc_show_wordpress_error(__(
'Rewriting is disabled. Please select another permalink setting.',
JTLWCC_TEXT_DOMAIN
));
}
function jtlwcc_show_wordpress_error($message, $show_install_link = false)
{
$link = $show_install_link
? '<a class="" href="' .
admin_url("plugin-install.php?tab=search&s=" .
urlencode("WooCommerce")) . '">WooCommerce</a>'
: '';
echo "<div class='error'><h3>JTL-Connector</h3><p>$message</p><p>$link</p></div>";
}