Completed
Push — master ( 72ec0a...f6d0fe )
by
unknown
15s queued 11s
created

alterCartProcessTable()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 2
eloc 6
c 2
b 0
f 0
nc 2
nop 0
dl 0
loc 9
rs 10
1
<?php
2
3
function requireWPPluginFunctions()
4
{
5
    if (! function_exists('is_plugin_active')) {
6
        require_once(ABSPATH . 'wp-admin/includes/plugin.php');
0 ignored issues
show
Bug introduced by
The constant ABSPATH was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
7
    }
8
}
9
10
requireWPPluginFunctions();
11
12
13
/**
14
 * Get lowercase config value from WP DB
15
 *
16
 * @param string $configKey
17
 *
18
 * @return string
19
 * @global wpdb  $wpdb WordPress database abstraction object.
20
 */
21
function getConfigValue($configKey)
22
{
23
    global $wpdb;
24
    $tableName = $wpdb->prefix . PG_CONFIG_TABLE_NAME;
25
    $value     = $wpdb->get_var($wpdb->prepare("SELECT value FROM $tableName WHERE config= %s ", $configKey));
26
27
    return $value;
28
}
29
30
/**
31
 * Check if table exists in WordPress DB
32
 *
33
 * @param string $tableToCheck
34
 *
35
 * @return bool
36
 * @see wpdb::get_var()
37
 */
38
function isPgTableCreated($tableToCheck)
39
{
40
    global $wpdb;
41
    $tableName = $wpdb->prefix . $tableToCheck;
42
    if ($wpdb->get_var("SHOW TABLES LIKE '$tableName'") == $tableName) {
43
        return true;
44
    }
45
46
    return false;
47
}
48
49
/**
50
 * Check if orders table exists
51
 */
52
function createPgCartTable()
53
{
54
    global $wpdb;
55
    $tableName       = $wpdb->prefix . PG_CART_PROCESS_TABLE;
56
    $charset_collate = $wpdb->get_charset_collate();
57
    $sql             = "CREATE TABLE $tableName ( id int, order_id varchar(50), wc_order_id varchar(50),  
58
                  UNIQUE KEY id (id)) $charset_collate";
59
60
    require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
0 ignored issues
show
Bug introduced by
The constant ABSPATH was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
61
    dbDelta($sql);
0 ignored issues
show
Bug introduced by
The function dbDelta was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

61
    /** @scrutinizer ignore-call */ 
62
    dbDelta($sql);
Loading history...
62
}
63
64
function createLogsTable()
65
{
66
    global $wpdb;
67
    $charset_collate = $wpdb->get_charset_collate();
68
    $LogsTableName   = $wpdb->prefix . PG_LOGS_TABLE_NAME;
69
    $sqlQuery        = "CREATE TABLE $LogsTableName ( 
70
    id int NOT NULL AUTO_INCREMENT,
71
    log text NOT NULL, 
72
    createdAt timestamp DEFAULT CURRENT_TIMESTAMP,
73
    UNIQUE KEY id (id)) 
74
    $charset_collate";
75
    require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
0 ignored issues
show
Bug introduced by
The constant ABSPATH was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
76
    dbDelta($sqlQuery);
0 ignored issues
show
Bug introduced by
The function dbDelta was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

76
    /** @scrutinizer ignore-call */ 
77
    dbDelta($sqlQuery);
Loading history...
77
}
78
79
/**
80
 * @param null $exception
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $exception is correct as it would always require null to be passed?
Loading history...
81
 * @param null $message
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $message is correct as it would always require null to be passed?
Loading history...
82
 */
83
function insertLogEntry($exception = null, $message = null)
84
{
85
    global $wpdb;
86
    if (! isPgTableCreated(PG_LOGS_TABLE_NAME)) {
87
        createLogsTable();
88
    }
89
    $logEntry = new Pagantis\ModuleUtils\Model\Log\LogEntry();
90
    if ($exception instanceof \Exception) {
91
        $logEntry = $logEntry->error($exception);
92
    } else {
93
        $logEntry = $logEntry->info($message);
94
    }
95
    $tableName = $wpdb->prefix . PG_LOGS_TABLE_NAME;
96
    $wpdb->insert($tableName, array('log' => $logEntry->toJson()));
97
}
98
99
/**
100
 * @return bool
101
 */
