PluginProbe
PostNL for WooCommerce / 4.0.0
PostNL for WooCommerce v4.0.0
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.0.0, at woocommerce-postnl.php

381 lines 12.7 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.0.0
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 MINIMUM_PHP_VERSION_5_4 = '5.4';
29 const PHP_VERSION_7_1 = '7.1';
30
31 public $version = '4.0.0';
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->includes . "/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::MINIMUM_PHP_VERSION_5_4)) {
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 }
191
192 /**
193 * Check if woocommerce is activated
194 */
195 public function is_woocommerce_activated()
196 {
197 $blog_plugins = get_option('active_plugins', []);
198 $site_plugins = get_site_option('active_sitewide_plugins', []);
199
200 if (in_array('woocommerce/woocommerce.php', $blog_plugins)
201 || isset($site_plugins['woocommerce/woocommerce.php'])) {
202 return true;
203 } else {
204 return false;
205 }
206 }
207
208 /**
209 * WooCommerce not active notice.
210 */
211 public function need_woocommerce()
212 {
213 $error = sprintf(
214 __("WooCommerce PostNL requires %sWooCommerce%s to be installed & activated!",
215 "woocommerce-postnl"
216 ),
217 '<a href="http://wordpress.org/extend/plugins/woocommerce/">',
218 '</a>'
219 );
220
221 $message = '<div class="error"><p>' . $error . '</p></div>';
222
223 echo $message;
224 }
225
226 /**
227 * PHP version requirement notice
228 */
229
230 public function required_php_version()
231 {
232 $error = __("WooCommerce PostNL requires PHP 5.4 or higher (5.6 or later recommended).",
233 "woocommerce-postnl"
234 );
235 $how_to_update = __("How to update your PHP version", "woocommerce-postnl");
236 $message = sprintf(
237 '<div class="error"><p>%s</p><p><a href="%s">%s</a></p></div>',
238 $error,
239 'http://docs.wpovernight.com/general/how-to-update-your-php-version/',
240 $how_to_update
241 );
242
243 echo $message;
244 }
245
246 /** Lifecycle methods *******************************************************
247 * Because register_activation_hook only runs when the plugin is manually
248 * activated by the user, we're checking the current version against the
249 * version stored in the database
250 ****************************************************************************/
251
252 /**
253 * Handles version checking
254 */
255 public function do_install()
256 {
257 $version_setting = "woocommerce_postnl_version";
258 $installed_version = get_option($version_setting);
259
260 // installed version lower than plugin version?
261 if (version_compare($installed_version, $this->version, '<')) {
262 if (! $installed_version) {
263 $this->install();
264 } else {
265 $this->upgrade($installed_version);
266 }
267
268 // new version number
269 update_option($version_setting, $this->version);
270 }
271 }
272
273 /**
274 * Plugin install method. Perform any installation tasks here
275 */
276 protected function install()
277 {
278 // Pre 2.0.0
279 if (! empty(get_option('wcpostnl_settings'))) {
280 require_once('migration/wcpn-installation-migration-v2-0-0.php');
281 }
282 // todo: Pre 4.0.0?
283 }
284
285 /**
286 * Plugin upgrade method. Perform any required upgrades here
287 *
288 * @param string $installed_version the currently installed ('old') version
289 */
290 protected function upgrade($installed_version)
291 {
292 if (version_compare($installed_version, '2.4.0-beta-4', '<')) {
293 require_once('migration/wcpn-upgrade-migration-v2-4-0-beta-4.php');
294 }
295
296 if (version_compare($installed_version, '3.0.4', '<=')) {
297 require_once('migration/wcpn-upgrade-migration-v3-0-4.php');
298 }
299
300 if ($this->phpVersionMeets(\WCPOST::PHP_VERSION_7_1)) {
301 // Import the migration class base
302 require_once('migration/wcpn-upgrade-migration.php');
303
304 // Migrate php 7.1+ only version settings
305 if (version_compare($installed_version, '4.0.0', '<=')) {
306 require_once('migration/wcpn-upgrade-migration-v4-0-0.php');
307 require_once('migration/wcpn-upgrade-migration-v4-1-0.php');
308 }
309 }
310 }
311
312 /**
313 * Get the plugin url.
314 *
315 * @return string
316 */
317 public function plugin_url()
318 {
319 return untrailingslashit(plugins_url('/', __FILE__));
320 }
321
322 /**
323 * Get the plugin path.
324 *
325 * @return string
326 */
327 public function plugin_path()
328 {
329 return untrailingslashit(plugin_dir_path(__FILE__));
330 }
331
332 /**
333 * Initialize the settings.
334 * Legacy: Before PHP 7.1, use old settings structure.
335 */
336 public function initSettings()
337 {
338 // Create the settings collection by importing this function, because we can't use the sdk
339 // imports in the legacy version.
340 require_once('includes/wcpn-initialize-settings-collection.php');
341 if (empty($this->setting_collection)) {
342 $this->setting_collection = (new WCPN_Initialize_Settings_Collection())->initialize();
343 }
344 }
345
346 /**
347 * @param string $version
348 *
349 * @return bool
350 */
351 private function phpVersionMeets($version)
352 {
353 return version_compare(PHP_VERSION, $version, '>=');
354 }
355 }
356
357 endif;
358
359 /**
360 * Returns the main instance of the plugin class to prevent the need to use globals.
361 *
362 * @return WCPOST
363 * @since 2.0
364 */
365 function WCPOST()
366 {
367 return WCPOST::instance();
368 }
369
370 /**
371 * For PHP < 7.1 support.
372 *
373 * @return WCPOST
374 */
375 function WooCommerce_PostNL()
376 {
377 return WCPOST();
378 }
379
380 WCPOST(); // load plugin
381