PluginProbe ʕ •ᴥ•ʔ
WP STAGING – WordPress Backup, Restore, Migration & Clone / 3.1.2
WP STAGING – WordPress Backup, Restore, Migration & Clone v3.1.2
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 2 years ago
Assets.php
480 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
153 // Load below assets only on wp staging admin pages
154 if ($this->isNotWPStagingAdminPage($hook)) {
155 return;
156 }
157
158 // Load admin js files
159 $asset = 'js/dist/wpstg.js';
160 wp_enqueue_script(
161 "wpstg-common",
162 $this->getAssetsUrl($asset),
163 ["jquery"],
164 $this->getAssetsVersion($asset),
165 false
166 );
167
168 // Load admin js files
169 $asset = 'js/dist/wpstg-admin.min.js';
170 if ($this->isDebugOrDevMode()) {
171 $asset = 'js/dist/wpstg-admin.js';
172 }
173
174 wp_enqueue_script(
175 "wpstg-admin-script",
176 $this->getAssetsUrl($asset),
177 ["wpstg-common", "wpstg-admin-notyf", "wpstg-admin-sweetalerts"],
178 $this->getAssetsVersion($asset),
179 false
180 );
181
182 // Sweet Alert
183 $asset = 'js/dist/wpstg-sweetalert2.min.js';
184 if ($this->isDebugOrDevMode()) {
185 $asset = 'js/dist/wpstg-sweetalert2.js';
186 }
187
188 wp_enqueue_script(
189 'wpstg-admin-sweetalerts',
190 $this->getAssetsUrl($asset),
191 [],
192 $this->getAssetsVersion($asset),
193 true
194 );
195
196 $asset = 'css/dist/wpstg-sweetalert2.min.css';
197 if ($this->isDebugOrDevMode()) {
198 $asset = 'css/dist/wpstg-sweetalert2.css';
199 }
200
201 wp_enqueue_style(
202 'wpstg-admin-sweetalerts',
203 $this->getAssetsUrl($asset),
204 [],
205 $this->getAssetsVersion($asset)
206 );
207
208 // Notyf Toast Notification
209 $asset = 'js/vendor/notyf.min.js';
210 wp_enqueue_script(
211 'wpstg-admin-notyf',
212 $this->getAssetsUrl($asset),
213 [],
214 $this->getAssetsVersion($asset),
215 true
216 );
217
218 $asset = 'css/vendor/notyf.min.css';
219 wp_enqueue_style(
220 'wpstg-admin-notyf',
221 $this->getAssetsUrl($asset),
222 [],
223 $this->getAssetsVersion($asset)
224 );
225
226 // Internal hook to enqueue backup scripts, used by the backup addon
227 do_action('wpstg_enqueue_backup_scripts', $this->isDebugOrDevMode());
228
229 // Load admin js pro files
230 if (defined('WPSTGPRO_VERSION')) {
231 $asset = 'js/dist/pro/wpstg-admin-pro.min.js';
232 if ($this->isDebugOrDevMode()) {
233 $asset = 'js/dist/pro/wpstg-admin-pro.js';
234 }
235
236 wp_enqueue_script(
237 "wpstg-admin-pro-script",
238 $this->getAssetsUrl($asset),
239 ["jquery", "wpstg-admin-notyf", "wpstg-admin-sweetalerts"],
240 $this->getAssetsVersion($asset),
241 false
242 );
243 }
244
245 // Load admin css files
246 $asset = 'css/dist/wpstg-admin.min.css';
247 if ($this->isDebugOrDevMode()) {
248 $asset = 'css/dist/wpstg-admin.css';
249 }
250
251 wp_enqueue_style(
252 "wpstg-admin",
253 $this->getAssetsUrl($asset),
254 [],
255 $this->getAssetsVersion($asset)
256 );
257
258 $wpstgConfig = [
259 //"delayReq" => $this->getDelay(),
260 "delayReq" => 0,
261 // TODO: move directorySeparator to consts?
262 "settings" => (object)[
263 "directorySeparator" => ScanConst::DIRECTORIES_SEPARATOR
264 ],
265 "tblprefix" => WPStaging::getTablePrefix(),
266 "isMultisite" => is_multisite(),
267 AccessToken::REQUEST_KEY => (string)$this->accessToken->getToken() ?: (string)$this->accessToken->generateNewToken(),
268 'nonce' => wp_create_nonce(Nonce::WPSTG_NONCE),
269 'assetsUrl' => $this->getAssetsUrl(),
270 'ajaxUrl' => admin_url('admin-ajax.php'),
271 'wpstgIcon' => $this->getAssetsUrl('img/wpstaging-icon.png'),
272 'maxUploadChunkSize' => $this->getMaxUploadChunkSize(),
273 'backupDBExtension' => DatabaseBackupTask::PART_IDENTIFIER . '.' . DatabaseBackupTask::FILE_FORMAT,
274 'analyticsConsentAllow' => esc_url($this->analyticsConsent->getConsentLink(true)),
275 'analyticsConsentDeny' => esc_url($this->analyticsConsent->getConsentLink(false)),
276 // TODO: handle i18n translations through Class/Service Provider?
277 'i18n' => [
278 'dbConnectionSuccess' => esc_html__('Database Connection - Success', 'wp-staging'),
279 'dbConnectionFailed' => esc_html__('Database Connection - Failed', 'wp-staging'),
280 'somethingWentWrong' => esc_html__('Something went wrong.', 'wp-staging'),
281 'noRestoreFileFound' => esc_html__('No backup file found.', 'wp-staging'),
282 'selectFileToRestore' => esc_html__('Select backup file to restore.', 'wp-staging'),
283 'cloneResetComplete' => esc_html__('Reset Complete!', 'wp-staging'),
284 'cloneUpdateComplete' => esc_html__('Update Complete!', 'wp-staging'),
285 'success' => esc_html__('Success', 'wp-staging'),
286 'resetClone' => esc_html__('Reset Staging Site', 'wp-staging'),
287 'showLogs' => esc_html__('Show Logs', 'wp-staging'),
288 'hideLogs' => esc_html__('Hide Logs', 'wp-staging'),
289 'noTableSelected' => esc_html__('No table selected', 'wp-staging'),
290 'tablesSelected' => esc_html__('{d} tables(s) selected', 'wp-staging'),
291 'noFileSelected' => esc_html__('No file selected', 'wp-staging'),
292 'filesSelected' => esc_html__('{t} theme(s), {p} plugin(s) selected', 'wp-staging'),
293 'wpstg_cloning' => [
294 'title' => esc_html__('Staging Site Created Successfully!', 'wp-staging'),
295 'body' => esc_html__('You can access it from here:', 'wp-staging'),
296 ],
297 'wpstg_update' => [
298 'title' => esc_html__('Staging Site Updated Successfully!', 'wp-staging'),
299 'body' => esc_html__('You can access it from here:', 'wp-staging'),
300 ],
301 'wpstg_push_processing' => [
302 'title' => esc_html__('Staging Site Pushed Successfully!', 'wp-staging'),
303 'body' => esc_html__('Clear the site cache if changes are not visible.', 'wp-staging'),
304 ],
305 'wpstg_reset' => [
306 'title' => esc_html__('Staging Site Reset Successfully!', 'wp-staging'),
307 'body' => esc_html__('You can access it from here:', 'wp-staging'),
308 ],
309 'wpstg_delete_clone' => [
310 'title' => esc_html__('Staging Site Deleted Successfully!', 'wp-staging'),
311 ],
312 ],
313 ];
314
315 wp_localize_script("wpstg-admin-script", "wpstg", $wpstgConfig);
316 }
317
318 /**
319 * Load js vars globally but NOT on wp staging admin pages
320 * @return void
321 */
322 private function loadGlobalAssets($pageSlug)
323 {
324 if (!$this->isNotWPStagingAdminPage($pageSlug)) {
325 return;
326 }
327
328 wp_enqueue_script('wpstg-global', $this->getAssetsUrl('js/dist/wpstg-blank-loader.js'), [], [], false);
329
330 $vars = [
331 'nonce' => wp_create_nonce(Nonce::WPSTG_NONCE),
332 ];
333
334 wp_localize_script("wpstg-global", "wpstg", $vars);
335 }
336
337 /**
338 * @return int The max upload size for a file.
339 */
340 protected function getMaxUploadChunkSize()
341 {
342 $lowerLimit = 64 * KB_IN_BYTES;
343 $upperLimit = 16 * MB_IN_BYTES;
344
345 $maxPostSize = wp_convert_hr_to_bytes(ini_get('post_max_size'));
346 $uploadMaxFileSize = wp_convert_hr_to_bytes(ini_get('upload_max_filesize'));
347
348 // The real limit, read from the PHP context.
349 $limit = min($maxPostSize, $uploadMaxFileSize) * 0.90;
350
351 // Do not allow going over upper limit.
352 $limit = min($limit, $upperLimit);
353
354 // Do not allow going under lower limit.
355 $limit = max($lowerLimit, $limit);
356
357 return (int)$limit;
358 }
359
360 /**
361 * Load css and js files only on wp staging admin pages
362 *
363 * @param $page string slug of the current page
364 *
365 * @return bool
366 */
367 private function isNotWPStagingAdminPage($page)
368 {
369 if (defined('WPSTGPRO_VERSION')) {
370 $availablePages = [
371 "toplevel_page_wpstg_clone",
372 "wp-staging-pro_page_wpstg_backup",
373 "wp-staging-pro_page_wpstg-settings",
374 "wp-staging-pro_page_wpstg-tools",
375 "wp-staging-pro_page_wpstg-license",
376 ];
377 } else {
378 $availablePages = [
379 "toplevel_page_wpstg_clone",
380 "wp-staging_page_wpstg_backup",
381 "wp-staging_page_wpstg-settings",
382 "wp-staging_page_wpstg-tools",
383 "wp-staging_page_wpstg-welcome",
384 ];
385 }
386
387 return !in_array($page, $availablePages) || !is_admin();
388 }
389
390 /**
391 * Remove heartbeat api and user login check
392 *
393 * @action admin_enqueue_scripts 100 1
394 * @see AssetServiceProvider.php
395 *
396 * @param bool $hook
397 */
398 public function removeWPCoreJs($hook)
399 {
400 if ($this->isNotWPStagingAdminPage($hook)) {
401 return;
402 }
403
404 // Disable user login status check
405 // Todo: Can we remove this now that we have AccessToken?
406 remove_action('admin_enqueue_scripts', 'wp_auth_check_load');
407
408 // Disable heartbeat check for cloning and pushing
409 wp_deregister_script('heartbeat');
410 }
411
412 /**
413 * Check if current page is plugins.php
414 * @global array $pagenow
415 * @return bool
416 */
417 private function isPluginsPage()
418 {
419 global $pagenow;
420
421 return ($pagenow === 'plugins.php');
422 }
423
424 /**
425 * @return int
426 */
427 /* public function getDelay()
428 {
429 switch ($this->settings->getDelayRequests()) {
430 case "0":
431 $delay = 0;
432 break;
433
434 case "1":
435 $delay = 1000;
436 break;
437
438 case "2":
439 $delay = 2000;
440 break;
441
442 case "3":
443 $delay = 3000;
444 break;
445
446 case "4":
447 $delay = 4000;
448 break;
449
450 default:
451 $delay = 0;
452 }
453
454 return $delay;
455 }*/
456
457 /**
458 * @return string
459 */
460 public function getStagingAdminBarColor()
461 {
462 $barColor = $this->settings->getAdminBarColor();
463 if (!preg_match("/#([a-f0-9]{3}){1,2}\b/i", $barColor)) {
464 $barColor = self::DEFAULT_ADMIN_BAR_BG;
465 }
466
467 return "#wpadminbar { background-color: {$barColor} !important; }";
468 }
469
470 /**
471 * Check whether app is in debug mode or in dev mode
472 *
473 * @return bool
474 */
475 private function isDebugOrDevMode()
476 {
477 return ($this->settings->isDebugMode() || (defined('WPSTG_DEV') && WPSTG_DEV === true) || (defined('WPSTG_DEBUG') && WPSTG_DEBUG === true));
478 }
479 }
480