Total Complexity | 40 |
Total Lines | 319 |
Duplicated Lines | 0 % |
Changes | 7 | ||
Bugs | 0 | Features | 0 |
Complex classes like WcPaylater 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 WcPaylater, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
17 | class WcPaylater |
||
18 | { |
||
19 | const GIT_HUB_URL = 'https://github.com/PagaMasTarde/woocommerce'; |
||
20 | const PMT_DOC_URL = 'https://docs.pagamastarde.com'; |
||
21 | const SUPPORT_EML = 'mailto:[email protected]?Subject=woocommerce_plugin'; |
||
22 | /** Concurrency tablename */ |
||
23 | const LOGS_TABLE = 'pmt_logs'; |
||
24 | /** Config tablename */ |
||
25 | const CONFIG_TABLE = 'pmt_config'; |
||
26 | |||
27 | public $defaultConfigs = array('PMT_TITLE'=>'Instant Financing', |
||
28 | 'PMT_SIMULATOR_DISPLAY_TYPE'=>'pmtSDK.simulator.types.SIMPLE', |
||
29 | 'PMT_SIMULATOR_DISPLAY_SKIN'=>'pmtSDK.simulator.skins.BLUE', |
||
30 | 'PMT_SIMULATOR_DISPLAY_POSITION'=>'hookDisplayProductButtons', |
||
31 | 'PMT_SIMULATOR_START_INSTALLMENTS'=>3, |
||
32 | 'PMT_SIMULATOR_MAX_INSTALLMENTS'=>12, |
||
33 | 'PMT_SIMULATOR_CSS_POSITION_SELECTOR'=>'default', |
||
34 | 'PMT_SIMULATOR_DISPLAY_CSS_POSITION'=>'pmtSDK.simulator.positions.INNER', |
||
35 | 'PMT_SIMULATOR_CSS_PRICE_SELECTOR'=>'default', |
||
36 | 'PMT_SIMULATOR_CSS_QUANTITY_SELECTOR'=>'default', |
||
37 | 'PMT_FORM_DISPLAY_TYPE'=>0, |
||
38 | 'PMT_DISPLAY_MIN_AMOUNT'=>1, |
||
39 | 'PMT_URL_OK'=>'', |
||
40 | 'PMT_URL_KO'=>'', |
||
41 | 'PMT_TITLE_EXTRA' => 'Paga hasta en 12 cómodas cuotas con Paga+Tarde. Solicitud totalmente |
||
42 | online y sin papeleos,¡y la respuesta es inmediata!' |
||
43 | ); |
||
44 | |||
45 | /** |
||
46 | * WC_Paylater constructor. |
||
47 | */ |
||
48 | public function __construct() |
||
49 | { |
||
50 | require_once(plugin_dir_path(__FILE__).'/vendor/autoload.php'); |
||
|
|||
51 | |||
52 | $this->template_path = plugin_dir_path(__FILE__).'/templates/'; |
||
53 | |||
54 | $this->paylaterActivation(); |
||
55 | |||
56 | load_plugin_textdomain('paylater', false, basename(dirname(__FILE__)).'/languages'); |
||
57 | add_filter('woocommerce_payment_gateways', array($this, 'addPaylaterGateway')); |
||
58 | add_filter('woocommerce_available_payment_gateways', array($this, 'paylaterFilterGateways'), 9999); |
||
59 | add_filter('plugin_row_meta', array($this, 'paylaterRowMeta'), 10, 2); |
||
60 | add_filter('plugin_action_links_'.plugin_basename(__FILE__), array($this, 'paylaterActionLinks')); |
||
61 | add_action('woocommerce_after_add_to_cart_form', array($this, 'paylaterAddProductSimulator')); |
||
62 | add_action('wp_enqueue_scripts', 'add_widget_js'); |
||
63 | add_action('rest_api_init', array($this, 'paylaterRegisterEndpoint')); //Endpoint |
||
64 | } |
||
65 | |||
66 | /** |
||
67 | * Sql table |
||
68 | */ |
||
69 | public function paylaterActivation() |
||
70 | { |
||
71 | global $wpdb; |
||
72 | $tableName = $wpdb->prefix.self::CONFIG_TABLE; |
||
73 | |||
74 | //Check if table exists |
||
75 | $tableExists = $wpdb->get_var("SHOW TABLES LIKE '$tableName'") != $tableName; |
||
76 | if ($tableExists) { |
||
77 | $charset_collate = $wpdb->get_charset_collate(); |
||
78 | $sql = "CREATE TABLE IF NOT EXISTS $tableName ( |
||
79 | id int NOT NULL AUTO_INCREMENT, |
||
80 | config varchar(60) NOT NULL, |
||
81 | value varchar(100) NOT NULL, |
||
82 | UNIQUE KEY id(id)) $charset_collate"; |
||
83 | |||
84 | require_once(ABSPATH.'wp-admin/includes/upgrade.php'); |
||
85 | dbDelta($sql); |
||
86 | } |
||
87 | |||
88 | $dbConfigs = $wpdb->get_results("select * from $tableName", ARRAY_A); |
||
89 | |||
90 | // Convert a multimple dimension array for SQL insert statements into a simple key/value |
||
91 | $simpleDbConfigs = array(); |
||
92 | foreach ($dbConfigs as $config) { |
||
93 | $simpleDbConfigs[$config['config']] = $config['value']; |
||
94 | } |
||
95 | $newConfigs = array_diff_key($this->defaultConfigs, $simpleDbConfigs); |
||
96 | if (!empty($newConfigs)) { |
||
97 | foreach ($newConfigs as $key => $value) { |
||
98 | $wpdb->insert($tableName, array('config' => $key, 'value' => $value), array('%s', '%s')); |
||
99 | } |
||
100 | } |
||
101 | |||
102 | foreach (array_merge($this->defaultConfigs, $simpleDbConfigs) as $key => $value) { |
||
103 | putenv($key . '=' . $value); |
||
104 | } |
||
105 | |||
106 | //Current plugin config: pmt_public_key => New field --- public_key => Old field |
||
107 | $settings = get_option('woocommerce_paylater_settings'); |
||
108 | |||
109 | if (!isset($settings['pmt_public_key']) && $settings['public_key']) { |
||
110 | $settings['pmt_public_key'] = $settings['public_key']; |
||
111 | unset($settings['public_key']); |
||
112 | } |
||
113 | |||
114 | if (!isset($settings['pmt_private_key']) && $settings['secret_key']) { |
||
115 | $settings['pmt_private_key'] = $settings['secret_key']; |
||
116 | unset($settings['secret_key']); |
||
117 | } |
||
118 | |||
119 | update_option('woocommerce_paylater_settings', $settings); |
||
120 | } |
||
121 | |||
122 | /** |
||
123 | * Product simulator |
||
124 | */ |
||
125 | public function paylaterAddProductSimulator() |
||
126 | { |
||
127 | global $product; |
||
128 | |||
129 | $cfg = get_option('woocommerce_paylater_settings'); |
||
130 | if ($cfg['enabled'] !== 'yes' || $cfg['pmt_public_key'] == '' || $cfg['pmt_private_key'] == '' || |
||
131 | $cfg['simulator'] !== 'yes') { |
||
132 | return; |
||
133 | } |
||
134 | |||
135 | $template_fields = array( |
||
136 | 'total' => is_numeric($product->price) ? $product->price : 0, |
||
137 | 'public_key' => $cfg['pmt_public_key'], |
||
138 | 'simulator_type' => getenv('PMT_SIMULATOR_DISPLAY_TYPE'), |
||
139 | 'positionSelector' => getenv('PMT_SIMULATOR_CSS_POSITION_SELECTOR'), |
||
140 | 'quantitySelector' => getenv('PMT_SIMULATOR_CSS_QUANTITY_SELECTOR'), |
||
141 | 'priceSelector' => getenv('PMT_SIMULATOR_CSS_PRICE_SELECTOR'), |
||
142 | 'totalAmount' => is_numeric($product->price) ? $product->price : 0 |
||
143 | ); |
||
144 | wc_get_template('product_simulator.php', $template_fields, '', $this->template_path); |
||
145 | } |
||
146 | |||
147 | /** |
||
148 | * Add Paylater to payments list. |
||
149 | * |
||
150 | * @param $methods |
||
151 | * |
||
152 | * @return array |
||
153 | */ |
||
154 | public function addPaylaterGateway($methods) |
||
155 | { |
||
156 | if (! class_exists('WC_Payment_Gateway')) { |
||
157 | return $methods; |
||
158 | } |
||
159 | |||
160 | include_once('controllers/paymentController.php'); |
||
161 | $methods[] = 'WcPaylaterGateway'; |
||
162 | |||
163 | return $methods; |
||
164 | } |
||
165 | |||
166 | /** |
||
167 | * Initialize WC_Paylater class |
||
168 | * |
||
169 | * @param $methods |
||
170 | * |
||
171 | * @return mixed |
||
172 | */ |
||
173 | public function paylaterFilterGateways($methods) |
||
179 | } |
||
180 | |||
181 | /** |
||
182 | * Add links to Plugin description |
||
183 | * |
||
184 | * @param $links |
||
185 | * |
||
186 | * @return mixed |
||
187 | */ |
||
188 | public function paylaterActionLinks($links) |
||
189 | { |
||
190 | $params_array = array('page' => 'wc-settings', 'tab' => 'checkout', 'section' => 'paylater'); |
||
191 | $setting_url = esc_url(add_query_arg($params_array, admin_url('admin.php?'))); |
||
192 | $setting_link = '<a href="'.$setting_url.'">'.__('Settings', 'paylater').'</a>'; |
||
193 | |||
194 | array_unshift($links, $setting_link); |
||
195 | |||
196 | return $links; |
||
197 | } |
||
198 | |||
199 | /** |
||
200 | * Add links to Plugin options |
||
201 | * |
||
202 | * @param $links |
||
203 | * @param $file |
||
204 | * |
||
205 | * @return array |
||
206 | */ |
||
207 | public function paylaterRowMeta($links, $file) |
||
208 | { |
||
209 | if ($file == plugin_basename(__FILE__)) { |
||
210 | $links[] = '<a href="'.WcPaylater::GIT_HUB_URL.'" target="_blank">'.__('Documentation', 'paylater').'</a>'; |
||
211 | $links[] = '<a href="'.WcPaylater::PMT_DOC_URL.'" target="_blank">'. |
||
212 | __('API documentation', 'paylater').'</a>'; |
||
213 | $links[] = '<a href="'.WcPaylater::SUPPORT_EML.'">'.__('Support', 'paylater').'</a>'; |
||
214 | |||
215 | return $links; |
||
216 | } |
||
217 | |||
218 | return $links; |
||
219 | } |
||
220 | |||
221 | /** |
||
222 | * Read logs |
||
223 | */ |
||
224 | public function readLogs($data) |
||
225 | { |
||
226 | global $wpdb; |
||
227 | $filters = ($data->get_params()); |
||
228 | $response = array(); |
||
229 | $secretKey = $filters['secret']; |
||
230 | $from = $filters['from']; |
||
231 | $to = $filters['to']; |
||
232 | $cfg = get_option('woocommerce_paylater_settings'); |
||
233 | $privateKey = isset($cfg['secret_key']) ? $cfg['secret_key'] : null; |
||
234 | $tableName = $wpdb->prefix.self::LOGS_TABLE; |
||
235 | $query = "select * from $tableName where createdAt>$from and createdAt<$to order by createdAt desc"; |
||
236 | $results = $wpdb->get_results($query); |
||
237 | if (isset($results) && $privateKey == $secretKey) { |
||
238 | foreach ($results as $key => $result) { |
||
239 | $response[$key]['timestamp'] = $result->createdAt; |
||
240 | $response[$key]['log'] = json_decode($result->log); |
||
241 | } |
||
242 | } else { |
||
243 | $response['result'] = 'Error'; |
||
244 | } |
||
245 | $response = json_encode($response); |
||
246 | header("HTTP/1.1 200", true, 200); |
||
247 | header('Content-Type: application/json', true); |
||
248 | header('Content-Length: '.strlen($response)); |
||
249 | echo($response); |
||
250 | exit(); |
||
251 | } |
||
252 | |||
253 | /** |
||
254 | * Update extra config |
||
255 | */ |
||
256 | public function updateExtraConfig($data) |
||
306 | } |
||
307 | |||
308 | /** |
||
309 | * ENDPOINT - Read logs -> Hook: rest_api_init |
||
310 | * @return mixed |
||
311 | */ |
||
312 | public function paylaterRegisterEndpoint() |
||
336 | ); |
||
337 | } |
||
338 | } |
||
339 | |||
340 | /** |
||
341 | * Add widget Js |
||
342 | **/ |
||
349 |