forked from PostiDigital/woocommerce-shipping-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathposti-shipping.php
169 lines (142 loc) · 5.35 KB
/
posti-shipping.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
<?php
/**
* Plugin Name: Posti Shipping
* Version: 1.0.0
* Plugin URI: https://github.com/PostiDigital/woocommerce-shipping-plugin
* Description: Posti shipping service for WooCommerce.
* Author: Posti
* Author URI: https://www.posti.fi/
* Text Domain: woo-posti_shipping
* Domain Path: /languages/
* License: GPL v3 or later
*
* WC requires at least: 3.4
* WC tested up to: 4.1
*
* Copyright: © 2017-2019 Seravo Oy
* License: GNU General Public License v3.0
* License URI: http://www.gnu.org/licenses/gpl-3.0.html
*/
// Prevent direct access to this script
if ( ! defined('ABSPATH') ) {
exit;
}
/**
* Autoloader loads nothing but Pakettikauppa libraries. The classname of the generated autoloader is not unique,
* posti_shipping forks use the same autoloader which results in a fatal error if the main plugin and a posti_shipping plugin
* co-exist.
*/
if ( ! class_exists('\Pakettikauppa\Client') ) {
require_once __DIR__ . '/vendor/autoload.php';
}
require_once 'core/class-core.php';
class Woo_Posti_Shipping extends Woo_Pakettikauppa_Core\Core {
public $prefix = 'woo_posti_shipping';
public function __construct( $config = [] ) {
parent::__construct($config);
add_action('admin_enqueue_scripts', array( $this, 'enqueue_admin_scripts' ));
add_action('woocommerce_before_checkout_form', array( $this, 'enqueue_frontend_scripts' ));
add_action(
'init',
function() {
if ( apply_filters('woo_posti_shipping_enable_setup_wizard', true) && current_user_can('manage_woocommerce') ) {
add_action('admin_enqueue_scripts', array( $this, 'enqueue_setup_scripts' ));
}
}
);
}
public function can_load() {
if ( class_exists('Woo_Pakettikauppa') ) {
add_action(
'admin_notices',
function() {
echo '<div class="notice notice-error">';
echo '<p>' . $this->text->activated_core_plugin_error() . '</p>';
echo '</div>';
}
);
return false;
}
return true;
}
public function enqueue_setup_scripts() {
wp_enqueue_style('woo_posti_shipping_admin_setup', $this->dir_url . 'posti_shipping/assets/admin-setup.css', array(), $this->version);
wp_enqueue_style('wp-admin');
wp_enqueue_style('buttons');
}
public function enqueue_admin_scripts() {
// wp_enqueue_style('woo_posti_shipping_admin', $this->dir_url . 'posti_shipping/assets/admin.css', array(), $this->version);
// wp_enqueue_script('woo_posti_shipping_admin_js', $this->dir_url . 'assets/js/admin.js', array( 'jquery' ), $this->version, true);
}
public function enqueue_frontend_scripts() {
// wp_enqueue_style('woo_posti_shipping', $this->dir_url . '/posti_shipping/assets/frontend.css', array(), $this->version);
// wp_enqueue_script('woo_posti_shipping_js', $this->dir_url . '/assets/js/frontend.js', array( 'jquery' ), $this->version, true);
}
protected function load_shipment_class() {
require_once 'core/class-shipment.php';
require_once 'posti_shipping/classes/class-shipment.php';
$shipment = new \Woo_Posti_Shipping\Shipment($this);
$shipment->load();
return $shipment;
}
public function add_shipping_method() {
add_filter(
'woocommerce_shipping_methods',
function( $methods ) {
// Ideally we'd control the class init ourselves, but the legacy shipping method doesn't work
// if WC doesn't control it.
// $methods[$this->shippingmethod] = $this->shipping_method_instance;
$methods[$this->shippingmethod] = '\Woo_Posti_Shipping\Shipping_Method';
return $methods;
}
);
}
public function load_textdomain() {
parent::load_textdomain();
load_plugin_textdomain(
'woo_posti_shipping',
false,
dirname($this->basename) . '/posti_shipping/languages/'
);
}
protected function load_shipping_method_class() {
require_once 'core/class-shipping-method.php';
require_once 'posti_shipping/classes/class-shipping-method.php';
$method = new \Woo_Posti_Shipping\Shipping_Method();
// We can't inject the core to the shipping method class if WooCommerce controls
// the init of it. This class was turned into a singleton to go around that.
// $method->injectCore($this)->load();
// $method->load();
return $method;
}
protected function load_text_class() {
require_once 'core/class-text.php';
require_once 'posti_shipping/classes/class-text.php';
return new \Woo_Posti_Shipping\Text($this);
}
}
$instance = new Woo_Posti_Shipping(
[
'root' => __FILE__,
'version' => get_file_data(__FILE__, array( 'Version' ), 'plugin')[0],
'shipping_method_name' => 'posti_shipping_method',
'vendor_name' => 'Posti',
'vendor_url' => 'https://www.posti.fi/',
'vendor_logo' => 'assets/img/posti-logo.png',
'setup_background' => 'assets/img/posti-background.jpg',
'setup_page' => 'wcpk-setup',
'pakettikauppa_api_config' => [
'production' => [
'base_uri' => 'https://nextshipping.posti.fi',
'use_posti_auth' => true,
'posti_auth_url' => 'https://oauth.posti.com',
],
'test' => [
'base_uri' => 'https://nextshipping.posti.fi',
'use_posti_auth' => true,
'posti_auth_url' => 'https://oauth.posti.com',
],
], // Overrides defaults and UI settings
// 'pakettikauppa_api_comment' => 'From WooCommerce', // Overrides default
]
);