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

406 lines 11.6 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 = '3.0.0';
72 if ( defined( 'MAKECOMMERCE_VERSION' ) ) {
73 $this->version = MAKECOMMERCE_VERSION;
74 }
75
76 $this->plugin_name = 'makecommerce';
77
78 //configure autoloader
79 require_once plugin_dir_path( __FILE__ ) . "autoloader.php";
80
81 $autoLoader = new MakeCommerce\Autoloader;
82 $autoLoader->register();
83
84 //register includes and the complete plugin path as MakeCommerce namespace
85 $autoLoader->addNamespace( 'MakeCommerce', __DIR__ );
86 $autoLoader->addNamespace( 'MakeCommerce', plugin_dir_path( __DIR__ ) );
87
88 //get Maksekeskus API
89 require_once plugin_dir_path( __FILE__ ) . 'vendor/autoload.php';
90 require_once plugin_dir_path( __FILE__ ) . 'vendor/Maksekeskus.php';
91
92 $this->loader = new MakeCommerce\Loader();
93
94 $this->set_locale();
95
96 $plugin_payment = new MakeCommerce\Payment ( $this->get_plugin_name(), $this->get_version(), $this->loader );
97
98 $this->define_api_hooks();
99 $this->define_shipping_hooks();
100 $this->define_cron_hooks();
101 }
102
103 /**
104 * Define the locale for this plugin for internationalization.
105 *
106 * Uses the Makecommerce_i18n class in order to set the domain and to register the hook
107 * with WordPress.
108 *
109 * @since 3.0.0
110 * @access private
111 */
112 private function set_locale() {
113
114 $plugin_i18n = new MakeCommerce\i18n();
115
116 $this->loader->add_action( 'plugins_loaded', $plugin_i18n, 'load_plugin_textdomain' );
117 }
118
119 /**
120 * Register all of the hooks related to cron functionality
121 * of the plugin.
122 *
123 * @since 3.0.0
124 * @access private
125 */
126 private function define_cron_hooks() {
127
128 $plugin_cron = new MakeCommerce\Cron( $this->get_plugin_name(), $this->get_version() );
129
130 //cron define query vars
131 $this->loader->add_filter( 'query_vars', $plugin_cron, 'query_vars' );
132
133 // WP schedule callable action
134 $this->loader->add_action( 'mc_banklinks_update_cron', $plugin_cron, 'update_banklinks' );
135
136 $this->loader->add_action( 'parse_request', $plugin_cron, 'parse_update_vars' );
137 }
138
139 /**
140 * Register all of the hooks related to the api functionality
141 * of the plugin.
142 *
143 * @since 3.0.0
144 * @access private
145 */
146 private function define_api_hooks() {
147
148 $api = new MakeCommerce\API( $this->get_plugin_name(), $this->get_version(), $this->loader );
149
150 //check if CURL is installed
151 $this->loader->add_action( 'admin_notices', $api, 'check_if_curl_is_loaded');
152
153 //Add admin notice if api is not set up
154 if ( !\MakeCommerce::get_api() ) {
155 $this->loader->add_action( 'admin_notices', $api, 'api_info_missing' );
156 }
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( 'save_post', $plugin_shipping, 'set_parcel_machine_meta' );
212 }
213
214 /**
215 * Run the loader to execute all of the hooks with WordPress.
216 *
217 * @since 3.0.0
218 */
219 public function run() {
220
221 $this->loader->run();
222 }
223
224 /**
225 * The name of the plugin used to uniquely identify it within the context of
226 * WordPress and to define internationalization functionality.
227 *
228 * @since 3.0.0
229 * @return string The name of the plugin.
230 */
231 public function get_plugin_name() {
232
233 return $this->plugin_name;
234 }
235
236 /**
237 * The reference to the class that orchestrates the hooks with the plugin.
238 *
239 * @since 3.0.0
240 * @return Loader Orchestrates the hooks of the plugin.
241 */
242 public function get_loader() {
243
244 return $this->loader;
245 }
246
247 /**
248 * Retrieve the version number of the plugin.
249 *
250 * @since 3.0.0
251 * @return string The version number of the plugin.
252 */
253 public function get_version() {
254
255 return $this->version;
256 }
257
258 /**
259 * Get the HTML version of the logo
260 *
261 * @since 3.0.0
262 */
263 public static function get_logo_html() {
264
265 return '
266 <div class="makecommerce-info">
267 <div class="makecommerce-logo">
268 <a target="_blank" href="http://maksekeskus.ee"><img src="'. plugins_url( '../payment/gateway/woocommerce/images/makecommerce_logo_en.svg', __FILE__ ) .'" class="makecommerce-logo"></a>
269 </div>
270
271 <div class="makecommerce-links">
272 <div class="makecommerce-link"><a target="_blank" href="https://merchant.maksekeskus.ee">Merchant Portal</a></div>
273 <div class="makecommerce-link"><a target="_blank" href="https://makecommerce.net/">makecommerce.net</a></div>
274 <div class="makecommerce-link"><a target="_blank" href="http://maksekeskus.ee">maksekeskus.ee</a></div>
275 </div>
276 </div>
277 ';
278 }
279
280 /**
281 * Returns true if WooCommerce version is 3.4.0 or higher (different links in admin)
282 *
283 * @since 3.0.0
284 */
285 public static function new_woo_version() {
286
287 global $woocommerce;
288
289 if ( version_compare( $woocommerce->version, "3.4.0", ">=" ) ) {
290 return true;
291 }
292
293 return false;
294 }
295
296 /**
297 * Checks if API is set.
298 *
299 * @since 3.0.0
300 */
301 public static function is_api_set() {
302
303 return self::get_api( true );
304 }
305
306 /**
307 * Displays error in admin that API access is not configured properly
308 *
309 * @since 3.0.0
310 */
311 public function mk_admin_error() {
312
313 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' ) );
314 }
315
316 /**
317 * Returns MK api, unless check is set to true, then returns if api is set
318 *
319 * @since 3.0.0
320 */
321 public static function get_api( $check = false ) {
322
323 $mk_api_type = get_option( 'mk_api_type', false );
324
325 if ( !$mk_api_type ) {
326 return false;
327 }
328
329 $key_prefix = '';
330 if ( $mk_api_type !== 'live' ) {
331 $key_prefix = $mk_api_type.'_';
332 }
333
334 $mk_shop_id = get_option( 'mk_'.$key_prefix.'shop_id', '' );
335 $mk_public_key = get_option( 'mk_'.$key_prefix.'public_key', '' );
336 $mk_private_key = get_option( 'mk_'.$key_prefix.'private_key', '' );
337
338 if ( !$mk_shop_id || !$mk_public_key || !$mk_shop_id ) {
339 return false;
340 }
341
342 //if this was just a check, return true
343 if ( $check === true ) {
344 return true;
345 }
346
347 global $MKAPI;
348 $MKAPI = new \Maksekeskus\Maksekeskus( $mk_shop_id, $mk_public_key, $mk_private_key, $mk_api_type === 'live' ? false : true );
349
350 return $MKAPI;
351 }
352
353 /**
354 * Sets up shop getConfig request parameters
355 *
356 * @since 3.0.0
357 */
358 public static function config_request_parameters( $module = "" ) {
359
360 return array(
361 'environment' => json_encode( array(
362 'system' => array( 'wordpress' => get_bloginfo( 'version' ), "php" => phpversion() ),
363 'platform' => 'woocommerce '. WC_VERSION,
364 'module' => $module,
365 )),
366 );
367 }
368
369 /**
370 * Notify that woocommerce version is not supported with our plugin
371 *
372 * @since 3.0.0
373 */
374 public function old_woocommerce_notice() {
375
376 $class = 'notice notice-error';
377 $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 );
378 printf( '<div class="%1$s"><p>%2$s</p></div>', $class, $message );
379 }
380
381 /**
382 * Helpful tool for debugging
383 *
384 * @since 3.0.0
385 */
386 public static function prd() {
387
388 //get all arguments
389 $args = func_get_args();
390
391 echo "<pre>";
392
393 foreach ( $args as $arg ) {
394
395 $printR = print_r( $arg, true );
396
397 error_log( $printR );
398
399 echo $printR . "\r\n\r\n\r\n";
400 }
401
402 echo "</pre>";
403
404 die();
405 }
406 }