PluginProbe
MakeCommerce for WooCommerce / 3.5.1
MakeCommerce for WooCommerce v3.5.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 / includes / makecommerce.php

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

433 lines 12.5 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( 'plugins_loaded', $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 //if API type is updated then refresh the cache
156 $this->loader->add_action( 'update_option_mk_api_type', $api, 'mk_delete_api_cache' );
157
158 //add API access menu in woocommerce->settings->advanced (_api version is for WC <= 3.4.0)
159 $this->loader->add_action( 'woocommerce_get_sections_advanced', $api, 'add_woo_menu' );
160 $this->loader->add_action( 'woocommerce_get_sections_api', $api, 'add_woo_menu');
161
162 //add woocommerce admin menu settings for woocommerce->settings->advanced->mk_api (_api version is for WC <= 3.4.0)
163 $this->loader->add_filter( 'woocommerce_get_settings_advanced', $api, 'add_woo_admin_settings', 10, 2 );
164 $this->loader->add_filter( 'woocommerce_get_settings_api', $api, 'add_woo_admin_settings', 10, 2 );
165
166 //add new customer column to orders view, make it sortable and fill with values
167 $this->loader->add_filter( 'manage_edit-shop_order_columns', $api, 'add_ordersview_paymentmethod_column' );
168 $this->loader->add_filter( 'manage_edit-shop_order_sortable_columns', $api, 'make_ordersview_paymentmethod_column_sortable' );
169 $this->loader->add_action( 'manage_shop_order_posts_custom_column', $api, 'fill_ordersview_paymentmethod_column', 10, 2 );
170
171 //various hooks that update banklinks
172 $this->loader->add_action( 'wp_login', $api, 'admin_login', 10, 2 );
173 $this->loader->add_action( 'woocommerce_settings_saved', $api, 'save_settings', 30, 0 );
174
175 //adds settings link to plugins page
176 $this->loader->add_filter( 'plugin_action_links_makecommerce/makecommerce.php', $api, 'add_plugin_settings_link' );
177
178 $this->loader->add_filter( 'woocommerce_admin_field_api_javascript_ui', $api, 'api_javascript_ui' );
179 }
180
181 /**
182 * Register all of the hooks related to the public-facing functionality
183 * of the plugin.
184 *
185 * @since 3.0.0
186 * @access private
187 */
188 private function define_shipping_hooks() {
189
190 $plugin_shipping = new MakeCommerce\Shipping( $this->get_plugin_name(), $this->get_version(), $this->loader );
191
192 //load scripts and styles
193 $this->loader->add_action( 'wp_enqueue_scripts', $plugin_shipping, 'enqueue_styles' );
194 $this->loader->add_action( 'wp_enqueue_scripts', $plugin_shipping, 'enqueue_scripts' );
195
196 //load scripts and styles
197 $this->loader->add_action( 'admin_enqueue_scripts', $plugin_shipping, 'enqueue_styles' );
198 $this->loader->add_action( 'admin_enqueue_scripts', $plugin_shipping, 'enqueue_scripts' );
199
200 //Initialize shipping
201 $this->loader->add_action( 'woocommerce_shipping_init', $plugin_shipping, 'initalize' );
202
203 //add shipping methods
204 $this->loader->add_filter( 'woocommerce_shipping_methods', $plugin_shipping, 'add_shipping_methods' );
205
206 $this->loader->add_filter( 'woocommerce_checkout_update_order_review', $plugin_shipping, 'clear_shipping_rates_cache' );
207 $this->loader->add_filter( 'posts_where', $plugin_shipping, 'shipping_filter', 10, 2 );
208
209 $this->loader->add_filter( 'woocommerce_order_status_processing', $plugin_shipping, 'register_shipment' );
210
211 $this->loader->add_action( 'woocommerce_update_order', $plugin_shipping, 'set_parcel_machine_meta' );
212 $this->loader->add_action( 'woocommerce_update_order', $plugin_shipping, 'update_courier_meta' );
213
214 $this->loader->add_action( 'wp_ajax_update_map_center', $plugin_shipping, 'update_map_center' );
215 $this->loader->add_action( 'wp_ajax_nopriv_update_map_center', $plugin_shipping, 'update_map_center' );
216
217 $this->loader->add_action( 'update_option_mc_map_geocoding_api_key', $plugin_shipping, 'mc_check_api_key' );
218 $this->loader->add_action( 'admin_notices', $plugin_shipping, 'invalid_api_key_notification' );
219 }
220
221 /**
222 * Run the loader to execute all of the hooks with WordPress.
223 *
224 * @since 3.0.0
225 */
226 public function run() {
227
228 $this->loader->run();
229 }
230
231 /**
232 * The name of the plugin used to uniquely identify it within the context of
233 * WordPress and to define internationalization functionality.
234 *
235 * @since 3.0.0
236 * @return string The name of the plugin.
237 */
238 public function get_plugin_name() {
239
240 return $this->plugin_name;
241 }
242
243 /**
244 * The reference to the class that orchestrates the hooks with the plugin.
245 *
246 * @since 3.0.0
247 * @return Loader Orchestrates the hooks of the plugin.
248 */
249 public function get_loader() {
250
251 return $this->loader;
252 }
253
254 /**
255 * Retrieve the version number of the plugin.
256 *
257 * @since 3.0.0
258 * @return string The version number of the plugin.
259 */
260 public function get_version() {
261
262 return $this->version;
263 }
264
265 /**
266 * Get the HTML version of the logo
267 *
268 * @since 3.0.0
269 */
270 public static function get_logo_html() {
271
272 return '
273 <div class="makecommerce-info">
274 <div class="makecommerce-logo">
275 <a target="_blank" href="http://maksekeskus.ee"><img src="'. plugins_url( '../payment/gateway/woocommerce/images/makecommerce_logo_en.svg', __FILE__ ) .'" class="makecommerce-logo"></a>
276 </div>
277
278 <div class="makecommerce-links">
279 <div class="makecommerce-link"><a target="_blank" href="https://merchant.maksekeskus.ee">Merchant Portal</a></div>
280 <div class="makecommerce-link"><a target="_blank" href="https://makecommerce.net/">makecommerce.net</a></div>
281 <div class="makecommerce-link"><a target="_blank" href="http://maksekeskus.ee">maksekeskus.ee</a></div>
282 </div>
283 </div>
284 ';
285 }
286
287 /**
288 * Returns true if WooCommerce version is 3.4.0 or higher (different links in admin)
289 *
290 * @since 3.0.0
291 */
292 public static function new_woo_version() {
293
294 global $woocommerce;
295
296 if ( version_compare( $woocommerce->version, "3.4.0", ">=" ) ) {
297 return true;
298 }
299
300 return false;
301 }
302
303 /**
304 * Checks if API is set.
305 *
306 * @since 3.0.0
307 */
308 public static function is_api_set() {
309
310 return self::get_api( true );
311 }
312
313 /**
314 * Displays error in admin that API access is not configured properly
315 *
316 * @since 3.0.0
317 */
318 public function mk_admin_error() {
319
320 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' ) );
321 }
322
323 /**
324 * Returns MK api, unless check is set to true, then returns if api is set
325 *
326 * @since 3.0.0
327 */
328 public static function get_api( $check = false ) {
329
330 $mk_api_type = get_option( 'mk_api_type', false );
331
332 if ( !$mk_api_type ) {
333 return false;
334 }
335
336 $key_prefix = '';
337 if ( $mk_api_type !== 'live' ) {
338 $key_prefix = $mk_api_type.'_';
339 }
340
341 $mk_shop_id = get_option( 'mk_'.$key_prefix.'shop_id', '' );
342 $mk_public_key = get_option( 'mk_'.$key_prefix.'public_key', '' );
343 $mk_private_key = get_option( 'mk_'.$key_prefix.'private_key', '' );
344
345 if ( !$mk_shop_id || !$mk_public_key || !$mk_shop_id ) {
346 return false;
347 }
348
349 //if this was just a check, return true
350 if ( $check === true ) {
351 return true;
352 }
353
354 global $MKAPI;
355 $MKAPI = new \Maksekeskus\Maksekeskus( $mk_shop_id, $mk_public_key, $mk_private_key, $mk_api_type === 'live' ? false : true );
356
357 return $MKAPI;
358 }
359
360 /**
361 * Sets up shop getConfig request parameters
362 *
363 * @since 3.0.0
364 */
365 public static function config_request_parameters( $module = "" ) {
366
367 return array(
368 'environment' => json_encode( array(
369 'system' => array( 'wordpress' => get_bloginfo( 'version' ), "php" => phpversion() ),
370 'platform' => 'woocommerce '. WC_VERSION,
371 'module' => $module,
372 )),
373 );
374 }
375
376 /**
377 * Helpful tool for debugging
378 *
379 * @since 3.0.0
380 */
381 public static function prd() {
382
383 //get all arguments
384 $args = func_get_args();
385
386 echo "<pre>";
387
388 foreach ( $args as $arg ) {
389
390 $printR = print_r( $arg, true );
391
392 error_log( $printR );
393
394 echo $printR . "\r\n\r\n\r\n";
395 }
396
397 echo "</pre>";
398
399 die();
400 }
401
402
403 public static $scriptLoaderAttributes = array();
404
405 /**
406 * Enqueues javascript with parameters.
407 *
408 * @since 3.0.9
409 */
410 public static function mc_enqueue_script( $handle, $src, $data = [], $deps = [], $external = false ) {
411
412 //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.
413 //https://github.com/wp-media/wp-rocket/issues/3125
414 if ( wp_script_is( $handle, 'enqueued' ) ) {
415 return;
416 }
417
418 $version = null;
419 if ( !$external ) {
420
421 $version = filemtime( $src );
422 $src = plugin_dir_url( $src ) . basename( $src );
423 }
424
425 wp_register_script( $handle, $src, $deps, $version );
426
427 if ( !empty( $data ) ) {
428 wp_add_inline_script( $handle, 'const ' . $handle . ' = ' . json_encode( $data ), 'before' );
429 }
430
431 wp_enqueue_script( $handle );
432 }
433 }