Total Complexity | 96 |
Total Lines | 739 |
Duplicated Lines | 0 % |
Changes | 0 |
Complex classes like WC_Pagantis_Plugin 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 WC_Pagantis_Plugin, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
40 | class WC_Pagantis_Plugin |
||
41 | { |
||
42 | |||
43 | |||
44 | /** |
||
45 | * The reference the *Singleton* instance of this class. |
||
46 | * |
||
47 | * @var $instance |
||
48 | */ |
||
49 | private static $instance; |
||
50 | |||
51 | |||
52 | /** |
||
53 | * @var array $defaultConfig |
||
54 | */ |
||
55 | private $initialConfig; |
||
56 | |||
57 | /** |
||
58 | * @var array $extraConfig |
||
59 | */ |
||
60 | private $extraConfig; |
||
61 | |||
62 | |||
63 | /** |
||
64 | * WC_Pagantis constructor. |
||
65 | */ |
||
66 | public function __construct() |
||
67 | { |
||
68 | require_once(plugin_dir_path(__FILE__) . 'vendor/autoload.php'); |
||
69 | require_once dirname(__FILE__) . '/includes/class-wc-pagantis-config.php'; |
||
70 | require_once dirname(__FILE__) . '/includes/functions.php'; |
||
71 | |||
72 | $this->template_path = plugin_dir_path(__FILE__) . 'templates/'; |
||
73 | |||
74 | $this->prepare_wpdb_tables(); |
||
75 | $this->initialConfig = WC_Pagantis_Config::getDefaultConfig(); |
||
76 | |||
77 | $this->extraConfig = WC_Pagantis_Config::getExtraConfig(); |
||
78 | add_action('plugins_loaded', array($this, 'bootstrap')); |
||
79 | load_plugin_textdomain('pagantis', false, basename(dirname(__FILE__)) . '/languages'); |
||
80 | add_filter('woocommerce_payment_gateways', array($this, 'add_pagantis_gateway')); |
||
81 | add_filter('woocommerce_available_payment_gateways', array($this, 'check_if_pg_is_in_available_gateways'), |
||
82 | 9999); |
||
83 | add_filter('plugin_row_meta', array($this, 'get_plugin_row_meta_links'), 10, 2); |
||
84 | add_filter('plugin_action_links_' . plugin_basename(__FILE__), array($this, 'get_plugin_action_links')); |
||
85 | |||
86 | add_action('wp_enqueue_scripts', 'add_pagantis_widget_js'); |
||
87 | add_action('rest_api_init', array($this, 'register_pg_rest_routes')); //Endpoint |
||
88 | add_filter('load_textdomain_mofile', array($this, 'loadPagantisTranslation'), 10, 2); |
||
89 | register_activation_hook(__FILE__, array($this, 'prepare_wpdb_tables')); |
||
90 | add_action('woocommerce_product_options_general_product_data', array($this, 'pagantisPromotedProductTpl')); |
||
91 | add_action('woocommerce_process_product_meta', array($this, 'pagantisPromotedVarSave')); |
||
92 | add_action('woocommerce_product_bulk_edit_start', array($this, 'pagantis_promoted_bulk_template')); |
||
93 | add_action('woocommerce_product_bulk_edit_save', array($this, 'save_pg_promoted_bulk_template')); |
||
94 | add_action('woocommerce_after_add_to_cart_form', array($this, 'pagantisAddProductSimulator')); |
||
95 | //add_action('wp_enqueue_scripts', array($this, 'enqueue_simulator_scripts')); |
||
96 | } |
||
97 | |||
98 | /** |
||
99 | * Returns the *Singleton* instance of this class. |
||
100 | * |
||
101 | * @return self::$instance The *Singleton* instance. |
||
102 | */ |
||
103 | public static function get_instance() |
||
104 | { |
||
105 | if (null === self::$instance) { |
||
106 | self::$instance = new self(); |
||
107 | } |
||
108 | |||
109 | return self::$instance; |
||
110 | } |
||
111 | |||
112 | /** |
||
113 | * Private clone method to prevent cloning of the instance of the |
||
114 | * *Singleton* instance. |
||
115 | * |
||
116 | * @return void |
||
117 | */ |
||
118 | private function __clone() |
||
119 | { |
||
120 | } |
||
121 | |||
122 | /** |
||
123 | * Private unserialize method to prevent unserializing of the *Singleton* |
||
124 | * instance. |
||
125 | * |
||
126 | * @return void |
||
127 | */ |
||
128 | private function __wakeup() |
||
129 | { |
||
130 | } |
||
131 | |||
132 | public function bootstrap() |
||
133 | { |
||
134 | try { |
||
135 | $this->check_dependencies(); |
||
136 | } catch (Exception $e) { |
||
137 | $e->getMessage(); |
||
138 | } |
||
139 | } |
||
140 | |||
141 | /** |
||
142 | * @throws Exception |
||
143 | */ |
||
144 | public function check_dependencies() |
||
145 | { |
||
146 | if (version_compare(WC()->version, '3.0', '<')) { |
||
147 | throw new Exception(__('Pagantis requires WooCommerce version 3.0 or greater', 'pagantis')); |
||
148 | } |
||
149 | |||
150 | if ( ! function_exists('curl_init')) { |
||
151 | throw new Exception(__('Pagantis requires cURL to be installed on your server', 'pagantis')); |
||
152 | } |
||
153 | if ( ! version_compare(phpversion(), '5.3.0', '>=')) { |
||
154 | throw new Exception(__('Pagantis requires PHP 5.3 or greater to be installed on your server', 'pagantis')); |
||
155 | } |
||
156 | } |
||
157 | |||
158 | /** |
||
159 | * Piece of html code to insert into BULK admin edit |
||
160 | */ |
||
161 | public function pagantis_promoted_bulk_template() |
||
162 | { |
||
163 | echo '<div class="inline-edit-group"> |
||
164 | <label class="alignleft"> |
||
165 | <span class="title">Pagantis promoted</span> |
||
166 | <span class="input-text-wrap"> |
||
167 | <input type="checkbox" id="pagantis_promoted" name="pagantis_promoted"/> |
||
168 | </span> |
||
169 | </label> |
||
170 | </div>'; |
||
171 | } |
||
172 | |||
173 | /** |
||
174 | * Php code to save our meta after a bulk admin edit |
||
175 | * |
||
176 | * @param $product |
||
177 | */ |
||
178 | public function save_pg_promoted_bulk_template($product) |
||
179 | { |
||
180 | $post_id = $product->get_id(); |
||
181 | $pagantis_promoted_value = $_REQUEST['pagantis_promoted']; |
||
182 | if ($pagantis_promoted_value === 'on') { |
||
183 | $pagantis_promoted_value = 'yes'; |
||
184 | } else { |
||
185 | $pagantis_promoted_value = 'no'; |
||
186 | } |
||
187 | |||
188 | update_post_meta($post_id, 'custom_product_pagantis_promoted', esc_attr($pagantis_promoted_value)); |
||
189 | } |
||
190 | |||
191 | /** |
||
192 | * Piece of html code to insert into PRODUCT admin edit |
||
193 | */ |
||
194 | public function pagantisPromotedProductTpl() |
||
195 | { |
||
196 | global $post; |
||
197 | $_product = get_post_meta($post->ID); |
||
198 | woocommerce_wp_checkbox(array( |
||
199 | 'id' => 'pagantis_promoted', |
||
200 | 'label' => __('Pagantis promoted', 'woocommerce'), // phpcs:ignore WordPress.WP.I18n.TextDomainMismatch |
||
201 | 'value' => $_product['custom_product_pagantis_promoted']['0'], |
||
202 | 'cbvalue' => 'yes', |
||
203 | 'echo' => true, |
||
204 | )); |
||
205 | } |
||
206 | |||
207 | /** |
||
208 | * Php code to save our meta after a PRODUCT admin edit |
||
209 | * |
||
210 | * @param $post_id |
||
211 | */ |
||
212 | public function pagantisPromotedVarSave($post_id) |
||
213 | { |
||
214 | $pagantis_promoted_value = $_POST['pagantis_promoted']; |
||
215 | if ($pagantis_promoted_value === null) { |
||
216 | $pagantis_promoted_value = 'no'; |
||
217 | } |
||
218 | update_post_meta($post_id, 'custom_product_pagantis_promoted', esc_attr($pagantis_promoted_value)); |
||
219 | } |
||
220 | |||
221 | /* |
||
222 | * Replace 'textdomain' with your plugin's textdomain. e.g. 'woocommerce'. |
||
223 | * File to be named, for example, yourtranslationfile-en_GB.mo |
||
224 | * File to be placed, for example, wp-content/languages/textdomain/yourtranslationfile-en_GB.mo |
||
225 | */ |
||
226 | public function loadPagantisTranslation($mofile, $domain) |
||
227 | { |
||
228 | if ('pagantis' === $domain) { |
||
229 | $mofile = WP_LANG_DIR . '/../plugins/pagantis/languages/pagantis-' . get_locale() . '.mo'; |
||
230 | } |
||
231 | |||
232 | return $mofile; |
||
233 | } |
||
234 | |||
235 | /** |
||
236 | * Sql table |
||
237 | */ |
||
238 | public function prepare_wpdb_tables() |
||
239 | { |
||
240 | global $wpdb; |
||
241 | |||
242 | $tableName = $wpdb->prefix . PAGANTIS_CONCURRENCY_TABLE; |
||
243 | if ($wpdb->get_var("SHOW TABLES LIKE '$tableName'") !== $tableName) { |
||
244 | $charset_collate = $wpdb->get_charset_collate(); |
||
245 | $sql = "CREATE TABLE $tableName ( order_id int NOT NULL, |
||
246 | createdAt timestamp DEFAULT CURRENT_TIMESTAMP, UNIQUE KEY id (order_id)) $charset_collate"; |
||
247 | require_once(ABSPATH . 'wp-admin/includes/upgrade.php'); |
||
248 | dbDelta($sql); |
||
249 | } |
||
250 | |||
251 | $tableName = $wpdb->prefix . PAGANTIS_CONFIG_TABLE; |
||
252 | |||
253 | //Check if table exists |
||
254 | $tableExists = $wpdb->get_var("SHOW TABLES LIKE '$tableName'") !== $tableName; |
||
255 | if ($tableExists) { |
||
256 | $charset_collate = $wpdb->get_charset_collate(); |
||
257 | $sql = "CREATE TABLE IF NOT EXISTS $tableName ( |
||
258 | id int NOT NULL AUTO_INCREMENT, |
||
259 | config varchar(60) NOT NULL, |
||
260 | value varchar(1000) NOT NULL, |
||
261 | UNIQUE KEY id(id)) $charset_collate"; |
||
262 | |||
263 | require_once(ABSPATH . 'wp-admin/includes/upgrade.php'); |
||
264 | dbDelta($sql); |
||
265 | } else { |
||
266 | //Updated value field to adapt to new length < v8.0.1 |
||
267 | $query = |
||
268 | "select COLUMN_TYPE FROM information_schema.COLUMNS where TABLE_NAME='$tableName' AND COLUMN_NAME='value'"; |
||
269 | $results = $wpdb->get_results($query, ARRAY_A); |
||
270 | if ($results['0']['COLUMN_TYPE'] === 'varchar(100)') { |
||
271 | $sql = "ALTER TABLE $tableName MODIFY value varchar(1000)"; |
||
272 | $wpdb->query($sql); |
||
273 | } |
||
274 | |||
275 | //Adapting selector to array < v8.1.1 |
||
276 | $query = "select * from $tableName where config='PAGANTIS_SIMULATOR_CSS_QUANTITY_SELECTOR' |
||
277 | or config='PAGANTIS_SIMULATOR_CSS_PRICE_SELECTOR'"; |
||
278 | $dbCurrentConfig = $wpdb->get_results($query, ARRAY_A); |
||
279 | foreach ($dbCurrentConfig as $item) { |
||
280 | if ($item['config'] === 'PAGANTIS_SIMULATOR_CSS_PRICE_SELECTOR') { |
||
281 | $css_price_selector = $this->preparePriceSelector($item['value']); |
||
282 | if ($item['value'] !== $css_price_selector) { |
||
283 | $wpdb->update($tableName, array('value' => stripslashes($css_price_selector)), |
||
284 | array('config' => 'PAGANTIS_SIMULATOR_CSS_PRICE_SELECTOR'), array('%s'), array('%s')); |
||
285 | } |
||
286 | } elseif ($item['config'] === 'PAGANTIS_SIMULATOR_CSS_QUANTITY_SELECTOR') { |
||
287 | $css_quantity_selector = $this->prepareQuantitySelector($item['value']); |
||
288 | if ($item['value'] !== $css_quantity_selector) { |
||
289 | $wpdb->update($tableName, array('value' => stripslashes($css_quantity_selector)), |
||
290 | array('config' => 'PAGANTIS_SIMULATOR_CSS_QUANTITY_SELECTOR'), array('%s'), array('%s')); |
||
291 | } |
||
292 | } |
||
293 | } |
||
294 | } |
||
295 | |||
296 | //Adapting selector to array < v8.2.2 |
||
297 | $tableName = $wpdb->prefix . PAGANTIS_CONFIG_TABLE; |
||
298 | $query = "select * from $tableName where config='PAGANTIS_SIMULATOR_THOUSANDS_SEPARATOR'"; |
||
299 | $results = $wpdb->get_results($query, ARRAY_A); |
||
300 | if (count($results) === 0) { |
||
301 | $wpdb->insert($tableName, array('config' => 'PAGANTIS_SIMULATOR_THOUSANDS_SEPARATOR', 'value' => '.'), |
||
302 | array('%s', '%s')); |
||
303 | $wpdb->insert($tableName, array('config' => 'PAGANTIS_SIMULATOR_DECIMAL_SEPARATOR', 'value' => ','), |
||
304 | array('%s', '%s')); |
||
305 | } |
||
306 | |||
307 | //Adding new selector < v8.3.0 |
||
308 | $tableName = $wpdb->prefix . PAGANTIS_CONFIG_TABLE; |
||
309 | $query = "select * from $tableName where config='PAGANTIS_DISPLAY_MAX_AMOUNT'"; |
||
310 | $results = $wpdb->get_results($query, ARRAY_A); |
||
311 | if (count($results) === 0) { |
||
312 | $wpdb->insert($tableName, array('config' => 'PAGANTIS_DISPLAY_MAX_AMOUNT', 'value' => '0'), |
||
313 | array('%s', '%s')); |
||
314 | } |
||
315 | |||
316 | //Adding new selector < v8.3.2 |
||
317 | $tableName = $wpdb->prefix . PAGANTIS_CONFIG_TABLE; |
||
318 | $query = "select * from $tableName where config='PAGANTIS_SIMULATOR_DISPLAY_SITUATION'"; |
||
319 | $results = $wpdb->get_results($query, ARRAY_A); |
||
320 | if (count($results) === 0) { |
||
321 | $wpdb->insert($tableName, array('config' => 'PAGANTIS_SIMULATOR_DISPLAY_SITUATION', 'value' => 'default'), |
||
322 | array('%s', '%s')); |
||
323 | $wpdb->insert($tableName, array('config' => 'PAGANTIS_SIMULATOR_SELECTOR_VARIATION', 'value' => 'default'), |
||
324 | array('%s', '%s')); |
||
325 | } |
||
326 | |||
327 | //Adding new selector < v8.3.3 |
||
328 | $tableName = $wpdb->prefix . PAGANTIS_CONFIG_TABLE; |
||
329 | $query = "select * from $tableName where config='PAGANTIS_SIMULATOR_DISPLAY_TYPE_CHECKOUT'"; |
||
330 | $results = $wpdb->get_results($query, ARRAY_A); |
||
331 | if (count($results) === 0) { |
||
332 | $wpdb->insert($tableName, array( |
||
333 | 'config' => 'PAGANTIS_SIMULATOR_DISPLAY_TYPE_CHECKOUT', |
||
334 | 'value' => 'sdk.simulator.types.CHECKOUT_PAGE', |
||
335 | ), array('%s', '%s')); |
||
336 | $wpdb->update($tableName, array('value' => 'sdk.simulator.types.PRODUCT_PAGE'), |
||
337 | array('config' => 'PAGANTIS_SIMULATOR_DISPLAY_TYPE'), array('%s'), array('%s')); |
||
338 | } |
||
339 | |||
340 | //Adapting to variable selector < v8.3.6 |
||
341 | $variableSelector = |
||
342 | 'div.summary div.woocommerce-variation.single_variation > div.woocommerce-variation-price span.price'; |
||
343 | $tableName = $wpdb->prefix . PAGANTIS_CONFIG_TABLE; |
||
344 | $query = |
||
345 | "select * from $tableName where config='PAGANTIS_SIMULATOR_SELECTOR_VARIATION' and value='default'"; |
||
346 | $results = $wpdb->get_results($query, ARRAY_A); |
||
347 | if (count($results) === 0) { |
||
348 | $wpdb->update($tableName, array('value' => $variableSelector), |
||
349 | array('config' => 'PAGANTIS_SIMULATOR_SELECTOR_VARIATION'), array('%s'), array('%s')); |
||
350 | } |
||
351 | |||
352 | $dbConfigs = $wpdb->get_results("select * from $tableName", ARRAY_A); |
||
353 | |||
354 | // Convert a multiple dimension array for SQL insert statements into a simple key/value |
||
355 | $simpleDbConfigs = array(); |
||
356 | foreach ($dbConfigs as $config) { |
||
357 | $simpleDbConfigs[$config['config']] = $config['value']; |
||
358 | } |
||
359 | $newConfigs = array_diff_key(WC_Pagantis_Config::getDefaultConfig(), $simpleDbConfigs); |
||
360 | if ( ! empty($newConfigs)) { |
||
361 | foreach ($newConfigs as $key => $value) { |
||
362 | $wpdb->insert($tableName, array('config' => $key, 'value' => $value), array('%s', '%s')); |
||
363 | } |
||
364 | } |
||
365 | |||
366 | //Current plugin config: pagantis_public_key => New field --- public_key => Old field |
||
367 | $settings = get_option('woocommerce_pagantis_settings'); |
||
368 | |||
369 | if ( ! isset($settings['pagantis_public_key']) && $settings['public_key']) { |
||
370 | $settings['pagantis_public_key'] = $settings['public_key']; |
||
371 | unset($settings['public_key']); |
||
372 | } |
||
373 | |||
374 | if ( ! isset($settings['pagantis_private_key']) && $settings['secret_key']) { |
||
375 | $settings['pagantis_private_key'] = $settings['secret_key']; |
||
376 | unset($settings['secret_key']); |
||
377 | } |
||
378 | |||
379 | update_option('woocommerce_pagantis_settings', $settings); |
||
380 | } |
||
381 | |||
382 | |||
383 | public function enqueue_simulator_scripts() |
||
384 | { |
||
385 | if ( ! pg_isPluginActive()) { |
||
386 | return; |
||
387 | } |
||
388 | |||
389 | wp_register_script('pagantis-simulator', plugins_url('assets/js/pagantis-simulator.js', PAGANTIS_PLUGIN_ID), |
||
390 | array('jquery'), ''); |
||
391 | wp_enqueue_script('pagantis-simulator'); |
||
392 | |||
393 | global $product; |
||
394 | |||
395 | pg_canProductSimulatorLoad(); |
||
396 | $locale = pg_GetLocaleString(); |
||
397 | $settings = pg_get_plugin_settings(); |
||
398 | |||
399 | $post_id = $product->get_id(); |
||
400 | $simulator_localized_params = array( |
||
401 | 'total' => is_numeric($product->get_price()) ? $product->get_price() : 0, |
||
402 | 'public_key' => $settings['pagantis_public_key'], |
||
403 | 'simulator_type' => WC_Pagantis_Config::getValueOfKey('PAGANTIS_SIMULATOR_DISPLAY_TYPE'), |
||
404 | 'positionSelector' => WC_Pagantis_Config::getValueOfKey('PAGANTIS_SIMULATOR_CSS_POSITION_SELECTOR'), |
||
405 | 'quantitySelector' => WC_Pagantis_Config::getValueOfKey('PAGANTIS_SIMULATOR_CSS_QUANTITY_SELECTOR', |
||
406 | true), |
||
407 | 'priceSelector' => WC_Pagantis_Config::getValueOfKey('PAGANTIS_SIMULATOR_CSS_PRICE_SELECTOR', |
||
408 | true), |
||
409 | 'totalAmount' => is_numeric($product->get_price()) ? $product->get_price() : 0, |
||
410 | 'locale' => $locale, |
||
411 | 'country' => $locale, |
||
412 | 'isProductPromoted' => pg_isProductPromoted($post_id), |
||
413 | 'promotedMessage' => WC_Pagantis_Config::getValueOfKey('PAGANTIS_PROMOTION_EXTRA'), |
||
414 | 'thousandSeparator' => WC_Pagantis_Config::getValueOfKey('PAGANTIS_SIMULATOR_THOUSANDS_SEPARATOR'), |
||
415 | 'decimalSeparator' => WC_Pagantis_Config::getValueOfKey('PAGANTIS_SIMULATOR_DECIMAL_SEPARATOR'), |
||
416 | 'pagantisQuotesStart' => WC_Pagantis_Config::getValueOfKey('PAGANTIS_SIMULATOR_START_INSTALLMENTS'), |
||
417 | 'pagantisSimulatorSkin' => WC_Pagantis_Config::getValueOfKey('PAGANTIS_SIMULATOR_DISPLAY_SKIN'), |
||
418 | 'pagantisSimulatorPosition' => WC_Pagantis_Config::getValueOfKey('PAGANTIS_SIMULATOR_DISPLAY_CSS_POSITION'), |
||
419 | 'finalDestination' => WC_Pagantis_Config::getValueOfKey('PAGANTIS_SIMULATOR_DISPLAY_SITUATION'), |
||
420 | 'variationSelector' => WC_Pagantis_Config::getValueOfKey('PAGANTIS_SIMULATOR_SELECTOR_VARIATION'), |
||
421 | 'productType' => $product->get_type(), |
||
422 | ); |
||
423 | |||
424 | wp_localize_script('pagantis-simulator', 'pg_sim_params', $simulator_localized_params); |
||
425 | |||
426 | wp_enqueue_script('pg_sim_params'); |
||
427 | } |
||
428 | |||
429 | |||
430 | /** |
||
431 | * Product simulator |
||
432 | */ |
||
433 | public function addProductSimulatorTemplate() |
||
434 | { |
||
435 | global $product; |
||
436 | |||
437 | pg_canProductSimulatorLoad(); |
||
438 | |||
439 | $post_id = $product->get_id(); |
||
440 | $template_arguments = array( |
||
441 | 'isProductPromoted' => pg_isProductPromoted($post_id), |
||
442 | 'promotedMessage' => WC_Pagantis_Config::getValueOfKey('PAGANTIS_PROMOTION_EXTRA'), |
||
443 | ); |
||
444 | |||
445 | wc_get_template('product_simulator.php', $template_arguments, '', $this->template_path); |
||
446 | } |
||
447 | |||
448 | /** |
||
449 | * Product simulator |
||
450 | * |
||
451 | * @global WC_Product $product Product object. |
||
452 | */ |
||
453 | public function pagantisAddProductSimulator() |
||
497 | } |
||
498 | |||
499 | |||
500 | /** |
||
501 | * Add Pagantis to payments list. |
||
502 | * |
||
503 | * @param $methods |
||
504 | * |
||
505 | * @return array |
||
506 | * @hook woocommerce_payment_gateways |
||
507 | */ |
||
508 | public function add_pagantis_gateway($methods) |
||
509 | { |
||
510 | if ( ! class_exists('WC_Payment_Gateway')) { |
||
511 | return $methods; |
||
512 | } |
||
513 | |||
514 | include_once('controllers/class-wc-pagantis-gateway.php'); |
||
515 | $methods[] = 'WC_Pagantis_Gateway'; |
||
516 | |||
517 | return $methods; |
||
518 | } |
||
519 | |||
520 | /** |
||
521 | * Initialize WC_Pagantis class |
||
522 | * |
||
523 | * @param $methods |
||
524 | * |
||
525 | * @return mixed |
||
526 | */ |
||
527 | public function check_if_pg_is_in_available_gateways($methods) |
||
528 | { |
||
529 | $pagantis = new WC_Pagantis_Gateway(); |
||
530 | if ( ! $pagantis->is_available()) { |
||
531 | unset($methods['pagantis']); |
||
532 | } |
||
533 | |||
534 | return $methods; |
||
535 | } |
||
536 | |||
537 | /** |
||
538 | * Add links to Plugin description in WP Plugins panel |
||
539 | * |
||
540 | * @param $links |
||
541 | * |
||
542 | * @return mixed |
||
543 | * @hook plugin_action_links_pagantis |
||
544 | */ |
||
545 | public function get_plugin_action_links($links) |
||
553 | } |
||
554 | |||
555 | |||
556 | public function get_setting_link() |
||
557 | { |
||
558 | $section_slug = 'pagantis'; |
||
559 | |||
560 | return admin_url('admin.php?page=wc-settings&tab=checkout§ion=' . $section_slug); |
||
561 | } |
||
562 | |||
563 | /** |
||
564 | * Add links to Plugin options |
||
565 | * |
||
566 | * @param $links |
||
567 | * @param $file |
||
568 | * |
||
569 | * @hook plugin_row_meta |
||
570 | * @return array |
||
571 | */ |
||
572 | public function get_plugin_row_meta_links($links, $file) |
||
573 | { |
||
574 | if ($file === plugin_basename(__FILE__)) { |
||
575 | $links[] = |
||
576 | '<a href="' . PAGANTIS_GIT_HUB_URL . '" target="_blank">' . __('Documentation', 'pagantis') . '</a>'; |
||
577 | $links[] = |
||
578 | '<a href="' . PAGANTIS_DOC_URL . '" target="_blank">' . __('API documentation', 'pagantis') . '</a>'; |
||
579 | $links[] = '<a href="' . PAGANTIS_SUPPORT_EMAIL . '">' . __('Support', 'pagantis') . '</a>'; |
||
580 | |||
581 | return $links; |
||
582 | } |
||
583 | |||
584 | return $links; |
||
585 | } |
||
586 | |||
587 | /** |
||
588 | * Read logs |
||
589 | * |
||
590 | * @param $data |
||
591 | * |
||
592 | * @global wpdb $wpdb WordPress database abstraction object. |
||
593 | */ |
||
594 | public function get_pagantis_logs($data) |
||
595 | { |
||
596 | global $wpdb; |
||
597 | $filters = ($data->get_params()); |
||
598 | $response = array(); |
||
599 | $secretKey = $filters['secret']; |
||
600 | $from = $filters['from']; |
||
601 | $to = $filters['to']; |
||
602 | $cfg = get_option('woocommerce_pagantis_settings'); |
||
603 | $privateKey = isset($cfg['pagantis_private_key']) ? $cfg['pagantis_private_key'] : null; |
||
604 | $tableName = $wpdb->prefix . PAGANTIS_LOGS_TABLE; |
||
605 | $query = "SELECT * FROM $tableName WHERE createdAt>$from AND createdAt<$to ORDER BY createdAt DESC"; |
||
606 | $results = $wpdb->get_results($query); |
||
607 | if (isset($results) && $privateKey === $secretKey) { |
||
608 | foreach ($results as $key => $result) { |
||
609 | $response[$key]['timestamp'] = $result->createdAt; |
||
610 | $response[$key]['log'] = json_decode($result->log); |
||
611 | } |
||
612 | } else { |
||
613 | $response['result'] = 'Error'; |
||
614 | } |
||
615 | $response = json_encode($response); |
||
616 | header('HTTP/1.1 200', true, 200); |
||
617 | header('Content-Type: application/json', true); |
||
618 | header('Content-Length: ' . strlen($response)); |
||
619 | echo($response); |
||
620 | exit(); |
||
621 | } |
||
622 | |||
623 | /** |
||
624 | * Update extra config |
||
625 | * |
||
626 | * @param $data |
||
627 | */ |
||
628 | public function updateExtraConfig($data) |
||
629 | { |
||
630 | global $wpdb; |
||
631 | $tableName = $wpdb->prefix . PAGANTIS_CONFIG_TABLE; |
||
632 | $response = array('status' => null); |
||
633 | |||
634 | $filters = ($data->get_params()); |
||
635 | $secretKey = $filters['secret']; |
||
636 | $cfg = get_option('woocommerce_pagantis_settings'); |
||
637 | $privateKey = isset($cfg['pagantis_private_key']) ? $cfg['pagantis_private_key'] : null; |
||
638 | if ($privateKey !== $secretKey) { |
||
639 | $response['status'] = 401; |
||
640 | $response['result'] = 'Unauthorized'; |
||
641 | } elseif ($_SERVER['REQUEST_METHOD'] === 'POST') { |
||
642 | if (count($_POST)) { |
||
643 | foreach ($_POST as $config => $value) { |
||
644 | if (isset($this->initialConfig[$config]) && $response['status'] === null) { |
||
645 | $wpdb->update($tableName, array('value' => stripslashes($value)), array('config' => $config), |
||
646 | array('%s'), array('%s')); |
||
647 | } else { |
||
648 | $response['status'] = 400; |
||
649 | $response['result'] = 'Bad request'; |
||
650 | } |
||
651 | } |
||
652 | } else { |
||
653 | $response['status'] = 422; |
||
654 | $response['result'] = 'Empty data'; |
||
655 | } |
||
656 | } |
||
657 | |||
658 | if ($response['status'] === null) { |
||
659 | $tableName = $wpdb->prefix . PAGANTIS_CONFIG_TABLE; |
||
660 | $dbResult = $wpdb->get_results("select config, value from $tableName", ARRAY_A); |
||
661 | foreach ($dbResult as $value) { |
||
662 | $formattedResult[$value['config']] = $value['value']; |
||
663 | } |
||
664 | $response['result'] = $formattedResult; |
||
665 | } |
||
666 | |||
667 | $result = json_encode($response['result']); |
||
668 | header('HTTP/1.1 ' . $response['status'], true, $response['status']); |
||
669 | header('Content-Type: application/json', true); |
||
670 | header('Content-Length: ' . strlen($result)); |
||
671 | echo($result); |
||
672 | exit(); |
||
673 | } |
||
674 | |||
675 | /** |
||
676 | * Read logs |
||
677 | * |
||
678 | * @param $data |
||
679 | */ |
||
680 | public function readApi($data) |
||
681 | { |
||
682 | global $wpdb; |
||
683 | $filters = ($data->get_params()); |
||
684 | $response = array('timestamp' => time()); |
||
685 | $secretKey = $filters['secret']; |
||
686 | $from = ($filters['from']) ? date_create($filters['from']) : date('Y-m-d', strtotime('-7 day')); |
||
687 | $to = ($filters['to']) ? date_create($filters['to']) : date('Y-m-d', strtotime('+1 day')); |
||
688 | $method = ($filters['method']) ? ($filters['method']) : 'Pagantis'; |
||
689 | $cfg = get_option('woocommerce_pagantis_settings'); |
||
690 | $privateKey = isset($cfg['pagantis_private_key']) ? $cfg['pagantis_private_key'] : null; |
||
691 | $tableName = $wpdb->prefix . PAGANTIS_WC_ORDERS_TABLE; |
||
692 | $tableNameInner = $wpdb->prefix . 'postmeta'; |
||
693 | $query = "SELECT * FROM $tableName tn INNER JOIN $tableNameInner tn2 ON tn2.post_id = tn.id |
||
694 | WHERE tn.post_type='shop_order' AND tn.post_date>'" . $from->format('Y-m-d') . "' |
||
695 | AND tn.post_date<'" . $to->format('Y-m-d') . "' ORDER BY tn.post_date DESC"; |
||
696 | $results = $wpdb->get_results($query); |
||
697 | |||
698 | if (isset($results) && $privateKey === $secretKey) { |
||
699 | foreach ($results as $result) { |
||
700 | $key = $result->ID; |
||
701 | $response['message'][$key]['timestamp'] = $result->post_date; |
||
702 | $response['message'][$key]['order_id'] = $key; |
||
703 | $response['message'][$key][$result->meta_key] = $result->meta_value; |
||
704 | } |
||
705 | } else { |
||
706 | $response['result'] = 'Error'; |
||
707 | } |
||
708 | $response = json_encode($response); |
||
709 | header('HTTP/1.1 200', true, 200); |
||
710 | header('Content-Type: application/json', true); |
||
711 | header('Content-Length: ' . strlen($response)); |
||
712 | echo($response); |
||
713 | exit(); |
||
714 | } |
||
715 | |||
716 | /** |
||
717 | * ENDPOINT - Read logs -> Hook: rest_api_init |
||
718 | * |
||
719 | * @hook rest_api_init |
||
720 | * @return mixed |
||
721 | */ |
||
722 | public function register_pg_rest_routes() |
||
723 | { |
||
724 | register_rest_route('pagantis/v1', '/logs/(?P<secret>\w+)/(?P<from>\d+)/(?P<to>\d+)', array( |
||
725 | 'methods' => 'GET', |
||
726 | 'callback' => array( |
||
727 | $this, |
||
728 | 'get_pagantis_logs', |
||
729 | ), |
||
730 | ), true); |
||
731 | |||
732 | register_rest_route('pagantis/v1', '/configController/(?P<secret>\w+)', array( |
||
733 | 'methods' => 'GET, POST', |
||
734 | 'callback' => array( |
||
735 | $this, |
||
736 | 'updateExtraConfig', |
||
737 | ), |
||
738 | ), true); |
||
739 | |||
740 | register_rest_route('pagantis/v1', '/api/(?P<secret>\w+)/(?P<from>\w+)/(?P<to>\w+)', array( |
||
741 | 'methods' => 'GET', |
||
742 | 'callback' => array( |
||
743 | $this, |
||
744 | 'readApi', |
||
745 | ), |
||
746 | ), true); |
||
747 | } |
||
748 | |||
749 | /** |
||
750 | * @param $css_quantity_selector |
||
751 | * |
||
752 | * @return mixed|string |
||
753 | */ |
||
754 | private function prepareQuantitySelector($css_quantity_selector) |
||
763 | } |
||
764 | |||
765 | /** |
||
766 | * @param $css_price_selector |
||
767 | * |
||
768 | * @return mixed|string |
||
769 | */ |
||
770 | private function preparePriceSelector($css_price_selector) |
||
779 | } |
||
780 | } |
||
781 | |||
782 | /** |
||
783 | * Add widget Js |
||
803 |