-
Notifications
You must be signed in to change notification settings - Fork 138
Expand file tree
/
Copy pathautoupgrade.php
More file actions
284 lines (245 loc) · 9.42 KB
/
Copy pathautoupgrade.php
File metadata and controls
284 lines (245 loc) · 9.42 KB
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
<?php
/**
* For the full copyright and license information, please view the
* LICENSE.md file that was distributed with this source code.
*/
class Autoupgrade extends Module
{
/**
* @var int
*/
public $multishop_context;
/**
* @var \PrestaShop\Module\AutoUpgrade\UpgradeContainer
*/
protected $container;
public function __construct()
{
$this->name = 'autoupgrade';
$this->tab = 'administration';
$this->author = 'PrestaShop';
$this->version = '7.6.5';
$this->need_instance = 1;
$this->module_key = '926bc3e16738b7b834f37fc63d59dcf8';
$this->bootstrap = true;
parent::__construct();
$this->multishop_context = Shop::CONTEXT_ALL;
$this->displayName = $this->trans('Update Assistant');
$this->description = $this->trans('The Update Assistant module helps you backup, update and restore your PrestaShop store. With just a few clicks, you can move to the latest version of PrestaShop with confidence.');
$this->ps_versions_compliancy = ['min' => '1.7.0.0', 'max' => _PS_VERSION_];
}
/**
* following the Core documentation :
* https://devdocs.prestashop-project.org/8/modules/creation/module-translation/new-system/#translating-your-module
*
* @return bool
*/
public function isUsingNewTranslationSystem()
{
return true;
}
/**
* @return bool
*/
public function install()
{
require_once _PS_ROOT_DIR_ . '/modules/autoupgrade/classes/VersionUtils.php';
if (!\PrestaShop\Module\AutoUpgrade\VersionUtils::isActualPHPVersionCompatible()) {
$this->_errors[] = $this->trans(
'This module requires PHP %s to work properly. Please upgrade your server configuration.',
[\PrestaShop\Module\AutoUpgrade\VersionUtils::getHumanReadableVersionOf(\PrestaShop\Module\AutoUpgrade\VersionUtils::MODULE_COMPATIBLE_PHP_VERSION)]
);
return false;
}
// If the "AdminSelfUpgrade" tab does not exist yet, create it
$moduleTabName = 'AdminSelfUpgrade';
if (!Tab::getIdFromClassName($moduleTabName)) {
$tab = new Tab();
$tab->class_name = $moduleTabName;
$tab->icon = 'arrow_upward';
$tab->module = 'autoupgrade';
// We use DEFAULT to add Upgrade tab as a standalone tab in the back office menu
$tab->id_parent = (int) Tab::getIdFromClassName('CONFIGURE');
foreach (Language::getLanguages(false) as $lang) {
$tab->name[(int) $lang['id_lang']] = 'Update assistant';
}
if (!$tab->save()) {
return $this->_abortInstall($this->trans('Unable to create the %s tab', [$moduleTabName]));
}
}
$ajaxTabName = 'AdminAutoupgradeAjax';
if (!Tab::getIdFromClassName($ajaxTabName)) {
$ajaxTab = new Tab();
$ajaxTab->class_name = $ajaxTabName;
$ajaxTab->module = 'autoupgrade';
$ajaxTab->id_parent = -1;
foreach (Language::getLanguages(false) as $lang) {
$ajaxTab->name[(int) $lang['id_lang']] = 'Update assistant';
}
if (!$ajaxTab->save()) {
return $this->_abortInstall($this->trans('Unable to create the %s tab', [$ajaxTabName]));
}
}
return parent::install() && $this->registerHook('displayBackOfficeHeader') && $this->registerHook('displayBackOfficeEmployeeMenu');
}
/**
* @return bool
*/
public function uninstall()
{
// Delete the module Back-office tab
$id_tab = Tab::getIdFromClassName('AdminSelfUpgrade');
if ($id_tab) {
$tab = new Tab((int) $id_tab);
$tab->delete();
}
$id_ajax_tab = Tab::getIdFromClassName('AdminAutoupgradeAjax');
if ($id_ajax_tab) {
$ajaxTab = new Tab((int) $id_ajax_tab);
$ajaxTab->delete();
}
if ($this->initAutoloaderIfCompliant()) {
// Remove the 'autoupgrade' admin directory except backups
$this->getUpgradeContainer()->getFilesystemAdapter()->clearDirectory(_PS_ADMIN_DIR_ . DIRECTORY_SEPARATOR . 'autoupgrade', ['backup']);
}
return parent::uninstall();
}
/**
* @return void
*/
public function getContent()
{
Tools::redirectAdmin($this->context->link->getAdminLink('AdminSelfUpgrade'));
}
/**
* Set installation errors and return false.
*
* @param string $error Installation abortion reason
*
* @return bool Always false
*/
protected function _abortInstall($error)
{
$this->_errors[] = $error;
return false;
}
/**
* Adapter for trans calls, existing only on PS 1.7.
* Making them available for PS 1.6 as well.
*
* @param string $id
* @param array<int|string, int|string> $parameters $parameters
* @param string $domain
* @param string $locale
*
* @return string
*/
public function trans($id, array $parameters = [], $domain = null, $locale = null)
{
require_once _PS_ROOT_DIR_ . '/modules/autoupgrade/classes/UpgradeTools/Translator.php';
$translator = new \PrestaShop\Module\AutoUpgrade\UpgradeTools\Translator(
_PS_ROOT_DIR_ . DIRECTORY_SEPARATOR . 'modules' . DIRECTORY_SEPARATOR . 'autoupgrade' . DIRECTORY_SEPARATOR . 'translations' . DIRECTORY_SEPARATOR,
\Context::getContext()->language->iso_code
);
return $translator->trans($id, $parameters);
}
/**
* Hook called after the backoffice content is rendered.
* Used to display the update notification dialog.
*
* @return string
*
* @throws \PrestaShop\Module\AutoUpgrade\Exceptions\DistributionApiException
* @throws \PrestaShop\Module\AutoUpgrade\Exceptions\ProcessException
* @throws \Twig\Error\LoaderError
* @throws \Twig\Error\RuntimeError
* @throws \Twig\Error\SyntaxError
*/
public function hookDisplayBackOfficeHeader()
{
if (!$this->initAutoloaderIfCompliant()) {
return '';
}
if (isset($this->context->controller->ajax) && $this->context->controller->ajax) {
return '';
}
return (new \PrestaShop\Module\AutoUpgrade\Hooks\DisplayBackOfficeHeader(
$this->getUpgradeContainer(),
$this->getCurrentRequest()
))->renderUpdateNotification();
}
/**
* Only available from PS8.
*
* @param array{links: \PrestaShop\PrestaShop\Core\Action\ActionsBarButtonsCollection} $params
*
* @return void
*/
public function hookDisplayBackOfficeEmployeeMenu(array $params)
{
if (
!$this->initAutoloaderIfCompliant()
|| !class_exists(\PrestaShop\PrestaShop\Core\Action\ActionsBarButtonsCollection::class)
|| !class_exists(\PrestaShop\PrestaShop\Core\Action\ActionsBarButton::class)
// @phpstan-ignore instanceof.alwaysTrue (Depends on the PrestaShop version)
|| !($params['links'] instanceof \PrestaShop\PrestaShop\Core\Action\ActionsBarButtonsCollection)
) {
return;
}
$params['links']->add(
new \PrestaShop\PrestaShop\Core\Action\ActionsBarButton(
__CLASS__,
[
'link' => \PrestaShop\Module\AutoUpgrade\DocumentationLinks::getPrestashopReleasesUrl(),
'icon' => 'history',
'isExternalLink' => true,
],
$this->trans('Discover the latest releases')
)
);
}
/**
* @return bool
*/
public function initAutoloaderIfCompliant()
{
require_once _PS_ROOT_DIR_ . DIRECTORY_SEPARATOR . 'modules' . DIRECTORY_SEPARATOR . 'autoupgrade' . DIRECTORY_SEPARATOR . 'classes' . DIRECTORY_SEPARATOR . 'VersionUtils.php';
if (!\PrestaShop\Module\AutoUpgrade\VersionUtils::isActualPHPVersionCompatible()) {
return false;
}
$autoloadPath = __DIR__ . DIRECTORY_SEPARATOR . 'vendor' . DIRECTORY_SEPARATOR . 'autoload.php';
if (file_exists($autoloadPath)) {
require_once $autoloadPath;
}
return true;
}
/**
* @return \PrestaShop\Module\AutoUpgrade\UpgradeContainer
*/
public function getUpgradeContainer()
{
if (null === $this->container) {
$this->container = new \PrestaShop\Module\AutoUpgrade\UpgradeContainer(_PS_ROOT_DIR_, realpath(_PS_ADMIN_DIR_));
}
return $this->container;
}
/**
* @return \Symfony\Component\HttpFoundation\Request
*/
public function getCurrentRequest()
{
// When possible, retrieve an existing instance of Request to avoid issues on pages with uploaded files.
// We are compatible with PS 1.7.0 BUT:
// - Module::get() exists since PS 1.7.3.
// - Service "request_stack" is found from PS 8.
if (version_compare(_PS_VERSION_, '8.0.0', '>=')) {
/** @var \Symfony\Component\HttpFoundation\RequestStack $requestStack */
$requestStack = $this->get('request_stack');
$request = $requestStack->getCurrentRequest();
if ($request) {
return $request;
}
}
return \Symfony\Component\HttpFoundation\Request::createFromGlobals();
}
}