102
function areDecimalSeparatorEqual()
103
{
104
    $pgDecimalSeparator = getPgSimulatorDecimalSeparatorConfig();
105
    $wc_decimal_sep     = get_option('woocommerce_price_decimal_sep');
0 ignored issues
show
Bug introduced by
The function get_option was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

105
    $wc_decimal_sep     = /** @scrutinizer ignore-call */ get_option('woocommerce_price_decimal_sep');
Loading history...
106
    if (stripslashes($wc_decimal_sep) == stripslashes($pgDecimalSeparator)) {
107
        return true;
108
    } else {
109
        return false;
110
    }
111
}
112
113
114
/**
115
 * @return bool
116
 */
117
function areThousandsSeparatorEqual()
118
{
119
    $pgThousandSeparator = getPgSimulatorThousandsSeparator();
120
    $wc_price_thousand   = get_option('woocommerce_price_thousand_sep');
0 ignored issues
show
Bug introduced by
The function get_option was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

120
    $wc_price_thousand   = /** @scrutinizer ignore-call */ get_option('woocommerce_price_thousand_sep');
Loading history...
121
    if (stripslashes($wc_price_thousand) == stripslashes($pgThousandSeparator)) {
122
        return true;
123
    } else {
124
        return false;
125
    }
126
}
127
128
/**
129
 * @return array|object|null
130
 */
131
function getPgSimulatorThousandsSeparator()
132
{
133
    global $wpdb;
134
    $tableName = $wpdb->prefix . PG_CONFIG_TABLE_NAME;
135
    $query     = "SELECT value FROM $tableName WHERE config='PAGANTIS_SIMULATOR_THOUSANDS_SEPARATOR'";
136
    $result    = $wpdb->get_row($query, ARRAY_A);
0 ignored issues
show
Bug introduced by
The constant ARRAY_A was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
137
138
    return $result['value'];
139
}
140
141
/**
142
 * @return array|object|null
143
 */
144
function getPgSimulatorDecimalSeparatorConfig()
145
{
146
    global $wpdb;
147
    $tableName = $wpdb->prefix . PG_CONFIG_TABLE_NAME;
148
    $query     = "SELECT value FROM $tableName WHERE config='PAGANTIS_SIMULATOR_DECIMAL_SEPARATOR'";
149
    $result    = $wpdb->get_row($query, ARRAY_A);
0 ignored issues
show
Bug introduced by
The constant ARRAY_A was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
150
151
    return $result['value'];
152
}
153
154
function updateThousandsSeparatorDbConfig()
155
{
156
    global $wpdb;
157
    if (areThousandsSeparatorEqual()) {
158
        return;
159
    }
160
    $tableName         = $wpdb->prefix . PG_CONFIG_TABLE_NAME;
161
    $thousandSeparator = get_option('woocommerce_price_thousand_sep');
0 ignored issues
show
Bug introduced by
The function get_option was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

161
    $thousandSeparator = /** @scrutinizer ignore-call */ get_option('woocommerce_price_thousand_sep');
Loading history...
162
    $wpdb->update(
163
        $tableName,
164
        array('value' => $thousandSeparator),
165
        array('config' => 'PAGANTIS_SIMULATOR_THOUSANDS_SEPARATOR'),
166
        array('%s'),
167
        array('%s')
168
    );
169
}
170
171
function updateDecimalSeparatorDbConfig()
172
{
173
    global $wpdb;
174
    if (areDecimalSeparatorEqual()) {
175
        return;
176
    }
177
    $tableName        = $wpdb->prefix . PG_CONFIG_TABLE_NAME;
178
    $decimalSeparator = get_option('woocommerce_price_decimal_sep');
0 ignored issues
show
Bug introduced by
The function get_option was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

178
    $decimalSeparator = /** @scrutinizer ignore-call */ get_option('woocommerce_price_decimal_sep');
Loading history...
179
    $wpdb->update(
180
        $tableName,
181
        array('value' => $decimalSeparator),
182
        array('config' => 'PAGANTIS_SIMULATOR_DECIMAL_SEPARATOR'),
183
        array('%s'),
184
        array('%s')
185
    );
186
}
187
188
189
/**
190
 * @param $simulatorType
191
 * @param $validSimulatorTypes array
192
 *
193
 * @return bool
194
 */
