PluginProbe
MakeCommerce for WooCommerce / trunk
MakeCommerce for WooCommerce vtrunk
4.1.0 4.0.8 trunk 1.0 1.0.1 1.0.2 1.0.3 1.1.0 1.1.1 1.1.2 1.1.3 1.1.4 1.1.5 1.1.6 1.1.7 2.0.0 2.1.0 2.1.1 2.1.2 2.1.3 2.1.4 2.1.5 2.2.0 2.2.1 2.2.2 All 96 releases
makecommerce / api / api.php

api.php in MakeCommerce for WooCommerce trunk, at api/api.php

597 lines 20.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * The api functionality of the plugin.
5 *
6 * @link https://makecommerce.net/
7 * @since 3.0.0
8 *
9 * @package Makecommerce
10 * @subpackage Makecommerce/api
11 */
12
13 namespace MakeCommerce;
14
15 /**
16 * The payment functionality of the plugin.
17 *
18 * Defines the plugin name, version, and two examples hooks for how to
19 * enqueue the admin-specific stylesheet and JavaScript.
20 *
21 * @package Makecommerce
22 * @subpackage Makecommerce/api
23 * @author Maksekeskus AS <support@maksekeskus.ee>
24 */
25 class API {
26
27 /**
28 * Shipping+ confirmation page and the authorization data guarding its form.
29 */
30 public const SHIPPING_PLUS_SLUG = 'makecommerce_shipping_plus';
31 public const SHIPPING_PLUS_CAPABILITY = 'manage_options';
32 public const SHIPPING_PLUS_NONCE_ACTION = 'mc_shipping_plus_confirm';
33 public const SHIPPING_PLUS_NONCE_FIELD = 'mc_shipping_plus_nonce';
34
35 /**
36 * The ID of this plugin.
37 *
38 * @since 3.0.0
39 * @access private
40 * @var string $plugin_name The ID of this plugin.
41 */
42 private $plugin_name;
43
44 /**
45 * The version of this plugin.
46 *
47 * @since 3.0.0
48 * @access private
49 * @var string $version The current version of this plugin.
50 */
51 private $version;
52
53 private Loader $loader;
54
55 /**
56 * Initialize the class and set its properties.
57 *
58 * @since 3.0.0
59 * @param string $plugin_name The name of this plugin.
60 * @param string $version The version of this plugin.
61 */
62 public function __construct( $plugin_name, $version, Loader $loader ) {
63
64 $this->plugin_name = $plugin_name;
65 $this->version = $version;
66 $this->loader = $loader;
67 }
68
69 public function label_formats() {
70
71 $MK = \MakeCommerce::get_api();
72
73 if ( !$MK ) {
74 return array( 'A4' => 'A4' );
75 }
76
77 $optionsArray = array();
78
79 try {
80
81 $formats = $MK->getLabelFormats();
82
83 foreach ( $formats as $format ) {
84 $optionsArray[$format] = $format;
85 }
86 } catch ( \Exception $e ) {
87 //do nothing, something is wrong with the credentials or the shop is disabled
88 }
89
90 return $optionsArray;
91 }
92
93 /**
94 * Gives error in admin if curl is not loaded
95 */
96 public function check_if_curl_is_loaded() {
97
98 if ( !extension_loaded('curl') ) {
99
100 $class = 'notice notice-error';
101 $message = __( 'You have enabled MakeCommerce module but it seems that you don\'t have CURL enabled. This way MakeCommerce unfortunately does not work!', 'wc_makecommerce_domain' );
102
103 printf( '<div class="%1$s"><p>%2$s</p></div>', esc_attr( $class ), esc_html( $message ) );
104 }
105 }
106
107 /**
108 * Add menu item to WooCommerce advanced section
109 *
110 * @since 3.0.0
111 */
112 public function add_woo_menu( $sections ) {
113
114 $sections['mk_api'] = __('MakeCommerce API access', 'wc_makecommerce_domain');
115
116 return $sections;
117 }
118
119 /**
120 * Adds new column (payment method) in order view
121 *
122 * @since 3.0.0
123 */
124 public function add_ordersview_paymentmethod_column( $columns ) {
125
126 $columns['makecommerce_payment_method'] = __('Payment method (MC)');
127
128 return $columns;
129 }
130
131 /**
132 * Makes payment method column in order view sortable
133 *
134 * @since 3.0.0
135 */
136 public function make_ordersview_paymentmethod_column_sortable( $columns ) {
137
138 return wp_parse_args (array( 'makecommerce_payment_method' => 'makecommerce_payment_method' ), $columns );
139 }
140
141 /**
142 * Inserts content for payment method column in order view
143 *
144 * @since 3.0.0
145 */
146 public function fill_ordersview_paymentmethod_column( $column, $post_id ) {
147
148 if ( 'makecommerce_payment_method' === $column ) {
149 $order = wc_get_order( $post_id );
150 echo $order->get_meta( '_makecommerce_preselected_method', true );
151 }
152 }
153
154 /**
155 * Update banklinks when an admin logs in
156 *
157 * @since 3.0.0
158 */
159 public function admin_login( $user_login, $user ) {
160
161 if ( user_can( $user, 'administrator' ) ) {
162 Payment::update_banklinks();
163 }
164 }
165
166 /**
167 * Adds settings link to plugins page
168 *
169 * @since 3.0.0
170 */
171 public function add_plugin_settings_link( $links ) {
172
173 $settings_link = '<a href="admin.php?page=wc-settings&tab=api&section=mk_api">API '.__('Settings').'</a>';
174
175 if ( \MakeCommerce::new_woo_version() ) {
176 $settings_link = '<a href="admin.php?page=wc-settings&tab=advanced&section=mk_api">API '.__('Settings').'</a>';
177 }
178
179 array_unshift( $links, $settings_link );
180
181 return $links;
182 }
183
184 /**
185 * Updates banklinks if current section is not in mk_api
186 *
187 * @since 3.0.0
188 */
189 public function save_settings() {
190
191 global $current_section;
192
193 if ( $current_section !== 'mk_api' ) {
194 return;
195 }
196
197 Payment::update_banklinks();
198 }
199
200 /**
201 * Test and live switching in API settings (jQuery)
202 *
203 * @since 3.0.0
204 */
205 public function api_javascript_ui( $data ) {
206
207 wp_enqueue_script( $this->plugin_name . "api-admin", plugin_dir_url(__FILE__) . 'js/api.js', array( 'jquery' ), $this->version );
208 }
209
210 /**
211 * Add admin settings for api in woocommerce->settings->advanced->Makcommerce API
212 *
213 * @since 3.0.0
214 */
215 public function add_woo_admin_settings( $settings ) {
216
217 /**
218 * Prevent the settings from being loaded if we are not at actually the page
219 */
220 global $current_section;
221
222 if ( $current_section !== 'mk_api' ) {
223 return $settings;
224 }
225
226 /**
227 * Return array with fields for settings
228 */
229 return [
230 [ 'type' => 'title', 'desc' => \MakeCommerce::get_logo_html() ],
231 [
232 'type' => 'title',
233 'title' => __('MakeCommerce Shipping+ is now available', 'wc_makecommerce_domain'),
234 'desc' => sprintf(
235 __('You\'re still using the legacy version — <a href="%s">click here to switch to the new module</a>', 'wc_makecommerce_domain'),
236 admin_url('admin.php?page=makecommerce_shipping_plus')
237 ),
238 'id' => 'mk_api_settings'
239 ],
240 ['type' => 'sectionend', 'id' => 'mk_api_settings'],
241 [
242 'type' => 'title',
243 'title' => __('MakeCommerce API access credentials', 'wc_makecommerce_domain'),
244 'desc' => __('To use MakeCommerce/Maksekeskus services you need to enter API credentials below here <br/> <br/>', 'wc_makecommerce_domain').
245 sprintf( __('To further configure the Payment methods please go to <a href="%s">MakeCommerce Checkout Options</a>, links to settings of our Shipment methods are listed below', 'wc_makecommerce_domain'), 'admin.php?page=wc-settings&tab=checkout&section=makecommerce'),
246 'id' => 'mk_api_settings'
247 ],
248 [
249 'type' => 'select',
250 'title' => __('Current environment', 'wc_makecommerce_domain'),
251 'desc' => __('See more about <a href="https://maksekeskus.ee/en/for-developers/test-environment/">MakeCommerce Test environment</a>', 'wc_makecommerce_domain'),
252 'default' => 'live',
253 'options' => [
254 'live' => __('Live', 'wc_makecommerce_domain'),
255 'test' => __('Test', 'wc_makecommerce_domain'),
256 ],
257 'id' => 'mk_api_type'
258 ],
259 [
260 'id' => 'mk_shop_id',
261 'type' => 'text',
262 'title' => __('Shop ID (live)', 'wc_makecommerce_domain'),
263 'desc' => __('Get it from <a href="https://merchant.maksekeskus.ee/api.html" target="_blank">Merchant Portal</a>','wc_makecommerce_domain'),
264 'class' => 'input-text regular-input',
265 ],
266 [
267 'id' => 'mk_private_key',
268 'type' => 'text',
269 'title' => __('Secret key (live)', 'wc_makecommerce_domain'),
270 'class' => 'input-text regular-input',
271 ],
272 [
273 'id' => 'mk_public_key',
274 'type' => 'text',
275 'title' => __('Publishable key (live)', 'wc_makecommerce_domain'),
276 'class' => 'input-text regular-input',
277 ],
278 [
279 'id' => 'mk_test_shop_id',
280 'type' => 'text',
281 'title' => __('Shop ID (test)', 'wc_makecommerce_domain'),
282 'class' => 'input-text regular-input',
283 'default' => 'f64b4f20-5ef9-4b7b-a6fa-d623d87f0b9c',
284 'desc' => __('Get it from <a href="https://merchant.test.maksekeskus.ee/api.html" target="_blank">Merchant Portal Test</a>','wc_makecommerce_domain'),
285 ],
286 [
287 'id' => 'mk_test_private_key',
288 'type' => 'text',
289 'title' => __('Secret key (test)', 'wc_makecommerce_domain'),
290 'default' => 'MPjcVMoRZPAucsTuGK5ZukOlV7BlzgSvXOowJUkk9IFSQPVooRwcVOxzz3mhEpgM',
291 'class' => 'input-text regular-input',
292 ],
293 [
294 'id' => 'mk_test_public_key',
295 'type' => 'text',
296 'title' => __('Publishable key (test)', 'wc_makecommerce_domain'),
297 'default' => '7Hog41ci2mKkmtviMycWxpNx14pNP70m',
298 'class' => 'input-text regular-input',
299 ],
300 [
301 'type' => 'select',
302 'title' => __('Label print format', 'wc_makecommerce_domain'),
303 'desc' => __('In which format should shipping labels be printed. (*make sure you have API access to see more options)', 'wc_makecommerce_domain'),
304 'default' => 'A4',
305 'options' => $this->label_formats(),
306 'id' => 'mk_label_format'
307 ],
308 ['type' => 'sectionend', 'id' => 'mk_api_settings'],
309 [
310 'type' => 'title',
311 'desc' => '<br><hr><br>',
312 ],
313 [
314 'title' => __( 'Parcel Machine Map Settings', 'wc_makecommerce_domain' ),
315 'type' => 'title',
316 'desc' => __( 'Before using, please read more about the functionalities, benefits and security measures of our parcel machine map from', 'wc_makecommerce_domain' ) .
317 ' ' . sprintf('<a target="_blank" href="%s">' .
318 __( 'our plugin instructions', 'wc_makecommerce_domain' ) .
319 '</a>', __( 'https://makecommerce.net/integration-modules/makecommerce-plugin-for-woocommerce/#map-view-for-the-selection-of-parcel-machines', 'wc_makecommerce_domain' ) ),
320 'id' => 'mc_map_settings',
321 ],
322 [
323 'title' => __( 'Enable map selection for parcel machines', 'wc_makecommerce_domain' ),
324 'label' => __( 'Allow customers to select their desired parcel machine from a map view', 'wc_makecommerce_domain' ),
325 'type' => 'checkbox',
326 'default' => 'no',
327 'id' => 'mc_parcel_machine_map',
328 ],
329 [
330 'title' => __( 'Define Google Javascript API key', 'wc_makecommerce_domain' ),
331 'type' => 'text',
332 'desc_tip' => __( 'In order to enable the map feature for parcel machine seletion, a Google Javascript API key needs to be obtained', 'wc_makecommerce_domain' ),
333 'class' => 'ui-map-identifier',
334 'id' => 'mc_google_api_key',
335 ],
336 [
337 'title' => __( 'Use Google Geocoding', 'wc_makecommerce_domain' ),
338 'label' => __( 'Geocoding allows the map to centralize on the shipping address', 'wc_makecommerce_domain' ),
339 'type' => 'checkbox',
340 'default' => 'no',
341 'id' => 'mc_map_geocoding',
342 ],
343 [
344 'title' => __( 'Define a Google Geocoding API key', 'wc_makecommerce_domain' ),
345 'type' => 'text',
346 'desc_tip' => __( 'A separate API key is recommended to be defined for Geocoding due to security reasons', 'wc_makecommerce_domain' ),
347 'class' => 'ui-map-identifier',
348 'id' => 'mc_map_geocoding_api_key',
349 ],
350 [
351 'type' => 'sectionend',
352 'id' => 'mc_map_settings',
353 ],
354 [
355 'type' => 'title',
356 'desc' => '<br><hr><br>',
357 ],
358 [
359 'id' => 'mk_module_title',
360 'type' => 'title',
361 'title' => __('Makecommerce modules', 'wc_makecommerce_domain'),
362 'desc' => __('Our plugin adds several modules to your shop. Here you can switch off modules that are not important for you, they will disappear from Woocommerce settings menus.<br> Each active module have their own settings dialog, where they must also be Enabled before use.', 'wc_makecommerce_domain'),
363 'class' => '',
364 ],
365 [
366 'id' => 'mk_transport_apt_omniva',
367 'type' => 'checkbox',
368 'default' => 'yes',
369 'title' => __('Omniva Parcel Machine', 'wc_makecommerce_domain'),
370 'desc' => __('enable Omniva parcel machines shipping method', 'wc_makecommerce_domain').' ('. sprintf(__('<a href="%s">module settings</a>', 'wc_makecommerce_domain'), admin_url('admin.php?page=wc-settings&tab=shipping&section=parcelmachine_omniva')).')',
371 'class' => '',
372 ],
373 [
374 'id' => 'mk_transport_apt_smartpost',
375 'type' => 'checkbox',
376 'default' => 'yes',
377 'title' => __('SmartPosti Parcel Machine', 'wc_makecommerce_domain'),
378 'desc' => __('enable SmartPosti parcel machines shipping method', 'wc_makecommerce_domain').' ('. sprintf(__('<a href="%s">module settings</a>', 'wc_makecommerce_domain'), admin_url('admin.php?page=wc-settings&tab=shipping&section=parcelmachine_smartpost')).')',
379 'class' => '',
380 ],
381 [
382 'id' => 'mk_transport_apt_dpd',
383 'type' => 'checkbox',
384 'default' => 'no',
385 'title' => __('DPD', 'wc_makecommerce_domain') . ' ' . __('parcel machine', 'wc_makecommerce_domain'),
386 'desc' => __('enable DPD parcel machine shipping method', 'wc_makecommerce_domain').' ('. sprintf(__('<a href="%s">module settings</a>', 'wc_makecommerce_domain'), admin_url('admin.php?page=wc-settings&tab=shipping&section=parcelmachine_dpd')).')',
387 'class' => '',
388 ],
389 [
390 'id' => 'mk_transport_apt_lp_express_lt',
391 'type' => 'checkbox',
392 'default' => 'no',
393 'title' => __('LP Express', 'wc_makecommerce_domain') . ' ' . __('parcel machine', 'wc_makecommerce_domain'),
394 'desc' => __('enable LP Express parcel machine shipping method', 'wc_makecommerce_domain').' ('. sprintf(__('<a href="%s">module settings</a>', 'wc_makecommerce_domain'), admin_url('admin.php?page=wc-settings&tab=shipping&section=parcelmachine_lp_express_lt')).')',
395 'class' => '',
396 ],
397 [
398 'id' => 'mk_transport_courier_omniva',
399 'type' => 'checkbox',
400 'default' => 'yes',
401 'title' => __('Omniva Courier', 'wc_makecommerce_domain'),
402 'desc' => __('enable Omniva courier shipping method', 'wc_makecommerce_domain').' ('. sprintf(__('<a href="%s">module settings</a>', 'wc_makecommerce_domain'), admin_url('admin.php?page=wc-settings&tab=shipping&section=courier_omniva')).')',
403 'class' => '',
404 ],
405 [
406 'id' => 'mk_transport_courier_smartpost',
407 'type' => 'checkbox',
408 'default' => 'yes',
409 'title' => __('Smartpost Courier', 'wc_makecommerce_domain'),
410 'desc' => __('enable Smartpost courier shipping method', 'wc_makecommerce_domain').' ('. sprintf(__('<a href="%s">module settings</a>', 'wc_makecommerce_domain'), admin_url('admin.php?page=wc-settings&tab=shipping&section=courier_smartpost')).')',
411 'class' => '',
412 ],
413 [
414 'id' => 'mk_transport_courier_dpd',
415 'type' => 'checkbox',
416 'default' => 'yes',
417 'title' => __('DPD Courier', 'wc_makecommerce_domain'),
418 'desc' => __('enable DPD courier shipping method', 'wc_makecommerce_domain').' ('. sprintf(__('<a href="%s">module settings</a>', 'wc_makecommerce_domain'), admin_url('admin.php?page=wc-settings&tab=shipping&section=courier_dpd')).')',
419 'class' => '',
420 ],
421 [
422 'id' => 'mk_javascript_ui',
423 'type' => 'api_javascript_ui',
424 ],
425 ['type' => 'sectionend', 'id' => 'mk_api_settings']
426 ];
427 }
428
429 /**
430 * Function for resetting machines and payment methods when API type is changed
431 *
432 * @since 3.2.1
433 */
434 public function mk_delete_api_cache() {
435
436 global $wpdb;
437 $tableName = $wpdb->prefix . MAKECOMMERCE_TABLENAME;
438
439 // Delete machines' cache and timer
440 delete_option( 'mk_machines_expires' );
441 delete_option( 'mk_machines_cache' );
442
443 // Delete payment methods
444 $wpdb->query( 'TRUNCATE TABLE `'.$tableName.'`' );
445 }
446
447 /**
448 * Notice for missing API information
449 *
450 * @since 3.0.0
451 */
452 public function api_info_missing() {
453
454 ?>
455 <div class="notice notice-error is-dismissible">
456 <p>
457 <?php echo __( 'You have not entered the Shop ID and keys for the MakeCommerce payment module. The module will not work without them.', 'wc_makecommerce_domain' ); ?>
458 <?php if ( \MakeCommerce::new_woo_version() ) { ?>
459 <a href="<?php echo admin_url( 'admin.php?page=wc-settings&tab=advanced&section=mk_api' ); ?>"><?php echo __( 'Click here to enter them', 'wc_makecommerce_domain' ); ?></a>
460 <?php } else { ?>
461 <a href="<?php echo admin_url( 'admin.php?page=wc-settings&tab=api&section=mk_api' ); ?>"><?php echo __( 'Click here to enter them', 'wc_makecommerce_domain' ); ?></a>
462 <?php } ?>
463 </p>
464 </div>
465 <?php
466 }
467
468
469 public function shipping_api_notice()
470 {
471 ?>
472 <div class="notice notice-success">
473 <p>
474 <strong><?php echo __('MakeCommerce Shipping+ is now available', 'wc_makecommerce_domain'); ?> </strong> <br/>
475 <?php echo __("You're still using the legacy version —", 'wc_makecommerce_domain'); ?>
476 <a href="<?php echo admin_url('admin.php?page=makecommerce_shipping_plus'); ?>"><?php echo __('click here to switch to the new module', 'wc_makecommerce_domain'); ?></a>
477 </p>
478 </div>
479 <?php
480 }
481
482 // Separate Admin page for Shipping+ confirmation, when user checks shipping+ in old version
483 public function enqueue_shipping_plus_assets($hook): void
484 {
485 if ($hook !== 'admin_page_makecommerce_shipping_plus') {
486 return;
487 }
488
489 wp_enqueue_style(
490 'makecommerce-bootstrap',
491 plugin_dir_url(__DIR__) . 'makecommerce/assets/bootstrap.5.3.3.min.css'
492 );
493
494 wp_enqueue_style(
495 'shipping-confirm',
496 'https://static.maksekeskus.ee/modules/woocommerce/css/shipping-confirm.css'
497 );
498
499 wp_enqueue_script(
500 'makecommerce-bootstrap',
501 plugin_dir_url(__DIR__) . 'makecommerce/assets/bootstrap.bundle.min.js',
502 [],
503 null,
504 true
505 );
506 }
507 public function add_shipping_plus_admin_page()
508 {
509 add_submenu_page(
510 'shipping_plus',
511 'Shipping+ Confirmation',
512 'Shipping+ Confirmation',
513 self::SHIPPING_PLUS_CAPABILITY,
514 self::SHIPPING_PLUS_SLUG,
515 [$this, 'render_shipping_plus_page']
516 );
517 }
518
519 public function remove_shipping_confirm_menu(): void {
520 remove_submenu_page( 'shipping_plus', self::SHIPPING_PLUS_SLUG );
521 }
522
523 public function render_shipping_plus_page()
524 {
525 $file = plugin_dir_path(__FILE__) . 'templates/shipping_confirm.php';
526
527 if (file_exists($file)) {
528 include $file;
529 } else {
530 echo '<div class="wrap"><h1>Page not found</h1></div>';
531 }
532 }
533
534 public function hide_shipping_admin_notices(): void
535 {
536 $screen = get_current_screen();
537
538 if ($screen && $screen->id === 'admin_page_makecommerce_shipping_plus') {
539 remove_all_actions('admin_notices');
540 remove_all_actions('all_admin_notices');
541 }
542 }
543
544 public function save_shipping_plus_confirmation(): void
545 {
546 $current_page = isset($_GET['page']) ? sanitize_text_field(wp_unslash($_GET['page'])) : '';
547
548 if (
549 isset($_POST['accept_shipping']) &&
550 $current_page === self::SHIPPING_PLUS_SLUG
551 ) {
552 // Only users who are allowed to see this page may confirm the migration
553 if (!current_user_can(self::SHIPPING_PLUS_CAPABILITY)) {
554 wp_die(
555 esc_html__('You do not have permission to change MakeCommerce settings.', 'wc_makecommerce_domain'),
556 esc_html__('Forbidden', 'wc_makecommerce_domain'),
557 ['response' => 403]
558 );
559 }
560
561 check_admin_referer(self::SHIPPING_PLUS_NONCE_ACTION, self::SHIPPING_PLUS_NONCE_FIELD);
562
563 update_option('mc_shipping_plus_confirmed', 'yes');
564 update_option('mc_shipping_plus', 'yes');
565 update_option('makecommerce_install_status', 'upgrade');
566 $this->mc_shipping_plus_creds_migration();
567 wp_redirect(admin_url('admin.php?page=makecommerce_dashboard'));
568 exit;
569 }
570 }
571
572 /**
573 * Run credentials migration when 'mc_shipping_plus' is turned on
574 *
575 * @return void
576 */
577 private function mc_shipping_plus_creds_migration(): void
578 {
579 $migration_map = [
580 'mc_shop_id' => 'mk_shop_id',
581 'mc_secret_key' => 'mk_private_key',
582 'mc_public_key' => 'mk_public_key',
583 'mc_test_shop_id' => 'mk_test_shop_id',
584 'mc_test_secret_key' => 'mk_test_private_key',
585 'mc_test_public_key' => 'mk_test_public_key',
586 'mc_api_mode' => 'mk_api_type',
587 ];
588
589 foreach ($migration_map as $new_key => $old_key) {
590 $old_value = get_option($old_key, null);
591
592 if ($old_value !== null) {
593 update_option($new_key, $old_value);
594 }
595 }
596 }
597 }