PluginProbe
PostNL for WooCommerce / 4.4.1
PostNL for WooCommerce v4.4.1
5.9.12 5.9.11 5.9.10 5.9.9 5.9.8 5.9.7 5.9.6 trunk 2.5.0 2.5.1 2.5.2 2.5.3 2.5.4 2.5.5 3.1.4 3.1.5 3.1.6 3.1.7 4.0.0 4.0.1 4.0.2 4.3.2 4.3.3 4.4.0 4.4.1 All 72 releases
woo-postnl / woocommerce-postnl.php

woocommerce-postnl.php in PostNL for WooCommerce 4.4.1, at woocommerce-postnl.php

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