195
function isSimulatorTypeValid($simulatorType, $validSimulatorTypes)
196
{
197
    if (! in_array($simulatorType, $validSimulatorTypes)) {
198
        return false;
199
    }
200
201
    return true;
202
}
203
204
/**
205
 * @param $currentTemplateName
206
 *
207
 * @param $validTemplateNames array
208
 *
209
 * @return bool
210
 */
211
function isTemplatePresent($currentTemplateName, $validTemplateNames)
212
{
213
    if (in_array($currentTemplateName, $validTemplateNames)) {
214
        return true;
215
    }
216
217
    return false;
218
}
219
220
221
function areMerchantKeysSet()
222
{
223
    $settings   = get_option('woocommerce_pagantis_settings');
0 ignored issues
show
Bug introduced by
The function get_option was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

223
    $settings   = /** @scrutinizer ignore-call */ get_option('woocommerce_pagantis_settings');
Loading history...
224
    $publicKey  = ! empty($settings['pagantis_public_key']) ? $settings['pagantis_public_key'] : '';
225
    $privateKey = ! empty($settings['pagantis_private_key']) ? $settings['pagantis_private_key'] : '';
226
    if ((empty($publicKey) && empty($privateKey)) || (empty($publicKey) || empty($privateKey))) {
227
        return false;
228
    }
229
230
    return true;
231
}
232
233
function areMerchantKeysSet4x()
234
{
235
    $settings   = get_option('woocommerce_pagantis_settings');
0 ignored issues
show
Bug introduced by
The function get_option was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

235
    $settings   = /** @scrutinizer ignore-call */ get_option('woocommerce_pagantis_settings');
Loading history...
236
    $publicKey  = ! empty($settings['pagantis_public_key_4x']) ? $settings['pagantis_public_key_4x'] : '';
237
    $privateKey = ! empty($settings['pagantis_private_key_4x']) ? $settings['pagantis_private_key_4x'] : '';
238
    if ((empty($publicKey) && empty($privateKey)) || (empty($publicKey) || empty($privateKey))) {
239
        return false;
240
    }
241
242
    return true;
243
}
244
245
function isSimulatorEnabled()
246
{
247
    $settings = get_option('woocommerce_pagantis_settings');
0 ignored issues
show
Bug introduced by
The function get_option was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

247
    $settings = /** @scrutinizer ignore-call */ get_option('woocommerce_pagantis_settings');
Loading history...
248
    if ((! empty($settings['simulator']) && 'yes' === $settings['simulator']) ? true : false) {
249
        return true;
250
    }
251
252
    return false;
253
}
254
255
function isPluginEnabled()
256
{
257
    $settings = get_option('woocommerce_pagantis_settings');
0 ignored issues
show
Bug introduced by
The function get_option was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

257
    $settings = /** @scrutinizer ignore-call */ get_option('woocommerce_pagantis_settings');
Loading history...
258
259
    return (! empty($settings['enabled']) && 'yes' === $settings['enabled']);
260
}
261
262
function isPluginEnabled4x()
263
{
264
    $settings = get_option('woocommerce_pagantis_settings');
0 ignored issues
show
Bug introduced by
The function get_option was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

264
    $settings = /** @scrutinizer ignore-call */ get_option('woocommerce_pagantis_settings');
Loading history...
265
    return (! empty($settings['enabled_4x']) && 'yes' === $settings['enabled_4x']);
266
}
267
268
269
function isCountryShopContextValid()
270
{
271
    $locale           = strtolower(strstr(get_locale(), '_', true));
0 ignored issues
show
Bug introduced by
The function get_locale was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

271
    $locale           = strtolower(strstr(/** @scrutinizer ignore-call */ get_locale(), '_', true));
Loading history...
272
    $allowedCountries = maybe_unserialize(getConfigValue('PAGANTIS_ALLOWED_COUNTRIES'));
0 ignored issues
show
Bug introduced by
The function maybe_unserialize was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

272
    $allowedCountries = /** @scrutinizer ignore-call */ maybe_unserialize(getConfigValue('PAGANTIS_ALLOWED_COUNTRIES'));
Loading history...
273
    if (! in_array(strtolower($locale), $allowedCountries)) {
274
        return false;
275
    }
276
277
    return true;
278
}
279
280
/**
281
 * @return bool
282
 */
