| 1 |
<?php |
| 2 |
|
| 3 |
namespace YayMail\Migrations\Versions; |
| 4 |
|
| 5 |
use Exception; |
| 6 |
use YayMail\Migrations\AbstractMigration; |
| 7 |
use YayMail\Models\SettingModel; |
| 8 |
use YayMail\Utils\SingletonTrait; |
| 9 |
use YayMail\YayMailTemplate; |
| 10 |
|
| 11 |
/** |
| 12 |
* Script to migrate from YayMail legacy (pre 4.0.7) to 4.0.7 |
| 13 |
*/ |
| 14 |
final class Ver_4_1_0 extends AbstractMigration { |
| 15 |
|
| 16 |
use SingletonTrait; |
| 17 |
|
| 18 |
private function __construct() { |
| 19 |
parent::__construct( '4.0.9', '4.1.0' ); |
| 20 |
} |
| 21 |
|
| 22 |
protected function up() { |
| 23 |
$this->migrate_templates(); |
| 24 |
} |
| 25 |
|
| 26 |
/** |
| 27 |
* Private functions |
| 28 |
*/ |
| 29 |
private function migrate_templates() { |
| 30 |
$this->logger->log( 'Start migrating templates to 4.1.0' ); |
| 31 |
global $wpdb; |
| 32 |
|
| 33 |
// Make sure the backup existed |
| 34 |
if ( empty( $this->backup_option_name ) || empty( get_option( $this->backup_option_name ) ) ) { |
| 35 |
throw new Exception( 'Could not find backup option' ); |
| 36 |
} |
| 37 |
|
| 38 |
$template_posts_query = " |
| 39 |
SELECT * |
| 40 |
FROM {$wpdb->posts} |
| 41 |
WHERE post_type = 'yaymail_template' |
| 42 |
"; |
| 43 |
$template_posts = $wpdb->get_results( $template_posts_query ); // phpcs:ignore |
| 44 |
if ( empty( $template_posts ) ) { |
| 45 |
$this->logger->log( 'There is no template to be migrated' ); |
| 46 |
return; |
| 47 |
} |
| 48 |
|
| 49 |
$has_global_header_foter_in_templates = false; |
| 50 |
$templates_need_to_hide_header = []; |
| 51 |
$templates_need_to_hide_footer = []; |
| 52 |
|
| 53 |
$default_template_global_header_settings = YayMailTemplate::DEFAULT_DATA['global_header_settings']; |
| 54 |
$default_template_global_footer_settings = YayMailTemplate::DEFAULT_DATA['global_footer_settings']; |
| 55 |
|
| 56 |
foreach ( $template_posts as $template ) { |
| 57 |
if ( empty( $template->ID ) ) { |
| 58 |
continue; |
| 59 |
} |
| 60 |
/** |
| 61 |
* ========================== |
| 62 |
* Start Elements migrations |
| 63 |
*/ |
| 64 |
|
| 65 |
$elements = get_post_meta( $template->ID, YayMailTemplate::META_KEYS['elements'], true ); |
| 66 |
|
| 67 |
if ( empty( $elements ) ) { |
| 68 |
continue; |
| 69 |
} |
| 70 |
|
| 71 |
$has_global_header_in_template = false; |
| 72 |
$has_global_footer_in_template = false; |
| 73 |
|
| 74 |
$template_global_header_override_content = null; |
| 75 |
$template_global_header_settings = get_post_meta( $template->ID, YayMailTemplate::META_KEYS['global_header_settings'], true ); |
| 76 |
$template_global_footer_settings = get_post_meta( $template->ID, YayMailTemplate::META_KEYS['global_footer_settings'], true ); |
| 77 |
|
| 78 |
$legacy_default_global_header_override_content = '<h1 style="font-size: 30px; font-weight: 300; line-height: normal; margin: 0px; color: inherit; text-align: left;">Email Heading</h1>'; |
| 79 |
|
| 80 |
if ( empty( $template_global_footer_settings ) ) { |
| 81 |
$template_global_footer_settings = $default_template_global_footer_settings; |
| 82 |
} |
| 83 |
if ( empty( $template_global_header_settings ) ) { |
| 84 |
$template_global_header_settings = $default_template_global_header_settings; |
| 85 |
} |
| 86 |
|
| 87 |
foreach ( $elements as $key => $element ) { |
| 88 |
if ( $element['type'] !== 'global_header' && $element['type'] !== 'global_footer' ) { |
| 89 |
continue; |
| 90 |
} |
| 91 |
if ( $element['type'] === 'global_header' ) { |
| 92 |
$has_global_header_in_template = true; |
| 93 |
if ( $legacy_default_global_header_override_content !== $element['data']['rich_text'] ) { |
| 94 |
$template_global_header_override_content = $element['data']['rich_text']; |
| 95 |
} |
| 96 |
} |
| 97 |
if ( $element['type'] === 'global_footer' ) { |
| 98 |
$has_global_footer_in_template = true; |
| 99 |
} |
| 100 |
} |
| 101 |
|
| 102 |
if ( $has_global_header_in_template || $has_global_footer_in_template ) { |
| 103 |
$has_global_header_foter_in_templates = true; |
| 104 |
} |
| 105 |
|
| 106 |
if ( ! $has_global_header_in_template ) { |
| 107 |
$templates_need_to_hide_header[] = $template->ID; |
| 108 |
} elseif ( ! empty( $template_global_header_override_content ) ) { |
| 109 |
$template_global_header_settings['content_override'] = true; |
| 110 |
$template_global_header_settings['heading_content'] = $template_global_header_override_content; |
| 111 |
} |
| 112 |
|
| 113 |
if ( ! $has_global_footer_in_template ) { |
| 114 |
$templates_need_to_hide_footer[] = $template->ID; |
| 115 |
} |
| 116 |
|
| 117 |
update_post_meta( $template->ID, YayMailTemplate::META_KEYS['global_header_settings'], $template_global_header_settings ); |
| 118 |
update_post_meta( $template->ID, YayMailTemplate::META_KEYS['global_footer_settings'], $template_global_footer_settings ); |
| 119 |
|
| 120 |
/** |
| 121 |
* Finish Template settings migrations |
| 122 |
* ========================== |
| 123 |
*/ |
| 124 |
|
| 125 |
}//end foreach |
| 126 |
|
| 127 |
if ( $has_global_header_foter_in_templates ) { |
| 128 |
SettingModel::update( |
| 129 |
[ |
| 130 |
'global_header_footer_enabled' => true, |
| 131 |
] |
| 132 |
); |
| 133 |
foreach ( $templates_need_to_hide_header as $template_id ) { |
| 134 |
$template_global_header_settings = get_post_meta( $template->ID, YayMailTemplate::META_KEYS['global_header_settings'], true ); |
| 135 |
if ( empty( $template_global_footer_settings ) ) { |
| 136 |
$template_global_footer_settings = $default_template_global_footer_settings; |
| 137 |
} |
| 138 |
$template_global_header_settings['hidden'] = true; |
| 139 |
update_post_meta( $template_id, YayMailTemplate::META_KEYS['global_header_settings'], $template_global_header_settings ); |
| 140 |
} |
| 141 |
foreach ( $templates_need_to_hide_footer as $template_id ) { |
| 142 |
$template_global_footer_settings = get_post_meta( $template->ID, YayMailTemplate::META_KEYS['global_footer_settings'], true ); |
| 143 |
if ( empty( $template_global_header_settings ) ) { |
| 144 |
$template_global_header_settings = $default_template_global_header_settings; |
| 145 |
} |
| 146 |
$template_global_footer_settings['hidden'] = true; |
| 147 |
update_post_meta( $template_id, YayMailTemplate::META_KEYS['global_footer_settings'], $template_global_footer_settings ); |
| 148 |
} |
| 149 |
}//end if |
| 150 |
|
| 151 |
$this->logger->log( 'Done migrating templates to 4.0.7' ); |
| 152 |
} |
| 153 |
} |
| 154 |
|