| 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 |
|
| 97 |
$plugin_shipping = new MakeCommerce\Shipping( $this->get_plugin_name(), $this->get_version(), $this->loader ); |
| 98 |
$this->define_cron_hooks(); |
| 99 |
$this->define_admin_hooks(); |
| 100 |
} |
| 101 |
|
| 102 |
/** |
| 103 |
* Define the locale for this plugin for internationalization. |
| 104 |
* |
| 105 |
* Uses the Makecommerce_i18n class in order to set the domain and to register the hook |
| 106 |
* with WordPress. |
| 107 |
* |
| 108 |
* @since 3.0.0 |
| 109 |
* @access private |
| 110 |
*/ |
| 111 |
private function set_locale() { |
| 112 |
|
| 113 |
$plugin_i18n = new MakeCommerce\i18n(); |
| 114 |
|
| 115 |
$this->loader->add_action( 'init', $plugin_i18n, 'load_plugin_textdomain' ); |
| 116 |
} |
| 117 |
|
| 118 |
/** |
| 119 |
* Register all of the hooks related to cron functionality |
| 120 |
* of the plugin. |
| 121 |
* |
| 122 |
* @since 3.0.0 |
| 123 |
* @access private |
| 124 |
*/ |
| 125 |
private function define_cron_hooks() { |
| 126 |
|
| 127 |
$plugin_cron = new MakeCommerce\Cron( $this->get_plugin_name(), $this->get_version() ); |
| 128 |
|
| 129 |
//cron define query vars |
| 130 |
$this->loader->add_filter( 'query_vars', $plugin_cron, 'query_vars' ); |
| 131 |
|
| 132 |
// WP schedule callable action |
| 133 |
$this->loader->add_action( 'mc_banklinks_update_cron', $plugin_cron, 'update_banklinks' ); |
| 134 |
|
| 135 |
$this->loader->add_action( 'parse_request', $plugin_cron, 'parse_update_vars' ); |
| 136 |
} |
| 137 |
|
| 138 |
/** |
| 139 |
* Register all of the hooks related to the api functionality |
| 140 |
* of the plugin. |
| 141 |
* |
| 142 |
* @since 3.0.0 |
| 143 |
* @access private |
| 144 |
*/ |
| 145 |
private function define_api_hooks() { |
| 146 |
|
| 147 |
$api = new MakeCommerce\API( $this->get_plugin_name(), $this->get_version(), $this->loader ); |
| 148 |
|
| 149 |
//check if CURL is installed |
| 150 |
$this->loader->add_action( 'admin_notices', $api, 'check_if_curl_is_loaded'); |
| 151 |
|
| 152 |
//Add admin notice if api is not set up |
| 153 |
if ( !\MakeCommerce::get_api() ) { |
| 154 |
$this->loader->add_action( 'admin_notices', $api, 'api_info_missing' ); |
| 155 |
} |
| 156 |
|
| 157 |
//if API type is updated then refresh the cache |
| 158 |
$this->loader->add_action( 'update_option_mc_api_mode', $api, 'mk_delete_api_cache' ); |
| 159 |
|
| 160 |
//add new customer column to orders view, make it sortable and fill with values |
| 161 |
$this->loader->add_filter( 'manage_edit-shop_order_columns', $api, 'add_ordersview_paymentmethod_column' ); |
| 162 |
$this->loader->add_filter( 'manage_edit-shop_order_sortable_columns', $api, 'make_ordersview_paymentmethod_column_sortable' ); |
| 163 |
$this->loader->add_action( 'manage_shop_order_posts_custom_column', $api, 'fill_ordersview_paymentmethod_column', 10, 2 ); |
| 164 |
|
| 165 |
// Compatibility with WC HPOS |
| 166 |
$this->loader->add_filter( 'manage_woocommerce_page_wc-orders_columns', $api, 'add_ordersview_paymentmethod_column' ); |
| 167 |
$this->loader->add_action( 'manage_woocommerce_page_wc-orders_custom_column', $api, 'fill_ordersview_paymentmethod_column', 10, 2 ); |
| 168 |
|
| 169 |
//hooks that update banklinks |
| 170 |
$this->loader->add_action( 'wp_login', $api, 'admin_login', 10, 2 ); |
| 171 |
|
| 172 |
//adds settings link |
| 173 |
$this->loader->add_filter( 'plugin_action_links_makecommerce/makecommerce.php', $api, 'add_plugin_settings_link' ); |
| 174 |
|
| 175 |
} |
| 176 |
|
| 177 |
|
| 178 |
/** |
| 179 |
* Register all of the hooks related to the admin dashboard |
| 180 |
* |
| 181 |
* @since 4.0.0 |
| 182 |
* @access private |
| 183 |
*/ |
| 184 |
private function define_admin_hooks() { |
| 185 |
$dashboard = new MakeCommerce\Admin\Dashboard(); |
| 186 |
|
| 187 |
$this->loader->add_action( 'admin_init', $dashboard, 'initialize' ); |
| 188 |
$this->loader->add_action( 'admin_init', $dashboard, 'save_settings' ); |
| 189 |
$this->loader->add_action( 'admin_head', $dashboard, 'hide_admin_notices' ); |
| 190 |
$this->loader->add_action( 'admin_menu', $dashboard, 'add_menu' ); |
| 191 |
$this->loader->add_action( 'admin_menu', $dashboard, 'remove_parent_menu', 15 ); |
| 192 |
$this->loader->add_action( 'admin_enqueue_scripts', $dashboard, 'enqueue_dashboard_scripts' ); |
| 193 |
$this->loader->add_action( 'add_option_mc_payments', $dashboard, 'sync_enabled_with_mc_payments', 10, 2 ); |
| 194 |
$this->loader->add_action( 'update_option_mc_payments', $dashboard, 'sync_enabled_with_mc_payments', 10, 2 ); |
| 195 |
} |
| 196 |
|
| 197 |
/** |
| 198 |
* Run the loader to execute all of the hooks with WordPress. |
| 199 |
* |
| 200 |
* @since 3.0.0 |
| 201 |
*/ |
| 202 |
public function run() { |
| 203 |
|
| 204 |
$this->loader->run(); |
| 205 |
} |
| 206 |
|
| 207 |
/** |
| 208 |
* The name of the plugin used to uniquely identify it within the context of |
| 209 |
* WordPress and to define internationalization functionality. |
| 210 |
* |
| 211 |
* @since 3.0.0 |
| 212 |
* @return string The name of the plugin. |
| 213 |
*/ |
| 214 |
public function get_plugin_name() { |
| 215 |
|
| 216 |
return $this->plugin_name; |
| 217 |
} |
| 218 |
|
| 219 |
/** |
| 220 |
* The reference to the class that orchestrates the hooks with the plugin. |
| 221 |
* |
| 222 |
* @since 3.0.0 |
| 223 |
* @return Loader Orchestrates the hooks of the plugin. |
| 224 |
*/ |
| 225 |
public function get_loader() { |
| 226 |
|
| 227 |
return $this->loader; |
| 228 |
} |
| 229 |
|
| 230 |
/** |
| 231 |
* Retrieve the version number of the plugin. |
| 232 |
* |
| 233 |
* @since 3.0.0 |
| 234 |
* @return string The version number of the plugin. |
| 235 |
*/ |
| 236 |
public function get_version() { |
| 237 |
|
| 238 |
return $this->version; |
| 239 |
} |
| 240 |
|
| 241 |
/** |
| 242 |
* Checks if API is set. |
| 243 |
* |
| 244 |
* @since 3.0.0 |
| 245 |
*/ |
| 246 |
public static function is_api_set() { |
| 247 |
|
| 248 |
return self::get_api( true ); |
| 249 |
} |
| 250 |
|
| 251 |
/** |
| 252 |
* Displays error in admin that API access is not configured properly |
| 253 |
* |
| 254 |
* @since 3.0.0 |
| 255 |
*/ |
| 256 |
public function mk_admin_error() { |
| 257 |
|
| 258 |
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' ) ); |
| 259 |
} |
| 260 |
|
| 261 |
/** |
| 262 |
* Returns MK api, unless check is set to true, then returns if api is set |
| 263 |
* |
| 264 |
* @since 3.0.0 |
| 265 |
*/ |
| 266 |
public static function get_api( $check = false ) { |
| 267 |
|
| 268 |
$mc_api_type = get_option( 'mc_api_mode', false ); |
| 269 |
|
| 270 |
if ( !$mc_api_type ) { |
| 271 |
return false; |
| 272 |
} |
| 273 |
|
| 274 |
$key_prefix = ''; |
| 275 |
if ( $mc_api_type !== 'live' ) { |
| 276 |
$key_prefix = $mc_api_type.'_'; |
| 277 |
} |
| 278 |
|
| 279 |
$mc_shop_id = get_option( 'mc_'.$key_prefix.'shop_id', '' ); |
| 280 |
$mc_public_key = get_option( 'mc_'.$key_prefix.'public_key', '' ); |
| 281 |
$mc_secret_key = get_option( 'mc_'.$key_prefix.'secret_key', '' ); |
| 282 |
|
| 283 |
if ( !$mc_shop_id || !$mc_public_key || !$mc_shop_id ) { |
| 284 |
return false; |
| 285 |
} |
| 286 |
|
| 287 |
//if this was just a check, return true |
| 288 |
if ( $check === true ) { |
| 289 |
return true; |
| 290 |
} |
| 291 |
|
| 292 |
global $MKAPI; |
| 293 |
$MKAPI = new \Maksekeskus\Maksekeskus( $mc_shop_id, $mc_public_key, $mc_secret_key, $mc_api_type === 'live' ? false : true ); |
| 294 |
|
| 295 |
return $MKAPI; |
| 296 |
} |
| 297 |
|
| 298 |
/** |
| 299 |
* Sets up shop getConfig request parameters |
| 300 |
* |
| 301 |
* @since 3.0.0 |
| 302 |
*/ |
| 303 |
public static function config_request_parameters( $module = "" ) { |
| 304 |
|
| 305 |
return array( |
| 306 |
'environment' => json_encode( array( |
| 307 |
'system' => array( 'wordpress' => get_bloginfo( 'version' ), "php" => phpversion() ), |
| 308 |
'platform' => 'woocommerce '. WC_VERSION, |
| 309 |
'module' => $module, |
| 310 |
)), |
| 311 |
); |
| 312 |
} |
| 313 |
|
| 314 |
/** |
| 315 |
* Sets up shop getConfig request parameters |
| 316 |
* |
| 317 |
* @since 3.0.0 |
| 318 |
*/ |
| 319 |
public static function get_sdk_config() { |
| 320 |
$conf = [ |
| 321 |
"module" => "MakeCommerce", |
| 322 |
"module_version" => MAKECOMMERCE_VERSION, |
| 323 |
"platform" => "WooCommerce", |
| 324 |
"hpos_enabled" => filter_var( get_option( 'woocommerce_custom_orders_table_enabled' ), FILTER_VALIDATE_BOOLEAN ), |
| 325 |
"payments_enabled" => filter_var( get_option( 'mc_payments' ), FILTER_VALIDATE_BOOLEAN ), |
| 326 |
"shipping_enabled" => filter_var( get_option( 'mc_shipping' ), FILTER_VALIDATE_BOOLEAN ) |
| 327 |
]; |
| 328 |
|
| 329 |
if (class_exists('WooCommerce')) { |
| 330 |
$conf['platform_version'] = WC()->version; |
| 331 |
} |
| 332 |
|
| 333 |
return $conf; |
| 334 |
} |
| 335 |
|
| 336 |
/** |
| 337 |
* Gets static url based on env |
| 338 |
* |
| 339 |
* @since 3.0.0 |
| 340 |
*/ |
| 341 |
public static function get_static_url() { |
| 342 |
|
| 343 |
$mc_api_type = get_option( 'mc_api_mode', 'live' ); |
| 344 |
|
| 345 |
if ($mc_api_type === 'test') { |
| 346 |
return 'https://static.test.maksekeskus.ee/'; |
| 347 |
} else { |
| 348 |
return 'https://static.maksekeskus.ee/'; |
| 349 |
} |
| 350 |
} |
| 351 |
|
| 352 |
/** |
| 353 |
* Enqueues javascript with parameters. |
| 354 |
* |
| 355 |
* @since 3.0.9 |
| 356 |
*/ |
| 357 |
public static function mc_enqueue_script( $handle, $src, $data = [], $deps = [], $external = false ) { |
| 358 |
//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. |
| 359 |
//https://github.com/wp-media/wp-rocket/issues/3125 |
| 360 |
if ( wp_script_is( $handle, 'enqueued' ) ) { |
| 361 |
return; |
| 362 |
} |
| 363 |
|
| 364 |
$version = null; |
| 365 |
if ( !$external ) { |
| 366 |
|
| 367 |
$version = filemtime( $src ); |
| 368 |
$src = plugin_dir_url( $src ) . basename( $src ); |
| 369 |
} |
| 370 |
|
| 371 |
wp_register_script( $handle, $src, $deps, $version ); |
| 372 |
|
| 373 |
if ( !empty( $data ) ) { |
| 374 |
wp_add_inline_script( $handle, 'const ' . $handle . ' = ' . json_encode( $data ), 'before' ); |
| 375 |
} |
| 376 |
|
| 377 |
wp_enqueue_script( $handle ); |
| 378 |
} |
| 379 |
} |