-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathwoocommerce-postnl.php
executable file
·380 lines (325 loc) · 12.7 KB
/
woocommerce-postnl.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
<?php
/*
Plugin Name: WooCommerce PostNL
Plugin URI: https://postnl.nl/
Description: Export your WooCommerce orders to PostNL (https://postnl.nl/) and print labels directly from the WooCommerce admin
Author: PostNL
Author URI: https://postnl.nl
Version: 4.0.1
Text Domain: woocommerce-postnl
License: GPLv3 or later
License URI: http://www.opensource.org/licenses/gpl-license.php
*/
if (! defined('ABSPATH')) {
exit;
} // Exit if accessed directly
if (! class_exists('WCPOST')) :
class WCPOST
{
/**
* Translations domain
*/
const DOMAIN = 'woocommerce-postnl';
const NONCE_ACTION = 'wc_postnl';
const MINIMUM_PHP_VERSION_5_4 = '5.4';
const PHP_VERSION_7_1 = '7.1';
public $version = '4.0.1';
public $plugin_basename;
protected static $_instance = null;
/**
* @var WPO\WC\PostNL\Collections\SettingsCollection
*/
public $setting_collection;
/**
* @var string
*/
public $includes;
/**
* @var WCPN_Export
*/
public $export;
/**
* @var WCPOST_Admin
*/
public $admin;
/**
* Main Plugin Instance
* Ensures only one instance of plugin is loaded or can be loaded.
*/
public static function instance()
{
if (is_null(self::$_instance)) {
self::$_instance = new self();
}
return self::$_instance;
}
/**
* Constructor
*/
public function __construct()
{
$this->define('WC_POSTNL_VERSION', $this->version);
$this->plugin_basename = plugin_basename(__FILE__);
// load the localisation & classes
add_action('plugins_loaded', [$this, 'translations']);
add_action('init', [$this, 'load_classes'], 9999);
// run lifecycle methods
if (is_admin() && ! defined('DOING_AJAX')) {
add_action('init', [$this, 'do_install']);
}
}
/**
* Define constant if not already set
*
* @param string $name
* @param string|bool $value
*/
private function define($name, $value)
{
if (! defined($name)) {
define($name, $value);
}
}
/**
* Load the translation / text-domain files
* Note: the first-loaded translation file overrides any following ones if the same translation is present
*/
public function translations()
{
$locale = apply_filters('plugin_locale', get_locale(), self::DOMAIN);
$dir = trailingslashit(WP_LANG_DIR);
/**
* Frontend/global Locale. Looks in:
* - WP_LANG_DIR/woocommerce-postnl/woocommerce-postnl-LOCALE.mo
* - WP_LANG_DIR/plugins/woocommerce-postnl-LOCALE.mo
* - woocommerce-postnl/languages/woocommerce-postnl-LOCALE.mo (which if not found falls back to:)
* - WP_LANG_DIR/plugins/woocommerce-postnl-LOCALE.mo
*/
load_textdomain(
self::DOMAIN,
$dir . 'woocommerce-postnl/' . self::DOMAIN . '-' . $locale . '.mo'
);
load_textdomain(self::DOMAIN, $dir . 'plugins/' . self::DOMAIN . '-' . $locale . '.mo');
load_plugin_textdomain(self::DOMAIN, false, dirname(plugin_basename(__FILE__)) . '/languages');
}
/**
* Load the main plugin classes and functions
*/
public function includes()
{
$this->includes = $this->plugin_path() . '/includes';
// Use minimum php version 7.1
require_once($this->includes . "/vendor/autoload.php");
require_once($this->includes . "/admin/OrderSettings.php");
require_once($this->includes . "/admin/OrderSettingsRows.php");
// include compatibility classes
require_once($this->includes . "/compatibility/abstract-wc-data-compatibility.php");
require_once($this->includes . "/compatibility/class-wc-date-compatibility.php");
require_once($this->includes . "/compatibility/class-wc-core-compatibility.php");
require_once($this->includes . "/compatibility/class-wc-order-compatibility.php");
require_once($this->includes . "/compatibility/class-wc-product-compatibility.php");
require_once($this->includes . "/compatibility/class-ce-compatibility.php");
require_once($this->includes . "/compatibility/class-wcpdf-compatibility.php");
require_once($this->includes . "/class-wcpn-data.php");
require_once($this->includes . "/collections/settings-collection.php");
require_once($this->includes . "/entities/setting.php");
require_once($this->includes . "/entities/settings-field-arguments.php");
require_once($this->includes . "/class-wcpn-assets.php");
require_once($this->includes . "/frontend/class-wcpn-cart-fees.php");
require_once($this->includes . "/frontend/class-wcpn-frontend-track-trace.php");
require_once($this->includes . "/frontend/class-wcpn-checkout.php");
require_once($this->includes . "/frontend/class-wcpn-frontend.php");
$this->admin = require_once($this->includes . "/admin/class-wcpost-admin.php");
require_once($this->includes . "/admin/settings/class-wcpost-settings.php");
require_once($this->includes . "/class-wcpn-log.php");
require_once($this->includes . "/admin/class-wcpn-country-codes.php");
require_once($this->includes . '/admin/settings/class-wcpn-shipping-methods.php');
$this->export = require_once($this->includes . "/admin/class-wcpn-export.php");
require_once($this->includes . "/class-wcpn-postcode-fields.php");
require_once($this->includes . "/adapter/delivery-options-from-order-adapter.php");
require_once($this->includes . "/adapter/pickup-location-from-order-adapter.php");
require_once($this->includes . "/adapter/shipment-options-from-order-adapter.php");
require_once($this->includes . "/admin/class-wcpn-export-consignments.php");
}
/**
* Instantiate classes when WooCommerce is activated
*/
public function load_classes()
{
if ($this->is_woocommerce_activated() === false) {
add_action('admin_notices', [$this, 'need_woocommerce']);
return;
}
if (! $this->phpVersionMeets(self::MINIMUM_PHP_VERSION_5_4)) {
add_action('admin_notices', [$this, 'required_php_version']);
return;
}
// php 7.1
$this->includes();
$this->initSettings();
}
/**
* Check if woocommerce is activated
*/
public function is_woocommerce_activated()
{
$blog_plugins = get_option('active_plugins', []);
$site_plugins = get_site_option('active_sitewide_plugins', []);
if (in_array('woocommerce/woocommerce.php', $blog_plugins)
|| isset($site_plugins['woocommerce/woocommerce.php'])) {
return true;
} else {
return false;
}
}
/**
* WooCommerce not active notice.
*/
public function need_woocommerce()
{
$error = sprintf(
__("WooCommerce PostNL requires %sWooCommerce%s to be installed & activated!",
"woocommerce-postnl"
),
'<a href="http://wordpress.org/extend/plugins/woocommerce/">',
'</a>'
);
$message = '<div class="error"><p>' . $error . '</p></div>';
echo $message;
}
/**
* PHP version requirement notice
*/
public function required_php_version()
{
$error = __("WooCommerce PostNL requires PHP 5.4 or higher (5.6 or later recommended).",
"woocommerce-postnl"
);
$how_to_update = __("How to update your PHP version", "woocommerce-postnl");
$message = sprintf(
'<div class="error"><p>%s</p><p><a href="%s">%s</a></p></div>',
$error,
'http://docs.wpovernight.com/general/how-to-update-your-php-version/',
$how_to_update
);
echo $message;
}
/** Lifecycle methods *******************************************************
* Because register_activation_hook only runs when the plugin is manually
* activated by the user, we're checking the current version against the
* version stored in the database
****************************************************************************/
/**
* Handles version checking
*/
public function do_install()
{
$version_setting = "woocommerce_postnl_version";
$installed_version = get_option($version_setting);
// installed version lower than plugin version?
if (version_compare($installed_version, $this->version, '<')) {
if (! $installed_version) {
$this->install();
} else {
$this->upgrade($installed_version);
}
// new version number
update_option($version_setting, $this->version);
}
}
/**
* Plugin install method. Perform any installation tasks here
*/
protected function install()
{
// Pre 2.0.0
if (! empty(get_option('wcpostnl_settings'))) {
require_once('migration/wcpn-installation-migration-v2-0-0.php');
}
// todo: Pre 4.0.0?
}
/**
* Plugin upgrade method. Perform any required upgrades here
*
* @param string $installed_version the currently installed ('old') version
*/
protected function upgrade($installed_version)
{
if (version_compare($installed_version, '2.4.0-beta-4', '<')) {
require_once('migration/wcpn-upgrade-migration-v2-4-0-beta-4.php');
}
if (version_compare($installed_version, '3.0.4', '<=')) {
require_once('migration/wcpn-upgrade-migration-v3-0-4.php');
}
if ($this->phpVersionMeets(\WCPOST::PHP_VERSION_7_1)) {
// Import the migration class base
require_once('migration/wcpn-upgrade-migration.php');
// Migrate php 7.1+ only version settings
if (version_compare($installed_version, '4.0.0', '<=')) {
require_once('migration/wcpn-upgrade-migration-v4-0-0.php');
require_once('migration/wcpn-upgrade-migration-v4-1-0.php');
}
}
}
/**
* Get the plugin url.
*
* @return string
*/
public function plugin_url()
{
return untrailingslashit(plugins_url('/', __FILE__));
}
/**
* Get the plugin path.
*
* @return string
*/
public function plugin_path()
{
return untrailingslashit(plugin_dir_path(__FILE__));
}
/**
* Initialize the settings.
* Legacy: Before PHP 7.1, use old settings structure.
*/
public function initSettings()
{
// Create the settings collection by importing this function, because we can't use the sdk
// imports in the legacy version.
require_once('includes/wcpn-initialize-settings-collection.php');
if (empty($this->setting_collection)) {
$this->setting_collection = (new WCPN_Initialize_Settings_Collection())->initialize();
}
}
/**
* @param string $version
*
* @return bool
*/
private function phpVersionMeets($version)
{
return version_compare(PHP_VERSION, $version, '>=');
}
}
endif;
/**
* Returns the main instance of the plugin class to prevent the need to use globals.
*
* @return WCPOST
* @since 2.0
*/
function WCPOST()
{
return WCPOST::instance();
}
/**
* For PHP < 7.1 support.
*
* @return WCPOST
*/
function WooCommerce_PostNL()
{
return WCPOST();
}
WCPOST(); // load plugin