| 1 |
<?php |
| 2 |
|
| 3 |
namespace Templately\Core; |
| 4 |
|
| 5 |
use Templately\Modules\Auth\REST\Profile; |
| 6 |
use Templately\Utils\Base; |
| 7 |
use Templately\Utils\Options; |
| 8 |
|
| 9 |
class Migrator extends Base { |
| 10 |
/** |
| 11 |
* Options Database |
| 12 |
* |
| 13 |
* @var Options|null |
| 14 |
*/ |
| 15 |
private $options = null; |
| 16 |
|
| 17 |
public function __construct(){ |
| 18 |
$this->options = Options::get_instance(); |
| 19 |
add_action('admin_init', [ $this, 'init' ]); |
| 20 |
} |
| 21 |
|
| 22 |
public function init(){ |
| 23 |
$_old_version = $this->options->get_option( '_templately_migrate' ); |
| 24 |
|
| 25 |
/** |
| 26 |
* Migration for v1.3.6 to v2.0.1 |
| 27 |
*/ |
| 28 |
if( \version_compare(TEMPLATELY_VERSION, '2.0.1', '=') && \version_compare($_old_version, '1.3.6', '=') ) { |
| 29 |
$user_choice = $this->options->get_option('_templately_user_login_choice'); |
| 30 |
$user_id = false; |
| 31 |
if( ! empty( $user_choice ) && isset($user_choice['choice']) ) { |
| 32 |
$user_id = intval($user_choice['id']); |
| 33 |
} |
| 34 |
|
| 35 |
$cloud_activity = $this->options->get_option('_templately_cloud_last_activity'); |
| 36 |
$user_profile = $this->options->get_option('_templately_connect_data'); |
| 37 |
$_api_key = $this->options->get_option('_templately_api_key'); |
| 38 |
|
| 39 |
if ( $user_id === false ) { |
| 40 |
$user_id = get_current_user_id(); |
| 41 |
|
| 42 |
$_api_key = $this->options->get('api_key', false, $user_id ); |
| 43 |
$user_profile = $this->options->get('connect_data', false, $user_id ); |
| 44 |
$cloud_activity = $this->options->get('cloud_last_activity', false, $user_id ); |
| 45 |
} |
| 46 |
|
| 47 |
|
| 48 |
if( ! empty( $_api_key ) ) { |
| 49 |
// SET API |
| 50 |
$this->options->set('api_key', $_api_key, $user_id ); |
| 51 |
$this->options->set('cloud_activity', $cloud_activity, $user_id ); |
| 52 |
|
| 53 |
$_favourites = []; |
| 54 |
if( isset( $user_profile['favourites'] ) ) { |
| 55 |
$_favourites = $user_profile['favourites']; |
| 56 |
unset($user_profile['favourites']); |
| 57 |
} |
| 58 |
|
| 59 |
$this->options->set('user', $user_profile, $user_id ); |
| 60 |
$this->options->set('favourites', $_favourites, $user_id ); |
| 61 |
} |
| 62 |
} |
| 63 |
|
| 64 |
/** |
| 65 |
* Migration for v2.2.10 to v2.2.11 |
| 66 |
*/ |
| 67 |
if( TEMPLATELY_VERSION !== $_old_version && \version_compare($_old_version, '2.2.10', '<=') ) { |
| 68 |
Profile::get_instance()->sync(); |
| 69 |
} |
| 70 |
|
| 71 |
$this->cleanup_gutenberg_elementor_meta(); |
| 72 |
|
| 73 |
$this->options->update_option( '_templately_migrate', TEMPLATELY_VERSION ); |
| 74 |
} |
| 75 |
|
| 76 |
/** |
| 77 |
* One-time cleanup: a bug mapped EVERY `templately_library` template to an Elementor |
| 78 |
* document type regardless of platform (the mapping gated on the CPT, not the |
| 79 |
* template's platform). Elementor therefore adopted GUTENBERG templates as Elementor |
| 80 |
* documents and persisted Elementor meta onto them (`_elementor_edit_mode=builder`, |
| 81 |
* `_elementor_template_type`, ...), so they reported `is_built_with_elementor() === true` |
| 82 |
* and surfaced Elementor's "Edit with Elementor" UI for a Gutenberg post. The mapping is |
| 83 |
* now platform-gated (ThemeBuilder::modify_template_type); strip the already-persisted |
| 84 |
* Elementor meta from Gutenberg-platform templates so they are no longer treated as |
| 85 |
* Elementor documents. Runs once (own flag), independent of the version gates above. |
| 86 |
*/ |
| 87 |
private function cleanup_gutenberg_elementor_meta(){ |
| 88 |
$flag = '_templately_cleaned_gutenberg_elementor_meta'; |
| 89 |
|
| 90 |
if ( $this->options->get_option( $flag ) ) { |
| 91 |
return; |
| 92 |
} |
| 93 |
|
| 94 |
global $wpdb; |
| 95 |
|
| 96 |
$post_ids = $wpdb->get_col( $wpdb->prepare( |
| 97 |
"SELECT p.ID FROM {$wpdb->posts} p |
| 98 |
INNER JOIN {$wpdb->postmeta} pm ON pm.post_id = p.ID |
| 99 |
WHERE p.post_type = %s AND pm.meta_key = %s AND pm.meta_value = %s", |
| 100 |
'templately_library', |
| 101 |
'_templately_template_platform', |
| 102 |
'gutenberg' |
| 103 |
) ); |
| 104 |
|
| 105 |
// Pure-Elementor document meta that must not live on a Gutenberg template. |
| 106 |
$meta_keys = [ |
| 107 |
'_elementor_edit_mode', |
| 108 |
'_elementor_template_type', |
| 109 |
'_elementor_data', |
| 110 |
'_elementor_version', |
| 111 |
'_elementor_page_settings', |
| 112 |
'_elementor_css', |
| 113 |
]; |
| 114 |
|
| 115 |
foreach ( (array) $post_ids as $post_id ) { |
| 116 |
foreach ( $meta_keys as $meta_key ) { |
| 117 |
delete_post_meta( (int) $post_id, $meta_key ); |
| 118 |
} |
| 119 |
} |
| 120 |
|
| 121 |
$this->options->update_option( $flag, time() ); |
| 122 |
} |
| 123 |
|
| 124 |
} |
| 125 |
|