283
function isProductAmountValid()
284
{
285
    $minAmount = getConfigValue('PAGANTIS_DISPLAY_MIN_AMOUNT');
286
    $maxAmount = getConfigValue('PAGANTIS_DISPLAY_MAX_AMOUNT');
287
    global $product;
288
    if (method_exists($product, 'get_price')) {
289
        $productPrice = $product->get_price();
290
        $validAmount  = ($productPrice >= $minAmount && ($productPrice <= $maxAmount || $maxAmount == '0'));
291
        if ($validAmount) {
292
            return true;
293
        }
294
    }
295
296
    return false;
297
}
298
299
/**
300
 * @return bool
301
 */
302
function isProductAmountValid4x()
303
{
304
    $minAmount = getConfigValue('PAGANTIS_DISPLAY_MIN_AMOUNT_4x');
305
    $maxAmount = getConfigValue('PAGANTIS_DISPLAY_MAX_AMOUNT_4x');
306
    global $product;
307
    if (method_exists($product, 'get_price')) {
308
        $productPrice = $product->get_price();
309
        $validAmount  = ($productPrice >= $minAmount && ($productPrice <= $maxAmount || $maxAmount == '0'));
310
        if ($validAmount) {
311
            return true;
312
        }
313
    }
314
315
    return false;
316
}
317
function getAllowedCurrencies()
318
{
319
    return array("EUR");
320
}
321
322
/**
323
 * @return array
324
 */
325
function getExtraConfig()
326
{
327
    global $wpdb;
328
    $tableName = $wpdb->prefix . PG_CONFIG_TABLE_NAME;
329
    $response  = array();
330
    $dbResult  = $wpdb->get_results("select config, value from $tableName", ARRAY_A);
0 ignored issues
show
Bug introduced by
The constant ARRAY_A was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
331
    foreach ($dbResult as $value) {
332
        $response[$value['config']] = $value['value'];
333
    }
334
335
    return $response;
336
}
337
338
function getModuleVersion()
339
{
340
341
    $mainFile = plugin_dir_path(PG_WC_MAIN_FILE). '/WC_Pagantis.php';
0 ignored issues
show
Bug introduced by
The function plugin_dir_path was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

341
    $mainFile = /** @scrutinizer ignore-call */ plugin_dir_path(PG_WC_MAIN_FILE). '/WC_Pagantis.php';
Loading history...
342
    $version = get_file_data($mainFile, array('Version' => 'Version'), false);
0 ignored issues
show
Bug introduced by
The function get_file_data was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

342
    $version = /** @scrutinizer ignore-call */ get_file_data($mainFile, array('Version' => 'Version'), false);
Loading history...
343
    return $version['Version'];
344
}
345
346
/**
347
 * @param $order
348
 *
349
 * @return null
350
 */
351
function getNationalId($order)
352
{
353
    foreach ((array)$order->get_meta_data() as $mdObject) {
354
        $data = $mdObject->get_data();
355
        if ($data['key'] == 'vat_number') {
356
            return $data['value'];
357
        }
358
    }
359
360
    return null;
361
}
362
363
/**
364
 * @param $order
365
 *
366
 * @return mixed
367
 */
