| 1 |
<?php |
| 2 |
/** |
| 3 |
* Migration Initialization for Easy Invoice |
| 4 |
* |
| 5 |
* @package EasyInvoice |
| 6 |
* @subpackage Migration |
| 7 |
* @since 2.0.0 |
| 8 |
*/ |
| 9 |
|
| 10 |
namespace EasyInvoice\Migration; |
| 11 |
|
| 12 |
/** |
| 13 |
* Migration initialization class. |
| 14 |
* |
| 15 |
* Handles all migration system initialization and setup. |
| 16 |
* |
| 17 |
* @since 2.0.0 |
| 18 |
*/ |
| 19 |
class MigrationInit { |
| 20 |
|
| 21 |
/** |
| 22 |
* Initialize the complete migration system. |
| 23 |
* |
| 24 |
* @since 2.0.0 |
| 25 |
* @return void |
| 26 |
*/ |
| 27 |
public static function init() { |
| 28 |
// Add admin notices and migration page |
| 29 |
add_action('admin_notices', [__CLASS__, 'show_migration_notice']); |
| 30 |
add_action('admin_menu', [__CLASS__, 'add_migration_page'], 20); |
| 31 |
add_action('admin_menu', [__CLASS__, 'hide_migration_menu'], 999); |
| 32 |
add_action('admin_init', [__CLASS__, 'handle_migration_redirect']); |
| 33 |
|
| 34 |
// Only initialize migration loader if needed |
| 35 |
if (self::should_initialize_migration()) { |
| 36 |
MigrationLoader::init(); |
| 37 |
} |
| 38 |
} |
| 39 |
|
| 40 |
/** |
| 41 |
* Check if migration should be initialized. |
| 42 |
* |
| 43 |
* @since 2.0.0 |
| 44 |
* @return bool |
| 45 |
*/ |
| 46 |
private static function should_initialize_migration(): bool { |
| 47 |
// Check if we're in admin area |
| 48 |
if (!is_admin()) { |
| 49 |
return false; |
| 50 |
} |
| 51 |
|
| 52 |
// Check if migration is needed |
| 53 |
$version_checker = VersionChecker::get_instance(); |
| 54 |
return $version_checker->is_migration_needed(); |
| 55 |
} |
| 56 |
|
| 57 |
/** |
| 58 |
* Show migration notice in admin. |
| 59 |
* |
| 60 |
* @since 2.0.0 |
| 61 |
* @return void |
| 62 |
*/ |
| 63 |
public static function show_migration_notice() { |
| 64 |
// Only show in admin area |
| 65 |
if (!is_admin()) { |
| 66 |
return; |
| 67 |
} |
| 68 |
|
| 69 |
// Check if migration is already completed |
| 70 |
$version_checker = VersionChecker::get_instance(); |
| 71 |
if ($version_checker->is_migration_completed()) { |
| 72 |
return; |
| 73 |
} |
| 74 |
|
| 75 |
// Check if migration is needed |
| 76 |
if (!$version_checker->is_migration_needed()) { |
| 77 |
return; |
| 78 |
} |
| 79 |
|
| 80 |
// Additional check: if this appears to be a fresh installation, |
| 81 |
// automatically mark it as completed and don't show notice |
| 82 |
if ($version_checker->is_fresh_installation()) { |
| 83 |
$version_checker->mark_fresh_installation_completed(); |
| 84 |
return; |
| 85 |
} |
| 86 |
|
| 87 |
// Get current screen |
| 88 |
$screen = get_current_screen(); |
| 89 |
|
| 90 |
// Determine notice class based on screen |
| 91 |
$notice_class = 'notice-warning'; |
| 92 |
if ($screen && property_exists($screen, 'id') && strpos($screen->id, 'easy-invoice') !== false) { |
| 93 |
$notice_class = 'notice-error'; |
| 94 |
} |
| 95 |
|
| 96 |
?> |
| 97 |
<div class="notice <?php echo esc_attr($notice_class); ?> is-dismissible"> |
| 98 |
<p> |
| 99 |
<strong><?php esc_html_e('Easy Invoice Migration Required', 'easy-invoice'); ?></strong> |
| 100 |
<br> |
| 101 |
<?php esc_html_e('Your Easy Invoice plugin needs to be migrated to version 2.0.0. This will update your data structure and ensure compatibility with the new version.', 'easy-invoice'); ?> |
| 102 |
</p> |
| 103 |
<p> |
| 104 |
<a href="<?php echo esc_url(admin_url('admin.php?page=easy-invoice-migration')); ?>" class="button button-primary"> |
| 105 |
<?php esc_html_e('Run Migration', 'easy-invoice'); ?> |
| 106 |
</a> |
| 107 |
<a href="<?php echo esc_url(wp_nonce_url(admin_url('admin.php?page=easy-invoice-migration&action=skip'), 'easy_invoice_migration_skip')); ?>" class="button button-secondary"> |
| 108 |
<?php esc_html_e('Skip for Now', 'easy-invoice'); ?> |
| 109 |
</a> |
| 110 |
</p> |
| 111 |
</div> |
| 112 |
<?php |
| 113 |
} |
| 114 |
|
| 115 |
/** |
| 116 |
* Add migration page to admin menu. |
| 117 |
* |
| 118 |
* @since 2.0.0 |
| 119 |
* @return void |
| 120 |
*/ |
| 121 |
public static function add_migration_page() { |
| 122 |
$version_checker = VersionChecker::get_instance(); |
| 123 |
|
| 124 |
if ($version_checker->is_migration_needed()) { |
| 125 |
// Show migration menu when migration is needed |
| 126 |
add_submenu_page( |
| 127 |
'easy-invoice', |
| 128 |
__('Easy Invoice Migration', 'easy-invoice'), |
| 129 |
__('Migration', 'easy-invoice'), |
| 130 |
'manage_options', |
| 131 |
'easy-invoice-migration', |
| 132 |
[__CLASS__, 'render_migration_page_handler'] |
| 133 |
); |
| 134 |
} else { |
| 135 |
// Register page without parent menu when migration is not needed |
| 136 |
add_submenu_page( |
| 137 |
'null', // No parent menu |
| 138 |
__('Easy Invoice Migration', 'easy-invoice'), |
| 139 |
__('Migration', 'easy-invoice'), |
| 140 |
'manage_options', |
| 141 |
'easy-invoice-migration', |
| 142 |
[__CLASS__, 'render_migration_page_handler'] |
| 143 |
); |
| 144 |
} |
| 145 |
} |
| 146 |
|
| 147 |
/** |
| 148 |
* Hide migration menu if not needed. |
| 149 |
* |
| 150 |
* @since 2.0.0 |
| 151 |
* @return void |
| 152 |
*/ |
| 153 |
public static function hide_migration_menu() { |
| 154 |
$version_checker = VersionChecker::get_instance(); |
| 155 |
if (!$version_checker->is_migration_needed()) { |
| 156 |
//remove_submenu_page('easy-invoice', 'easy-invoice-migration'); |
| 157 |
} |
| 158 |
} |
| 159 |
|
| 160 |
/** |
| 161 |
* Render migration page. |
| 162 |
* |
| 163 |
* @since 2.0.0 |
| 164 |
* @return void |
| 165 |
*/ |
| 166 |
public static function render_migration_page_handler() { |
| 167 |
// Check if user has permissions |
| 168 |
if (!current_user_can('manage_options')) { |
| 169 |
wp_die(esc_html__('You do not have sufficient permissions to access this page.', 'easy-invoice')); |
| 170 |
} |
| 171 |
|
| 172 |
// Ensure page is set properly |
| 173 |
if (!isset($_GET['page'])) { |
| 174 |
$_GET['page'] = 'easy-invoice-migration'; |
| 175 |
} |
| 176 |
|
| 177 |
// Set a proper page title to avoid WordPress core issues |
| 178 |
add_filter('admin_title', function($title) { |
| 179 |
return __('Easy Invoice Migration', 'easy-invoice') . ' - ' . get_bloginfo('name'); |
| 180 |
}); |
| 181 |
|
| 182 |
// Include the main template with sidebar |
| 183 |
include EASY_INVOICE_PLUGIN_DIR . 'templates/main-template.php'; |
| 184 |
} |
| 185 |
|
| 186 |
/** |
| 187 |
* Handle migration redirect. |
| 188 |
* |
| 189 |
* @since 2.0.0 |
| 190 |
* @return void |
| 191 |
*/ |
| 192 |
public static function handle_migration_redirect() { |
| 193 |
if (isset($_GET['page']) && $_GET['page'] === 'easy-invoice-migration') { |
| 194 |
// Handle skip action |
| 195 |
if (isset($_GET['action']) && $_GET['action'] === 'skip' |
| 196 |
&& current_user_can('manage_options') |
| 197 |
&& wp_verify_nonce(sanitize_text_field(wp_unslash($_GET['_wpnonce'] ?? '')), 'easy_invoice_migration_skip')) { |
| 198 |
update_option('easy_invoice_migration_skipped', true); |
| 199 |
wp_safe_redirect(admin_url('admin.php?page=easy-invoice')); |
| 200 |
exit; |
| 201 |
} |
| 202 |
|
| 203 |
// Enqueue migration assets |
| 204 |
wp_enqueue_style( |
| 205 |
'easy-invoice-migration', |
| 206 |
EASY_INVOICE_PLUGIN_URL . 'includes/Migration/assets/migration.css', |
| 207 |
[], |
| 208 |
EASY_INVOICE_VERSION |
| 209 |
); |
| 210 |
|
| 211 |
wp_enqueue_script( |
| 212 |
'easy-invoice-migration', |
| 213 |
EASY_INVOICE_PLUGIN_URL . 'includes/Migration/assets/migration.js', |
| 214 |
['jquery'], |
| 215 |
EASY_INVOICE_VERSION, |
| 216 |
true |
| 217 |
); |
| 218 |
|
| 219 |
wp_localize_script('easy-invoice-migration', 'easyInvoiceMigration', [ |
| 220 |
'nonce' => wp_create_nonce('easy_invoice_migration_nonce'), |
| 221 |
'ajaxurl' => admin_url('admin-ajax.php'), |
| 222 |
]); |
| 223 |
} |
| 224 |
} |
| 225 |
} |
| 226 |
|