| 1 |
<?php |
| 2 |
/** |
| 3 |
* Post install migration for version 3.0.0 of the plugin. |
| 4 |
* |
| 5 |
* @package SeQura/WC |
| 6 |
* @subpackage SeQura/WC/Repositories |
| 7 |
*/ |
| 8 |
|
| 9 |
namespace SeQura\WC\Repositories\Migrations; |
| 10 |
|
| 11 |
use Exception; |
| 12 |
use SeQura\Core\BusinessLogic\AdminAPI\AdminAPI; |
| 13 |
use SeQura\Core\BusinessLogic\AdminAPI\Connection\Requests\OnboardingRequest; |
| 14 |
use SeQura\Core\BusinessLogic\AdminAPI\CountryConfiguration\Requests\CountryConfigurationRequest; |
| 15 |
use SeQura\WC\Core\Extension\BusinessLogic\AdminAPI\GeneralSettings\Requests\General_Settings_Request; |
| 16 |
use SeQura\WC\Core\Extension\BusinessLogic\AdminAPI\PromotionalWidgets\Requests\Widget_Settings_Request; |
| 17 |
use SeQura\WC\Core\Extension\BusinessLogic\Domain\PromotionalWidgets\Models\Widget_Location; |
| 18 |
use Throwable; |
| 19 |
|
| 20 |
/** |
| 21 |
* Post install migration for version 3.0.0 of the plugin. |
| 22 |
*/ |
| 23 |
class Migration_Install_300 extends Migration { |
| 24 |
|
| 25 |
/** |
| 26 |
* Get the plugin version when the changes were made. |
| 27 |
*/ |
| 28 |
public function get_version(): string { |
| 29 |
return '3.0.0'; |
| 30 |
} |
| 31 |
|
| 32 |
/** |
| 33 |
* Run the migration. |
| 34 |
* |
| 35 |
* @throws Throwable|Critical_Migration_Exception |
| 36 |
*/ |
| 37 |
public function run(): void { |
| 38 |
$this->add_new_tables_to_database(); |
| 39 |
$woocommerce_sequra_settings = (array) \get_option( 'woocommerce_sequra_settings', array() ); |
| 40 |
if ( ! empty( $woocommerce_sequra_settings ) ) { |
| 41 |
$this->migrate_connection_configuration( $woocommerce_sequra_settings ); |
| 42 |
$this->migrate_general_settings_configuration( $woocommerce_sequra_settings ); |
| 43 |
$this->migrate_country_configuration( $woocommerce_sequra_settings ); |
| 44 |
$this->migrate_widget_configuration( $woocommerce_sequra_settings ); |
| 45 |
} |
| 46 |
} |
| 47 |
|
| 48 |
/** |
| 49 |
* Check if the table exists in the database |
| 50 |
* |
| 51 |
* @throws Critical_Migration_Exception |
| 52 |
*/ |
| 53 |
private function check_if_table_exists( string $table_name ): void { |
| 54 |
if ( $this->db->get_var( "SHOW TABLES LIKE '{$table_name}'" ) !== $table_name ) { |
| 55 |
throw new Critical_Migration_Exception( \esc_html( "Could not create the table \"$table_name\"" ) ); |
| 56 |
} |
| 57 |
} |
| 58 |
|
| 59 |
/** |
| 60 |
* Add new tables to the database. |
| 61 |
* |
| 62 |
* @throws Throwable|Critical_Migration_Exception |
| 63 |
*/ |
| 64 |
private function add_new_tables_to_database(): void { |
| 65 |
$charset_collate = $this->db->get_charset_collate(); |
| 66 |
|
| 67 |
require_once ABSPATH . 'wp-admin/includes/upgrade.php'; |
| 68 |
|
| 69 |
foreach ( array( 'sequra_entity', 'sequra_order' ) as $table ) { |
| 70 |
$table_name = $this->db->prefix . $table; |
| 71 |
dbDelta( |
| 72 |
"CREATE TABLE $table_name ( |
| 73 |
`id` BIGINT UNSIGNED NOT NULL AUTO_INCREMENT, |
| 74 |
`type` VARCHAR(255), |
| 75 |
`index_1` VARCHAR(127), |
| 76 |
`index_2` VARCHAR(127), |
| 77 |
`index_3` VARCHAR(127), |
| 78 |
`index_4` VARCHAR(127), |
| 79 |
`index_5` VARCHAR(127), |
| 80 |
`index_6` VARCHAR(127), |
| 81 |
`index_7` VARCHAR(127), |
| 82 |
`data` LONGTEXT, |
| 83 |
PRIMARY KEY (id) |
| 84 |
) $charset_collate;" |
| 85 |
); |
| 86 |
$this->check_if_table_exists( $table_name ); |
| 87 |
} |
| 88 |
|
| 89 |
$table_name = $this->db->prefix . 'sequra_queue'; |
| 90 |
dbDelta( |
| 91 |
"CREATE TABLE $table_name ( |
| 92 |
`id` BIGINT UNSIGNED NOT NULL AUTO_INCREMENT, |
| 93 |
`type` VARCHAR(255), |
| 94 |
`index_1` VARCHAR(127), |
| 95 |
`index_2` VARCHAR(127), |
| 96 |
`index_3` VARCHAR(127), |
| 97 |
`index_4` VARCHAR(127), |
| 98 |
`index_5` VARCHAR(127), |
| 99 |
`index_6` BIGINT UNSIGNED, |
| 100 |
`index_7` BIGINT UNSIGNED, |
| 101 |
`index_8` BIGINT UNSIGNED, |
| 102 |
`index_9` BIGINT UNSIGNED, |
| 103 |
`data` LONGTEXT, |
| 104 |
PRIMARY KEY (id) |
| 105 |
) $charset_collate;" |
| 106 |
); |
| 107 |
$this->check_if_table_exists( $table_name ); |
| 108 |
} |
| 109 |
|
| 110 |
/** |
| 111 |
* Check if the entity exists in the database. |
| 112 |
*/ |
| 113 |
private function entity_exists( string $type ): bool { |
| 114 |
$table_name = $this->db->prefix . 'sequra_entity'; |
| 115 |
$query = $this->db->prepare( 'SELECT id FROM %i WHERE `type` = %s LIMIT 1', $table_name, $type ); |
| 116 |
$result = $this->db->get_results( $query ); |
| 117 |
return is_array( $result ) && count( $result ) > 0; |
| 118 |
} |
| 119 |
|
| 120 |
/** Migrate connection settings from v2 |
| 121 |
* |
| 122 |
* @param string[] $settings |
| 123 |
* @throws Throwable|Exception |
| 124 |
*/ |
| 125 |
private function migrate_connection_configuration( array $settings ): void { |
| 126 |
if ( $this->entity_exists( 'ConnectionData' ) ) { |
| 127 |
// Skip this migration if the data is already set. |
| 128 |
return; |
| 129 |
} |
| 130 |
|
| 131 |
$env_mapping = array( |
| 132 |
'0' => 'live', |
| 133 |
'1' => 'sandbox', |
| 134 |
); |
| 135 |
|
| 136 |
if ( ! isset( $settings['env'], $settings['user'], $settings['password'] ) |
| 137 |
|| ! array_key_exists( $settings['env'], $env_mapping ) ) { |
| 138 |
// Skip this migration if the data isn't set or valid. |
| 139 |
return; |
| 140 |
} |
| 141 |
|
| 142 |
$response = AdminAPI::get() |
| 143 |
->connection( $this->configuration->get_store_id() ) |
| 144 |
->saveOnboardingData( |
| 145 |
new OnboardingRequest( |
| 146 |
$env_mapping[ $settings['env'] ], |
| 147 |
strval( $settings['user'] ), |
| 148 |
strval( $settings['password'] ), |
| 149 |
true |
| 150 |
) |
| 151 |
); |
| 152 |
|
| 153 |
if ( ! $response->isSuccessful() ) { |
| 154 |
throw new Exception( 'Error migrating connection settings' ); |
| 155 |
} |
| 156 |
} |
| 157 |
|
| 158 |
/** |
| 159 |
* Get the store country code. |
| 160 |
*/ |
| 161 |
private function get_store_country(): string { |
| 162 |
$country = strval( get_option( 'woocommerce_default_country', 'ES' ) ); |
| 163 |
return strtoupper( explode( ':', $country )[0] ); |
| 164 |
} |
| 165 |
|
| 166 |
/** |
| 167 |
* Translate the widget style identifier to the new format if it is needed. |
| 168 |
* If no identifier is provided, the default widget style is returned. |
| 169 |
*/ |
| 170 |
private function get_widget_style( ?string $style = null ): string { |
| 171 |
if ( is_string( $style ) && json_decode( $style, true ) !== null ) { |
| 172 |
return $style; // Already in the new format. |
| 173 |
} |
| 174 |
|
| 175 |
$default_widget_style = '{"alignment":"center","amount-font-bold":"true","amount-font-color":"#1C1C1C","amount-font-size":"15","background-color":"white","border-color":"#B1AEBA","border-radius":"","class":"","font-color":"#1C1C1C","link-font-color":"#1C1C1C","link-underline":"true","no-costs-claim":"","size":"M","starting-text":"only","type":"banner"}'; |
| 176 |
|
| 177 |
$map = array( |
| 178 |
'L' => '{"alignment":"left"}', |
| 179 |
'R' => '{"alignment":"right"}', |
| 180 |
'legacy' => '{"type":"legacy"}', |
| 181 |
'legacyL' => '{"type":"legacy","alignment":"left"}', |
| 182 |
'legacyR' => '{"type":"legacy","alignment":"right"}', |
| 183 |
'minimal' => '{"type":"text","branding":"none","size":"S","starting-text":"as-low-as"}', |
| 184 |
'minimalL' => '{"type":"text","branding":"none","size":"S","starting-text":"as-low-as","alignment":"left"}', |
| 185 |
'minimalR' => '{"type":"text","branding":"none","size":"S","starting-text":"as-low-as","alignment":"right"}', |
| 186 |
); |
| 187 |
|
| 188 |
return null === $style || ! isset( $map[ $style ] ) ? $default_widget_style : $map[ $style ]; |
| 189 |
} |
| 190 |
|
| 191 |
/** Migrate country settings from v2 |
| 192 |
* |
| 193 |
* @param string[] $settings |
| 194 |
* @throws Throwable|Exception |
| 195 |
*/ |
| 196 |
private function migrate_country_configuration( array $settings ): void { |
| 197 |
if ( $this->entity_exists( 'CountryConfiguration' ) || ! isset( $settings['merchantref'] ) ) { |
| 198 |
return; |
| 199 |
} |
| 200 |
|
| 201 |
$response = AdminAPI::get() |
| 202 |
->countryConfiguration( $this->configuration->get_store_id() ) |
| 203 |
->saveCountryConfigurations( |
| 204 |
new CountryConfigurationRequest( |
| 205 |
array( |
| 206 |
array( |
| 207 |
'countryCode' => $this->get_store_country(), |
| 208 |
'merchantId' => strval( $settings['merchantref'] ), |
| 209 |
), |
| 210 |
) |
| 211 |
) |
| 212 |
); |
| 213 |
if ( ! $response->isSuccessful() ) { |
| 214 |
throw new Exception( 'Error migrating country settings' ); |
| 215 |
} |
| 216 |
} |
| 217 |
|
| 218 |
/** Migrate general settings from v2 |
| 219 |
* |
| 220 |
* @param string[] $settings |
| 221 |
* @throws Throwable|Exception |
| 222 |
*/ |
| 223 |
private function migrate_general_settings_configuration( array $settings ): void { |
| 224 |
|
| 225 |
if ( $this->entity_exists( 'GeneralSettings' ) ) { |
| 226 |
return; |
| 227 |
} |
| 228 |
|
| 229 |
$allowed_ip_addresses = array(); |
| 230 |
foreach ( explode( ',', strval( $settings['test_ips'] ?? '' ) ) as $ip ) { |
| 231 |
$ip = trim( $ip ); |
| 232 |
if ( ! empty( $ip ) ) { |
| 233 |
$allowed_ip_addresses[] = $ip; |
| 234 |
} |
| 235 |
} |
| 236 |
|
| 237 |
$response = AdminAPI::get() |
| 238 |
->generalSettings( $this->configuration->get_store_id() ) |
| 239 |
->saveGeneralSettings( |
| 240 |
new General_Settings_Request( |
| 241 |
true, |
| 242 |
false, |
| 243 |
$allowed_ip_addresses, |
| 244 |
null, |
| 245 |
null, |
| 246 |
strval( $settings['enable_for_virtual'] ?? 'no' ) === 'yes', |
| 247 |
strval( $settings['allow_payment_delay'] ?? 'no' ) === 'yes', |
| 248 |
strval( $settings['allow_registration_items'] ?? 'no' ) === 'yes', |
| 249 |
strval( $settings['default_service_end_date'] ?? 'P1Y' ) |
| 250 |
) |
| 251 |
); |
| 252 |
|
| 253 |
if ( ! $response->isSuccessful() ) { |
| 254 |
throw new Exception( 'Error general settings' ); |
| 255 |
} |
| 256 |
} |
| 257 |
|
| 258 |
/** Migrate widget settings from v2 |
| 259 |
* |
| 260 |
* @param string[] $settings |
| 261 |
* @throws Throwable|Exception |
| 262 |
*/ |
| 263 |
private function migrate_widget_configuration( array $settings ): void { |
| 264 |
|
| 265 |
if ( $this->entity_exists( 'WidgetSettings' ) ) { |
| 266 |
return; |
| 267 |
} |
| 268 |
|
| 269 |
$enabled = false; |
| 270 |
$default_sel_for_alt_price = '.woocommerce-variation-price .price>.amount,.woocommerce-variation-price .price ins .amount'; |
| 271 |
$default_sel_for_alt_price_trigger = '.variations'; |
| 272 |
$sel_for_default_location = '.summary>.price'; |
| 273 |
$country = $this->get_store_country(); |
| 274 |
$custom_locations = array(); |
| 275 |
foreach ( $settings as $key => $value ) { |
| 276 |
if ( false !== strpos( $key, 'enabled_in_product_' ) ) { |
| 277 |
$enabled_in_product = 'yes' === $value; |
| 278 |
$enabled = $enabled || $enabled_in_product; |
| 279 |
$product_campaign = str_replace( 'enabled_in_product_', '', $key ); |
| 280 |
$parts = explode( '_', $product_campaign, 2 ); |
| 281 |
|
| 282 |
$loc = new Widget_Location( |
| 283 |
$enabled_in_product, |
| 284 |
$settings[ "dest_css_sel_$product_campaign" ] ?? $sel_for_default_location, |
| 285 |
$this->get_widget_style( $settings[ "widget_theme_$product_campaign" ] ?? null ), |
| 286 |
$parts[0], |
| 287 |
$country, |
| 288 |
$parts[1] ?? null |
| 289 |
); |
| 290 |
$custom_locations[] = $loc->to_array(); |
| 291 |
} |
| 292 |
} |
| 293 |
|
| 294 |
$response = AdminAPI::get() |
| 295 |
->widgetConfiguration( $this->configuration->get_store_id() ) |
| 296 |
->setWidgetSettings( |
| 297 |
new Widget_Settings_Request( |
| 298 |
$enabled, |
| 299 |
$settings['assets_secret'] ?? '', |
| 300 |
$enabled, |
| 301 |
false, |
| 302 |
false, |
| 303 |
'', |
| 304 |
$this->get_widget_style(), |
| 305 |
array(), |
| 306 |
array(), |
| 307 |
$settings['price_css_sel'] ?? null, |
| 308 |
$default_sel_for_alt_price, |
| 309 |
$default_sel_for_alt_price_trigger, |
| 310 |
$sel_for_default_location, |
| 311 |
$custom_locations |
| 312 |
) |
| 313 |
); |
| 314 |
if ( ! $response->isSuccessful() ) { |
| 315 |
throw new Exception( 'Error migrating widget settings' ); |
| 316 |
} |
| 317 |
} |
| 318 |
} |
| 319 |
|