Passed
Pull Request — master (#48)
by
unknown
02:31
created

pg_canProductSimulatorLoad()   B

Complexity

Conditions 8
Paths 6

Size

Total Lines 17
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 8
eloc 13
c 2
b 0
f 0
nc 6
nop 0
dl 0
loc 17
rs 8.4444
1
<?php
2
3
4
use Pagantis\ModuleUtils\Model\Log\LogEntry;
5
6
require_once dirname(__FILE__) . '/class-wc-pagantis-config.php';
7
8
function pg_areKeysSet()
9
{
10
    $settings = pg_get_plugin_settings();
11
    if ($settings['pagantis_public_key'] == '' || $settings['pagantis_private_key'] == '') {
12
        return false;
13
    }
14
15
    return true;
16
}
17
18
function pg_isSimulatorSettingEnabled()
19
{
20
    $settings = pg_get_plugin_settings();
21
22
    if ($settings['simulator'] !== 'yes') {
23
        WC_Admin_Settings::add_error(__('Error: PG Simulator is not enabled', 'pagantis'));
0 ignored issues
show
Bug introduced by
The type WC_Admin_Settings 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...
Bug introduced by
The function __ 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

23
        WC_Admin_Settings::add_error(/** @scrutinizer ignore-call */ __('Error: PG Simulator is not enabled', 'pagantis'));
Loading history...
24
25
        return false;
26
    }
27
28
    return true;
29
}
30
31
function pg_IsCountryAllowed()
32
{
33
    $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

33
    $locale           = strtolower(strstr(/** @scrutinizer ignore-call */ get_locale(), '_', true));
Loading history...
34
    $allowedCountries = WC_Pagantis_Config::getAllowedCountriesSerialized();
35
    $allowedCountry   = (in_array(strtolower($locale), $allowedCountries));
36
37
    if (! $allowedCountry) {
38
        return false;
39
    }
40
41
    return true;
42
}
43
44
function pg_IsAmountValid()
45
{
46
    global $product;
47
    $locale           = pg_GetLocaleString();
48
    $allowedCountries = WC_Pagantis_Config::getAllowedCountriesSerialized();
49
    $allowedCountry   = (in_array(strtolower($locale), $allowedCountries));
0 ignored issues
show
Unused Code introduced by
The assignment to $allowedCountry is dead and can be removed.
Loading history...
50
    $minAmount        = WC_Pagantis_Config::getValueOfKey('PAGANTIS_DISPLAY_MIN_AMOUNT');
51
    $maxAmount        = WC_Pagantis_Config::getValueOfKey('PAGANTIS_DISPLAY_MAX_AMOUNT');
52
    $totalPrice       = $product->get_price();
53
    $validAmount      = ($totalPrice >= $minAmount && ($totalPrice <= $maxAmount || $maxAmount == '0'));
54
    if (! $validAmount) {
55
        return false;
56
    }
57
58
    return true;
59
}
60
61
function pg_get_plugin_settings()
62
{
63
    $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

63
    $settings = /** @scrutinizer ignore-call */ get_option('woocommerce_pagantis_settings');
Loading history...
64
65
    return $settings;
66
}
67
68
function pg_GetLocaleString()
69
{
70
    $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

70
    $locale = strtolower(strstr(/** @scrutinizer ignore-call */ get_locale(), '_', true));
Loading history...
71
72
    return $locale;
73
}
74
75
function pg_canProductSimulatorLoad()
76
{
77
    global $product;
78
79
    $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

79
    $settings         = /** @scrutinizer ignore-call */ get_option('woocommerce_pagantis_settings');
Loading history...
80
    $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

80
    $locale           = strtolower(strstr(/** @scrutinizer ignore-call */ get_locale(), '_', true));
Loading history...
81
    $allowedCountries = WC_Pagantis_Config::getAllowedCountriesSerialized();
82
    $allowedCountry   = (in_array(strtolower($locale), $allowedCountries));
0 ignored issues
show
Unused Code introduced by
The assignment to $allowedCountry is dead and can be removed.
Loading history...
83
    $minAmount        = WC_Pagantis_Config::getValueOfKey('PAGANTIS_DISPLAY_MIN_AMOUNT');
84
    $maxAmount        = WC_Pagantis_Config::getValueOfKey('PAGANTIS_DISPLAY_MAX_AMOUNT');
85
    $totalPrice       = $product->get_price();
86
    $validAmount      = ($totalPrice >= $minAmount && ($totalPrice <= $maxAmount || $maxAmount == '0'));
0 ignored issues
show
Unused Code introduced by
The assignment to $validAmount is dead and can be removed.
Loading history...
87
    if ($settings['enabled'] !== 'yes' || pg_areKeysSet() || pg_isSimulatorSettingEnabled()
88
        || ! pg_IsCountryAllowed()
89
        || ! pg_IsAmountValid()
90
    ) {
91
        return;
92
    }
93
}
94
95
/**
96
 * @param $product_id
97
 *
98
 * @return string
99
 */
100
function pg_isProductPromoted($product_id)
101
{
102
    $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

102
    $metaProduct = /** @scrutinizer ignore-call */ get_post_meta($product_id);
Loading history...
103
104
    return (array_key_exists('custom_product_pagantis_promoted', $metaProduct)
105
            && $metaProduct['custom_product_pagantis_promoted']['0'] === 'yes') ? 'true' : 'false';
106
}
107
108
/**
109
 * Determines if the plugin is active.*
110
 *
111
 * @return bool True, if in the active plugins list. False, not in the list.
112
 * @since 8.3.7
113
 */
114
function pg_isPluginActive()
115
{
116
    return in_array('pagantis', (array) get_option('active_plugins', array()), true);
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

116
    return in_array('pagantis', (array) /** @scrutinizer ignore-call */ get_option('active_plugins', array()), true);
Loading history...
117
}
118
119
120
/**
121
 * @param $css_price_selector
122
 *
123
 * @return mixed|string
124
 */
125
function preparePriceSelector($css_price_selector)
126
{
127
    if ($css_price_selector === 'default' || $css_price_selector === '') {
128
        $css_price_selector === $this->defaultConfigs['PAGANTIS_SIMULATOR_CSS_PRICE_SELECTOR'];
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $this seems to be never defined.
Loading history...
129
    } elseif (! unserialize($css_price_selector)) { //in the case of a custom string selector, we keep it
130
        $css_price_selector = serialize(array($css_price_selector));
131
    }
132
133
    return $css_price_selector;
134
}
135
136
function pg_wc_is_screen_correct()
137
{
138
    $current_screen = get_current_screen();
0 ignored issues
show
Bug introduced by
The function get_current_screen 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

138
    $current_screen = /** @scrutinizer ignore-call */ get_current_screen();
Loading history...
139
    if ('shop_order' === $current_screen->id || 'plugins' === $current_screen->id
140
        || 'woocommerce_page_wc-settings' === $current_screen->id
141
    ) {
142
        return true;
143
    }
144
145
    return false;
146
}
147
148
149
function pg_wc_get_pagantis_admin_url()
150
{
151
    return admin_url('admin.php?page=wc-settings&tab=checkout&section=pagantis');
0 ignored issues
show
Bug introduced by
The function admin_url 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

151
    return /** @scrutinizer ignore-call */ admin_url('admin.php?page=wc-settings&tab=checkout&section=pagantis');
Loading history...
152
}
153
154
/**
155
 * @global wpdb $wpdb WordPress database abstraction object.
156
 */
157
function pg_wc_check_db_log_table()
158
{
159
    global $wpdb;
160
    $tableName = $wpdb->prefix . PAGANTIS_LOGS_TABLE;
161
    if ($wpdb->get_var("SHOW TABLES LIKE '$tableName'") !== $tableName) {
162
        $charset_collate = $wpdb->get_charset_collate();
163
        $sql             = "CREATE TABLE $tableName ( id int NOT NULL AUTO_INCREMENT, log text NOT NULL, 
164
                    createdAt timestamp DEFAULT CURRENT_TIMESTAMP, UNIQUE KEY id (id)) $charset_collate";
165
        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...
166
        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

166
        /** @scrutinizer ignore-call */ 
167
        dbDelta($sql);
Loading history...
167
    }
168
169
    return;
170
}
171
172
173
/**
174
 * Determine if the store is running SSL.
175
 *
176
 * @return bool Flag SSL enabled.
177
 * @since  8.3.7
178
 */
179
function pg_wc_is_ssl_active()
180
{
181
    $shop_page = wc_get_page_permalink('shop');
0 ignored issues
show
Bug introduced by
The function wc_get_page_permalink 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

181
    $shop_page = /** @scrutinizer ignore-call */ wc_get_page_permalink('shop');
Loading history...
182
183
    return (is_ssl() && 'https' === substr($shop_page, 0, 5));
0 ignored issues
show
Bug introduced by
The function is_ssl 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

183
    return (/** @scrutinizer ignore-call */ is_ssl() && 'https' === substr($shop_page, 0, 5));
Loading history...
184
}
185