-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathwoocommerce-myparcel.php
executable file
·167 lines (139 loc) · 4.39 KB
/
woocommerce-myparcel.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
<?php
/** @noinspection AutoloadingIssuesInspection */
declare(strict_types=1);
/*
Plugin Name: MyParcelNL
Plugin URI: https://github.com/myparcelnl/woocommerce
Description: Export your WooCommerce orders to MyParcel and print labels directly from the WooCommerce admin
Author: MyParcel
Author URI: https://myparcel.nl
Version: 5.3.1
License: MIT
License URI: http://www.opensource.org/licenses/mit-license.php
*/
use Automattic\WooCommerce\Blocks\Integrations\IntegrationRegistry;
use MyParcelNL\Pdk\Base\Pdk as PdkInstance;
use MyParcelNL\Pdk\Facade\Installer;
use MyParcelNL\Pdk\Facade\Pdk;
use MyParcelNL\WooCommerce\Facade\WooCommerce;
use MyParcelNL\WooCommerce\Integration\WcBlocksLoader;
use MyParcelNL\WooCommerce\Service\WordPressHookService;
use function MyParcelNL\WooCommerce\bootPdk;
require(plugin_dir_path(__FILE__) . 'vendor/autoload.php');
final class MyParcelNLWooCommerce
{
/**
* @throws \Throwable
*/
public function __construct()
{
register_activation_hook(__FILE__, [$this, 'install']);
register_deactivation_hook(__FILE__, [$this, 'uninstall']);
add_action('init', [$this, 'initialize'], 9999);
add_action('woocommerce_blocks_checkout_block_registration', [$this, 'registerCheckoutBlocks']);
}
/**
* Perform required tasks that initialize the plugin.
*
* @throws \Throwable
*/
public function initialize(): void
{
$this->boot();
/** @var WordPressHookService $hookService */
$hookService = Pdk::get(WordPressHookService::class);
$hookService->applyAll();
}
/**
* @return void
* @throws \Exception
*/
public function install(): void
{
$this->boot();
$errors = $this->checkPrerequisites();
if (! empty($errors)) {
/** @noinspection ForgottenDebugOutputInspection */
wp_die(implode('<br>', $errors), '', ['back_link' => true]);
}
Installer::install();
}
/**
* @param \Automattic\WooCommerce\Blocks\Integrations\IntegrationRegistry $integrationRegistry
*
* @return void
* @throws \Throwable
*/
public function registerCheckoutBlocks(IntegrationRegistry $integrationRegistry): void
{
$this->initialize();
/** @var \MyParcelNL\WooCommerce\Integration\WcBlocksLoader $loader */
$loader = Pdk::get(WcBlocksLoader::class);
$loader->setRegistry($integrationRegistry);
$loader->registerBlocks(Pdk::get('wooCommerceBlocksCheckout'));
}
/**
* @return void
* @throws \Exception
*/
public function uninstall(): void
{
$this->boot();
Installer::uninstall();
}
/**
* @return void
* @throws \Exception
*/
private function boot(): void
{
$version = $this->getVersion();
bootPdk(
'myparcelnl',
'MyParcel',
$version,
plugin_dir_path(__FILE__),
plugin_dir_url(__FILE__),
constant('WP_DEBUG')
? PdkInstance::MODE_DEVELOPMENT
: PdkInstance::MODE_PRODUCTION
);
if (! defined('MYPARCELNL_WC_VERSION')) {
define('MYPARCELNL_WC_VERSION', $version);
}
$errors = $this->checkPrerequisites();
if (! empty($errors)) {
add_action('admin_init', static function () use ($errors) {
add_action('admin_notices', static function () use ($errors) {
echo sprintf('<div class="error"><p>%s</p></div>', implode('<br>', $errors));
});
deactivate_plugins(plugin_basename(__FILE__));
});
}
}
/**
* Check if the minimum requirements are met.
*
* @return array
*/
private function checkPrerequisites(): array
{
$errors = [];
if (! Pdk::get('isPhpVersionSupported')) {
$errors[] = Pdk::get('errorMessagePhpVersion');
}
if (! WooCommerce::isActive() || ! Pdk::get('isWooCommerceVersionSupported')) {
$errors[] = Pdk::get('errorMessageWooCommerceVersion');
}
return $errors;
}
/**
* @return string
*/
private function getVersion(): string
{
$composerJson = json_decode(file_get_contents(__DIR__ . '/composer.json'), false);
return $composerJson->version;
}
}
new MyParcelNLWooCommerce();