368
function getTaxId($order)
369
{
370
    foreach ((array)$order->get_meta_data() as $mdObject) {
371
        $data = $mdObject->get_data();
372
        if ($data['key'] == 'billing_cfpiva') {
373
            return $data['value'];
374
        }
375
    }
376
}
377
378
379
/**
380
 * @param $product_id
381
 *
382
 * @return string
383
 */
384
function isProductPromoted($product_id)
385
{
386
    $metaProduct = get_post_meta($product_id);
0 ignored issues
show
Bug introduced by
The function get_post_meta was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

386
    $metaProduct = /** @scrutinizer ignore-call */ get_post_meta($product_id);
Loading history...
387
388
    return (array_key_exists('custom_product_pagantis_promoted', $metaProduct)
389
            && $metaProduct['custom_product_pagantis_promoted']['0'] === 'yes') ? 'true' : 'false';
390
}
391
392
/**
393
 * @return int
394
 */
395
function getPromotedAmount()
396
{
397
    $promotedAmount = 0;
398
    foreach (WC()->cart->get_cart() as $key => $item) {
0 ignored issues
show
Bug introduced by
The function WC was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

398
    foreach (/** @scrutinizer ignore-call */ WC()->cart->get_cart() as $key => $item) {
Loading history...
399
        $promotedProduct = isProductPromoted($item['product_id']);
400
        if ($promotedProduct == 'true') {
401
            $promotedAmount += $item['line_total'] + $item['line_tax'];
402
        }
403
    }
404
405
    return $promotedAmount;
406
}
407
408
/**
409
 * @param $pagantisOrderId
410
 * @param $wcOrderID
411
 * @param $urlToken
412
 * @param $methodID
413
 */
414
function addOrderToProcessingQueue($pagantisOrderId, $wcOrderID, $urlToken ,$methodID)
415
{
416
    global $wpdb;
417
    checkCartProcessTable();
418
    $tableName = $wpdb->prefix . PG_CART_PROCESS_TABLE;
419
420
    //Check if id exists
421
    $resultsSelect = $wpdb->get_results("SELECT * FROM $tableName WHERE id='$pagantisOrderId'");
422
    $countResults  = count($resultsSelect);
423
424
    if ($countResults == 0) {
425
        $wpdb->insert($tableName, array(
426
            'order_id' => $pagantisOrderId,
427
            'wc_order_id' => $wcOrderID,
428
            'token'       => $urlToken
429
        ), array('%s', '%s', '%s'));
430
431
        $logEntry = "Cart Added to Processing Queue" .
432
            " cart hash: ".WC()->cart->get_cart_hash().
0 ignored issues
show
Bug introduced by
The function WC was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

432
            " cart hash: "./** @scrutinizer ignore-call */ WC()->cart->get_cart_hash().
Loading history...
433
            " Merchant order id: ".$wcOrderID.
434
            " Pagantis order id: ".$pagantisOrderId.
435
            " Pagantis urlToken: ".$urlToken.
436
            " Pagantis Product: ".$methodID;
437
        insertLogEntry(null, $logEntry);
438
    } else {
439
        $wpdb->update($tableName,
440
            array('order_id' => $pagantisOrderId,'token' => $urlToken),
441
            array('wc_order_id' => $wcOrderID),
442
            array('%s,%s'),
443
            array('%s'));
444
    }
445
446
}
447
448
function alterCartProcessTable()
449
{
450
    global $wpdb;
451
    $tableName = $wpdb->prefix . PG_CART_PROCESS_TABLE;
452
    if (! $wpdb->get_var( "SHOW COLUMNS FROM `{$tableName}` LIKE 'token';" ) ) {
453
        $wpdb->query("ALTER TABLE $tableName ADD COLUMN `token` VARCHAR(32) NOT NULL AFTER `order_id`");
454
        $wpdb->query("ALTER TABLE $tableName DROP PRIMARY KEY, ADD PRIMARY KEY(order_id)");
455
        // OLDER VERSIONS OF MODULE USE UNIQUE KEY ON `id` MEANING THIS VALUE WAS NULLABLE
456
        $wpdb->query("ALTER TABLE $tableName MODIFY `id` INT AUTO_INCREMENT");
457
    }
458
}
459
460
/**
461
 * Check if cart processing table exists
462
 */
463
function checkCartProcessTable()
464
{
465
    global $wpdb;
466
    $tableName = $wpdb->prefix . PG_CART_PROCESS_TABLE;
467
468
    if ($wpdb->get_var("SHOW TABLES LIKE '$tableName'") != $tableName) {
469
470
        $charset_collate = $wpdb->get_charset_collate();
471
        $sql = "CREATE TABLE IF NOT EXISTS $tableName(
472
            `id` INT AUTO_INCREMENT, 
473
            `order_id` varchar(60),
474
            `wc_order_id` varchar(60),
475
            `token` varchar(32) NOT NULL,
476
             PRIMARY KEY (`id`, `order_id`)
477
            )$charset_collate";
478
479
        require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
0 ignored issues
show
Bug introduced by
The constant ABSPATH was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
480
        dbDelta($sql);
0 ignored issues
show
Bug introduced by
The function dbDelta was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

480
        /** @scrutinizer ignore-call */ 
481
        dbDelta($sql);
Loading history...
481
    }
482
}
483
484
485
/**
486
 * Get the orders of a customer
487
 *
488
 * @param WP_User $current_user
0 ignored issues
show
Bug introduced by
The type WP_User was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
489
 * @param $billingEmail
490
 *
491
 * @return mixed
492
 * @uses Wordpress Core Post API
493
 */
