Total Complexity | 133 |
Total Lines | 842 |
Duplicated Lines | 0 % |
Changes | 42 | ||
Bugs | 0 | Features | 2 |
Complex classes like WcPagantis often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use WcPagantis, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
36 | class WcPagantis |
||
37 | { |
||
38 | const GIT_HUB_URL = 'https://github.com/pagantis/woocommerce'; |
||
39 | const PAGANTIS_DOC_URL = 'https://developer.pagantis.com'; |
||
40 | const SUPPORT_EML = 'mailto:[email protected]?Subject=woocommerce_plugin'; |
||
41 | |||
42 | |||
43 | public $defaultConfigs = array( |
||
44 | 'PAGANTIS_TITLE'=>'Instant financing', |
||
45 | 'PAGANTIS_SIMULATOR_DISPLAY_TYPE'=>'sdk.simulator.types.PRODUCT_PAGE', |
||
46 | 'PAGANTIS_SIMULATOR_DISPLAY_TYPE_CHECKOUT'=>'sdk.simulator.types.CHECKOUT_PAGE', |
||
47 | 'PAGANTIS_SIMULATOR_DISPLAY_SKIN'=>'sdk.simulator.skins.BLUE', |
||
48 | 'PAGANTIS_SIMULATOR_DISPLAY_POSITION'=>'hookDisplayProductButtons', |
||
49 | 'PAGANTIS_SIMULATOR_START_INSTALLMENTS'=>3, |
||
50 | 'PAGANTIS_SIMULATOR_MAX_INSTALLMENTS'=>12, |
||
51 | 'PAGANTIS_SIMULATOR_CSS_POSITION_SELECTOR'=>'default', |
||
52 | 'PAGANTIS_SIMULATOR_DISPLAY_CSS_POSITION'=>'sdk.simulator.positions.INNER', |
||
53 | 'PAGANTIS_SIMULATOR_CSS_PRICE_SELECTOR'=>'a:4:{i:0;s:52:"div.summary *:not(del)>.woocommerce-Price-amount bdi";i:1;s:48:"div.summary *:not(del)>.woocommerce-Price-amount";i:2;s:54:"div.entry-summary *:not(del)>.woocommerce-Price-amount";i:3;s:36:"*:not(del)>.woocommerce-Price-amount";}', |
||
54 | 'PAGANTIS_SIMULATOR_CSS_QUANTITY_SELECTOR'=>'a:2:{i:0;s:22:"div.quantity input.qty";i:1;s:18:"div.quantity>input";}', |
||
55 | 'PAGANTIS_FORM_DISPLAY_TYPE'=>0, |
||
56 | 'PAGANTIS_DISPLAY_MIN_AMOUNT'=>1, |
||
57 | 'PAGANTIS_DISPLAY_MAX_AMOUNT'=>1500, |
||
58 | 'PAGANTIS_URL_OK'=>'', |
||
59 | 'PAGANTIS_URL_KO'=>'', |
||
60 | 'PAGANTIS_ALLOWED_COUNTRIES' => 'a:3:{i:0;s:2:"es";i:1;s:2:"it";i:2;s:2:"fr";}', |
||
61 | 'PAGANTIS_PROMOTION_EXTRA' => '<p>Finance this product <span class="pg-no-interest">without interest!</span></p>', |
||
62 | 'PAGANTIS_SIMULATOR_THOUSANDS_SEPARATOR' => '.', |
||
63 | 'PAGANTIS_SIMULATOR_DECIMAL_SEPARATOR' => ',', |
||
64 | 'PAGANTIS_SIMULATOR_DISPLAY_SITUATION' => 'default', |
||
65 | 'PAGANTIS_SIMULATOR_SELECTOR_VARIATION' => 'default', |
||
66 | //4x |
||
67 | 'PAGANTIS_DISPLAY_MIN_AMOUNT_4x'=>0, |
||
68 | 'PAGANTIS_DISPLAY_MAX_AMOUNT_4x'=>800, |
||
69 | 'PAGANTIS_TITLE_4x'=>'Hasta 4 pagos, sin coste', |
||
70 | |||
71 | ); |
||
72 | |||
73 | /** @var array $extraConfig */ |
||
74 | public $extraConfig; |
||
75 | |||
76 | /** |
||
77 | * WC_Pagantis constructor. |
||
78 | */ |
||
79 | public function __construct() |
||
80 | { |
||
81 | require_once(plugin_dir_path(__FILE__).'/vendor/autoload.php'); |
||
82 | require_once(PG_ABSPATH . '/includes/pg-functions.php'); |
||
83 | require_once(PG_ABSPATH . '/includes/logger.php'); |
||
84 | $this->template_path = plugin_dir_path(__FILE__).'/templates/'; |
||
85 | |||
86 | $this->pagantisActivation(); |
||
87 | |||
88 | $this->extraConfig = getExtraConfig(); |
||
89 | |||
90 | load_plugin_textdomain('pagantis', false, dirname(plugin_basename(__FILE__)).'/languages'); |
||
91 | |||
92 | add_action('plugins_loaded', array($this, 'pagantisCheckTables')); |
||
93 | add_filter('woocommerce_payment_gateways', array($this, 'addPagantisGateway')); |
||
94 | add_filter('woocommerce_available_payment_gateways', array($this, 'pagantisFilterGateways'), 9999); |
||
95 | add_filter('plugin_row_meta', array($this, 'pagantisRowMeta'), 10, 2); |
||
96 | add_filter('plugin_action_links_'.plugin_basename(__FILE__), array($this, 'pagantisActionLinks')); |
||
97 | add_action('init', array($this, 'checkWcPriceSettings'), 10); |
||
98 | add_action('woocommerce_after_template_part', array($this, 'pagantisAddSimulatorHtmlDiv'), 10); |
||
99 | add_action('woocommerce_single_product_summary', array($this, 'pagantisInitProductSimulator'), 20); |
||
100 | add_action('woocommerce_single_variation', array($this,'pagantisAddProductSnippetForVariations'), 30); |
||
101 | add_action('wp_enqueue_scripts', 'add_pagantis_widget_js'); |
||
102 | add_action('rest_api_init', array($this, 'pagantisRegisterEndpoint')); //Endpoint |
||
103 | add_filter('load_textdomain_mofile', array($this, 'loadPagantisTranslation'), 10, 2); |
||
104 | register_activation_hook(__FILE__, array($this, 'pagantisActivation')); |
||
105 | add_action('woocommerce_product_options_general_product_data', array($this, 'pagantisPromotedProductTpl')); |
||
106 | add_action('woocommerce_process_product_meta', array($this, 'pagantisPromotedVarSave')); |
||
107 | add_action('woocommerce_product_bulk_edit_start', array($this,'pagantisPromotedBulkTemplate')); |
||
108 | add_action('woocommerce_product_bulk_edit_save', array($this,'pagantisPromotedBulkTemplateSave')); |
||
109 | } |
||
110 | |||
111 | /** |
||
112 | * Piece of html code to insert into BULK admin edit |
||
113 | */ |
||
114 | public function pagantisPromotedBulkTemplate() |
||
115 | { |
||
116 | echo '<div class="inline-edit-group"> |
||
117 | <label class="alignleft"> |
||
118 | <span class="title">Pagantis promoted</span> |
||
119 | <span class="input-text-wrap"> |
||
120 | <input type="checkbox" id="pagantis_promoted" name="pagantis_promoted"/> |
||
121 | </span> |
||
122 | </label> |
||
123 | </div>'; |
||
124 | } |
||
125 | |||
126 | /** |
||
127 | * Php code to save our meta after a bulk admin edit |
||
128 | * @param $product |
||
129 | */ |
||
130 | public function pagantisPromotedBulkTemplateSave($product) |
||
131 | { |
||
132 | $post_id = $product->get_id(); |
||
133 | $pagantis_promoted_value = $_REQUEST['pagantis_promoted']; |
||
134 | if ($pagantis_promoted_value === 'on') { |
||
135 | $pagantis_promoted_value = 'yes'; |
||
136 | } else { |
||
137 | $pagantis_promoted_value = 'no'; |
||
138 | } |
||
139 | |||
140 | update_post_meta($post_id, 'custom_product_pagantis_promoted', esc_attr($pagantis_promoted_value)); |
||
141 | } |
||
142 | |||
143 | /** |
||
144 | * Piece of html code to insert into PRODUCT admin edit |
||
145 | */ |
||
146 | public function pagantisPromotedProductTpl() |
||
147 | { |
||
148 | global $post; |
||
149 | $_product = get_post_meta($post->ID); |
||
150 | woocommerce_wp_checkbox( |
||
151 | array( |
||
152 | 'id' => 'pagantis_promoted', |
||
153 | 'label' => __('Pagantis promoted', 'woocommerce'), |
||
154 | 'value' => $_product['custom_product_pagantis_promoted']['0'], |
||
155 | 'cbvalue' => 'yes', |
||
156 | 'echo' => true |
||
157 | ) |
||
158 | ); |
||
159 | } |
||
160 | |||
161 | /** |
||
162 | * Php code to save our meta after a PRODUCT admin edit |
||
163 | * @param $post_id |
||
164 | */ |
||
165 | public function pagantisPromotedVarSave($post_id) |
||
173 | } |
||
174 | |||
175 | /* |
||
176 | * Replace 'textdomain' with your plugin's textdomain. e.g. 'woocommerce'. |
||
177 | * File to be named, for example, yourtranslationfile-en_GB.mo |
||
178 | * File to be placed, for example, wp-content/languages/textdomain/yourtranslationfile-en_GB.mo |
||
179 | */ |
||
180 | public function loadPagantisTranslation($mofile, $domain) |
||
186 | } |
||
187 | |||
188 | |||
189 | /** |
||
190 | * |
||
191 | */ |
||
192 | public function pagantisCheckTables() |
||
193 | { |
||
194 | // |
||
195 | } |
||
196 | |||
197 | /** |
||
198 | * Sql table |
||
199 | */ |
||
200 | public function pagantisActivation() |
||
201 | { |
||
202 | global $wpdb; |
||
203 | |||
204 | $tableName = $wpdb->prefix.PG_CONCURRENCY_TABLE_NAME; |
||
205 | if ($wpdb->get_var("SHOW TABLES LIKE '$tableName'") != $tableName) { |
||
206 | $charset_collate = $wpdb->get_charset_collate(); |
||
207 | $sql = "CREATE TABLE $tableName ( order_id int NOT NULL, |
||
208 | createdAt timestamp DEFAULT CURRENT_TIMESTAMP, UNIQUE KEY id (order_id)) $charset_collate"; |
||
209 | require_once(ABSPATH.'wp-admin/includes/upgrade.php'); |
||
210 | dbDelta($sql); |
||
211 | } |
||
212 | |||
213 | $tableName = $wpdb->prefix.PG_CONFIG_TABLE_NAME; |
||
214 | |||
215 | //Check if table exists |
||
216 | $tableExists = $wpdb->get_var("SHOW TABLES LIKE '$tableName'") != $tableName; |
||
217 | if ($tableExists) { |
||
218 | $charset_collate = $wpdb->get_charset_collate(); |
||
219 | $sql = "CREATE TABLE IF NOT EXISTS $tableName ( |
||
220 | id int NOT NULL AUTO_INCREMENT, |
||
221 | config varchar(60) NOT NULL, |
||
222 | value varchar(1000) NOT NULL, |
||
223 | UNIQUE KEY id(id)) $charset_collate"; |
||
224 | |||
225 | require_once(ABSPATH.'wp-admin/includes/upgrade.php'); |
||
226 | dbDelta($sql); |
||
227 | } else { |
||
228 | //Updated value field to adapt to new length < v8.0.1 |
||
229 | $query = "select COLUMN_TYPE FROM information_schema.COLUMNS where TABLE_NAME='$tableName' AND COLUMN_NAME='value'"; |
||
230 | $results = $wpdb->get_results($query, ARRAY_A); |
||
231 | if ($results['0']['COLUMN_TYPE'] == 'varchar(100)') { |
||
232 | $sql = "ALTER TABLE $tableName MODIFY value varchar(1000)"; |
||
233 | $wpdb->query($sql); |
||
234 | } |
||
235 | |||
236 | //Adapting selector to array < v8.1.1 |
||
237 | $query = "select * from $tableName where config='PAGANTIS_SIMULATOR_CSS_QUANTITY_SELECTOR' |
||
238 | or config='PAGANTIS_SIMULATOR_CSS_PRICE_SELECTOR'"; |
||
239 | $dbCurrentConfig = $wpdb->get_results($query, ARRAY_A); |
||
240 | foreach ($dbCurrentConfig as $item) { |
||
241 | if ($item['config'] == 'PAGANTIS_SIMULATOR_CSS_PRICE_SELECTOR') { |
||
242 | $css_price_selector = $this->preparePriceSelector($item['value']); |
||
243 | if ($item['value'] != $css_price_selector) { |
||
244 | $wpdb->update( |
||
245 | $tableName, |
||
246 | array('value' => stripslashes($css_price_selector)), |
||
247 | array('config' => 'PAGANTIS_SIMULATOR_CSS_PRICE_SELECTOR'), |
||
248 | array('%s'), |
||
249 | array('%s') |
||
250 | ); |
||
251 | } |
||
252 | } elseif ($item['config'] == 'PAGANTIS_SIMULATOR_CSS_QUANTITY_SELECTOR') { |
||
253 | $css_quantity_selector = $this->prepareQuantitySelector($item['value']); |
||
254 | if ($item['value'] != $css_quantity_selector) { |
||
255 | $wpdb->update( |
||
256 | $tableName, |
||
257 | array('value' => stripslashes($css_quantity_selector)), |
||
258 | array('config' => 'PAGANTIS_SIMULATOR_CSS_QUANTITY_SELECTOR'), |
||
259 | array('%s'), |
||
260 | array('%s') |
||
261 | ); |
||
262 | } |
||
263 | } |
||
264 | } |
||
265 | } |
||
266 | |||
267 | // Creating new cart processing table < 8.6.13 |
||
268 | if (isPgTableCreated(PG_CART_PROCESS_TABLE)){ |
||
269 | pagantisLogger::log(PG_CART_PROCESS_TABLE . " ALTERED " . "on " . __LINE__ . " in " . __FILE__); |
||
270 | pagantisLogger::log(pagantisLogger::pg_debug_backtrace()); |
||
271 | alterCartProcessingTable(); |
||
272 | } else{ |
||
273 | pagantisLogger::log(PG_CART_PROCESS_TABLE . " CREATED " . "on " . __LINE__ . " in " . __FILE__); |
||
274 | pagantisLogger::log(pagantisLogger::pg_debug_backtrace()); |
||
275 | createCartProcessingTable(); |
||
276 | } |
||
277 | |||
278 | // Making sure DB tables are created < v8.6.9 |
||
279 | if (!isPgTableCreated(PG_LOGS_TABLE_NAME)){ |
||
280 | createLogsTable(); |
||
281 | } |
||
282 | |||
283 | //Adapting selector to array < v8.2.2 |
||
284 | $tableName = $wpdb->prefix.PG_CONFIG_TABLE_NAME; |
||
285 | $query = "select * from $tableName where config='PAGANTIS_SIMULATOR_THOUSANDS_SEPARATOR'"; |
||
286 | $results = $wpdb->get_results($query, ARRAY_A); |
||
287 | if (count($results) == 0) { |
||
288 | $wpdb->insert($tableName, array('config' => 'PAGANTIS_SIMULATOR_THOUSANDS_SEPARATOR', 'value' => '.'), array('%s', '%s')); |
||
289 | $wpdb->insert($tableName, array('config' => 'PAGANTIS_SIMULATOR_DECIMAL_SEPARATOR', 'value' => ','), array('%s', '%s')); |
||
290 | } |
||
291 | |||
292 | //Adding new selector < v8.3.0 |
||
293 | $tableName = $wpdb->prefix.PG_CONFIG_TABLE_NAME; |
||
294 | $query = "select * from $tableName where config='PAGANTIS_DISPLAY_MAX_AMOUNT'"; |
||
295 | $results = $wpdb->get_results($query, ARRAY_A); |
||
296 | if (count($results) == 0) { |
||
297 | $wpdb->insert($tableName, array('config' => 'PAGANTIS_DISPLAY_MAX_AMOUNT', 'value' => '0'), array('%s', '%s')); |
||
298 | } |
||
299 | |||
300 | //Adding new selector < v8.3.2 |
||
301 | $tableName = $wpdb->prefix.PG_CONFIG_TABLE_NAME; |
||
302 | $query = "select * from $tableName where config='PAGANTIS_SIMULATOR_DISPLAY_SITUATION'"; |
||
303 | $results = $wpdb->get_results($query, ARRAY_A); |
||
304 | if (count($results) == 0) { |
||
305 | $wpdb->insert($tableName, array('config' => 'PAGANTIS_SIMULATOR_DISPLAY_SITUATION', 'value' => 'default'), array('%s', '%s')); |
||
306 | $wpdb->insert($tableName, array('config' => 'PAGANTIS_SIMULATOR_SELECTOR_VARIATION', 'value' => 'default'), array('%s', '%s')); |
||
307 | } |
||
308 | |||
309 | |||
310 | //Adding new selector < v8.3.3 |
||
311 | $tableName = $wpdb->prefix.PG_CONFIG_TABLE_NAME; |
||
312 | $query = "select * from $tableName where config='PAGANTIS_SIMULATOR_DISPLAY_TYPE_CHECKOUT'"; |
||
313 | $results = $wpdb->get_results($query, ARRAY_A); |
||
314 | if (count($results) == 0) { |
||
315 | $wpdb->insert($tableName, array('config' => 'PAGANTIS_SIMULATOR_DISPLAY_TYPE_CHECKOUT', 'value' => 'sdk.simulator.types.CHECKOUT_PAGE'), array('%s', '%s')); |
||
316 | $wpdb->update($tableName, array('value' => 'sdk.simulator.types.PRODUCT_PAGE'), array('config' => 'PAGANTIS_SIMULATOR_DISPLAY_TYPE'), array('%s'), array('%s')); |
||
317 | } |
||
318 | |||
319 | //Adapting to variable selector < v8.3.6 |
||
320 | $variableSelector="div.summary div.woocommerce-variation.single_variation > div.woocommerce-variation-price span.price"; |
||
321 | $tableName = $wpdb->prefix.PG_CONFIG_TABLE_NAME; |
||
322 | $query = "select * from $tableName where config='PAGANTIS_SIMULATOR_SELECTOR_VARIATION' and value='default'"; |
||
323 | $results = $wpdb->get_results($query, ARRAY_A); |
||
324 | if (count($results) == 0) { |
||
325 | $wpdb->update($tableName, array('value' => $variableSelector), array('config' => 'PAGANTIS_SIMULATOR_SELECTOR_VARIATION'), array('%s'), array('%s')); |
||
326 | } |
||
327 | |||
328 | //Adapting vars to 4x < v8.6.x |
||
329 | $tableName = $wpdb->prefix.PG_CONFIG_TABLE_NAME; |
||
330 | $query = "select * from $tableName where config='PAGANTIS_TITLE_4x'"; |
||
331 | $results = $wpdb->get_results($query, ARRAY_A); |
||
332 | if (count($results) == 0) { |
||
333 | $wpdb->insert($tableName, array('config' => 'PAGANTIS_TITLE_4x', 'value' => 'Until 4 installments, without fees'), array('%s', '%s')); |
||
334 | $wpdb->insert($tableName, array('config' => 'PAGANTIS_DISPLAY_MIN_AMOUNT_4x', 'value' => 1), array('%s', '%s')); |
||
335 | $wpdb->insert($tableName, array('config' => 'PAGANTIS_DISPLAY_MAX_AMOUNT_4x', 'value' => 800), array('%s', '%s')); |
||
336 | |||
337 | $wpdb->update($tableName, array('value' => 'Instant financing'), array('config' => 'PAGANTIS_TITLE'), array('%s'), array('%s')); |
||
338 | $wpdb->update($tableName, array('value' => 1500), array('config' => 'PAGANTIS_DISPLAY_MAX_AMOUNT'), array('%s'), array('%s')); |
||
339 | } |
||
340 | |||
341 | //Adapting situation var of 4x < v8.6.2 |
||
342 | $tableName = $wpdb->prefix.PG_CONFIG_TABLE_NAME; |
||
343 | $query = "select * from $tableName where config='PAGANTIS_SIMULATOR_CSS_POSITION_SELECTOR_4X'"; |
||
344 | $results = $wpdb->get_results($query, ARRAY_A); |
||
345 | if (count($results) == 0) { |
||
346 | $wpdb->insert($tableName, array('config' => 'PAGANTIS_SIMULATOR_CSS_POSITION_SELECTOR_4X', 'value' => 'default'), array('%s', '%s')); |
||
347 | } |
||
348 | |||
349 | //Adding WC price separator verifications to adapt extra config dynamically < v8.3.9 |
||
350 | if (!areDecimalSeparatorEqual()) { |
||
351 | updateDecimalSeparatorDbConfig(); |
||
352 | } |
||
353 | if (!areThousandsSeparatorEqual()) { |
||
354 | updateThousandsSeparatorDbConfig(); |
||
355 | } |
||
356 | |||
357 | //Adapting product price selector < v8.6.7 |
||
358 | $tableName = $wpdb->prefix.PG_CONFIG_TABLE_NAME; |
||
359 | $query = "select * from $tableName where config='PAGANTIS_SIMULATOR_CSS_PRICE_SELECTOR'"; |
||
360 | $results = $wpdb->get_results($query, ARRAY_A); |
||
361 | if (count($results) == 0) { |
||
362 | $wpdb->update($tableName, array('value' => 'a:4:{i:0;s:52:"div.summary *:not(del)>.woocommerce-Price-amount bdi";i:1;s:48:"div.summary *:not(del)>.woocommerce-Price-amount";i:2;s:54:"div.entry-summary *:not(del)>.woocommerce-Price-amount";i:3;s:36:"*:not(del)>.woocommerce-Price-amount";}'), array('config' => 'PAGANTIS_SIMULATOR_CSS_PRICE_SELECTOR'), array('%s'), array('%s')); |
||
363 | } |
||
364 | |||
365 | $dbConfigs = $wpdb->get_results("select * from $tableName", ARRAY_A); |
||
366 | |||
367 | // Convert a multiple dimension array for SQL insert statements into a simple key/value |
||
368 | $simpleDbConfigs = array(); |
||
369 | foreach ($dbConfigs as $config) { |
||
370 | $simpleDbConfigs[$config['config']] = $config['value']; |
||
371 | } |
||
372 | $newConfigs = array_diff_key($this->defaultConfigs, $simpleDbConfigs); |
||
373 | if (!empty($newConfigs)) { |
||
374 | foreach ($newConfigs as $key => $value) { |
||
375 | $wpdb->insert($tableName, array('config' => $key, 'value' => $value), array('%s', '%s')); |
||
376 | } |
||
377 | } |
||
378 | |||
379 | //Current plugin config: pagantis_public_key => New field --- public_key => Old field |
||
380 | $settings = get_option('woocommerce_pagantis_settings'); |
||
381 | |||
382 | if (!isset($settings['pagantis_public_key']) && $settings['public_key']) { |
||
383 | $settings['pagantis_public_key'] = $settings['public_key']; |
||
384 | unset($settings['public_key']); |
||
385 | } |
||
386 | |||
387 | if (!isset($settings['pagantis_private_key']) && $settings['secret_key']) { |
||
388 | $settings['pagantis_private_key'] = $settings['secret_key']; |
||
389 | unset($settings['secret_key']); |
||
390 | } |
||
391 | |||
392 | update_option('woocommerce_pagantis_settings', $settings); |
||
393 | } |
||
394 | |||
395 | /** |
||
396 | * Checks the WC settings to know if we should modify our config |
||
397 | */ |
||
398 | public function checkWcPriceSettings() |
||
399 | { |
||
400 | if (!is_product() || !is_shop()) { |
||
401 | return; |
||
402 | } |
||
403 | $this->checkWcDecimalSeparatorSettings(); |
||
404 | $this->checkWcThousandsSeparatorSettings(); |
||
405 | } |
||
406 | |||
407 | /** |
||
408 | * Check woocommerce_price_thousand_sep and update our config if necessary |
||
409 | */ |
||
410 | private function checkWcThousandsSeparatorSettings() |
||
411 | { |
||
412 | if (areThousandsSeparatorEqual()) { |
||
413 | return; |
||
414 | } |
||
415 | if (!areThousandsSeparatorEqual()) { |
||
416 | updateThousandsSeparatorDbConfig(); |
||
417 | } |
||
418 | } |
||
419 | |||
420 | /** |
||
421 | * Check woocommerce_price_decimal_sep and update our config if necessary |
||
422 | */ |
||
423 | private function checkWcDecimalSeparatorSettings() |
||
424 | { |
||
425 | if (areDecimalSeparatorEqual()) { |
||
426 | return; |
||
427 | } |
||
428 | |||
429 | if (!areDecimalSeparatorEqual()) { |
||
430 | updateDecimalSeparatorDbConfig(); |
||
431 | } |
||
432 | } |
||
433 | |||
434 | /** |
||
435 | * Pushes the simulator div depending on the config and plugin settings |
||
436 | * |
||
437 | * @param $template_name |
||
438 | * |
||
439 | * @return bool|mixed|void |
||
440 | * @hooked woocommerce_after_template_part - 10 |
||
441 | * @see wc_get_template |
||
442 | */ |
||
443 | public function pagantisAddSimulatorHtmlDiv($template_name) |
||
444 | { |
||
445 | $areSimulatorTypesValid = isSimulatorTypeValid( |
||
446 | getConfigValue('PAGANTIS_SIMULATOR_DISPLAY_TYPE'), |
||
447 | array('sdk.simulator.types.SELECTABLE_TEXT_CUSTOM', |
||
448 | 'sdk.simulator.types.PRODUCT_PAGE') |
||
449 | ); |
||
450 | $isPriceTplPresent = isTemplatePresent($template_name, array('single-product/price.php')); |
||
451 | $isAtcTplPresent = isTemplatePresent( |
||
452 | $template_name, |
||
453 | array('single-product/add-to-cart/variation-add-to-cart-button.php', |
||
454 | 'single-product/add-to-cart/variation.php','single-product/add-to-cart/simple.php') |
||
455 | ); |
||
456 | |||
457 | $html = apply_filters('pagantis_simulator_selector_html', '<div class="mainPagantisSimulator"></div><div class="pagantisSimulator"></div>'); |
||
458 | |||
459 | |||
460 | $pagantisSimulator = 'enabled'; |
||
461 | if (!isPluginEnabled() || !areMerchantKeysSet() || !isSimulatorEnabled() || !isCountryShopContextValid() || !isProductAmountValid()) { |
||
462 | $pagantisSimulator = 'disabled'; |
||
463 | } |
||
464 | |||
465 | $pagantisSimulator4x = 'enabled'; |
||
466 | if (!isPluginEnabled4x() || !areMerchantKeysSet4x() || !isCountryShopContextValid() || !isProductAmountValid4x()) { |
||
467 | $pagantisSimulator4x = 'disabled'; |
||
468 | } |
||
469 | if ($pagantisSimulator === 'disabled' && $pagantisSimulator4x === 'disabled') { |
||
470 | return; |
||
471 | } |
||
472 | |||
473 | if (($areSimulatorTypesValid && $isPriceTplPresent) || (!$areSimulatorTypesValid && $isAtcTplPresent)) { |
||
474 | self::enqueueSimulatorCss(); |
||
475 | echo $html; |
||
476 | } |
||
477 | } |
||
478 | |||
479 | |||
480 | /** |
||
481 | * Init code required to update price for products with variations |
||
482 | * |
||
483 | */ |
||
484 | public function pagantisAddProductSnippetForVariations() |
||
485 | { |
||
486 | global $product; |
||
487 | if (!isPluginEnabled() || !areMerchantKeysSet() || !isSimulatorEnabled() || !isCountryShopContextValid() || !isProductAmountValid()) { |
||
488 | return; |
||
489 | } |
||
490 | |||
491 | wp_register_script('pg-product-variation-simulator', plugins_url('assets/js/pg-product-variation-simulator.js', PG_WC_MAIN_FILE), array('pg-product-simulator'), '', true); |
||
492 | |||
493 | $variationSimulatorData = array( |
||
494 | 'variationSelector' => $this->extraConfig['PAGANTIS_SIMULATOR_SELECTOR_VARIATION'], |
||
495 | 'productType' => $product->get_type() |
||
496 | ); |
||
497 | |||
498 | wp_localize_script('pg-product-variation-simulator', 'variationSimulatorData', $variationSimulatorData); |
||
499 | wp_enqueue_script('pg-product-variation-simulator'); |
||
500 | } |
||
501 | |||
502 | /** |
||
503 | * @return string|void |
||
504 | * |
||
505 | */ |
||
506 | public function pagantisInitProductSimulator() |
||
507 | { |
||
508 | global $product; |
||
509 | |||
510 | //12x |
||
511 | $pagantisSimulator = 'enabled'; |
||
512 | |||
513 | wp_register_script('pg-product-simulator', plugins_url('assets/js/pg-product-simulator.js', PG_WC_MAIN_FILE), array(), '', true); |
||
514 | $settings = get_option('woocommerce_pagantis_settings'); |
||
515 | $locale = strtolower(strstr(get_locale(), '_', true)); |
||
516 | if (!isPluginEnabled() || !areMerchantKeysSet() || !isSimulatorEnabled() || !isCountryShopContextValid() || !isProductAmountValid()) { |
||
517 | $pagantisSimulator = 'disabled'; |
||
518 | } |
||
519 | |||
520 | $pagantisSimulator4x = 'enabled'; |
||
521 | if (!isPluginEnabled4x() || !areMerchantKeysSet4x() || !isCountryShopContextValid() || !isProductAmountValid4x()) { |
||
522 | $pagantisSimulator4x = 'disabled'; |
||
523 | } |
||
524 | |||
525 | if ($pagantisSimulator === 'disabled' && $pagantisSimulator4x === 'disabled') { |
||
526 | return; |
||
527 | } |
||
528 | |||
529 | $totalPrice = $product->get_price(); |
||
530 | $formattedInstallments = number_format($totalPrice/4, 2); |
||
531 | $simulatorMessage = sprintf(__('or 4 installments of %s€, without fees, with ', 'pagantis'), $formattedInstallments); |
||
532 | $post_id = $product->get_id(); |
||
533 | $logo = 'https://cdn.digitalorigin.com/assets/master/logos/pg-130x30.svg'; |
||
534 | $simulatorData = array( |
||
535 | 'total' => is_numeric($product->get_price()) ? $product->get_price() : 0, |
||
536 | 'public_key' => $settings['pagantis_public_key'], |
||
537 | 'simulator_type' => $this->extraConfig['PAGANTIS_SIMULATOR_DISPLAY_TYPE'], |
||
538 | 'positionSelector' => $this->extraConfig['PAGANTIS_SIMULATOR_CSS_POSITION_SELECTOR'], |
||
539 | 'positionSelector4x' => $this->extraConfig['PAGANTIS_SIMULATOR_CSS_POSITION_SELECTOR_4X'], |
||
540 | 'quantitySelector' => unserialize($this->extraConfig['PAGANTIS_SIMULATOR_CSS_QUANTITY_SELECTOR']), |
||
541 | 'priceSelector' => unserialize($this->extraConfig['PAGANTIS_SIMULATOR_CSS_PRICE_SELECTOR']), |
||
542 | 'totalAmount' => is_numeric($product->get_price()) ? $product->get_price() : 0, |
||
543 | 'locale' => $locale, |
||
544 | 'country' => $locale, |
||
545 | 'promoted' => $this->isPromoted($post_id), |
||
546 | 'promotedMessage' => $this->extraConfig['PAGANTIS_PROMOTION_EXTRA'], |
||
547 | 'thousandSeparator' => $this->extraConfig['PAGANTIS_SIMULATOR_THOUSANDS_SEPARATOR'], |
||
548 | 'decimalSeparator' => $this->extraConfig['PAGANTIS_SIMULATOR_DECIMAL_SEPARATOR'], |
||
549 | 'pagantisQuotesStart' => $this->extraConfig['PAGANTIS_SIMULATOR_START_INSTALLMENTS'], |
||
550 | 'pagantisSimulatorSkin' => $this->extraConfig['PAGANTIS_SIMULATOR_DISPLAY_SKIN'], |
||
551 | 'pagantisSimulatorPosition' => $this->extraConfig['PAGANTIS_SIMULATOR_DISPLAY_CSS_POSITION'], |
||
552 | 'finalDestination' => $this->extraConfig['PAGANTIS_SIMULATOR_DISPLAY_SITUATION'], |
||
553 | 'variationSelector' => $this->extraConfig['PAGANTIS_SIMULATOR_SELECTOR_VARIATION'], |
||
554 | 'productType' => $product->get_type(), |
||
555 | 'pagantisSimulator' => $pagantisSimulator, |
||
556 | 'pagantisSimulator4x' => $pagantisSimulator4x, |
||
557 | 'simulatorMessage' => "$simulatorMessage<img class='mainImageLogo' src='$logo'/>" |
||
558 | ); |
||
559 | |||
560 | wp_localize_script('pg-product-simulator', 'simulatorData', $simulatorData); |
||
561 | wp_enqueue_script('pg-product-simulator'); |
||
562 | } |
||
563 | |||
564 | /** |
||
565 | * Add Pagantis to payments list. |
||
566 | * |
||
567 | * @param $methods |
||
568 | * |
||
569 | * @return array |
||
570 | */ |
||
571 | public function addPagantisGateway($methods) |
||
572 | { |
||
573 | if (! class_exists('WC_Payment_Gateway')) { |
||
574 | return $methods; |
||
575 | } |
||
576 | |||
577 | //4x |
||
578 | include_once('controllers/paymentController4x.php'); |
||
579 | $methods[] = 'WcPagantis4xGateway'; |
||
580 | |||
581 | //12x |
||
582 | include_once('controllers/paymentController.php'); |
||
583 | $methods[] = 'WcPagantisGateway'; |
||
584 | |||
585 | return $methods; |
||
586 | } |
||
587 | |||
588 | /** |
||
589 | * Initialize WC_Pagantis class |
||
590 | * |
||
591 | * @param $methods |
||
592 | * |
||
593 | * @return mixed |
||
594 | */ |
||
595 | public function pagantisFilterGateways($methods) |
||
596 | { |
||
597 | $pagantis4x = new WcPagantis4xGateway(); |
||
598 | if (!$pagantis4x->is_available()) { |
||
599 | unset($methods['pagantis4x']); |
||
600 | } |
||
601 | |||
602 | $pagantis = new WcPagantisGateway(); |
||
603 | if (!$pagantis->is_available()) { |
||
604 | unset($methods['pagantis']); |
||
605 | } |
||
606 | |||
607 | return $methods; |
||
608 | } |
||
609 | |||
610 | /** |
||
611 | * Add links to Plugin description |
||
612 | * |
||
613 | * @param $links |
||
614 | * |
||
615 | * @return mixed |
||
616 | */ |
||
617 | public function pagantisActionLinks($links) |
||
626 | } |
||
627 | |||
628 | /** |
||
629 | * Add links to Plugin options |
||
630 | * |
||
631 | * @param $links |
||
632 | * @param $file |
||
633 | * |
||
634 | * @return array |
||
635 | */ |
||
636 | public function pagantisRowMeta($links, $file) |
||
648 | } |
||
649 | |||
650 | /** |
||
651 | * Read logs |
||
652 | */ |
||
653 | public function readLogs($data) |
||
654 | { |
||
655 | global $wpdb; |
||
656 | $filters = ($data->get_params()); |
||
657 | $response = array(); |
||
658 | $secretKey = $filters['secret']; |
||
659 | $from = $filters['from']; |
||
660 | $to = $filters['to']; |
||
661 | $cfg = get_option('woocommerce_pagantis_settings'); |
||
662 | $privateKey = isset($cfg['pagantis_private_key']) ? $cfg['pagantis_private_key'] : null; |
||
663 | $privateKey4x = isset($cfg['pagantis_private_key_4x']) ? $cfg['pagantis_private_key_4x'] : null; |
||
664 | $tableName = $wpdb->prefix.PG_LOGS_TABLE_NAME; |
||
665 | $query = "select * from $tableName where createdAt>$from and createdAt<$to order by createdAt desc"; |
||
666 | $results = $wpdb->get_results($query); |
||
667 | if (isset($results) && ($privateKey == $secretKey || $privateKey4x == $secretKey)) { |
||
668 | foreach ($results as $key => $result) { |
||
669 | $response[$key]['timestamp'] = $result->createdAt; |
||
670 | $response[$key]['log'] = json_decode($result->log); |
||
671 | } |
||
672 | } else { |
||
673 | $response['result'] = 'Error'; |
||
674 | } |
||
675 | $response = json_encode($response); |
||
676 | header("HTTP/1.1 200", true, 200); |
||
677 | header('Content-Type: application/json', true); |
||
678 | header('Content-Length: '.strlen($response)); |
||
679 | echo($response); |
||
680 | exit(); |
||
681 | } |
||
682 | |||
683 | /** |
||
684 | * Update extra config |
||
685 | */ |
||
686 | public function updateExtraConfig($data) |
||
687 | { |
||
688 | global $wpdb; |
||
689 | $tableName = $wpdb->prefix.PG_CONFIG_TABLE_NAME; |
||
690 | $response = array('status'=>null); |
||
691 | |||
692 | $filters = ($data->get_params()); |
||
693 | $secretKey = $filters['secret']; |
||
694 | $cfg = get_option('woocommerce_pagantis_settings'); |
||
695 | $privateKey = isset($cfg['pagantis_private_key']) ? $cfg['pagantis_private_key'] : null; |
||
696 | $privateKey4x = isset($cfg['pagantis_private_key_4x']) ? $cfg['pagantis_private_key_4x'] : null; |
||
697 | if ($privateKey != $secretKey && $privateKey4x != $secretKey) { |
||
698 | $response['status'] = 401; |
||
699 | $response['result'] = 'Unauthorized'; |
||
700 | } elseif ($_SERVER['REQUEST_METHOD'] == 'POST') { |
||
701 | if (count($_POST)) { |
||
702 | foreach ($_POST as $config => $value) { |
||
703 | if (isset($this->defaultConfigs[$config]) && $response['status']==null) { |
||
704 | $wpdb->update( |
||
705 | $tableName, |
||
706 | array('value' => stripslashes($value)), |
||
707 | array('config' => $config), |
||
708 | array('%s'), |
||
709 | array('%s') |
||
710 | ); |
||
711 | } else { |
||
712 | $response['status'] = 400; |
||
713 | $response['result'] = 'Bad request'; |
||
714 | } |
||
715 | } |
||
716 | } else { |
||
717 | $response['status'] = 422; |
||
718 | $response['result'] = 'Empty data'; |
||
719 | } |
||
720 | } |
||
721 | |||
722 | if ($response['status']==null) { |
||
723 | $tableName = $wpdb->prefix.PG_CONFIG_TABLE_NAME; |
||
724 | $dbResult = $wpdb->get_results("select config, value from $tableName", ARRAY_A); |
||
725 | foreach ($dbResult as $value) { |
||
726 | $formattedResult[$value['config']] = $value['value']; |
||
727 | } |
||
728 | $response['result'] = $formattedResult; |
||
729 | } |
||
730 | |||
731 | $result = json_encode($response['result']); |
||
732 | header("HTTP/1.1 ".$response['status'], true, $response['status']); |
||
733 | header('Content-Type: application/json', true); |
||
734 | header('Content-Length: '.strlen($result)); |
||
735 | echo($result); |
||
736 | exit(); |
||
737 | } |
||
738 | |||
739 | /** |
||
740 | * Read logs |
||
741 | */ |
||
742 | public function readApi($data) |
||
743 | { |
||
744 | global $wpdb; |
||
745 | $filters = ($data->get_params()); |
||
746 | $response = array('timestamp'=>time()); |
||
747 | $secretKey = $filters['secret']; |
||
748 | $from = ($filters['from']) ? date_create($filters['from']) : date("Y-m-d", strtotime("-7 day")); |
||
749 | $to = ($filters['to']) ? date_create($filters['to']) : date("Y-m-d", strtotime("+1 day")); |
||
750 | $method = ($filters['method']) ? ($filters['method']) : 'Pagantis'; |
||
751 | $cfg = get_option('woocommerce_pagantis_settings'); |
||
752 | $privateKey = isset($cfg['pagantis_private_key']) ? $cfg['pagantis_private_key'] : null; |
||
753 | $privateKey4x = isset($cfg['pagantis_private_key_4x']) ? $cfg['pagantis_private_key_4x'] : null; |
||
754 | $tableName = $wpdb->prefix.PG_ORDERS_TABLE; |
||
755 | $tableNameInner = $wpdb->prefix.'postmeta'; |
||
756 | $query = "select * from $tableName tn INNER JOIN $tableNameInner tn2 ON tn2.post_id = tn.id |
||
757 | where tn.post_type='shop_order' and tn.post_date>'".$from->format("Y-m-d")."' |
||
758 | and tn.post_date<'".$to->format("Y-m-d")."' order by tn.post_date desc"; |
||
759 | $results = $wpdb->get_results($query); |
||
760 | |||
761 | if (isset($results) && ($privateKey == $secretKey || $privateKey4x == $secretKey)) { |
||
762 | foreach ($results as $result) { |
||
763 | $key = $result->ID; |
||
764 | $response['message'][$key]['timestamp'] = $result->post_date; |
||
765 | $response['message'][$key]['order_id'] = $key; |
||
766 | $response['message'][$key][$result->meta_key] = $result->meta_value; |
||
767 | } |
||
768 | } else { |
||
769 | $response['result'] = 'Error'; |
||
770 | } |
||
771 | $response = json_encode($response); |
||
772 | header("HTTP/1.1 200", true, 200); |
||
773 | header('Content-Type: application/json', true); |
||
774 | header('Content-Length: '.strlen($response)); |
||
775 | echo($response); |
||
776 | exit(); |
||
777 | } |
||
778 | |||
779 | /** |
||
780 | * ENDPOINT - Read logs -> Hook: rest_api_init |
||
781 | * @return mixed |
||
782 | */ |
||
783 | public function pagantisRegisterEndpoint() |
||
784 | { |
||
785 | register_rest_route( |
||
786 | 'pagantis/v1', |
||
787 | '/logs/(?P<secret>\w+)/(?P<from>\d+)/(?P<to>\d+)', |
||
788 | array( |
||
789 | 'methods' => 'GET', |
||
790 | 'callback' => array( |
||
791 | $this, |
||
792 | 'readLogs'), |
||
793 | 'permission_callback' => '__return_true', |
||
794 | ), |
||
795 | true |
||
796 | ); |
||
797 | |||
798 | register_rest_route( |
||
799 | 'pagantis/v1', |
||
800 | '/configController/(?P<secret>\w+)', |
||
801 | array( |
||
802 | 'methods' => 'GET, POST', |
||
803 | 'callback' => array( |
||
804 | $this, |
||
805 | 'updateExtraConfig'), |
||
806 | 'permission_callback' => '__return_true', |
||
807 | ), |
||
808 | true |
||
809 | ); |
||
810 | |||
811 | register_rest_route( |
||
812 | 'pagantis/v1', |
||
813 | '/api/(?P<secret>\w+)/(?P<from>\w+)/(?P<to>\w+)', |
||
814 | array( |
||
815 | 'methods' => 'GET', |
||
816 | 'callback' => array( |
||
817 | $this, |
||
818 | 'readApi'), |
||
819 | 'permission_callback' => '__return_true', |
||
820 | ), |
||
821 | true |
||
822 | ); |
||
823 | } |
||
824 | |||
825 | |||
826 | |||
827 | /** |
||
828 | * @param $css_quantity_selector |
||
829 | * |
||
830 | * @return mixed|string |
||
831 | */ |
||
832 | private function prepareQuantitySelector($css_quantity_selector) |
||
841 | } |
||
842 | |||
843 | /** |
||
844 | * @param $css_price_selector |
||
845 | * |
||
846 | * @return mixed|string |
||
847 | */ |
||
848 | private function preparePriceSelector($css_price_selector) |
||
857 | } |
||
858 | |||
859 | /** |
||
860 | * @param $product_id |
||
861 | * |
||
862 | * @return string |
||
863 | */ |
||
864 | private function isPromoted($product_id) |
||
865 | { |
||
866 | $metaProduct = get_post_meta($product_id); |
||
867 | return (array_key_exists('custom_product_pagantis_promoted', $metaProduct) && |
||
868 | $metaProduct['custom_product_pagantis_promoted']['0'] === 'yes') ? 'true' : 'false'; |
||
869 | } |
||
870 | |||
871 | /** |
||
872 | * @see wp_enqueue_style |
||
873 | */ |
||
874 | private static function enqueueSimulatorCss() |
||
878 | } |
||
879 | } |
||
880 | |||
881 | /** |
||
882 | * Add widget Js |
||
883 | **/ |
||
890 |