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