| 1 |
<?php |
| 2 |
|
| 3 |
namespace YayMail\Migrations\Versions; |
| 4 |
|
| 5 |
use Exception; |
| 6 |
use YayMail\Migrations\AbstractMigration; |
| 7 |
use YayMail\Utils\SingletonTrait; |
| 8 |
use YayMail\YayMailTemplate; |
| 9 |
|
| 10 |
/** |
| 11 |
* Script to migrate from YayMail legacy (pre 4.0.7) to 4.0.7 |
| 12 |
*/ |
| 13 |
final class Ver_4_0_7 extends AbstractMigration { |
| 14 |
|
| 15 |
use SingletonTrait; |
| 16 |
|
| 17 |
private function __construct() { |
| 18 |
parent::__construct( '3.9.9', '4.0.7' ); |
| 19 |
} |
| 20 |
|
| 21 |
protected function up() { |
| 22 |
$this->migrate_templates(); |
| 23 |
} |
| 24 |
|
| 25 |
/** |
| 26 |
* Private functions |
| 27 |
*/ |
| 28 |
private function migrate_templates() { |
| 29 |
$this->logger->log( 'Start migrating templates to 4.0.7' ); |
| 30 |
global $wpdb; |
| 31 |
|
| 32 |
// Make sure the backup existed |
| 33 |
if ( empty( $this->backup_option_name ) || empty( get_option( $this->backup_option_name ) ) ) { |
| 34 |
throw new Exception( 'Could not find backup option' ); |
| 35 |
} |
| 36 |
|
| 37 |
$template_posts_query = " |
| 38 |
SELECT * |
| 39 |
FROM {$wpdb->posts} |
| 40 |
WHERE post_type = 'yaymail_template' |
| 41 |
"; |
| 42 |
$template_posts = $wpdb->get_results( $template_posts_query ); // phpcs:ignore |
| 43 |
if ( empty( $template_posts ) ) { |
| 44 |
$this->logger->log( 'There is no template to be migrated' ); |
| 45 |
return; |
| 46 |
} |
| 47 |
|
| 48 |
foreach ( $template_posts as $template ) { |
| 49 |
if ( empty( $template->ID ) ) { |
| 50 |
continue; |
| 51 |
} |
| 52 |
/** |
| 53 |
* ========================== |
| 54 |
* Start Elements migrations |
| 55 |
*/ |
| 56 |
|
| 57 |
$elements = get_post_meta( $template->ID, \YayMail\YayMailTemplate::META_KEYS['elements'], true ); |
| 58 |
|
| 59 |
if ( empty( $elements ) ) { |
| 60 |
continue; |
| 61 |
} |
| 62 |
|
| 63 |
$yaymail_settings = yaymail_settings(); |
| 64 |
$payment_display_mode = $yaymail_settings['payment_display_mode']; |
| 65 |
|
| 66 |
$element_text = [ |
| 67 |
'id' => uniqid(), |
| 68 |
'type' => 'text', |
| 69 |
'name' => 'Text', |
| 70 |
'available' => true, |
| 71 |
'data' => [ |
| 72 |
'padding' => [ |
| 73 |
'top' => '15', |
| 74 |
'right' => '50', |
| 75 |
'bottom' => '15', |
| 76 |
'left' => '50', |
| 77 |
], |
| 78 |
'background_color' => '#fff', |
| 79 |
'text_color' => '#636363', |
| 80 |
'font_family' => 'Helvetica,Roboto,Arial,sans-serif', |
| 81 |
'rich_text' => '[yaymail_payment_instructions]', |
| 82 |
], |
| 83 |
]; |
| 84 |
|
| 85 |
if ( $payment_display_mode === 'yes' ) { |
| 86 |
$has_payment_instructions = false; |
| 87 |
$order_details_index = null; |
| 88 |
|
| 89 |
foreach ( $elements as $key => $element ) { |
| 90 |
// Check if the element contains the payment instructions shortcode |
| 91 |
if ( strpos( $element['data']['rich_text'] ?? '', '[yaymail_payment_instructions]' ) !== false ) { |
| 92 |
$has_payment_instructions = true; |
| 93 |
break; |
| 94 |
// Shortcode already exists, no further action needed |
| 95 |
} |
| 96 |
|
| 97 |
// Store the position of the 'order_details' element (only the first match) |
| 98 |
if ( $order_details_index === null && ( $element['type'] ?? '' ) === 'order_details' ) { |
| 99 |
$order_details_index = $key; |
| 100 |
} |
| 101 |
} |
| 102 |
|
| 103 |
// If the shortcode was not found and we know where to insert, do it now |
| 104 |
if ( ! $has_payment_instructions && $order_details_index !== null ) { |
| 105 |
array_splice( $elements, $order_details_index, 0, [ $element_text ] ); |
| 106 |
} |
| 107 |
}//end if |
| 108 |
|
| 109 |
update_post_meta( $template->ID, \YayMail\YayMailTemplate::META_KEYS['elements'], $elements ); |
| 110 |
|
| 111 |
/** |
| 112 |
* Finish Template settings migrations |
| 113 |
* ========================== |
| 114 |
*/ |
| 115 |
|
| 116 |
}//end foreach |
| 117 |
$this->logger->log( 'Done migrating templates to 4.0.7' ); |
| 118 |
} |
| 119 |
} |
| 120 |
|