PluginProbe ʕ •ᴥ•ʔ
WP STAGING – WordPress Backups, Restore, Migration & Clone / 4.11.0
WP STAGING – WordPress Backups, Restore, Migration & Clone v4.11.0
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 day ago
Assets.php
812 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
201 if (WPStaging::isPro() && is_admin()) {
202 $asset = $this->getJsAssetsFileName('pro/wpstg-admin-all-pages');
203 wp_enqueue_script(
204 "wpstg-admin-all-pages-script",
205 $this->getAssetsUrl($asset),
206 ["jquery"],
207 $this->getAssetsVersion($asset),
208 $this->getScriptLoadingStrategy()
209 );
210
211 $asset = $this->getCssAssetsFileName('wpstg-admin-all-pages');
212 wp_enqueue_style(
213 "wpstg-admin-all-pages-style",
214 $this->getAssetsUrl($asset),
215 [],
216 $this->getAssetsVersion($asset)
217 );
218 }
219
220 if ($this->isWordPressUpdatePage() && WPStaging::make(UpdateProtectionSettings::class)->isEnabled()) {
221 $this->enqueueBackupBeforeUpdateAssets();
222 }
223
224
225 if ($this->isNotWPStagingAdminPage($hook)) {
226 return;
227 }
228
229
230 $asset = $this->getJsAssetsFileName('wpstg');
231 wp_enqueue_script(
232 "wpstg-common",
233 $this->getAssetsUrl($asset),
234 ["jquery"],
235 $this->getAssetsVersion($asset),
236 $this->getScriptLoadingStrategy()
237 );
238
239
240 $asset = $this->getJsAssetsFileName('wpstg-admin');
241 wp_enqueue_script(
242 "wpstg-admin-script",
243 $this->getAssetsUrl($asset),
244 ["wpstg-common", "wpstg-admin-notyf", "wpstg-admin-sweetalerts"],
245 $this->getAssetsVersion($asset),
246 $this->getScriptLoadingStrategy()
247 );
248
249
250 $solidAsset = $this->getJsAssetsFileName('wpstg-solid');
251 $solidAssetPath = $this->getAssetsPath($solidAsset);
252 if (file_exists($solidAssetPath)) {
253 wp_enqueue_script(
254 "wpstg-solid",
255 $this->getAssetsUrl($solidAsset),
256 ["wpstg-admin-script"],
257 $this->getAssetsVersion($solidAsset),
258 $this->getScriptLoadingStrategy()
259 );
260 }
261
262
263 if (is_admin() && isset($_GET['page']) && $_GET['page'] === 'wpstg-settings') {
264 $asset = $this->getJsAssetsFileName('wpstg-admin-settings');
265 wp_enqueue_script(
266 'wpstg-admin-settings-script',
267 $this->getAssetsUrl($asset),
268 ['wpstg-common'],
269 $this->getAssetsVersion($asset),
270 $this->getScriptLoadingStrategy()
271 );
272 }
273
274
275 $asset = $this->getJsAssetsFileName('wpstg-sweetalert2');
276 wp_enqueue_script(
277 'wpstg-admin-sweetalerts',
278 $this->getAssetsUrl($asset),
279 [],
280 $this->getAssetsVersion($asset),
281 $this->getScriptLoadingStrategy()
282 );
283
284 $asset = $this->getCssAssetsFileName('wpstg-sweetalert2');
285 wp_enqueue_style(
286 'wpstg-admin-sweetalerts',
287 $this->getAssetsUrl($asset),
288 [],
289 $this->getAssetsVersion($asset)
290 );
291
292
293 $asset = 'js/vendor/notyf.min.js';
294 wp_enqueue_script(
295 'wpstg-admin-notyf',
296 $this->getAssetsUrl($asset),
297 [],
298 $this->getAssetsVersion($asset),
299 $this->getScriptLoadingStrategy()
300 );
301
302 $asset = 'css/vendor/notyf.min.css';
303 wp_enqueue_style(
304 'wpstg-admin-notyf',
305 $this->getAssetsUrl($asset),
306 [],
307 $this->getAssetsVersion($asset)
308 );
309
310
311 Hooks::doAction(BackupServiceProvider::ACTION_BACKUP_ENQUEUE_SCRIPTS);
312
313
314 wp_localize_script('wpstg-backup', 'wpstgAllStorages', $this->providers->getStorages(true));
315
316
317 if (WPStaging::isPro()) {
318 $asset = $this->getJsAssetsFileName('pro/wpstg-admin-pro');
319 wp_enqueue_script(
320 "wpstg-admin-pro-script",
321 $this->getAssetsUrl($asset),
322 ["jquery", "wpstg-common", "wpstg-admin-script", "wpstg-admin-notyf", "wpstg-admin-sweetalerts"],
323 $this->getAssetsVersion($asset),
324 $this->getScriptLoadingStrategy()
325 );
326 }
327
328
329 $asset = $this->getCssAssetsFileName('wpstg-admin');
330 wp_enqueue_style(
331 "wpstg-admin",
332 $this->getAssetsUrl($asset),
333 [],
334 $this->getAssetsVersion($asset)
335 );
336
337 $wpstgConfig = [
338 "delayReq" => 0,
339
340 'backupStatusInterval' => Hooks::applyFilters(self::FILTER_BACKUP_STATUS_REQUEST_INTERVAL, 8000),
341 "settings" => (object)[
342 "directorySeparator" => ScanConst::DIRECTORIES_SEPARATOR,
343 ],
344 "tblprefix" => WPStaging::getTablePrefix(),
345 "isMultisite" => is_multisite(),
346 AccessToken::REQUEST_KEY => (string)$this->accessToken->getToken() ?: (string)$this->accessToken->generateNewToken(),
347 'nonce' => wp_create_nonce(Nonce::WPSTG_NONCE),
348 'assetsUrl' => $this->getAssetsUrl(),
349 'ajaxUrl' => admin_url('admin-ajax.php'),
350 'restUrl' => $this->getRestUrl(),
351 'wpstgIcon' => $this->getAssetsUrl('img/wpstg-loader.gif'),
352 'maxUploadChunkSize' => $this->getMaxUploadChunkSize(),
353 'backupDBExtension' => PartIdentifier::DATABASE_PART_IDENTIFIER . '.' . DatabaseImporter::FILE_FORMAT,
354 'analyticsConsentAllow' => esc_url($this->analyticsConsent->getConsentLink(true)),
355 'analyticsConsentDeny' => esc_url($this->analyticsConsent->getConsentLink(false)),
356 'analyticsConsentLater' => esc_url($this->analyticsConsent->getRemindMeLaterConsentLink()),
357 'pluginVersion' => WPStaging::getVersion(),
358 'isPro' => WPStaging::isPro(),
359 'isDeveloperOrHigherLicense' => WPStaging::make(CliIntegrationNotice::class)->isDeveloperOrHigherLicense(),
360 'isExpiredDeveloperOrHigherLicense' => WPStaging::make(CliIntegrationNotice::class)->isExpiredDeveloperOrHigherLicense(),
361 'canExtractSingleFile' => WPStaging::isPro() && $this->isValidProLicense(),
362 'licensePlanName' => WPStaging::make(CliIntegrationNotice::class)->getLicensePlanName(),
363 'licenseUpgradeUrl' => $this->getLicenseUpgradeUrl(),
364 'licenseRenewalUrl' => $this->getLicenseRenewalUrl(),
365 'pricingUrl' => Language::getUpgradeUrl('plugin_upsell'),
366 'proFeaturesUrl' => Language::addClientAttribution(Language::localizeUrl('https://wp-staging.com/pro-features/?utm_source=wp-admin&utm_medium=plugin&utm_campaign=pro_features')),
367 'checkoutFallbackUrl' => Language::localizeCheckoutUrl('https://wp-staging.com/checkout/?nocache=true&download_id=11'),
368 'newsfeedData' => $this->getNewsfeedDataForJs(),
369 'isNewUser' => $this->isNewUser(),
370 'maxFailedRetries' => apply_filters(self::FILTER_TESTS_MAXIMUM_RETRIES, 10),
371 'i18n' => $this->i18n->getTranslations(),
372 'isCloneable' => (new SiteInfo())->isCloneable(),
373 'isTestMode' => defined('WPSTG_TEST') && WPSTG_TEST,
374 'defaultColorMode' => get_option(DarkMode::OPTION_DEFAULT_COLOR_MODE, ''),
375 'siteUrl' => site_url(),
376 'stagingEnginePreference' => WPStaging::make(StagingEngine::class)->getEngine(),
377 ];
378
379
380 wp_localize_script("wpstg-common", "wpstg", $wpstgConfig);
381
382 if (defined('WPSTG_TEST') && WPSTG_TEST) {
383 add_filter('admin_body_class', function ($classes) {
384 return $classes . ' wpstg-test-mode';
385 });
386 }
387 }
388
389 public function enqueueAnalyticsConsentAssets()
390 {
391 $asset = $this->getJsAssetsFileName('analytics-consent-modal');
392 wp_enqueue_script(
393 "wpstg-show-analytics-modal",
394 $this->getAssetsUrl($asset),
395 [],
396 $this->getAssetsVersion($asset),
397 $this->getScriptLoadingStrategy()
398 );
399
400 $asset = $this->getCssAssetsFileName('analytics-consent-modal');
401 wp_enqueue_style(
402 'wpstg-plugin-activation',
403 $this->getAssetsUrl($asset),
404 [],
405 $this->getAssetsVersion($asset)
406 );
407 }
408
409
410
411
412
413
414
415 private function loadGlobalAssets($pageSlug)
416 {
417 if (!$this->isNotWPStagingAdminPage($pageSlug) || !is_admin()) {
418 return;
419 }
420
421 $asset = $this->getJsAssetsFileName('wpstg-blank-loader');
422 wp_enqueue_script('wpstg-global', $this->getAssetsUrl($asset), [], false, $this->getScriptLoadingStrategy());
423
424 $vars = [
425 'nonce' => wp_create_nonce(Nonce::WPSTG_NONCE),
426 ];
427
428 wp_localize_script("wpstg-global", "wpstg", $vars);
429 }
430
431
432
433
434 protected function getMaxUploadChunkSize()
435 {
436 $lowerLimit = 64 * KB_IN_BYTES;
437 $upperLimit = 16 * MB_IN_BYTES;
438
439 $maxPostSize = wp_convert_hr_to_bytes(ini_get('post_max_size'));
440 $uploadMaxFileSize = wp_convert_hr_to_bytes(ini_get('upload_max_filesize'));
441
442
443 $limit = min($maxPostSize, $uploadMaxFileSize) * 0.90;
444
445
446 $limit = min($limit, $upperLimit);
447
448
449 $limit = max($lowerLimit, $limit);
450
451 return (int)$limit;
452 }
453
454
455
456
457 private function getNewsfeedDataForJs()
458 {
459 try {
460
461 $provider = WPStaging::make(NewsfeedProvider::class);
462 return $provider->getNewsfeedData();
463 } catch (\Exception $e) {
464 return null;
465 }
466 }
467
468
469
470
471
472
473
474
475
476
477
478 private function isNewUser(): bool
479 {
480 $upgradedFromOption = WPStaging::isPro() ? 'wpstgpro_version_upgraded_from' : 'wpstg_version_upgraded_from';
481
482 return empty(get_option($upgradedFromOption, ''));
483 }
484
485
486
487
488 private function isValidProLicense(): bool
489 {
490 if (!class_exists('\WPStaging\Pro\License\Licensing')) {
491 return false;
492 }
493
494 try {
495 return WPStaging::make(\WPStaging\Pro\License\Licensing::class)->isRegisteredLicense();
496 } catch (\Exception $e) {
497 return false;
498 }
499 }
500
501
502
503
504 private function getLicenseUpgradeUrl(): string
505 {
506 if (!WPStaging::isPro() || !class_exists('\WPStaging\Pro\License\Licensing')) {
507 return '';
508 }
509
510 try {
511
512 $licensing = WPStaging::make(\WPStaging\Pro\License\Licensing::class);
513 return $licensing->getUpgradeToDevUrl();
514 } catch (\Exception $e) {
515 return '';
516 }
517 }
518
519
520
521
522 private function getLicenseRenewalUrl(): string
523 {
524 if (!WPStaging::isPro() || !class_exists('\WPStaging\Pro\License\Licensing')) {
525 return '';
526 }
527
528 try {
529 $isExpired = WPStaging::make(CliIntegrationNotice::class)->isExpiredDeveloperOrHigherLicense();
530 if (!$isExpired) {
531 return '';
532 }
533
534 $licenseKey = trim(get_option(\WPStaging\Pro\License\Licensing::WPSTG_LICENSE_KEY, ''));
535 return Language::localizeCheckoutUrl('https://wp-staging.com/checkout/?nocache=true&edd_license_key=' . urlencode($licenseKey) . '&download_id=11');
536 } catch (\Exception $e) {
537 return '';
538 }
539 }
540
541
542
543
544
545
546
547
548 public function getScriptLoadingStrategy()
549 {
550 if (function_exists('wp_register_script_module')) {
551 return ['strategy' => 'defer', 'in_footer' => false];
552 }
553
554 return true;
555 }
556
557
558
559
560
561
562
563
564 private function isNotWPStagingAdminPage($slug)
565 {
566 if (WPStaging::isPro() || WPStaging::isDevBasic()) {
567 $availableSlugs = [
568 "toplevel_page_wpstg_clone",
569 "toplevel_page_wpstg_backup",
570 "wp-staging-pro_page_wpstg_clone",
571 "wp-staging-pro_page_wpstg_backup",
572 "wp-staging-pro_page_wpstg-settings",
573 "wp-staging-pro_page_wpstg-tools",
574 "wp-staging-pro_page_wpstg-license",
575 "wp-staging-pro_page_wpstg-restorer",
576
577
578 "wp-staging_page_wpstg_clone",
579 "wp-staging_page_wpstg_backup",
580 "wp-staging_page_wpstg-settings",
581 "wp-staging_page_wpstg-tools",
582 ];
583 } else {
584 $availableSlugs = [
585 "toplevel_page_wpstg_clone",
586 "toplevel_page_wpstg_backup",
587 "wp-staging_page_wpstg_clone",
588 "wp-staging_page_wpstg_backup",
589 "wp-staging_page_wpstg-settings",
590 "wp-staging_page_wpstg-tools",
591 "wp-staging_page_wpstg-welcome",
592 ];
593 }
594
595 return !in_array($slug, $availableSlugs);
596 }
597
598
599
600
601
602
603
604
605
606 public function removeWPCoreJs($hook)
607 {
608 if ($this->isNotWPStagingAdminPage($hook)) {
609 return;
610 }
611
612
613
614 remove_action('admin_enqueue_scripts', 'wp_auth_check_load');
615
616
617 wp_deregister_script('heartbeat');
618 }
619
620
621
622
623
624
625 private function enqueueBackupBeforeUpdateAssets()
626 {
627 if (!current_user_can(WPStaging::make(Capabilities::class)->manageWPSTG())) {
628 return;
629 }
630
631 $css = $this->getCssAssetsFileName('wpstg-update-pages');
632 wp_enqueue_style('wpstg-update-pages', $this->getAssetsUrl($css), [], $this->getAssetsVersion($css));
633
634 $token = (string)$this->accessToken->getToken() ?: (string)$this->accessToken->generateNewToken();
635 $translations = $this->i18n->getTranslations();
636 $protectionSettings = WPStaging::make(UpdateProtectionSettings::class);
637 wp_add_inline_script(
638 'wpstg-global',
639 'window.wpstg = Object.assign(window.wpstg || {}, ' . wp_json_encode([
640 'accessToken' => $token,
641 'ajaxUrl' => admin_url('admin-ajax.php'),
642 'settingsUrl' => admin_url('admin.php?page=wpstg-settings'),
643 'loaderUrl' => $this->getAssetsUrl('img/wpstg-loader.gif'),
644 'logoUrl' => $this->getAssetsUrl('img/logo.svg'),
645 'backupBeforeUpdateMode' => $protectionSettings->getMode(),
646 'backupBeforeUpdateIntrosSeen' => $protectionSettings->getIntrosSeen(),
647 'updateProtectionPaused' => WPStaging::make(UpdateProtectionHealth::class)->isPaused(),
648 'i18n' => ['backup_before_update' => $translations['backup_before_update'] ?? []],
649 ]) . ');',
650 'after'
651 );
652
653 $solidAsset = $this->getJsAssetsFileName('wpstg-solid');
654 if (file_exists($this->getAssetsPath($solidAsset)) && !wp_script_is('wpstg-solid', 'registered') && !wp_script_is('wpstg-solid', 'enqueued')) {
655 wp_enqueue_script('wpstg-solid', $this->getAssetsUrl($solidAsset), ['wpstg-global'], $this->getAssetsVersion($solidAsset), $this->getScriptLoadingStrategy());
656 }
657
658 $js = $this->getJsAssetsFileName('backup/before-update');
659 wp_enqueue_script('wpstg-before-update', $this->getAssetsUrl($js), ['wpstg-solid'], $this->getAssetsVersion($js), $this->getScriptLoadingStrategy());
660 }
661
662
663
664
665
666
667 private function isPluginsPage()
668 {
669 global $pagenow;
670
671 return ($pagenow === 'plugins.php');
672 }
673
674
675
676
677 public function getStagingAdminBarColor()
678 {
679 $barColor = $this->settings->getAdminBarColor();
680 if (!preg_match("/#([a-f0-9]{3}){1,2}\b/i", $barColor)) {
681 $barColor = self::DEFAULT_ADMIN_BAR_BG;
682 }
683
684 return "#wpadminbar { background-color: {$barColor} !important; }";
685 }
686
687
688
689
690
691
692 private function isDebugOrDevMode()
693 {
694 return ($this->settings->isDebugMode() || (defined('WPSTG_IS_DEV') && WPSTG_IS_DEV === true) || (defined('WPSTG_DEBUG') && WPSTG_DEBUG === true));
695 }
696
697
698
699
700
701
702
703 public function changeSiteName()
704 {
705 if (!(new SiteInfo())->isStagingSite()) {
706 return;
707 }
708
709 global $wp_admin_bar;
710 $blogName = get_bloginfo('name');
711 if (empty($blogName)) {
712 $siteUrl = get_site_url();
713 $parsedUrl = parse_url($siteUrl);
714 $blogName = $parsedUrl['host'];
715 }
716
717 $siteTitle = Hooks::applyFilters(self::FILTER_STAGING_SITE_TITLE, 'STAGING');
718 $title = (strlen($blogName) > 20) ? substr($blogName, 0, 20) . '...' : $blogName;
719 $wp_admin_bar->add_menu(
720 [
721 'id' => 'site-name',
722 'title' => $siteTitle . ' - ' . $title,
723 'href' => is_admin() ? home_url('/') : admin_url(),
724 ]
725 );
726 }
727
728
729
730
731
732 public function dequeueNonWpstgElements($hook)
733 {
734 if ($this->isNotWPStagingAdminPage($hook)) {
735 return;
736 }
737
738 $stylesToRemove = ['wp-reset-sweetalert2'];
739 $scriptsToRemove = [
740 'wp-reset-sweetalert2',
741 'wp-reset',
742 ];
743
744 foreach ($stylesToRemove as $style) {
745 wp_dequeue_style($style);
746 }
747
748 foreach ($scriptsToRemove as $script) {
749 wp_dequeue_script($script);
750 }
751 }
752
753
754
755
756
757
758 public function renderSvg(string $iconName, string $class = '')
759 {
760 $fullPath = WPSTG_PLUGIN_DIR . '/assets/svg/' . $iconName . '.svg';
761 if (!file_exists($fullPath)) {
762 return;
763 }
764
765 $svgCode = file_get_contents($fullPath);
766 $svgCode = preg_replace('/<svg(.*?)>/', '<svg$1 class="' . $class . '">', $svgCode);
767 echo Escape::escapeHtml($svgCode);
768 }
769
770
771 private function getRestUrl(): string
772 {
773 $restUrl = get_transient(self::TRANSIENT_REST_URL);
774 if ($restUrl) {
775 return $restUrl;
776 }
777
778 $restUrl = get_rest_url(null, Rest::WPSTG_ROUTE_NAMESPACE_V1);
779 if (!$this->isWorkingTestUrl($restUrl)) {
780 $restUrl = site_url('/?rest_route=/' . Rest::WPSTG_ROUTE_NAMESPACE_V1);
781 }
782
783 set_transient(self::TRANSIENT_REST_URL, $restUrl, 24 * HOUR_IN_SECONDS);
784
785 return $restUrl;
786 }
787
788 private function isWorkingTestUrl($url)
789 {
790 $url .= '/ping&accessToken=' . $this->accessToken->getToken();
791 $response = wp_remote_request($url, [
792 'method' => 'GET',
793 'timeout' => 5,
794 'sslverify' => false,
795 'headers' => [
796 'Accept' => 'application/json',
797 ],
798 ]);
799
800 if (is_wp_error($response)) {
801 return false;
802 }
803
804 $code = wp_remote_retrieve_response_code($response);
805 if ($code !== 200) {
806 return false;
807 }
808
809 return true;
810 }
811 }
812