forked from ITJ-RSA/codeswholesale-woocommerce
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcodeswholesale.php
462 lines (379 loc) · 13.7 KB
/
codeswholesale.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
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
<?php
/**
* Plugin Name: CodesWholesale for WooCommerce
* Plugin URI: http://docs.codeshowlesale.com
* Depends: WooCommerce
* Description: Integration with CodesWholesale API. PHP >= 7.0
* Version: 2.6.8
* Author: DevTeam [email protected]
* Author URI: http://docs.codeswholesale.com
* License: GPL2
*/
use CodesWholesaleFramework\Connection\Connection;
use GuzzleHttp\Exception\ClientException;
require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
defined('ABSPATH') or die("No script kiddies please!");
final class CodesWholesaleOrderFullFilledStatus
{
const FILLED = 1;
const TO_FILL = 0;
}
final class CodesWholesaleConst
{
const OPTIONS_NAME = "cw_options";
const SETTINGS_CODESWHOLESALE_PARAMS_NAME = "codeswholesale_params";
const ORDER_ITEM_LINKS_PROP_NAME = "_codeswholesale_links";
const ORDER_ITEM_ORDER_ID_PROP_NAME = "_codeswholesale_order_id";
const ORDER_ITEM_NUMBER_OF_PRE_ORDERS_PROP_NAME = "_codeswholesale_number_of_pre_order";
const ORDER_FULL_FILLED_PARAM_NAME = "_codeswholesale_filled";
const PRODUCT_CODESWHOLESALE_ID_PROP_NAME = "_codeswholesale_product_id";
const PRODUCT_STOCK_PRICE_PROP_NAME = "_codeswholesale_product_stock_price";
const PRODUCT_SPREAD_TYPE_PROP_NAME = "_codeswholesale_product_spread_type";
const PRODUCT_SPREAD_VALUE_PROP_NAME = "_codeswholesale_product_spread_value";
const PRODUCT_CALCULATE_PRICE_METHOD_PROP_NAME = "_codeswholesale_product_calculate_price_method";
const HIDE_PRODUCT_WHEN_DISABLED_OPTION_NAME = "_codeswholesale_product_hide_when_disabled";
const AUTOMATICALLY_IMPORT_NEWLY_PRODUCT_OPTION_NAME = "_codeswholesale_product_automatically_import_newly";
const AUTOMATICALLY_COMPLETE_ORDER_OPTION_NAME = "_codeswholesale_auto_complete";
const NOTIFY_LOW_BALANCE_VALUE_OPTION_NAME = "_codeswholesale_notify_balance_value";
const PREFERRED_LANGUAGE_FOR_PRODUCT_OPTION_NAME = "_codeswholesale_preferred_language_for_product";
const RISK_SCORE_PROP_NAME = "_codeswholesale_risk_score";
const DOUBLE_CHECK_PRICE_PROP_NAME = "_codeswholesale_double_check_price";
const ALLOW_PRE_ORDER_PROP_NAME = "_codeswholesale_allow_pre_order";
const TEST_SIGNATURE = "b4cded07-e13e-4021-8b9f-a3cee994109b";
/**
* Format money with euro.
*
* @param $money
* @return string
*/
static public function format_money($money)
{
return "€" . number_format($money, 2, '.', '');
}
}
final class CodesWholesaleAutoCompleteOrder
{
const COMPLETE = 1;
const NOT_COMPLETE = 0;
}
final class CodesWholesale
{
/**
*
* @var CodesWholesale
*/
protected static $_instance = null;
/**
* CodesWholesale API client
*
* @var CodesWholesale\Client
*/
private $codes_wholesale_client;
/**
* Plugin version
*
* @var string
*/
private $version = "2.6.6";
/**
* @var array
*/
private $plugin_options;
/**
*
*/
public function __construct()
{
register_activation_hook(__FILE__, array('CodesWholesale', 'codeswholesale_install'));
register_deactivation_hook(__FILE__, array('CodesWholesale', 'codeswholesale_uninstall'));
// Auto-load classes on demand
if (function_exists("__autoload")) {
spl_autoload_register("__autoload");
}
spl_autoload_register(array($this, 'autoload'));
$this->define_constants();
$this->includes();
self::codeswholesale_activate();
$this->configure_cw_client();
}
public static function codeswholesale_install()
{
if ( !is_plugin_active('woocommerce/woocommerce.php')) {
// Deactivate the plugin
deactivate_plugins(FILE);
$error_message = __('This plugin requires <a href="http://wordpress.org/extend/plugins/woocommerce/">WooCommerce</a> to be active!', 'woocommerce');
die($error_message);
}
$options = CW()->get_options();
if (1 == $options['environment'] && CW()->isClientCorrect()) {
ApiClient::sendActivity(ApiClient::CONNECTED_TO_WOOCOMMERCE);
}
}
public static function codeswholesale_uninstall()
{
$options = CW()->get_options();
if (1 == $options['environment'] && CW()->isClientCorrect()) {
ApiClient::sendActivity(ApiClient::API_DISCONNECTED);
}
}
public static function codeswholesale_activate()
{
foreach (self::getRepositories() as $repository) {
$repository->createTable();
}
}
/**
* @return \CodesWholesaleFramework\Database\Interfaces\RepositoryInterface[]
*/
private static function getRepositories(): array
{
$db = new WP_DbManager();
return [
new \CodesWholesaleFramework\Database\Repositories\ImportPropertyRepository($db),
new \CodesWholesaleFramework\Database\Repositories\AccessTokenRepository($db),
new \CodesWholesaleFramework\Database\Repositories\RefreshTokenRepository($db),
new \CodesWholesaleFramework\Database\Repositories\CodeswholesaleProductRepository($db),
new \CodesWholesaleFramework\Database\Repositories\CurrencyControlRepository($db),
new \CodesWholesaleFramework\Database\Repositories\PostbackImportRepository($db),
new \CodesWholesaleFramework\Database\Repositories\PostbackImportDetailsRepository($db),
];
}
/**
* Auto-load WC classes on demand to reduce memory consumption.
*
* @param mixed $class
* @return void
*/
public function autoload($class)
{
$path = null;
$class = strtolower($class);
$file = 'class-' . str_replace('_', '-', $class) . '.php';
if (strpos($class, 'cw_admin') === 0) {
$path = $this->plugin_path() . '/includes/admin/';
}
if ($path && is_readable($path . $file)) {
include_once($path . $file);
return;
}
// Fallback
if (strpos($class, 'cw_') === 0) {
$path = $this->plugin_path() . '/includes/';
}
if ($path && is_readable($path . $file)) {
include_once($path . $file);
return;
}
}
/**
*
*/
private function includes()
{
include_once('includes/cw-core-functions.php');
include_once('vendor/autoload.php');
include_once('includes/class-cw-install.php');
include_once('includes/class-cw-disable-plugin-while-update.php');
// support the process
include_once('includes/process/class-cw-buy-keys.php');
include_once('includes/process/class-cw-send-keys.php');
include_once('includes/process/class-cw-balance-checker.php');
include_once('includes/process/class-wp-update-products.php');
include_once('includes/process/class-wp-update-orders.php');
//Validation
include_once('includes/validate/wp-validate-purchase.php');
//Visitor
include_once('includes/visitor/WP_InternalOrderVisitor.php');
//Checkers
include_once('includes/checker/wp-configuration-checker.php');
//Client
include_once('includes/client/wp-api-client.php');
//Exec
include_once('includes/exec/wp-exec-manager.php');
//Exporters
include_once('includes/exporters/wp-database-exporter.php');
//Imports
include_once('includes/import/postback-impot-products.php');
//Updaters
include_once('includes/updaters/wp-attachment-updater.php');
include_once('includes/updaters/wp-attribute-updater.php');
include_once('includes/updaters/wp-category-updater.php');
include_once('includes/updaters/wp-product-updater.php');
include_once('includes/updaters/product/create-internal-product.php');
include_once('includes/updaters/product/update-internal-product.php');
// Retrievers
include_once('includes/retrievers/wp-links-retriever.php');
include_once('includes/retrievers/wp-order-item-retriever.php');
// Dispatchers
include_once('includes/dispatchers/wp-order-event-dispatcher.php');
include_once('includes/dispatchers/wp-send-codes-dispatcher.php');
include_once('includes/dispatchers/wp-order-notification-dispatcher.php');
// Managers
include_once('includes/managers/wp-file-manager.php');
include_once('includes/managers/wp-db-manager.php');
//WooCommerce
include_once('includes/woocommerce/class-cw-woocommerce-order.php');
include_once('includes/woocommerce/class-cw-checkout.php');
//E-mails
include_once('includes/emails/class-cw-emails.php');
// Cron Job
// include_once( 'includes/abstracts/class-cw-cron-job.php');
// include_once( 'includes/class-cw-cron-update-stock.php');
if (is_admin()) {
include_once('includes/admin/class-cw-admin.php');
}
}
/**
* Define WC Constants
*/
private function define_constants()
{
define('CW_PLUGIN_FILE', __FILE__);
define('CW_VERSION', $this->version);
}
/**
* Main CodesWholesale Instance
*
* Ensures only one instance of CodesWholesale is loaded or can be loaded.
*
* @since 1.0
* @static
* @see CW()
* @return CodesWholesale
*/
public static function instance()
{
if (is_null(self::$_instance)) {
self::$_instance = new self();
}
return self::$_instance;
}
/**
* Get the plugin path.
*
* @return string
*/
public function plugin_path()
{
return untrailingslashit(plugin_dir_path(__FILE__));
}
/**
* Get the template path.
*
* @return string
*/
public function template_path()
{
return apply_filters('CW_TEMPLATE_PATH', 'codeswholesale-woocommerce/');
}
/**
* Get the plugin url.
*
* @return string
*/
public function plugin_url()
{
return untrailingslashit(plugins_url('/', __FILE__));
}
/**
*
*/
private function configure_cw_client()
{
global $wpdb;
$options = get_option(CodesWholesaleConst::OPTIONS_NAME);
if ($options) {
if (Connection::hasConnection()) {
return Connection::getConnection(array());
}
$options = array(
'environment' => $options['environment'],
'client_id' => empty($options['api_client_id']) ? '0' : $options['api_client_id'] ,
'client_secret' => empty($options['api_client_secret']) ? '0' : $options['api_client_secret'],
'client_headers' => 'Codeswholesale-WooCommerce/' . $this->version,
'db' => new PDO('mysql:host=' . $wpdb->dbhost . ';dbname=' . $wpdb->dbname, $wpdb->dbuser, $wpdb->dbpassword),
'prefix' => sprintf('%s%s', $wpdb->prefix, \CodesWholesaleFramework\Database\Interfaces\RepositoryInterface::CW_PREFIX),
'signature' => empty($options['api_client_singature']) ? CodesWholesaleConst::TEST_SIGNATURE : $options['api_client_singature']
);
try {
return $this->codes_wholesale_client = Connection::getConnection($options);
} catch (ClientException $e) {
}
}
}
/**
* @return \CodesWholesale\Client
*/
public function get_codes_wholesale_client()
{
return $this->codes_wholesale_client;
}
/**
*
*/
public function refresh_codes_wholesale_client()
{
$_SESSION["php-oauth-client"] = array();
$this->configure_cw_client();
}
/**
* Return options for CW plugin
*/
public function get_options()
{
if (empty($this->plugin_options)) {
$this->plugin_options = get_option(CodesWholesaleConst::OPTIONS_NAME);
}
return $this->plugin_options;
}
public function get_related_wp_products($cwProductId, $postStatus = 'any') {
$args = array(
'posts_per_page' => -1,
'post_type' => 'product',
'post_status' => $postStatus,
'meta_key' => CodesWholesaleConst::PRODUCT_CODESWHOLESALE_ID_PROP_NAME,
'meta_value' => $cwProductId
);
return get_posts($args);
}
public function isClientCorrect(): bool
{
try {
CW()->get_codes_wholesale_client()->getAccount();
return true;
} catch (\Error $e) {
return false;
}
}
/**
* @param string $orderId
*
* @return string
*/
public function getWooCommerceOrderIdByExternalId(string $orderId): string
{
global $wpdb;
$query = sprintf("SELECT * FROM %swoocommerce_order_itemmeta WHERE meta_key = '%s' AND meta_value = '%s'",
$wpdb->prefix,
CodesWholesaleConst::ORDER_ITEM_ORDER_ID_PROP_NAME,
$orderId
);
$result = $wpdb->get_row($query);
if (null === $result) {
return '';
}
$product = new WC_Order_Item_Product($result->order_item_id);
return $product->get_order()->get_id();
}
}
/**
* Returns the main instance of CW to prevent the need to use globals.
*
* @since 1.0
* @return CodesWholesale
*/
function CW()
{
return CodesWholesale::instance();
}
CW();