Completed
Push — master ( f6d0fe...75b072 )
by
unknown
16s queued 10s
created

maybeGetPhoneNumber()   B

Complexity

Conditions 8
Paths 19

Size

Total Lines 19
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 8
eloc 10
c 0
b 0
f 0
nc 19
nop 2
dl 0
loc 19
rs 8.4444
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
 * @param $customerID
349
 * @return string
350
 * @throws \Exception
351
 */
352
function maybeGetIDNumber($order, $customerID)
353
{
354
355
    $number = '';
356
    $customer = new WC_Customer($customerID);
0 ignored issues
show
Bug introduced by
The type WC_Customer 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...
357
358
    if ($order instanceof WC_Order) {
0 ignored issues
show
Bug introduced by
The type WC_Order 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...
359
        $metaKeys = array(
360
            'national_id',
361
            'dni',
362
            'nie',
363
            'shipping_nif',
364
            'billing_nif'
365
        );
366
367
        foreach ($metaKeys as $key) {
368
369
            $number = $order->get_meta($key);
370
371
            if (empty($number) && !empty($customer)) {
372
                $number = $customer->get_meta($key);
373
            }
374
            if (!empty($number)) {
375
                break;
376
            }
377
        }
378
    }
379
    return is_string($number) ? $number : '';
380
}
381
382
/**
383
 * @param $order
384
 * @param $customerID
385
 * @return string
386
 * @throws \Exception
387
 */
388
function maybeGetTaxIDNumber($order, $customerID)
389
{
390
391
    $number = '';
392
    $customer = new WC_Customer($customerID);
393
394
    if ($order instanceof WC_Order) {
395
396
        $metaKeys = array(
397
            'billing_cfpiva',
398
            '_vat_number',
399
            'fiscalcode',
400
            'tax_id',
401
            'cif',
402
            '_billing_vat_number',
403
            'billing_vat_number',
404
            'billing_eu_vat_number',
405
            'VAT Number',
406
            'vat_number',
407
            '_billing_wc_avatax_vat_id'
408
        );
409
410
        foreach ($metaKeys as $key) {
411
            $number = $order->get_meta($key);
412
413
            if (empty($number) && !empty($customer)) {
414
                $number = $customer->get_meta($key);
415
            }
416
417
            if (!empty($number)) {
418
                break;
419
            }
420
        }
421
    }
422
423
    return is_string($number) ? $number : '';
424
}
425
426
427
/**
428
 * @param $order
429
 * @param $customerID
430
 * @return string|void
431
 * @throws \Exception
432
 */
433
function maybeGetPhoneNumber($order, $customerID)
434
{
435
436
    $isWCVersionLT3 = version_compare(WC_VERSION, '3.0', '<');
0 ignored issues
show
Bug introduced by
The constant WC_VERSION was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
437
    $customer = new WC_Customer($customerID);
438
    $number = '';
439
440
    if (!$order instanceof WC_Order || !$customer instanceof WC_Customer) {
441
        return;
442
    }
443
444
    if (!empty($customer)) {
445
        $number = $isWCVersionLT3 ? $customer->billing_phone : $customer->get_billing_phone();
446
    }
447
    if (empty($number)) {
448
        $number = $isWCVersionLT3 ? $order->billing_phone : $order->get_billing_phone();
449
    }
450
451
    return is_string($number) ? $number : '';
452
}
453
454
/**
455
 * @param $customerID
456
 * @return string|void
457
 * @throws \Exception
458
 */
459
function maybeGetDateOfBirth($customerID)
460
{
461
462
    $isWCVersionLT3 = version_compare(WC_VERSION, '3.0', '<');
0 ignored issues
show
Bug introduced by
The constant WC_VERSION was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
Unused Code introduced by
The assignment to $isWCVersionLT3 is dead and can be removed.
Loading history...
463
    $customer = new WC_Customer($customerID);
464
    $date = '';
465
466
    if (!empty($customer)) {
467
    $metaKeys = array(
468
        'birthday',
469
        'date_of_birth',
470
        'dob',
471
    );
472
473
        foreach ($metaKeys as $key) {
474
            $date = empty($customer->get_meta($key)) ? $customer->get_meta($key) : get_user_meta( $customerID, '_automatewoo_birthday_full', true );
0 ignored issues
show
Bug introduced by
The function get_user_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

474
            $date = empty($customer->get_meta($key)) ? $customer->get_meta($key) : /** @scrutinizer ignore-call */ get_user_meta( $customerID, '_automatewoo_birthday_full', true );
Loading history...
475
        }
476
477
    }
478
479
    return is_string($date) ? $date : '';
480
}
481
482
/**
483
 * @param $product_id
484
 *
485
 * @return string
486
 */
487
function isProductPromoted($product_id)
488
{
489
    $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

489
    $metaProduct = /** @scrutinizer ignore-call */ get_post_meta($product_id);
Loading history...
490
491
    return (array_key_exists('custom_product_pagantis_promoted', $metaProduct)
492
            && $metaProduct['custom_product_pagantis_promoted']['0'] === 'yes') ? 'true' : 'false';
493
}
494
495
/**
496
 * @return int
497
 */
498
function getPromotedAmount()
499
{
500
    $promotedAmount = 0;
501
    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

501
    foreach (/** @scrutinizer ignore-call */ WC()->cart->get_cart() as $key => $item) {
Loading history...
502
        $promotedProduct = isProductPromoted($item['product_id']);
503
        if ($promotedProduct == 'true') {
504
            $promotedAmount += $item['line_total'] + $item['line_tax'];
505
        }
506
    }
507
508
    return $promotedAmount;
509
}
510
511
/**
512
 * @param $pagantisOrderId
513
 * @param $wcOrderID
514
 * @param $urlToken
515
 * @param $methodID
516
 */
