Total Complexity | 42 |
Total Lines | 332 |
Duplicated Lines | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
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 |
||
17 | class WcPagantis |
||
18 | { |
||
19 | const GIT_HUB_URL = 'https://github.com/pagantis/woocommerce'; |
||
20 | const PAGANTIS_DOC_URL = 'https://developer.pagamastarde.com'; |
||
21 | const SUPPORT_EML = 'mailto:[email protected]?Subject=woocommerce_plugin'; |
||
22 | /** Concurrency tablename */ |
||
23 | const LOGS_TABLE = 'pagantis_logs'; |
||
24 | /** Config tablename */ |
||
25 | const CONFIG_TABLE = 'pagantis_config'; |
||
26 | |||
27 | public $defaultConfigs = array('PAGANTIS_TITLE'=>'Instant Financing', |
||
28 | 'PAGANTIS_SIMULATOR_DISPLAY_TYPE'=>'pmtSDK.simulator.types.SIMPLE', |
||
29 | 'PAGANTIS_SIMULATOR_DISPLAY_SKIN'=>'pmtSDK.simulator.skins.BLUE', |
||
30 | 'PAGANTIS_SIMULATOR_DISPLAY_POSITION'=>'hookDisplayProductButtons', |
||
31 | 'PAGANTIS_SIMULATOR_START_INSTALLMENTS'=>3, |
||
32 | 'PAGANTIS_SIMULATOR_MAX_INSTALLMENTS'=>12, |
||
33 | 'PAGANTIS_SIMULATOR_CSS_POSITION_SELECTOR'=>'default', |
||
34 | 'PAGANTIS_SIMULATOR_DISPLAY_CSS_POSITION'=>'pmtSDK.simulator.positions.INNER', |
||
35 | 'PAGANTIS_SIMULATOR_CSS_PRICE_SELECTOR'=>'default', |
||
36 | 'PAGANTIS_SIMULATOR_CSS_QUANTITY_SELECTOR'=>'default', |
||
37 | 'PAGANTIS_FORM_DISPLAY_TYPE'=>0, |
||
38 | 'PAGANTIS_DISPLAY_MIN_AMOUNT'=>1, |
||
39 | 'PAGANTIS_URL_OK'=>'', |
||
40 | 'PAGANTIS_URL_KO'=>'', |
||
41 | 'PAGANTIS_TITLE_EXTRA' => 'Pay up to 12 comfortable installments with Pagantis. Completely online and sympathetic request, and the answer is immediate!' |
||
42 | ); |
||
43 | |||
44 | /** |
||
45 | * WC_Pagantis constructor. |
||
46 | */ |
||
47 | public function __construct() |
||
48 | { |
||
49 | require_once(plugin_dir_path(__FILE__).'/vendor/autoload.php'); |
||
|
|||
50 | |||
51 | $this->template_path = plugin_dir_path(__FILE__).'/templates/'; |
||
52 | |||
53 | $this->pagantisActivation(); |
||
54 | |||
55 | load_plugin_textdomain('pagantis', false, basename(dirname(__FILE__)).'/languages'); |
||
56 | add_filter('woocommerce_payment_gateways', array($this, 'addPagantisGateway')); |
||
57 | add_filter('woocommerce_available_payment_gateways', array($this, 'pagantisFilterGateways'), 9999); |
||
58 | add_filter('plugin_row_meta', array($this, 'pagantisRowMeta'), 10, 2); |
||
59 | add_filter('plugin_action_links_'.plugin_basename(__FILE__), array($this, 'pagantisActionLinks')); |
||
60 | add_action('woocommerce_after_add_to_cart_form', array($this, 'pagantisAddProductSimulator')); |
||
61 | add_action('wp_enqueue_scripts', 'add_widget_js'); |
||
62 | add_action('rest_api_init', array($this, 'pagantisRegisterEndpoint')); //Endpoint |
||
63 | add_filter('load_textdomain_mofile', array($this, 'loadPagantisTranslation'), 10, 2); |
||
64 | } |
||
65 | |||
66 | /* |
||
67 | * Replace 'textdomain' with your plugin's textdomain. e.g. 'woocommerce'. |
||
68 | * File to be named, for example, yourtranslationfile-en_GB.mo |
||
69 | * File to be placed, for example, wp-content/lanaguages/textdomain/yourtranslationfile-en_GB.mo |
||
70 | */ |
||
71 | public function loadPagantisTranslation($mofile, $domain) |
||
72 | { |
||
73 | if ('pagantis' === $domain) { |
||
74 | $mofile = WP_LANG_DIR . '/../plugins/pagantis/languages/pagantis-' . get_locale() . '.mo'; |
||
75 | } |
||
76 | return $mofile; |
||
77 | } |
||
78 | |||
79 | /** |
||
80 | * Sql table |
||
81 | */ |
||
82 | public function pagantisActivation() |
||
83 | { |
||
84 | global $wpdb; |
||
85 | $tableName = $wpdb->prefix.self::CONFIG_TABLE; |
||
86 | |||
87 | //Check if table exists |
||
88 | $tableExists = $wpdb->get_var("SHOW TABLES LIKE '$tableName'") != $tableName; |
||
89 | if ($tableExists) { |
||
90 | $charset_collate = $wpdb->get_charset_collate(); |
||
91 | $sql = "CREATE TABLE IF NOT EXISTS $tableName ( |
||
92 | id int NOT NULL AUTO_INCREMENT, |
||
93 | config varchar(60) NOT NULL, |
||
94 | value varchar(100) NOT NULL, |
||
95 | UNIQUE KEY id(id)) $charset_collate"; |
||
96 | |||
97 | require_once(ABSPATH.'wp-admin/includes/upgrade.php'); |
||
98 | dbDelta($sql); |
||
99 | } |
||
100 | |||
101 | $dbConfigs = $wpdb->get_results("select * from $tableName", ARRAY_A); |
||
102 | |||
103 | // Convert a multimple dimension array for SQL insert statements into a simple key/value |
||
104 | $simpleDbConfigs = array(); |
||
105 | foreach ($dbConfigs as $config) { |
||
106 | $simpleDbConfigs[$config['config']] = $config['value']; |
||
107 | } |
||
108 | $newConfigs = array_diff_key($this->defaultConfigs, $simpleDbConfigs); |
||
109 | if (!empty($newConfigs)) { |
||
110 | foreach ($newConfigs as $key => $value) { |
||
111 | $wpdb->insert($tableName, array('config' => $key, 'value' => $value), array('%s', '%s')); |
||
112 | } |
||
113 | } |
||
114 | |||
115 | foreach (array_merge($this->defaultConfigs, $simpleDbConfigs) as $key => $value) { |
||
116 | putenv($key . '=' . $value); |
||
117 | } |
||
118 | |||
119 | //Current plugin config: pagantis_public_key => New field --- public_key => Old field |
||
120 | $settings = get_option('woocommerce_pagantis_settings'); |
||
121 | |||
122 | if (!isset($settings['pagantis_public_key']) && $settings['public_key']) { |
||
123 | $settings['pagantis_public_key'] = $settings['public_key']; |
||
124 | unset($settings['public_key']); |
||
125 | } |
||
126 | |||
127 | if (!isset($settings['pagantis_private_key']) && $settings['secret_key']) { |
||
128 | $settings['pagantis_private_key'] = $settings['secret_key']; |
||
129 | unset($settings['secret_key']); |
||
130 | } |
||
131 | |||
132 | update_option('woocommerce_pagantis_settings', $settings); |
||
133 | } |
||
134 | |||
135 | /** |
||
136 | * Product simulator |
||
137 | */ |
||
138 | public function pagantisAddProductSimulator() |
||
139 | { |
||
140 | global $product; |
||
141 | |||
142 | $cfg = get_option('woocommerce_pagantis_settings'); |
||
143 | if ($cfg['enabled'] !== 'yes' || $cfg['pagantis_public_key'] == '' || $cfg['pagantis_private_key'] == '' || |
||
144 | $cfg['simulator'] !== 'yes') { |
||
145 | return; |
||
146 | } |
||
147 | |||
148 | $template_fields = array( |
||
149 | 'total' => is_numeric($product->price) ? $product->price : 0, |
||
150 | 'public_key' => $cfg['pagantis_public_key'], |
||
151 | 'simulator_type' => getenv('PAGANTIS_SIMULATOR_DISPLAY_TYPE'), |
||
152 | 'positionSelector' => getenv('PAGANTIS_SIMULATOR_CSS_POSITION_SELECTOR'), |
||
153 | 'quantitySelector' => getenv('PAGANTIS_SIMULATOR_CSS_QUANTITY_SELECTOR'), |
||
154 | 'priceSelector' => getenv('PAGANTIS_SIMULATOR_CSS_PRICE_SELECTOR'), |
||
155 | 'totalAmount' => is_numeric($product->price) ? $product->price : 0 |
||
156 | ); |
||
157 | wc_get_template('product_simulator.php', $template_fields, '', $this->template_path); |
||
158 | } |
||
159 | |||
160 | /** |
||
161 | * Add Pagantis to payments list. |
||
162 | * |
||
163 | * @param $methods |
||
164 | * |
||
165 | * @return array |
||
166 | */ |
||
167 | public function addPagantisGateway($methods) |
||
168 | { |
||
169 | if (! class_exists('WC_Payment_Gateway')) { |
||
170 | return $methods; |
||
171 | } |
||
172 | |||
173 | include_once('controllers/paymentController.php'); |
||
174 | $methods[] = 'WcPagantisGateway'; |
||
175 | |||
176 | return $methods; |
||
177 | } |
||
178 | |||
179 | /** |
||
180 | * Initialize WC_Pagantis class |
||
181 | * |
||
182 | * @param $methods |
||
183 | * |
||
184 | * @return mixed |
||
185 | */ |
||
186 | public function pagantisFilterGateways($methods) |
||
187 | { |
||
188 | global $woocommerce; |
||
189 | $pagantis = new WcPagantisGateway(); |
||
190 | |||
191 | return $methods; |
||
192 | } |
||
193 | |||
194 | /** |
||
195 | * Add links to Plugin description |
||
196 | * |
||
197 | * @param $links |
||
198 | * |
||
199 | * @return mixed |
||
200 | */ |
||
201 | public function pagantisActionLinks($links) |
||
210 | } |
||
211 | |||
212 | /** |
||
213 | * Add links to Plugin options |
||
214 | * |
||
215 | * @param $links |
||
216 | * @param $file |
||
217 | * |
||
218 | * @return array |
||
219 | */ |
||
220 | public function pagantisRowMeta($links, $file) |
||
232 | } |
||
233 | |||
234 | /** |
||
235 | * Read logs |
||
236 | */ |
||
237 | public function readLogs($data) |
||
238 | { |
||
239 | global $wpdb; |
||
240 | $filters = ($data->get_params()); |
||
241 | $response = array(); |
||
242 | $secretKey = $filters['secret']; |
||
243 | $from = $filters['from']; |
||
244 | $to = $filters['to']; |
||
245 | $cfg = get_option('woocommerce_pagantis_settings'); |
||
246 | $privateKey = isset($cfg['secret_key']) ? $cfg['secret_key'] : null; |
||
247 | $tableName = $wpdb->prefix.self::LOGS_TABLE; |
||
248 | $query = "select * from $tableName where createdAt>$from and createdAt<$to order by createdAt desc"; |
||
249 | $results = $wpdb->get_results($query); |
||
250 | if (isset($results) && $privateKey == $secretKey) { |
||
251 | foreach ($results as $key => $result) { |
||
252 | $response[$key]['timestamp'] = $result->createdAt; |
||
253 | $response[$key]['log'] = json_decode($result->log); |
||
254 | } |
||
255 | } else { |
||
256 | $response['result'] = 'Error'; |
||
257 | } |
||
258 | $response = json_encode($response); |
||
259 | header("HTTP/1.1 200", true, 200); |
||
260 | header('Content-Type: application/json', true); |
||
261 | header('Content-Length: '.strlen($response)); |
||
262 | echo($response); |
||
263 | exit(); |
||
264 | } |
||
265 | |||
266 | /** |
||
267 | * Update extra config |
||
268 | */ |
||
269 | public function updateExtraConfig($data) |
||
319 | } |
||
320 | |||
321 | /** |
||
322 | * ENDPOINT - Read logs -> Hook: rest_api_init |
||
323 | * @return mixed |
||
324 | */ |
||
325 | public function pagantisRegisterEndpoint() |
||
349 | ); |
||
350 | } |
||
351 | } |
||
352 | |||
353 | /** |
||
362 |