PluginProbe
MakeCommerce for WooCommerce / 3.0.8
MakeCommerce for WooCommerce v3.0.8
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 3.0.8, at api/api.php

409 lines 13.7 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 echo get_post_meta( $post_id, '_makecommerce_preselected_method', true );
142 }
143 }
144
145 /**
146 * Update banklinks when an admin logs in
147 *
148 * @since 3.0.0
149 */
150 public function admin_login( $user_login, $user ) {
151
152 if ( user_can( $user, 'administrator' ) ) {
153 Payment::update_banklinks();
154 }
155 }
156
157 /**
158 * Adds settings link to plugins page
159 *
160 * @since 3.0.0
161 */
162 public function add_plugin_settings_link( $links ) {
163
164 $settings_link = '<a href="admin.php?page=wc-settings&tab=api&section=mk_api">API '.__('Settings').'</a>';
165
166 if ( \MakeCommerce::new_woo_version() ) {
167 $settings_link = '<a href="admin.php?page=wc-settings&tab=advanced&section=mk_api">API '.__('Settings').'</a>';
168 }
169
170 array_unshift( $links, $settings_link );
171
172 return $links;
173 }
174
175 /**
176 * Updates banklinks if current section is not in mk_api
177 *
178 * @since 3.0.0
179 */
180 public function save_settings() {
181
182 global $current_section;
183
184 if ( $current_section !== 'mk_api' ) {
185 return;
186 }
187
188 Payment::update_banklinks();
189 }
190
191 /**
192 * Test and live switching in API settings (jQuery)
193 *
194 * @since 3.0.0
195 */
196 public function api_javascript_ui( $data ) {
197
198 ?>
199 <script type="text/javascript">
200 jQuery(document).ready(function($) {
201
202 var env_select = $('#mk_api_type');
203 var test_fields = ['mk_test_shop_id', 'mk_test_private_key', 'mk_test_public_key'];
204 var live_fields = ['mk_shop_id', 'mk_private_key', 'mk_public_key'];
205
206 env_select.on('change', function(){
207 hideFields($(this).val());
208 });
209
210 hideFields(env_select.val());
211
212 function hideFields(type) {
213 var hide = type == 'live' ? test_fields : live_fields;
214 var show = type == 'live' ? live_fields : test_fields;
215 $.each(hide, function(i, elem) {
216 $('#'+elem).closest('tr').hide();
217 });
218 $.each(show, function(i, elem) {
219 $('#'+elem).closest('tr').show();
220 });
221 }
222 });
223 </script>
224 <?php
225 }
226
227 /**
228 * Add admin settings for api in woocommerce->settings->advanced->Makcommerce API
229 *
230 * @since 3.0.0
231 */
232 public function add_woo_admin_settings( $settings ) {
233
234 /**
235 * Prevent the settings from being loaded if we are not at actually the page
236 */
237 global $current_section;
238
239 if ( $current_section !== 'mk_api' ) {
240 return $settings;
241 }
242
243 /**
244 * Return array with fields for settings
245 */
246 return array(
247 array( 'type' => 'title', 'desc' => \MakeCommerce::get_logo_html() ),
248 array(
249 'type' => 'title',
250 'title' => __('MakeCommerce API access credentials', 'wc_makecommerce_domain'),
251 'desc' => __('To use MakeCommerce/Maksekeskus services you need to enter API credentials below here <br/> <br/>', 'wc_makecommerce_domain').
252 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'),
253 'id' => 'mk_api_settings'
254 ),
255 array(
256 'type' => 'select',
257 'title' => __('Current environment', 'wc_makecommerce_domain'),
258 'desc' => __('See more about <a href="https://maksekeskus.ee/en/for-developers/test-environment/">MakeCommerce Test environment</a>', 'wc_makecommerce_domain'),
259 'default' => 'live',
260 'options' => array(
261 'live' => __('Live', 'wc_makecommerce_domain'),
262 'test' => __('Test', 'wc_makecommerce_domain'),
263 ),
264 'id' => 'mk_api_type'
265 ),
266 array(
267 'id' => 'mk_shop_id',
268 'type' => 'text',
269 'title' => __('Shop ID (live)', 'wc_makecommerce_domain'),
270 'desc' => __('Get it from <a href="https://merchant.maksekeskus.ee/api.html" target="_blank">Merchant Portal</a>','wc_makecommerce_domain'),
271 'class' => 'input-text regular-input',
272 ),
273 array(
274 'id' => 'mk_private_key',
275 'type' => 'text',
276 'title' => __('Secret key (live)', 'wc_makecommerce_domain'),
277 'class' => 'input-text regular-input',
278 ),
279 array(
280 'id' => 'mk_public_key',
281 'type' => 'text',
282 'title' => __('Publishable key (live)', 'wc_makecommerce_domain'),
283 'class' => 'input-text regular-input',
284 ),
285 array(
286 'id' => 'mk_test_shop_id',
287 'type' => 'text',
288 'title' => __('Shop ID (test)', 'wc_makecommerce_domain'),
289 'class' => 'input-text regular-input',
290 'default' => 'f64b4f20-5ef9-4b7b-a6fa-d623d87f0b9c',
291 'desc' => __('Get it from <a href="https://merchant.test.maksekeskus.ee/api.html" target="_blank">Merchant Portal Test</a>','wc_makecommerce_domain'),
292 ),
293 array(
294 'id' => 'mk_test_private_key',
295 'type' => 'text',
296 'title' => __('Secret key (test)', 'wc_makecommerce_domain'),
297 'default' => 'MPjcVMoRZPAucsTuGK5ZukOlV7BlzgSvXOowJUkk9IFSQPVooRwcVOxzz3mhEpgM',
298 'class' => 'input-text regular-input',
299 ),
300 array(
301 'id' => 'mk_test_public_key',
302 'type' => 'text',
303 'title' => __('Publishable key (test)', 'wc_makecommerce_domain'),
304 'default' => '7Hog41ci2mKkmtviMycWxpNx14pNP70m',
305 'class' => 'input-text regular-input',
306 ),
307 array(
308 'type' => 'select',
309 'title' => __('Label print format', 'wc_makecommerce_domain'),
310 'desc' => __('In which format should shipping labels be printed. (*make sure you have API access to see more options)', 'wc_makecommerce_domain'),
311 'default' => 'A4',
312 'options' => $this->label_formats(),
313 'id' => 'mk_label_format'
314 ),
315 array('type' => 'sectionend', 'id' => 'mk_api_settings'),
316 array(
317 'type' => 'title',
318 'desc' => '<br><hr><br>',
319 ),
320 array(
321 'id' => 'mk_module_title',
322 'type' => 'title',
323 'title' => __('Makecommerce modules', 'wc_makecommerce_domain'),
324 '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'),
325 'class' => '',
326 ),
327 array(
328 'id' => 'mk_transport_apt_omniva',
329 'type' => 'checkbox',
330 'default' => 'yes',
331 'title' => __('Omniva Parcel Machine', 'wc_makecommerce_domain'),
332 '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')).')',
333 'class' => '',
334 ),
335 array(
336 'id' => 'mk_transport_apt_smartpost',
337 'type' => 'checkbox',
338 'default' => 'yes',
339 'title' => __('Smartpost Parcel Machine', 'wc_makecommerce_domain'),
340 'desc' => __('enable Smartpost 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')).')',
341 'class' => '',
342 ),
343 array(
344 'id' => 'mk_transport_apt_dpd',
345 'type' => 'checkbox',
346 'default' => 'no',
347 'title' => __('DPD', 'wc_makecommerce_domain') . ' ' . __('parcel machine', 'wc_makecommerce_domain'),
348 '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')).')',
349 'class' => '',
350 ),
351 array(
352 'id' => 'mk_transport_courier_omniva',
353 'type' => 'checkbox',
354 'default' => 'yes',
355 'title' => __('Omniva Courier', 'wc_makecommerce_domain'),
356 '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')).')',
357 'class' => '',
358 ),
359 array(
360 'id' => 'mk_transport_courier_smartpost',
361 'type' => 'checkbox',
362 'default' => 'yes',
363 'title' => __('Smartpost Courier', 'wc_makecommerce_domain'),
364 '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')).')',
365 'class' => '',
366 ),
367 array(
368 'id' => 'mk_checkout_sco',
369 'type' => 'checkbox',
370 'title' => __('SimpleCheckout', 'wc_makecommerce_domain'),
371 'desc' => __('enable SimpleCheckout&trade; Checkout method', 'wc_makecommerce_domain').' ('. sprintf(__('<a href="%s">module settings</a>', 'wc_makecommerce_domain'), admin_url('admin.php?page=wc-settings&tab=checkout&section=makecommerce_sc')).')',
372 'class' => '',
373 ),
374 array(
375 'id' => 'mk_product_slice',
376 'type' => 'checkbox',
377 'title' => __('Indivy Slice 3 information in product view', 'wc_makecommerce_domain'),
378 'desc' => __('enable Indivy Slice 3 in product view', 'wc_makecommerce_domain'),
379 'class' => '',
380 ),
381 array(
382 'id' => 'mk_javascript_ui',
383 'type' => 'api_javascript_ui',
384 ),
385 array('type' => 'sectionend', 'id' => 'mk_api_settings')
386 );
387 }
388
389 /**
390 * Notice for missing API information
391 *
392 * @since 3.0.0
393 */
394 public function api_info_missing() {
395
396 ?>
397 <div class="notice notice-error is-dismissible">
398 <p>
399 <?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' ); ?>
400 <?php if ( \MakeCommerce::new_woo_version() ) { ?>
401 <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>
402 <?php } else { ?>
403 <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>
404 <?php } ?>
405 </p>
406 </div>
407 <?php
408 }
409 }