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

395 lines 13.3 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 wp_enqueue_script( $this->plugin_name . "api-admin", plugin_dir_url(__FILE__) . 'js/api.js', array( 'jquery' ), $this->version );
199 }
200
201 /**
202 * Add admin settings for api in woocommerce->settings->advanced->Makcommerce API
203 *
204 * @since 3.0.0
205 */
206 public function add_woo_admin_settings( $settings ) {
207
208 /**
209 * Prevent the settings from being loaded if we are not at actually the page
210 */
211 global $current_section;
212
213 if ( $current_section !== 'mk_api' ) {
214 return $settings;
215 }
216
217 /**
218 * Return array with fields for settings
219 */
220 return array(
221 array( 'type' => 'title', 'desc' => \MakeCommerce::get_logo_html() ),
222 array(
223 'type' => 'title',
224 'title' => __('MakeCommerce API access credentials', 'wc_makecommerce_domain'),
225 'desc' => __('To use MakeCommerce/Maksekeskus services you need to enter API credentials below here <br/> <br/>', 'wc_makecommerce_domain').
226 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'),
227 'id' => 'mk_api_settings'
228 ),
229 array(
230 'type' => 'select',
231 'title' => __('Current environment', 'wc_makecommerce_domain'),
232 'desc' => __('See more about <a href="https://maksekeskus.ee/en/for-developers/test-environment/">MakeCommerce Test environment</a>', 'wc_makecommerce_domain'),
233 'default' => 'live',
234 'options' => array(
235 'live' => __('Live', 'wc_makecommerce_domain'),
236 'test' => __('Test', 'wc_makecommerce_domain'),
237 ),
238 'id' => 'mk_api_type'
239 ),
240 array(
241 'id' => 'mk_shop_id',
242 'type' => 'text',
243 'title' => __('Shop ID (live)', 'wc_makecommerce_domain'),
244 'desc' => __('Get it from <a href="https://merchant.maksekeskus.ee/api.html" target="_blank">Merchant Portal</a>','wc_makecommerce_domain'),
245 'class' => 'input-text regular-input',
246 ),
247 array(
248 'id' => 'mk_private_key',
249 'type' => 'text',
250 'title' => __('Secret key (live)', 'wc_makecommerce_domain'),
251 'class' => 'input-text regular-input',
252 ),
253 array(
254 'id' => 'mk_public_key',
255 'type' => 'text',
256 'title' => __('Publishable key (live)', 'wc_makecommerce_domain'),
257 'class' => 'input-text regular-input',
258 ),
259 array(
260 'id' => 'mk_test_shop_id',
261 'type' => 'text',
262 'title' => __('Shop ID (test)', 'wc_makecommerce_domain'),
263 'class' => 'input-text regular-input',
264 'default' => 'f64b4f20-5ef9-4b7b-a6fa-d623d87f0b9c',
265 'desc' => __('Get it from <a href="https://merchant.test.maksekeskus.ee/api.html" target="_blank">Merchant Portal Test</a>','wc_makecommerce_domain'),
266 ),
267 array(
268 'id' => 'mk_test_private_key',
269 'type' => 'text',
270 'title' => __('Secret key (test)', 'wc_makecommerce_domain'),
271 'default' => 'MPjcVMoRZPAucsTuGK5ZukOlV7BlzgSvXOowJUkk9IFSQPVooRwcVOxzz3mhEpgM',
272 'class' => 'input-text regular-input',
273 ),
274 array(
275 'id' => 'mk_test_public_key',
276 'type' => 'text',
277 'title' => __('Publishable key (test)', 'wc_makecommerce_domain'),
278 'default' => '7Hog41ci2mKkmtviMycWxpNx14pNP70m',
279 'class' => 'input-text regular-input',
280 ),
281 array(
282 'type' => 'select',
283 'title' => __('Label print format', 'wc_makecommerce_domain'),
284 'desc' => __('In which format should shipping labels be printed. (*make sure you have API access to see more options)', 'wc_makecommerce_domain'),
285 'default' => 'A4',
286 'options' => $this->label_formats(),
287 'id' => 'mk_label_format'
288 ),
289 array('type' => 'sectionend', 'id' => 'mk_api_settings'),
290 array(
291 'type' => 'title',
292 'desc' => '<br><hr><br>',
293 ),
294 array(
295 'id' => 'mk_module_title',
296 'type' => 'title',
297 'title' => __('Makecommerce modules', 'wc_makecommerce_domain'),
298 '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'),
299 'class' => '',
300 ),
301 array(
302 'id' => 'mk_transport_apt_omniva',
303 'type' => 'checkbox',
304 'default' => 'yes',
305 'title' => __('Omniva Parcel Machine', 'wc_makecommerce_domain'),
306 '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')).')',
307 'class' => '',
308 ),
309 array(
310 'id' => 'mk_transport_apt_smartpost',
311 'type' => 'checkbox',
312 'default' => 'yes',
313 'title' => __('Smartpost Parcel Machine', 'wc_makecommerce_domain'),
314 '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')).')',
315 'class' => '',
316 ),
317 array(
318 'id' => 'mk_transport_apt_dpd',
319 'type' => 'checkbox',
320 'default' => 'no',
321 'title' => __('DPD', 'wc_makecommerce_domain') . ' ' . __('parcel machine', 'wc_makecommerce_domain'),
322 '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')).')',
323 'class' => '',
324 ),
325 array(
326 'id' => 'mk_transport_apt_lp_express_lt',
327 'type' => 'checkbox',
328 'default' => 'no',
329 'title' => __('LP Express', 'wc_makecommerce_domain') . ' ' . __('parcel machine', 'wc_makecommerce_domain'),
330 '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')).')',
331 'class' => '',
332 ),
333 array(
334 'id' => 'mk_transport_courier_omniva',
335 'type' => 'checkbox',
336 'default' => 'yes',
337 'title' => __('Omniva Courier', 'wc_makecommerce_domain'),
338 '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')).')',
339 'class' => '',
340 ),
341 array(
342 'id' => 'mk_transport_courier_smartpost',
343 'type' => 'checkbox',
344 'default' => 'yes',
345 'title' => __('Smartpost Courier', 'wc_makecommerce_domain'),
346 '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')).')',
347 'class' => '',
348 ),
349 array(
350 'id' => 'mk_javascript_ui',
351 'type' => 'api_javascript_ui',
352 ),
353 array('type' => 'sectionend', 'id' => 'mk_api_settings')
354 );
355 }
356
357 /**
358 * Function for resetting machines and payment methods when API type is changed
359 *
360 * @since 3.2.1
361 */
362 public function mk_delete_api_cache() {
363
364 global $wpdb;
365 $tableName = $wpdb->prefix . MAKECOMMERCE_TABLENAME;
366
367 // Delete machines' cache and timer
368 delete_option( 'mk_machines_expires' );
369 delete_option( 'mk_machines_cache' );
370
371 // Delete payment methods
372 $wpdb->query( 'TRUNCATE TABLE `'.$tableName.'`' );
373 }
374
375 /**
376 * Notice for missing API information
377 *
378 * @since 3.0.0
379 */
380 public function api_info_missing() {
381
382 ?>
383 <div class="notice notice-error is-dismissible">
384 <p>
385 <?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' ); ?>
386 <?php if ( \MakeCommerce::new_woo_version() ) { ?>
387 <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>
388 <?php } else { ?>
389 <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>
390 <?php } ?>
391 </p>
392 </div>
393 <?php
394 }
395 }