| 1 |
<?php |
| 2 |
namespace Elementor\Core\Upgrade; |
| 3 |
|
| 4 |
use Elementor\Core\Common\Modules\Ajax\Module as Ajax; |
| 5 |
use Elementor\Core\Settings\Manager as SettingsManager; |
| 6 |
use Elementor\Core\Settings\Page\Manager as SettingsPageManager; |
| 7 |
use Elementor\Plugin; |
| 8 |
use Elementor\Tools; |
| 9 |
|
| 10 |
if ( ! defined( 'ABSPATH' ) ) { |
| 11 |
exit; // Exit if accessed directly |
| 12 |
} |
| 13 |
|
| 14 |
class Elementor_3_Re_Migrate_Globals { |
| 15 |
|
| 16 |
public function register_ajax_actions( Ajax $ajax ) { |
| 17 |
$ajax->register_ajax_action( 're_migrate_globals', [ $this, 'ajax_re_migrate_globals' ] ); |
| 18 |
} |
| 19 |
|
| 20 |
public function ajax_re_migrate_globals() { |
| 21 |
if ( ! current_user_can( 'manage_options' ) ) { |
| 22 |
throw new \Exception( 'permission denied' ); |
| 23 |
} |
| 24 |
|
| 25 |
$this->run(); |
| 26 |
|
| 27 |
return true; |
| 28 |
} |
| 29 |
|
| 30 |
/** |
| 31 |
* @deprecated 3.1.0 |
| 32 |
*/ |
| 33 |
public function admin_localize_settings() { |
| 34 |
Plugin::$instance->modules_manager->get_modules( 'dev-tools' )->deprecation->deprecated_function( __METHOD__, '3.1.0' ); |
| 35 |
|
| 36 |
return []; |
| 37 |
} |
| 38 |
|
| 39 |
public function register_admin_tools_fields( Tools $tools ) { |
| 40 |
// Add the re-migrate button only if needed. |
| 41 |
if ( ! $this->has_typography() && ! $this->has_colors() && ! $this->has_saved_colors() ) { |
| 42 |
return; |
| 43 |
} |
| 44 |
|
| 45 |
// Add the button to the rollback screen. |
| 46 |
$tools->add_fields( 'versions', 'rollback', [ |
| 47 |
're_migrate_globals_separator' => [ |
| 48 |
'field_args' => [ |
| 49 |
'type' => 'raw_html', |
| 50 |
'html' => '<hr>', |
| 51 |
], |
| 52 |
], |
| 53 |
're_migrate_globals' => [ |
| 54 |
'label' => esc_html__( 'Rerun Update Script', 'elementor' ), |
| 55 |
'field_args' => [ |
| 56 |
'type' => 'raw_html', |
| 57 |
'html' => sprintf( |
| 58 |
'<a href="#" data-nonce="%s" class="button elementor-button-spinner elementor-re-migrate-globals-button">%s</a>', |
| 59 |
wp_create_nonce( 'elementor_re_migrate_globals' ), |
| 60 |
__( 'Migrate to v3.0', 'elementor' ) |
| 61 |
), |
| 62 |
'desc' => '<span style="color: red;">' . esc_html__( 'Warning: This will reset your current Global Fonts and Colors, and will migrate your previous settings from v2.x versions.', 'elementor' ) . '</span>', |
| 63 |
], |
| 64 |
], |
| 65 |
] ); |
| 66 |
} |
| 67 |
|
| 68 |
private function run() { |
| 69 |
$this->notice( 'Re-migrate 3.0.0 globals: Start' ); |
| 70 |
|
| 71 |
$callbacks = []; |
| 72 |
|
| 73 |
$kit = Plugin::$instance->kits_manager->get_active_kit(); |
| 74 |
// Already exist. use raw settings that doesn't have default values. |
| 75 |
$meta_key = SettingsPageManager::META_KEY; |
| 76 |
$kit_raw_settings = $kit->get_meta( $meta_key ); |
| 77 |
|
| 78 |
// Apply a default value for $kit_raw_settings in case it's not defined. |
| 79 |
if ( empty( $kit_raw_settings ) ) { |
| 80 |
$kit_raw_settings = []; |
| 81 |
} |
| 82 |
|
| 83 |
if ( $this->has_typography() ) { |
| 84 |
$callbacks[] = [ Upgrades::class, '_v_3_0_0_move_default_typography_to_kit' ]; |
| 85 |
unset( $kit_raw_settings['system_typography'] ); |
| 86 |
} else { |
| 87 |
$this->notice( 'Typography not found' ); |
| 88 |
} |
| 89 |
|
| 90 |
if ( $this->has_colors() ) { |
| 91 |
$callbacks[] = [ Upgrades::class, '_v_3_0_0_move_default_colors_to_kit' ]; |
| 92 |
unset( $kit_raw_settings['system_colors'] ); |
| 93 |
} else { |
| 94 |
$this->notice( 'Colors not found' ); |
| 95 |
} |
| 96 |
|
| 97 |
if ( $this->has_saved_colors() ) { |
| 98 |
$callbacks[] = [ Upgrades::class, '_v_3_0_0_move_saved_colors_to_kit' ]; |
| 99 |
unset( $kit_raw_settings['custom_colors'] ); |
| 100 |
} else { |
| 101 |
$this->notice( 'Saved Colors not found' ); |
| 102 |
} |
| 103 |
|
| 104 |
if ( ! empty( $callbacks ) ) { |
| 105 |
$page_settings_manager = SettingsManager::get_settings_managers( 'page' ); |
| 106 |
$page_settings_manager->save_settings( $kit_raw_settings, $active_kit_id ); |
| 107 |
|
| 108 |
$this->run_callbacks( $callbacks ); |
| 109 |
} |
| 110 |
|
| 111 |
Plugin::$instance->files_manager->clear_cache(); |
| 112 |
|
| 113 |
$this->notice( 'Re-migrate 3.0.0 globals: End' ); |
| 114 |
} |
| 115 |
|
| 116 |
private function run_callbacks( $callbacks ) { |
| 117 |
foreach ( $callbacks as $callback ) { |
| 118 |
$updater = $this->create_updater(); |
| 119 |
|
| 120 |
$this->notice( 'Re-migrate 3.0.0 globals: ' . $callback[1] ); |
| 121 |
|
| 122 |
// Run upgrade. |
| 123 |
call_user_func( $callback, $updater, false ); |
| 124 |
} |
| 125 |
} |
| 126 |
|
| 127 |
/** |
| 128 |
* @return Updater |
| 129 |
*/ |
| 130 |
private function create_updater() { |
| 131 |
$upgrades_manager = new Manager(); |
| 132 |
|
| 133 |
/** @var Updater $updater */ |
| 134 |
$updater = $upgrades_manager->get_task_runner(); |
| 135 |
|
| 136 |
$updater->set_current_item( [ |
| 137 |
'iterate_num' => 1, |
| 138 |
] ); |
| 139 |
|
| 140 |
return $updater; |
| 141 |
} |
| 142 |
|
| 143 |
private function has_typography() { |
| 144 |
return ! ! get_option( 'elementor_scheme_typography' ); |
| 145 |
} |
| 146 |
|
| 147 |
private function has_colors() { |
| 148 |
return ! ! get_option( 'elementor_scheme_color' ); |
| 149 |
} |
| 150 |
|
| 151 |
private function has_saved_colors() { |
| 152 |
return ! ! get_option( 'elementor_scheme_color-picker' ); |
| 153 |
} |
| 154 |
|
| 155 |
private function notice( $message ) { |
| 156 |
$logger = Plugin::$instance->logger->get_logger(); |
| 157 |
$logger->notice( $message ); |
| 158 |
} |
| 159 |
|
| 160 |
public function __construct() { |
| 161 |
add_action( 'elementor/admin/after_create_settings/' . Tools::PAGE_ID, [ $this, 'register_admin_tools_fields' ], 60 /* After plugins rollback */ ); |
| 162 |
add_action( 'elementor/ajax/register_actions', [ $this, 'register_ajax_actions' ] ); |
| 163 |
|
| 164 |
} |
| 165 |
} |
| 166 |
|