Tables
1 year ago
GeneralMigration.php
1 year ago
MigrationsServiceProvider.php
1 year ago
ProductPageMigrationService.php
1 year ago
RewriteRulesMigrationService.php
1 year ago
Table.php
3 years ago
UpdateMigrationServiceProvider.php
2 years ago
UserMetaMigrationsService.php
3 years ago
VersionMigration.php
1 year ago
WebhookMigrationsService.php
1 year ago
ProductPageMigrationService.php
83 lines
| 1 | <?php |
| 2 | |
| 3 | namespace SureCart\Database; |
| 4 | |
| 5 | /** |
| 6 | * Run this migration when version changes or for new installations. |
| 7 | */ |
| 8 | class ProductPageMigrationService extends VersionMigration { |
| 9 | /** |
| 10 | * The key for the migration. |
| 11 | * |
| 12 | * @var string |
| 13 | */ |
| 14 | protected $migration_key = 'surecart_product_page_migration_version'; |
| 15 | |
| 16 | /** |
| 17 | * Run the migration. |
| 18 | * |
| 19 | * @return void |
| 20 | */ |
| 21 | public function run(): void { |
| 22 | |
| 23 | $args = array( |
| 24 | 'post_type' => array( 'wp_template_part', 'wp_template' ), // specify the post types. |
| 25 | 'post_status' => array( 'auto-draft', 'draft', 'publish' ), // specify the post status. |
| 26 | 'posts_per_page' => -1, |
| 27 | 'lazy_load_term_meta' => false, |
| 28 | ); |
| 29 | |
| 30 | $query = new \WP_Query( $args ); |
| 31 | $templates = $query->posts ?? array(); |
| 32 | |
| 33 | if ( empty( $templates ) ) { |
| 34 | return; |
| 35 | } |
| 36 | |
| 37 | // filter out the templates that have post_name as product. |
| 38 | $product_templates = array_filter( |
| 39 | $templates, |
| 40 | function ( $template ) { |
| 41 | return in_array( $template->post_name, array( 'product-info', 'single-product' ) ) || |
| 42 | strpos( $template->post_name, 'sc-part-products-info-' ) !== false || |
| 43 | strpos( $template->post_name, 'sc-products-' ) !== false; |
| 44 | } |
| 45 | ); |
| 46 | |
| 47 | // if we don't have any product templates, return. |
| 48 | if ( empty( $product_templates ) ) { |
| 49 | return; |
| 50 | } |
| 51 | |
| 52 | // update each template if it doesn't have the wp:surecart/product-page block. |
| 53 | foreach ( $product_templates as $product_template ) { |
| 54 | |
| 55 | if ( ! has_block( 'surecart/product-page', $product_template->post_content ) ) { |
| 56 | wp_update_post( |
| 57 | array( |
| 58 | 'ID' => $product_template->ID, |
| 59 | 'post_content' => '<!-- wp:surecart/product-page {"align":"wide"} -->' . $product_template->post_content . '<!-- /wp:surecart/product-page -->', |
| 60 | ) |
| 61 | ); |
| 62 | } |
| 63 | |
| 64 | if ( ! has_block( 'surecart/product-selected-price-ad-hoc-amount', $product_template->post_content ) ) { |
| 65 | $insert_before_block = has_block( 'surecart/product-buy-buttons', $product_template ) ? 'surecart/product-buy-buttons' : 'surecart/product-buy-button'; |
| 66 | |
| 67 | $new_content = str_replace( |
| 68 | '<!-- wp:' . $insert_before_block, |
| 69 | '<!-- wp:surecart/product-selected-price-ad-hoc-amount /-->' . PHP_EOL . '<!-- wp:' . $insert_before_block, |
| 70 | $product_template->post_content |
| 71 | ); |
| 72 | |
| 73 | wp_update_post( |
| 74 | array( |
| 75 | 'ID' => $product_template->ID, |
| 76 | 'post_content' => $new_content, |
| 77 | ) |
| 78 | ); |
| 79 | } |
| 80 | } |
| 81 | } |
| 82 | } |
| 83 |