-
Notifications
You must be signed in to change notification settings - Fork 0
/
estonian-shipping-methods-for-woocommerce.php
288 lines (244 loc) · 8.78 KB
/
estonian-shipping-methods-for-woocommerce.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
<?php
/**
* Plugin Name: Estonian Shipping Methods for WooCommerce
* Plugin URI: https://github.com/KonektOU/estonian-shipping-methods-for-woocommerce
* Description: Extends WooCommerce with most commonly used Estonian shipping methods.
* Version: 1.5.8
* Author: Konekt OÜ
* Author URI: https://www.konekt.ee
* Developer: Risto Niinemets
* Developer URI: https://www.konekt.ee
* Text Domain: wc-estonian-shipping-methods
* Domain Path: /languages
* WC requires at least: 2.6
* WC tested up to: 4.3
*/
// Security check.
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
/**
* Main file constant
*/
define( 'WC_ESTONIAN_SHIPPING_METHODS_MAIN_FILE', __FILE__ );
/**
* Includes folder path
*/
define( 'WC_ESTONIAN_SHIPPING_METHODS_INCLUDES_PATH', plugin_dir_path( WC_ESTONIAN_SHIPPING_METHODS_MAIN_FILE ) . 'includes' );
/**
* @class Estonian_Shipping_Methods_For_WooCommerce
* @category Plugin
* @package Estonian_Shipping_Methods_For_WooCommerce
*/
class Estonian_Shipping_Methods_For_WooCommerce {
/**
* Instance
*
* @var null
*/
private static $instance = null;
/**
* This plugins methods
*
* @var array
*/
public $methods = array(
// Smartpost.
'WC_Estonian_Shipping_Method_Smartpost_Estonia' => false,
'WC_Estonian_Shipping_Method_Smartpost_Finland' => false,
'WC_Estonian_Shipping_Method_Smartpost_Courier' => false,
// Omniva.
'WC_Estonian_Shipping_Method_Omniva_Parcel_Machines_EE' => false,
'WC_Estonian_Shipping_Method_Omniva_Parcel_Machines_LV' => false,
'WC_Estonian_Shipping_Method_Omniva_Parcel_Machines_LT' => false,
// Omniva Post Offices
'WC_Estonian_Shipping_Method_Omniva_Post_Offices_EE' => false,
// DPD.
'WC_Estonian_Shipping_Method_DPD_Shops_EE' => false,
'WC_Estonian_Shipping_Method_DPD_Shops_LV' => false,
'WC_Estonian_Shipping_Method_DPD_Shops_LT' => false,
// Collect.net.
'WC_Estonian_Shipping_Method_Collect_Net' => false,
);
/**
* Class constructor
*/
public function __construct() {
// Load plugin functionality when others have loaded.
add_action( 'plugins_loaded', array( $this, 'plugins_loaded' ) );
}
/**
* Initialize plugin
* @return void
*/
public function plugins_loaded() {
// Check if shipping methods are available.
if ( ! $this->is_shipping_class_available() ) {
return false;
}
// Load functionality, translations
$this->includes();
$this->load_translations();
// Shipping
add_action( 'woocommerce_shipping_init', array( $this, 'shipping_init' ) );
// Add shipping methods
add_filter( 'woocommerce_shipping_methods', array( $this, 'register_shipping_methods' ) );
// Allow WC template file search in this plugin
add_filter( 'woocommerce_locate_template', array( $this, 'locate_template' ), 20, 3 );
add_filter( 'woocommerce_locate_core_template', array( $this, 'locate_template' ), 20, 3 );
add_action( 'woocommerce_view_order', array( $this, 'load_shipping_method' ), 1, 1 );
add_action( 'woocommerce_email', array( $this, 'load_shipping_method' ), 1, 1 );
}
/**
* Require functionality
*
* @return void
*/
public function includes() {
// Compatibility helpers
require_once WC_ESTONIAN_SHIPPING_METHODS_INCLUDES_PATH . '/compatibility-helpers.php';
// Abstract classes
require_once WC_ESTONIAN_SHIPPING_METHODS_INCLUDES_PATH . '/abstracts/class-wc-estonian-shipping-method.php';
require_once WC_ESTONIAN_SHIPPING_METHODS_INCLUDES_PATH . '/abstracts/class-wc-estonian-shipping-method-terminals.php';
require_once WC_ESTONIAN_SHIPPING_METHODS_INCLUDES_PATH . '/abstracts/class-wc-estonian-shipping-method-smartpost.php';
require_once WC_ESTONIAN_SHIPPING_METHODS_INCLUDES_PATH . '/abstracts/class-wc-estonian-shipping-method-omniva.php';
require_once WC_ESTONIAN_SHIPPING_METHODS_INCLUDES_PATH . '/abstracts/class-wc-estonian-shipping-method-dpd-shops.php';
// Methods
require_once WC_ESTONIAN_SHIPPING_METHODS_INCLUDES_PATH . '/methods/class-wc-estonian-shipping-method-smartpost-estonia.php';
require_once WC_ESTONIAN_SHIPPING_METHODS_INCLUDES_PATH . '/methods/class-wc-estonian-shipping-method-smartpost-finland.php';
require_once WC_ESTONIAN_SHIPPING_METHODS_INCLUDES_PATH . '/methods/class-wc-estonian-shipping-method-smartpost-courier.php';
require_once WC_ESTONIAN_SHIPPING_METHODS_INCLUDES_PATH . '/methods/class-wc-estonian-shipping-method-omniva-parcel-machines-ee.php';
require_once WC_ESTONIAN_SHIPPING_METHODS_INCLUDES_PATH . '/methods/class-wc-estonian-shipping-method-omniva-parcel-machines-lv.php';
require_once WC_ESTONIAN_SHIPPING_METHODS_INCLUDES_PATH . '/methods/class-wc-estonian-shipping-method-omniva-parcel-machines-lt.php';
require_once WC_ESTONIAN_SHIPPING_METHODS_INCLUDES_PATH . '/methods/class-wc-estonian-shipping-method-omniva-post-offices-ee.php';
require_once WC_ESTONIAN_SHIPPING_METHODS_INCLUDES_PATH . '/methods/class-wc-estonian-shipping-method-dpd-shops-ee.php';
require_once WC_ESTONIAN_SHIPPING_METHODS_INCLUDES_PATH . '/methods/class-wc-estonian-shipping-method-dpd-shops-lv.php';
require_once WC_ESTONIAN_SHIPPING_METHODS_INCLUDES_PATH . '/methods/class-wc-estonian-shipping-method-dpd-shops-lt.php';
require_once WC_ESTONIAN_SHIPPING_METHODS_INCLUDES_PATH . '/methods/class-wc-estonian-shipping-method-collect-net.php';
}
/**
* Force loading the shipping method on, for example, "view order" page,
* otherwise selected terminal will not be shown
*
* @param integer $order_id Order ID
* @return void
*/
public function load_shipping_method( $order_id ) {
WC()->shipping();
}
/**
* Construct our shipping methods for hooks, etc
*
* @return void
*/
public function shipping_init() {
foreach ( $this->methods as $method_id => $method ) {
$this->methods[ $method_id ] = new $method_id();
}
}
/**
* Check if WooCommerce WC_Shipping_Method class exists
*
* @return boolean True if it does
*/
public function is_shipping_class_available() {
return class_exists( 'WC_Shipping_Method' );
}
/**
* Load translations
*
* Allows overriding the offical translation by placing
* the translation files in wp-content/languages/estonian-shipping-methods-for-woocommerce
*
* @return void
*/
public function load_translations() {
$domain = 'wc-estonian-shipping-methods';
$locale = apply_filters( 'plugin_locale', get_locale(), $domain );
load_textdomain( $domain, WP_LANG_DIR . '/estonian-shipping-methods-for-woocommerce/' . $domain . '-' . $locale . '.mo' );
load_plugin_textdomain( $domain, '', dirname( plugin_basename( WC_ESTONIAN_SHIPPING_METHODS_MAIN_FILE ) ) . '/languages/' );
}
/**
* Register shipping methods
*
* @param array $methods Shipping methods
* @return array Shipping methods
*/
public function register_shipping_methods( $methods ) {
// Add methods
foreach ( $this->methods as $method_id => $method ) {
$methods[ $method_id ] = $method;
}
return $methods;
}
/**
* 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__ ) );
}
/**
* Locates the WooCommerce template files from this plugin directory
*
* @param string $template Already found template
* @param string $template_name Searchable template name
* @param string $template_path Template path
* @return string Search result for the template
*/
public function locate_template( $template, $template_name, $template_path ) {
// Tmp holder
$_template = $template;
if ( ! $template_path ) {
$template_path = WC_TEMPLATE_PATH;
}
// Set our base path
$plugin_path = $this->plugin_path() . '/woocommerce/';
// Look within passed path within the theme - this is priority
$template = locate_template(
array(
trailingslashit( $template_path ) . $template_name,
$template_name,
)
);
// Get the template from this plugin, if it exists
if ( ! $template && file_exists( $plugin_path . $template_name ) ) {
$template = $plugin_path . $template_name;
}
// Use default template
if ( ! $template ) {
$template = $_template;
}
// Return what we found
return $template;
}
/**
* Fetch instance of this plugin
*
* @return Estonian_Shipping_Methods_For_WooCommerce
*/
public static function instance() {
if ( ! isset( self::$instance ) ) {
self::$instance = new self;
}
return self::$instance;
}
}
/**
* Returns the main instance of Estonian_Shipping_Methods_For_WooCommerce to prevent the need to use globals.
* @return Estonian_Shipping_Methods_For_WooCommerce
*/
function WC_Estonian_Shipping_Methods() {
return Estonian_Shipping_Methods_For_WooCommerce::instance();
}
// Global for backwards compatibility.
$GLOBALS['wc_estonian_shipping_methods'] = WC_Estonian_Shipping_Methods();