Completed
Push — master ( 026455...55c688 )
by
unknown
13s
created

WcPagantis::preparePriceSelector()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 6
c 1
b 0
f 0
nc 3
nop 0
dl 0
loc 10
rs 10
1
<?php
2
/**
3
 * Plugin Name: Pagantis
4
 * Plugin URI: http://www.pagantis.com/
5
 * Description: Financiar con Pagantis
6
 * Version: 8.1.2
7
 * Author: Pagantis
8
 */
9
10
//namespace Gateways;
11
12
13
if (!defined('ABSPATH')) {
14
    exit;
15
}
16
17
class WcPagantis
18
{
19
    const GIT_HUB_URL = 'https://github.com/pagantis/woocommerce';
20
    const PAGANTIS_DOC_URL = 'https://developer.pagantis.com';
21
    const SUPPORT_EML = 'mailto:[email protected]?Subject=woocommerce_plugin';
22
23
    /** Concurrency tablename */
24
    const LOGS_TABLE = 'pagantis_logs';
25
26
    /** Config tablename */
27
    const CONFIG_TABLE = 'pagantis_config';
28
29
    /** Concurrency tablename  */
30
    const CONCURRENCY_TABLE = 'pagantis_concurrency';
31
32
    public $defaultConfigs = array('PAGANTIS_TITLE'=>'Instant Financing',
33
                            'PAGANTIS_SIMULATOR_DISPLAY_TYPE'=>'pgSDK.simulator.types.SIMPLE',
34
                            'PAGANTIS_SIMULATOR_DISPLAY_SKIN'=>'pgSDK.simulator.skins.BLUE',
35
                            'PAGANTIS_SIMULATOR_DISPLAY_POSITION'=>'hookDisplayProductButtons',
36
                            'PAGANTIS_SIMULATOR_START_INSTALLMENTS'=>3,
37
                            'PAGANTIS_SIMULATOR_MAX_INSTALLMENTS'=>12,
38
                            'PAGANTIS_SIMULATOR_CSS_POSITION_SELECTOR'=>'default',
39
                            'PAGANTIS_SIMULATOR_DISPLAY_CSS_POSITION'=>'pgSDK.simulator.positions.INNER',
40
                            'PAGANTIS_SIMULATOR_CSS_PRICE_SELECTOR'=>'a:3:{i:0;s:48:"div.summary *:not(del)>.woocommerce-Price-amount";i:1;s:54:"div.entry-summary *:not(del)>.woocommerce-Price-amount";i:2;s:36:"*:not(del)>.woocommerce-Price-amount";}',
41
                            'PAGANTIS_SIMULATOR_CSS_QUANTITY_SELECTOR'=>'a:2:{i:0;s:22:"div.quantity input.qty";i:1;s:18:"div.quantity>input";}',
42
                            'PAGANTIS_FORM_DISPLAY_TYPE'=>0,
43
                            'PAGANTIS_DISPLAY_MIN_AMOUNT'=>1,
44
                            'PAGANTIS_URL_OK'=>'',
45
                            'PAGANTIS_URL_KO'=>'',
46
                            'PAGANTIS_ALLOWED_COUNTRIES' => 'a:2:{i:0;s:2:"es";i:1;s:2:"it";}'
47
    );
48
49
    /** @var Array $extraConfig */
50
    public $extraConfig;
51
52
    /**
53
     * WC_Pagantis constructor.
54
     */
55
    public function __construct()
56
    {
57
        require_once(plugin_dir_path(__FILE__).'/vendor/autoload.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

57
        require_once(/** @scrutinizer ignore-call */ plugin_dir_path(__FILE__).'/vendor/autoload.php');
Loading history...
58
59
        $this->template_path = plugin_dir_path(__FILE__).'/templates/';
0 ignored issues
show
Bug Best Practice introduced by
The property template_path does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
60
61
        $this->pagantisActivation();
62
63
        $this->extraConfig = $this->getExtraConfig();
64
65
        load_plugin_textdomain('pagantis', false, basename(dirname(__FILE__)).'/languages');
0 ignored issues
show
Bug introduced by
The function load_plugin_textdomain 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

65
        /** @scrutinizer ignore-call */ 
66
        load_plugin_textdomain('pagantis', false, basename(dirname(__FILE__)).'/languages');
Loading history...
66
        add_filter('woocommerce_payment_gateways', array($this, 'addPagantisGateway'));
0 ignored issues
show
Bug introduced by
The function add_filter 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

66
        /** @scrutinizer ignore-call */ 
67
        add_filter('woocommerce_payment_gateways', array($this, 'addPagantisGateway'));
Loading history...
67
        add_filter('woocommerce_available_payment_gateways', array($this, 'pagantisFilterGateways'), 9999);
68
        add_filter('plugin_row_meta', array($this, 'pagantisRowMeta'), 10, 2);
69
        add_filter('plugin_action_links_'.plugin_basename(__FILE__), array($this, 'pagantisActionLinks'));
0 ignored issues
show
Bug introduced by
The function plugin_basename 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

69
        add_filter('plugin_action_links_'./** @scrutinizer ignore-call */ plugin_basename(__FILE__), array($this, 'pagantisActionLinks'));
Loading history...
70
        add_action('woocommerce_after_add_to_cart_form', array($this, 'pagantisAddProductSimulator'));
0 ignored issues
show
Bug introduced by
The function add_action 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
        /** @scrutinizer ignore-call */ 
71
        add_action('woocommerce_after_add_to_cart_form', array($this, 'pagantisAddProductSimulator'));
Loading history...
71
        add_action('wp_enqueue_scripts', 'add_pagantis_widget_js');
72
        add_action('rest_api_init', array($this, 'pagantisRegisterEndpoint')); //Endpoint
73
        add_filter('load_textdomain_mofile', array($this, 'loadPagantisTranslation'), 10, 2);
74
    }
75
76
    /*
77
     * Replace 'textdomain' with your plugin's textdomain. e.g. 'woocommerce'.
78
     * File to be named, for example, yourtranslationfile-en_GB.mo
79
     * File to be placed, for example, wp-content/lanaguages/textdomain/yourtranslationfile-en_GB.mo
80
     */
81
    public function loadPagantisTranslation($mofile, $domain)
82
    {
83
        if ('pagantis' === $domain) {
84
            $mofile = WP_LANG_DIR . '/../plugins/pagantis/languages/pagantis-' . get_locale() . '.mo';
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

84
            $mofile = WP_LANG_DIR . '/../plugins/pagantis/languages/pagantis-' . /** @scrutinizer ignore-call */ get_locale() . '.mo';
Loading history...
Bug introduced by
The constant WP_LANG_DIR was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
85
        }
86
        return $mofile;
87
    }
88
89
    /**
90
     * Sql table
91
     */
92
    public function pagantisActivation()
93
    {
94
        global $wpdb;
95
96
        $tableName = $wpdb->prefix.self::CONCURRENCY_TABLE;
97
        if ($wpdb->get_var("SHOW TABLES LIKE '$tableName'") != $tableName) {
98
            $charset_collate = $wpdb->get_charset_collate();
99
            $sql = "CREATE TABLE $tableName ( order_id int NOT NULL,  
100
                    createdAt timestamp DEFAULT CURRENT_TIMESTAMP, UNIQUE KEY id (order_id)) $charset_collate";
101
            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...
102
            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

102
            /** @scrutinizer ignore-call */ 
103
            dbDelta($sql);
Loading history...
103
        }
104
105
        $tableName = $wpdb->prefix.self::CONFIG_TABLE;
106
107
        //Check if table exists
108
        $tableExists = $wpdb->get_var("SHOW TABLES LIKE '$tableName'") != $tableName;
109
        if ($tableExists) {
110
            $charset_collate = $wpdb->get_charset_collate();
111
            $sql = "CREATE TABLE IF NOT EXISTS $tableName (
112
                                id int NOT NULL AUTO_INCREMENT, 
113
                                config varchar(60) NOT NULL, 
114
                                value varchar(1000) NOT NULL, 
115
                                UNIQUE KEY id(id)) $charset_collate";
116
117
            require_once(ABSPATH.'wp-admin/includes/upgrade.php');
118
            dbDelta($sql);
119
        }
120
121
        $dbConfigs = $wpdb->get_results("select * 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...
122
123
        // Convert a multimple dimension array for SQL insert statements into a simple key/value
124
        $simpleDbConfigs = array();
125
        foreach ($dbConfigs as $config) {
126
            $simpleDbConfigs[$config['config']] = $config['value'];
127
        }
128
        $newConfigs = array_diff_key($this->defaultConfigs, $simpleDbConfigs);
129
        if (!empty($newConfigs)) {
130
            foreach ($newConfigs as $key => $value) {
131
                $wpdb->insert($tableName, array('config' => $key, 'value'  => $value), array('%s', '%s'));
132
            }
133
        }
134
135
        //Current plugin config: pagantis_public_key => New field --- public_key => Old field
136
        $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

136
        $settings = /** @scrutinizer ignore-call */ get_option('woocommerce_pagantis_settings');
Loading history...
137
138
        if (!isset($settings['pagantis_public_key']) && $settings['public_key']) {
139
            $settings['pagantis_public_key'] = $settings['public_key'];
140
            unset($settings['public_key']);
141
        }
142
143
        if (!isset($settings['pagantis_private_key']) && $settings['secret_key']) {
144
            $settings['pagantis_private_key'] = $settings['secret_key'];
145
            unset($settings['secret_key']);
146
        }
147
148
        update_option('woocommerce_pagantis_settings', $settings);
0 ignored issues
show
Bug introduced by
The function update_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

148
        /** @scrutinizer ignore-call */ 
149
        update_option('woocommerce_pagantis_settings', $settings);
Loading history...
149
    }
150
151
    /**
152
     * Product simulator
153
     */
154
    public function pagantisAddProductSimulator()
155
    {
156
        global $product;
157
158
        $cfg = 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

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

159
        $locale = strtolower(strstr(/** @scrutinizer ignore-call */ get_locale(), '_', true));
Loading history...
160
        $allowedCountries = unserialize($this->extraConfig['PAGANTIS_ALLOWED_COUNTRIES']);
161
        $allowedCountry = (in_array(strtolower($locale), $allowedCountries));
162
        if ($cfg['enabled'] !== 'yes' || $cfg['pagantis_public_key'] == '' || $cfg['pagantis_private_key'] == '' ||
163
            $cfg['simulator'] !== 'yes' ||  $product->price < $this->extraConfig['PAGANTIS_DISPLAY_MIN_AMOUNT'] ||
164
            !$allowedCountry ) {
165
            return;
166
        }
167
168
        $css_quantity_selector = $this->prepareQuantitySelector();
169
        $css_price_selector = $this->preparePriceSelector();
170
        $template_fields = array(
171
            'total'    => is_numeric($product->price) ? $product->price : 0,
172
            'public_key' => $cfg['pagantis_public_key'],
173
            'simulator_type' => $this->extraConfig['PAGANTIS_SIMULATOR_DISPLAY_TYPE'],
174
            'positionSelector' => $this->extraConfig['PAGANTIS_SIMULATOR_CSS_POSITION_SELECTOR'],
175
            'quantitySelector' => unserialize($css_quantity_selector),
176
            'priceSelector' => unserialize($css_price_selector),
177
            'totalAmount' => is_numeric($product->price) ? $product->price : 0,
178
            'locale' => $locale
179
        );
180
        wc_get_template('product_simulator.php', $template_fields, '', $this->template_path);
0 ignored issues
show
Bug introduced by
The function wc_get_template 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

180
        /** @scrutinizer ignore-call */ 
181
        wc_get_template('product_simulator.php', $template_fields, '', $this->template_path);
Loading history...
181
    }
182
183
    /**
184
     * Add Pagantis to payments list.
185
     *
186
     * @param $methods
187
     *
188
     * @return array
189
     */
190
    public function addPagantisGateway($methods)
191
    {
192
        if (! class_exists('WC_Payment_Gateway')) {
193
            return $methods;
194
        }
195
196
        include_once('controllers/paymentController.php');
197
        $methods[] = 'WcPagantisGateway';
198
199
        return $methods;
200
    }
201
202
    /**
203
     * Initialize WC_Pagantis class
204
     *
205
     * @param $methods
206
     *
207
     * @return mixed
208
     */
209
    public function pagantisFilterGateways($methods)
210
    {
211
        $pagantis = new WcPagantisGateway();
212
        if (!$pagantis->is_available()) {
213
            unset($methods['pagantis']);
214
        }
215
216
        return $methods;
217
    }
218
219
    /**
220
     * Add links to Plugin description
221
     *
222
     * @param $links
223
     *
224
     * @return mixed
225
     */
226
    public function pagantisActionLinks($links)
227
    {
228
        $params_array = array('page' => 'wc-settings', 'tab' => 'checkout', 'section' => 'pagantis');
229
        $setting_url  = esc_url(add_query_arg($params_array, admin_url('admin.php?')));
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

229
        $setting_url  = esc_url(add_query_arg($params_array, /** @scrutinizer ignore-call */ admin_url('admin.php?')));
Loading history...
Bug introduced by
The function esc_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

229
        $setting_url  = /** @scrutinizer ignore-call */ esc_url(add_query_arg($params_array, admin_url('admin.php?')));
Loading history...
Bug introduced by
The function add_query_arg 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

229
        $setting_url  = esc_url(/** @scrutinizer ignore-call */ add_query_arg($params_array, admin_url('admin.php?')));
Loading history...
230
        $setting_link = '<a href="'.$setting_url.'">'.__('Settings', 'pagantis').'</a>';
0 ignored issues
show
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

230
        $setting_link = '<a href="'.$setting_url.'">'./** @scrutinizer ignore-call */ __('Settings', 'pagantis').'</a>';
Loading history...
231
232
        array_unshift($links, $setting_link);
233
234
        return $links;
235
    }
236
237
    /**
238
     * Add links to Plugin options
239
     *
240
     * @param $links
241
     * @param $file
242
     *
243
     * @return array
244
     */
245
    public function pagantisRowMeta($links, $file)
246
    {
247
        if ($file == plugin_basename(__FILE__)) {
0 ignored issues
show
Bug introduced by
The function plugin_basename 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
        if ($file == /** @scrutinizer ignore-call */ plugin_basename(__FILE__)) {
Loading history...
248
            $links[] = '<a href="'.WcPagantis::GIT_HUB_URL.'" target="_blank">'.__('Documentation', 'pagantis').'</a>';
0 ignored issues
show
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

248
            $links[] = '<a href="'.WcPagantis::GIT_HUB_URL.'" target="_blank">'./** @scrutinizer ignore-call */ __('Documentation', 'pagantis').'</a>';
Loading history...
249
            $links[] = '<a href="'.WcPagantis::PAGANTIS_DOC_URL.'" target="_blank">'.
250
            __('API documentation', 'pagantis').'</a>';
251
            $links[] = '<a href="'.WcPagantis::SUPPORT_EML.'">'.__('Support', 'pagantis').'</a>';
252
253
            return $links;
254
        }
255
256
        return $links;
257
    }
258
259
    /**
260
     * Read logs
261
     */
262
    public function readLogs($data)
263
    {
264
        global $wpdb;
265
        $filters   = ($data->get_params());
266
        $response  = array();
267
        $secretKey = $filters['secret'];
268
        $from = $filters['from'];
269
        $to   = $filters['to'];
270
        $cfg  = 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

270
        $cfg  = /** @scrutinizer ignore-call */ get_option('woocommerce_pagantis_settings');
Loading history...
271
        $privateKey = isset($cfg['secret_key']) ? $cfg['secret_key'] : null;
272
        $tableName = $wpdb->prefix.self::LOGS_TABLE;
273
        $query = "select * from $tableName where createdAt>$from and createdAt<$to order by createdAt desc";
274
        $results = $wpdb->get_results($query);
275
        if (isset($results) && $privateKey == $secretKey) {
276
            foreach ($results as $key => $result) {
277
                $response[$key]['timestamp'] = $result->createdAt;
278
                $response[$key]['log']       = json_decode($result->log);
279
            }
280
        } else {
281
            $response['result'] = 'Error';
282
        }
283
        $response = json_encode($response);
284
        header("HTTP/1.1 200", true, 200);
285
        header('Content-Type: application/json', true);
286
        header('Content-Length: '.strlen($response));
287
        echo($response);
288
        exit();
0 ignored issues
show
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
289
    }
290
291
    /**
292
     * Update extra config
293
     */
294
    public function updateExtraConfig($data)
295
    {
296
        global $wpdb;
297
        $tableName = $wpdb->prefix.self::CONFIG_TABLE;
298
        $response = array('status'=>null);
299
300
        $filters   = ($data->get_params());
301
        $secretKey = $filters['secret'];
302
        $cfg  = 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

302
        $cfg  = /** @scrutinizer ignore-call */ get_option('woocommerce_pagantis_settings');
Loading history...
303
        $privateKey = isset($cfg['pagantis_private_key']) ? $cfg['pagantis_private_key'] : null;
304
        if ($privateKey != $secretKey) {
305
            $response['status'] = 401;
306
            $response['result'] = 'Unauthorized';
307
        } elseif ($_SERVER['REQUEST_METHOD'] == 'POST') {
308
            if (count($_POST)) {
309
                foreach ($_POST as $config => $value) {
310
                    if (isset($this->defaultConfigs[$config]) && $response['status']==null) {
311
                        $wpdb->update(
312
                            $tableName,
313
                            array('value' => $value),
314
                            array('config' => $config),
315
                            array('%s'),
316
                            array('%s')
317
                        );
318
                    } else {
319
                        $response['status'] = 400;
320
                        $response['result'] = 'Bad request';
321
                    }
322
                }
323
            } else {
324
                $response['status'] = 422;
325
                $response['result'] = 'Empty data';
326
            }
327
        }
328
329
        if ($response['status']==null) {
330
            $tableName = $wpdb->prefix.self::CONFIG_TABLE;
331
            $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...
332
            foreach ($dbResult as $value) {
333
                $formattedResult[$value['config']] = $value['value'];
334
            }
335
            $response['result'] = $formattedResult;
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $formattedResult seems to be defined by a foreach iteration on line 332. Are you sure the iterator is never empty, otherwise this variable is not defined?
Loading history...
336
        }
337
338
        $result = json_encode($response['result']);
339
        header("HTTP/1.1 ".$response['status'], true, $response['status']);
340
        header('Content-Type: application/json', true);
341
        header('Content-Length: '.strlen($result));
342
        echo($result);
343
        exit();
0 ignored issues
show
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
344
    }
345
346
    /**
347
     * ENDPOINT - Read logs -> Hook: rest_api_init
348
     * @return mixed
349
     */
350
    public function pagantisRegisterEndpoint()
351
    {
352
        register_rest_route(
0 ignored issues
show
Bug introduced by
The function register_rest_route 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

352
        /** @scrutinizer ignore-call */ 
353
        register_rest_route(
Loading history...
353
            'pagantis/v1',
354
            '/logs/(?P<secret>\w+)/(?P<from>\d+)/(?P<to>\d+)',
355
            array(
356
            'methods'  => 'GET',
357
            'callback' => array(
358
                $this,
359
                'readLogs')
360
            ),
361
            true
362
        );
363
364
        register_rest_route(
365
            'pagantis/v1',
366
            '/configController/(?P<secret>\w+)',
367
            array(
368
                'methods'  => 'GET, POST',
369
                'callback' => array(
370
                    $this,
371
                    'updateExtraConfig')
372
            ),
373
            true
374
        );
375
    }
376
377
    /**
378
     * @return array
379
     */
380
    private function getExtraConfig()
381
    {
382
        global $wpdb;
383
        $tableName = $wpdb->prefix.self::CONFIG_TABLE;
384
        $response = array();
385
        $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...
386
        foreach ($dbResult as $value) {
387
            $response[$value['config']] = $value['value'];
388
        }
389
390
        return $response;
391
    }
392
393
    /**
394
     * @return mixed|string
395
     */
396
    private function prepareQuantitySelector()
397
    {
398
        $css_quantity_selector = $this->extraConfig['PAGANTIS_SIMULATOR_CSS_QUANTITY_SELECTOR'];
399
        if ($css_quantity_selector == 'default' || $css_quantity_selector == '') {
400
            $css_quantity_selector = $this->defaultConfigs['PAGANTIS_SIMULATOR_CSS_QUANTITY_SELECTOR'];
401
        } elseif (!unserialize($css_quantity_selector)) { //in the case of a custom string selector, we keep it
402
            $css_quantity_selector = serialize(array($css_quantity_selector));
403
        }
404
405
        return $css_quantity_selector;
406
    }
407
408
    /**
409
     * @return mixed|string
410
     */
411
    private function preparePriceSelector()
412
    {
413
        $css_price_selector = $this->extraConfig['PAGANTIS_SIMULATOR_CSS_PRICE_SELECTOR'];
414
        if ($css_price_selector == 'default' || $css_price_selector == '') {
415
            $css_price_selector = $this->defaultConfigs['PAGANTIS_SIMULATOR_CSS_PRICE_SELECTOR'];
416
        } elseif (!unserialize($css_price_selector)) { //in the case of a custom string selector, we keep it
417
            $css_price_selector = serialize(array($css_price_selector));
418
        }
419
420
        return $css_price_selector;
421
    }
422
}
423
424
/**
425
 * Add widget Js
426
 **/
427
function add_pagantis_widget_js()
428
{
429
    wp_enqueue_script('pgSDK', 'https://cdn.pagantis.com/js/pg-v2/sdk.js', '', '', true);
0 ignored issues
show
Bug introduced by
The function wp_enqueue_script 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

429
    /** @scrutinizer ignore-call */ 
430
    wp_enqueue_script('pgSDK', 'https://cdn.pagantis.com/js/pg-v2/sdk.js', '', '', true);
Loading history...
430
    wp_enqueue_script('pmtSDK', 'https://cdn.pagamastarde.com/js/pmt-v2/sdk.js', '', '', true);
431
}
432
433
$WcPagantis = new WcPagantis();
434