Tables
1 year ago
CartLineItemDetailsMigrationService.php
6 days ago
GeneralMigration.php
1 year ago
MigrationsServiceProvider.php
6 days ago
ProductPageMigrationService.php
1 year ago
RewriteRulesMigrationService.php
1 year ago
Table.php
4 years ago
ThemeMigrationService.php
4 months ago
UpdateMigrationServiceProvider.php
1 month ago
UserMetaMigrationsService.php
4 years ago
VersionMigration.php
1 year ago
WebhookMigrationsService.php
1 year ago
CartLineItemDetailsMigrationService.php
214 lines
| 1 | <?php |
| 2 | |
| 3 | namespace SureCart\Database; |
| 4 | |
| 5 | /** |
| 6 | * Migrates saved cart templates: wraps the standalone `cart-line-item-variant` |
| 7 | * block inside the new `cart-line-item-details` container so bundle line-items |
| 8 | * get the collapsible "+N more" region on upgrade. |
| 9 | * |
| 10 | * The `cart-line-item-note` block is intentionally left standalone — in |
| 11 | * released carts it already sits right after the variant, so once the variant |
| 12 | * is wrapped the note naturally ends up after the details container. |
| 13 | */ |
| 14 | class CartLineItemDetailsMigrationService extends VersionMigration { |
| 15 | /** |
| 16 | * The key for the migration. |
| 17 | * |
| 18 | * @var string |
| 19 | */ |
| 20 | protected $migration_key = 'surecart_cart_line_item_details_migration_version'; |
| 21 | |
| 22 | /** |
| 23 | * Whether any post update failed during the run. When true we skip marking |
| 24 | * the migration complete so it retries on the next admin_init. |
| 25 | * |
| 26 | * @var bool |
| 27 | */ |
| 28 | protected $failed = false; |
| 29 | |
| 30 | /** |
| 31 | * Default variant attributes — matches the default cart template. |
| 32 | * |
| 33 | * @var string |
| 34 | */ |
| 35 | const DEFAULT_CHILD_ATTRS = '{"style":{"typography":{"fontSize":"14px","lineHeight":"1.4"}}}'; |
| 36 | |
| 37 | /** |
| 38 | * Matches the details container form: opener (with or without attrs), |
| 39 | * inner blocks, closer. Its presence means the cart is already migrated. |
| 40 | * |
| 41 | * @var string |
| 42 | */ |
| 43 | const DETAILS_WRAPPER_PATTERN = '/<!--\s*wp:surecart\/cart-line-item-details\b.*?-->.*?<!--\s*\/wp:surecart\/cart-line-item-details\s*-->/s'; |
| 44 | |
| 45 | /** |
| 46 | * Matches the standalone self-closing variant block the container now owns. |
| 47 | * |
| 48 | * @var string |
| 49 | */ |
| 50 | const VARIANT_BLOCK_PATTERN = '/[ \t]*<!--\s*wp:surecart\/cart-line-item-variant\b.*?\/-->\n?/s'; |
| 51 | |
| 52 | /** |
| 53 | * Run the migration. |
| 54 | * |
| 55 | * @return void |
| 56 | */ |
| 57 | public function run(): void { |
| 58 | foreach ( $this->getCartPosts() as $cart_post ) { |
| 59 | $content = $cart_post->post_content ?? ''; |
| 60 | if ( empty( $content ) ) { |
| 61 | continue; |
| 62 | } |
| 63 | |
| 64 | $migrated = $this->consolidateDetails( $content ); |
| 65 | if ( $migrated === $content ) { |
| 66 | continue; |
| 67 | } |
| 68 | |
| 69 | $result = wp_update_post( |
| 70 | array( |
| 71 | 'ID' => $cart_post->ID, |
| 72 | 'post_content' => $migrated, |
| 73 | ), |
| 74 | true |
| 75 | ); |
| 76 | |
| 77 | // Leave the migration unmarked (see complete()) so a failed update |
| 78 | // retries next admin_init instead of being stranded on the old markup. |
| 79 | if ( is_wp_error( $result ) || empty( $result ) ) { |
| 80 | $this->failed = true; |
| 81 | } |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | /** |
| 86 | * Only mark the migration complete when every cart updated cleanly. |
| 87 | * |
| 88 | * @return void |
| 89 | */ |
| 90 | public function complete() { |
| 91 | if ( $this->failed ) { |
| 92 | return; |
| 93 | } |
| 94 | parent::complete(); |
| 95 | } |
| 96 | |
| 97 | /** |
| 98 | * Collect the cart posts to migrate. |
| 99 | * |
| 100 | * Covers both storage locations: |
| 101 | * - wp_template_part with post_name 'cart' (newer installs). |
| 102 | * - sc_cart post type (legacy installs whose cart hasn't been moved yet). |
| 103 | * |
| 104 | * @return array |
| 105 | */ |
| 106 | protected function getCartPosts(): array { |
| 107 | $cart_part_query = new \WP_Query( |
| 108 | array( |
| 109 | 'post_type' => 'wp_template_part', |
| 110 | 'post_status' => array( 'auto-draft', 'draft', 'publish' ), |
| 111 | 'posts_per_page' => -1, |
| 112 | 'name' => 'cart', |
| 113 | 'lazy_load_term_meta' => false, |
| 114 | ) |
| 115 | ); |
| 116 | |
| 117 | $sc_cart_query = new \WP_Query( |
| 118 | array( |
| 119 | 'post_type' => 'sc_cart', |
| 120 | 'post_status' => array( 'auto-draft', 'draft', 'publish' ), |
| 121 | 'posts_per_page' => -1, |
| 122 | 'lazy_load_term_meta' => false, |
| 123 | ) |
| 124 | ); |
| 125 | |
| 126 | return array_merge( $cart_part_query->posts ?? array(), $sc_cart_query->posts ?? array() ); |
| 127 | } |
| 128 | |
| 129 | /** |
| 130 | * Wrap the standalone variant block in a details container, preserving the |
| 131 | * merchant's variant styling. The note is left untouched. |
| 132 | * |
| 133 | * Deterministic, so re-running on migrated content is a no-op. |
| 134 | * |
| 135 | * @param string $content Post content. |
| 136 | * @return string Migrated content (unchanged if nothing matched). |
| 137 | */ |
| 138 | protected function consolidateDetails( string $content ): string { |
| 139 | // Idempotent: a details container already present means the cart is in |
| 140 | // the target shape (container owns the variant; note stays standalone). |
| 141 | if ( preg_match( self::DETAILS_WRAPPER_PATTERN, $content ) ) { |
| 142 | return $content; |
| 143 | } |
| 144 | |
| 145 | // Preserve the existing variant attributes (merchant styling). |
| 146 | $variant_attrs = $this->blockAttrs( $content, 'cart-line-item-variant' ) ?? self::DEFAULT_CHILD_ATTRS; |
| 147 | |
| 148 | $details = "<!-- wp:surecart/cart-line-item-details -->\n" |
| 149 | . '<!-- wp:surecart/cart-line-item-variant ' . $variant_attrs . " /-->\n" |
| 150 | . '<!-- /wp:surecart/cart-line-item-details -->'; |
| 151 | |
| 152 | // Wrap the first standalone variant in place. The note (a separate |
| 153 | // standalone block) is deliberately not matched, so it stays exactly |
| 154 | // where it is — after the variant, hence after the new container. |
| 155 | if ( preg_match( self::VARIANT_BLOCK_PATTERN, $content ) ) { |
| 156 | $migrated = preg_replace( self::VARIANT_BLOCK_PATTERN, $details . "\n", $content, 1 ); |
| 157 | return is_string( $migrated ) ? $migrated : $content; |
| 158 | } |
| 159 | |
| 160 | // No variant block at all: inject the container before the first |
| 161 | // sensible anchor so bundle items still show for older carts. Prefer |
| 162 | // the note so the container lands before it (details, then note). |
| 163 | $anchor = $this->firstExistingAnchor( |
| 164 | $content, |
| 165 | array( |
| 166 | 'surecart/cart-line-item-note', |
| 167 | 'surecart/cart-line-item-status', |
| 168 | 'surecart/cart-line-item-quantity', |
| 169 | 'surecart/cart-line-item-remove', |
| 170 | ) |
| 171 | ); |
| 172 | |
| 173 | if ( ! $anchor ) { |
| 174 | return $content; |
| 175 | } |
| 176 | |
| 177 | return str_replace( |
| 178 | '<!-- wp:' . $anchor, |
| 179 | $details . "\n" . '<!-- wp:' . $anchor, |
| 180 | $content |
| 181 | ); |
| 182 | } |
| 183 | |
| 184 | /** |
| 185 | * Get the attribute JSON of the first occurrence of a self-closing block. |
| 186 | * |
| 187 | * @param string $content Post content to scan. |
| 188 | * @param string $block Block name without the `surecart/` prefix. |
| 189 | * @return string|null Attribute JSON, or null when absent/attribute-less. |
| 190 | */ |
| 191 | protected function blockAttrs( string $content, string $block ): ?string { |
| 192 | if ( preg_match( '/<!--\s*wp:surecart\/' . preg_quote( $block, '/' ) . '\s+(\{.*?\})\s*\/-->/s', $content, $matches ) ) { |
| 193 | return $matches[1]; |
| 194 | } |
| 195 | return null; |
| 196 | } |
| 197 | |
| 198 | /** |
| 199 | * Pick the first anchor block from a preferred list that exists in the content. |
| 200 | * |
| 201 | * @param string $content Post content to scan. |
| 202 | * @param array $candidates Block names, in order of preference. |
| 203 | * @return string|null Block name to anchor against, or null if none match. |
| 204 | */ |
| 205 | protected function firstExistingAnchor( string $content, array $candidates ): ?string { |
| 206 | foreach ( $candidates as $block_name ) { |
| 207 | if ( has_block( $block_name, $content ) ) { |
| 208 | return $block_name; |
| 209 | } |
| 210 | } |
| 211 | return null; |
| 212 | } |
| 213 | } |
| 214 |