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