| 1 |
<?php |
| 2 |
|
| 3 |
namespace Templately\Core; |
| 4 |
|
| 5 |
use Templately\Utils\Base; |
| 6 |
use Templately\Utils\Options; |
| 7 |
|
| 8 |
class Migrator extends Base { |
| 9 |
/** |
| 10 |
* Options Database |
| 11 |
* |
| 12 |
* @var Options|null |
| 13 |
*/ |
| 14 |
private $options = null; |
| 15 |
|
| 16 |
public function __construct(){ |
| 17 |
$this->options = Options::get_instance(); |
| 18 |
add_action('admin_init', [ $this, 'init' ]); |
| 19 |
} |
| 20 |
|
| 21 |
public function init(){ |
| 22 |
$_old_version = $this->options->get_option( '_templately_migrate' ); |
| 23 |
|
| 24 |
/** |
| 25 |
* Migration for v1.3.6 to v2.0.1 |
| 26 |
*/ |
| 27 |
if( \version_compare(TEMPLATELY_VERSION, '2.0.1', '=') && \version_compare($_old_version, '1.3.6', '=') ) { |
| 28 |
$user_choice = $this->options->get_option('_templately_user_login_choice'); |
| 29 |
$user_id = false; |
| 30 |
if( ! empty( $user_choice ) && isset($user_choice['choice']) ) { |
| 31 |
$user_id = intval($user_choice['id']); |
| 32 |
} |
| 33 |
|
| 34 |
$cloud_activity = $this->options->get_option('_templately_cloud_last_activity'); |
| 35 |
$user_profile = $this->options->get_option('_templately_connect_data'); |
| 36 |
$_api_key = $this->options->get_option('_templately_api_key'); |
| 37 |
|
| 38 |
if ( $user_id === false ) { |
| 39 |
$user_id = get_current_user_id(); |
| 40 |
|
| 41 |
$_api_key = $this->options->get('api_key', false, $user_id ); |
| 42 |
$user_profile = $this->options->get('connect_data', false, $user_id ); |
| 43 |
$cloud_activity = $this->options->get('cloud_last_activity', false, $user_id ); |
| 44 |
} |
| 45 |
|
| 46 |
|
| 47 |
if( ! empty( $_api_key ) ) { |
| 48 |
// SET API |
| 49 |
$this->options->set('api_key', $_api_key, $user_id ); |
| 50 |
$this->options->set('cloud_activity', $cloud_activity, $user_id ); |
| 51 |
|
| 52 |
$_favourites = []; |
| 53 |
if( isset( $user_profile['favourites'] ) ) { |
| 54 |
$_favourites = $user_profile['favourites']; |
| 55 |
unset($user_profile['favourites']); |
| 56 |
} |
| 57 |
|
| 58 |
$this->options->set('user', $user_profile, $user_id ); |
| 59 |
$this->options->set('favourites', $_favourites, $user_id ); |
| 60 |
} |
| 61 |
} |
| 62 |
|
| 63 |
$this->options->update_option( '_templately_migrate', TEMPLATELY_VERSION ); |
| 64 |
} |
| 65 |
|
| 66 |
} |
| 67 |
|