Total Complexity | 130 |
Total Lines | 815 |
Duplicated Lines | 0 % |
Changes | 41 | ||
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_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) |
||
181 | { |
||
182 | if ('pagantis' === $domain) { |
||
183 | $mofile = WP_LANG_DIR . '/../plugins/pagantis/languages/pagantis-' . get_locale() . '.mo'; |
||
184 | } |
||
185 | return $mofile; |
||
186 | } |
||
187 | |||
188 | /** |
||
189 | * Sql table |
||
190 | */ |
||
191 | public function pagantisActivation() |
||
369 | } |
||
370 | |||
371 | /** |
||
372 | * Checks the WC settings to know if we should modify our config |
||
373 | */ |
||
374 | public function checkWcPriceSettings() |
||
375 | { |
||
376 | if (!is_product() || !is_shop()) { |
||
377 | return; |
||
378 | } |
||
379 | $this->checkWcDecimalSeparatorSettings(); |
||
380 | $this->checkWcThousandsSeparatorSettings(); |
||
381 | } |
||
382 | |||
383 | /** |
||
384 | * Check woocommerce_price_thousand_sep and update our config if necessary |
||
385 | */ |
||
386 | private function checkWcThousandsSeparatorSettings() |
||
387 | { |
||
388 | if (areThousandsSeparatorEqual()) { |
||
389 | return; |
||
390 | } |
||
391 | if (!areThousandsSeparatorEqual()) { |
||
392 | updateThousandsSeparatorDbConfig(); |
||
393 | } |
||
394 | } |
||
395 | |||
396 | /** |
||
397 | * Check woocommerce_price_decimal_sep and update our config if necessary |
||
398 | */ |
||
399 | private function checkWcDecimalSeparatorSettings() |
||
400 | { |
||
401 | if (areDecimalSeparatorEqual()) { |
||
402 | return; |
||
403 | } |
||
404 | |||
405 | if (!areDecimalSeparatorEqual()) { |
||
406 | updateDecimalSeparatorDbConfig(); |
||
407 | } |
||
408 | } |
||
409 | |||
410 | /** |
||
411 | * Pushes the simulator div depending on the config and plugin settings |
||
412 | * |
||
413 | * @param $template_name |
||
414 | * |
||
415 | * @return bool|mixed|void |
||
416 | * @hooked woocommerce_after_template_part - 10 |
||
417 | * @see wc_get_template |
||
418 | */ |
||
419 | public function pagantisAddSimulatorHtmlDiv($template_name) |
||
420 | { |
||
421 | $areSimulatorTypesValid = isSimulatorTypeValid( |
||
422 | getConfigValue('PAGANTIS_SIMULATOR_DISPLAY_TYPE'), |
||
423 | array('sdk.simulator.types.SELECTABLE_TEXT_CUSTOM', |
||
424 | 'sdk.simulator.types.PRODUCT_PAGE') |
||
425 | ); |
||
426 | $isPriceTplPresent = isTemplatePresent($template_name, array('single-product/price.php')); |
||
427 | $isAtcTplPresent = isTemplatePresent( |
||
428 | $template_name, |
||
429 | array('single-product/add-to-cart/variation-add-to-cart-button.php', |
||
430 | 'single-product/add-to-cart/variation.php','single-product/add-to-cart/simple.php') |
||
431 | ); |
||
432 | |||
433 | $html = apply_filters('pagantis_simulator_selector_html', '<div class="mainPagantisSimulator"></div><div class="pagantisSimulator"></div>'); |
||
434 | |||
435 | |||
436 | $pagantisSimulator = 'enabled'; |
||
437 | if (!isPluginEnabled() || !areMerchantKeysSet() || !isSimulatorEnabled() || !isCountryShopContextValid() || !isProductAmountValid()) { |
||
438 | $pagantisSimulator = 'disabled'; |
||
439 | } |
||
440 | |||
441 | $pagantisSimulator4x = 'enabled'; |
||
442 | if (!isPluginEnabled4x() || !areMerchantKeysSet4x() || !isCountryShopContextValid() || !isProductAmountValid4x()) { |
||
443 | $pagantisSimulator4x = 'disabled'; |
||
444 | } |
||
445 | if ($pagantisSimulator === 'disabled' && $pagantisSimulator4x === 'disabled') { |
||
446 | return; |
||
447 | } |
||
448 | |||
449 | if (($areSimulatorTypesValid && $isPriceTplPresent) || (!$areSimulatorTypesValid && $isAtcTplPresent)) { |
||
450 | self::enqueueSimulatorCss(); |
||
451 | echo $html; |
||
452 | } |
||
453 | } |
||
454 | |||
455 | |||
456 | /** |
||
457 | * Init code required to update price for products with variations |
||
458 | * |
||
459 | */ |
||
460 | public function pagantisAddProductSnippetForVariations() |
||
476 | } |
||
477 | |||
478 | /** |
||
479 | * @return string|void |
||
480 | * |
||
481 | */ |
||
482 | public function pagantisInitProductSimulator() |
||
483 | { |
||
484 | global $product; |
||
485 | |||
486 | //12x |
||
487 | $pagantisSimulator = 'enabled'; |
||
488 | |||
489 | wp_register_script('pg-product-simulator', plugins_url('assets/js/pg-product-simulator.js', PG_WC_MAIN_FILE), array(), '', true); |
||
490 | $settings = get_option('woocommerce_pagantis_settings'); |
||
491 | $locale = strtolower(strstr(get_locale(), '_', true)); |
||
492 | if (!isPluginEnabled() || !areMerchantKeysSet() || !isSimulatorEnabled() || !isCountryShopContextValid() || !isProductAmountValid()) { |
||
493 | $pagantisSimulator = 'disabled'; |
||
494 | } |
||
495 | |||
496 | $pagantisSimulator4x = 'enabled'; |
||
497 | if (!isPluginEnabled4x() || !areMerchantKeysSet4x() || !isCountryShopContextValid() || !isProductAmountValid4x()) { |
||
498 | $pagantisSimulator4x = 'disabled'; |
||
499 | } |
||
500 | |||
501 | if ($pagantisSimulator === 'disabled' && $pagantisSimulator4x === 'disabled') { |
||
502 | return; |
||
503 | } |
||
504 | |||
505 | $totalPrice = $product->get_price(); |
||
506 | $formattedInstallments = number_format($totalPrice/4, 2); |
||
507 | $simulatorMessage = sprintf(__('or 4 installments of %s€, without fees, with ', 'pagantis'), $formattedInstallments); |
||
508 | $post_id = $product->get_id(); |
||
509 | $simulatorData = array( |
||
510 | 'total' => is_numeric($product->get_price()) ? $product->get_price() : 0, |
||
511 | 'public_key' => $settings['pagantis_public_key'], |
||
512 | 'simulator_type' => $this->extraConfig['PAGANTIS_SIMULATOR_DISPLAY_TYPE'], |
||
513 | 'positionSelector' => $this->extraConfig['PAGANTIS_SIMULATOR_CSS_POSITION_SELECTOR'], |
||
514 | 'positionSelector4x' => $this->extraConfig['PAGANTIS_SIMULATOR_CSS_POSITION_SELECTOR_4X'], |
||
515 | 'quantitySelector' => unserialize($this->extraConfig['PAGANTIS_SIMULATOR_CSS_QUANTITY_SELECTOR']), |
||
516 | 'priceSelector' => unserialize($this->extraConfig['PAGANTIS_SIMULATOR_CSS_PRICE_SELECTOR']), |
||
517 | 'totalAmount' => is_numeric($product->get_price()) ? $product->get_price() : 0, |
||
518 | 'locale' => $locale, |
||
519 | 'country' => $locale, |
||
520 | 'promoted' => $this->isPromoted($post_id), |
||
521 | 'promotedMessage' => $this->extraConfig['PAGANTIS_PROMOTION_EXTRA'], |
||
522 | 'thousandSeparator' => $this->extraConfig['PAGANTIS_SIMULATOR_THOUSANDS_SEPARATOR'], |
||
523 | 'decimalSeparator' => $this->extraConfig['PAGANTIS_SIMULATOR_DECIMAL_SEPARATOR'], |
||
524 | 'pagantisQuotesStart' => $this->extraConfig['PAGANTIS_SIMULATOR_START_INSTALLMENTS'], |
||
525 | 'pagantisSimulatorSkin' => $this->extraConfig['PAGANTIS_SIMULATOR_DISPLAY_SKIN'], |
||
526 | 'pagantisSimulatorPosition' => $this->extraConfig['PAGANTIS_SIMULATOR_DISPLAY_CSS_POSITION'], |
||
527 | 'finalDestination' => $this->extraConfig['PAGANTIS_SIMULATOR_DISPLAY_SITUATION'], |
||
528 | 'variationSelector' => $this->extraConfig['PAGANTIS_SIMULATOR_SELECTOR_VARIATION'], |
||
529 | 'productType' => $product->get_type(), |
||
530 | 'pagantisSimulator' => $pagantisSimulator, |
||
531 | 'pagantisSimulator4x' => $pagantisSimulator4x, |
||
532 | 'simulatorMessage' => "$simulatorMessage <img class='mainImageLogo' alt='PAGANTIS'/>" |
||
533 | ); |
||
534 | |||
535 | wp_localize_script('pg-product-simulator', 'simulatorData', $simulatorData); |
||
536 | wp_enqueue_script('pg-product-simulator'); |
||
537 | } |
||
538 | |||
539 | /** |
||
540 | * Add Pagantis to payments list. |
||
541 | * |
||
542 | * @param $methods |
||
543 | * |
||
544 | * @return array |
||
545 | */ |
||
546 | public function addPagantisGateway($methods) |
||
547 | { |
||
548 | if (! class_exists('WC_Payment_Gateway')) { |
||
549 | return $methods; |
||
550 | } |
||
551 | |||
552 | //4x |
||
553 | include_once('controllers/paymentController4x.php'); |
||
554 | $methods[] = 'WcPagantis4xGateway'; |
||
555 | |||
556 | //12x |
||
557 | include_once('controllers/paymentController.php'); |
||
558 | $methods[] = 'WcPagantisGateway'; |
||
559 | |||
560 | return $methods; |
||
561 | } |
||
562 | |||
563 | /** |
||
564 | * Initialize WC_Pagantis class |
||
565 | * |
||
566 | * @param $methods |
||
567 | * |
||
568 | * @return mixed |
||
569 | */ |
||
570 | public function pagantisFilterGateways($methods) |
||
571 | { |
||
572 | $pagantis4x = new WcPagantis4xGateway(); |
||
573 | if (!$pagantis4x->is_available()) { |
||
574 | unset($methods['pagantis4x']); |
||
575 | } |
||
576 | |||
577 | $pagantis = new WcPagantisGateway(); |
||
578 | if (!$pagantis->is_available()) { |
||
579 | unset($methods['pagantis']); |
||
580 | } |
||
581 | |||
582 | return $methods; |
||
583 | } |
||
584 | |||
585 | /** |
||
586 | * Add links to Plugin description |
||
587 | * |
||
588 | * @param $links |
||
589 | * |
||
590 | * @return mixed |
||
591 | */ |
||
592 | public function pagantisActionLinks($links) |
||
601 | } |
||
602 | |||
603 | /** |
||
604 | * Add links to Plugin options |
||
605 | * |
||
606 | * @param $links |
||
607 | * @param $file |
||
608 | * |
||
609 | * @return array |
||
610 | */ |
||
611 | public function pagantisRowMeta($links, $file) |
||
623 | } |
||
624 | |||
625 | /** |
||
626 | * Read logs |
||
627 | */ |
||
628 | public function readLogs($data) |
||
629 | { |
||
630 | global $wpdb; |
||
631 | $filters = ($data->get_params()); |
||
632 | $response = array(); |
||
633 | $secretKey = $filters['secret']; |
||
634 | $from = $filters['from']; |
||
635 | $to = $filters['to']; |
||
636 | $cfg = get_option('woocommerce_pagantis_settings'); |
||
637 | $privateKey = isset($cfg['pagantis_private_key']) ? $cfg['pagantis_private_key'] : null; |
||
638 | $privateKey4x = isset($cfg['pagantis_private_key_4x']) ? $cfg['pagantis_private_key_4x'] : null; |
||
639 | $tableName = $wpdb->prefix.PG_LOGS_TABLE_NAME; |
||
640 | $query = "select * from $tableName where createdAt>$from and createdAt<$to order by createdAt desc"; |
||
641 | $results = $wpdb->get_results($query); |
||
642 | if (isset($results) && ($privateKey == $secretKey || $privateKey4x == $secretKey)) { |
||
643 | foreach ($results as $key => $result) { |
||
644 | $response[$key]['timestamp'] = $result->createdAt; |
||
645 | $response[$key]['log'] = json_decode($result->log); |
||
646 | } |
||
647 | } else { |
||
648 | $response['result'] = 'Error'; |
||
649 | } |
||
650 | $response = json_encode($response); |
||
651 | header("HTTP/1.1 200", true, 200); |
||
652 | header('Content-Type: application/json', true); |
||
653 | header('Content-Length: '.strlen($response)); |
||
654 | echo($response); |
||
655 | exit(); |
||
656 | } |
||
657 | |||
658 | /** |
||
659 | * Update extra config |
||
660 | */ |
||
661 | public function updateExtraConfig($data) |
||
662 | { |
||
663 | global $wpdb; |
||
664 | $tableName = $wpdb->prefix.PG_CONFIG_TABLE_NAME; |
||
665 | $response = array('status'=>null); |
||
666 | |||
667 | $filters = ($data->get_params()); |
||
668 | $secretKey = $filters['secret']; |
||
669 | $cfg = get_option('woocommerce_pagantis_settings'); |
||
670 | $privateKey = isset($cfg['pagantis_private_key']) ? $cfg['pagantis_private_key'] : null; |
||
671 | $privateKey4x = isset($cfg['pagantis_private_key_4x']) ? $cfg['pagantis_private_key_4x'] : null; |
||
672 | if ($privateKey != $secretKey && $privateKey4x != $secretKey) { |
||
673 | $response['status'] = 401; |
||
674 | $response['result'] = 'Unauthorized'; |
||
675 | } elseif ($_SERVER['REQUEST_METHOD'] == 'POST') { |
||
676 | if (count($_POST)) { |
||
677 | foreach ($_POST as $config => $value) { |
||
678 | if (isset($this->defaultConfigs[$config]) && $response['status']==null) { |
||
679 | $wpdb->update( |
||
680 | $tableName, |
||
681 | array('value' => stripslashes($value)), |
||
682 | array('config' => $config), |
||
683 | array('%s'), |
||
684 | array('%s') |
||
685 | ); |
||
686 | } else { |
||
687 | $response['status'] = 400; |
||
688 | $response['result'] = 'Bad request'; |
||
689 | } |
||
690 | } |
||
691 | } else { |
||
692 | $response['status'] = 422; |
||
693 | $response['result'] = 'Empty data'; |
||
694 | } |
||
695 | } |
||
696 | |||
697 | if ($response['status']==null) { |
||
698 | $tableName = $wpdb->prefix.PG_CONFIG_TABLE_NAME; |
||
699 | $dbResult = $wpdb->get_results("select config, value from $tableName", ARRAY_A); |
||
700 | foreach ($dbResult as $value) { |
||
701 | $formattedResult[$value['config']] = $value['value']; |
||
702 | } |
||
703 | $response['result'] = $formattedResult; |
||
704 | } |
||
705 | |||
706 | $result = json_encode($response['result']); |
||
707 | header("HTTP/1.1 ".$response['status'], true, $response['status']); |
||
708 | header('Content-Type: application/json', true); |
||
709 | header('Content-Length: '.strlen($result)); |
||
710 | echo($result); |
||
711 | exit(); |
||
712 | } |
||
713 | |||
714 | /** |
||
715 | * Read logs |
||
716 | */ |
||
717 | public function readApi($data) |
||
718 | { |
||
719 | global $wpdb; |
||
720 | $filters = ($data->get_params()); |
||
721 | $response = array('timestamp'=>time()); |
||
722 | $secretKey = $filters['secret']; |
||
723 | $from = ($filters['from']) ? date_create($filters['from']) : date("Y-m-d", strtotime("-7 day")); |
||
724 | $to = ($filters['to']) ? date_create($filters['to']) : date("Y-m-d", strtotime("+1 day")); |
||
725 | $method = ($filters['method']) ? ($filters['method']) : 'Pagantis'; |
||
726 | $cfg = get_option('woocommerce_pagantis_settings'); |
||
727 | $privateKey = isset($cfg['pagantis_private_key']) ? $cfg['pagantis_private_key'] : null; |
||
728 | $privateKey4x = isset($cfg['pagantis_private_key_4x']) ? $cfg['pagantis_private_key_4x'] : null; |
||
729 | $tableName = $wpdb->prefix.PG_ORDERS_TABLE; |
||
730 | $tableNameInner = $wpdb->prefix.'postmeta'; |
||
731 | $query = "select * from $tableName tn INNER JOIN $tableNameInner tn2 ON tn2.post_id = tn.id |
||
732 | where tn.post_type='shop_order' and tn.post_date>'".$from->format("Y-m-d")."' |
||
733 | and tn.post_date<'".$to->format("Y-m-d")."' order by tn.post_date desc"; |
||
734 | $results = $wpdb->get_results($query); |
||
735 | |||
736 | if (isset($results) && ($privateKey == $secretKey || $privateKey4x == $secretKey)) { |
||
737 | foreach ($results as $result) { |
||
738 | $key = $result->ID; |
||
739 | $response['message'][$key]['timestamp'] = $result->post_date; |
||
740 | $response['message'][$key]['order_id'] = $key; |
||
741 | $response['message'][$key][$result->meta_key] = $result->meta_value; |
||
742 | } |
||
743 | } else { |
||
744 | $response['result'] = 'Error'; |
||
745 | } |
||
746 | $response = json_encode($response); |
||
747 | header("HTTP/1.1 200", true, 200); |
||
748 | header('Content-Type: application/json', true); |
||
749 | header('Content-Length: '.strlen($response)); |
||
750 | echo($response); |
||
751 | exit(); |
||
752 | } |
||
753 | |||
754 | /** |
||
755 | * ENDPOINT - Read logs -> Hook: rest_api_init |
||
756 | * @return mixed |
||
757 | */ |
||
758 | public function pagantisRegisterEndpoint() |
||
759 | { |
||
760 | register_rest_route( |
||
761 | 'pagantis/v1', |
||
762 | '/logs/(?P<secret>\w+)/(?P<from>\d+)/(?P<to>\d+)', |
||
763 | array( |
||
764 | 'methods' => 'GET', |
||
765 | 'callback' => array( |
||
766 | $this, |
||
767 | 'readLogs'), |
||
768 | 'permission_callback' => '__return_true', |
||
769 | ), |
||
770 | true |
||
771 | ); |
||
772 | |||
773 | register_rest_route( |
||
774 | 'pagantis/v1', |
||
775 | '/configController/(?P<secret>\w+)', |
||
776 | array( |
||
777 | 'methods' => 'GET, POST', |
||
778 | 'callback' => array( |
||
779 | $this, |
||
780 | 'updateExtraConfig'), |
||
781 | 'permission_callback' => '__return_true', |
||
782 | ), |
||
783 | true |
||
784 | ); |
||
785 | |||
786 | register_rest_route( |
||
787 | 'pagantis/v1', |
||
788 | '/api/(?P<secret>\w+)/(?P<from>\w+)/(?P<to>\w+)', |
||
789 | array( |
||
790 | 'methods' => 'GET', |
||
791 | 'callback' => array( |
||
792 | $this, |
||
793 | 'readApi'), |
||
794 | 'permission_callback' => '__return_true', |
||
795 | ), |
||
796 | true |
||
797 | ); |
||
798 | } |
||
799 | |||
800 | |||
801 | |||
802 | /** |
||
803 | * @param $css_quantity_selector |
||
804 | * |
||
805 | * @return mixed|string |
||
806 | */ |
||
807 | private function prepareQuantitySelector($css_quantity_selector) |
||
816 | } |
||
817 | |||
818 | /** |
||
819 | * @param $css_price_selector |
||
820 | * |
||
821 | * @return mixed|string |
||
822 | */ |
||
823 | private function preparePriceSelector($css_price_selector) |
||
832 | } |
||
833 | |||
834 | /** |
||
835 | * @param $product_id |
||
836 | * |
||
837 | * @return string |
||
838 | */ |
||
839 | private function isPromoted($product_id) |
||
840 | { |
||
841 | $metaProduct = get_post_meta($product_id); |
||
842 | return (array_key_exists('custom_product_pagantis_promoted', $metaProduct) && |
||
843 | $metaProduct['custom_product_pagantis_promoted']['0'] === 'yes') ? 'true' : 'false'; |
||
844 | } |
||
845 | |||
846 | /** |
||
847 | * @see wp_enqueue_style |
||
848 | */ |
||
849 | private static function enqueueSimulatorCss() |
||
853 | } |
||
854 | } |
||
855 | |||
865 |