494
function getOrders($current_user, $billingEmail)
495
{
496
    $sign_up         = '';
497
    $total_orders    = 0;
0 ignored issues
show
Unused Code introduced by
The assignment to $total_orders is dead and can be removed.
Loading history...
498
    $total_amt       = 0;
0 ignored issues
show
Unused Code introduced by
The assignment to $total_amt is dead and can be removed.
Loading history...
499
    $refund_amt      = 0;
0 ignored issues
show
Unused Code introduced by
The assignment to $refund_amt is dead and can be removed.
Loading history...
500
    $total_refunds   = 0;
0 ignored issues
show
Unused Code introduced by
The assignment to $total_refunds is dead and can be removed.
Loading history...
501
    $partial_refunds = 0;
0 ignored issues
show
Unused Code introduced by
The assignment to $partial_refunds is dead and can be removed.
Loading history...
502
    if ($current_user->user_login) {
503
        $is_guest        = "false";
0 ignored issues
show
Unused Code introduced by
The assignment to $is_guest is dead and can be removed.
Loading history...
504
        $sign_up         = substr($current_user->user_registered, 0, 10);
0 ignored issues
show
Unused Code introduced by
The assignment to $sign_up is dead and can be removed.
Loading history...
505
        $customer_orders = get_posts(array(
0 ignored issues
show
Bug introduced by
The function get_posts was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

505
        $customer_orders = /** @scrutinizer ignore-call */ get_posts(array(
Loading history...
506
            'numberposts' => -1,
507
            'meta_key'    => '_customer_user',
508
            'meta_value'  => $current_user->ID,
509
            'post_type'   => array('shop_order'),
510
            'post_status' => array('wc-completed', 'wc-processing', 'wc-refunded'),
511
        ));
512
    } else {
513
        $is_guest        = "true";
514
        $customer_orders = get_posts(array(
515
            'numberposts' => -1,
516
            'meta_key'    => '_billing_email',
517
            'meta_value'  => $billingEmail,
518
            'post_type'   => array('shop_order'),
519
            'post_status' => array('wc-completed', 'wc-processing', 'wc-refunded'),
520
        ));
521
        foreach ($customer_orders as $customer_order) {
522
            if (trim($sign_up) == '' || strtotime(substr($customer_order->post_date, 0, 10)) <= strtotime($sign_up)) {
523
                $sign_up = substr($customer_order->post_date, 0, 10);
524
            }
525
        }
526
    }
527
528
    return $customer_orders;
529
}
530