| 1 |
<?php |
| 2 |
|
| 3 |
namespace YayMail\Migrations\Versions; |
| 4 |
|
| 5 |
use Exception; |
| 6 |
use YayMail\Elements\ColumnLayout; |
| 7 |
use YayMail\Migrations\AbstractMigration; |
| 8 |
use YayMail\Utils\Helpers; |
| 9 |
use YayMail\Utils\SingletonTrait; |
| 10 |
use YayMail\Models\SettingModel; |
| 11 |
|
| 12 |
/** |
| 13 |
* Script to migrate from YayMail legacy (pre 4.0.0) to 4.0.0 |
| 14 |
*/ |
| 15 |
final class Ver_4_0_0 extends AbstractMigration { |
| 16 |
|
| 17 |
use SingletonTrait; |
| 18 |
|
| 19 |
private $current_template_id; |
| 20 |
private $element_types_map = []; |
| 21 |
|
| 22 |
private function __construct() { |
| 23 |
parent::__construct( '3.9.9', '4.0.0' ); |
| 24 |
} |
| 25 |
|
| 26 |
protected function up() { |
| 27 |
$this->migrate_templates(); |
| 28 |
$this->migrate_yaymail_settings(); |
| 29 |
} |
| 30 |
|
| 31 |
/** |
| 32 |
* Private functions |
| 33 |
*/ |
| 34 |
private function migrate_templates() { |
| 35 |
$this->logger->log( 'Start migrating templates' ); |
| 36 |
global $wpdb; |
| 37 |
|
| 38 |
// Make sure the backup existed |
| 39 |
if ( empty( $this->backup_option_name ) || empty( get_option( $this->backup_option_name ) ) ) { |
| 40 |
throw new Exception( 'Could not find backup option' ); |
| 41 |
} |
| 42 |
|
| 43 |
$template_posts_query = " |
| 44 |
SELECT * |
| 45 |
FROM {$wpdb->posts} |
| 46 |
WHERE post_type = 'yaymail_template' |
| 47 |
"; |
| 48 |
$template_posts = $wpdb->get_results( $template_posts_query ); // phpcs:ignore |
| 49 |
if ( empty( $template_posts ) ) { |
| 50 |
$this->logger->log( 'There is no template to be migrated' ); |
| 51 |
return; |
| 52 |
} |
| 53 |
|
| 54 |
foreach ( $template_posts as $template ) { |
| 55 |
if ( empty( $template->ID ) ) { |
| 56 |
continue; |
| 57 |
} |
| 58 |
/** |
| 59 |
* ========================== |
| 60 |
* Start Elements migrations |
| 61 |
*/ |
| 62 |
$this->current_template_id = $template->ID; |
| 63 |
|
| 64 |
$elements = get_post_meta( $this->current_template_id, \YayMail\YayMailTemplate::META_KEYS['elements'], true ); |
| 65 |
|
| 66 |
if ( empty( $elements ) ) { |
| 67 |
continue; |
| 68 |
} |
| 69 |
|
| 70 |
$elements = array_map( [ $this, 'convert_element' ], $elements ); |
| 71 |
|
| 72 |
update_post_meta( $this->current_template_id, \YayMail\YayMailTemplate::META_KEYS['elements'], $elements ); |
| 73 |
/** |
| 74 |
* ========================== |
| 75 |
*/ |
| 76 |
|
| 77 |
/** |
| 78 |
* ========================== |
| 79 |
* Start Template settings migrations |
| 80 |
*/ |
| 81 |
// Template status |
| 82 |
$old_activation_status = get_post_meta( $this->current_template_id, \YayMail\YayMailTemplate::META_KEYS['status'], true ); |
| 83 |
$new_activation_status = in_array( $old_activation_status, [ 'active', 'inactive' ], true ) ? $old_activation_status : ( '1' === $old_activation_status ? 'active' : 'inactive' ); |
| 84 |
update_post_meta( $this->current_template_id, \YayMail\YayMailTemplate::META_KEYS['status'], $new_activation_status ); |
| 85 |
|
| 86 |
// Template outer background color |
| 87 |
$old_outer_bg_color = get_post_meta( $this->current_template_id, '_email_backgroundColor_settings', true ); |
| 88 |
update_post_meta( $this->current_template_id, \YayMail\YayMailTemplate::META_KEYS['background_color'], $old_outer_bg_color ); |
| 89 |
|
| 90 |
$this->may_mark_template_as_v4_supported( $this->current_template_id ); |
| 91 |
|
| 92 |
/** |
| 93 |
* Finish Template settings migrations |
| 94 |
* ========================== |
| 95 |
*/ |
| 96 |
|
| 97 |
}//end foreach |
| 98 |
$this->logger->log( 'Done migrating templates' ); |
| 99 |
} |
| 100 |
|
| 101 |
private function migrate_yaymail_settings() { |
| 102 |
$this->logger->log( 'Start migrating YayMail settings' ); |
| 103 |
$yaymail_settings = yaymail_settings(); |
| 104 |
|
| 105 |
/** |
| 106 |
* ============================== |
| 107 |
* Start mapping attribute names |
| 108 |
*/ |
| 109 |
$attributes_map = [ |
| 110 |
'payment' => SettingModel::META_KEYS['payment_display_mode'], |
| 111 |
'product_image' => SettingModel::META_KEYS['show_product_image'], |
| 112 |
'image_position' => SettingModel::META_KEYS['product_image_position'], |
| 113 |
'image_width' => SettingModel::META_KEYS['product_image_width'], |
| 114 |
'image_height' => SettingModel::META_KEYS['product_image_height'], |
| 115 |
'product_sku' => SettingModel::META_KEYS['show_product_sku'], |
| 116 |
'product_des' => SettingModel::META_KEYS['show_product_description'], |
| 117 |
'product_regular_price' => SettingModel::META_KEYS['show_product_regular_price'], |
| 118 |
'product_hyper_links' => SettingModel::META_KEYS['show_product_hyper_links'], |
| 119 |
'product_item_cost' => SettingModel::META_KEYS['show_product_item_cost'], |
| 120 |
'direction_rtl' => SettingModel::META_KEYS['direction'], |
| 121 |
'enable_css_custom' => SettingModel::META_KEYS['enable_custom_css'], |
| 122 |
]; |
| 123 |
// Convert element data names |
| 124 |
foreach ( $attributes_map as $old_key => $new_key ) { |
| 125 |
$value = Helpers::get_object_value( $yaymail_settings, $old_key ); |
| 126 |
|
| 127 |
if ( ! isset( $value ) ) { |
| 128 |
continue; |
| 129 |
} |
| 130 |
|
| 131 |
if ( in_array( $value, [ '0', 'no' ], true ) ) { |
| 132 |
$value = false; |
| 133 |
} |
| 134 |
|
| 135 |
if ( in_array( $value, [ '1', 'yes' ], true ) ) { |
| 136 |
$value = true; |
| 137 |
} |
| 138 |
|
| 139 |
Helpers::set_object_value( $yaymail_settings, $new_key, $value ); |
| 140 |
}//end foreach |
| 141 |
|
| 142 |
/** |
| 143 |
* End mapping attribute names |
| 144 |
* ============================ |
| 145 |
*/ |
| 146 |
|
| 147 |
/** |
| 148 |
* ============================== |
| 149 |
* Start mapping settings values |
| 150 |
*/ |
| 151 |
$payment_display_mode_map = [ |
| 152 |
'0' => 'no', |
| 153 |
'1' => 'yes', |
| 154 |
'2' => 'customer', |
| 155 |
]; |
| 156 |
$yaymail_settings[ SettingModel::META_KEYS['payment_display_mode'] ] = $payment_display_mode_map[ $yaymail_settings[ SettingModel::META_KEYS['payment_display_mode'] ] ] ?? 'yes'; |
| 157 |
|
| 158 |
$yaymail_settings[ SettingModel::META_KEYS['product_image_position'] ] = strtolower( $yaymail_settings[ SettingModel::META_KEYS['product_image_position'] ] ?? 'top' ); |
| 159 |
$yaymail_settings[ SettingModel::META_KEYS['product_image_width'] ] = (int) str_replace( 'px', '', $yaymail_settings[ SettingModel::META_KEYS['product_image_width'] ] ?? '30' ); |
| 160 |
$yaymail_settings[ SettingModel::META_KEYS['product_image_height'] ] = (int) str_replace( 'px', '', $yaymail_settings[ SettingModel::META_KEYS['product_image_height'] ] ?? '30' ); |
| 161 |
$yaymail_settings[ SettingModel::META_KEYS['container_width'] ] = (int) str_replace( 'px', '', $yaymail_settings[ SettingModel::META_KEYS['container_width'] ] ?? '605' ); |
| 162 |
if ( empty( $yaymail_settings[ SettingModel::META_KEYS['container_width'] ] ) ) { |
| 163 |
$yaymail_settings[ SettingModel::META_KEYS['container_width'] ] = 605; |
| 164 |
} |
| 165 |
/** |
| 166 |
* Finish mapping settings values |
| 167 |
* ============================== |
| 168 |
*/ |
| 169 |
|
| 170 |
// Update yaymail settings to db |
| 171 |
$update_yaymail_setting_options_result = update_option( SettingModel::OPTION_NAME, $yaymail_settings ); |
| 172 |
if ( ! $update_yaymail_setting_options_result ) { |
| 173 |
$this->logger->log( 'Failed to update new YayMail settings to db' ); |
| 174 |
} |
| 175 |
$this->logger->log( 'Done migrating YayMail settings' ); |
| 176 |
} |
| 177 |
|
| 178 |
private function convert_element( $element ) { |
| 179 |
$attributes_map = [ |
| 180 |
'nameElement' => 'name', |
| 181 |
'settingRow' => 'data', |
| 182 |
]; |
| 183 |
|
| 184 |
foreach ( $attributes_map as $old_key => $new_key ) { |
| 185 |
if ( isset( $element[ $old_key ] ) ) { |
| 186 |
$element[ $new_key ] = $element[ $old_key ]; |
| 187 |
} |
| 188 |
} |
| 189 |
|
| 190 |
$this->convert_element_type( $element ); |
| 191 |
|
| 192 |
$this->convert_element_data( $element ); |
| 193 |
|
| 194 |
$element['available'] = true; |
| 195 |
|
| 196 |
return $element; |
| 197 |
} |
| 198 |
|
| 199 |
private function convert_element_type( &$element ) { |
| 200 |
$attributes_map = |
| 201 |
[ |
| 202 |
'Logo' => 'logo', |
| 203 |
'ElementText' => 'text', |
| 204 |
'Images' => 'image', |
| 205 |
'Button' => 'button', |
| 206 |
'Title' => 'title', |
| 207 |
'SocialIcon' => 'social_icon', |
| 208 |
'Video' => 'video', |
| 209 |
'ImageList' => 'image_list', |
| 210 |
'ImageBox' => 'image_box', |
| 211 |
'TextList' => 'text_list', |
| 212 |
'HTMLCode' => 'html', |
| 213 |
// Note: Old version uses ElementText for Heading and Footer |
| 214 |
|
| 215 |
'Space' => 'space', |
| 216 |
'Divider' => 'divider', |
| 217 |
'OneColumn' => 'column_layout', |
| 218 |
'TwoColumns' => 'column_layout', |
| 219 |
'ThreeColumns' => 'column_layout', |
| 220 |
'FourColumns' => 'column_layout', |
| 221 |
|
| 222 |
'ShippingAddress' => 'shipping_address', |
| 223 |
'BillingAddress' => 'billing_address', |
| 224 |
'OrderItem' => 'order_details', |
| 225 |
'Hook' => 'hook', |
| 226 |
'OrderItemDownload' => 'order_details_download', |
| 227 |
|
| 228 |
]; |
| 229 |
$type = $element['type']; |
| 230 |
|
| 231 |
if ( $type === 'BillingAddress' ) { |
| 232 |
// Note: Old version uses BillingAddress for BillingShippingAddress |
| 233 |
if ( isset( $element['settingRow']['nameColumn'] ) && $element['settingRow']['nameColumn'] === 'BillingShippingAddress' ) { |
| 234 |
$element['type'] = 'billing_shipping_address'; |
| 235 |
return; |
| 236 |
} |
| 237 |
} |
| 238 |
|
| 239 |
if ( empty( $attributes_map[ $type ] ) ) { |
| 240 |
$this->logger->log( 'Element type was not handled: ' . $type ); |
| 241 |
} |
| 242 |
|
| 243 |
if ( isset( $attributes_map[ $type ] ) ) { |
| 244 |
$element['type'] = $attributes_map[ $type ]; |
| 245 |
} |
| 246 |
} |
| 247 |
|
| 248 |
private function convert_element_data( &$element ) { |
| 249 |
$data =& $element['data']; |
| 250 |
if ( empty( $data ) ) { |
| 251 |
return; |
| 252 |
} |
| 253 |
|
| 254 |
$this->convert_element_data_attribute_names( $data, $element['type'] ); |
| 255 |
$this->convert_element_data_attribute_values( $element ); |
| 256 |
|
| 257 |
$this->add_new_attributes_to_element_data( $element ); |
| 258 |
|
| 259 |
$this->migrate_shortcodes( $data, $element['type'] ); |
| 260 |
} |
| 261 |
|
| 262 |
private function convert_element_data_attribute_names( &$data, $element_type ) { |
| 263 |
$attributes_map = $this->get_element_data_attributes_map( $element_type ); |
| 264 |
|
| 265 |
// Convert element data names |
| 266 |
foreach ( $attributes_map as $old_key => $new_key ) { |
| 267 |
$value = Helpers::get_object_value( $data, $old_key ); |
| 268 |
|
| 269 |
if ( ! isset( $value ) ) { |
| 270 |
continue; |
| 271 |
} |
| 272 |
|
| 273 |
Helpers::set_object_value( $data, $new_key, $value ); |
| 274 |
} |
| 275 |
} |
| 276 |
|
| 277 |
private function get_element_data_attributes_map( $element_type ) { |
| 278 |
$common_map = [ |
| 279 |
'backgroundColor' => 'background_color', |
| 280 |
'borderColor' => 'border_color', |
| 281 |
'borderRadiusBottom' => [ 'border_radius', 'bottom_right' ], |
| 282 |
'borderRadiusLeft' => [ 'border_radius', 'bottom_left' ], |
| 283 |
'borderRadiusRight' => [ 'border_radius', 'top_right' ], |
| 284 |
'borderRadiusTop' => [ 'border_radius', 'top_left' ], |
| 285 |
'content' => 'rich_text', |
| 286 |
'contentTitle' => 'title', |
| 287 |
'titleColor' => 'title_color', |
| 288 |
'family' => 'font_family', |
| 289 |
'paddingBottom' => [ 'padding', 'bottom' ], |
| 290 |
'paddingLeft' => [ 'padding', 'left' ], |
| 291 |
'paddingRight' => [ 'padding', 'right' ], |
| 292 |
'paddingTop' => [ 'padding', 'top' ], |
| 293 |
'pathImg' => 'src', |
| 294 |
'textColor' => 'text_color', |
| 295 |
'fontSize' => 'font_size', |
| 296 |
]; |
| 297 |
|
| 298 |
$element_specific_map = []; |
| 299 |
switch ( $element_type ) { |
| 300 |
case 'button': |
| 301 |
$element_specific_map = [ |
| 302 |
'buttonBackgroundColor' => 'button_background_color', |
| 303 |
'buttonType' => 'button_type', |
| 304 |
'styleTheme' => 'theme', |
| 305 |
'heightButton' => 'height', |
| 306 |
'widthButton' => 'width', |
| 307 |
'pathUrl' => 'url', |
| 308 |
]; |
| 309 |
break; |
| 310 |
case 'video': |
| 311 |
$element_specific_map = [ |
| 312 |
'videoUrl' => 'url', |
| 313 |
]; |
| 314 |
break; |
| 315 |
case 'title': |
| 316 |
$element_specific_map = [ |
| 317 |
'sizeSubTitle' => 'subtitle_size', |
| 318 |
'sizeTitle' => 'title_size', |
| 319 |
'subTitle' => 'subtitle', |
| 320 |
'titleBilling' => 'billing_title', |
| 321 |
'titleColor' => 'title_color', |
| 322 |
'titleShipping' => 'shipping_title', |
| 323 |
]; |
| 324 |
break; |
| 325 |
case 'social_icon': |
| 326 |
$element_specific_map = [ |
| 327 |
'styleTheme' => 'theme', |
| 328 |
'iconSocialsArr' => 'icon_list', |
| 329 |
'iconSpacing' => 'spacing', |
| 330 |
'widthSocialIcon' => 'width_icon', |
| 331 |
]; |
| 332 |
break; |
| 333 |
case 'image_list': |
| 334 |
$element_specific_map = [ |
| 335 |
'col1Align' => [ 'image_list', 'column_1', 'align', 'value' ], |
| 336 |
'col1PaddingBottom' => [ 'image_list', 'column_1', 'padding', 'value', 'bottom' ], |
| 337 |
'col1PaddingLeft' => [ 'image_list', 'column_1', 'padding', 'value', 'left' ], |
| 338 |
'col1PaddingRight' => [ 'image_list', 'column_1', 'padding', 'value', 'right' ], |
| 339 |
'col1PaddingTop' => [ 'image_list', 'column_1', 'padding', 'value', 'top' ], |
| 340 |
'col1PathImg' => [ 'image_list', 'column_1', 'image', 'value' ], |
| 341 |
'col1Url' => [ 'image_list', 'column_1', 'url', 'value' ], |
| 342 |
'col1Width' => [ 'image_list', 'column_1', 'width', 'value' ], |
| 343 |
'col2Align' => [ 'image_list', 'column_2', 'align', 'value' ], |
| 344 |
'col2PaddingBottom' => [ 'image_list', 'column_2', 'padding', 'value', 'bottom' ], |
| 345 |
'col2PaddingLeft' => [ 'image_list', 'column_2', 'padding', 'value', 'left' ], |
| 346 |
'col2PaddingRight' => [ 'image_list', 'column_2', 'padding', 'value', 'right' ], |
| 347 |
'col2PaddingTop' => [ 'image_list', 'column_2', 'padding', 'value', 'top' ], |
| 348 |
'col2PathImg' => [ 'image_list', 'column_2', 'image', 'value' ], |
| 349 |
'col2Url' => [ 'image_list', 'column_2', 'url', 'value' ], |
| 350 |
'col2Width' => [ 'image_list', 'column_2', 'width', 'value' ], |
| 351 |
'col3Align' => [ 'image_list', 'column_3', 'align', 'value' ], |
| 352 |
'col3PaddingBottom' => [ 'image_list', 'column_3', 'padding', 'value', 'bottom' ], |
| 353 |
'col3PaddingLeft' => [ 'image_list', 'column_3', 'padding', 'value', 'left' ], |
| 354 |
'col3PaddingRight' => [ 'image_list', 'column_3', 'padding', 'value', 'right' ], |
| 355 |
'col3PaddingTop' => [ 'image_list', 'column_3', 'padding', 'value', 'top' ], |
| 356 |
'col3PathImg' => [ 'image_list', 'column_3', 'image', 'value' ], |
| 357 |
'col3Url' => [ 'image_list', 'column_3', 'url', 'value' ], |
| 358 |
'col3Width' => [ 'image_list', 'column_3', 'width', 'value' ], |
| 359 |
'numberCol' => 'number_column', |
| 360 |
]; |
| 361 |
break; |
| 362 |
case 'image_box': |
| 363 |
$element_specific_map = [ |
| 364 |
'col1Align' => [ 'image_box', 'column_1', 'align', 'value' ], |
| 365 |
'col1PaddingBottom' => [ 'image_box', 'column_1', 'padding', 'value', 'bottom' ], |
| 366 |
'col1PaddingLeft' => [ 'image_box', 'column_1', 'padding', 'value', 'left' ], |
| 367 |
'col1PaddingRight' => [ 'image_box', 'column_1', 'padding', 'value', 'right' ], |
| 368 |
'col1PaddingTop' => [ 'image_box', 'column_1', 'padding', 'value', 'top' ], |
| 369 |
'col1PathImg' => [ 'image_box', 'column_1', 'image', 'value' ], |
| 370 |
'col1Url' => [ 'image_box', 'column_1', 'url', 'value' ], |
| 371 |
'col1Width' => [ 'image_box', 'column_1', 'width', 'value' ], |
| 372 |
'col2Content' => [ 'image_box', 'column_2', 'rich_text', 'value' ], |
| 373 |
'col2Family' => [ 'image_box', 'column_2', 'font_family', 'value' ], |
| 374 |
'col2PaddingBottom' => [ 'image_box', 'column_2', 'padding', 'value', 'bottom' ], |
| 375 |
'col2PaddingLeft' => [ 'image_box', 'column_2', 'padding', 'value', 'left' ], |
| 376 |
'col2PaddingRight' => [ 'image_box', 'column_2', 'padding', 'value', 'right' ], |
| 377 |
'col2PaddingTop' => [ 'image_box', 'column_2', 'padding', 'value', 'top' ], |
| 378 |
'numberCol' => 'number_column', |
| 379 |
|
| 380 |
]; |
| 381 |
break; |
| 382 |
case 'text_list': |
| 383 |
$element_specific_map = [ |
| 384 |
'numberCol' => 'number_column', |
| 385 |
'buttonCol1' => [ 'text_list', 'column_1', 'show_button', 'value' ], |
| 386 |
'buttonCol2' => [ 'text_list', 'column_2', 'show_button', 'value' ], |
| 387 |
'buttonCol3' => [ 'text_list', 'column_3', 'show_button', 'value' ], |
| 388 |
|
| 389 |
'col1BtAlign' => [ 'text_list', 'column_1', 'button_align', 'value' ], |
| 390 |
'col1BtBgColor' => [ 'text_list', 'column_1', 'button_background_color', 'value' ], |
| 391 |
'col1BtButtonType' => [ 'text_list', 'column_1', 'button_type', 'value' ], |
| 392 |
'col1BtFamily' => [ 'text_list', 'column_1', 'button_font_family', 'value' ], |
| 393 |
'col1BtFontSize' => [ 'text_list', 'column_1', 'button_font_size', 'value' ], |
| 394 |
'col1BtPaddingBottom' => [ 'text_list', 'column_1', 'button_padding', 'value', 'bottom' ], |
| 395 |
'col1BtPaddingLeft' => [ 'text_list', 'column_1', 'button_padding', 'value', 'left' ], |
| 396 |
'col1BtPaddingRight' => [ 'text_list', 'column_1', 'button_padding', 'value', 'right' ], |
| 397 |
'col1BtPaddingTop' => [ 'text_list', 'column_1', 'button_padding', 'value', 'top' ], |
| 398 |
'col1BtPathUrl' => [ 'text_list', 'column_1', 'button_url', 'value' ], |
| 399 |
'col1BtText' => [ 'text_list', 'column_1', 'button_text', 'value' ], |
| 400 |
'col1BtTextColor' => [ 'text_list', 'column_1', 'button_text_color', 'value' ], |
| 401 |
'col1BtWeight' => [ 'text_list', 'column_1', 'button_weight', 'value' ], |
| 402 |
'col1BtWidthButton' => [ 'text_list', 'column_1', 'button_width', 'value' ], |
| 403 |
'col1TtContent' => [ 'text_list', 'column_1', 'rich_text', 'value' ], |
| 404 |
'col1TtFamily' => [ 'text_list', 'column_1', 'font_family', 'value' ], |
| 405 |
'col1TtPaddingBottom' => [ 'text_list', 'column_1', 'padding', 'value', 'bottom' ], |
| 406 |
'col1TtPaddingLeft' => [ 'text_list', 'column_1', 'padding', 'value', 'left' ], |
| 407 |
'col1TtPaddingRight' => [ 'text_list', 'column_1', 'padding', 'value', 'right' ], |
| 408 |
'col1TtPaddingTop' => [ 'text_list', 'column_1', 'padding', 'value', 'top' ], |
| 409 |
|
| 410 |
'col2BtAlign' => [ 'text_list', 'column_2', 'button_align', 'value' ], |
| 411 |
'col2BtBgColor' => [ 'text_list', 'column_2', 'button_background_color', 'value' ], |
| 412 |
'col2BtButtonType' => [ 'text_list', 'column_2', 'button_type', 'value' ], |
| 413 |
'col2BtFamily' => [ 'text_list', 'column_2', 'button_font_family', 'value' ], |
| 414 |
'col2BtFontSize' => [ 'text_list', 'column_2', 'button_font_size', 'value' ], |
| 415 |
'col2BtPaddingBottom' => [ 'text_list', 'column_2', 'button_padding', 'value', 'bottom' ], |
| 416 |
'col2BtPaddingLeft' => [ 'text_list', 'column_2', 'button_padding', 'value', 'left' ], |
| 417 |
'col2BtPaddingRight' => [ 'text_list', 'column_2', 'button_padding', 'value', 'right' ], |
| 418 |
'col2BtPaddingTop' => [ 'text_list', 'column_2', 'button_padding', 'value', 'top' ], |
| 419 |
'col2BtPathUrl' => [ 'text_list', 'column_2', 'button_url', 'value' ], |
| 420 |
'col2BtText' => [ 'text_list', 'column_2', 'button_text', 'value' ], |
| 421 |
'col2BtTextColor' => [ 'text_list', 'column_2', 'button_text_color', 'value' ], |
| 422 |
'col2BtWeight' => [ 'text_list', 'column_2', 'button_weight', 'value' ], |
| 423 |
'col2BtWidthButton' => [ 'text_list', 'column_2', 'button_width', 'value' ], |
| 424 |
'col2TtContent' => [ 'text_list', 'column_2', 'rich_text', 'value' ], |
| 425 |
'col2TtFamily' => [ 'text_list', 'column_2', 'font_family', 'value' ], |
| 426 |
'col2TtPaddingBottom' => [ 'text_list', 'column_2', 'padding', 'value', 'bottom' ], |
| 427 |
'col2TtPaddingLeft' => [ 'text_list', 'column_2', 'padding', 'value', 'left' ], |
| 428 |
'col2TtPaddingRight' => [ 'text_list', 'column_2', 'padding', 'value', 'right' ], |
| 429 |
'col2TtPaddingTop' => [ 'text_list', 'column_2', 'padding', 'value', 'top' ], |
| 430 |
|
| 431 |
'col3BtAlign' => [ 'text_list', 'column_3', 'button_align', 'value' ], |
| 432 |
'col3BtBgColor' => [ 'text_list', 'column_3', 'button_background_color', 'value' ], |
| 433 |
'col3BtButtonType' => [ 'text_list', 'column_3', 'button_type', 'value' ], |
| 434 |
'col3BtFamily' => [ 'text_list', 'column_3', 'button_font_family', 'value' ], |
| 435 |
'col3BtFontSize' => [ 'text_list', 'column_3', 'button_font_size', 'value' ], |
| 436 |
'col3BtPaddingBottom' => [ 'text_list', 'column_3', 'button_padding', 'value', 'bottom' ], |
| 437 |
'col3BtPaddingLeft' => [ 'text_list', 'column_3', 'button_padding', 'value', 'left' ], |
| 438 |
'col3BtPaddingRight' => [ 'text_list', 'column_3', 'button_padding', 'value', 'right' ], |
| 439 |
'col3BtPaddingTop' => [ 'text_list', 'column_3', 'button_padding', 'value', 'top' ], |
| 440 |
'col3BtPathUrl' => [ 'text_list', 'column_3', 'button_url', 'value' ], |
| 441 |
'col3BtText' => [ 'text_list', 'column_3', 'button_text', 'value' ], |
| 442 |
'col3BtTextColor' => [ 'text_list', 'column_3', 'button_text_color', 'value' ], |
| 443 |
'col3BtWeight' => [ 'text_list', 'column_3', 'button_weight', 'value' ], |
| 444 |
'col3BtWidthButton' => [ 'text_list', 'column_3', 'button_width', 'value' ], |
| 445 |
'col3TtContent' => [ 'text_list', 'column_3', 'rich_text', 'value' ], |
| 446 |
'col3TtFamily' => [ 'text_list', 'column_3', 'font_family', 'value' ], |
| 447 |
'col3TtPaddingBottom' => [ 'text_list', 'column_3', 'padding', 'value', 'bottom' ], |
| 448 |
'col3TtPaddingLeft' => [ 'text_list', 'column_3', 'padding', 'value', 'left' ], |
| 449 |
'col3TtPaddingRight' => [ 'text_list', 'column_3', 'padding', 'value', 'right' ], |
| 450 |
'col3TtPaddingTop' => [ 'text_list', 'column_3', 'padding', 'value', 'top' ], |
| 451 |
]; |
| 452 |
break; |
| 453 |
case 'html': |
| 454 |
$element_specific_map = [ |
| 455 |
'HTMLContent' => 'rich_text', |
| 456 |
]; |
| 457 |
break; |
| 458 |
case 'divider': |
| 459 |
$element_specific_map = [ |
| 460 |
'dividerColor' => 'divider_color', |
| 461 |
'dividerStyle' => 'divider_type', |
| 462 |
]; |
| 463 |
break; |
| 464 |
case 'billing_address': |
| 465 |
$element_specific_map = [ |
| 466 |
'titleBilling' => 'title', |
| 467 |
]; |
| 468 |
break; |
| 469 |
case 'column_layout': |
| 470 |
$element_specific_map = [ |
| 471 |
'backgroundImage' => [ 'background_image', 'url' ], |
| 472 |
'backgroundRepeat' => [ 'background_image', 'repeat' ], |
| 473 |
'backgroundSize' => [ 'background_image', 'size' ], |
| 474 |
]; |
| 475 |
break; |
| 476 |
case 'hook': |
| 477 |
$element_specific_map = [ |
| 478 |
'content' => 'hook_shortcode', |
| 479 |
]; |
| 480 |
break; |
| 481 |
|
| 482 |
default: |
| 483 |
break; |
| 484 |
}//end switch |
| 485 |
|
| 486 |
return array_merge( $common_map, $element_specific_map ); |
| 487 |
} |
| 488 |
|
| 489 |
private function convert_element_data_attribute_values( &$element ) { |
| 490 |
// Map data for logo |
| 491 |
if ( $element['type'] === 'logo' ) { |
| 492 |
// $element['data']['src'] = str_replace( 'assets/dist/images', 'assets/images', $element['data']['src'] ); |
| 493 |
if ( empty( $element['data']['src'] ) ) { |
| 494 |
$element['data']['src'] = YAYMAIL_PLUGIN_URL . 'assets/images/woocommerce-logo.png'; |
| 495 |
|
| 496 |
} |
| 497 |
} |
| 498 |
// End logo logic |
| 499 |
|
| 500 |
// Map data for social_icon |
| 501 |
if ( $element['type'] === 'social_icon' ) { |
| 502 |
if ( ! empty( $element['data']['icon_list'] ) ) { |
| 503 |
$icon_list =& $element['data']['icon_list']; |
| 504 |
|
| 505 |
$icon_list = array_map( |
| 506 |
function ( $item ) { |
| 507 |
return [ |
| 508 |
'icon' => strtolower( $item['icon'] ?? 'facebook' ), |
| 509 |
'url' => $item['pathLink'] ?? '#', |
| 510 |
]; |
| 511 |
}, |
| 512 |
$icon_list |
| 513 |
); |
| 514 |
} |
| 515 |
return; |
| 516 |
} |
| 517 |
// End social_icon logic |
| 518 |
|
| 519 |
if ( $element['type'] === 'button' ) { |
| 520 |
// Increase button's width |
| 521 |
$data['width'] = min( $element['data']['width'] + 20, 100 ); |
| 522 |
} |
| 523 |
|
| 524 |
if ( $element['type'] === 'video' ) { |
| 525 |
// Old core uses percent, new core uses px |
| 526 |
$data['width'] = (int) $element['data']['width'] * 600 / 100; |
| 527 |
} |
| 528 |
|
| 529 |
$number_column_value_map = [ |
| 530 |
'one' => 1, |
| 531 |
'two' => 2, |
| 532 |
'three' => 3, |
| 533 |
]; |
| 534 |
|
| 535 |
// Map data for image_list and image_box |
| 536 |
if ( $element['type'] === 'image_list' ) { |
| 537 |
|
| 538 |
if ( ! empty( $element['data']['number_column'] ) ) { |
| 539 |
$element['data']['number_column'] = $number_column_value_map[ $element['data']['number_column'] ]; |
| 540 |
return; |
| 541 |
} |
| 542 |
return; |
| 543 |
} |
| 544 |
// End image_list and image_box logic |
| 545 |
|
| 546 |
// Map data for text_list |
| 547 |
if ( $element['type'] === 'text_list' ) { |
| 548 |
if ( ! empty( $element['data']['number_column'] ) ) { |
| 549 |
$element['data']['number_column'] = $number_column_value_map[ $element['data']['number_column'] ]; |
| 550 |
} |
| 551 |
|
| 552 |
foreach ( [ 'column_1', 'column_2', 'column_3' ] as $column ) { |
| 553 |
if ( isset( $element['data']['text_list'][ $column ]['show_button'] ) ) { |
| 554 |
$element['data']['text_list'][ $column ]['show_button']['value'] = ( $element['data']['text_list'][ $column ]['show_button']['value'] === 'show' ); |
| 555 |
} |
| 556 |
} |
| 557 |
return; |
| 558 |
} |
| 559 |
// End text_list logic |
| 560 |
} |
| 561 |
|
| 562 |
private function add_new_attributes_to_element_data( &$element ) { |
| 563 |
$data =& $element['data']; |
| 564 |
|
| 565 |
// For element Order Details |
| 566 |
if ( $element['type'] === 'order_details' ) { |
| 567 |
// Add these fields to order_details element |
| 568 |
$data['payment_instructions'] = '[yaymail_payment_instructions]'; |
| 569 |
|
| 570 |
// Headers of the table columns/rows |
| 571 |
$order_details_titles = get_post_meta( $this->current_template_id, '_yaymail_email_order_item_title', true ); |
| 572 |
$data['title'] = $order_details_titles['order_title'] ?? '<span style="font-size: 20px;">Order #[yaymail_order_number] <b>([yaymail_order_date])</b></span>'; |
| 573 |
$data['product_title'] = $order_details_titles['product_title'] ?? __( 'Product', 'yaymail' ); |
| 574 |
$data['cost_title'] = $order_details_titles['cost_title'] ?? __( 'Cost', 'yaymail' ); |
| 575 |
$data['quantity_title'] = $order_details_titles['quantity_title'] ?? __( 'Quantity', 'yaymail' ); |
| 576 |
$data['price_title'] = $order_details_titles['price_title'] ?? __( 'Price', 'yaymail' ); |
| 577 |
$data['cart_subtotal_title'] = $order_details_titles['subtoltal_title'] ?? __( 'Subtotal:', 'yaymail' ); |
| 578 |
$data['payment_method_title'] = $order_details_titles['payment_method_title'] ?? __( 'Payment method:', 'yaymail' ); |
| 579 |
$data['order_total_title'] = $order_details_titles['total_title'] ?? __( 'Total:', 'yaymail' ); |
| 580 |
$data['order_note_title'] = $order_details_titles['customer_note'] ?? __( 'Note:', 'yaymail' ); |
| 581 |
$data['shipping_title'] = $order_details_titles['shipping_title'] ?? __( 'Shipping:', 'yaymail' ); |
| 582 |
$data['discount_title'] = $order_details_titles['discount_title'] ?? __( 'Discount:', 'yaymail' ); |
| 583 |
return; |
| 584 |
} |
| 585 |
|
| 586 |
if ( $element['type'] === 'billing_address' ) { |
| 587 |
$data['rich_text'] = '[yaymail_billing_address]'; |
| 588 |
return; |
| 589 |
} |
| 590 |
|
| 591 |
if ( $element['type'] === 'shipping_address' ) { |
| 592 |
$data['rich_text'] = '[yaymail_shipping_address]'; |
| 593 |
return; |
| 594 |
} |
| 595 |
|
| 596 |
if ( $element['type'] === 'billing_shipping_address' ) { |
| 597 |
$billing_title = get_post_meta( $this->current_template_id, '_email_title_billing', true ); |
| 598 |
$shipping_title = get_post_meta( $this->current_template_id, '_email_title_shipping', true ); |
| 599 |
|
| 600 |
$data['billing_title'] = ! empty( $billing_title ) ? $billing_title : __( 'Billing Address', 'woocommerce' ); |
| 601 |
$data['shipping_title'] = ! empty( $shipping_title ) ? $shipping_title : __( 'Shipping Address', 'woocommerce' ); |
| 602 |
$data['shipping_address_content'] = '[yaymail_shipping_address]'; |
| 603 |
$data['billing_address_content'] = '[yaymail_billing_address]'; |
| 604 |
return; |
| 605 |
} |
| 606 |
|
| 607 |
if ( $element['type'] === 'order_details_download' ) { |
| 608 |
$data['title'] = __( 'Downloads', 'yaymail' ); |
| 609 |
$data['product_title'] = __( 'Products', 'yaymail' ); |
| 610 |
$data['expires_title'] = __( 'Expires', 'yaymail' ); |
| 611 |
$data['download_title'] = __( 'Download', 'yaymail' ); |
| 612 |
return; |
| 613 |
} |
| 614 |
|
| 615 |
if ( $element['type'] === 'column_layout' && empty( $element['children'] ) ) { |
| 616 |
$data['inner_border_radius'] = [ |
| 617 |
'top_left' => 0, |
| 618 |
'top_right' => 0, |
| 619 |
'bottom_left' => 0, |
| 620 |
'bottom_right' => 0, |
| 621 |
]; |
| 622 |
$data['inner_background_color'] = '#fff'; |
| 623 |
|
| 624 |
if ( ! empty( $data['backgroundPosition'] ) ) { |
| 625 |
$position_map = [ |
| 626 |
'unset' => 'default', |
| 627 |
'center center' => 'center_center', |
| 628 |
'center left' => 'center_left', |
| 629 |
'center right' => 'center_right', |
| 630 |
'top center' => 'top_center', |
| 631 |
'top left' => 'top_left', |
| 632 |
'top right' => 'top_right', |
| 633 |
'bottom center' => 'bottom_center', |
| 634 |
'bottom left' => 'bottom_left', |
| 635 |
'bottom right' => 'bottom_right', |
| 636 |
]; |
| 637 |
|
| 638 |
if ( isset( $position_map[ $data['backgroundPosition'] ] ) ) { |
| 639 |
$data['backgroundPosition'] = $position_map[ $data['backgroundPosition'] ]; |
| 640 |
} |
| 641 |
|
| 642 |
$position_list = array_values( $position_map ); |
| 643 |
if ( in_array( $data['backgroundPosition'], $position_list, true ) ) { |
| 644 |
$data['background_image']['position'] = $data['backgroundPosition']; |
| 645 |
} else { |
| 646 |
$data['background_image']['position'] = 'custom'; |
| 647 |
$position = explode( ' ', str_replace( '%', '', $data['backgroundPosition'] ) ); |
| 648 |
$x = $position[0]; |
| 649 |
$y = $position[1]; |
| 650 |
|
| 651 |
$data['background_image']['x_position'] = $x; |
| 652 |
$data['background_image']['y_position'] = $y; |
| 653 |
} |
| 654 |
}//end if |
| 655 |
|
| 656 |
$amount_of_columns = 1; |
| 657 |
$data['amount_of_columns'] = $amount_of_columns; |
| 658 |
|
| 659 |
if ( isset( $data['column4'] ) ) { |
| 660 |
$amount_of_columns = 4; |
| 661 |
} elseif ( isset( $data['column3'] ) ) { |
| 662 |
$amount_of_columns = 3; |
| 663 |
} elseif ( isset( $data['column2'] ) ) { |
| 664 |
$amount_of_columns = 2; |
| 665 |
} |
| 666 |
|
| 667 |
$element['children'] = ColumnLayout::get_data( $amount_of_columns )['children'] ?? []; |
| 668 |
|
| 669 |
if ( empty( $element['children'] ) ) { |
| 670 |
return; |
| 671 |
} |
| 672 |
$children =& $element['children']; |
| 673 |
if ( $amount_of_columns >= 1 && isset( $children[0] ) && isset( $data['column1'] ) ) { |
| 674 |
$column_1 = array_map( [ $this, 'convert_element' ], $data['column1'] ); |
| 675 |
$children[0]['children'] = $column_1; |
| 676 |
} |
| 677 |
if ( $amount_of_columns >= 2 && isset( $children[1] ) && isset( $data['column2'] ) ) { |
| 678 |
$column_2 = array_map( [ $this, 'convert_element' ], $data['column2'] ); |
| 679 |
$children[1]['children'] = $column_2; |
| 680 |
} |
| 681 |
if ( $amount_of_columns >= 3 && isset( $children[2] ) && isset( $data['column3'] ) ) { |
| 682 |
$column_3 = array_map( [ $this, 'convert_element' ], $data['column3'] ); |
| 683 |
$children[2]['children'] = $column_3; |
| 684 |
} |
| 685 |
if ( $amount_of_columns >= 4 && isset( $children[3] ) && isset( $data['column4'] ) ) { |
| 686 |
$column_4 = array_map( [ $this, 'convert_element' ], $data['column4'] ); |
| 687 |
$children[3]['children'] = $column_4; |
| 688 |
} |
| 689 |
|
| 690 |
return; |
| 691 |
}//end if |
| 692 |
|
| 693 |
if ( $element['type'] === 'text_list' ) { |
| 694 |
$text_list =& $data['text_list']; |
| 695 |
$border_radius_value = [ |
| 696 |
'top_left' => '5', |
| 697 |
'top_right' => '5', |
| 698 |
'bottom_right' => '5', |
| 699 |
'bottom_left' => '5', |
| 700 |
]; |
| 701 |
|
| 702 |
$columns = [ 'column_1', 'column_2', 'column_3' ]; |
| 703 |
|
| 704 |
foreach ( $columns as $column ) { |
| 705 |
if ( ! empty( $text_list[ $column ] ) ) { |
| 706 |
$text_list[ $column ]['button_border_radius'] = [ |
| 707 |
'value' => $border_radius_value, |
| 708 |
]; |
| 709 |
$text_list[ $column ]['button_height'] = [ |
| 710 |
'value' => '21', |
| 711 |
]; |
| 712 |
} |
| 713 |
} |
| 714 |
}//end if |
| 715 |
} |
| 716 |
|
| 717 |
private function migrate_shortcodes( &$data, $element_type = '' ) { |
| 718 |
$shortcodes_map = [ |
| 719 |
'[yaymail_items_border_content]' => '[yaymail_order_details]', |
| 720 |
'[yaymail_items_border_title]' => '[Order #[yaymail_order_number]] ([yaymail_order_date])', |
| 721 |
'[yaymail_items_downloadable_product]' => '[yaymail_order_details_download_product]', |
| 722 |
'[yaymail_user_account_url_string]' => '[yaymail_user_account_url]', |
| 723 |
'[yaymail_quantity_count]' => '[yaymail_order_product_item_count]', |
| 724 |
'[yaymail_orders_count]' => '[yaymail_order_product_line_item_count]', |
| 725 |
'[yaymail_orders_count_double]' => '[yaymail_order_product_line_item_count_double]', |
| 726 |
'[yaymail_order_total_numbers]' => '[yaymail_order_total_value]', |
| 727 |
'[yaymail_order_sub_total]' => '[yaymail_order_subtotal]', |
| 728 |
'[yaymail_order_shipping]' => '[yaymail_shipping_total]', |
| 729 |
'[yaymail_payment_instruction]' => '[yaymail_payment_instructions]', |
| 730 |
'[yaymail_payment_method]' => '[yaymail_order_payment_method]', |
| 731 |
'[yaymail_transaction_id]' => '[yaymail_payment_transaction_id]', |
| 732 |
'[yaymail_set_password_url_string]' => '[yaymail_set_password_url]', |
| 733 |
'[yaymail_order_payment_url_string]' => '[yaymail_order_payment_url]', |
| 734 |
|
| 735 |
// Turn the shortcode into a plain hook name |
| 736 |
'[woocommerce_email_before_order_table]' => '[yaymail_custom_hook hook="woocommerce_email_before_order_table"]', |
| 737 |
'[woocommerce_email_after_order_table]' => '[yaymail_custom_hook hook="woocommerce_email_after_order_table"]', |
| 738 |
]; |
| 739 |
|
| 740 |
if ( $element_type === 'heading' || $element_type === 'text' ) { |
| 741 |
$shortcodes_map['[yaymail_order_id]'] = '[yaymail_order_id is_plain="true"]'; |
| 742 |
$shortcodes_map['[yaymail_order_number]'] = '[yaymail_order_number is_plain="true"]'; |
| 743 |
} |
| 744 |
|
| 745 |
foreach ( $data as $key => &$value ) { |
| 746 |
if ( is_array( $value ) ) { |
| 747 |
$this->migrate_shortcodes( $value, $element_type ); |
| 748 |
continue; |
| 749 |
} |
| 750 |
|
| 751 |
foreach ( $shortcodes_map as $old => $new ) { |
| 752 |
|
| 753 |
$value = str_replace( $old, $new, $value ); |
| 754 |
} |
| 755 |
} |
| 756 |
} |
| 757 |
} |
| 758 |
|