BackupPluginsNotice.php
1 day ago
BooleanNotice.php
1 day ago
CliIntegrationNotice.php
1 day ago
DisabledItemsNotice.php
1 day ago
DismissNotice.php
1 day ago
NextGenEngineNotice.php
1 day ago
Notices.php
1 day ago
NoticesHandler.php
1 day ago
ObjectCacheNotice.php
1 day ago
OutdatedWpStagingNotice.php
1 day ago
WarningsNotice.php
1 day ago
WpVersionCompatNotice.php
1 day ago
CliIntegrationNotice.php
431 lines
| 1 | <?php |
| 2 | |
| 3 | namespace WPStaging\Framework\Notices; |
| 4 | |
| 5 | use WPStaging\Core\WPStaging; |
| 6 | use WPStaging\Framework\Security\Auth; |
| 7 | use WPStaging\Framework\Traits\NoticesTrait; |
| 8 | |
| 9 | |
| 10 | |
| 11 | |
| 12 | |
| 13 | |
| 14 | |
| 15 | |
| 16 | |
| 17 | class CliIntegrationNotice |
| 18 | { |
| 19 | use NoticesTrait; |
| 20 | |
| 21 | const IS_ENABLED = true; |
| 22 | |
| 23 | |
| 24 | |
| 25 | |
| 26 | const OPTION_CLI_NOTICE_HIDDEN_FOREVER = 'wpstg_cli_notice_hidden_forever'; |
| 27 | |
| 28 | |
| 29 | |
| 30 | |
| 31 | const OPTION_CLI_DOCK_CTA_SHOWN = 'wpstg_cli_dock_cta_shown'; |
| 32 | |
| 33 | |
| 34 | |
| 35 | |
| 36 | const OPTION_CLI_NOTICE_DISMISSED_UNTIL = 'wpstg_cli_notice_dismissed_until'; |
| 37 | |
| 38 | |
| 39 | |
| 40 | |
| 41 | private $auth; |
| 42 | |
| 43 | |
| 44 | |
| 45 | |
| 46 | public function __construct(Auth $auth) |
| 47 | { |
| 48 | $this->auth = $auth; |
| 49 | } |
| 50 | |
| 51 | |
| 52 | |
| 53 | |
| 54 | |
| 55 | |
| 56 | public function maybeShowCliNotice() |
| 57 | { |
| 58 | if (!self::IS_ENABLED) { |
| 59 | return; |
| 60 | } |
| 61 | |
| 62 | if (!$this->isWPStagingAdminPage()) { |
| 63 | return; |
| 64 | } |
| 65 | |
| 66 | if (!current_user_can('manage_options')) { |
| 67 | return; |
| 68 | } |
| 69 | |
| 70 | if (get_option(self::OPTION_CLI_NOTICE_HIDDEN_FOREVER)) { |
| 71 | return; |
| 72 | } |
| 73 | |
| 74 | if ($this->isTemporarilyDismissed()) { |
| 75 | return; |
| 76 | } |
| 77 | |
| 78 | $notice = WPSTG_VIEWS_DIR . 'notices/cli-integration-notice.php'; |
| 79 | |
| 80 | if (!file_exists($notice)) { |
| 81 | return; |
| 82 | } |
| 83 | |
| 84 | $isDeveloperOrHigher = $this->isDeveloperOrHigherLicense(); |
| 85 | $hasActiveLicense = $this->hasActiveLicense(); |
| 86 | $planName = $this->getLicensePlanName(); |
| 87 | $backups = $this->fetchSortedBackups($isDeveloperOrHigher); |
| 88 | $urlAssets = trailingslashit(WPSTG_PLUGIN_URL) . 'assets/'; |
| 89 | $licenseType = $this->getLicenseTypeSlug(); |
| 90 | $licenseId = $this->getLicenseId(); |
| 91 | |
| 92 | include $notice; |
| 93 | } |
| 94 | |
| 95 | |
| 96 | |
| 97 | |
| 98 | |
| 99 | |
| 100 | |
| 101 | private function isTemporarilyDismissed(): bool |
| 102 | { |
| 103 | $dismissedUntil = (int)get_option(self::OPTION_CLI_NOTICE_DISMISSED_UNTIL, 0); |
| 104 | if ($dismissedUntil === 0) { |
| 105 | return false; |
| 106 | } |
| 107 | |
| 108 | if (time() < $dismissedUntil) { |
| 109 | return true; |
| 110 | } |
| 111 | |
| 112 | delete_option(self::OPTION_CLI_NOTICE_DISMISSED_UNTIL); |
| 113 | return false; |
| 114 | } |
| 115 | |
| 116 | |
| 117 | |
| 118 | |
| 119 | |
| 120 | |
| 121 | |
| 122 | |
| 123 | public function ajaxCliNoticeClose() |
| 124 | { |
| 125 | if (!$this->auth->isAuthenticatedRequest('', 'manage_options')) { |
| 126 | wp_send_json_error(); |
| 127 | } |
| 128 | |
| 129 | update_option(self::OPTION_CLI_NOTICE_DISMISSED_UNTIL, time() + DAY_IN_SECONDS, false); |
| 130 | update_option(self::OPTION_CLI_DOCK_CTA_SHOWN, true, false); |
| 131 | wp_send_json_success(); |
| 132 | } |
| 133 | |
| 134 | |
| 135 | |
| 136 | |
| 137 | |
| 138 | |
| 139 | public function ajaxCliNoticeHideForever() |
| 140 | { |
| 141 | if (!$this->auth->isAuthenticatedRequest('', 'manage_options')) { |
| 142 | wp_send_json_error(); |
| 143 | } |
| 144 | |
| 145 | update_option(self::OPTION_CLI_NOTICE_HIDDEN_FOREVER, true); |
| 146 | update_option(self::OPTION_CLI_DOCK_CTA_SHOWN, true, false); |
| 147 | wp_send_json_success(); |
| 148 | } |
| 149 | |
| 150 | |
| 151 | |
| 152 | |
| 153 | |
| 154 | |
| 155 | |
| 156 | |
| 157 | public function shouldShowDockCta(): bool |
| 158 | { |
| 159 | if (!self::IS_ENABLED) { |
| 160 | return false; |
| 161 | } |
| 162 | |
| 163 | if (!$this->isWPStagingAdminPage()) { |
| 164 | return false; |
| 165 | } |
| 166 | |
| 167 | if (!current_user_can('manage_options')) { |
| 168 | return false; |
| 169 | } |
| 170 | |
| 171 | if (!get_option(self::OPTION_CLI_DOCK_CTA_SHOWN)) { |
| 172 | return false; |
| 173 | } |
| 174 | |
| 175 | |
| 176 | if (!$this->isBannerDismissed()) { |
| 177 | return false; |
| 178 | } |
| 179 | |
| 180 | return true; |
| 181 | } |
| 182 | |
| 183 | |
| 184 | |
| 185 | |
| 186 | |
| 187 | |
| 188 | private function isBannerDismissed(): bool |
| 189 | { |
| 190 | return (bool)get_option(self::OPTION_CLI_NOTICE_HIDDEN_FOREVER) || $this->isTemporarilyDismissed(); |
| 191 | } |
| 192 | |
| 193 | |
| 194 | |
| 195 | |
| 196 | |
| 197 | |
| 198 | |
| 199 | public function isDeveloperOrHigherLicense(): bool |
| 200 | { |
| 201 | return $this->checkLicensingCondition('isActiveAgencyOrDeveloperPlan'); |
| 202 | } |
| 203 | |
| 204 | |
| 205 | |
| 206 | |
| 207 | |
| 208 | |
| 209 | public function isExpiredDeveloperOrHigherLicense(): bool |
| 210 | { |
| 211 | return $this->checkLicensingCondition('isExpiredDeveloperOrAgencyPlan'); |
| 212 | } |
| 213 | |
| 214 | |
| 215 | |
| 216 | |
| 217 | |
| 218 | private function hasActiveLicense(): bool |
| 219 | { |
| 220 | return $this->checkLicensingCondition('isValidOrExpiredLicenseKey'); |
| 221 | } |
| 222 | |
| 223 | |
| 224 | |
| 225 | |
| 226 | |
| 227 | |
| 228 | |
| 229 | public function getLicensePlanName(): string |
| 230 | { |
| 231 | if (WPStaging::isBasic()) { |
| 232 | return __('Free', 'wp-staging'); |
| 233 | } |
| 234 | |
| 235 | if (!class_exists('\WPStaging\Pro\License\Licensing')) { |
| 236 | return __('Unregistered', 'wp-staging'); |
| 237 | } |
| 238 | |
| 239 | $licensing = WPStaging::make(\WPStaging\Pro\License\Licensing::class); |
| 240 | |
| 241 | if (!$licensing->isValidOrExpiredLicenseKey()) { |
| 242 | return __('Unregistered', 'wp-staging'); |
| 243 | } |
| 244 | |
| 245 | $planName = $licensing->getPlanDisplayName(); |
| 246 | |
| 247 | return $planName !== '' ? $planName : __('Unregistered', 'wp-staging'); |
| 248 | } |
| 249 | |
| 250 | |
| 251 | |
| 252 | |
| 253 | |
| 254 | |
| 255 | public function maybeRenderDockCta() |
| 256 | { |
| 257 | if (!$this->shouldShowDockCta()) { |
| 258 | return; |
| 259 | } |
| 260 | |
| 261 | $dockCtaView = WPSTG_VIEWS_DIR . 'cli/cli-dock-cta.php'; |
| 262 | if (!file_exists($dockCtaView)) { |
| 263 | return; |
| 264 | } |
| 265 | |
| 266 | include $dockCtaView; |
| 267 | } |
| 268 | |
| 269 | |
| 270 | |
| 271 | |
| 272 | |
| 273 | |
| 274 | |
| 275 | |
| 276 | |
| 277 | public function maybeRenderCliModalForDockCta() |
| 278 | { |
| 279 | if (!$this->shouldShowDockCta()) { |
| 280 | return; |
| 281 | } |
| 282 | |
| 283 | $this->renderCliModalContent(); |
| 284 | } |
| 285 | |
| 286 | |
| 287 | |
| 288 | |
| 289 | |
| 290 | |
| 291 | private function renderCliModalContent() |
| 292 | { |
| 293 | if (!empty($GLOBALS['wpstg_cli_modal_rendered'])) { |
| 294 | return; |
| 295 | } |
| 296 | |
| 297 | $isDeveloperOrHigher = $this->isDeveloperOrHigherLicense(); |
| 298 | $backups = $this->fetchSortedBackups($isDeveloperOrHigher); |
| 299 | $urlAssets = trailingslashit(WPSTG_PLUGIN_URL) . 'assets/'; |
| 300 | $licenseType = $this->getLicenseTypeSlug(); |
| 301 | $licenseId = $this->getLicenseId(); |
| 302 | |
| 303 | $modalView = WPSTG_VIEWS_DIR . 'cli/cli-integration-modal.php'; |
| 304 | if (file_exists($modalView)) { |
| 305 | include $modalView; |
| 306 | $GLOBALS['wpstg_cli_modal_rendered'] = true; |
| 307 | } |
| 308 | } |
| 309 | |
| 310 | |
| 311 | |
| 312 | |
| 313 | |
| 314 | |
| 315 | private function getLicenseTypeSlug(): string |
| 316 | { |
| 317 | if (!WPStaging::isPro() || !class_exists('\WPStaging\Pro\License\Licensing')) { |
| 318 | return 'free'; |
| 319 | } |
| 320 | |
| 321 | $licensing = WPStaging::make(\WPStaging\Pro\License\Licensing::class); |
| 322 | $type = $licensing->getLicenseType(); |
| 323 | |
| 324 | return $type === 'basic' ? 'free' : $type; |
| 325 | } |
| 326 | |
| 327 | |
| 328 | |
| 329 | |
| 330 | |
| 331 | |
| 332 | |
| 333 | private function getLicenseId(): string |
| 334 | { |
| 335 | $licenseData = $this->getLicenseData(); |
| 336 | if (!$licenseData) { |
| 337 | return ''; |
| 338 | } |
| 339 | |
| 340 | return !empty($licenseData->license_id) ? (string)$licenseData->license_id : ''; |
| 341 | } |
| 342 | |
| 343 | |
| 344 | |
| 345 | |
| 346 | private function getLicenseData() |
| 347 | { |
| 348 | if (!WPStaging::isPro()) { |
| 349 | return null; |
| 350 | } |
| 351 | |
| 352 | $license = get_option('wpstg_license_status', false); |
| 353 | return $license ? (object)$license : null; |
| 354 | } |
| 355 | |
| 356 | |
| 357 | |
| 358 | |
| 359 | |
| 360 | |
| 361 | |
| 362 | private function checkLicensingCondition(string $method): bool |
| 363 | { |
| 364 | if (WPStaging::isBasic()) { |
| 365 | return false; |
| 366 | } |
| 367 | |
| 368 | if (!class_exists('\WPStaging\Pro\License\Licensing')) { |
| 369 | return false; |
| 370 | } |
| 371 | |
| 372 | $licensing = WPStaging::make(\WPStaging\Pro\License\Licensing::class); |
| 373 | return $licensing->$method(); |
| 374 | } |
| 375 | |
| 376 | |
| 377 | |
| 378 | |
| 379 | |
| 380 | |
| 381 | |
| 382 | private function fetchSortedBackups(bool $isDeveloperOrHigher = true): array |
| 383 | { |
| 384 | if (!$isDeveloperOrHigher || !class_exists('\WPStaging\Backup\Ajax\FileList\ListableBackupsCollection')) { |
| 385 | return []; |
| 386 | } |
| 387 | |
| 388 | try { |
| 389 | |
| 390 | $listableBackupsCollection = WPStaging::make(\WPStaging\Backup\Ajax\FileList\ListableBackupsCollection::class); |
| 391 | return $listableBackupsCollection->getSortedListableBackups(); |
| 392 | } catch (\Exception $e) { |
| 393 | return []; |
| 394 | } |
| 395 | } |
| 396 | |
| 397 | |
| 398 | |
| 399 | |
| 400 | |
| 401 | |
| 402 | public function ajaxGetCliBackupList() |
| 403 | { |
| 404 | if (!$this->auth->isAuthenticatedRequest('', 'manage_options')) { |
| 405 | wp_send_json_error(); |
| 406 | } |
| 407 | |
| 408 | $isDeveloperOrHigher = $this->isDeveloperOrHigherLicense(); |
| 409 | $backups = $this->fetchSortedBackups($isDeveloperOrHigher); |
| 410 | $urlAssets = trailingslashit(WPSTG_PLUGIN_URL) . 'assets/'; |
| 411 | |
| 412 | |
| 413 | $hasBackups = false; |
| 414 | foreach ($backups as $backup) { |
| 415 | if (!$backup->isCorrupt && !$backup->isLegacy) { |
| 416 | $hasBackups = true; |
| 417 | break; |
| 418 | } |
| 419 | } |
| 420 | |
| 421 | ob_start(); |
| 422 | include WPSTG_VIEWS_DIR . 'cli/cli-backup-list.php'; |
| 423 | $html = ob_get_clean(); |
| 424 | |
| 425 | wp_send_json_success([ |
| 426 | 'html' => $html, |
| 427 | 'hasBackups' => $hasBackups, |
| 428 | ]); |
| 429 | } |
| 430 | } |
| 431 |