PluginProbe
YayMail – WooCommerce Email Customizer / 4.4.7
YayMail – WooCommerce Email Customizer v4.4.7
4.4.7 4.4.6 4.4.5 4.4.4 4.4.3 4.4.2 4.4.1 trunk 1.9.6 2.1.4 2.1.5 3.2.2 3.2.6 3.2.7.1 3.2.8.1 3.2.9 3.3 3.3.1 3.3.4 3.3.5 3.3.6 3.3.7 3.3.8 3.3.9 3.4 All 58 releases
yaymail / src / Migrations / AbstractMigration.php

AbstractMigration.php in YayMail – WooCommerce Email Customizer 4.4.7, at src/Migrations/AbstractMigration.php

222 lines 7.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 namespace YayMail\Migrations;
3
4 use YayMail\SupportedPlugins;
5 use YayMail\Utils\Logger;
6 use YayMail\Utils\SafeUnserializer;
7 use YayMail\YayMailTemplate;
8
9 /**
10 * AbstractMigration
11 */
12 abstract class AbstractMigration {
13
14 const BACKUP_PREFIX = '_yaymail_backup_';
15 const SUCCESSFUL_MIGRATIONS = '_yaymail_successful_migrations';
16 /**
17 * @var string $old_version
18 * @Example '3.9.9'
19 */
20 protected $old_version;
21
22 /**
23 * @var string $new_version
24 * @Example '4.0.0'
25 */
26 protected $new_version;
27
28 /** @var Logger */
29 protected $logger;
30
31 /**
32 * @var string $backup_option_name
33 */
34 protected $backup_option_name;
35
36 /**
37 * Addon Namespace, empty means core
38 *
39 * @var string $addon_namespace
40 */
41 protected $addon_namespace = '';
42
43
44
45 abstract protected function up();
46
47 protected function __construct( $old_version, $new_version, $addon_namespace = '' ) {
48 $this->old_version = $old_version;
49 $this->new_version = $new_version;
50
51 $this->addon_namespace = $addon_namespace;
52
53 $this->logger = new Logger();
54 }
55
56 public function perform_migration( $skip_check_migration = false ) {
57 $this->logger->log( "Attempt to migrate data from [$this->old_version] to [$this->new_version]." );
58
59 if ( ! $skip_check_migration && $this->has_migration_been_performed() ) {
60 $this->logger->log( 'Migration aborted. Data had been successfully migrated before.' );
61 return;
62 }
63
64 $this->backup();
65
66 $this->up();
67
68 if ( ! $skip_check_migration ) {
69 $this->log_succeeded_migration();
70 }
71 }
72
73
74 protected function backup() {
75 global $wpdb;
76
77 $all_backups = $this->get_all_backups();
78
79 $prefix = self::BACKUP_PREFIX . ( ! empty( $this->addon_namespace ) ? $this->addon_namespace . '_' : '' );
80 $this->backup_option_name = $prefix . str_replace( '.', '_', $this->old_version );
81
82 foreach ( $all_backups as $backup_name => $backup ) {
83 if ( $backup_name === $this->backup_option_name
84 || MigrationHelper::is_within_time_window( $backup['created_date'], 60 ) ) {
85
86 // Set this backup name as the current backup option name
87 // In order to let the log_succeeded_migration() know which backup to use
88 $this->backup_option_name = $backup_name;
89
90 // No need for new backup
91 $this->logger->log( "Back up [$this->backup_option_name] already existed or there is new backup added recently. No more backup needed" );
92 return;
93 }
94 }
95
96 /**
97 * Backup posts and postmeta
98 */
99 $query_posts = "
100 SELECT *
101 FROM {$wpdb->posts}
102 WHERE post_type = 'yaymail_template'
103 ";
104 $yaymail_template_posts = $wpdb->get_results( $query_posts );// phpcs:ignore
105
106 $query_postmeta = "
107 SELECT *
108 FROM {$wpdb->postmeta}
109 WHERE meta_key LIKE '%yaymail%'
110 ";
111 $yaymail_template_postmeta = $wpdb->get_results( $query_postmeta );// phpcs:ignore
112
113 $backup_data = [
114 'posts' => $yaymail_template_posts,
115 'postmeta' => $yaymail_template_postmeta,
116 ];
117 /** ****************************** */
118
119 /**
120 * Backup options
121 */
122 $query_options = "
123 SELECT *
124 FROM {$wpdb->options}
125 WHERE option_name LIKE '%yaymail%'
126 ";
127 $yaymail_options = $wpdb->get_results( $query_options ); // phpcs:ignore
128 $backup_data['options'] = $yaymail_options;
129
130 $backup_data['created_date'] = current_datetime()->format( 'Y-m-d H:i:s' );
131
132 $backup_data = apply_filters( 'yaymail_migration_backup_data', $backup_data );
133 /** ****************************** */
134
135 $backup_response = update_option( $this->backup_option_name, $backup_data );
136
137 if ( ! $backup_response ) {
138 // When update_option failes, throw exception
139 throw new \Exception( 'YayMail failed to save backup option' );
140 }
141 $this->logger->log( $this->backup_option_name . ' saved!' );
142 }
143
144 /**
145 * Log succeeded migration to the db
146 */
147 protected function log_succeeded_migration() {
148 $successful_migrations = get_option( self::SUCCESSFUL_MIGRATIONS, [] );
149
150 $current_migration = [
151 'created_date' => current_datetime()->format( 'Y-m-d H:i:s' ),
152 'from_version' => $this->old_version,
153 'to_version' => $this->new_version,
154 'backup_name' => $this->backup_option_name,
155 ];
156
157 if ( ! empty( $this->addon_namespace ) ) {
158 $current_migration['addon_namespace'] = $this->addon_namespace;
159 }
160
161 array_unshift( $successful_migrations, $current_migration );
162
163 update_option( self::SUCCESSFUL_MIGRATIONS, $successful_migrations );
164 }
165
166 protected function has_migration_been_performed() {
167 $successful_migrations = get_option( self::SUCCESSFUL_MIGRATIONS, [] );
168
169 foreach ( $successful_migrations as $migration ) {
170
171 if ( version_compare( $migration['to_version'], $this->new_version, '<' ) ) {
172 continue;
173 }
174
175 if ( empty( $this->addon_namespace ) ) {
176 if ( empty( $migration['addon_namespace'] ) ) {
177 return true;
178 }
179 } elseif ( $migration['addon_namespace'] === $this->addon_namespace ) {
180 return true;
181 }
182 }
183
184 return false;
185 }
186
187 protected function get_all_backups() {
188 global $wpdb;
189
190 $options = $wpdb->get_results(
191 $wpdb->prepare(
192 "SELECT option_name, option_value FROM {$wpdb->options} WHERE option_name LIKE %s",
193 $wpdb->esc_like( self::BACKUP_PREFIX ) . '%'
194 ),
195 ARRAY_A
196 );
197 $formatted_options = [];
198 foreach ( $options as $option ) {
199 $formatted_options[ $option['option_name'] ] = SafeUnserializer::decode_backup( $option['option_value'] );
200 }
201 return $formatted_options;
202 }
203
204 protected function may_mark_template_as_v4_supported( string $template_post_id, $migration_manager = null ): void {
205 if ( ! isset( $migration_manager ) ) {
206 $supported_template_ids = SupportedPlugins::get_instance()->get_template_ids_from_core();
207 } else {
208 $supported_template_ids = $migration_manager->get_supported_template_ids();
209 }
210
211 if ( empty( $template_post_id ) || empty( $supported_template_ids ) ) {
212 return;
213 }
214
215 $template_name = get_post_meta( $template_post_id, YayMailTemplate::META_KEYS['name'], true );
216
217 if ( in_array( $template_name, $supported_template_ids, true ) ) {
218 update_post_meta( $template_post_id, YayMailTemplate::META_KEYS['is_v4_supported'], true );
219 }
220 }
221 }
222