| 1 |
<?php |
| 2 |
/* |
| 3 |
Plugin Name: WooCommerce PostNL |
| 4 |
Plugin URI: http://www.postnl.nl |
| 5 |
Description: Export your WooCommerce orders to PostNL (www.postnl.nl) and print labels directly from the WooCommerce admin |
| 6 |
Author: PostNL |
| 7 |
Version: 3.1.4 |
| 8 |
|
| 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')) exit; // Exit if accessed directly |
| 16 |
|
| 17 |
if ( ! class_exists('WooCommerce_PostNL')) : |
| 18 |
|
| 19 |
class WooCommerce_PostNL { |
| 20 |
|
| 21 |
public $version = '3.1.4'; |
| 22 |
public $plugin_basename; |
| 23 |
protected static $_instance = null; |
| 24 |
|
| 25 |
/** |
| 26 |
* Main Plugin Instance |
| 27 |
* Ensures only one instance of plugin is loaded or can be loaded. |
| 28 |
*/ |
| 29 |
public static function instance() { |
| 30 |
if (is_null(self::$_instance)) { |
| 31 |
self::$_instance = new self(); |
| 32 |
} |
| 33 |
|
| 34 |
return self::$_instance; |
| 35 |
} |
| 36 |
|
| 37 |
/** |
| 38 |
* Constructor |
| 39 |
*/ |
| 40 |
|
| 41 |
public function __construct() { |
| 42 |
$this->define('WC_POSTNL_VERSION', $this->version); |
| 43 |
$this->define('WC_CHANNEL_ENGINE_ACTIVE', class_exists('Channel_Engine')); |
| 44 |
$this->plugin_basename = plugin_basename(__FILE__); |
| 45 |
|
| 46 |
// Load settings |
| 47 |
$this->general_settings = get_option('woocommerce_postnl_general_settings'); |
| 48 |
$this->export_defaults = get_option('woocommerce_postnl_export_defaults_settings'); |
| 49 |
$this->checkout_settings = get_option('woocommerce_postnl_checkout_settings'); |
| 50 |
|
| 51 |
// load the localisation & classes |
| 52 |
add_action('plugins_loaded', array($this, 'translations')); |
| 53 |
add_action('init', array($this, 'load_classes')); |
| 54 |
|
| 55 |
// run lifecycle methods |
| 56 |
if (is_admin() && ! defined('DOING_AJAX')) { |
| 57 |
add_action('wp_loaded', array($this, 'do_install')); |
| 58 |
} |
| 59 |
} |
| 60 |
|
| 61 |
/** |
| 62 |
* Define constant if not already set |
| 63 |
* |
| 64 |
* @param string $name |
| 65 |
* @param string|bool $value |
| 66 |
*/ |
| 67 |
private function define($name, $value) { |
| 68 |
if ( ! defined($name)) { |
| 69 |
define($name, $value); |
| 70 |
} |
| 71 |
} |
| 72 |
|
| 73 |
/** |
| 74 |
* Load the translation / text-domain files |
| 75 |
* Note: the first-loaded translation file overrides any following ones if the same translation is present |
| 76 |
*/ |
| 77 |
public function translations() { |
| 78 |
$locale = apply_filters('plugin_locale', get_locale(), 'woocommerce-postnl'); |
| 79 |
$dir = trailingslashit(WP_LANG_DIR); |
| 80 |
|
| 81 |
/** |
| 82 |
* Frontend/global Locale. Looks in: |
| 83 |
* - WP_LANG_DIR/woocommerce-postnl/woocommerce-postnl-LOCALE.mo |
| 84 |
* - WP_LANG_DIR/plugins/woocommerce-postnl-LOCALE.mo |
| 85 |
* - woocommerce-postnl/languages/woocommerce-postnl-LOCALE.mo (which if not found falls back to:) |
| 86 |
* - WP_LANG_DIR/plugins/woocommerce-postnl-LOCALE.mo |
| 87 |
*/ |
| 88 |
load_textdomain('woocommerce-postnl', $dir . 'woocommerce-postnl/woocommerce-postnl-' . $locale . '.mo'); |
| 89 |
load_textdomain('woocommerce-postnl', $dir . 'plugins/woocommerce-postnl-' . $locale . '.mo'); |
| 90 |
load_plugin_textdomain('woocommerce-postnl', false, dirname(plugin_basename(__FILE__)) . '/languages'); |
| 91 |
} |
| 92 |
|
| 93 |
/** |
| 94 |
* Load the main plugin classes and functions |
| 95 |
*/ |
| 96 |
public function includes() { |
| 97 |
// include compatibility classes |
| 98 |
include_once('includes/compatibility/abstract-wc-data-compatibility.php'); |
| 99 |
include_once('includes/compatibility/class-wc-date-compatibility.php'); |
| 100 |
include_once('includes/compatibility/class-wc-core-compatibility.php'); |
| 101 |
include_once('includes/compatibility/class-wc-order-compatibility.php'); |
| 102 |
include_once('includes/compatibility/class-wc-product-compatibility.php'); |
| 103 |
|
| 104 |
include_once('includes/class-wcmp-assets.php'); |
| 105 |
$this->admin = include_once('includes/class-wcmp-admin.php'); |
| 106 |
include_once('includes/class-wcmp-frontend-settings.php'); |
| 107 |
include_once('includes/class-wcmp-frontend.php'); |
| 108 |
include_once('includes/class-wcmp-settings.php'); |
| 109 |
$this->export = include_once('includes/class-wcmp-export.php'); |
| 110 |
include_once('includes/class-wcmp-nl-postcode-fields.php'); |
| 111 |
} |
| 112 |
|
| 113 |
/** |
| 114 |
* Instantiate classes when woocommerce is activated |
| 115 |
*/ |
| 116 |
public function load_classes() { |
| 117 |
if ($this->is_woocommerce_activated() === false) { |
| 118 |
add_action('admin_notices', array($this, 'need_woocommerce')); |
| 119 |
|
| 120 |
return; |
| 121 |
} |
| 122 |
|
| 123 |
if (version_compare(PHP_VERSION, '5.4', '<')) { |
| 124 |
add_action('admin_notices', array($this, 'required_php_version')); |
| 125 |
|
| 126 |
return; |
| 127 |
} |
| 128 |
|
| 129 |
// all systems ready - GO! |
| 130 |
$this->includes(); |
| 131 |
} |
| 132 |
|
| 133 |
/** |
| 134 |
* Check if woocommerce is activated |
| 135 |
*/ |
| 136 |
public function is_woocommerce_activated() { |
| 137 |
$blog_plugins = get_option('active_plugins', array()); |
| 138 |
$site_plugins = get_site_option('active_sitewide_plugins', array()); |
| 139 |
|
| 140 |
if (in_array('woocommerce/woocommerce.php', $blog_plugins) || isset($site_plugins['woocommerce/woocommerce.php'])) { |
| 141 |
return true; |
| 142 |
} else { |
| 143 |
return false; |
| 144 |
} |
| 145 |
} |
| 146 |
|
| 147 |
/** |
| 148 |
* WooCommerce not active notice. |
| 149 |
* @return string Fallack notice. |
| 150 |
*/ |
| 151 |
public function need_woocommerce() { |
| 152 |
$error = sprintf(__('WooCommerce PostNL requires %sWooCommerce%s to be installed & activated!', 'woocommerce-postnl'), '<a href="http://wordpress.org/extend/plugins/woocommerce/">', '</a>'); |
| 153 |
|
| 154 |
$message = '<div class="error"><p>' . $error . '</p></div>'; |
| 155 |
|
| 156 |
echo $message; |
| 157 |
} |
| 158 |
|
| 159 |
/** |
| 160 |
* PHP version requirement notice |
| 161 |
*/ |
| 162 |
|
| 163 |
public function required_php_version() { |
| 164 |
$error = __('WooCommerce PostNL requires PHP 5.4 or higher (5.6 or later recommended).', 'woocommerce-postnl'); |
| 165 |
$how_to_update = __('How to update your PHP version', 'woocommerce-postnl'); |
| 166 |
$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); |
| 167 |
|
| 168 |
echo $message; |
| 169 |
} |
| 170 |
|
| 171 |
/** Lifecycle methods ******************************************************* |
| 172 |
* Because register_activation_hook only runs when the plugin is manually |
| 173 |
* activated by the user, we're checking the current version against the |
| 174 |
* version stored in the database |
| 175 |
****************************************************************************/ |
| 176 |
|
| 177 |
/** |
| 178 |
* Handles version checking |
| 179 |
*/ |
| 180 |
public function do_install() { |
| 181 |
$version_setting = 'woocommerce_postnl_version'; |
| 182 |
$installed_version = get_option($version_setting); |
| 183 |
|
| 184 |
// installed version lower than plugin version? |
| 185 |
if (version_compare($installed_version, $this->version, '<')) { |
| 186 |
if ( ! $installed_version) { |
| 187 |
$this->install(); |
| 188 |
} else { |
| 189 |
$this->upgrade($installed_version); |
| 190 |
} |
| 191 |
|
| 192 |
// new version number |
| 193 |
update_option($version_setting, $this->version); |
| 194 |
} |
| 195 |
} |
| 196 |
|
| 197 |
/** |
| 198 |
* Plugin install method. Perform any installation tasks here |
| 199 |
*/ |
| 200 |
protected function install() { |
| 201 |
// copy old settings if available (pre 2.0 didn't store the version, so technically, this is a new install) |
| 202 |
$old_settings = get_option('wcpostnl_settings'); |
| 203 |
if ( ! empty($old_settings)) { |
| 204 |
// map old key => new_key |
| 205 |
$general_settings_keys = array( |
| 206 |
'api_key' => 'api_key', |
| 207 |
'download_display' => 'download_display', |
| 208 |
'email_tracktrace' => 'email_tracktrace', |
| 209 |
'myaccount_tracktrace' => 'myaccount_tracktrace', |
| 210 |
'process' => 'process_directly', |
| 211 |
'barcode_in_note' => 'barcode_in_note', |
| 212 |
'keep_consignments' => 'keep_shipments', |
| 213 |
'error_logging' => 'error_logging', |
| 214 |
); |
| 215 |
|
| 216 |
$general_settings = array(); |
| 217 |
foreach ($general_settings_keys as $old_key => $new_key) { |
| 218 |
if ( ! empty($old_settings[$old_key])) { |
| 219 |
$general_settings[$new_key] = $old_settings[$old_key]; |
| 220 |
} |
| 221 |
} |
| 222 |
// auto_complete breaks down into: |
| 223 |
// order_status_automation & automatic_order_status |
| 224 |
if ( ! empty($old_settings['auto_complete'])) { |
| 225 |
$general_settings['order_status_automation'] = 1; |
| 226 |
$general_settings['automatic_order_status'] = 'completed'; |
| 227 |
} |
| 228 |
|
| 229 |
// map old key => new_key |
| 230 |
$defaults_settings_keys = array( |
| 231 |
'telefoon' => 'connect_phone', |
| 232 |
'huisadres' => 'only_recipient', |
| 233 |
'handtekening' => 'signature', |
| 234 |
'retourbgg' => 'return', |
| 235 |
'kenmerk' => 'label_description', |
| 236 |
'verpakkingsgewicht' => 'empty_parcel_weight', |
| 237 |
'verzekerd' => 'insured', |
| 238 |
'verzekerdbedrag' => 'insured_amount', |
| 239 |
); |
| 240 |
$defaults_settings = array(); |
| 241 |
foreach ($defaults_settings_keys as $old_key => $new_key) { |
| 242 |
if ( ! empty($old_settings[$old_key])) { |
| 243 |
$defaults_settings[$new_key] = $old_settings[$old_key]; |
| 244 |
} |
| 245 |
} |
| 246 |
// set custom insurance amount |
| 247 |
if ( ! empty($defaults_settings['insured']) && (int) $defaults_settings['insured_amount'] > 249) { |
| 248 |
$defaults_settings['insured_amount'] = 0; |
| 249 |
$defaults_settings['insured_amount_custom'] = $old_settings['verzekerdbedrag']; |
| 250 |
} |
| 251 |
|
| 252 |
// add options |
| 253 |
update_option('woocommerce_postnl_general_settings', $general_settings); |
| 254 |
update_option('woocommerce_postnl_export_defaults_settings', $defaults_settings); |
| 255 |
} |
| 256 |
} |
| 257 |
|
| 258 |
/** |
| 259 |
* Plugin upgrade method. Perform any required upgrades here |
| 260 |
* |
| 261 |
* @param string $installed_version the currently installed ('old') version |
| 262 |
*/ |
| 263 |
protected function upgrade($installed_version) { |
| 264 |
if (version_compare($installed_version, '2.4.0-beta-4', '<')) { |
| 265 |
// remove log file (now uses WC logger) |
| 266 |
$upload_dir = wp_upload_dir(); |
| 267 |
$upload_base = trailingslashit($upload_dir['basedir']); |
| 268 |
$log_file = $upload_base . 'postnl_log.txt'; |
| 269 |
if (@file_exists($log_file)) { |
| 270 |
@unlink($log_file); |
| 271 |
} |
| 272 |
} |
| 273 |
|
| 274 |
if (version_compare($installed_version, '3.0.4', '<=')) { |
| 275 |
$old_settings = get_option('woocommerce_postnl_checkout_settings'); |
| 276 |
$new_settings = $old_settings; |
| 277 |
|
| 278 |
// Add/replace new settings |
| 279 |
$new_settings['use_split_address_fields'] = '1'; |
| 280 |
|
| 281 |
// Rename signed to signature and night to evening for consistency |
| 282 |
$new_settings['signature_enabled'] = $old_settings['signed_enabled']; |
| 283 |
$new_settings['signature_title'] = $old_settings['signed_title']; |
| 284 |
$new_settings['signature_fee'] = $old_settings['signed_fee']; |
| 285 |
|
| 286 |
$new_settings['evening_enabled'] = $old_settings['night_enabled']; |
| 287 |
$new_settings['evening_title'] = $old_settings['night_title']; |
| 288 |
$new_settings['evening_fee'] = $old_settings['night_fee']; |
| 289 |
|
| 290 |
// Remove old settings |
| 291 |
unset($new_settings['signed_enabled']); |
| 292 |
unset($new_settings['signed_title']); |
| 293 |
unset($new_settings['signed_fee']); |
| 294 |
|
| 295 |
unset($new_settings['night_enabled']); |
| 296 |
unset($new_settings['night_title']); |
| 297 |
unset($new_settings['night_fee']); |
| 298 |
|
| 299 |
update_option('woocommerce_postnl_checkout_settings', $new_settings); |
| 300 |
} |
| 301 |
} |
| 302 |
|
| 303 |
/** |
| 304 |
* Get the plugin url. |
| 305 |
* @return string |
| 306 |
*/ |
| 307 |
public function plugin_url() { |
| 308 |
return untrailingslashit(plugins_url('/', __FILE__)); |
| 309 |
} |
| 310 |
|
| 311 |
/** |
| 312 |
* Get the plugin path. |
| 313 |
* @return string |
| 314 |
*/ |
| 315 |
public function plugin_path() { |
| 316 |
return untrailingslashit(plugin_dir_path(__FILE__)); |
| 317 |
} |
| 318 |
} // class WooCommerce_PostNL |
| 319 |
|
| 320 |
endif; // class_exists |
| 321 |
|
| 322 |
/** |
| 323 |
* Returns the main instance of the plugin class to prevent the need to use globals. |
| 324 |
* @since 2.0 |
| 325 |
* @return WooCommerce_PostNL |
| 326 |
*/ |
| 327 |
function WooCommerce_PostNL() { |
| 328 |
return WooCommerce_PostNL::instance(); |
| 329 |
} |
| 330 |
|
| 331 |
WooCommerce_PostNL(); // load plugin |
| 332 |
|