PluginProbe ʕ •ᴥ•ʔ
WP STAGING – WordPress Backup, Restore, Migration & Clone / 3.3.1
WP STAGING – WordPress Backup, Restore, Migration & Clone v3.3.1
4.9.1 4.9.0 4.8.1 trunk 3.0.0 3.0.1 3.0.2 3.0.3 3.0.4 3.0.5 3.0.6 3.1.0 3.1.1 3.1.2 3.1.3 3.1.4 3.10.0 3.2.0 3.3.1 3.3.2 3.3.3 3.4.1 3.4.3 3.5.0 3.6.0 3.7.1 3.8.0 3.8.1 3.8.2 3.8.3 3.8.4 3.8.5 3.8.6 3.8.7 3.9.0 3.9.1 3.9.2 3.9.3 3.9.4 4.0.0 4.1.0 4.1.1 4.1.2 4.1.3 4.1.4 4.2.0 4.2.1 4.3.0 4.3.1 4.3.2 4.4.0 4.5.0 4.6.0 4.7.0 4.7.1 4.7.2 4.7.3 4.8.0
wp-staging / Framework / Assets / Assets.php
wp-staging / Framework / Assets Last commit date
Assets.php 2 years ago
Assets.php
497 lines
1 <?php
2
3 namespace WPStaging\Framework\Assets;
4
5 use WPStaging\Core\DTO\Settings;
6 use WPStaging\Core\WPStaging;
7 use WPStaging\Framework\Filesystem\Scanning\ScanConst;
8 use WPStaging\Framework\Security\AccessToken;
9 use WPStaging\Framework\Security\Nonce;
10 use WPStaging\Framework\Traits\ResourceTrait;
11 use WPStaging\Framework\SiteInfo;
12 use WPStaging\Backup\Task\Tasks\JobBackup\DatabaseBackupTask;
13 use WPStaging\Framework\Analytics\AnalyticsConsent;
14
15 class Assets
16 {
17 use ResourceTrait;
18
19 /**
20 * Default admin bar background color for staging site
21 * @var string
22 */
23 const DEFAULT_ADMIN_BAR_BG = "#ff8d00";
24
25 private $accessToken;
26
27 private $settings;
28
29 private $analyticsConsent;
30
31 public function __construct(AccessToken $accessToken, Settings $settings, AnalyticsConsent $analyticsConsent)
32 {
33 $this->accessToken = $accessToken;
34 $this->settings = $settings;
35 $this->analyticsConsent = $analyticsConsent;
36 }
37
38 /**
39 * Prepend the URL to the assets to the given file
40 *
41 * @param string $assetsFile optional
42 * @return string
43 */
44 public function getAssetsUrl($assetsFile = '')
45 {
46 return WPSTG_PLUGIN_URL . "assets/$assetsFile";
47 }
48
49 /**
50 * Get the version the given file. Use for caching
51 *
52 * @param string $assetsFile
53 * @param string $assetsVersion use WPStaging::getVersion() instead if not given
54 * @return string
55 */
56 public function getAssetsUrlWithVersion($assetsFile, $assetsVersion = '')
57 {
58 $url = $this->getAssetsUrl($assetsFile);
59 $ver = empty($assetsVersion) ? $this->getAssetsVersion($assetsFile, $assetsVersion) : $assetsVersion;
60 return $url . '?v=' . $ver;
61 }
62
63 /**
64 * Prepend the Path to the assets to the given file
65 *
66 * @param string $assetsFile optional
67 * @return string
68 */
69 public function getAssetsPath($assetsFile = '')
70 {
71 return WPSTG_PLUGIN_DIR . "assets/$assetsFile";
72 }
73
74 /**
75 * Get the version the given file. Use for caching
76 *
77 * @param string $assetsFile
78 * @param string $assetsVersion Optional, use WPStaging::getVersion() instead if not given
79 * @return string|int
80 */
81 public function getAssetsVersion($assetsFile, $assetsVersion = '')
82 {
83 $filename = $this->getAssetsPath($assetsFile);
84 $filemtime = file_exists($filename) ? @filemtime($filename) : false;
85
86 if ($filemtime !== false) {
87 return $filemtime;
88 } else {
89 return $assetsVersion !== '' ? $assetsVersion : WPStaging::getVersion();
90 }
91 }
92
93 /**
94 * @action admin_enqueue_scripts 100 1
95 * @action wp_enqueue_scripts 100 1
96 */
97 public function enqueueElements($hook)
98 {
99
100 $this->loadGlobalAssets($hook);
101
102 // Load this css file on frontend and backend on all pages if current site is a staging site
103 if ((new SiteInfo())->isStagingSite()) {
104 wp_register_style('wpstg-admin-bar', false);
105 wp_enqueue_style('wpstg-admin-bar');
106 wp_add_inline_style('wpstg-admin-bar', $this->getStagingAdminBarColor());
107 }
108
109 // Load feedback form js file on page plugins.php in free version or in free dev version
110 if (!WPStaging::isPro() && $this->isPluginsPage()) {
111 $asset = 'js/dist/wpstg-admin-plugins.min.js';
112 if ($this->isDebugOrDevMode()) {
113 $asset = 'js/dist/wpstg-admin-plugins.js';
114 }
115
116 wp_enqueue_script(
117 "wpstg-admin-script",
118 $this->getAssetsUrl($asset),
119 ["jquery"],
120 $this->getAssetsVersion($asset),
121 false
122 );
123
124 $asset = 'css/dist/wpstg-admin-feedback.min.css';
125 if ($this->isDebugOrDevMode()) {
126 $asset = 'css/dist/wpstg-admin-feedback.css';
127 }
128
129 wp_enqueue_style(
130 "wpstg-admin-feedback",
131 $this->getAssetsUrl($asset),
132 [],
133 $this->getAssetsVersion($asset)
134 );
135 }
136
137 // Load js file on page plugins.php for pro version
138 if (WPStaging::isPro()) {
139 $asset = 'js/dist/pro/wpstg-admin-all-pages.min.js';
140 if ($this->isDebugOrDevMode()) {
141 $asset = 'js/dist/pro/wpstg-admin-all-pages.js';
142 }
143
144 wp_enqueue_script(
145 "wpstg-admin-all-pages-script",
146 $this->getAssetsUrl($asset),
147 ["jquery"],
148 $this->getAssetsVersion($asset),
149 false
150 );
151
152 $asset = 'css/dist/wpstg-admin-all-pages.min.css';
153 if ($this->isDebugOrDevMode()) {
154 $asset = 'css/dist/wpstg-admin-all-pages.css';
155 }
156
157 wp_enqueue_style(
158 "wpstg-admin-all-pages-style",
159 $this->getAssetsUrl($asset),
160 [],
161 $this->getAssetsVersion($asset)
162 );
163 }
164
165 // Load below assets only on wp staging admin pages
166 if ($this->isNotWPStagingAdminPage($hook)) {
167 return;
168 }
169
170 // Load admin js files
171 $asset = 'js/dist/wpstg.js';
172 wp_enqueue_script(
173 "wpstg-common",
174 $this->getAssetsUrl($asset),
175 ["jquery"],
176 $this->getAssetsVersion($asset),
177 false
178 );
179
180 // Load admin js files
181 $asset = 'js/dist/wpstg-admin.min.js';
182 if ($this->isDebugOrDevMode()) {
183 $asset = 'js/dist/wpstg-admin.js';
184 }
185
186 wp_enqueue_script(
187 "wpstg-admin-script",
188 $this->getAssetsUrl($asset),
189 ["wpstg-common", "wpstg-admin-notyf", "wpstg-admin-sweetalerts"],
190 $this->getAssetsVersion($asset),
191 false
192 );
193
194 // Sweet Alert
195 $asset = 'js/dist/wpstg-sweetalert2.min.js';
196 if ($this->isDebugOrDevMode()) {
197 $asset = 'js/dist/wpstg-sweetalert2.js';
198 }
199
200 wp_enqueue_script(
201 'wpstg-admin-sweetalerts',
202 $this->getAssetsUrl($asset),
203 [],
204 $this->getAssetsVersion($asset),
205 true
206 );
207
208 $asset = 'css/dist/wpstg-sweetalert2.min.css';
209 if ($this->isDebugOrDevMode()) {
210 $asset = 'css/dist/wpstg-sweetalert2.css';
211 }
212
213 wp_enqueue_style(
214 'wpstg-admin-sweetalerts',
215 $this->getAssetsUrl($asset),
216 [],
217 $this->getAssetsVersion($asset)
218 );
219
220 // Notyf Toast Notification
221 $asset = 'js/vendor/notyf.min.js';
222 wp_enqueue_script(
223 'wpstg-admin-notyf',
224 $this->getAssetsUrl($asset),
225 [],
226 $this->getAssetsVersion($asset),
227 true
228 );
229
230 $asset = 'css/vendor/notyf.min.css';
231 wp_enqueue_style(
232 'wpstg-admin-notyf',
233 $this->getAssetsUrl($asset),
234 [],
235 $this->getAssetsVersion($asset)
236 );
237
238 // Internal hook to enqueue backup scripts, used by the backup addon
239 do_action('wpstg_enqueue_backup_scripts', $this->isDebugOrDevMode());
240
241 // Load admin js pro files
242 if (defined('WPSTGPRO_VERSION')) {
243 $asset = 'js/dist/pro/wpstg-admin-pro.min.js';
244 if ($this->isDebugOrDevMode()) {
245 $asset = 'js/dist/pro/wpstg-admin-pro.js';
246 }
247
248 wp_enqueue_script(
249 "wpstg-admin-pro-script",
250 $this->getAssetsUrl($asset),
251 ["jquery", "wpstg-admin-notyf", "wpstg-admin-sweetalerts"],
252 $this->getAssetsVersion($asset),
253 false
254 );
255 }
256
257 // Load admin css files
258 $asset = 'css/dist/wpstg-admin.min.css';
259 if ($this->isDebugOrDevMode()) {
260 $asset = 'css/dist/wpstg-admin.css';
261 }
262
263 wp_enqueue_style(
264 "wpstg-admin",
265 $this->getAssetsUrl($asset),
266 [],
267 $this->getAssetsVersion($asset)
268 );
269
270 $wpstgConfig = [
271 //"delayReq" => $this->getDelay(),
272 "delayReq" => 0,
273 // TODO: move directorySeparator to consts?
274 "settings" => (object)[
275 "directorySeparator" => ScanConst::DIRECTORIES_SEPARATOR
276 ],
277 "tblprefix" => WPStaging::getTablePrefix(),
278 "isMultisite" => is_multisite(),
279 AccessToken::REQUEST_KEY => (string)$this->accessToken->getToken() ?: (string)$this->accessToken->generateNewToken(),
280 'nonce' => wp_create_nonce(Nonce::WPSTG_NONCE),
281 'assetsUrl' => $this->getAssetsUrl(),
282 'ajaxUrl' => admin_url('admin-ajax.php'),
283 'wpstgIcon' => $this->getAssetsUrl('img/wpstaging-icon.png'),
284 'maxUploadChunkSize' => $this->getMaxUploadChunkSize(),
285 'backupDBExtension' => DatabaseBackupTask::PART_IDENTIFIER . '.' . DatabaseBackupTask::FILE_FORMAT,
286 'analyticsConsentAllow' => esc_url($this->analyticsConsent->getConsentLink(true)),
287 'analyticsConsentDeny' => esc_url($this->analyticsConsent->getConsentLink(false)),
288 'isPro' => WPStaging::isPro(),
289 // TODO: handle i18n translations through Class/Service Provider?
290 'i18n' => [
291 'dbConnectionSuccess' => esc_html__('Database Connection - Success', 'wp-staging'),
292 'dbConnectionFailed' => esc_html__('Database Connection - Failed', 'wp-staging'),
293 'somethingWentWrong' => esc_html__('Something went wrong.', 'wp-staging'),
294 'noRestoreFileFound' => esc_html__('No backup file found.', 'wp-staging'),
295 'selectFileToRestore' => esc_html__('Select backup file to restore.', 'wp-staging'),
296 'cloneResetComplete' => esc_html__('Reset Complete!', 'wp-staging'),
297 'cloneUpdateComplete' => esc_html__('Update Complete!', 'wp-staging'),
298 'success' => esc_html__('Success', 'wp-staging'),
299 'resetClone' => esc_html__('Reset Staging Site', 'wp-staging'),
300 'showLogs' => esc_html__('Show Logs', 'wp-staging'),
301 'hideLogs' => esc_html__('Hide Logs', 'wp-staging'),
302 'noTableSelected' => esc_html__('No table selected', 'wp-staging'),
303 'tablesSelected' => esc_html__('{d} tables(s) selected', 'wp-staging'),
304 'noFileSelected' => esc_html__('No file selected', 'wp-staging'),
305 'filesSelected' => esc_html__('{t} theme(s), {p} plugin(s) selected', 'wp-staging'),
306 'wpstg_cloning' => [
307 'title' => esc_html__('Staging Site Created Successfully!', 'wp-staging'),
308 'body' => esc_html__('You can access it from here:', 'wp-staging'),
309 ],
310 'wpstg_update' => [
311 'title' => esc_html__('Staging Site Updated Successfully!', 'wp-staging'),
312 'body' => esc_html__('You can access it from here:', 'wp-staging'),
313 ],
314 'wpstg_push_processing' => [
315 'title' => esc_html__('Staging Site Pushed Successfully!', 'wp-staging'),
316 'body' => esc_html__('Clear the site cache if changes are not visible.', 'wp-staging'),
317 ],
318 'wpstg_reset' => [
319 'title' => esc_html__('Staging Site Reset Successfully!', 'wp-staging'),
320 'body' => esc_html__('You can access it from here:', 'wp-staging'),
321 ],
322 'wpstg_delete_clone' => [
323 'title' => esc_html__('Staging Site Deleted Successfully!', 'wp-staging'),
324 ],
325 ],
326 ];
327
328 wp_localize_script("wpstg-admin-script", "wpstg", $wpstgConfig);
329 }
330
331 /**
332 * Load js vars globally but NOT on wp staging admin pages
333 * @return void
334 */
335 private function loadGlobalAssets($pageSlug)
336 {
337 if (!$this->isNotWPStagingAdminPage($pageSlug)) {
338 return;
339 }
340
341 wp_enqueue_script('wpstg-global', $this->getAssetsUrl('js/dist/wpstg-blank-loader.js'), [], [], false);
342
343 $vars = [
344 'nonce' => wp_create_nonce(Nonce::WPSTG_NONCE),
345 ];
346
347 wp_localize_script("wpstg-global", "wpstg", $vars);
348 }
349
350 /**
351 * @return int The max upload size for a file.
352 */
353 protected function getMaxUploadChunkSize()
354 {
355 $lowerLimit = 64 * KB_IN_BYTES;
356 $upperLimit = 16 * MB_IN_BYTES;
357
358 $maxPostSize = wp_convert_hr_to_bytes(ini_get('post_max_size'));
359 $uploadMaxFileSize = wp_convert_hr_to_bytes(ini_get('upload_max_filesize'));
360
361 // The real limit, read from the PHP context.
362 $limit = min($maxPostSize, $uploadMaxFileSize) * 0.90;
363
364 // Do not allow going over upper limit.
365 $limit = min($limit, $upperLimit);
366
367 // Do not allow going under lower limit.
368 $limit = max($lowerLimit, $limit);
369
370 return (int)$limit;
371 }
372
373 /**
374 * Load css and js files only on wp staging admin pages
375 *
376 * @param $page string slug of the current page
377 *
378 * @return bool
379 */
380 private function isNotWPStagingAdminPage($page)
381 {
382 if (defined('WPSTGPRO_VERSION')) {
383 $availablePages = [
384 "toplevel_page_wpstg_clone",
385 "toplevel_page_wpstg_backup",
386 "wp-staging-pro_page_wpstg_clone",
387 "wp-staging-pro_page_wpstg_backup",
388 "wp-staging-pro_page_wpstg-settings",
389 "wp-staging-pro_page_wpstg-tools",
390 "wp-staging-pro_page_wpstg-license",
391 ];
392 } else {
393 $availablePages = [
394 "toplevel_page_wpstg_clone",
395 "toplevel_page_wpstg_backup",
396 "wp-staging_page_wpstg_clone",
397 "wp-staging_page_wpstg_backup",
398 "wp-staging_page_wpstg-settings",
399 "wp-staging_page_wpstg-tools",
400 "wp-staging_page_wpstg-welcome",
401 ];
402 }
403
404 return !in_array($page, $availablePages) || !is_admin();
405 }
406
407 /**
408 * Remove heartbeat api and user login check
409 *
410 * @action admin_enqueue_scripts 100 1
411 * @see AssetServiceProvider.php
412 *
413 * @param bool $hook
414 */
415 public function removeWPCoreJs($hook)
416 {
417 if ($this->isNotWPStagingAdminPage($hook)) {
418 return;
419 }
420
421 // Disable user login status check
422 // Todo: Can we remove this now that we have AccessToken?
423 remove_action('admin_enqueue_scripts', 'wp_auth_check_load');
424
425 // Disable heartbeat check for cloning and pushing
426 wp_deregister_script('heartbeat');
427 }
428
429 /**
430 * Check if current page is plugins.php
431 * @global array $pagenow
432 * @return bool
433 */
434 private function isPluginsPage()
435 {
436 global $pagenow;
437
438 return ($pagenow === 'plugins.php');
439 }
440
441 /**
442 * @return int
443 */
444 /* public function getDelay()
445 {
446 switch ($this->settings->getDelayRequests()) {
447 case "0":
448 $delay = 0;
449 break;
450
451 case "1":
452 $delay = 1000;
453 break;
454
455 case "2":
456 $delay = 2000;
457 break;
458
459 case "3":
460 $delay = 3000;
461 break;
462
463 case "4":
464 $delay = 4000;
465 break;
466
467 default:
468 $delay = 0;
469 }
470
471 return $delay;
472 }*/
473
474 /**
475 * @return string
476 */
477 public function getStagingAdminBarColor()
478 {
479 $barColor = $this->settings->getAdminBarColor();
480 if (!preg_match("/#([a-f0-9]{3}){1,2}\b/i", $barColor)) {
481 $barColor = self::DEFAULT_ADMIN_BAR_BG;
482 }
483
484 return "#wpadminbar { background-color: {$barColor} !important; }";
485 }
486
487 /**
488 * Check whether app is in debug mode or in dev mode
489 *
490 * @return bool
491 */
492 private function isDebugOrDevMode()
493 {
494 return ($this->settings->isDebugMode() || (defined('WPSTG_DEV') && WPSTG_DEV === true) || (defined('WPSTG_DEBUG') && WPSTG_DEBUG === true));
495 }
496 }
497