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

429 lines 12.1 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 }
210
211 /**
212 * Run the loader to execute all of the hooks with WordPress.
213 *
214 * @since 3.0.0
215 */
216 public function run() {
217
218 $this->loader->run();
219 }
220
221 /**
222 * The name of the plugin used to uniquely identify it within the context of
223 * WordPress and to define internationalization functionality.
224 *
225 * @since 3.0.0
226 * @return string The name of the plugin.
227 */
228 public function get_plugin_name() {
229
230 return $this->plugin_name;
231 }
232
233 /**
234 * The reference to the class that orchestrates the hooks with the plugin.
235 *
236 * @since 3.0.0
237 * @return Loader Orchestrates the hooks of the plugin.
238 */
239 public function get_loader() {
240
241 return $this->loader;
242 }
243
244 /**
245 * Retrieve the version number of the plugin.
246 *
247 * @since 3.0.0
248 * @return string The version number of the plugin.
249 */
250 public function get_version() {
251
252 return $this->version;
253 }
254
255 /**
256 * Get the HTML version of the logo
257 *
258 * @since 3.0.0
259 */
260 public static function get_logo_html() {
261
262 return '
263 <div class="makecommerce-info">
264 <div class="makecommerce-logo">
265 <a target="_blank" href="http://maksekeskus.ee"><img src="'. plugins_url( '../payment/gateway/woocommerce/images/makecommerce_logo_en.svg', __FILE__ ) .'" class="makecommerce-logo"></a>
266 </div>
267
268 <div class="makecommerce-links">
269 <div class="makecommerce-link"><a target="_blank" href="https://merchant.maksekeskus.ee">Merchant Portal</a></div>
270 <div class="makecommerce-link"><a target="_blank" href="https://makecommerce.net/">makecommerce.net</a></div>
271 <div class="makecommerce-link"><a target="_blank" href="http://maksekeskus.ee">maksekeskus.ee</a></div>
272 </div>
273 </div>
274 ';
275 }
276
277 /**
278 * Returns true if WooCommerce version is 3.4.0 or higher (different links in admin)
279 *
280 * @since 3.0.0
281 */
282 public static function new_woo_version() {
283
284 global $woocommerce;
285
286 if ( version_compare( $woocommerce->version, "3.4.0", ">=" ) ) {
287 return true;
288 }
289
290 return false;
291 }
292
293 /**
294 * Checks if API is set.
295 *
296 * @since 3.0.0
297 */
298 public static function is_api_set() {
299
300 return self::get_api( true );
301 }
302
303 /**
304 * Displays error in admin that API access is not configured properly
305 *
306 * @since 3.0.0
307 */
308 public function mk_admin_error() {
309
310 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' ) );
311 }
312
313 /**
314 * Returns MK api, unless check is set to true, then returns if api is set
315 *
316 * @since 3.0.0
317 */
318 public static function get_api( $check = false ) {
319
320 $mk_api_type = get_option( 'mk_api_type', false );
321
322 if ( !$mk_api_type ) {
323 return false;
324 }
325
326 $key_prefix = '';
327 if ( $mk_api_type !== 'live' ) {
328 $key_prefix = $mk_api_type.'_';
329 }
330
331 $mk_shop_id = get_option( 'mk_'.$key_prefix.'shop_id', '' );
332 $mk_public_key = get_option( 'mk_'.$key_prefix.'public_key', '' );
333 $mk_private_key = get_option( 'mk_'.$key_prefix.'private_key', '' );
334
335 if ( !$mk_shop_id || !$mk_public_key || !$mk_shop_id ) {
336 return false;
337 }
338
339 //if this was just a check, return true
340 if ( $check === true ) {
341 return true;
342 }
343
344 global $MKAPI;
345 $MKAPI = new \Maksekeskus\Maksekeskus( $mk_shop_id, $mk_public_key, $mk_private_key, $mk_api_type === 'live' ? false : true );
346
347 return $MKAPI;
348 }
349
350 /**
351 * Sets up shop getConfig request parameters
352 *
353 * @since 3.0.0
354 */
355 public static function config_request_parameters( $module = "" ) {
356
357 return array(
358 'environment' => json_encode( array(
359 'system' => array( 'wordpress' => get_bloginfo( 'version' ), "php" => phpversion() ),
360 'platform' => 'woocommerce '. WC_VERSION,
361 'module' => $module,
362 )),
363 );
364 }
365
366 /**
367 * Notify that woocommerce version is not supported with our plugin
368 *
369 * @since 3.0.0
370 */
371 public function old_woocommerce_notice() {
372
373 $class = 'notice notice-error';
374 $message = sprintf( __( 'Makecommerce plugin needs at least <strong>WooCommerce 2.6</strong> to function correctly. Please update yours (currently %s)', 'wc_makecommerce_domain' ), $woocommerce->version );
375 printf( '<div class="%1$s"><p>%2$s</p></div>', $class, $message );
376 }
377
378 /**
379 * Helpful tool for debugging
380 *
381 * @since 3.0.0
382 */
383 public static function prd() {
384
385 //get all arguments
386 $args = func_get_args();
387
388 echo "<pre>";
389
390 foreach ( $args as $arg ) {
391
392 $printR = print_r( $arg, true );
393
394 error_log( $printR );
395
396 echo $printR . "\r\n\r\n\r\n";
397 }
398
399 echo "</pre>";
400
401 die();
402 }
403
404
405 public static $scriptLoaderAttributes = array();
406
407 /**
408 * Enqueues javascript with parameters.
409 *
410 * @since 3.0.9
411 */
412 public static function mc_enqueue_script( $handle, $src, $data = [], $deps = [], $external = false ) {
413
414 $version = null;
415 if ( !$external ) {
416
417 $version = filemtime( $src );
418 $src = plugin_dir_url( $src ) . basename( $src );
419 }
420
421 wp_register_script( $handle, $src, $deps, $version );
422
423 if ( !empty( $data ) ) {
424 wp_add_inline_script( $handle, 'const ' . $handle . ' = ' . json_encode( $data ), 'before' );
425 }
426
427 wp_enqueue_script( $handle );
428 }
429 }