| 1 |
<?php |
| 2 |
|
| 3 |
namespace Leadpages; |
| 4 |
|
| 5 |
defined('ABSPATH') || die('No script kiddies please!'); // Avoid direct file request |
| 6 |
|
| 7 |
use Leadpages\providers\Utils; |
| 8 |
use Leadpages\models\DB; |
| 9 |
use Leadpages\models\Options; |
| 10 |
|
| 11 |
/** |
| 12 |
* This class handles plugin operations that need to happen on plugin activation/deactivation |
| 13 |
* or when the plugin is updated to a new version. It is only used in the plugins Core class. |
| 14 |
* |
| 15 |
* Note: we are not doing anything on plugin activation. If this changes in the future we should |
| 16 |
* create a new method in this class and use the register_activation_hook() function to register it |
| 17 |
* in the Core class. |
| 18 |
* |
| 19 |
*/ |
| 20 |
class Activator { |
| 21 |
|
| 22 |
use Utils; |
| 23 |
|
| 24 |
/** @var DB $db */ |
| 25 |
private $db; |
| 26 |
|
| 27 |
public function __construct() { |
| 28 |
$this->db = new DB(); |
| 29 |
} |
| 30 |
|
| 31 |
/** |
| 32 |
* This method gets fired when the user deactivates the plugin. |
| 33 |
* |
| 34 |
* On deactivation, we delete the access and refresh tokens from the options table but leave the |
| 35 |
* rest of the users data intact, should they wish to reactivate later to resume the work they had done. |
| 36 |
*/ |
| 37 |
public function deactivate() { |
| 38 |
Options::delete(Options::$refresh_token); |
| 39 |
Options::delete(Options::$access_token); |
| 40 |
Options::delete(Options::$nova_refresh_token); |
| 41 |
Options::delete(Options::$nova_access_token); |
| 42 |
Options::delete(Options::$nova_code_verifier); |
| 43 |
Options::delete(Options::$nova_oauth_state); |
| 44 |
} |
| 45 |
|
| 46 |
/** |
| 47 |
* Ensure that the database schema this plugin is dependent on is up to date for the current |
| 48 |
* version of the plugin, whatever that might be. |
| 49 |
* |
| 50 |
* An admin notice will be shown if the database update failed. This would be horrendous if |
| 51 |
* it occurred but there is little we can do and need to bubble it up to the user. |
| 52 |
*/ |
| 53 |
public function update_db_check() { |
| 54 |
$lpdb = $this->db; |
| 55 |
$current_db_version = Options::get(Options::$db_version); |
| 56 |
|
| 57 |
try { |
| 58 |
// if the database version is not set we need to install all tables |
| 59 |
// otherwise migrate the database to the latest version |
| 60 |
if (! $current_db_version) { |
| 61 |
$this->debug('Installing leadpages database tables'); |
| 62 |
$lpdb->install(); |
| 63 |
} else { |
| 64 |
$this->debug("Migrating leadpages database tables from $current_db_version to " . LEADPAGES_VERSION); |
| 65 |
$lpdb->migrate($current_db_version); |
| 66 |
} |
| 67 |
} catch (\Exception $e) { |
| 68 |
add_action( |
| 69 |
'admin_notices', |
| 70 |
function () { |
| 71 |
echo wp_kses( |
| 72 |
"<div class='notice notice-error is-dismissible'><p> |
| 73 |
Leadpages encountered an error while installing/updating its database tables. |
| 74 |
You can try refreshing the page or |
| 75 |
<a href='https://support.leadpages.com/hc/en-us/articles/205046170'> |
| 76 |
contact support |
| 77 |
</a> |
| 78 |
for assistance. |
| 79 |
</p></div>", |
| 80 |
[ |
| 81 |
'div' => [ |
| 82 |
'class' => [], |
| 83 |
], |
| 84 |
'p' => [], |
| 85 |
'a' => [ |
| 86 |
'href' => [], |
| 87 |
], |
| 88 |
] |
| 89 |
); |
| 90 |
} |
| 91 |
); |
| 92 |
} |
| 93 |
} |
| 94 |
} |
| 95 |
|