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 / includes / makecommerce.php

makecommerce.php in MakeCommerce for WooCommerce 4.0.6, at includes/makecommerce.php

443 lines 13.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 file that defines the core plugin class
5 *
6 * A class definition that includes attributes and functions used across both the
7 * public-facing side of the site and the admin area.
8 *
9 * @link https://makecommerce.net/
10 * @since 3.0.0
11 *
12 * @package Makecommerce
13 * @subpackage Makecommerce/includes
14 */
15
16 /**
17 * The core plugin class.
18 *
19 * This is used to define internationalization, admin-specific hooks, and
20 * public-facing site hooks.
21 *
22 * Also maintains the unique identifier of this plugin as well as the current
23 * version of the plugin.
24 *
25 * @since 3.0.0
26 * @package Makecommerce
27 * @subpackage Makecommerce/includes
28 * @author Maksekeskus AS <support@maksekeskus.ee>
29 */
30 class MakeCommerce {
31
32 /**
33 * The loader that's responsible for maintaining and registering all hooks that power
34 * the plugin.
35 *
36 * @since 3.0.0
37 * @access protected
38 * @var MakeCommerce\Loader $loader Maintains and registers all hooks for the plugin.
39 */
40 protected $loader;
41
42 /**
43 * The unique identifier of this plugin.
44 *
45 * @since 3.0.0
46 * @access protected
47 * @var string $plugin_name The string used to uniquely identify this plugin.
48 */
49 protected $plugin_name;
50
51 /**
52 * The current version of the plugin.
53 *
54 * @since 3.0.0
55 * @access protected
56 * @var string $version The current version of the plugin.
57 */
58 protected $version;
59
60 /**
61 * Define the core functionality of the plugin.
62 *
63 * Set the plugin name and the plugin version that can be used throughout the plugin.
64 * Load the dependencies, define the locale, and set the hooks for the admin area and
65 * the public-facing side of the site.
66 *
67 * @since 3.0.0
68 */
69 public function __construct() {
70
71 $this->version = MAKECOMMERCE_VERSION;
72
73 $this->plugin_name = 'makecommerce';
74
75 //configure autoloader
76 require_once plugin_dir_path( __FILE__ ) . "autoloader.php";
77
78 $autoLoader = new MakeCommerce\Autoloader;
79 $autoLoader->register();
80
81 //register includes and the complete plugin path as MakeCommerce namespace
82 $autoLoader->addNamespace( 'MakeCommerce', __DIR__ );
83 $autoLoader->addNamespace( 'MakeCommerce', plugin_dir_path( __DIR__ ) );
84
85 //get Maksekeskus API
86 require_once plugin_dir_path( __FILE__ ) . 'vendor/autoload.php';
87 require_once plugin_dir_path( __FILE__ ) . 'vendor/Maksekeskus.php';
88
89 $this->loader = new MakeCommerce\Loader();
90
91 $this->set_locale();
92
93 $plugin_payment = new MakeCommerce\Payment ( $this->get_plugin_name(), $this->get_version(), $this->loader );
94
95 $this->define_api_hooks();
96 $this->define_shipping_hooks();
97 $this->define_cron_hooks();
98 }
99
100 /**
101 * Define the locale for this plugin for internationalization.
102 *
103 * Uses the Makecommerce_i18n class in order to set the domain and to register the hook
104 * with WordPress.
105 *
106 * @since 3.0.0
107 * @access private
108 */
109 private function set_locale() {
110
111 $plugin_i18n = new MakeCommerce\i18n();
112
113 $this->loader->add_action( 'init', $plugin_i18n, 'load_plugin_textdomain' );
114 }
115
116 /**
117 * Register all of the hooks related to cron functionality
118 * of the plugin.
119 *
120 * @since 3.0.0
121 * @access private
122 */
123 private function define_cron_hooks() {
124
125 $plugin_cron = new MakeCommerce\Cron( $this->get_plugin_name(), $this->get_version() );
126
127 //cron define query vars
128 $this->loader->add_filter( 'query_vars', $plugin_cron, 'query_vars' );
129
130 // WP schedule callable action
131 $this->loader->add_action( 'mc_banklinks_update_cron', $plugin_cron, 'update_banklinks' );
132
133 $this->loader->add_action( 'parse_request', $plugin_cron, 'parse_update_vars' );
134 }
135
136 /**
137 * Register all of the hooks related to the api functionality
138 * of the plugin.
139 *
140 * @since 3.0.0
141 * @access private
142 */
143 private function define_api_hooks() {
144
145 $api = new MakeCommerce\API( $this->get_plugin_name(), $this->get_version(), $this->loader );
146
147 //check if CURL is installed
148 $this->loader->add_action( 'admin_notices', $api, 'check_if_curl_is_loaded');
149
150 //Add admin notice if api is not set up
151 if ( !\MakeCommerce::get_api() ) {
152 $this->loader->add_action( 'admin_notices', $api, 'api_info_missing' );
153 }
154
155 //Add admin notice about Shipping+
156 $this->loader->add_action( 'admin_notices', $api, 'shipping_api_notice' );
157
158 //if API type is updated then refresh the cache
159 $this->loader->add_action( 'update_option_mk_api_type', $api, 'mk_delete_api_cache' );
160
161 //add API access menu in woocommerce->settings->advanced (_api version is for WC <= 3.4.0)
162 $this->loader->add_action( 'woocommerce_get_sections_advanced', $api, 'add_woo_menu' );
163 $this->loader->add_action( 'woocommerce_get_sections_api', $api, 'add_woo_menu');
164
165 //add woocommerce admin menu settings for woocommerce->settings->advanced->mk_api (_api version is for WC <= 3.4.0)
166 $this->loader->add_filter( 'woocommerce_get_settings_advanced', $api, 'add_woo_admin_settings', 10, 2 );
167 $this->loader->add_filter( 'woocommerce_get_settings_api', $api, 'add_woo_admin_settings', 10, 2 );
168
169 //add new customer column to orders view, make it sortable and fill with values
170 $this->loader->add_filter( 'manage_edit-shop_order_columns', $api, 'add_ordersview_paymentmethod_column' );
171 $this->loader->add_filter( 'manage_edit-shop_order_sortable_columns', $api, 'make_ordersview_paymentmethod_column_sortable' );
172 $this->loader->add_action( 'manage_shop_order_posts_custom_column', $api, 'fill_ordersview_paymentmethod_column', 10, 2 );
173
174 //various hooks that update banklinks
175 $this->loader->add_action( 'wp_login', $api, 'admin_login', 10, 2 );
176 $this->loader->add_action( 'woocommerce_settings_saved', $api, 'save_settings', 30, 0 );
177
178 //adds settings link to plugins page
179 $this->loader->add_filter( 'plugin_action_links_makecommerce/makecommerce.php', $api, 'add_plugin_settings_link' );
180
181 $this->loader->add_filter( 'woocommerce_admin_field_api_javascript_ui', $api, 'api_javascript_ui' );
182
183 // Shipping plus confirmation page in Admin panel and related hooks to it
184 $this->loader->add_action('admin_enqueue_scripts', $api, 'enqueue_shipping_plus_assets');
185 $this->loader->add_action('admin_menu', $api, 'add_shipping_plus_admin_page');
186 $this->loader->add_action( 'admin_head', $api, 'remove_shipping_confirm_menu', 15 );
187 $this->loader->add_action( 'admin_head', $api, 'hide_shipping_admin_notices' );
188 $this->loader->add_action( 'admin_init', $api, 'save_shipping_plus_confirmation' );
189 }
190
191 /**
192 * Register all of the hooks related to the public-facing functionality
193 * of the plugin.
194 *
195 * @since 3.0.0
196 * @access private
197 */
198 private function define_shipping_hooks() {
199
200 $plugin_shipping = new MakeCommerce\Shipping( $this->get_plugin_name(), $this->get_version(), $this->loader );
201
202 //load scripts and styles
203 $this->loader->add_action( 'wp_enqueue_scripts', $plugin_shipping, 'enqueue_styles' );
204 $this->loader->add_action( 'wp_enqueue_scripts', $plugin_shipping, 'enqueue_scripts' );
205
206 //load scripts and styles
207 $this->loader->add_action( 'admin_enqueue_scripts', $plugin_shipping, 'enqueue_styles' );
208 $this->loader->add_action( 'admin_enqueue_scripts', $plugin_shipping, 'enqueue_scripts' );
209
210 //Initialize shipping
211 $this->loader->add_action( 'woocommerce_shipping_init', $plugin_shipping, 'initalize' );
212
213 //add shipping methods
214 $this->loader->add_filter( 'woocommerce_shipping_methods', $plugin_shipping, 'add_shipping_methods' );
215
216 $this->loader->add_filter( 'woocommerce_checkout_update_order_review', $plugin_shipping, 'clear_shipping_rates_cache' );
217 $this->loader->add_filter( 'posts_where', $plugin_shipping, 'shipping_filter', 10, 2 );
218
219 $this->loader->add_filter( 'woocommerce_order_status_processing', $plugin_shipping, 'register_shipment' );
220
221 $this->loader->add_action( 'woocommerce_update_order', $plugin_shipping, 'set_parcel_machine_meta' );
222 $this->loader->add_action( 'woocommerce_update_order', $plugin_shipping, 'update_courier_meta' );
223
224 $this->loader->add_action( 'wp_ajax_update_map_center', $plugin_shipping, 'update_map_center' );
225 $this->loader->add_action( 'wp_ajax_nopriv_update_map_center', $plugin_shipping, 'update_map_center' );
226
227 $this->loader->add_action( 'update_option_mc_map_geocoding_api_key', $plugin_shipping, 'mc_check_api_key' );
228 $this->loader->add_action( 'admin_notices', $plugin_shipping, 'invalid_api_key_notification' );
229 }
230
231 /**
232 * Run the loader to execute all of the hooks with WordPress.
233 *
234 * @since 3.0.0
235 */
236 public function run() {
237
238 $this->loader->run();
239 }
240
241 /**
242 * The name of the plugin used to uniquely identify it within the context of
243 * WordPress and to define internationalization functionality.
244 *
245 * @since 3.0.0
246 * @return string The name of the plugin.
247 */
248 public function get_plugin_name() {
249
250 return $this->plugin_name;
251 }
252
253 /**
254 * The reference to the class that orchestrates the hooks with the plugin.
255 *
256 * @since 3.0.0
257 * @return Loader Orchestrates the hooks of the plugin.
258 */
259 public function get_loader() {
260
261 return $this->loader;
262 }
263
264 /**
265 * Retrieve the version number of the plugin.
266 *
267 * @since 3.0.0
268 * @return string The version number of the plugin.
269 */
270 public function get_version() {
271
272 return $this->version;
273 }
274
275 /**
276 * Get the HTML version of the logo
277 *
278 * @since 3.0.0
279 */
280 public static function get_logo_html() {
281
282 return '
283 <div class="makecommerce-info">
284 <div class="makecommerce-logo">
285 <a target="_blank" href="http://maksekeskus.ee"><img src="'. plugins_url( '../payment/gateway/woocommerce/images/makecommerce_logo_en.svg', __FILE__ ) .'" class="makecommerce-logo"></a>
286 </div>
287
288 <div class="makecommerce-links">
289 <div class="makecommerce-link"><a target="_blank" href="https://merchant.maksekeskus.ee">Merchant Portal</a></div>
290 <div class="makecommerce-link"><a target="_blank" href="https://makecommerce.net/">makecommerce.net</a></div>
291 <div class="makecommerce-link"><a target="_blank" href="http://maksekeskus.ee">maksekeskus.ee</a></div>
292 </div>
293 </div>
294 ';
295 }
296
297 /**
298 * Returns true if WooCommerce version is 3.4.0 or higher (different links in admin)
299 *
300 * @since 3.0.0
301 */
302 public static function new_woo_version() {
303
304 global $woocommerce;
305
306 if ( version_compare( $woocommerce->version, "3.4.0", ">=" ) ) {
307 return true;
308 }
309
310 return false;
311 }
312
313 /**
314 * Checks if API is set.
315 *
316 * @since 3.0.0
317 */
318 public static function is_api_set() {
319
320 return self::get_api( true );
321 }
322
323 /**
324 * Displays error in admin that API access is not configured properly
325 *
326 * @since 3.0.0
327 */
328 public function mk_admin_error() {
329
330 printf( '<div class="%1$s"><p>%2$s</p></div>', 'notice notice-error', __( 'Please check that you have configured correctly MakeCommerce API accesses', 'wc_makecommerce_domain' ) );
331 }
332
333 /**
334 * Returns MK api, unless check is set to true, then returns if api is set
335 *
336 * @since 3.0.0
337 */
338 public static function get_api( $check = false ) {
339
340 $mk_api_type = get_option( 'mk_api_type', false );
341
342 if ( !$mk_api_type ) {
343 return false;
344 }
345
346 $key_prefix = '';
347 if ( $mk_api_type !== 'live' ) {
348 $key_prefix = $mk_api_type.'_';
349 }
350
351 $mk_shop_id = get_option( 'mk_'.$key_prefix.'shop_id', '' );
352 $mk_public_key = get_option( 'mk_'.$key_prefix.'public_key', '' );
353 $mk_private_key = get_option( 'mk_'.$key_prefix.'private_key', '' );
354
355 if ( !$mk_shop_id || !$mk_public_key || !$mk_shop_id ) {
356 return false;
357 }
358
359 //if this was just a check, return true
360 if ( $check === true ) {
361 return true;
362 }
363
364 global $MKAPI;
365 $MKAPI = new \Maksekeskus\Maksekeskus( $mk_shop_id, $mk_public_key, $mk_private_key, $mk_api_type === 'live' ? false : true );
366
367 return $MKAPI;
368 }
369
370 /**
371 * Sets up shop getConfig request parameters
372 *
373 * @since 3.0.0
374 */
375 public static function config_request_parameters( $module = "" ) {
376
377 return array(
378 'environment' => json_encode( array(
379 'system' => array( 'wordpress' => get_bloginfo( 'version' ), "php" => phpversion() ),
380 'platform' => 'woocommerce '. WC_VERSION,
381 'module' => $module,
382 )),
383 );
384 }
385
386 /**
387 * Helpful tool for debugging
388 *
389 * @since 3.0.0
390 */
391 public static function prd() {
392
393 //get all arguments
394 $args = func_get_args();
395
396 echo "<pre>";
397
398 foreach ( $args as $arg ) {
399
400 $printR = print_r( $arg, true );
401
402 error_log( $printR );
403
404 echo $printR . "\r\n\r\n\r\n";
405 }
406
407 echo "</pre>";
408
409 die();
410 }
411
412
413 public static $scriptLoaderAttributes = array();
414
415 /**
416 * Enqueues javascript with parameters.
417 *
418 * @since 3.0.9
419 */
420 public static function mc_enqueue_script( $handle, $src, $data = [], $deps = [], $external = false ) {
421
422 //check if the script is already enqueued. Fixes issue for misbehaving plugins who the queue again which causes inline script to be added more than once.
423 //https://github.com/wp-media/wp-rocket/issues/3125
424 if ( wp_script_is( $handle, 'enqueued' ) ) {
425 return;
426 }
427
428 $version = null;
429 if ( !$external ) {
430
431 $version = filemtime( $src );
432 $src = plugin_dir_url( $src ) . basename( $src );
433 }
434
435 wp_register_script( $handle, $src, $deps, $version );
436
437 if ( !empty( $data ) ) {
438 wp_add_inline_script( $handle, 'const ' . $handle . ' = ' . json_encode( $data ), 'before' );
439 }
440
441 wp_enqueue_script( $handle );
442 }
443 }