PluginProbe
MakeCommerce for WooCommerce / 3.2.0
MakeCommerce for WooCommerce v3.2.0
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.2.0, at includes/makecommerce.php

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