517
function addOrderToProcessingQueue($pagantisOrderId, $wcOrderID, $urlToken ,$methodID)
518
{
519
    global $wpdb;
520
    checkCartProcessTable();
521
    $tableName = $wpdb->prefix . PG_CART_PROCESS_TABLE;
522
523
    //Check if id exists
524
    $resultsSelect = $wpdb->get_results("SELECT * FROM $tableName WHERE id='$pagantisOrderId'");
525
    $countResults  = count($resultsSelect);
526
527
    if ($countResults == 0) {
528
        $wpdb->insert($tableName, array(
529
            'order_id' => $pagantisOrderId,
530
            'wc_order_id' => $wcOrderID,
531
            'token'       => $urlToken
532
        ), array('%s', '%s', '%s'));
533
534
        $logEntry = "Cart Added to Processing Queue" .
535
            " 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

535
            " cart hash: "./** @scrutinizer ignore-call */ WC()->cart->get_cart_hash().
Loading history...
536
            " Merchant order id: ".$wcOrderID.
537
            " Pagantis order id: ".$pagantisOrderId.
538
            " Pagantis urlToken: ".$urlToken.
539
            " Pagantis Product: ".$methodID;
540
        insertLogEntry(null, $logEntry);
541
    } else {
542
        $wpdb->update($tableName,
543
            array('order_id' => $pagantisOrderId,'token' => $urlToken),
544
            array('wc_order_id' => $wcOrderID),
545
            array('%s,%s'),
546
            array('%s'));
547
    }
548
549
}
550
551
function alterCartProcessTable()
552
{
553
    global $wpdb;
554
    $tableName = $wpdb->prefix . PG_CART_PROCESS_TABLE;
555
    if (! $wpdb->get_var( "SHOW COLUMNS FROM `{$tableName}` LIKE 'token';" ) ) {
556
        $wpdb->query("ALTER TABLE $tableName ADD COLUMN `token` VARCHAR(32) NOT NULL AFTER `order_id`");
557
        $wpdb->query("ALTER TABLE $tableName DROP PRIMARY KEY, ADD PRIMARY KEY(order_id)");
558
        // OLDER VERSIONS OF MODULE USE UNIQUE KEY ON `id` MEANING THIS VALUE WAS NULLABLE
559
        $wpdb->query("ALTER TABLE $tableName MODIFY `id` INT AUTO_INCREMENT");
560
    }
561
}
562
563
/**
564
 * Check if cart processing table exists
565
 */
566
function checkCartProcessTable()
567
{
568
    global $wpdb;
569
    $tableName = $wpdb->prefix . PG_CART_PROCESS_TABLE;
570
571
    if ($wpdb->get_var("SHOW TABLES LIKE '$tableName'") != $tableName) {
572
573
        $charset_collate = $wpdb->get_charset_collate();
574
        $sql = "CREATE TABLE IF NOT EXISTS $tableName(
575
            `id` INT AUTO_INCREMENT, 
576
            `order_id` varchar(60),
577
            `wc_order_id` varchar(60),
578
            `token` varchar(32) NOT NULL,
579
             PRIMARY KEY (`id`, `order_id`)
580
            )$charset_collate";
581
582
        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...
583
        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

583
        /** @scrutinizer ignore-call */ 
584
        dbDelta($sql);
Loading history...
584
    }
585
}
586
587
588
/**
589
 * Get the orders of a customer
590
 *
591
 * @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...
592
 * @param $billingEmail
593
 *
594
 * @return mixed
595
 * @uses Wordpress Core Post API
596
 */
597
function getOrders($current_user, $billingEmail)
598
{
599
    $sign_up         = '';
600
    $total_orders    = 0;
0 ignored issues
show
Unused Code introduced by
The assignment to $total_orders is dead and can be removed.
Loading history...
601
    $total_amt       = 0;
0 ignored issues
show
Unused Code introduced by
The assignment to $total_amt is dead and can be removed.
Loading history...
602
    $refund_amt      = 0;
0 ignored issues
show
Unused Code introduced by
The assignment to $refund_amt is dead and can be removed.
Loading history...
603
    $total_refunds   = 0;
0 ignored issues
show
Unused Code introduced by
The assignment to $total_refunds is dead and can be removed.
Loading history...
604
    $partial_refunds = 0;
0 ignored issues
show
Unused Code introduced by
The assignment to $partial_refunds is dead and can be removed.
Loading history...
605
    if ($current_user->user_login) {
606
        $is_guest        = "false";
0 ignored issues
show
Unused Code introduced by
The assignment to $is_guest is dead and can be removed.
Loading history...
607
        $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...
608
        $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

608
        $customer_orders = /** @scrutinizer ignore-call */ get_posts(array(
Loading history...
609
            'numberposts' => -1,
610
            'meta_key'    => '_customer_user',
611
            'meta_value'  => $current_user->ID,
612
            'post_type'   => array('shop_order'),
613
            'post_status' => array('wc-completed', 'wc-processing', 'wc-refunded'),
614
        ));
615
    } else {
616
        $is_guest        = "true";
617
        $customer_orders = get_posts(array(
618
            'numberposts' => -1,
619
            'meta_key'    => '_billing_email',
620
            'meta_value'  => $billingEmail,
621
            'post_type'   => array('shop_order'),
622
            'post_status' => array('wc-completed', 'wc-processing', 'wc-refunded'),
623
        ));
624
        foreach ($customer_orders as $customer_order) {
625
            if (trim($sign_up) == '' || strtotime(substr($customer_order->post_date, 0, 10)) <= strtotime($sign_up)) {
626
                $sign_up = substr($customer_order->post_date, 0, 10);
627
            }
628
        }
629
    }
630
631
    return $customer_orders;
632
}
633
634