| 1 |
<?php |
| 2 |
/* |
| 3 |
* SOFTWARE LICENSE INFORMATION |
| 4 |
* |
| 5 |
* Copyright (c) 2017 Buttonizer, all rights reserved. |
| 6 |
* |
| 7 |
* This file is part of Buttonizer |
| 8 |
* |
| 9 |
* For detailed information regarding to the licensing of |
| 10 |
* this software, please review the license.txt or visit: |
| 11 |
* https://buttonizer.pro/license/ |
| 12 |
*/ |
| 13 |
|
| 14 |
namespace Buttonizer\Migration; |
| 15 |
|
| 16 |
# No script kiddies |
| 17 |
defined('ABSPATH') or die('No script kiddies please!'); |
| 18 |
|
| 19 |
/** |
| 20 |
* The way back to an absorbed plugin's old system. |
| 21 |
* |
| 22 |
* The acquired plugin offers this to anyone who has not signed up yet, and a |
| 23 |
* user who moves to Buttonizer should not lose it. Buttonizer offers the same |
| 24 |
* thing, in the same place — the signup screen — under one condition the |
| 25 |
* original could not check: that there is actually something to go back to. |
| 26 |
* |
| 27 |
* A site with no old buttons would land in an empty screen of a deprecated |
| 28 |
* system with nothing in it. That is not a way back, it is a way nowhere, so |
| 29 |
* those users are never offered it. |
| 30 |
* |
| 31 |
* It is offered even while the old system is already running, because that is |
| 32 |
* the screen someone reaches from the module's own "Try now" — one click away |
| 33 |
* from signing up and taking their buttons off the site. Hiding the way back |
| 34 |
* there, on the grounds that they have not left yet, hides it exactly when it |
| 35 |
* is worth reading. Following it from that state simply returns them to their |
| 36 |
* own screens, which is what "not ready yet" has always meant. |
| 37 |
*/ |
| 38 |
class LegacyFallback |
| 39 |
{ |
| 40 |
/** |
| 41 |
* The old system this site could return to, if any. |
| 42 |
*/ |
| 43 |
public static function available(): ?SourcePlugin |
| 44 |
{ |
| 45 |
foreach (SourcePlugins::all() as $source) { |
| 46 |
if (self::isAvailableFor($source)) { |
| 47 |
return $source; |
| 48 |
} |
| 49 |
} |
| 50 |
|
| 51 |
return null; |
| 52 |
} |
| 53 |
|
| 54 |
/** |
| 55 |
* Put the old system back and say where the user should land. |
| 56 |
* |
| 57 |
* Only the decision is reversed. The old options were never touched, so |
| 58 |
* the buttons come back exactly as the user left them. |
| 59 |
* |
| 60 |
* @return string|null Admin URL of the restored system, null when there is |
| 61 |
* nothing to restore. |
| 62 |
*/ |
| 63 |
public static function restore(): ?string |
| 64 |
{ |
| 65 |
$source = self::available(); |
| 66 |
|
| 67 |
if (!$source) { |
| 68 |
return null; |
| 69 |
} |
| 70 |
|
| 71 |
// Forget the move. This is what stops Buttonizer from serving the old |
| 72 |
// system: leaving the cloud is not enough on its own, because leaving |
| 73 |
// it by accident should not change what renders on someone's site. |
| 74 |
// |
| 75 |
// From here on Buttonizer serves that system itself, and says so: a |
| 76 |
// site that came across as a cloud connection was never marked as |
| 77 |
// embedded, and the loader only serves what is. |
| 78 |
MigrationState::set($source->id(), [ |
| 79 |
'moved_to_cloud' => false, |
| 80 |
'result' => MigrationManager::RESULT_LEGACY_MODULE, |
| 81 |
]); |
| 82 |
|
| 83 |
// A site that came in through the cloud never had this flag set, and |
| 84 |
// the old system reads it on every load. |
| 85 |
if ($source->legacyFlagOption()) { |
| 86 |
update_option($source->legacyFlagOption(), $source->legacyFlagValue()); |
| 87 |
} |
| 88 |
|
| 89 |
return admin_url('admin.php?page=' . $source->modulePageSlug()); |
| 90 |
} |
| 91 |
|
| 92 |
/** |
| 93 |
* Can this site go back to that plugin's old system? |
| 94 |
*/ |
| 95 |
private static function isAvailableFor(SourcePlugin $source): bool |
| 96 |
{ |
| 97 |
// Buttonizer carries the code and this site actually came from there. |
| 98 |
// A plain Buttonizer install ships the module like every other copy, |
| 99 |
// which is why the module alone proves nothing. |
| 100 |
if (!$source->hasModule() || !MigrationState::isMigrated($source->id())) { |
| 101 |
return false; |
| 102 |
} |
| 103 |
|
| 104 |
// The plugin itself is back and serving its own screens. |
| 105 |
// |
| 106 |
// Deliberately about the plugin's own active flag, not isModuleLoaded(): |
| 107 |
// Buttonizer's own ModuleLoader requires the module on every request |
| 108 |
// while it is embedded, including this one, so that check is true here |
| 109 |
// regardless of whether the source plugin is active. Going by it would |
| 110 |
// hide the way back on the one screen it has to stay reachable from. |
| 111 |
if ($source->isActive()) { |
| 112 |
return false; |
| 113 |
} |
| 114 |
|
| 115 |
return true; |
| 116 |
} |
| 117 |
} |
| 118 |
|