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