PluginProbe
MakeCommerce for WooCommerce / 4.0.6
MakeCommerce for WooCommerce v4.0.6
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 4.0.6, at api/api.php

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