PluginProbe ʕ •ᴥ•ʔ
WP STAGING – WordPress Backups, Restore, Migration & Clone / trunk
WP STAGING – WordPress Backups, Restore, Migration & Clone vtrunk
4.11.2 4.11.1 4.11.0 4.10.0 4.9.5 4.9.4 4.9.3 4.9.2 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 1 day ago I18n.php 1 week ago
Assets.php
822 lines
1 <?php
2
3 namespace WPStaging\Framework\Assets;
4
5 use WPStaging\Backup\BackupServiceProvider;
6 use WPStaging\Backup\Service\Database\DatabaseImporter;
7 use WPStaging\Backup\Service\UpdateProtectionHealth;
8 use WPStaging\Backup\Service\UpdateProtectionSettings;
9 use WPStaging\Framework\Facades\Escape;
10 use WPStaging\Framework\Filesystem\PartIdentifier;
11 use WPStaging\Framework\Language\Language;
12 use WPStaging\Core\DTO\Settings;
13 use WPStaging\Core\WPStaging;
14 use WPStaging\Framework\Filesystem\Scanning\ScanConst;
15 use WPStaging\Framework\Security\AccessToken;
16 use WPStaging\Framework\Security\Nonce;
17 use WPStaging\Framework\Security\Capabilities;
18 use WPStaging\Framework\Traits\PagesTrait;
19 use WPStaging\Framework\Traits\ResourceTrait;
20 use WPStaging\Framework\SiteInfo;
21 use WPStaging\Framework\Analytics\AnalyticsConsent;
22 use WPStaging\Framework\Facades\Hooks;
23 use WPStaging\Framework\Notices\Notices;
24 use WPStaging\Framework\Notices\CliIntegrationNotice;
25 use WPStaging\Framework\Newsfeed\NewsfeedProvider;
26 use WPStaging\Framework\Rest\Rest;
27 use WPStaging\Backup\Storage\Providers;
28 use WPStaging\Framework\Settings\DarkMode;
29 use WPStaging\Staging\Service\StagingEngine;
30
31 class Assets
32 {
33 use ResourceTrait;
34 use PagesTrait;
35
36
37
38
39
40 const DEFAULT_ADMIN_BAR_BG = "#ff8d00";
41
42
43 const FILTER_BACKUP_STATUS_REQUEST_INTERVAL = 'wpstg.backup.interval.status_request';
44
45
46 const FILTER_STAGING_SITE_TITLE = 'wpstg_staging_site_title';
47
48 const FILTER_TESTS_MAXIMUM_RETRIES = 'wpstg.tests.maximum_retries';
49
50
51 const TRANSIENT_REST_URL = 'wpstg_rest_url';
52
53 private $accessToken;
54
55
56 protected $settings;
57
58
59 private $analyticsConsent;
60
61
62 private $i18n;
63
64
65 private $providers;
66
67 public function __construct(AccessToken $accessToken, Settings $settings, AnalyticsConsent $analyticsConsent, I18n $i18n, Providers $providers)
68 {
69 $this->accessToken = $accessToken;
70 $this->settings = $settings;
71 $this->analyticsConsent = $analyticsConsent;
72 $this->i18n = $i18n;
73 $this->providers = $providers;
74 }
75
76
77
78
79
80
81
82 public function getAssetsUrl($assetsFile = '')
83 {
84 return WPSTG_PLUGIN_URL . "assets/$assetsFile";
85 }
86
87
88
89
90
91
92 public function getCssAssetsFileName(string $cssFileNameWithoutExtension): string
93 {
94
95 $nonMinCssFile = $this->getAssetsPath("css/dist/$cssFileNameWithoutExtension.css");
96 if ($this->isDebugOrDevMode() && file_exists($nonMinCssFile)) {
97 return "css/dist/$cssFileNameWithoutExtension.css";
98 }
99
100 return "css/dist/$cssFileNameWithoutExtension.min.css";
101 }
102
103
104
105
106
107
108 public function getJsAssetsFileName(string $jsFileNameWithoutExtension): string
109 {
110
111 $nonMinJsFile = $this->getAssetsPath("js/dist/$jsFileNameWithoutExtension.js");
112 if ($this->isDebugOrDevMode() && file_exists($nonMinJsFile)) {
113 return "js/dist/$jsFileNameWithoutExtension.js";
114 }
115
116 return "js/dist/$jsFileNameWithoutExtension.min.js";
117 }
118
119
120
121
122
123
124
125
126 public function getAssetsUrlWithVersion($assetsFile, $assetsVersion = '')
127 {
128 $url = $this->getAssetsUrl($assetsFile);
129 $ver = empty($assetsVersion) ? $this->getAssetsVersion($assetsFile, $assetsVersion) : $assetsVersion;
130 return $url . '?v=' . $ver;
131 }
132
133
134
135
136
137
138
139 public function getAssetsPath($assetsFile = '')
140 {
141 return WPSTG_PLUGIN_DIR . "assets/$assetsFile";
142 }
143
144
145
146
147
148
149
150
151 public function getAssetsVersion($assetsFile, $assetsVersion = '')
152 {
153 $filename = $this->getAssetsPath($assetsFile);
154 $filemtime = file_exists($filename) ? @filemtime($filename) : false;
155
156 if ($filemtime !== false) {
157 return $filemtime;
158 } else {
159 return $assetsVersion !== '' ? $assetsVersion : WPStaging::getVersion();
160 }
161 }
162
163
164
165
166
167 public function enqueueElements($hook)
168 {
169 $this->loadGlobalAssets($hook);
170
171 add_action(Notices::ACTION_INJECT_ANALYTICS_CONSENT_ASSETS, [$this, 'enqueueAnalyticsConsentAssets'], 10, 0);
172
173
174 if ((new SiteInfo())->isStagingSite()) {
175 wp_register_style('wpstg-admin-bar', false);
176 wp_enqueue_style('wpstg-admin-bar');
177 wp_add_inline_style('wpstg-admin-bar', $this->getStagingAdminBarColor());
178 }
179
180
181 if (!WPStaging::isPro() && $this->isPluginsPage()) {
182 $asset = $this->getJsAssetsFileName('wpstg-admin-plugins');
183 wp_enqueue_script(
184 "wpstg-admin-script",
185 $this->getAssetsUrl($asset),
186 ["jquery"],
187 $this->getAssetsVersion($asset),
188 $this->getScriptLoadingStrategy()
189 );
190
191 $asset = $this->getCssAssetsFileName('wpstg-admin-feedback');
192 wp_enqueue_style(
193 "wpstg-admin-feedback",
194 $this->getAssetsUrl($asset),
195 [],
196 $this->getAssetsVersion($asset)
197 );
198 }
199
200 if (is_admin()) {
201 $asset = $this->getCssAssetsFileName('wpstg-admin-menu-badge');
202 wp_enqueue_style(
203 "wpstg-admin-menu-badge-style",
204 $this->getAssetsUrl($asset),
205 [],
206 $this->getAssetsVersion($asset)
207 );
208 }
209
210
211 if (WPStaging::isPro() && is_admin()) {
212 $asset = $this->getJsAssetsFileName('pro/wpstg-admin-all-pages');
213 wp_enqueue_script(
214 "wpstg-admin-all-pages-script",
215 $this->getAssetsUrl($asset),
216 ["jquery"],
217 $this->getAssetsVersion($asset),
218 $this->getScriptLoadingStrategy()
219 );
220
221 $asset = $this->getCssAssetsFileName('wpstg-admin-all-pages');
222 wp_enqueue_style(
223 "wpstg-admin-all-pages-style",
224 $this->getAssetsUrl($asset),
225 [],
226 $this->getAssetsVersion($asset)
227 );
228 }
229
230 if ($this->isWordPressUpdatePage() && WPStaging::make(UpdateProtectionSettings::class)->isEnabled()) {
231 $this->enqueueBackupBeforeUpdateAssets();
232 }
233
234
235 if ($this->isNotWPStagingAdminPage($hook)) {
236 return;
237 }
238
239
240 $asset = $this->getJsAssetsFileName('wpstg');
241 wp_enqueue_script(
242 "wpstg-common",
243 $this->getAssetsUrl($asset),
244 ["jquery"],
245 $this->getAssetsVersion($asset),
246 $this->getScriptLoadingStrategy()
247 );
248
249
250 $asset = $this->getJsAssetsFileName('wpstg-admin');
251 wp_enqueue_script(
252 "wpstg-admin-script",
253 $this->getAssetsUrl($asset),
254 ["wpstg-common", "wpstg-admin-notyf", "wpstg-admin-sweetalerts"],
255 $this->getAssetsVersion($asset),
256 $this->getScriptLoadingStrategy()
257 );
258
259
260 $solidAsset = $this->getJsAssetsFileName('wpstg-solid');
261 $solidAssetPath = $this->getAssetsPath($solidAsset);
262 if (file_exists($solidAssetPath)) {
263 wp_enqueue_script(
264 "wpstg-solid",
265 $this->getAssetsUrl($solidAsset),
266 ["wpstg-admin-script"],
267 $this->getAssetsVersion($solidAsset),
268 $this->getScriptLoadingStrategy()
269 );
270 }
271
272
273 if (is_admin() && isset($_GET['page']) && $_GET['page'] === 'wpstg-settings') {
274 $asset = $this->getJsAssetsFileName('wpstg-admin-settings');
275 wp_enqueue_script(
276 'wpstg-admin-settings-script',
277 $this->getAssetsUrl($asset),
278 ['wpstg-common'],
279 $this->getAssetsVersion($asset),
280 $this->getScriptLoadingStrategy()
281 );
282 }
283
284
285 $asset = $this->getJsAssetsFileName('wpstg-sweetalert2');
286 wp_enqueue_script(
287 'wpstg-admin-sweetalerts',
288 $this->getAssetsUrl($asset),
289 [],
290 $this->getAssetsVersion($asset),
291 $this->getScriptLoadingStrategy()
292 );
293
294 $asset = $this->getCssAssetsFileName('wpstg-sweetalert2');
295 wp_enqueue_style(
296 'wpstg-admin-sweetalerts',
297 $this->getAssetsUrl($asset),
298 [],
299 $this->getAssetsVersion($asset)
300 );
301
302
303 $asset = 'js/vendor/notyf.min.js';
304 wp_enqueue_script(
305 'wpstg-admin-notyf',
306 $this->getAssetsUrl($asset),
307 [],
308 $this->getAssetsVersion($asset),
309 $this->getScriptLoadingStrategy()
310 );
311
312 $asset = 'css/vendor/notyf.min.css';
313 wp_enqueue_style(
314 'wpstg-admin-notyf',
315 $this->getAssetsUrl($asset),
316 [],
317 $this->getAssetsVersion($asset)
318 );
319
320
321 Hooks::doAction(BackupServiceProvider::ACTION_BACKUP_ENQUEUE_SCRIPTS);
322
323
324 wp_localize_script('wpstg-backup', 'wpstgAllStorages', $this->providers->getStorages(true));
325
326
327 if (WPStaging::isPro()) {
328 $asset = $this->getJsAssetsFileName('pro/wpstg-admin-pro');
329 wp_enqueue_script(
330 "wpstg-admin-pro-script",
331 $this->getAssetsUrl($asset),
332 ["jquery", "wpstg-common", "wpstg-admin-script", "wpstg-admin-notyf", "wpstg-admin-sweetalerts"],
333 $this->getAssetsVersion($asset),
334 $this->getScriptLoadingStrategy()
335 );
336 }
337
338
339 $asset = $this->getCssAssetsFileName('wpstg-admin');
340 wp_enqueue_style(
341 "wpstg-admin",
342 $this->getAssetsUrl($asset),
343 [],
344 $this->getAssetsVersion($asset)
345 );
346
347 $wpstgConfig = [
348 "delayReq" => 0,
349
350 'backupStatusInterval' => Hooks::applyFilters(self::FILTER_BACKUP_STATUS_REQUEST_INTERVAL, 8000),
351 "settings" => (object)[
352 "directorySeparator" => ScanConst::DIRECTORIES_SEPARATOR,
353 ],
354 "tblprefix" => WPStaging::getTablePrefix(),
355 "isMultisite" => is_multisite(),
356 AccessToken::REQUEST_KEY => (string)$this->accessToken->getToken() ?: (string)$this->accessToken->generateNewToken(),
357 'nonce' => wp_create_nonce(Nonce::WPSTG_NONCE),
358 'assetsUrl' => $this->getAssetsUrl(),
359 'ajaxUrl' => admin_url('admin-ajax.php'),
360 'restUrl' => $this->getRestUrl(),
361 'wpstgIcon' => $this->getAssetsUrl('img/wpstg-loader.gif'),
362 'maxUploadChunkSize' => $this->getMaxUploadChunkSize(),
363 'backupDBExtension' => PartIdentifier::DATABASE_PART_IDENTIFIER . '.' . DatabaseImporter::FILE_FORMAT,
364 'analyticsConsentAllow' => esc_url($this->analyticsConsent->getConsentLink(true)),
365 'analyticsConsentDeny' => esc_url($this->analyticsConsent->getConsentLink(false)),
366 'analyticsConsentLater' => esc_url($this->analyticsConsent->getRemindMeLaterConsentLink()),
367 'pluginVersion' => WPStaging::getVersion(),
368 'isPro' => WPStaging::isPro(),
369 'isDeveloperOrHigherLicense' => WPStaging::make(CliIntegrationNotice::class)->isDeveloperOrHigherLicense(),
370 'isExpiredDeveloperOrHigherLicense' => WPStaging::make(CliIntegrationNotice::class)->isExpiredDeveloperOrHigherLicense(),
371 'canExtractSingleFile' => WPStaging::isPro() && $this->isValidProLicense(),
372 'licensePlanName' => WPStaging::make(CliIntegrationNotice::class)->getLicensePlanName(),
373 'licenseUpgradeUrl' => $this->getLicenseUpgradeUrl(),
374 'licenseRenewalUrl' => $this->getLicenseRenewalUrl(),
375 'pricingUrl' => Language::getUpgradeUrl('plugin_upsell'),
376 'proFeaturesUrl' => Language::addClientAttribution(Language::localizeUrl('https://wp-staging.com/pro-features/?utm_source=wp-admin&utm_medium=plugin&utm_campaign=pro_features')),
377 'checkoutFallbackUrl' => Language::localizeCheckoutUrl('https://wp-staging.com/checkout/?nocache=true&download_id=11'),
378 'newsfeedData' => $this->getNewsfeedDataForJs(),
379 'isNewUser' => $this->isNewUser(),
380 'maxFailedRetries' => apply_filters(self::FILTER_TESTS_MAXIMUM_RETRIES, 10),
381 'i18n' => $this->i18n->getTranslations(),
382 'isCloneable' => (new SiteInfo())->isCloneable(),
383 'isTestMode' => defined('WPSTG_TEST') && WPSTG_TEST,
384 'defaultColorMode' => get_option(DarkMode::OPTION_DEFAULT_COLOR_MODE, ''),
385 'siteUrl' => site_url(),
386 'stagingEnginePreference' => WPStaging::make(StagingEngine::class)->getEngine(),
387 ];
388
389
390 wp_localize_script("wpstg-common", "wpstg", $wpstgConfig);
391
392 if (defined('WPSTG_TEST') && WPSTG_TEST) {
393 add_filter('admin_body_class', function ($classes) {
394 return $classes . ' wpstg-test-mode';
395 });
396 }
397 }
398
399 public function enqueueAnalyticsConsentAssets()
400 {
401 $asset = $this->getJsAssetsFileName('analytics-consent-modal');
402 wp_enqueue_script(
403 "wpstg-show-analytics-modal",
404 $this->getAssetsUrl($asset),
405 [],
406 $this->getAssetsVersion($asset),
407 $this->getScriptLoadingStrategy()
408 );
409
410 $asset = $this->getCssAssetsFileName('analytics-consent-modal');
411 wp_enqueue_style(
412 'wpstg-plugin-activation',
413 $this->getAssetsUrl($asset),
414 [],
415 $this->getAssetsVersion($asset)
416 );
417 }
418
419
420
421
422
423
424
425 private function loadGlobalAssets($pageSlug)
426 {
427 if (!$this->isNotWPStagingAdminPage($pageSlug) || !is_admin()) {
428 return;
429 }
430
431 $asset = $this->getJsAssetsFileName('wpstg-blank-loader');
432 wp_enqueue_script('wpstg-global', $this->getAssetsUrl($asset), [], false, $this->getScriptLoadingStrategy());
433
434 $vars = [
435 'nonce' => wp_create_nonce(Nonce::WPSTG_NONCE),
436 ];
437
438 wp_localize_script("wpstg-global", "wpstg", $vars);
439 }
440
441
442
443
444 protected function getMaxUploadChunkSize()
445 {
446 $lowerLimit = 64 * KB_IN_BYTES;
447 $upperLimit = 16 * MB_IN_BYTES;
448
449 $maxPostSize = wp_convert_hr_to_bytes(ini_get('post_max_size'));
450 $uploadMaxFileSize = wp_convert_hr_to_bytes(ini_get('upload_max_filesize'));
451
452
453 $limit = min($maxPostSize, $uploadMaxFileSize) * 0.90;
454
455
456 $limit = min($limit, $upperLimit);
457
458
459 $limit = max($lowerLimit, $limit);
460
461 return (int)$limit;
462 }
463
464
465
466
467 private function getNewsfeedDataForJs()
468 {
469 try {
470
471 $provider = WPStaging::make(NewsfeedProvider::class);
472 return $provider->getNewsfeedData();
473 } catch (\Exception $e) {
474 return null;
475 }
476 }
477
478
479
480
481
482
483
484
485
486
487
488 private function isNewUser(): bool
489 {
490 $upgradedFromOption = WPStaging::isPro() ? 'wpstgpro_version_upgraded_from' : 'wpstg_version_upgraded_from';
491
492 return empty(get_option($upgradedFromOption, ''));
493 }
494
495
496
497
498 private function isValidProLicense(): bool
499 {
500 if (!class_exists('\WPStaging\Pro\License\Licensing')) {
501 return false;
502 }
503
504 try {
505 return WPStaging::make(\WPStaging\Pro\License\Licensing::class)->isRegisteredLicense();
506 } catch (\Exception $e) {
507 return false;
508 }
509 }
510
511
512
513
514 private function getLicenseUpgradeUrl(): string
515 {
516 if (!WPStaging::isPro() || !class_exists('\WPStaging\Pro\License\Licensing')) {
517 return '';
518 }
519
520 try {
521
522 $licensing = WPStaging::make(\WPStaging\Pro\License\Licensing::class);
523 return $licensing->getUpgradeToDevUrl();
524 } catch (\Exception $e) {
525 return '';
526 }
527 }
528
529
530
531
532 private function getLicenseRenewalUrl(): string
533 {
534 if (!WPStaging::isPro() || !class_exists('\WPStaging\Pro\License\Licensing')) {
535 return '';
536 }
537
538 try {
539 $isExpired = WPStaging::make(CliIntegrationNotice::class)->isExpiredDeveloperOrHigherLicense();
540 if (!$isExpired) {
541 return '';
542 }
543
544 $licenseKey = trim(get_option(\WPStaging\Pro\License\Licensing::WPSTG_LICENSE_KEY, ''));
545 return Language::localizeCheckoutUrl('https://wp-staging.com/checkout/?nocache=true&edd_license_key=' . urlencode($licenseKey) . '&download_id=11');
546 } catch (\Exception $e) {
547 return '';
548 }
549 }
550
551
552
553
554
555
556
557
558 public function getScriptLoadingStrategy()
559 {
560 if (function_exists('wp_register_script_module')) {
561 return ['strategy' => 'defer', 'in_footer' => false];
562 }
563
564 return true;
565 }
566
567
568
569
570
571
572
573
574 private function isNotWPStagingAdminPage($slug)
575 {
576 if (WPStaging::isPro() || WPStaging::isDevBasic()) {
577 $availableSlugs = [
578 "toplevel_page_wpstg_clone",
579 "toplevel_page_wpstg_backup",
580 "wp-staging-pro_page_wpstg_clone",
581 "wp-staging-pro_page_wpstg_backup",
582 "wp-staging-pro_page_wpstg-settings",
583 "wp-staging-pro_page_wpstg-tools",
584 "wp-staging-pro_page_wpstg-license",
585 "wp-staging-pro_page_wpstg-restorer",
586
587
588 "wp-staging_page_wpstg_clone",
589 "wp-staging_page_wpstg_backup",
590 "wp-staging_page_wpstg-settings",
591 "wp-staging_page_wpstg-tools",
592 ];
593 } else {
594 $availableSlugs = [
595 "toplevel_page_wpstg_clone",
596 "toplevel_page_wpstg_backup",
597 "wp-staging_page_wpstg_clone",
598 "wp-staging_page_wpstg_backup",
599 "wp-staging_page_wpstg-settings",
600 "wp-staging_page_wpstg-tools",
601 "wp-staging_page_wpstg-welcome",
602 ];
603 }
604
605 return !in_array($slug, $availableSlugs);
606 }
607
608
609
610
611
612
613
614
615
616 public function removeWPCoreJs($hook)
617 {
618 if ($this->isNotWPStagingAdminPage($hook)) {
619 return;
620 }
621
622
623
624 remove_action('admin_enqueue_scripts', 'wp_auth_check_load');
625
626
627 wp_deregister_script('heartbeat');
628 }
629
630
631
632
633
634
635 private function enqueueBackupBeforeUpdateAssets()
636 {
637 if (!current_user_can(WPStaging::make(Capabilities::class)->manageWPSTG())) {
638 return;
639 }
640
641 $css = $this->getCssAssetsFileName('wpstg-update-pages');
642 wp_enqueue_style('wpstg-update-pages', $this->getAssetsUrl($css), [], $this->getAssetsVersion($css));
643
644 $token = (string)$this->accessToken->getToken() ?: (string)$this->accessToken->generateNewToken();
645 $translations = $this->i18n->getTranslations();
646 $protectionSettings = WPStaging::make(UpdateProtectionSettings::class);
647 wp_add_inline_script(
648 'wpstg-global',
649 'window.wpstg = Object.assign(window.wpstg || {}, ' . wp_json_encode([
650 'accessToken' => $token,
651 'ajaxUrl' => admin_url('admin-ajax.php'),
652 'settingsUrl' => admin_url('admin.php?page=wpstg-settings'),
653 'loaderUrl' => $this->getAssetsUrl('img/wpstg-loader.gif'),
654 'logoUrl' => $this->getAssetsUrl('img/logo.svg'),
655 'backupBeforeUpdateMode' => $protectionSettings->getMode(),
656 'backupBeforeUpdateIntrosSeen' => $protectionSettings->getIntrosSeen(),
657 'updateProtectionPaused' => WPStaging::make(UpdateProtectionHealth::class)->isPaused(),
658 'i18n' => ['backup_before_update' => $translations['backup_before_update'] ?? []],
659 ]) . ');',
660 'after'
661 );
662
663 $solidAsset = $this->getJsAssetsFileName('wpstg-solid');
664 if (file_exists($this->getAssetsPath($solidAsset)) && !wp_script_is('wpstg-solid', 'registered') && !wp_script_is('wpstg-solid', 'enqueued')) {
665 wp_enqueue_script('wpstg-solid', $this->getAssetsUrl($solidAsset), ['wpstg-global'], $this->getAssetsVersion($solidAsset), $this->getScriptLoadingStrategy());
666 }
667
668 $js = $this->getJsAssetsFileName('backup/before-update');
669 wp_enqueue_script('wpstg-before-update', $this->getAssetsUrl($js), ['wpstg-solid'], $this->getAssetsVersion($js), $this->getScriptLoadingStrategy());
670 }
671
672
673
674
675
676
677 private function isPluginsPage()
678 {
679 global $pagenow;
680
681 return ($pagenow === 'plugins.php');
682 }
683
684
685
686
687 public function getStagingAdminBarColor()
688 {
689 $barColor = $this->settings->getAdminBarColor();
690 if (!preg_match("/#([a-f0-9]{3}){1,2}\b/i", $barColor)) {
691 $barColor = self::DEFAULT_ADMIN_BAR_BG;
692 }
693
694 return "#wpadminbar { background-color: {$barColor} !important; }";
695 }
696
697
698
699
700
701
702 private function isDebugOrDevMode()
703 {
704 return ($this->settings->isDebugMode() || (defined('WPSTG_IS_DEV') && WPSTG_IS_DEV === true) || (defined('WPSTG_DEBUG') && WPSTG_DEBUG === true));
705 }
706
707
708
709
710
711
712
713 public function changeSiteName()
714 {
715 if (!(new SiteInfo())->isStagingSite()) {
716 return;
717 }
718
719 global $wp_admin_bar;
720 $blogName = get_bloginfo('name');
721 if (empty($blogName)) {
722 $siteUrl = get_site_url();
723 $parsedUrl = parse_url($siteUrl);
724 $blogName = $parsedUrl['host'];
725 }
726
727 $siteTitle = Hooks::applyFilters(self::FILTER_STAGING_SITE_TITLE, 'STAGING');
728 $title = (strlen($blogName) > 20) ? substr($blogName, 0, 20) . '...' : $blogName;
729 $wp_admin_bar->add_menu(
730 [
731 'id' => 'site-name',
732 'title' => $siteTitle . ' - ' . $title,
733 'href' => is_admin() ? home_url('/') : admin_url(),
734 ]
735 );
736 }
737
738
739
740
741
742 public function dequeueNonWpstgElements($hook)
743 {
744 if ($this->isNotWPStagingAdminPage($hook)) {
745 return;
746 }
747
748 $stylesToRemove = ['wp-reset-sweetalert2'];
749 $scriptsToRemove = [
750 'wp-reset-sweetalert2',
751 'wp-reset',
752 ];
753
754 foreach ($stylesToRemove as $style) {
755 wp_dequeue_style($style);
756 }
757
758 foreach ($scriptsToRemove as $script) {
759 wp_dequeue_script($script);
760 }
761 }
762
763
764
765
766
767
768 public function renderSvg(string $iconName, string $class = '')
769 {
770 $fullPath = WPSTG_PLUGIN_DIR . '/assets/svg/' . $iconName . '.svg';
771 if (!file_exists($fullPath)) {
772 return;
773 }
774
775 $svgCode = file_get_contents($fullPath);
776 $svgCode = preg_replace('/<svg(.*?)>/', '<svg$1 class="' . $class . '">', $svgCode);
777 echo Escape::escapeHtml($svgCode);
778 }
779
780
781 private function getRestUrl(): string
782 {
783 $restUrl = get_transient(self::TRANSIENT_REST_URL);
784 if ($restUrl) {
785 return $restUrl;
786 }
787
788 $restUrl = get_rest_url(null, Rest::WPSTG_ROUTE_NAMESPACE_V1);
789 if (!$this->isWorkingTestUrl($restUrl)) {
790 $restUrl = site_url('/?rest_route=/' . Rest::WPSTG_ROUTE_NAMESPACE_V1);
791 }
792
793 set_transient(self::TRANSIENT_REST_URL, $restUrl, 24 * HOUR_IN_SECONDS);
794
795 return $restUrl;
796 }
797
798 private function isWorkingTestUrl($url)
799 {
800 $url .= '/ping&accessToken=' . $this->accessToken->getToken();
801 $response = wp_remote_request($url, [
802 'method' => 'GET',
803 'timeout' => 5,
804 'sslverify' => false,
805 'headers' => [
806 'Accept' => 'application/json',
807 ],
808 ]);
809
810 if (is_wp_error($response)) {
811 return false;
812 }
813
814 $code = wp_remote_retrieve_response_code($response);
815 if ($code !== 200) {
816 return false;
817 }
818
819 return true;
820 }
821 }
822